Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
232624b1 | 4 | | CiviCRM version 4.4 | |
6a488035 TO |
5 | +--------------------------------------------------------------------+ |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
7 | +--------------------------------------------------------------------+ | |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
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. | | |
13 | | | | |
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. | | |
18 | | | | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
31 | * @copyright CiviCRM LLC (c) 2004-2013 | |
32 | * $Id$ | |
33 | * | |
34 | * add static functions to include some common functionality | |
35 | * used across location sub object BAO classes | |
36 | * | |
37 | */ | |
38 | class CRM_Core_BAO_Block { | |
39 | ||
40 | /** | |
41 | * Fields that are required for a valid block | |
42 | */ | |
43 | static $requiredBlockFields = array( | |
44 | 'email' => array('email'), | |
45 | 'phone' => array('phone'), | |
46 | 'im' => array('name'), | |
47 | 'openid' => array('openid') | |
48 | ); | |
49 | ||
50 | /** | |
51 | * Given the list of params in the params array, fetch the object | |
52 | * and store the values in the values array | |
53 | * | |
54 | * @param Object $block typically a Phone|Email|IM|OpenID object | |
55 | * @param string $blockName name of the above object | |
56 | * @param array $params input parameters to find object | |
57 | * @param array $values output values of the object | |
58 | * | |
59 | * @return array of $block objects. | |
60 | * @access public | |
61 | * @static | |
62 | */ | |
63 | static function &getValues($blockName, $params) { | |
64 | if (empty($params)) { | |
65 | return NULL; | |
66 | } | |
e52506b0 | 67 | $BAOString = 'CRM_Core_BAO_' . $blockName; |
68 | $block = new $BAOString( ); | |
6a488035 TO |
69 | |
70 | $blocks = array(); | |
71 | if (!isset($params['entity_table'])) { | |
72 | $block->contact_id = $params['contact_id']; | |
73 | if (!$block->contact_id) { | |
74 | CRM_Core_Error::fatal(); | |
75 | } | |
76 | $blocks = self::retrieveBlock($block, $blockName); | |
77 | } | |
78 | else { | |
79 | $blockIds = self::getBlockIds($blockName, NULL, $params); | |
80 | ||
81 | if (empty($blockIds)) { | |
82 | return $blocks; | |
83 | } | |
84 | ||
85 | $count = 1; | |
86 | foreach ($blockIds as $blockId) { | |
e52506b0 | 87 | $block = new $BAOString( ); |
6a488035 TO |
88 | $block->id = $blockId['id']; |
89 | $getBlocks = self::retrieveBlock($block, $blockName); | |
90 | $blocks[$count++] = array_pop($getBlocks); | |
91 | } | |
92 | } | |
93 | ||
94 | return $blocks; | |
95 | } | |
96 | ||
97 | /** | |
98 | * Given the list of params in the params array, fetch the object | |
99 | * and store the values in the values array | |
100 | * | |
101 | * @param Object $block typically a Phone|Email|IM|OpenID object | |
102 | * @param string $blockName name of the above object | |
103 | * @param array $values output values of the object | |
104 | * | |
105 | * @return array of $block objects. | |
106 | * @access public | |
107 | * @static | |
108 | */ | |
109 | static function retrieveBlock(&$block, $blockName) { | |
110 | // we first get the primary location due to the order by clause | |
111 | $block->orderBy('is_primary desc, id'); | |
112 | $block->find(); | |
113 | ||
114 | $count = 1; | |
115 | $blocks = array(); | |
116 | while ($block->fetch()) { | |
117 | CRM_Core_DAO::storeValues($block, $blocks[$count]); | |
118 | //unset is_primary after first block. Due to some bug in earlier version | |
119 | //there might be more than one primary blocks, hence unset is_primary other than first | |
120 | if ($count > 1) { | |
121 | unset($blocks[$count]['is_primary']); | |
122 | } | |
123 | $count++; | |
124 | } | |
125 | ||
126 | return $blocks; | |
127 | } | |
128 | ||
129 | /** | |
130 | * check if the current block object has any valid data | |
131 | * | |
132 | * @param array $blockFields array of fields that are of interest for this object | |
133 | * @param array $params associated array of submitted fields | |
134 | * | |
135 | * @return boolean true if the block has data, otherwise false | |
136 | * @access public | |
137 | * @static | |
138 | */ | |
139 | static function dataExists($blockFields, &$params) { | |
140 | foreach ($blockFields as $field) { | |
141 | if (CRM_Utils_System::isNull(CRM_Utils_Array::value($field, $params))) { | |
142 | return FALSE; | |
143 | } | |
144 | } | |
145 | return TRUE; | |
146 | } | |
147 | ||
148 | /** | |
149 | * check if the current block exits | |
150 | * | |
151 | * @param string $blockName bloack name | |
152 | * @param array $params associated array of submitted fields | |
153 | * | |
154 | * @return boolean true if the block exits, otherwise false | |
155 | * @access public | |
156 | * @static | |
157 | */ | |
158 | static function blockExists($blockName, &$params) { | |
159 | // return if no data present | |
160 | if (!CRM_Utils_Array::value($blockName, $params) || !is_array($params[$blockName])) { | |
161 | return FALSE; | |
162 | } | |
163 | ||
164 | return TRUE; | |
165 | } | |
166 | ||
167 | /** | |
168 | * Function to get all block ids for a contact | |
169 | * | |
170 | * @param string $blockName block name | |
171 | * @param int $contactId contact id | |
172 | * | |
173 | * @return array $contactBlockIds formatted array of block ids | |
174 | * | |
175 | * @access public | |
176 | * @static | |
177 | */ | |
178 | static function getBlockIds($blockName, $contactId = NULL, $entityElements = NULL, $updateBlankLocInfo = FALSE) { | |
179 | $allBlocks = array(); | |
e4f5a5f9 | 180 | |
6a488035 TO |
181 | $name = ucfirst($blockName); |
182 | if ($blockName == 'im') { | |
183 | $name = 'IM'; | |
184 | } | |
185 | elseif ($blockName == 'openid') { | |
186 | $name = 'OpenID'; | |
187 | } | |
188 | ||
e4f5a5f9 | 189 | $baoString = 'CRM_Core_BAO_' . $name; |
6a488035 | 190 | if ($contactId) { |
e4f5a5f9 | 191 | //@todo a cleverer way to do this would be to use the same fn name on each |
192 | // BAO rather than constructing the fn | |
193 | // it would also be easier to grep for | |
194 | // e.g $bao = new $baoString; | |
195 | // $bao->getAllBlocks() | |
196 | $baoFunction = 'all' . $name . 's'; | |
197 | $allBlocks = $baoString::$baoFunction( $contactId, $updateBlankLocInfo ); | |
6a488035 TO |
198 | } |
199 | elseif (!empty($entityElements) && $blockName != 'openid') { | |
e4f5a5f9 | 200 | $baoFunction = 'allEntity' . $name . 's'; |
201 | $allBlocks = $baoString::$baoFunction( $entityElements ); | |
6a488035 TO |
202 | } |
203 | ||
204 | return $allBlocks; | |
205 | } | |
206 | ||
207 | /** | |
208 | * takes an associative array and creates a block | |
209 | * | |
210 | * @param string $blockName block name | |
211 | * @param array $params (reference ) an assoc array of name/value pairs | |
212 | * @param array $requiredFields fields that's are required in a block | |
213 | * | |
214 | * @return object CRM_Core_BAO_Block object on success, null otherwise | |
215 | * @access public | |
216 | * @static | |
217 | */ | |
218 | static function create($blockName, &$params, $entity = NULL, $contactId = NULL) { | |
219 | if (!self::blockExists($blockName, $params)) { | |
220 | return NULL; | |
221 | } | |
222 | ||
223 | $name = ucfirst($blockName); | |
224 | $contactId = NULL; | |
225 | $isPrimary = $isBilling = TRUE; | |
226 | $entityElements = $blocks = array(); | |
227 | ||
228 | if ($entity) { | |
229 | $entityElements = array( | |
230 | 'entity_table' => $params['entity_table'], | |
231 | 'entity_id' => $params['entity_id'], | |
232 | ); | |
233 | } | |
234 | else { | |
235 | $contactId = $params['contact_id']; | |
236 | } | |
237 | ||
238 | $updateBlankLocInfo = CRM_Utils_Array::value('updateBlankLocInfo', $params, FALSE); | |
239 | ||
240 | //get existsing block ids. | |
241 | $blockIds = self::getBlockIds($blockName, $contactId, $entityElements, $updateBlankLocInfo); | |
242 | ||
243 | if (!$updateBlankLocInfo) { | |
244 | $resetPrimaryId = NULL; | |
245 | $primaryId = FALSE; | |
246 | foreach ($params[$blockName] as $count => $value) { | |
247 | $blockId = CRM_Utils_Array::value('id', $value); | |
248 | if ($blockId) { | |
249 | if (is_array($blockIds) | |
250 | && array_key_exists($blockId, $blockIds) | |
251 | ) { | |
252 | unset($blockIds[$blockId]); | |
253 | } | |
254 | else { | |
255 | unset($value['id']); | |
256 | } | |
257 | } | |
258 | //lets allow to update primary w/ more cleanly. | |
259 | if (!$resetPrimaryId && | |
260 | CRM_Utils_Array::value('is_primary', $value) | |
261 | ) { | |
262 | $primaryId = TRUE; | |
263 | if (is_array($blockIds)) { | |
264 | foreach ($blockIds as $blockId => $blockValue) { | |
265 | if (CRM_Utils_Array::value('is_primary', $blockValue)) { | |
266 | $resetPrimaryId = $blockId; | |
267 | break; | |
268 | } | |
269 | } | |
270 | } | |
271 | if ($resetPrimaryId) { | |
e4f5a5f9 | 272 | $baoString = 'CRM_Core_BAO_' . $blockName; |
273 | $block = new $baoString( ); | |
6a488035 TO |
274 | $block->selectAdd(); |
275 | $block->selectAdd("id, is_primary"); | |
276 | $block->id = $resetPrimaryId; | |
277 | if ($block->find(TRUE)) { | |
278 | $block->is_primary = FALSE; | |
279 | $block->save(); | |
280 | } | |
281 | $block->free(); | |
282 | } | |
283 | } | |
284 | } | |
285 | } | |
286 | ||
287 | foreach ($params[$blockName] as $count => $value) { | |
288 | if (!is_array($value)) { | |
289 | continue; | |
290 | } | |
291 | $contactFields = array( | |
292 | 'contact_id' => $contactId, | |
293 | 'location_type_id' => CRM_Utils_Array::value('location_type_id', $value), | |
294 | ); | |
295 | ||
296 | //check for update | |
297 | if (!CRM_Utils_Array::value('id', $value) && | |
298 | is_array($blockIds) && !empty($blockIds) | |
299 | ) { | |
300 | foreach ($blockIds as $blockId => $blockValue) { | |
301 | if ($updateBlankLocInfo) { | |
302 | if (CRM_Utils_Array::value($count, $blockIds)) { | |
303 | $value['id'] = $blockIds[$count]['id']; | |
304 | unset($blockIds[$count]); | |
305 | } | |
306 | } | |
307 | else { | |
308 | if ($blockValue['locationTypeId'] == CRM_Utils_Array::value('location_type_id', $value)) { | |
309 | $valueId = FALSE; | |
310 | ||
311 | if ($blockName == 'phone') { | |
312 | $phoneTypeBlockValue = CRM_Utils_Array::value('phoneTypeId', $blockValue); | |
d5f1ee75 | 313 | if ($phoneTypeBlockValue == CRM_Utils_Array::value('phone_type_id', $value)) { |
6a488035 TO |
314 | $valueId = TRUE; |
315 | } | |
316 | } | |
317 | elseif ($blockName == 'im') { | |
318 | $providerBlockValue = CRM_Utils_Array::value('providerId', $blockValue); | |
319 | if ($providerBlockValue == $value['provider_id']) { | |
320 | $valueId = TRUE; | |
321 | } | |
322 | } | |
323 | else { | |
324 | $valueId = TRUE; | |
325 | } | |
326 | ||
327 | if ($valueId) { | |
328 | //assigned id as first come first serve basis | |
329 | $value['id'] = $blockValue['id']; | |
330 | if (!$primaryId && CRM_Utils_Array::value('is_primary', $blockValue)) { | |
331 | $value['is_primary'] = $blockValue['is_primary']; | |
332 | } | |
333 | unset($blockIds[$blockId]); | |
334 | break; | |
335 | } | |
336 | } | |
337 | } | |
338 | } | |
339 | } | |
340 | ||
341 | $dataExits = self::dataExists(self::$requiredBlockFields[$blockName], $value); | |
342 | ||
343 | // Note there could be cases when block info already exist ($value[id] is set) for a contact/entity | |
344 | // BUT info is not present at this time, and therefore we should be really careful when deleting the block. | |
345 | // $updateBlankLocInfo will help take appropriate decision. CRM-5969 | |
346 | if (CRM_Utils_Array::value('id', $value) && !$dataExits && $updateBlankLocInfo) { | |
347 | //delete the existing record | |
348 | self::blockDelete($blockName, array('id' => $value['id'])); | |
349 | continue; | |
350 | } | |
351 | elseif (!$dataExits) { | |
352 | continue; | |
353 | } | |
354 | ||
355 | if ($isPrimary && CRM_Utils_Array::value('is_primary', $value)) { | |
356 | $contactFields['is_primary'] = $value['is_primary']; | |
357 | $isPrimary = FALSE; | |
358 | } | |
359 | else { | |
360 | $contactFields['is_primary'] = 0; | |
361 | } | |
362 | ||
363 | if ($isBilling && CRM_Utils_Array::value('is_billing', $value)) { | |
364 | $contactFields['is_billing'] = $value['is_billing']; | |
365 | $isBilling = FALSE; | |
366 | } | |
367 | else { | |
368 | $contactFields['is_billing'] = 0; | |
369 | } | |
370 | ||
371 | $blockFields = array_merge($value, $contactFields); | |
293daac2 | 372 | $baoString = 'CRM_Core_BAO_' . $name; |
373 | $blocks[] = $baoString::add( $blockFields ); | |
6a488035 TO |
374 | } |
375 | ||
376 | // we need to delete blocks that were deleted during update | |
377 | if ($updateBlankLocInfo && !empty($blockIds)) { | |
378 | foreach ($blockIds as $deleteBlock) { | |
379 | if (!CRM_Utils_Array::value('id', $deleteBlock)) { | |
380 | continue; | |
381 | } | |
382 | self::blockDelete($blockName, array('id' => $deleteBlock['id'])); | |
383 | } | |
384 | } | |
385 | ||
386 | return $blocks; | |
387 | } | |
388 | ||
389 | /** | |
390 | * Function to delete block | |
391 | * | |
392 | * @param string $blockName block name | |
393 | * @param int $params associates array | |
394 | * | |
395 | * @return void | |
396 | * @static | |
397 | */ | |
398 | static function blockDelete($blockName, $params) { | |
399 | $name = ucfirst($blockName); | |
400 | if ($blockName == 'im') { | |
401 | $name = 'IM'; | |
402 | } | |
403 | elseif ($blockName == 'openid') { | |
404 | $name = 'OpenID'; | |
405 | } | |
406 | ||
e4f5a5f9 | 407 | $baoString = 'CRM_Core_DAO_' . $name; |
408 | $block = new $baoString( ); | |
6a488035 TO |
409 | |
410 | $block->copyValues($params); | |
411 | /* | |
412 | * CRM-11006 add call to pre and post hook for delete action | |
413 | */ | |
414 | CRM_Utils_Hook::pre('delete', $name, $block->id, CRM_Core_DAO::$_nullArray); | |
415 | $block->delete(); | |
416 | CRM_Utils_Hook::post('delete', $name, $block->id, $block); | |
417 | } | |
418 | ||
419 | /** | |
420 | * Handling for is_primary. | |
421 | * $params is_primary could be | |
422 | * # 1 - find other entries with is_primary = 1 & reset them to 0 | |
423 | * # 0 - make sure at least one entry is set to 1 | |
424 | * - if no other entry is 1 change to 1 | |
425 | * - if one other entry exists change that to 1 | |
426 | * - if more than one other entry exists change first one to 1 | |
427 | * @fixme - perhaps should choose by location_type | |
428 | * # empty - same as 0 as once we have checked first step | |
429 | * we know if it should be 1 or 0 | |
430 | * | |
431 | * if $params['id'] is set $params['contact_id'] may need to be retrieved | |
432 | * | |
433 | * @param array $params | |
434 | * @static | |
435 | */ | |
436 | public static function handlePrimary(&$params, $class) { | |
37a77d9c TO |
437 | $table = CRM_Core_DAO_AllCoreTables::getTableForClass($class); |
438 | if (!$table) { | |
439 | throw new API_Exception("Failed to locate table for class [$class]"); | |
440 | } | |
d195b60c | 441 | |
50fa40e8 CW |
442 | // contact_id in params might be empty or the string 'null' so cast to integer |
443 | $contactId = (int) CRM_Utils_Array::value('contact_id', $params); | |
444 | // If id is set & we haven't been passed a contact_id, retrieve it | |
445 | if (!empty($params['id']) && !isset($params['contact_id'])) { | |
6a488035 TO |
446 | $entity = new $class(); |
447 | $entity->id = $params['id']; | |
448 | $entity->find(TRUE); | |
449 | $contactId = $entity->contact_id; | |
450 | } | |
50fa40e8 CW |
451 | // If entity is not associated with contact, concept of is_primary not relevant |
452 | if (!$contactId) { | |
6a488035 TO |
453 | return; |
454 | } | |
455 | ||
456 | // if params is_primary then set all others to not be primary & exit out | |
457 | if (CRM_Utils_Array::value('is_primary', $params)) { | |
458 | $sql = "UPDATE $table SET is_primary = 0 WHERE contact_id = %1"; | |
459 | $sqlParams = array(1 => array($contactId, 'Integer')); | |
460 | // we don't want to create unecessary entries in the log_ tables so exclude the one we are working on | |
461 | if(!empty($params['id'])){ | |
462 | $sql .= " AND id <> %2"; | |
463 | $sqlParams[2] = array($params['id'], 'Integer'); | |
464 | } | |
465 | CRM_Core_DAO::executeQuery($sql, $sqlParams); | |
466 | return; | |
467 | } | |
468 | ||
469 | //Check what other emails exist for the contact | |
470 | $existingEntities = new $class(); | |
471 | $existingEntities->contact_id = $contactId; | |
472 | $existingEntities->orderBy('is_primary DESC'); | |
473 | if (!$existingEntities->find(TRUE) || (!empty($params['id']) && $existingEntities->id == $params['id'])) { | |
474 | // ie. if no others is set to be primary then this has to be primary set to 1 so change | |
475 | $params['is_primary'] = 1; | |
476 | return; | |
477 | } | |
478 | else { | |
479 | /* | |
480 | * If the only existing email is the one we are editing then we must set | |
481 | * is_primary to 1 | |
482 | * CRM-10451 | |
483 | */ | |
484 | if ( $existingEntities->N == 1 && $existingEntities->id == CRM_Utils_Array::value( 'id', $params ) ) { | |
485 | $params['is_primary'] = 1; | |
486 | return; | |
487 | } | |
488 | ||
489 | if ($existingEntities->is_primary == 1) { | |
490 | return; | |
491 | } | |
492 | // so at this point we are only dealing with ones explicity setting is_primary to 0 | |
493 | // since we have reverse sorted by email we can either set the first one to | |
494 | // primary or return if is already is | |
495 | $existingEntities->is_primary = 1; | |
496 | $existingEntities->save(); | |
497 | } | |
498 | } | |
499 | ||
500 | /** | |
501 | * Sort location array so primary element is first | |
502 | * @param Array $location | |
503 | */ | |
504 | static function sortPrimaryFirst(&$locations){ | |
505 | uasort($locations, 'self::primaryComparison'); | |
506 | } | |
507 | ||
508 | /** | |
509 | * compare 2 locations to see which should go first based on is_primary | |
510 | * (sort function for sortPrimaryFirst) | |
511 | * @param array $location1 | |
512 | * @param array_type $location2 | |
513 | * @return number | |
514 | */ | |
515 | static function primaryComparison($location1, $location2){ | |
516 | $l1 = CRM_Utils_Array::value('is_primary', $location1); | |
517 | $l2 = CRM_Utils_Array::value('is_primary', $location2); | |
518 | if ($l1 == $l2) { | |
519 | return 0; | |
520 | } | |
521 | return ($l1 < $l2) ? -1 : 1; | |
522 | } | |
523 | } | |
524 |