Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dev/core#4144 Fix smarty notice on isDuplicate #25657

Merged
merged 3 commits into from
Mar 11, 2023

Conversation

eileenmcnaughton
Copy link
Contributor

@eileenmcnaughton eileenmcnaughton commented Feb 23, 2023

Overview

dev/core#4144 Fix smarty notice on isDuplicate

Before

image

After

image

Technical Details

It was really hard to figure out the point of this variable / how it was reached. Most of the change is code commentary to try to make that clearer

The way isDuplicate comes into play is that duplicate checking is done if (& only if) the profile is used in the context of creating a new (e.g) individual from an entity reference field

Comments

@civibot
Copy link

civibot bot commented Feb 23, 2023

No issue was found matching the number given in the pull request title. Please check the issue number.

@civibot
Copy link

civibot bot commented Feb 23, 2023

(Standard links)

@civibot civibot bot added the master label Feb 23, 2023
* Name of button for saving matching contacts.
* @var string
*/
protected $_duplicateButtonName;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this just muddies the waters using a property

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, strange choice

@eileenmcnaughton
Copy link
Contributor Author

This passed but I rebased to keep it fresh - seems like an infra issue test this please

Copy link
Contributor

@highfalutin highfalutin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The template could use one more little change to simplify it further

* Name of button for saving matching contacts.
* @var string
*/
protected $_duplicateButtonName;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, strange choice

@@ -261,6 +257,18 @@ protected static function handleDuplicateChecking(&$errors, $fields, $form) {
return $errors;
}

/**
* Is this being called from an entity reference field.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

helpful explanation

@@ -35,7 +35,7 @@
<div id="crm-container" class="crm-container crm-public" lang="{$config->lcMessages|truncate:2:"":true}" xml:lang="{$config->lcMessages|truncate:2:"":true}">
{/if}

{if $isDuplicate and ( ($action eq 1 and $mode eq 4 ) or ($action eq 2) or ($action eq 8192) ) }
{if array_key_exists('_qf_Edit_upload_duplicate', $form) && $isDuplicate}
<div class="crm-submit-buttons">
{$form._qf_Edit_upload_duplicate.html}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems odd that we hard-code the button name here, and go to the trouble to generate it dynamically in Form.php... but if it works, fine

@@ -35,7 +35,7 @@
<div id="crm-container" class="crm-container crm-public" lang="{$config->lcMessages|truncate:2:"":true}" xml:lang="{$config->lcMessages|truncate:2:"":true}">
{/if}

{if $isDuplicate and ( ($action eq 1 and $mode eq 4 ) or ($action eq 2) or ($action eq 8192) ) }
{if array_key_exists('_qf_Edit_upload_duplicate', $form) && $isDuplicate}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this not change the logic?

($action eq 1 and $mode eq 4 ): ADD and MODE_CREATE
$action eq 2: UPDATE
$action eq 8192: PROFILE

I can't grok why we need to test for these; $isDuplicate should be enough, I'd think.

And array_key_exists('_qf_Edit_upload_duplicate', $form) should always be TRUE.

So can we just change this to {if $isDuplicate}?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@highfalutin the reason for the button check is that the button is ONLY there under a very narrow situation - if you use an entity reference field & choose (e.g) new Organization AND choose an organization that exists THEN the button appears

@highfalutin
Copy link
Contributor

Now trying to do a run, but I can't find where to trigger these notices. What form should I go to?

@eileenmcnaughton
Copy link
Contributor Author

@highfalutin they occur when using profiles - I'm not sure how intolerant your set up has to be but I saw another php 8.x user try to fix it & I definitely see it when I turn on smarty hardening - https://docs.civicrm.org/dev/en/latest/security/outputs/#html

@highfalutin
Copy link
Contributor

OK, for me the notices only show up if I view the form in its own tab, instead of as a modal.

Smarty hardening doesn't need to be turned on for the notices to appear.

Side note: I was a bit confused by the mention of "entity reference" -- the special "New Individual" and "New Organization" links don't appear on Entity Reference/Contact Reference custom fields, only on built-in fields built with CRM_Core_Form::addEntityRef()... but anyway that turns out to be a red herring. The smarty notices actually seem to appear on any profile used in standalone Create/Edit mode, it doesn't really matter how you get to them.

Before

Open a profile edit page like civicrm/profile/create&reset=1&gid=xxxxx where xxxxx is the id of any profile which can be used in standalone create/edit mode.

Several notices appear, including:

Warning: Undefined array key "isDuplicate" in /path/to/civi.files/templates_c/en_US/%%0A/0AF/0AF6F87E%%Dynamic.tpl.php on line 35
Warning: Undefined array key "isDuplicate" in /path/to/civi.files/templates_c/en_US/%%0A/0AF/0AF6F87E%%Dynamic.tpl.php on line 86

The same notices appear when &context=dialog is added to the url, telling Civi that the output is intended for a modal dialog box.

After

In dialog context, both notices disappear. In non-dialog context, the notice about line 35 disappears but the one about line 86 remains.

To get both lines to disappear in both contexts:

diff --git a/CRM/Profile/Form.php b/CRM/Profile/Form.php
index 5bb2cf99d8..e8c8d9bdc4 100644
--- a/CRM/Profile/Form.php
+++ b/CRM/Profile/Form.php
@@ -904,10 +904,11 @@ class CRM_Profile_Form extends CRM_Core_Form {
       $this->freeze();
     }

+    // Assign FALSE, here - this is overwritten during form validation
+    // if duplicates are found during submit.
+    CRM_Core_Smarty::singleton()->assign('isDuplicate', FALSE);
+
     if ($this->isEntityReferenceContactCreateMode()) {
-      // Assign FALSE, here - this is overwritten during form validation
-      // if duplicates are found during submit.
-      CRM_Core_Smarty::singleton()->assign('isDuplicate', FALSE);
       $this->addElement(
         'xbutton',
         $this->getButtonName('upload', 'duplicate'),

I also took out the array_key_exists('_qf_Edit_upload_duplicate', $form) && from the template, and confirmed that everything works fine without it. $isDuplicate should be renamed to $showSaveDuplicateButton because that's really what it's about.

@eileenmcnaughton
Copy link
Contributor Author

Thanks @highfalutin - I'll make the changes you suggest once I have fulfilled my chauffeur duties...

@highfalutin
Copy link
Contributor

just made a PR to your PR 😆

fix notice in non-dialog mode too, make var name clearer, fix another notice
@eileenmcnaughton
Copy link
Contributor Author

OH wow - I got sooo confused - I thought your PR was against master & then I thought this must be already merged but then it wasn't and then my son sang 'I'm Slim Shady' & my brain broke

@eileenmcnaughton
Copy link
Contributor Author

I've given the merge-on-pass based on the review / collaboration from @highfalutin

@highfalutin
Copy link
Contributor

@Eileen we'll have to credit Eminem in the next release 🤣

@eileenmcnaughton
Copy link
Contributor Author

lol

@eileenmcnaughton eileenmcnaughton merged commit a846d2e into civicrm:master Mar 11, 2023
@eileenmcnaughton eileenmcnaughton deleted the dynamic branch March 11, 2023 03:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants