3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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
59 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 * Creates the entry in the civicrm_loc_block
101 static function createLocBlock(&$location, &$entityElements) {
102 $locId = self
::findExisting($entityElements);
106 $locBlock['id'] = $locId;
110 'phone', 'email', 'im', 'address') as $loc) {
111 $locBlock["{$loc}_id"] = !empty($location["$loc"][0]) ?
$location["$loc"][0]->id
: NULL;
112 $locBlock["{$loc}_2_id"] = !empty($location["$loc"][1]) ?
$location["$loc"][1]->id
: NULL;
116 foreach ($locBlock as $key => $block) {
117 if (empty($locBlock[$key])) {
118 $locBlock[$key] = 'null';
123 if (count($locBlock) == $countNull) {
124 // implies nothing is set.
128 $locBlockInfo = self
::addLocBlock($locBlock);
129 return $locBlockInfo->id
;
133 * Takes an entity array and finds the existing location block
137 static function findExisting($entityElements) {
138 $eid = $entityElements['entity_id'];
139 $etable = $entityElements['entity_table'];
141 SELECT e.loc_block_id as locId
145 $params = array(1 => array($eid, 'Integer'));
146 $dao = CRM_Core_DAO
::executeQuery($query, $params);
147 while ($dao->fetch()) {
148 $locBlockId = $dao->locId
;
154 * Takes an associative array and adds location block
156 * @param array $params (reference ) an assoc array of name/value pairs
158 * @return object CRM_Core_BAO_locBlock object on success, null otherwise
162 static function addLocBlock(&$params) {
163 $locBlock = new CRM_Core_DAO_LocBlock();
165 $locBlock->copyValues($params);
167 return $locBlock->save();
171 * Delete the Location Block
173 * @param int $locBlockId id of the Location Block
179 public static function deleteLocBlock($locBlockId) {
184 $locBlock = new CRM_Core_DAO_LocBlock();
185 $locBlock->id
= $locBlockId;
187 $locBlock->find(TRUE);
189 //resolve conflict of having same ids for multiple blocks
191 'IM_1' => $locBlock->im_id
,
192 'IM_2' => $locBlock->im_2_id
,
193 'Email_1' => $locBlock->email_id
,
194 'Email_2' => $locBlock->email_2_id
,
195 'Phone_1' => $locBlock->phone_id
,
196 'Phone_2' => $locBlock->phone_2_id
,
197 'Address_1' => $locBlock->address_id
,
198 'Address_2' => $locBlock->address_2_id
,
201 foreach ($store as $daoName => $id) {
203 $daoName = 'CRM_Core_DAO_' . substr($daoName, 0, -2);
204 $dao = new $daoName();
214 * Check if there is data to create the object
216 * @param array $params (reference ) an assoc array of name/value pairs
222 static function dataExists(&$params) {
223 // return if no data present
225 foreach (self
::$blocks as $block) {
226 if (array_key_exists($block, $params)) {
236 * @param $entityBlock
237 * @param bool $microformat
239 * @return array array of objects(CRM_Core_BAO_Location)
243 static function &getValues($entityBlock, $microformat = FALSE) {
244 if (empty($entityBlock)) {
250 'openid' => 'OpenID',
253 //get all the blocks for this contact
254 foreach (self
::$blocks as $block) {
255 if (array_key_exists($block, $name_map)) {
256 $name = $name_map[$block];
259 $name = ucfirst($block);
261 $baoString = 'CRM_Core_BAO_' . $name;
262 $blocks[$block] = $baoString::getValues($entityBlock, $microformat);
268 * Delete all the block associated with the location
270 * @param int $contactId contact id
271 * @param int $locationTypeId id of the location to delete
277 static function deleteLocationBlocks($contactId, $locationTypeId) {
278 // ensure that contactId has a value
279 if (empty($contactId) ||
280 !CRM_Utils_Rule
::positiveInteger($contactId)
282 CRM_Core_Error
::fatal();
285 if (empty($locationTypeId) ||
286 !CRM_Utils_Rule
::positiveInteger($locationTypeId)
288 // so we only delete the blocks which DO NOT have a location type Id
290 $locationTypeId = 'null';
293 static $blocks = array('Address', 'Phone', 'IM', 'OpenID', 'Email');
295 $params = array('contact_id' => $contactId, 'location_type_id' => $locationTypeId);
296 foreach ($blocks as $name) {
297 CRM_Core_BAO_Block
::blockDelete($name, $params);
302 * Copy or update location block.
304 * @param int $locBlockId location block id.
305 * @param int $updateLocBlockId update location block id
307 * @return int newly created/updated location block id.
309 static function copyLocBlock($locBlockId, $updateLocBlockId = NULL) {
310 //get the location info.
311 $defaults = $updateValues = array();
312 $locBlock = array('id' => $locBlockId);
313 CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_LocBlock', $locBlock, $defaults);
315 if ($updateLocBlockId) {
316 //get the location info for update.
317 $copyLocationParams = array('id' => $updateLocBlockId);
318 CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_LocBlock', $copyLocationParams, $updateValues);
319 foreach ($updateValues as $key => $value) {
321 $copyLocationParams[$key] = 'null';
326 //copy all location blocks (email, phone, address, etc)
327 foreach ($defaults as $key => $value) {
329 $tbl = explode("_", $key);
330 $name = ucfirst($tbl[0]);
331 $updateParams = NULL;
332 if ($updateId = CRM_Utils_Array
::value($key, $updateValues)) {
333 $updateParams = array('id' => $updateId);
336 $copy = CRM_Core_DAO
::copyGeneric('CRM_Core_DAO_' . $name, array('id' => $value), $updateParams);
337 $copyLocationParams[$key] = $copy->id
;
341 $copyLocation = &CRM_Core_DAO
::copyGeneric('CRM_Core_DAO_LocBlock',
342 array('id' => $locBlock['id']),
345 return $copyLocation->id
;
349 * If contact has data for any location block, make sure
350 * contact should have only one primary block, CRM-5051
352 * @param int $contactId - contact id
357 static function checkPrimaryBlocks($contactId) {
362 // get the loc block ids.
363 $primaryLocBlockIds = CRM_Contact_BAO_Contact
::getLocBlockIds($contactId, array('is_primary' => 1));
364 $nonPrimaryBlockIds = CRM_Contact_BAO_Contact
::getLocBlockIds($contactId, array('is_primary' => 0));
367 'Email', 'IM', 'Phone', 'Address', 'OpenID') as $block) {
368 $name = strtolower($block);
369 if (array_key_exists($name, $primaryLocBlockIds) &&
370 !CRM_Utils_System
::isNull($primaryLocBlockIds[$name])
372 if (count($primaryLocBlockIds[$name]) > 1) {
373 // keep only single block as primary.
374 $primaryId = array_pop($primaryLocBlockIds[$name]);
375 $resetIds = "(" . implode(',', $primaryLocBlockIds[$name]) . ")";
376 // reset all primary except one.
377 CRM_Core_DAO
::executeQuery("UPDATE civicrm_$name SET is_primary = 0 WHERE id IN $resetIds");
380 elseif (array_key_exists($name, $nonPrimaryBlockIds) &&
381 !CRM_Utils_System
::isNull($nonPrimaryBlockIds[$name])
383 // data exists and no primary block - make one primary.
384 CRM_Core_DAO
::setFieldValue("CRM_Core_DAO_" . $block,
385 array_pop($nonPrimaryBlockIds[$name]), 'is_primary', 1
392 * @param mixed $values
393 * @param string $valueType
394 * @param bool $flatten
398 static function getChainSelectValues($values, $valueType, $flatten = FALSE) {
402 $values = array_filter((array) $values);
405 $method = $valueType == 'country' ?
'stateProvinceForCountry' : 'countyForState';
406 foreach ($values as $val) {
407 $result = CRM_Core_PseudoConstant
::$method($val);
409 // Format for quickform
411 // Option-groups for multiple categories
412 if ($result && count($values) > 1) {
413 $elements["crm_optgroup_$val"] = CRM_Core_PseudoConstant
::$valueType($val, FALSE);
415 $elements +
= $result;
420 // Option-groups for multiple categories
421 if ($result && count($values) > 1) {
423 'value' => CRM_Core_PseudoConstant
::$valueType($val, FALSE),
424 'children' => array(),
426 $list = & $elements[count($elements) - 1]['children'];
428 foreach ($result as $id => $name) {