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
42 public static function create(&$params, $fixAddress = TRUE, $entity = NULL) {
44 if (!self
::dataExists($params)) {
48 // create location blocks.
49 foreach (self
::$blocks as $block) {
50 if ($block != 'address') {
51 $location[$block] = CRM_Core_BAO_Block
::create($block, $params, $entity);
54 $location[$block] = CRM_Core_BAO_Address
::create($params, $fixAddress, $entity);
59 // this is a special case for adding values in location block table
61 'entity_table' => $params['entity_table'],
62 'entity_id' => $params['entity_id'],
65 $location['id'] = self
::createLocBlock($location, $entityElements);
68 // when we come from a form which displays all the location elements (like the edit form or the inline block
69 // elements, we can skip the below check. The below check adds quite a feq queries to an already overloaded
71 if (empty($params['updateBlankLocInfo'])) {
72 // make sure contact should have only one primary block, CRM-5051
73 self
::checkPrimaryBlocks(CRM_Utils_Array
::value('contact_id', $params));
81 * Creates the entry in the civicrm_loc_block.
83 * @param string $location
84 * @param array $entityElements
88 public static function createLocBlock(&$location, &$entityElements) {
89 $locId = self
::findExisting($entityElements);
93 $locBlock['id'] = $locId;
102 $locBlock["{$loc}_id"] = !empty($location["$loc"][0]) ?
$location["$loc"][0]->id
: NULL;
103 $locBlock["{$loc}_2_id"] = !empty($location["$loc"][1]) ?
$location["$loc"][1]->id
: NULL;
107 foreach ($locBlock as $key => $block) {
108 if (empty($locBlock[$key])) {
109 $locBlock[$key] = 'null';
114 if (count($locBlock) == $countNull) {
115 // implies nothing is set.
119 $locBlockInfo = self
::addLocBlock($locBlock);
120 return $locBlockInfo->id
;
124 * Takes an entity array and finds the existing location block.
126 * @param array $entityElements
130 public static function findExisting($entityElements) {
131 $eid = $entityElements['entity_id'];
132 $etable = $entityElements['entity_table'];
134 SELECT e.loc_block_id as locId
138 $params = [1 => [$eid, 'Integer']];
139 $dao = CRM_Core_DAO
::executeQuery($query, $params);
140 while ($dao->fetch()) {
141 $locBlockId = $dao->locId
;
147 * Takes an associative array and adds location block.
149 * @param array $params
150 * (reference ) an assoc array of name/value pairs.
152 * @return CRM_Core_BAO_locBlock
153 * Object on success, null otherwise
155 public static function addLocBlock(&$params) {
156 $locBlock = new CRM_Core_DAO_LocBlock();
158 $locBlock->copyValues($params);
160 return $locBlock->save();
164 * Delete the Location Block.
166 * @param int $locBlockId
167 * Id of the Location Block.
169 public static function deleteLocBlock($locBlockId) {
174 $locBlock = new CRM_Core_DAO_LocBlock();
175 $locBlock->id
= $locBlockId;
177 $locBlock->find(TRUE);
179 //resolve conflict of having same ids for multiple blocks
181 'IM_1' => $locBlock->im_id
,
182 'IM_2' => $locBlock->im_2_id
,
183 'Email_1' => $locBlock->email_id
,
184 'Email_2' => $locBlock->email_2_id
,
185 'Phone_1' => $locBlock->phone_id
,
186 'Phone_2' => $locBlock->phone_2_id
,
187 'Address_1' => $locBlock->address_id
,
188 'Address_2' => $locBlock->address_2_id
,
191 foreach ($store as $daoName => $id) {
193 $daoName = 'CRM_Core_DAO_' . substr($daoName, 0, -2);
194 $dao = new $daoName();
203 * Check if there is data to create the object.
205 * @param array $params
206 * (reference ) an assoc array of name/value pairs.
210 public static function dataExists(&$params) {
211 // return if no data present
213 foreach (self
::$blocks as $block) {
214 if (array_key_exists($block, $params)) {
226 * @param array $entityBlock
227 * @param bool $microformat
230 * array of objects(CRM_Core_BAO_Location)
232 public static function &getValues($entityBlock, $microformat = FALSE) {
233 if (empty($entityBlock)) {
239 'openid' => 'OpenID',
242 //get all the blocks for this contact
243 foreach (self
::$blocks as $block) {
244 if (array_key_exists($block, $name_map)) {
245 $name = $name_map[$block];
248 $name = ucfirst($block);
250 $baoString = 'CRM_Core_BAO_' . $name;
251 $blocks[$block] = $baoString::getValues($entityBlock, $microformat);
257 * Delete all the block associated with the location.
259 * @param int $contactId
261 * @param int $locationTypeId
262 * Id of the location to delete.
264 public static function deleteLocationBlocks($contactId, $locationTypeId) {
265 // ensure that contactId has a value
266 if (empty($contactId) ||
267 !CRM_Utils_Rule
::positiveInteger($contactId)
269 CRM_Core_Error
::fatal();
272 if (empty($locationTypeId) ||
273 !CRM_Utils_Rule
::positiveInteger($locationTypeId)
275 // so we only delete the blocks which DO NOT have a location type Id
277 $locationTypeId = 'null';
280 static $blocks = ['Address', 'Phone', 'IM', 'OpenID', 'Email'];
282 $params = ['contact_id' => $contactId, 'location_type_id' => $locationTypeId];
283 foreach ($blocks as $name) {
284 CRM_Core_BAO_Block
::blockDelete($name, $params);
289 * Copy or update location block.
291 * @param int $locBlockId
293 * @param int $updateLocBlockId
294 * Update location block id.
297 * newly created/updated location block id.
299 public static function copyLocBlock($locBlockId, $updateLocBlockId = NULL) {
300 CRM_Core_Error
::deprecatedFunctionWarning('unused function which will be removed');
301 //get the location info.
302 $defaults = $updateValues = [];
303 $locBlock = ['id' => $locBlockId];
304 CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_LocBlock', $locBlock, $defaults);
306 if ($updateLocBlockId) {
307 //get the location info for update.
308 $copyLocationParams = ['id' => $updateLocBlockId];
309 CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_LocBlock', $copyLocationParams, $updateValues);
310 foreach ($updateValues as $key => $value) {
312 $copyLocationParams[$key] = 'null';
317 //copy all location blocks (email, phone, address, etc)
318 foreach ($defaults as $key => $value) {
320 $tbl = explode("_", $key);
321 $name = ucfirst($tbl[0]);
322 $updateParams = NULL;
323 if ($updateId = CRM_Utils_Array
::value($key, $updateValues)) {
324 $updateParams = ['id' => $updateId];
327 $copy = CRM_Core_DAO
::copyGeneric('CRM_Core_DAO_' . $name, ['id' => $value], $updateParams);
328 $copyLocationParams[$key] = $copy->id
;
332 $copyLocation = CRM_Core_DAO
::copyGeneric('CRM_Core_DAO_LocBlock',
333 ['id' => $locBlock['id']],
336 return $copyLocation->id
;
340 * Make sure contact should have only one primary block, CRM-5051.
342 * @param int $contactId
345 public static function checkPrimaryBlocks($contactId) {
350 // get the loc block ids.
351 $primaryLocBlockIds = CRM_Contact_BAO_Contact
::getLocBlockIds($contactId, ['is_primary' => 1]);
352 $nonPrimaryBlockIds = CRM_Contact_BAO_Contact
::getLocBlockIds($contactId, ['is_primary' => 0]);
361 $name = strtolower($block);
362 if (array_key_exists($name, $primaryLocBlockIds) &&
363 !CRM_Utils_System
::isNull($primaryLocBlockIds[$name])
365 if (count($primaryLocBlockIds[$name]) > 1) {
366 // keep only single block as primary.
367 $primaryId = array_pop($primaryLocBlockIds[$name]);
368 $resetIds = "(" . implode(',', $primaryLocBlockIds[$name]) . ")";
369 // reset all primary except one.
370 CRM_Core_DAO
::executeQuery("UPDATE civicrm_$name SET is_primary = 0 WHERE id IN $resetIds");
373 elseif (array_key_exists($name, $nonPrimaryBlockIds) &&
374 !CRM_Utils_System
::isNull($nonPrimaryBlockIds[$name])
376 // data exists and no primary block - make one primary.
377 CRM_Core_DAO
::setFieldValue("CRM_Core_DAO_" . $block,
378 array_pop($nonPrimaryBlockIds[$name]), 'is_primary', 1
385 * Get chain select values (whatever that means!).
387 * @param mixed $values
388 * @param string $valueType
389 * @param bool $flatten
393 public static function getChainSelectValues($values, $valueType, $flatten = FALSE) {
397 $values = array_filter((array) $values);
400 $method = $valueType == 'country' ?
'stateProvinceForCountry' : 'countyForState';
401 foreach ($values as $val) {
402 $result = CRM_Core_PseudoConstant
::$method($val);
404 // Format for quickform
406 // Option-groups for multiple categories
407 if ($result && count($values) > 1) {
408 $elements["crm_optgroup_$val"] = CRM_Core_PseudoConstant
::$valueType($val, FALSE);
410 $elements +
= $result;
415 // Option-groups for multiple categories
416 if ($result && count($values) > 1) {
418 'value' => CRM_Core_PseudoConstant
::$valueType($val, FALSE),
421 $list = &$elements[count($elements) - 1]['children'];
423 foreach ($result as $id => $name) {