/** * Given the list of params in the params array, fetch the object * and store the values in the values array * * @param array $entityBlock * Associated array of fields. * @param bool $microformat * If microformat output is required. * @param int|string $fieldName conditional field name * * @return array * array with address fields */ public static function &getValues($entityBlock, $microformat = FALSE, $fieldName = 'contact_id') { if (empty($entityBlock)) { return NULL; } $addresses = array(); $address = new CRM_Core_BAO_Address(); if (empty($entityBlock['entity_table'])) { $address->{$fieldName} = CRM_Utils_Array::value($fieldName, $entityBlock); } else { $addressIds = array(); $addressIds = self::allEntityAddress($entityBlock); if (!empty($addressIds[1])) { $address->id = $addressIds[1]; } else { return $addresses; } } //get primary address as a first block. $address->orderBy('is_primary desc, id'); $address->find(); $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'); $count = 1; while ($address->fetch()) { // deprecate reference. if ($count > 1) { foreach (array('state', 'state_name', 'country', 'world_region') as $fld) { if (isset($address->{$fld})) { unset($address->{$fld}); } } } $stree = $address->street_address; $values = array(); CRM_Core_DAO::storeValues($address, $values); // add state and country information: CRM-369 if (!empty($address->location_type_id)) { $values['location_type'] = CRM_Utils_Array::value($address->location_type_id, $locationTypes); } if (!empty($address->state_province_id)) { $address->state = CRM_Core_PseudoConstant::stateProvinceAbbreviation($address->state_province_id, FALSE); $address->state_name = CRM_Core_PseudoConstant::stateProvince($address->state_province_id, FALSE); } if (!empty($address->country_id)) { $address->country = CRM_Core_PseudoConstant::country($address->country_id); //get world region $regionId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Country', $address->country_id, 'region_id'); $address->world_region = CRM_Core_PseudoConstant::worldregion($regionId); } $address->addDisplay($microformat); $values['display'] = $address->display; $values['display_text'] = $address->display_text; if (is_numeric($address->master_id)) { $values['use_shared_address'] = 1; } $addresses[$count] = $values; //unset is_primary after first block. Due to some bug in earlier version //there might be more than one primary blocks, hence unset is_primary other than first if ($count > 1) { unset($addresses[$count]['is_primary']); } $count++; } return $addresses; }
/** * Function handles shared contact address processing. * In this function we just modify submitted values so that new address created for the user * has same address as shared contact address. We copy the address so that search etc will be * much efficient. * * @param array $address * This is associated array which contains submitted form values. */ public static function processSharedAddress(&$address) { if (!is_array($address)) { return; } // Sharing contact address during create mode is pretty straight forward. // In update mode we should check following: // - We should check if user has uncheck shared contact address // - If yes then unset the master_id or may be just delete the address that copied master // Normal update process will automatically create new address with submitted values // 1. loop through entire subnitted address array $masterAddress = array(); $skipFields = array('is_primary', 'location_type_id', 'is_billing', 'master_id'); foreach ($address as &$values) { // 2. check if master id exists, if not continue if (empty($values['master_id']) || empty($values['use_shared_address'])) { // we should unset master id when use uncheck share address for existing address $values['master_id'] = 'null'; continue; } // 3. get the address details for master_id $masterAddress = new CRM_Core_BAO_Address(); $masterAddress->id = CRM_Utils_Array::value('master_id', $values); $masterAddress->find(TRUE); // 4. modify submitted params and update it with shared contact address // make sure you preserve specific form values like location type, is_primary_ is_billing, master_id // CRM-10336: Also empty any fields from the existing address block if they don't exist in master (otherwise they will persist) foreach ($values as $field => $submittedValue) { if (!in_array($field, $skipFields)) { if (isset($masterAddress->{$field})) { $values[$field] = $masterAddress->{$field}; } else { $values[$field] = ''; } } } //5. modify the params to include county_id if it exist in master contact. // CRM-16152: This is a hack since QF does not submit disabled field. if (!empty($masterAddress->county_id) && empty($values['county_id'])) { $values['county_id'] = $masterAddress->county_id; } } }
/** * Function handles shared contact address processing * In this function we just modify submitted values so that new address created for the user * has same address as shared contact address. We copy the address so that search etc will be * much efficient. * * @param array $address this is associated array which contains submitted form values * * @return void * @static * @access public */ static function processSharedAddress(&$address) { if (!is_array($address)) { return; } // Sharing contact address during create mode is pretty straight forward. // In update mode we should check following: // - We should check if user has uncheck shared contact address // - If yes then unset the master_id or may be just delete the address that copied master // Normal update process will automatically create new address with submitted values // 1. loop through entire subnitted address array $masterAddress = array(); $skipFields = array('is_primary', 'location_type_id', 'is_billing', 'master_id'); foreach ($address as &$values) { // 2. check if master id exists, if not continue if (!CRM_Utils_Array::value('master_id', $values) || !CRM_Utils_Array::value('use_shared_address', $values)) { // we should unset master id when use uncheck share address for existing address $values['master_id'] = 'null'; continue; } // 3. get the address details for master_id $masterAddress = new CRM_Core_BAO_Address(); $masterAddress->id = CRM_Utils_Array::value('master_id', $values); $masterAddress->find(true); // 4. modify submitted params and update it with shared contact address // make sure you preserve specific form values like location type, is_primary_ is_billing, master_id foreach ($values as $field => $submittedValue) { if (!in_array($field, $skipFields) && isset($masterAddress->{$field})) { $values[$field] = $masterAddress->{$field}; } } } }