/** * takes an associative array and creates a address * * @param array $params (reference ) an assoc array of name/value pairs * @param boolean $fixAddress true if you need to fix (format) address values * before inserting in db * * @return array $blocks array of created address * @access public * @static */ static function create(&$params, $fixAddress, $entity = null) { if (!isset($params['address']) || !is_array($params['address'])) { return; } $addresses = array(); $contactId = null; if (!$entity) { $contactId = $params['contact_id']; //get all the addresses for this contact $addresses = self::allAddress($contactId); } else { // get all address from location block $entityElements = array('entity_table' => $params['entity_table'], 'entity_id' => $params['entity_id']); $addresses = self::allEntityAddress($entityElements); } $isPrimary = $isBilling = true; $blocks = array(); require_once "CRM/Core/BAO/Block.php"; foreach ($params['address'] as $key => $value) { if (!is_array($value)) { continue; } if (!empty($addresses) && array_key_exists($value['location_type_id'], $addresses)) { $value['id'] = $addresses[$value['location_type_id']]; } $addressExists = self::dataExists($value); if (isset($value['id']) && !$addressExists) { //delete the existing record CRM_Core_BAO_Block::blockDelete('Address', array('id' => $value['id'])); continue; } else { if (!$addressExists) { continue; } } if ($isPrimary && $value['is_primary']) { $isPrimary = false; } else { $value['is_primary'] = 0; } if ($isBilling && CRM_Utils_Array::value('is_billing', $value)) { $isBilling = false; } else { $value['is_billing'] = 0; } $value['contact_id'] = $contactId; $blocks[] = self::add($value, $fixAddress); } return $blocks; }
/** * Delete billing address record related contribution * @param int $contact_id contact id * @param int $contribution_id contributionId * @access public * @static */ static function deleteAddress($contributionId = null, $contactId = null) { $contributionCond = $contactCond = 'null'; if ($contributionId) { $contributionCond = "cc.id = {$contributionId}"; } if ($contactId) { $contactCond = "cco.id = {$contactId}"; } $query = "\nSELECT ca.id FROM \ncivicrm_address ca \nLEFT JOIN civicrm_contribution cc ON cc.address_id = ca.id \nLEFT JOIN civicrm_contact cco ON cc.contact_id = cco.id \nWHERE ( {$contributionCond} OR {$contactCond} )"; $dao = CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray); while ($dao->fetch()) { require_once "CRM/Core/BAO/Block.php"; $params = array('id' => $dao->id); CRM_Core_BAO_Block::blockDelete('Address', $params); } }
/** * Delete billing address record related contribution. * * @param int $contributionId * @param int $contactId */ public static function deleteAddress($contributionId = NULL, $contactId = NULL) { $clauses = array(); $contactJoin = NULL; if ($contributionId) { $clauses[] = "cc.id = {$contributionId}"; } if ($contactId) { $clauses[] = "cco.id = {$contactId}"; $contactJoin = "INNER JOIN civicrm_contact cco ON cc.contact_id = cco.id"; } if (empty($clauses)) { CRM_Core_Error::fatal(); } $condition = implode(' OR ', $clauses); $query = "\nSELECT ca.id\nFROM civicrm_address ca\nINNER JOIN civicrm_contribution cc ON cc.address_id = ca.id\n {$contactJoin}\nWHERE {$condition}\n"; $dao = CRM_Core_DAO::executeQuery($query); while ($dao->fetch()) { $params = array('id' => $dao->id); CRM_Core_BAO_Block::blockDelete('Address', $params); } }
/** * Delete all the block associated with the location * * @param int $contactId contact id * @param int $locationTypeId id of the location to delete * * @return void * @access public * @static */ static function deleteLocationBlocks($contactId, $locationTypeId) { // ensure that contactId has a value if (empty($contactId) || !CRM_Utils_Rule::positiveInteger($contactId)) { CRM_Core_Error::fatal(); } if (empty($locationTypeId) || !CRM_Utils_Rule::positiveInteger($locationTypeId)) { // so we only delete the blocks which DO NOT have a location type Id // CRM-3581 $locationTypeId = 'null'; } static $blocks = array('Address', 'Phone', 'IM', 'OpenID', 'Email'); $params = array('contact_id' => $contactId, 'location_type_id' => $locationTypeId); foreach ($blocks as $name) { CRM_Core_BAO_Block::blockDelete($name, $params); } }
/** * Takes an associative array and creates a address. * * @param array $params * (reference ) an assoc array of name/value pairs. * @param bool $fixAddress * True if you need to fix (format) address values. * before inserting in db * * @param null $entity * * @return array|NULL * array of created address */ public static function create(&$params, $fixAddress = TRUE, $entity = NULL) { if (!isset($params['address']) || !is_array($params['address'])) { return NULL; } CRM_Core_BAO_Block::sortPrimaryFirst($params['address']); $addresses = array(); $contactId = NULL; $updateBlankLocInfo = CRM_Utils_Array::value('updateBlankLocInfo', $params, FALSE); if (!$entity) { $contactId = $params['contact_id']; //get all the addresses for this contact $addresses = self::allAddress($contactId, $updateBlankLocInfo); } else { // get all address from location block $entityElements = array('entity_table' => $params['entity_table'], 'entity_id' => $params['entity_id']); $addresses = self::allEntityAddress($entityElements); } $isPrimary = $isBilling = TRUE; $blocks = array(); foreach ($params['address'] as $key => $value) { if (!is_array($value)) { continue; } $addressExists = self::dataExists($value); if (empty($value['id'])) { if ($updateBlankLocInfo) { if ((!empty($addresses) || !$addressExists) && array_key_exists($key, $addresses)) { $value['id'] = $addresses[$key]; } } else { if (!empty($addresses) && array_key_exists(CRM_Utils_Array::value('location_type_id', $value), $addresses)) { $value['id'] = $addresses[CRM_Utils_Array::value('location_type_id', $value)]; } } } // Note there could be cases when address info already exist ($value[id] is set) for a contact/entity // BUT info is not present at this time, and therefore we should be really careful when deleting the block. // $updateBlankLocInfo will help take appropriate decision. CRM-5969 if (isset($value['id']) && !$addressExists && $updateBlankLocInfo) { //delete the existing record CRM_Core_BAO_Block::blockDelete('Address', array('id' => $value['id'])); continue; } elseif (!$addressExists) { continue; } if ($isPrimary && !empty($value['is_primary'])) { $isPrimary = FALSE; } else { $value['is_primary'] = 0; } if ($isBilling && !empty($value['is_billing'])) { $isBilling = FALSE; } else { $value['is_billing'] = 0; } if (empty($value['manual_geo_code'])) { $value['manual_geo_code'] = 0; } $value['contact_id'] = $contactId; $blocks[] = self::add($value, $fixAddress); } return $blocks; }
/** * Takes an associative array and creates a address. * * @param array $params * (reference ) an assoc array of name/value pairs. * @param bool $fixAddress * True if you need to fix (format) address values. * before inserting in db * * @param null $entity * * @return array|NULL * array of created address */ public static function create(&$params, $fixAddress = TRUE, $entity = NULL) { if (!isset($params['address']) || !is_array($params['address'])) { return NULL; } CRM_Core_BAO_Block::sortPrimaryFirst($params['address']); $addresses = array(); $contactId = NULL; $updateBlankLocInfo = CRM_Utils_Array::value('updateBlankLocInfo', $params, FALSE); if (!$entity) { $contactId = $params['contact_id']; //get all the addresses for this contact $addresses = self::allAddress($contactId); } else { // get all address from location block $entityElements = array('entity_table' => $params['entity_table'], 'entity_id' => $params['entity_id']); $addresses = self::allEntityAddress($entityElements); } $isPrimary = $isBilling = TRUE; $blocks = array(); foreach ($params['address'] as $key => $value) { if (!is_array($value)) { continue; } $addressExists = self::dataExists($value); // If the ID is empty then this is either a new address, or an edit coming // from a Profile. So we need to match up the location type with any // that are currently on the contact. if (empty($value['id']) && !empty($value['location_type_id']) && !empty($addresses)) { // Does the submitted address type already exist? if (array_key_exists($value['location_type_id'], $addresses)) { // Match the address ID and remove from the list so we know its matched $value['id'] = $addresses[$value['location_type_id']]; unset($addresses[$value['location_type_id']]); } } // Note there could be cases when address info already exist ($value[id] is set) for a contact/entity // BUT info is not present at this time, and therefore we should be really careful when deleting the block. // $updateBlankLocInfo will help take appropriate decision. CRM-5969 if (!empty($value['id']) && !$addressExists && $updateBlankLocInfo) { //delete the existing record CRM_Core_BAO_Block::blockDelete('Address', array('id' => $value['id'])); continue; } elseif (!$addressExists) { continue; } if ($isPrimary && !empty($value['is_primary'])) { $isPrimary = FALSE; } else { $value['is_primary'] = 0; } if ($isBilling && !empty($value['is_billing'])) { $isBilling = FALSE; } else { $value['is_billing'] = 0; } if (empty($value['manual_geo_code'])) { $value['manual_geo_code'] = 0; } $value['contact_id'] = $contactId; $blocks[] = self::add($value, $fixAddress); } // If this is an edit from the full contact edit page then delete any // addresses that we couldn't match on - because they were deleted on the form if (!empty($params['isFullContactEdit']) && count($addresses)) { foreach ($addresses as $addressId) { CRM_Core_BAO_Block::blockDelete('Address', array('id' => $addressId)); } } return $blocks; }
/** * Function to cleanup Contact locations * Basically we need to delete unwanted location types for a contact in Edit mode * create() is also called by createProfileContact(), in that case we should preserve other location type's, * This is a special case where we need to delete location types that are not submitted. * * @param array $params associated array of formatted params * @return void * @static */ static function cleanupContactLocations($params) { //get the contact id from params $contactId = CRM_Utils_Array::value('contact_id', $params); // get existing locations $deleteBlocks = array(); $dbBlockValues = self::getValues(array('contact_id' => $contactId)); foreach (self::$blocks as $block) { if (!is_array($dbBlockValues[$block])) { continue; } foreach ($dbBlockValues[$block] as $dbCount => $dbValues) { if (!is_array($params[$block])) { $deleteBlocks[$block] = $dbBlockValues[$block]; continue; } $valueSubmitted = false; foreach ($params[$block] as $submitCount => $submitValues) { if ($submitValues['location_type_id'] == $dbValues['location_type_id']) { //unset from submitted since we map it across db. unset($params[$block][$submitCount]); $valueSubmitted = true; break; } } //since this value not present in submit params. if (!$valueSubmitted) { $deleteBlocks[$block][$dbCount] = $dbValues; } } } //finally delete unwanted blocks. foreach ($deleteBlocks as $blockName => $blockValues) { if (!is_array($blockValues)) { continue; } foreach ($blockValues as $count => $deleteBlock) { CRM_Core_BAO_Block::blockDelete($blockName, $deleteBlock); } } }