From 8bdfc216c1d6e54de791fd6d84ab49a2c18ebc9f Mon Sep 17 00:00:00 2001 From: atif-shaikh Date: Tue, 6 Jan 2015 13:08:39 +0530 Subject: [PATCH] CRM-15780 - Remove duplicate function https://issues.civicrm.org/jira/browse/CRM-15780 --- CRM/Contact/Form/Task/Label.php | 77 ++------------------------ CRM/Contact/Form/Task/LabelCommon.php | 27 ++------- CRM/Core/BAO/Address.php | 80 +++++++++++++++++++++++++++ CRM/Member/Form/Task/Label.php | 2 +- 4 files changed, 92 insertions(+), 94 deletions(-) diff --git a/CRM/Contact/Form/Task/Label.php b/CRM/Contact/Form/Task/Label.php index b5b6f436fc..213e7caca5 100644 --- a/CRM/Contact/Form/Task/Label.php +++ b/CRM/Contact/Form/Task/Label.php @@ -258,7 +258,7 @@ class CRM_Contact_Form_Task_Label extends CRM_Contact_Form_Task { // If location type is not primary, $contact contains // one more array as "$contact[$locName] = array( values... )" - if(!$this->tokenIsFound($contact, $mailingFormatProperties, $tokenFields)) { + if (!self::tokenIsFound($contact, $mailingFormatProperties, $tokenFields)) { continue; } @@ -297,7 +297,7 @@ class CRM_Contact_Form_Task_Label extends CRM_Contact_Form_Task { } } else { - if(!$this->tokenIsFound($contact, $mailingFormatProperties, $tokenFields)) { + if (!self::tokenIsFound($contact, $mailingFormatProperties, $tokenFields)) { continue; } @@ -316,7 +316,7 @@ class CRM_Contact_Form_Task_Label extends CRM_Contact_Form_Task { } if (isset($fv['merge_same_address'])) { - self::mergeSameAddress($rows); + CRM_Core_BAO_Address::mergeSameAddress($rows); $individualFormat = TRUE; } @@ -362,9 +362,10 @@ class CRM_Contact_Form_Task_Label extends CRM_Contact_Form_Task { * * @return bool */ - public function tokenIsFound($contact, $mailingFormatProperties, $tokenFields) { + public static function tokenIsFound($contact, $mailingFormatProperties, $tokenFields) { foreach (array_merge($mailingFormatProperties, array_fill_keys($tokenFields, 1)) as $key => $dontCare) { - if (!empty($contact[$key])) { + //we should not consider addressee for data exists, CRM-6025 + if ($key != 'addressee' && !empty($contact[$key])) { return TRUE; } } @@ -397,71 +398,5 @@ class CRM_Contact_Form_Task_Label extends CRM_Contact_Form_Task { $pdf->Output($fileName, 'D'); } - /** - * @param array $rows - */ - static public function mergeSameAddress(&$rows) { - $uniqueAddress = array(); - foreach (array_keys($rows) as $rowID) { - // load complete address as array key - $address = - trim($rows[$rowID]['street_address']) . trim($rows[$rowID]['city']) . trim($rows[$rowID]['state_province']) . trim($rows[$rowID]['postal_code']) . trim($rows[$rowID]['country']); - if (isset($rows[$rowID]['last_name'])) { - $name = $rows[$rowID]['last_name']; - } - else { - $name = $rows[$rowID]['display_name']; - } - // CRM-15120 - $formatted = array( - 'first_name' => $rows[$rowID]['first_name'], - 'individual_prefix' => $rows[$rowID]['individual_prefix'] - ); - $format = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'display_name_format'); - $firstNameWithPrefix = CRM_Utils_Address::format($formatted, $format, FALSE, FALSE, TRUE); - $firstNameWithPrefix = trim($firstNameWithPrefix); - - // fill uniqueAddress array with last/first name tree - if (isset($uniqueAddress[$address])) { - $uniqueAddress[$address]['names'][$name][$firstNameWithPrefix]['first_name'] = $rows[$rowID]['first_name']; - $uniqueAddress[$address]['names'][$name][$firstNameWithPrefix]['addressee_display'] = $rows[$rowID]['addressee_display']; - // drop unnecessary rows - unset($rows[$rowID]); - // this is the first listing at this address - } - else { - $uniqueAddress[$address]['ID'] = $rowID; - $uniqueAddress[$address]['names'][$name][$firstNameWithPrefix]['first_name'] = $rows[$rowID]['first_name']; - $uniqueAddress[$address]['names'][$name][$firstNameWithPrefix]['addressee_display'] = $rows[$rowID]['addressee_display']; - } - } - foreach ($uniqueAddress as $address => $data) { - // copy data back to $rows - $count = 0; - // one last name list per row - foreach ($data['names'] as $last_name => $first_names) { - // too many to list - if ($count > 2) { - break; - } - if(count($first_names) == 1){ - $family = $first_names[current(array_keys($first_names))]['addressee_display']; - } - else { - // collapse the tree to summarize - $family = trim(implode(" & ", array_keys($first_names)) . " " . $last_name); - } - if ($count) { - $processedNames .= "\n" . $family; - } - else { - // build display_name string - $processedNames = $family; - } - $count++; - } - $rows[$data['ID']]['addressee'] = $rows[$data['ID']]['addressee_display'] = $rows[$data['ID']]['display_name'] = $processedNames; - } - } } diff --git a/CRM/Contact/Form/Task/LabelCommon.php b/CRM/Contact/Form/Task/LabelCommon.php index 87ba7ed94d..0cb432b837 100644 --- a/CRM/Contact/Form/Task/LabelCommon.php +++ b/CRM/Contact/Form/Task/LabelCommon.php @@ -39,24 +39,7 @@ * components in CiviCRM (since they all have send email as a task) */ class CRM_Contact_Form_Task_LabelCommon { - /** - * Check for presence of tokens to be swapped out - * - * @param array $contact - * @param array $mailingFormatProperties - * @param array $tokenFields - * - * @return bool - */ - public function tokenIsFound($contact, $mailingFormatProperties, $tokenFields) { - foreach (array_merge($mailingFormatProperties, array_fill_keys($tokenFields, 1)) as $key => $dontCare) { - //we should not consider addressee for data exists, CRM-6025 - if ($key != 'addressee' && !empty($contact[$key])) { - return TRUE; - } - } - return FALSE; - } + /** * Create labels (pdf) * @@ -196,7 +179,7 @@ class CRM_Contact_Form_Task_LabelCommon { // If location type is not primary, $contact contains // one more array as "$contact[$locName] = array( values... )" - if(!CRM_Contact_Form_Task_LabelCommon::tokenIsFound($contact, $mailingFormatProperties, $tokenFields)) { + if (!CRM_Contact_Form_Task_Label::tokenIsFound($contact, $mailingFormatProperties, $tokenFields)) { continue; } @@ -234,7 +217,7 @@ class CRM_Contact_Form_Task_LabelCommon { } } else { - if(!CRM_Contact_Form_Task_LabelCommon::tokenIsFound($contact, $mailingFormatProperties, $tokenFields)) { + if (!CRM_Contact_Form_Task_Label::tokenIsFound($contact, $mailingFormatProperties, $tokenFields)) { continue; } @@ -264,7 +247,7 @@ class CRM_Contact_Form_Task_LabelCommon { * - [supplemental_address_2] => 1 * ) */ - public function getAddressReturnProperties() { + public static function getAddressReturnProperties() { $mailingFormat = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'mailing_format' ); @@ -283,7 +266,7 @@ class CRM_Contact_Form_Task_LabelCommon { * @param unknown_type $contacts * @return unknown */ - public function getTokenData(&$contacts) { + public static function getTokenData(&$contacts) { $mailingFormat = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'mailing_format' ); diff --git a/CRM/Core/BAO/Address.php b/CRM/Core/BAO/Address.php index 2df8a1235c..56368e31e6 100644 --- a/CRM/Core/BAO/Address.php +++ b/CRM/Core/BAO/Address.php @@ -958,6 +958,86 @@ SELECT is_primary, } } + /** + * Merge contacts with the Same address to get one shared label + * @param array $rows - array + * format to pass + * Array + ( + [103] => Array + ( + [contact_type] => Individual + [display_name] => Mr. Angelika Adams + [first_name] => Angelika + [last_name] => Adams + ) + ) + */ + public static function mergeSameAddress(&$rows) { + $uniqueAddress = array(); + foreach (array_keys($rows) as $rowID) { + // load complete address as array key + $address = + trim($rows[$rowID]['street_address']) . trim($rows[$rowID]['city']) . trim($rows[$rowID]['state_province']) . trim($rows[$rowID]['postal_code']) . trim($rows[$rowID]['country']); + if (isset($rows[$rowID]['last_name'])) { + $name = $rows[$rowID]['last_name']; + } + else { + $name = $rows[$rowID]['display_name']; + } + + // CRM-15120 + $formatted = array( + 'first_name' => $rows[$rowID]['first_name'], + 'individual_prefix' => $rows[$rowID]['individual_prefix'] + ); + $format = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'display_name_format'); + $firstNameWithPrefix = CRM_Utils_Address::format($formatted, $format, FALSE, FALSE, TRUE); + $firstNameWithPrefix = trim($firstNameWithPrefix); + + // fill uniqueAddress array with last/first name tree + if (isset($uniqueAddress[$address])) { + $uniqueAddress[$address]['names'][$name][$firstNameWithPrefix]['first_name'] = $rows[$rowID]['first_name']; + $uniqueAddress[$address]['names'][$name][$firstNameWithPrefix]['addressee_display'] = $rows[$rowID]['addressee_display']; + // drop unnecessary rows + unset($rows[$rowID]); + // this is the first listing at this address + } + else { + $uniqueAddress[$address]['ID'] = $rowID; + $uniqueAddress[$address]['names'][$name][$firstNameWithPrefix]['first_name'] = $rows[$rowID]['first_name']; + $uniqueAddress[$address]['names'][$name][$firstNameWithPrefix]['addressee_display'] = $rows[$rowID]['addressee_display']; + } + } + foreach ($uniqueAddress as $address => $data) { + // copy data back to $rows + $count = 0; + // one last name list per row + foreach ($data['names'] as $last_name => $first_names) { + // too many to list + if ($count > 2) { + break; + } + if(count($first_names) == 1){ + $family = $first_names[current(array_keys($first_names))]['addressee_display']; + } + else { + // collapse the tree to summarize + $family = trim(implode(" & ", array_keys($first_names)) . " " . $last_name); + } + if ($count) { + $processedNames .= "\n" . $family; + } + else { + // build display_name string + $processedNames = $family; + } + $count++; + } + $rows[$data['ID']]['addressee'] = $rows[$data['ID']]['addressee_display'] = $rows[$data['ID']]['display_name'] = $processedNames; + } + } + /** * Create relationship between contacts who share an address * diff --git a/CRM/Member/Form/Task/Label.php b/CRM/Member/Form/Task/Label.php index f889349b01..32d19a21a3 100644 --- a/CRM/Member/Form/Task/Label.php +++ b/CRM/Member/Form/Task/Label.php @@ -104,7 +104,7 @@ class CRM_Member_Form_Task_Label extends CRM_Member_Form_Task { $individualFormat = FALSE; if ($mergeSameAddress) { - CRM_Contact_Form_Task_Label::mergeSameAddress($rows); + CRM_Core_BAO_Address::mergeSameAddress($rows); $individualFormat = TRUE; } if ($mergeSameHousehold) { -- 2.25.1