3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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 * Function to 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
53 * @return array $location
57 static function create(&$params, $fixAddress = TRUE, $entity = NULL) {
59 if (!self
::dataExists($params)) {
63 // create location blocks.
64 foreach (self
::$blocks as $block) {
65 if ($block != 'address') {
66 $location[$block] = CRM_Core_BAO_Block
::create( $block, $params, $entity );
69 $location[$block] = CRM_Core_BAO_Address
::create($params, $fixAddress, $entity);
74 // this is a special case for adding values in location block table
75 $entityElements = array(
76 'entity_table' => $params['entity_table'],
77 'entity_id' => $params['entity_id'],
80 $location['id'] = self
::createLocBlock($location, $entityElements);
83 // when we come from a form which displays all the location elements (like the edit form or the inline block
84 // elements, we can skip the below check. The below check adds quite a feq queries to an already overloaded
86 if (!CRM_Utils_Array
::value('updateBlankLocInfo', $params, FALSE)) {
87 // make sure contact should have only one primary block, CRM-5051
88 self
::checkPrimaryBlocks(CRM_Utils_Array
::value('contact_id', $params));
96 * Creates the entry in the civicrm_loc_block
99 static function createLocBlock(&$location, &$entityElements) {
100 $locId = self
::findExisting($entityElements);
104 $locBlock['id'] = $locId;
108 'phone', 'email', 'im', 'address') as $loc) {
109 $locBlock["{$loc}_id"] = CRM_Utils_Array
::value(0, $location["$loc"]) ?
$location["$loc"][0]->id
: NULL;
110 $locBlock["{$loc}_2_id"] = CRM_Utils_Array
::value(1, $location["$loc"]) ?
$location["$loc"][1]->id
: NULL;
114 foreach ($locBlock as $key => $block) {
115 if (empty($locBlock[$key])) {
116 $locBlock[$key] = 'null';
121 if (count($locBlock) == $countNull) {
122 // implies nothing is set.
126 $locBlockInfo = self
::addLocBlock($locBlock);
127 return $locBlockInfo->id
;
131 * takes an entity array and finds the existing location block
135 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
160 static function addLocBlock(&$params) {
161 $locBlock = new CRM_Core_DAO_LocBlock();
163 $locBlock->copyValues($params);
165 return $locBlock->save();
169 * This function deletes the Location Block
171 * @param int $locBlockId id of the Location Block
177 public static function deleteLocBlock($locBlockId) {
182 $locBlock = new CRM_Core_DAO_LocBlock();
183 $locBlock->id
= $locBlockId;
185 $locBlock->find(TRUE);
187 //resolve conflict of having same ids for multiple blocks
189 'IM_1' => $locBlock->im_id
,
190 'IM_2' => $locBlock->im_2_id
,
191 'Email_1' => $locBlock->email_id
,
192 'Email_2' => $locBlock->email_2_id
,
193 'Phone_1' => $locBlock->phone_id
,
194 'Phone_2' => $locBlock->phone_2_id
,
195 'Address_1' => $locBlock->address_id
,
196 'Address_2' => $locBlock->address_2_id
,
199 foreach ($store as $daoName => $id) {
201 $daoName = 'CRM_Core_DAO_' . substr($daoName, 0, -2);
202 $dao = new $daoName();
212 * Check if there is data to create the object
214 * @param array $params (reference ) an assoc array of name/value pairs
220 static function dataExists(&$params) {
221 // return if no data present
223 foreach (self
::$blocks as $block) {
224 if (array_key_exists($block, $params)) {
234 * Given the list of params in the params array, fetch the object
235 * and store the values in the values array
237 * @param array $params input parameters to find object
238 * @param array $values output values of the object
240 * @return array array of objects(CRM_Core_BAO_Location)
244 static function &getValues($entityBlock, $microformat = FALSE) {
245 if (empty($entityBlock)) {
251 'openid' => 'OpenID',
254 //get all the blocks for this contact
255 foreach (self
::$blocks as $block) {
256 if (array_key_exists($block, $name_map)) {
257 $name = $name_map[$block];
260 $name = ucfirst($block);
262 $baoString = 'CRM_Core_BAO_' . $name;
263 $blocks[$block] = $baoString::getValues( $entityBlock, $microformat );
269 * Delete all the block associated with the location
271 * @param int $contactId contact id
272 * @param int $locationTypeId id of the location to delete
278 static function deleteLocationBlocks($contactId, $locationTypeId) {
279 // ensure that contactId has a value
280 if (empty($contactId) ||
281 !CRM_Utils_Rule
::positiveInteger($contactId)
283 CRM_Core_Error
::fatal();
286 if (empty($locationTypeId) ||
287 !CRM_Utils_Rule
::positiveInteger($locationTypeId)
289 // so we only delete the blocks which DO NOT have a location type Id
291 $locationTypeId = 'null';
294 static $blocks = array('Address', 'Phone', 'IM', 'OpenID', 'Email');
296 $params = array('contact_id' => $contactId, 'location_type_id' => $locationTypeId);
297 foreach ($blocks as $name) {
298 CRM_Core_BAO_Block
::blockDelete($name, $params);
302 /* Function to copy or update location block.
304 * @param int $locBlockId location block id.
305 * @param int $updateLocBlockId update location block id
306 * @return int newly created/updated location block id.
308 static function copyLocBlock($locBlockId, $updateLocBlockId = NULL) {
309 //get the location info.
310 $defaults = $updateValues = array();
311 $locBlock = array('id' => $locBlockId);
312 CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_LocBlock', $locBlock, $defaults);
314 if ($updateLocBlockId) {
315 //get the location info for update.
316 $copyLocationParams = array('id' => $updateLocBlockId);
317 CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_LocBlock', $copyLocationParams, $updateValues);
318 foreach ($updateValues as $key => $value) {
320 $copyLocationParams[$key] = 'null';
325 //copy all location blocks (email, phone, address, etc)
326 foreach ($defaults as $key => $value) {
328 $tbl = explode("_", $key);
329 $name = ucfirst($tbl[0]);
330 $updateParams = NULL;
331 if ($updateId = CRM_Utils_Array
::value($key, $updateValues)) {
332 $updateParams = array('id' => $updateId);
335 $copy = CRM_Core_DAO
::copyGeneric('CRM_Core_DAO_' . $name, array('id' => $value), $updateParams);
336 $copyLocationParams[$key] = $copy->id
;
340 $copyLocation = &CRM_Core_DAO
::copyGeneric('CRM_Core_DAO_LocBlock',
341 array('id' => $locBlock['id']),
344 return $copyLocation->id
;
348 * If contact has data for any location block, make sure
349 * contact should have only one primary block, CRM-5051
351 * @param int $contactId - contact id
356 static function checkPrimaryBlocks($contactId) {
361 // get the loc block ids.
362 $primaryLocBlockIds = CRM_Contact_BAO_Contact
::getLocBlockIds($contactId, array('is_primary' => 1));
363 $nonPrimaryBlockIds = CRM_Contact_BAO_Contact
::getLocBlockIds($contactId, array('is_primary' => 0));
366 'Email', 'IM', 'Phone', 'Address', 'OpenID') as $block) {
367 $name = strtolower($block);
368 if (array_key_exists($name, $primaryLocBlockIds) &&
369 !CRM_Utils_System
::isNull($primaryLocBlockIds[$name])
371 if (count($primaryLocBlockIds[$name]) > 1) {
372 // keep only single block as primary.
373 $primaryId = array_pop($primaryLocBlockIds[$name]);
374 $resetIds = "(" . implode(',', $primaryLocBlockIds[$name]) . ")";
375 // reset all primary except one.
376 CRM_Core_DAO
::executeQuery("UPDATE civicrm_$name SET is_primary = 0 WHERE id IN $resetIds");
379 elseif (array_key_exists($name, $nonPrimaryBlockIds) &&
380 !CRM_Utils_System
::isNull($nonPrimaryBlockIds[$name])
382 // data exists and no primary block - make one primary.
383 CRM_Core_DAO
::setFieldValue("CRM_Core_DAO_" . $block,
384 array_pop($nonPrimaryBlockIds[$name]), 'is_primary', 1