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