3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
37 * This class handle creation of location block elements
39 class CRM_Core_BAO_Location
extends CRM_Core_DAO
{
42 * Location block element array.
44 static $blocks = array('phone', 'email', 'im', 'openid', 'address');
47 * Create various elements of location block.
49 * @param array $params
50 * (reference ) an assoc array of name/value pairs.
51 * @param bool $fixAddress
52 * True if you need to fix (format) address values.
53 * before inserting in db
59 public static function create(&$params, $fixAddress = TRUE, $entity = NULL) {
61 if (!self
::dataExists($params)) {
65 // create location blocks.
66 foreach (self
::$blocks as $block) {
67 if ($block != 'address') {
68 $location[$block] = CRM_Core_BAO_Block
::create($block, $params, $entity);
71 $location[$block] = CRM_Core_BAO_Address
::create($params, $fixAddress, $entity);
76 // this is a special case for adding values in location block table
77 $entityElements = array(
78 'entity_table' => $params['entity_table'],
79 'entity_id' => $params['entity_id'],
82 $location['id'] = self
::createLocBlock($location, $entityElements);
85 // when we come from a form which displays all the location elements (like the edit form or the inline block
86 // elements, we can skip the below check. The below check adds quite a feq queries to an already overloaded
88 if (!CRM_Utils_Array
::value('updateBlankLocInfo', $params, FALSE)) {
89 // make sure contact should have only one primary block, CRM-5051
90 self
::checkPrimaryBlocks(CRM_Utils_Array
::value('contact_id', $params));
98 * Get the ID of the database billing location.
101 * Billing location type id.
103 * @throws \CRM_Core_Exception
105 public static function getBillingLocationId() {
106 static $billingTypeID = NULL;
107 if ($billingTypeID) {
108 return $billingTypeID;
110 $locationTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id', array(), 'validate');
111 $billingTypeID = array_search('Billing', $locationTypes);
112 if (!$billingTypeID) {
113 throw new CRM_Core_Exception(ts('Please set a location type of %1', array(1 => 'Billing')));
115 return $billingTypeID;
118 * Creates the entry in the civicrm_loc_block.
120 public static function createLocBlock(&$location, &$entityElements) {
121 $locId = self
::findExisting($entityElements);
125 $locBlock['id'] = $locId;
134 $locBlock["{$loc}_id"] = !empty($location["$loc"][0]) ?
$location["$loc"][0]->id
: NULL;
135 $locBlock["{$loc}_2_id"] = !empty($location["$loc"][1]) ?
$location["$loc"][1]->id
: NULL;
139 foreach ($locBlock as $key => $block) {
140 if (empty($locBlock[$key])) {
141 $locBlock[$key] = 'null';
146 if (count($locBlock) == $countNull) {
147 // implies nothing is set.
151 $locBlockInfo = self
::addLocBlock($locBlock);
152 return $locBlockInfo->id
;
156 * Takes an entity array and finds the existing location block.
158 public static function findExisting($entityElements) {
159 $eid = $entityElements['entity_id'];
160 $etable = $entityElements['entity_table'];
162 SELECT e.loc_block_id as locId
166 $params = array(1 => array($eid, 'Integer'));
167 $dao = CRM_Core_DAO
::executeQuery($query, $params);
168 while ($dao->fetch()) {
169 $locBlockId = $dao->locId
;
175 * Takes an associative array and adds location block.
177 * @param array $params
178 * (reference ) an assoc array of name/value pairs.
181 * CRM_Core_BAO_locBlock object on success, null otherwise
183 public static function addLocBlock(&$params) {
184 $locBlock = new CRM_Core_DAO_LocBlock();
186 $locBlock->copyValues($params);
188 return $locBlock->save();
192 * Delete the Location Block.
194 * @param int $locBlockId
195 * Id of the Location Block.
199 public static function deleteLocBlock($locBlockId) {
204 $locBlock = new CRM_Core_DAO_LocBlock();
205 $locBlock->id
= $locBlockId;
207 $locBlock->find(TRUE);
209 //resolve conflict of having same ids for multiple blocks
211 'IM_1' => $locBlock->im_id
,
212 'IM_2' => $locBlock->im_2_id
,
213 'Email_1' => $locBlock->email_id
,
214 'Email_2' => $locBlock->email_2_id
,
215 'Phone_1' => $locBlock->phone_id
,
216 'Phone_2' => $locBlock->phone_2_id
,
217 'Address_1' => $locBlock->address_id
,
218 'Address_2' => $locBlock->address_2_id
,
221 foreach ($store as $daoName => $id) {
223 $daoName = 'CRM_Core_DAO_' . substr($daoName, 0, -2);
224 $dao = new $daoName();
234 * Check if there is data to create the object.
236 * @param array $params
237 * (reference ) an assoc array of name/value pairs.
241 public static function dataExists(&$params) {
242 // return if no data present
244 foreach (self
::$blocks as $block) {
245 if (array_key_exists($block, $params)) {
255 * @param $entityBlock
256 * @param bool $microformat
259 * array of objects(CRM_Core_BAO_Location)
261 public static function &getValues($entityBlock, $microformat = FALSE) {
262 if (empty($entityBlock)) {
268 'openid' => 'OpenID',
271 //get all the blocks for this contact
272 foreach (self
::$blocks as $block) {
273 if (array_key_exists($block, $name_map)) {
274 $name = $name_map[$block];
277 $name = ucfirst($block);
279 $baoString = 'CRM_Core_BAO_' . $name;
280 $blocks[$block] = $baoString::getValues($entityBlock, $microformat);
286 * Delete all the block associated with the location.
288 * @param int $contactId
290 * @param int $locationTypeId
291 * Id of the location to delete.
295 public static function deleteLocationBlocks($contactId, $locationTypeId) {
296 // ensure that contactId has a value
297 if (empty($contactId) ||
298 !CRM_Utils_Rule
::positiveInteger($contactId)
300 CRM_Core_Error
::fatal();
303 if (empty($locationTypeId) ||
304 !CRM_Utils_Rule
::positiveInteger($locationTypeId)
306 // so we only delete the blocks which DO NOT have a location type Id
308 $locationTypeId = 'null';
311 static $blocks = array('Address', 'Phone', 'IM', 'OpenID', 'Email');
313 $params = array('contact_id' => $contactId, 'location_type_id' => $locationTypeId);
314 foreach ($blocks as $name) {
315 CRM_Core_BAO_Block
::blockDelete($name, $params);
320 * Copy or update location block.
322 * @param int $locBlockId
324 * @param int $updateLocBlockId
325 * Update location block id.
328 * newly created/updated location block id.
330 public static function copyLocBlock($locBlockId, $updateLocBlockId = NULL) {
331 //get the location info.
332 $defaults = $updateValues = array();
333 $locBlock = array('id' => $locBlockId);
334 CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_LocBlock', $locBlock, $defaults);
336 if ($updateLocBlockId) {
337 //get the location info for update.
338 $copyLocationParams = array('id' => $updateLocBlockId);
339 CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_LocBlock', $copyLocationParams, $updateValues);
340 foreach ($updateValues as $key => $value) {
342 $copyLocationParams[$key] = 'null';
347 //copy all location blocks (email, phone, address, etc)
348 foreach ($defaults as $key => $value) {
350 $tbl = explode("_", $key);
351 $name = ucfirst($tbl[0]);
352 $updateParams = NULL;
353 if ($updateId = CRM_Utils_Array
::value($key, $updateValues)) {
354 $updateParams = array('id' => $updateId);
357 $copy = CRM_Core_DAO
::copyGeneric('CRM_Core_DAO_' . $name, array('id' => $value), $updateParams);
358 $copyLocationParams[$key] = $copy->id
;
362 $copyLocation = &CRM_Core_DAO
::copyGeneric('CRM_Core_DAO_LocBlock',
363 array('id' => $locBlock['id']),
366 return $copyLocation->id
;
370 * If contact has data for any location block, make sure
371 * contact should have only one primary block, CRM-5051
373 * @param int $contactId
377 public static function checkPrimaryBlocks($contactId) {
382 // get the loc block ids.
383 $primaryLocBlockIds = CRM_Contact_BAO_Contact
::getLocBlockIds($contactId, array('is_primary' => 1));
384 $nonPrimaryBlockIds = CRM_Contact_BAO_Contact
::getLocBlockIds($contactId, array('is_primary' => 0));
393 $name = strtolower($block);
394 if (array_key_exists($name, $primaryLocBlockIds) &&
395 !CRM_Utils_System
::isNull($primaryLocBlockIds[$name])
397 if (count($primaryLocBlockIds[$name]) > 1) {
398 // keep only single block as primary.
399 $primaryId = array_pop($primaryLocBlockIds[$name]);
400 $resetIds = "(" . implode(',', $primaryLocBlockIds[$name]) . ")";
401 // reset all primary except one.
402 CRM_Core_DAO
::executeQuery("UPDATE civicrm_$name SET is_primary = 0 WHERE id IN $resetIds");
405 elseif (array_key_exists($name, $nonPrimaryBlockIds) &&
406 !CRM_Utils_System
::isNull($nonPrimaryBlockIds[$name])
408 // data exists and no primary block - make one primary.
409 CRM_Core_DAO
::setFieldValue("CRM_Core_DAO_" . $block,
410 array_pop($nonPrimaryBlockIds[$name]), 'is_primary', 1
417 * @param mixed $values
418 * @param string $valueType
419 * @param bool $flatten
423 public static function getChainSelectValues($values, $valueType, $flatten = FALSE) {
427 $values = array_filter((array) $values);
430 $method = $valueType == 'country' ?
'stateProvinceForCountry' : 'countyForState';
431 foreach ($values as $val) {
432 $result = CRM_Core_PseudoConstant
::$method($val);
434 // Format for quickform
436 // Option-groups for multiple categories
437 if ($result && count($values) > 1) {
438 $elements["crm_optgroup_$val"] = CRM_Core_PseudoConstant
::$valueType($val, FALSE);
440 $elements +
= $result;
445 // Option-groups for multiple categories
446 if ($result && count($values) > 1) {
448 'value' => CRM_Core_PseudoConstant
::$valueType($val, FALSE),
449 'children' => array(),
451 $list = &$elements[count($elements) - 1]['children'];
453 foreach ($result as $id => $name) {