3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
35 * This class handle creation of location block elements.
37 class CRM_Core_BAO_Location
extends CRM_Core_DAO
{
40 * Location block element array.
43 public static $blocks = ['phone', 'email', 'im', 'openid', 'address'];
46 * Create various elements of location block.
48 * @param array $params
49 * (reference ) an assoc array of name/value pairs.
50 * @param bool $fixAddress
51 * True if you need to fix (format) address values.
52 * before inserting in db
58 public static function create(&$params, $fixAddress = TRUE, $entity = NULL) {
60 if (!self
::dataExists($params)) {
64 // create location blocks.
65 foreach (self
::$blocks as $block) {
66 if ($block != 'address') {
67 $location[$block] = CRM_Core_BAO_Block
::create($block, $params, $entity);
70 $location[$block] = CRM_Core_BAO_Address
::create($params, $fixAddress, $entity);
75 // this is a special case for adding values in location block table
77 'entity_table' => $params['entity_table'],
78 'entity_id' => $params['entity_id'],
81 $location['id'] = self
::createLocBlock($location, $entityElements);
84 // when we come from a form which displays all the location elements (like the edit form or the inline block
85 // elements, we can skip the below check. The below check adds quite a feq queries to an already overloaded
87 if (empty($params['updateBlankLocInfo'])) {
88 // make sure contact should have only one primary block, CRM-5051
89 self
::checkPrimaryBlocks(CRM_Utils_Array
::value('contact_id', $params));
97 * Creates the entry in the civicrm_loc_block.
99 * @param string $location
100 * @param array $entityElements
104 public static function createLocBlock(&$location, &$entityElements) {
105 $locId = self
::findExisting($entityElements);
109 $locBlock['id'] = $locId;
118 $locBlock["{$loc}_id"] = !empty($location["$loc"][0]) ?
$location["$loc"][0]->id
: NULL;
119 $locBlock["{$loc}_2_id"] = !empty($location["$loc"][1]) ?
$location["$loc"][1]->id
: NULL;
123 foreach ($locBlock as $key => $block) {
124 if (empty($locBlock[$key])) {
125 $locBlock[$key] = 'null';
130 if (count($locBlock) == $countNull) {
131 // implies nothing is set.
135 $locBlockInfo = self
::addLocBlock($locBlock);
136 return $locBlockInfo->id
;
140 * Takes an entity array and finds the existing location block.
142 * @param array $entityElements
146 public static function findExisting($entityElements) {
147 $eid = $entityElements['entity_id'];
148 $etable = $entityElements['entity_table'];
150 SELECT e.loc_block_id as locId
154 $params = [1 => [$eid, 'Integer']];
155 $dao = CRM_Core_DAO
::executeQuery($query, $params);
156 while ($dao->fetch()) {
157 $locBlockId = $dao->locId
;
163 * Takes an associative array and adds location block.
165 * @param array $params
166 * (reference ) an assoc array of name/value pairs.
168 * @return CRM_Core_BAO_locBlock
169 * Object on success, null otherwise
171 public static function addLocBlock(&$params) {
172 $locBlock = new CRM_Core_DAO_LocBlock();
174 $locBlock->copyValues($params);
176 return $locBlock->save();
180 * Delete the Location Block.
182 * @param int $locBlockId
183 * Id of the Location Block.
185 public static function deleteLocBlock($locBlockId) {
190 $locBlock = new CRM_Core_DAO_LocBlock();
191 $locBlock->id
= $locBlockId;
193 $locBlock->find(TRUE);
195 //resolve conflict of having same ids for multiple blocks
197 'IM_1' => $locBlock->im_id
,
198 'IM_2' => $locBlock->im_2_id
,
199 'Email_1' => $locBlock->email_id
,
200 'Email_2' => $locBlock->email_2_id
,
201 'Phone_1' => $locBlock->phone_id
,
202 'Phone_2' => $locBlock->phone_2_id
,
203 'Address_1' => $locBlock->address_id
,
204 'Address_2' => $locBlock->address_2_id
,
207 foreach ($store as $daoName => $id) {
209 $daoName = 'CRM_Core_DAO_' . substr($daoName, 0, -2);
210 $dao = new $daoName();
219 * Check if there is data to create the object.
221 * @param array $params
222 * (reference ) an assoc array of name/value pairs.
226 public static function dataExists(&$params) {
227 // return if no data present
229 foreach (self
::$blocks as $block) {
230 if (array_key_exists($block, $params)) {
242 * @param array $entityBlock
243 * @param bool $microformat
246 * array of objects(CRM_Core_BAO_Location)
248 public static function &getValues($entityBlock, $microformat = FALSE) {
249 if (empty($entityBlock)) {
255 'openid' => 'OpenID',
258 //get all the blocks for this contact
259 foreach (self
::$blocks as $block) {
260 if (array_key_exists($block, $name_map)) {
261 $name = $name_map[$block];
264 $name = ucfirst($block);
266 $baoString = 'CRM_Core_BAO_' . $name;
267 $blocks[$block] = $baoString::getValues($entityBlock, $microformat);
273 * Delete all the block associated with the location.
275 * @param int $contactId
277 * @param int $locationTypeId
278 * Id of the location to delete.
280 public static function deleteLocationBlocks($contactId, $locationTypeId) {
281 // ensure that contactId has a value
282 if (empty($contactId) ||
283 !CRM_Utils_Rule
::positiveInteger($contactId)
285 CRM_Core_Error
::fatal();
288 if (empty($locationTypeId) ||
289 !CRM_Utils_Rule
::positiveInteger($locationTypeId)
291 // so we only delete the blocks which DO NOT have a location type Id
293 $locationTypeId = 'null';
296 static $blocks = ['Address', 'Phone', 'IM', 'OpenID', 'Email'];
298 $params = ['contact_id' => $contactId, 'location_type_id' => $locationTypeId];
299 foreach ($blocks as $name) {
300 CRM_Core_BAO_Block
::blockDelete($name, $params);
305 * Copy or update location block.
307 * @param int $locBlockId
309 * @param int $updateLocBlockId
310 * Update location block id.
313 * newly created/updated location block id.
315 public static function copyLocBlock($locBlockId, $updateLocBlockId = NULL) {
316 CRM_Core_Error
::deprecatedFunctionWarning('unused function which will be removed');
317 //get the location info.
318 $defaults = $updateValues = [];
319 $locBlock = ['id' => $locBlockId];
320 CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_LocBlock', $locBlock, $defaults);
322 if ($updateLocBlockId) {
323 //get the location info for update.
324 $copyLocationParams = ['id' => $updateLocBlockId];
325 CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_LocBlock', $copyLocationParams, $updateValues);
326 foreach ($updateValues as $key => $value) {
328 $copyLocationParams[$key] = 'null';
333 //copy all location blocks (email, phone, address, etc)
334 foreach ($defaults as $key => $value) {
336 $tbl = explode("_", $key);
337 $name = ucfirst($tbl[0]);
338 $updateParams = NULL;
339 if ($updateId = CRM_Utils_Array
::value($key, $updateValues)) {
340 $updateParams = ['id' => $updateId];
343 $copy = CRM_Core_DAO
::copyGeneric('CRM_Core_DAO_' . $name, ['id' => $value], $updateParams);
344 $copyLocationParams[$key] = $copy->id
;
348 $copyLocation = CRM_Core_DAO
::copyGeneric('CRM_Core_DAO_LocBlock',
349 ['id' => $locBlock['id']],
352 return $copyLocation->id
;
356 * Make sure contact should have only one primary block, CRM-5051.
358 * @param int $contactId
361 public static function checkPrimaryBlocks($contactId) {
366 // get the loc block ids.
367 $primaryLocBlockIds = CRM_Contact_BAO_Contact
::getLocBlockIds($contactId, ['is_primary' => 1]);
368 $nonPrimaryBlockIds = CRM_Contact_BAO_Contact
::getLocBlockIds($contactId, ['is_primary' => 0]);
377 $name = strtolower($block);
378 if (array_key_exists($name, $primaryLocBlockIds) &&
379 !CRM_Utils_System
::isNull($primaryLocBlockIds[$name])
381 if (count($primaryLocBlockIds[$name]) > 1) {
382 // keep only single block as primary.
383 $primaryId = array_pop($primaryLocBlockIds[$name]);
384 $resetIds = "(" . implode(',', $primaryLocBlockIds[$name]) . ")";
385 // reset all primary except one.
386 CRM_Core_DAO
::executeQuery("UPDATE civicrm_$name SET is_primary = 0 WHERE id IN $resetIds");
389 elseif (array_key_exists($name, $nonPrimaryBlockIds) &&
390 !CRM_Utils_System
::isNull($nonPrimaryBlockIds[$name])
392 // data exists and no primary block - make one primary.
393 CRM_Core_DAO
::setFieldValue("CRM_Core_DAO_" . $block,
394 array_pop($nonPrimaryBlockIds[$name]), 'is_primary', 1
401 * Get chain select values (whatever that means!).
403 * @param mixed $values
404 * @param string $valueType
405 * @param bool $flatten
409 public static function getChainSelectValues($values, $valueType, $flatten = FALSE) {
413 $values = array_filter((array) $values);
416 $method = $valueType == 'country' ?
'stateProvinceForCountry' : 'countyForState';
417 foreach ($values as $val) {
418 $result = CRM_Core_PseudoConstant
::$method($val);
420 // Format for quickform
422 // Option-groups for multiple categories
423 if ($result && count($values) > 1) {
424 $elements["crm_optgroup_$val"] = CRM_Core_PseudoConstant
::$valueType($val, FALSE);
426 $elements +
= $result;
431 // Option-groups for multiple categories
432 if ($result && count($values) > 1) {
434 'value' => CRM_Core_PseudoConstant
::$valueType($val, FALSE),
437 $list = &$elements[count($elements) - 1]['children'];
439 foreach ($result as $id => $name) {