3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
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 (reference ) an assoc array of name/value pairs
50 * @param boolean $fixAddress true if you need to fix (format) address values
51 * before inserting in db
55 * @return array $location
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
76 $entityElements = array(
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 (!CRM_Utils_Array
::value('updateBlankLocInfo', $params, FALSE)) {
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
100 public static function createLocBlock(&$location, &$entityElements) {
101 $locId = self
::findExisting($entityElements);
105 $locBlock['id'] = $locId;
109 'phone', 'email', 'im', 'address') as $loc) {
110 $locBlock["{$loc}_id"] = !empty($location["$loc"][0]) ?
$location["$loc"][0]->id
: NULL;
111 $locBlock["{$loc}_2_id"] = !empty($location["$loc"][1]) ?
$location["$loc"][1]->id
: NULL;
115 foreach ($locBlock as $key => $block) {
116 if (empty($locBlock[$key])) {
117 $locBlock[$key] = 'null';
122 if (count($locBlock) == $countNull) {
123 // implies nothing is set.
127 $locBlockInfo = self
::addLocBlock($locBlock);
128 return $locBlockInfo->id
;
132 * Takes an entity array and finds the existing location block
135 public static function findExisting($entityElements) {
136 $eid = $entityElements['entity_id'];
137 $etable = $entityElements['entity_table'];
139 SELECT e.loc_block_id as locId
143 $params = array(1 => array($eid, 'Integer'));
144 $dao = CRM_Core_DAO
::executeQuery($query, $params);
145 while ($dao->fetch()) {
146 $locBlockId = $dao->locId
;
152 * Takes an associative array and adds location block
154 * @param array $params (reference ) an assoc array of name/value pairs
156 * @return object CRM_Core_BAO_locBlock object on success, null otherwise
159 public static function addLocBlock(&$params) {
160 $locBlock = new CRM_Core_DAO_LocBlock();
162 $locBlock->copyValues($params);
164 return $locBlock->save();
168 * Delete the Location Block
170 * @param int $locBlockId id of the Location Block
175 public static function deleteLocBlock($locBlockId) {
180 $locBlock = new CRM_Core_DAO_LocBlock();
181 $locBlock->id
= $locBlockId;
183 $locBlock->find(TRUE);
185 //resolve conflict of having same ids for multiple blocks
187 'IM_1' => $locBlock->im_id
,
188 'IM_2' => $locBlock->im_2_id
,
189 'Email_1' => $locBlock->email_id
,
190 'Email_2' => $locBlock->email_2_id
,
191 'Phone_1' => $locBlock->phone_id
,
192 'Phone_2' => $locBlock->phone_2_id
,
193 'Address_1' => $locBlock->address_id
,
194 'Address_2' => $locBlock->address_2_id
,
197 foreach ($store as $daoName => $id) {
199 $daoName = 'CRM_Core_DAO_' . substr($daoName, 0, -2);
200 $dao = new $daoName();
210 * Check if there is data to create the object
212 * @param array $params (reference ) an assoc array of name/value pairs
217 public static function dataExists(&$params) {
218 // return if no data present
220 foreach (self
::$blocks as $block) {
221 if (array_key_exists($block, $params)) {
231 * @param $entityBlock
232 * @param bool $microformat
234 * @return array array of objects(CRM_Core_BAO_Location)
237 public static function &getValues($entityBlock, $microformat = FALSE) {
238 if (empty($entityBlock)) {
244 'openid' => 'OpenID',
247 //get all the blocks for this contact
248 foreach (self
::$blocks as $block) {
249 if (array_key_exists($block, $name_map)) {
250 $name = $name_map[$block];
253 $name = ucfirst($block);
255 $baoString = 'CRM_Core_BAO_' . $name;
256 $blocks[$block] = $baoString::getValues($entityBlock, $microformat);
262 * Delete all the block associated with the location
264 * @param int $contactId contact id
265 * @param int $locationTypeId id of the location to delete
270 public static function deleteLocationBlocks($contactId, $locationTypeId) {
271 // ensure that contactId has a value
272 if (empty($contactId) ||
273 !CRM_Utils_Rule
::positiveInteger($contactId)
275 CRM_Core_Error
::fatal();
278 if (empty($locationTypeId) ||
279 !CRM_Utils_Rule
::positiveInteger($locationTypeId)
281 // so we only delete the blocks which DO NOT have a location type Id
283 $locationTypeId = 'null';
286 static $blocks = array('Address', 'Phone', 'IM', 'OpenID', 'Email');
288 $params = array('contact_id' => $contactId, 'location_type_id' => $locationTypeId);
289 foreach ($blocks as $name) {
290 CRM_Core_BAO_Block
::blockDelete($name, $params);
295 * Copy or update location block.
297 * @param int $locBlockId location block id.
298 * @param int $updateLocBlockId update location block id
300 * @return int newly created/updated location block id.
302 public static function copyLocBlock($locBlockId, $updateLocBlockId = NULL) {
303 //get the location info.
304 $defaults = $updateValues = array();
305 $locBlock = array('id' => $locBlockId);
306 CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_LocBlock', $locBlock, $defaults);
308 if ($updateLocBlockId) {
309 //get the location info for update.
310 $copyLocationParams = array('id' => $updateLocBlockId);
311 CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_LocBlock', $copyLocationParams, $updateValues);
312 foreach ($updateValues as $key => $value) {
314 $copyLocationParams[$key] = 'null';
319 //copy all location blocks (email, phone, address, etc)
320 foreach ($defaults as $key => $value) {
322 $tbl = explode("_", $key);
323 $name = ucfirst($tbl[0]);
324 $updateParams = NULL;
325 if ($updateId = CRM_Utils_Array
::value($key, $updateValues)) {
326 $updateParams = array('id' => $updateId);
329 $copy = CRM_Core_DAO
::copyGeneric('CRM_Core_DAO_' . $name, array('id' => $value), $updateParams);
330 $copyLocationParams[$key] = $copy->id
;
334 $copyLocation = &CRM_Core_DAO
::copyGeneric('CRM_Core_DAO_LocBlock',
335 array('id' => $locBlock['id']),
338 return $copyLocation->id
;
342 * If contact has data for any location block, make sure
343 * contact should have only one primary block, CRM-5051
345 * @param int $contactId - contact id
349 public static function checkPrimaryBlocks($contactId) {
354 // get the loc block ids.
355 $primaryLocBlockIds = CRM_Contact_BAO_Contact
::getLocBlockIds($contactId, array('is_primary' => 1));
356 $nonPrimaryBlockIds = CRM_Contact_BAO_Contact
::getLocBlockIds($contactId, array('is_primary' => 0));
359 'Email', 'IM', 'Phone', 'Address', 'OpenID') as $block) {
360 $name = strtolower($block);
361 if (array_key_exists($name, $primaryLocBlockIds) &&
362 !CRM_Utils_System
::isNull($primaryLocBlockIds[$name])
364 if (count($primaryLocBlockIds[$name]) > 1) {
365 // keep only single block as primary.
366 $primaryId = array_pop($primaryLocBlockIds[$name]);
367 $resetIds = "(" . implode(',', $primaryLocBlockIds[$name]) . ")";
368 // reset all primary except one.
369 CRM_Core_DAO
::executeQuery("UPDATE civicrm_$name SET is_primary = 0 WHERE id IN $resetIds");
372 elseif (array_key_exists($name, $nonPrimaryBlockIds) &&
373 !CRM_Utils_System
::isNull($nonPrimaryBlockIds[$name])
375 // data exists and no primary block - make one primary.
376 CRM_Core_DAO
::setFieldValue("CRM_Core_DAO_" . $block,
377 array_pop($nonPrimaryBlockIds[$name]), 'is_primary', 1
384 * @param mixed $values
385 * @param string $valueType
386 * @param bool $flatten
390 public static function getChainSelectValues($values, $valueType, $flatten = FALSE) {
394 $values = array_filter((array) $values);
397 $method = $valueType == 'country' ?
'stateProvinceForCountry' : 'countyForState';
398 foreach ($values as $val) {
399 $result = CRM_Core_PseudoConstant
::$method($val);
401 // Format for quickform
403 // Option-groups for multiple categories
404 if ($result && count($values) > 1) {
405 $elements["crm_optgroup_$val"] = CRM_Core_PseudoConstant
::$valueType($val, FALSE);
407 $elements +
= $result;
412 // Option-groups for multiple categories
413 if ($result && count($values) > 1) {
415 'value' => CRM_Core_PseudoConstant
::$valueType($val, FALSE),
416 'children' => array(),
418 $list = & $elements[count($elements) - 1]['children'];
420 foreach ($result as $id => $name) {