3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class handle creation of location block elements.
21 class CRM_Core_BAO_Location
extends CRM_Core_DAO
{
24 * Location block element array.
27 public static $blocks = ['phone', 'email', 'im', 'openid', 'address'];
30 * Create various elements of location block.
32 * @param array $params
33 * (reference ) an assoc array of name/value pairs.
34 * @param bool $fixAddress
35 * True if you need to fix (format) address values.
36 * before inserting in db
40 public static function create(&$params, $fixAddress = TRUE) {
42 if (!self
::dataExists($params)) {
46 // create location blocks.
47 foreach (self
::$blocks as $block) {
48 if ($block != 'address') {
49 $location[$block] = CRM_Core_BAO_Block
::create($block, $params);
52 $location[$block] = CRM_Core_BAO_Address
::create($params, $fixAddress);
56 // when we come from a form which displays all the location elements (like the edit form or the inline block
57 // elements, we can skip the below check. The below check adds quite a feq queries to an already overloaded
59 if (empty($params['updateBlankLocInfo'])) {
60 // make sure contact should have only one primary block, CRM-5051
61 self
::checkPrimaryBlocks(CRM_Utils_Array
::value('contact_id', $params));
68 * Creates the entry in the civicrm_loc_block.
70 * @param array $location
71 * @param array $entityElements
75 public static function createLocBlock($location, $entityElements) {
76 $locId = self
::findExisting($entityElements);
80 $locBlock['id'] = $locId;
89 $locBlock["{$loc}_id"] = !empty($location["$loc"][0]) ?
$location["$loc"][0]->id
: NULL;
90 $locBlock["{$loc}_2_id"] = !empty($location["$loc"][1]) ?
$location["$loc"][1]->id
: NULL;
94 foreach ($locBlock as $key => $block) {
95 if (empty($locBlock[$key])) {
96 $locBlock[$key] = 'null';
101 if (count($locBlock) == $countNull) {
102 // implies nothing is set.
106 return self
::addLocBlock($locBlock)->id
;
110 * Takes an entity array and finds the existing location block.
112 * @param array $entityElements
116 public static function findExisting($entityElements) {
117 $eid = $entityElements['entity_id'];
118 $etable = $entityElements['entity_table'];
120 SELECT e.loc_block_id as locId
124 $params = [1 => [$eid, 'Integer']];
125 $dao = CRM_Core_DAO
::executeQuery($query, $params);
126 while ($dao->fetch()) {
127 $locBlockId = $dao->locId
;
133 * Takes an associative array and adds location block.
135 * @param array $params
137 * @return CRM_Core_DAO_LocBlock
138 * Object on success, null otherwise
140 public static function addLocBlock($params) {
141 $locBlock = new CRM_Core_DAO_LocBlock();
142 $locBlock->copyValues($params);
148 * Delete the Location Block.
150 * @param int $locBlockId
151 * Id of the Location Block.
153 public static function deleteLocBlock($locBlockId) {
158 $locBlock = new CRM_Core_DAO_LocBlock();
159 $locBlock->id
= $locBlockId;
161 $locBlock->find(TRUE);
163 //resolve conflict of having same ids for multiple blocks
165 'IM_1' => $locBlock->im_id
,
166 'IM_2' => $locBlock->im_2_id
,
167 'Email_1' => $locBlock->email_id
,
168 'Email_2' => $locBlock->email_2_id
,
169 'Phone_1' => $locBlock->phone_id
,
170 'Phone_2' => $locBlock->phone_2_id
,
171 'Address_1' => $locBlock->address_id
,
172 'Address_2' => $locBlock->address_2_id
,
175 foreach ($store as $daoName => $id) {
177 $daoName = 'CRM_Core_DAO_' . substr($daoName, 0, -2);
178 $dao = new $daoName();
187 * Check if there is data to create the object.
189 * @param array $params
190 * (reference ) an assoc array of name/value pairs.
194 public static function dataExists(&$params) {
195 // return if no data present
197 foreach (self
::$blocks as $block) {
198 if (array_key_exists($block, $params)) {
210 * @param array $entityBlock
211 * @param bool $microformat
213 * @return CRM_Core_BAO_Location[]|NULL
215 public static function getValues($entityBlock, $microformat = FALSE) {
216 if (empty($entityBlock)) {
222 'openid' => 'OpenID',
225 //get all the blocks for this contact
226 foreach (self
::$blocks as $block) {
227 if (array_key_exists($block, $name_map)) {
228 $name = $name_map[$block];
231 $name = ucfirst($block);
233 $baoString = 'CRM_Core_BAO_' . $name;
234 $blocks[$block] = $baoString::getValues($entityBlock, $microformat);
240 * Delete all the block associated with the location.
242 * @param int $contactId
244 * @param int $locationTypeId
245 * Id of the location to delete.
246 * @throws CRM_Core_Exception
248 public static function deleteLocationBlocks($contactId, $locationTypeId) {
249 // ensure that contactId has a value
250 if (empty($contactId) ||
251 !CRM_Utils_Rule
::positiveInteger($contactId)
253 throw new CRM_Core_Exception('Incorrect contact id parameter passed to deleteLocationBlocks');
256 if (empty($locationTypeId) ||
257 !CRM_Utils_Rule
::positiveInteger($locationTypeId)
259 // so we only delete the blocks which DO NOT have a location type Id
261 $locationTypeId = 'null';
264 static $blocks = ['Address', 'Phone', 'IM', 'OpenID', 'Email'];
266 $params = ['contact_id' => $contactId, 'location_type_id' => $locationTypeId];
267 foreach ($blocks as $name) {
268 CRM_Core_BAO_Block
::blockDelete($name, $params);
273 * Make sure contact should have only one primary block, CRM-5051.
275 * @param int $contactId
278 public static function checkPrimaryBlocks($contactId) {
283 // get the loc block ids.
284 $primaryLocBlockIds = CRM_Contact_BAO_Contact
::getLocBlockIds($contactId, ['is_primary' => 1]);
285 $nonPrimaryBlockIds = CRM_Contact_BAO_Contact
::getLocBlockIds($contactId, ['is_primary' => 0]);
294 $name = strtolower($block);
295 if (array_key_exists($name, $primaryLocBlockIds) &&
296 !CRM_Utils_System
::isNull($primaryLocBlockIds[$name])
298 if (count($primaryLocBlockIds[$name]) > 1) {
299 // keep only single block as primary.
300 $primaryId = array_pop($primaryLocBlockIds[$name]);
301 $resetIds = "(" . implode(',', $primaryLocBlockIds[$name]) . ")";
302 // reset all primary except one.
303 CRM_Core_DAO
::executeQuery("UPDATE civicrm_$name SET is_primary = 0 WHERE id IN $resetIds");
306 elseif (array_key_exists($name, $nonPrimaryBlockIds) &&
307 !CRM_Utils_System
::isNull($nonPrimaryBlockIds[$name])
309 // data exists and no primary block - make one primary.
310 CRM_Core_DAO
::setFieldValue("CRM_Core_DAO_" . $block,
311 array_pop($nonPrimaryBlockIds[$name]), 'is_primary', 1
318 * Get chain select values (whatever that means!).
320 * @param mixed $values
321 * @param string $valueType
322 * @param bool $flatten
326 public static function getChainSelectValues($values, $valueType, $flatten = FALSE) {
330 $values = array_filter((array) $values);
333 $method = $valueType == 'country' ?
'stateProvinceForCountry' : 'countyForState';
334 foreach ($values as $val) {
335 $result = CRM_Core_PseudoConstant
::$method($val);
337 // Format for quickform
339 // Option-groups for multiple categories
340 if ($result && count($values) > 1) {
341 $elements["crm_optgroup_$val"] = CRM_Core_PseudoConstant
::$valueType($val, FALSE);
343 $elements +
= $result;
348 // Option-groups for multiple categories
349 if ($result && count($values) > 1) {
351 'value' => CRM_Core_PseudoConstant
::$valueType($val, FALSE),
354 $list = &$elements[count($elements) - 1]['children'];
356 foreach ($result as $id => $name) {