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);
51 elseif (is_array($params['address'] ??
NULL)) {
52 $location[$block] = CRM_Core_BAO_Address
::legacyCreate($params, $fixAddress);
60 * Creates the entry in the civicrm_loc_block.
62 * @param array $location
63 * @param array $entityElements
67 public static function createLocBlock($location, $entityElements) {
68 CRM_Core_Error
::deprecatedFunctionWarning('Use LocBlock api');
69 $locId = self
::findExisting($entityElements);
73 $locBlock['id'] = $locId;
82 $locBlock["{$loc}_id"] = !empty($location["$loc"][0]) ?
$location["$loc"][0]->id
: NULL;
83 $locBlock["{$loc}_2_id"] = !empty($location["$loc"][1]) ?
$location["$loc"][1]->id
: NULL;
87 foreach ($locBlock as $key => $block) {
88 if (empty($locBlock[$key])) {
89 $locBlock[$key] = 'null';
94 if (count($locBlock) == $countNull) {
95 // implies nothing is set.
99 return self
::addLocBlock($locBlock)->id
;
103 * Takes an entity array and finds the existing location block.
105 * @param array $entityElements
109 public static function findExisting($entityElements) {
110 $eid = $entityElements['entity_id'];
111 $etable = $entityElements['entity_table'];
113 SELECT e.loc_block_id as locId
117 $params = [1 => [$eid, 'Integer']];
118 $dao = CRM_Core_DAO
::executeQuery($query, $params);
119 while ($dao->fetch()) {
120 $locBlockId = $dao->locId
;
126 * Takes an associative array and adds location block.
128 * @param array $params
130 * @return CRM_Core_DAO_LocBlock
131 * Object on success, null otherwise
133 public static function addLocBlock($params) {
134 $locBlock = new CRM_Core_DAO_LocBlock();
135 $locBlock->copyValues($params);
141 * Delete the Location Block.
143 * @param int $locBlockId
144 * Id of the Location Block.
146 public static function deleteLocBlock($locBlockId) {
151 $locBlock = new CRM_Core_DAO_LocBlock();
152 $locBlock->id
= $locBlockId;
154 $locBlock->find(TRUE);
156 //resolve conflict of having same ids for multiple blocks
158 'IM_1' => $locBlock->im_id
,
159 'IM_2' => $locBlock->im_2_id
,
160 'Email_1' => $locBlock->email_id
,
161 'Email_2' => $locBlock->email_2_id
,
162 'Phone_1' => $locBlock->phone_id
,
163 'Phone_2' => $locBlock->phone_2_id
,
164 'Address_1' => $locBlock->address_id
,
165 'Address_2' => $locBlock->address_2_id
,
168 foreach ($store as $daoName => $id) {
170 $daoName = 'CRM_Core_DAO_' . substr($daoName, 0, -2);
171 $dao = new $daoName();
180 * Check if there is data to create the object.
182 * @param array $params
183 * (reference ) an assoc array of name/value pairs.
187 public static function dataExists(&$params) {
188 // return if no data present
190 foreach (self
::$blocks as $block) {
191 if (array_key_exists($block, $params)) {
203 * @param array $entityBlock
204 * @param bool $microformat
206 * @return CRM_Core_BAO_Location[]|NULL
208 public static function getValues($entityBlock, $microformat = FALSE) {
209 if (empty($entityBlock)) {
215 'openid' => 'OpenID',
218 //get all the blocks for this contact
219 foreach (self
::$blocks as $block) {
220 if (array_key_exists($block, $name_map)) {
221 $name = $name_map[$block];
224 $name = ucfirst($block);
226 $baoString = 'CRM_Core_BAO_' . $name;
227 $blocks[$block] = $baoString::getValues($entityBlock, $microformat);
233 * Delete all the block associated with the location.
235 * Note a universe search on 1 Oct 2020 found no calls to this function.
239 * @param int $contactId
241 * @param int $locationTypeId
242 * Id of the location to delete.
243 * @throws CRM_Core_Exception
245 public static function deleteLocationBlocks($contactId, $locationTypeId) {
246 CRM_Core_Error
::deprecatedFunctionWarning('Use v4 api');
247 // ensure that contactId has a value
248 if (empty($contactId) ||
249 !CRM_Utils_Rule
::positiveInteger($contactId)
251 throw new CRM_Core_Exception('Incorrect contact id parameter passed to deleteLocationBlocks');
254 if (empty($locationTypeId) ||
255 !CRM_Utils_Rule
::positiveInteger($locationTypeId)
257 // so we only delete the blocks which DO NOT have a location type Id
259 $locationTypeId = 'null';
262 static $blocks = ['Address', 'Phone', 'IM', 'OpenID', 'Email'];
264 $params = ['contact_id' => $contactId, 'location_type_id' => $locationTypeId];
265 foreach ($blocks as $name) {
266 CRM_Core_BAO_Block
::blockDelete($name, $params);
271 * Make sure contact should have only one primary block, CRM-5051.
273 * @param int $contactId
276 public static function checkPrimaryBlocks($contactId) {
281 // get the loc block ids.
282 $primaryLocBlockIds = CRM_Contact_BAO_Contact
::getLocBlockIds($contactId, ['is_primary' => 1]);
283 $nonPrimaryBlockIds = CRM_Contact_BAO_Contact
::getLocBlockIds($contactId, ['is_primary' => 0]);
292 $name = strtolower($block);
293 if (array_key_exists($name, $primaryLocBlockIds) &&
294 !CRM_Utils_System
::isNull($primaryLocBlockIds[$name])
296 if (count($primaryLocBlockIds[$name]) > 1) {
297 // keep only single block as primary.
298 $primaryId = array_pop($primaryLocBlockIds[$name]);
299 $resetIds = "(" . implode(',', $primaryLocBlockIds[$name]) . ")";
300 // reset all primary except one.
301 CRM_Core_DAO
::executeQuery("UPDATE civicrm_$name SET is_primary = 0 WHERE id IN $resetIds");
304 elseif (array_key_exists($name, $nonPrimaryBlockIds) &&
305 !CRM_Utils_System
::isNull($nonPrimaryBlockIds[$name])
307 // data exists and no primary block - make one primary.
308 CRM_Core_DAO
::setFieldValue("CRM_Core_DAO_" . $block,
309 array_pop($nonPrimaryBlockIds[$name]), 'is_primary', 1
316 * Get chain select values (whatever that means!).
318 * @param mixed $values
319 * @param string $valueType
320 * @param bool $flatten
324 public static function getChainSelectValues($values, $valueType, $flatten = FALSE) {
328 $values = array_filter((array) $values);
331 $method = $valueType == 'country' ?
'stateProvinceForCountry' : 'countyForState';
332 foreach ($values as $val) {
333 $result = CRM_Core_PseudoConstant
::$method($val);
335 // Format for quickform
337 // Option-groups for multiple categories
338 if ($result && count($values) > 1) {
339 $elements["crm_optgroup_$val"] = CRM_Core_PseudoConstant
::$valueType($val, FALSE);
341 $elements +
= $result;
346 // Option-groups for multiple categories
347 if ($result && count($values) > 1) {
349 'value' => CRM_Core_PseudoConstant
::$valueType($val, FALSE),
352 $list = &$elements[count($elements) - 1]['children'];
354 foreach ($result as $id => $name) {