From 58ba7cf72fe2a43d1fc25b85239b305807146bfe Mon Sep 17 00:00:00 2001 From: eileen Date: Fri, 15 Feb 2019 09:42:00 +1300 Subject: [PATCH] [REF] extract chunk of code to a separate function --- CRM/Profile/Form.php | 188 +++++++++++++++++++++++-------------------- 1 file changed, 102 insertions(+), 86 deletions(-) diff --git a/CRM/Profile/Form.php b/CRM/Profile/Form.php index 7dfdbdd10f..70aff916cf 100644 --- a/CRM/Profile/Form.php +++ b/CRM/Profile/Form.php @@ -179,6 +179,106 @@ class CRM_Profile_Form extends CRM_Core_Form { protected $_currentUserID = NULL; protected $_session = NULL; + /** + * Check for any duplicates. + * + * Depending on form settings & usage scenario we potentially use the found id, + * create links to found ids or add an error. + * + * @param array $errors + * @param array $fields + * @param CRM_Profile_Form $form + * + * @return array + */ + protected static function handleDuplicateChecking(&$errors, $fields, $form) { + if ($form->_mode == CRM_Profile_Form::MODE_CREATE) { + // fix for CRM-2888 + $exceptions = []; + } + else { + // for edit mode we need to allow our own record to be a dupe match! + $exceptions = [$form->_session->get('userID')]; + } + $contactType = CRM_Core_BAO_UFGroup::getContactType($form->_gid); + // If all profile fields is of Contact Type then consider + // profile is of Individual type(default). + if (!$contactType) { + $contactType = 'Individual'; + } + + $ids = CRM_Contact_BAO_Contact::getDuplicateContacts( + $fields, $contactType, + ($form->_context === 'dialog' ? 'Supervised' : 'Unsupervised'), + $exceptions, + FALSE, + $form->_ruleGroupID + ); + if ($ids) { + if ($form->_isUpdateDupe == 2) { + CRM_Core_Session::setStatus(ts('Note: this contact may be a duplicate of an existing record.'), ts('Possible Duplicate Detected'), 'alert'); + } + elseif ($form->_isUpdateDupe == 1) { + if (!$form->_id) { + $form->_id = $ids[0]; + } + } + else { + if ($form->_context == 'dialog') { + $contactLinks = CRM_Contact_BAO_Contact_Utils::formatContactIDSToLinks($ids, TRUE, TRUE); + + $duplicateContactsLinks = '
'; + $duplicateContactsLinks .= ts('One matching contact was found. ', [ + 'count' => count($contactLinks['rows']), + 'plural' => '%count matching contacts were found.
', + ]); + if ($contactLinks['msg'] == 'view') { + $duplicateContactsLinks .= ts('You can View the existing contact.', [ + 'count' => count($contactLinks['rows']), + 'plural' => 'You can View the existing contacts.', + ]); + } + else { + $duplicateContactsLinks .= ts('You can View or Edit the existing contact.', [ + 'count' => count($contactLinks['rows']), + 'plural' => 'You can View or Edit the existing contacts.', + ]); + } + $duplicateContactsLinks .= '
'; + $duplicateContactsLinks .= ''; + $row = ''; + for ($i = 0; $i < count($contactLinks['rows']); $i++) { + $row .= ' '; + $row .= ' '; + $row .= ' '; + $row .= ' '; + $row .= ' '; + } + + $duplicateContactsLinks .= $row . '
'; + $row .= $contactLinks['rows'][$i]['display_name']; + $row .= ' '; + $row .= $contactLinks['rows'][$i]['primary_email']; + $row .= ' '; + $row .= $contactLinks['rows'][$i]['view'] . ' '; + $row .= $contactLinks['rows'][$i]['edit']; + $row .= '
'; + $duplicateContactsLinks .= "If you're sure this record is not a duplicate, click the 'Save Matching Contact' button below."; + + $errors['_qf_default'] = $duplicateContactsLinks; + + // let smarty know that there are duplicates + $template = CRM_Core_Smarty::singleton(); + $template->assign('isDuplicate', 1); + } + else { + $errors['_qf_default'] = ts('A record already exists with the same information.'); + } + } + } + return $errors; + } + /** * Explicitly declare the entity api name. */ @@ -893,12 +993,12 @@ class CRM_Profile_Form extends CRM_Core_Form { public static function formRule($fields, $files, $form) { CRM_Utils_Hook::validateProfile($form->_ufGroup['name']); - $errors = array(); // if no values, return if (empty($fields)) { return TRUE; } + $errors = []; $register = NULL; // hack we use a -1 in options to indicate that its registration @@ -922,91 +1022,7 @@ class CRM_Profile_Form extends CRM_Core_Form { $fields['phone-Primary'] = $fields['phone-Primary-1']; } - $ctype = CRM_Core_BAO_UFGroup::getContactType($form->_gid); - // If all profile fields is of Contact Type then consider - // profile is of Individual type(default). - if (!$ctype) { - $ctype = 'Individual'; - } - - if ($form->_mode == CRM_Profile_Form::MODE_CREATE) { - // fix for CRM-2888 - $exceptions = array(); - } - else { - // for edit mode we need to allow our own record to be a dupe match! - $exceptions = array($form->_session->get('userID')); - } - - $ids = CRM_Contact_BAO_Contact::getDuplicateContacts( - $fields, $ctype, - ($form->_context === 'dialog' ? 'Supervised' : 'Unsupervised'), - $exceptions, - FALSE, - $form->_ruleGroupID - ); - if ($ids) { - if ($form->_isUpdateDupe == 2) { - CRM_Core_Session::setStatus(ts('Note: this contact may be a duplicate of an existing record.'), ts('Possible Duplicate Detected'), 'alert'); - } - elseif ($form->_isUpdateDupe == 1) { - if (!$form->_id) { - $form->_id = $ids[0]; - } - } - else { - if ($form->_context == 'dialog') { - $contactLinks = CRM_Contact_BAO_Contact_Utils::formatContactIDSToLinks($ids, TRUE, TRUE); - - $duplicateContactsLinks = '
'; - $duplicateContactsLinks .= ts('One matching contact was found. ', array( - 'count' => count($contactLinks['rows']), - 'plural' => '%count matching contacts were found.
', - )); - if ($contactLinks['msg'] == 'view') { - $duplicateContactsLinks .= ts('You can View the existing contact.', array( - 'count' => count($contactLinks['rows']), - 'plural' => 'You can View the existing contacts.', - )); - } - else { - $duplicateContactsLinks .= ts('You can View or Edit the existing contact.', array( - 'count' => count($contactLinks['rows']), - 'plural' => 'You can View or Edit the existing contacts.', - )); - } - $duplicateContactsLinks .= '
'; - $duplicateContactsLinks .= ''; - $row = ''; - for ($i = 0; $i < count($contactLinks['rows']); $i++) { - $row .= ' '; - $row .= ' '; - $row .= ' '; - $row .= ' '; - $row .= ' '; - } - - $duplicateContactsLinks .= $row . '
'; - $row .= $contactLinks['rows'][$i]['display_name']; - $row .= ' '; - $row .= $contactLinks['rows'][$i]['primary_email']; - $row .= ' '; - $row .= $contactLinks['rows'][$i]['view'] . ' '; - $row .= $contactLinks['rows'][$i]['edit']; - $row .= '
'; - $duplicateContactsLinks .= "If you're sure this record is not a duplicate, click the 'Save Matching Contact' button below."; - - $errors['_qf_default'] = $duplicateContactsLinks; - - // let smarty know that there are duplicates - $template = CRM_Core_Smarty::singleton(); - $template->assign('isDuplicate', 1); - } - else { - $errors['_qf_default'] = ts('A record already exists with the same information.'); - } - } - } + self::handleDuplicateChecking($errors, $fields, $form); } foreach ($fields as $key => $value) { -- 2.25.1