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
229 * @return CRM_Core_BAO_Location[]|NULL
231 public static function getValues($entityBlock, $microformat = FALSE) {
232 if (empty($entityBlock)) {
238 'openid' => 'OpenID',
241 //get all the blocks for this contact
242 foreach (self
::$blocks as $block) {
243 if (array_key_exists($block, $name_map)) {
244 $name = $name_map[$block];
247 $name = ucfirst($block);
249 $baoString = 'CRM_Core_BAO_' . $name;
250 $blocks[$block] = $baoString::getValues($entityBlock, $microformat);
256 * Delete all the block associated with the location.
258 * @param int $contactId
260 * @param int $locationTypeId
261 * Id of the location to delete.
262 * @throws CRM_Core_Exception
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 throw new CRM_Core_Exception('Incorrect contact id parameter passed to deleteLocationBlocks');
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 * Make sure contact should have only one primary block, CRM-5051.
291 * @param int $contactId
294 public static function checkPrimaryBlocks($contactId) {
299 // get the loc block ids.
300 $primaryLocBlockIds = CRM_Contact_BAO_Contact
::getLocBlockIds($contactId, ['is_primary' => 1]);
301 $nonPrimaryBlockIds = CRM_Contact_BAO_Contact
::getLocBlockIds($contactId, ['is_primary' => 0]);
310 $name = strtolower($block);
311 if (array_key_exists($name, $primaryLocBlockIds) &&
312 !CRM_Utils_System
::isNull($primaryLocBlockIds[$name])
314 if (count($primaryLocBlockIds[$name]) > 1) {
315 // keep only single block as primary.
316 $primaryId = array_pop($primaryLocBlockIds[$name]);
317 $resetIds = "(" . implode(',', $primaryLocBlockIds[$name]) . ")";
318 // reset all primary except one.
319 CRM_Core_DAO
::executeQuery("UPDATE civicrm_$name SET is_primary = 0 WHERE id IN $resetIds");
322 elseif (array_key_exists($name, $nonPrimaryBlockIds) &&
323 !CRM_Utils_System
::isNull($nonPrimaryBlockIds[$name])
325 // data exists and no primary block - make one primary.
326 CRM_Core_DAO
::setFieldValue("CRM_Core_DAO_" . $block,
327 array_pop($nonPrimaryBlockIds[$name]), 'is_primary', 1
334 * Get chain select values (whatever that means!).
336 * @param mixed $values
337 * @param string $valueType
338 * @param bool $flatten
342 public static function getChainSelectValues($values, $valueType, $flatten = FALSE) {
346 $values = array_filter((array) $values);
349 $method = $valueType == 'country' ?
'stateProvinceForCountry' : 'countyForState';
350 foreach ($values as $val) {
351 $result = CRM_Core_PseudoConstant
::$method($val);
353 // Format for quickform
355 // Option-groups for multiple categories
356 if ($result && count($values) > 1) {
357 $elements["crm_optgroup_$val"] = CRM_Core_PseudoConstant
::$valueType($val, FALSE);
359 $elements +
= $result;
364 // Option-groups for multiple categories
365 if ($result && count($values) > 1) {
367 'value' => CRM_Core_PseudoConstant
::$valueType($val, FALSE),
370 $list = &$elements[count($elements) - 1]['children'];
372 foreach ($result as $id => $name) {