Beispiel #1
0
 /**
  * Test hook allowing modification of the data calculated for merging locations.
  *
  * We are testing a nuanced real life situation where the address data of the
  * most recent donor gets priority - resulting in the primary address being set
  * to the primary address of the most recent donor and address data on a per
  * location type basis also being set to the most recent donor. Hook also excludes
  * a fully matching address with a different location.
  *
  * This has been added to the test suite to ensure the code supports more this
  * type of intervention.
  *
  * @param array $blocksDAO
  *   Array of location DAO to be saved. These are arrays in 2 keys 'update' & 'delete'.
  * @param int $mainId
  *   Contact_id of the contact that survives the merge.
  * @param int $otherId
  *   Contact_id of the contact that will be absorbed and deleted.
  * @param array $migrationInfo
  *   Calculated migration info, informational only.
  *
  * @return mixed
  */
 public function hookMostRecentDonor(&$blocksDAO, $mainId, $otherId, $migrationInfo)
 {
     $lastDonorID = $this->callAPISuccessGetValue('Contribution', array('return' => 'contact_id', 'contact_id' => array('IN' => array($mainId, $otherId)), 'options' => array('sort' => 'receive_date DESC', 'limit' => 1)));
     // Since the last donor is not the main ID we are prioritising info from the last donor.
     // In the test this should always be true - but keep the check in case
     // something changes that we need to detect.
     if ($lastDonorID != $mainId) {
         foreach ($migrationInfo['other_details']['location_blocks'] as $blockType => $blocks) {
             foreach ($blocks as $block) {
                 if ($block['is_primary']) {
                     $primaryAddressID = $block['id'];
                     if (!empty($migrationInfo['main_details']['location_blocks'][$blockType])) {
                         foreach ($migrationInfo['main_details']['location_blocks'][$blockType] as $mainBlock) {
                             if (empty($blocksDAO[$blockType]['update'][$block['id']]) && $mainBlock['location_type_id'] == $block['location_type_id']) {
                                 // This was an address match - we just need to check the is_primary
                                 // is true on the matching kept address.
                                 $primaryAddressID = $mainBlock['id'];
                                 $blocksDAO[$blockType]['update'][$primaryAddressID] = _civicrm_api3_load_DAO($blockType);
                                 $blocksDAO[$blockType]['update'][$primaryAddressID]->id = $primaryAddressID;
                             }
                             $mainLocationTypeID = $mainBlock['location_type_id'];
                             // We also want to be more ruthless about removing matching addresses.
                             unset($mainBlock['location_type_id']);
                             if (CRM_Dedupe_Merger::locationIsSame($block, $mainBlock) && (!isset($blocksDAO[$blockType]['update']) || !isset($blocksDAO[$blockType]['update'][$mainBlock['id']])) && (!isset($blocksDAO[$blockType]['delete']) || !isset($blocksDAO[$blockType]['delete'][$mainBlock['id']]))) {
                                 $blocksDAO[$blockType]['delete'][$mainBlock['id']] = _civicrm_api3_load_DAO($blockType);
                                 $blocksDAO[$blockType]['delete'][$mainBlock['id']]->id = $mainBlock['id'];
                             } elseif ($mainBlock['is_primary'] && $mainLocationTypeID != $block['location_type_id']) {
                                 $blocksDAO['address']['update'][$mainBlock['id']] = _civicrm_api3_load_DAO($blockType);
                                 $blocksDAO['address']['update'][$mainBlock['id']]->is_primary = 0;
                                 $blocksDAO['address']['update'][$mainBlock['id']]->id = $mainBlock['id'];
                             }
                         }
                         $blocksDAO[$blockType]['update'][$primaryAddressID]->is_primary = 1;
                     }
                 }
             }
         }
     }
 }