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 | */ | |
35 | class CRM_Contact_BAO_Relationship extends CRM_Contact_DAO_Relationship { | |
36 | ||
37 | /** | |
38 | * various constants to indicate different type of relationships | |
39 | * | |
40 | * @var int | |
41 | */ | |
42 | CONST PAST = 1, DISABLED = 2, CURRENT = 4, INACTIVE = 8; | |
43 | ||
44 | /** | |
45 | * takes an associative array and creates a relationship object | |
46 | * | |
47 | * | |
48 | * @param array $params (reference ) an assoc array of name/value pairs | |
49 | * @param array $ids the array that holds all the db ids | |
26dcc9eb | 50 | * per http://wiki.civicrm.org/confluence/display/CRM/Database+layer |
51 | * "we are moving away from the $ids param " | |
6a488035 TO |
52 | * |
53 | * @return object CRM_Contact_BAO_Relationship object | |
54 | * @access public | |
55 | * @static | |
56 | */ | |
26dcc9eb | 57 | static function create(&$params, $ids = array()) { |
6a488035 | 58 | $valid = $invalid = $duplicate = $saved = 0; |
26dcc9eb | 59 | $relationships = array(); |
60 | $relationshipId = CRM_Utils_Array::value('relationship', $ids, CRM_Utils_Array::value('id', $params)); | |
6a488035 TO |
61 | //CRM-9015 - the hooks are called here & in add (since add doesn't call create) |
62 | // but in future should be tidied per ticket | |
26dcc9eb | 63 | if(empty($relationshipId)){ |
64 | $hook = 'create'; | |
65 | $action = CRM_Core_Action::ADD; | |
6a488035 | 66 | } |
26dcc9eb | 67 | else{ |
68 | $hook = 'edit'; | |
69 | $action = CRM_Core_Action::UPDATE; | |
6a488035 | 70 | } |
26dcc9eb | 71 | |
72 | CRM_Utils_Hook::pre($hook, 'Relationship', $relationshipId, $params); | |
73 | ||
6a488035 TO |
74 | if (!$relationshipId) { |
75 | // creating a new relationship | |
76 | $dataExists = self::dataExists($params); | |
77 | if (!$dataExists) { | |
43291913 | 78 | return array(FALSE, TRUE, FALSE, FALSE, NULL); |
6a488035 TO |
79 | } |
80 | $relationshipIds = array(); | |
81 | foreach ($params['contact_check'] as $key => $value) { | |
82 | $errors = ''; | |
83 | // check if the relationship is valid between contacts. | |
84 | // step 1: check if the relationship is valid if not valid skip and keep the count | |
85 | // step 2: check the if two contacts already have a relationship if yes skip and keep the count | |
86 | // step 3: if valid relationship then add the relation and keep the count | |
87 | ||
88 | // step 1 | |
89 | $errors = self::checkValidRelationship($params, $ids, $key); | |
90 | if ($errors) { | |
91 | $invalid++; | |
92 | continue; | |
93 | } | |
94 | ||
95 | if ( | |
96 | self::checkDuplicateRelationship( | |
97 | $params, | |
98 | CRM_Utils_Array::value('contact', $ids), | |
99 | // step 2 | |
100 | $key | |
101 | ) | |
102 | ) { | |
103 | $duplicate++; | |
104 | continue; | |
105 | } | |
106 | ||
107 | $relationship = self::add($params, $ids, $key); | |
108 | $relationshipIds[] = $relationship->id; | |
26dcc9eb | 109 | $relationships[$relationship->id] = $relationship; |
6a488035 TO |
110 | $valid++; |
111 | } | |
112 | // editing the relationship | |
113 | } | |
114 | else { | |
115 | // check for duplicate relationship | |
f0fc26a5 DL |
116 | // @todo this code doesn't cope well with updates - causes e-Notices. |
117 | // API has a lot of code to work around | |
49f8272d | 118 | // this but should review this code & remove the extra handling from the api |
f0fc26a5 DL |
119 | // it seems doubtful any of this is relevant if the contact fields & relationship |
120 | // type fields are not set | |
6a488035 TO |
121 | if ( |
122 | self::checkDuplicateRelationship( | |
123 | $params, | |
124 | CRM_Utils_Array::value('contact', $ids), | |
125 | $ids['contactTarget'], | |
126 | $relationshipId | |
127 | ) | |
128 | ) { | |
129 | $duplicate++; | |
43291913 | 130 | return array($valid, $invalid, $duplicate, $saved, NULL); |
6a488035 TO |
131 | } |
132 | ||
133 | $validContacts = TRUE; | |
134 | //validate contacts in update mode also. | |
135 | if (CRM_Utils_Array::value('contact', $ids) && | |
136 | CRM_Utils_Array::value('contactTarget', $ids) | |
137 | ) { | |
138 | if (self::checkValidRelationship($params, $ids, $ids['contactTarget'])) { | |
139 | $validContacts = FALSE; | |
140 | $invalid++; | |
141 | } | |
142 | } | |
143 | if ($validContacts) { | |
144 | // editing an existing relationship | |
145 | $relationship = self::add($params, $ids, $ids['contactTarget']); | |
146 | $relationshipIds[] = $relationship->id; | |
26dcc9eb | 147 | $relationships[$relationship->id] = $relationship; |
6a488035 TO |
148 | $saved++; |
149 | } | |
150 | } | |
151 | ||
152 | // do not add to recent items for import, CRM-4399 | |
153 | if (!(CRM_Utils_Array::value('skipRecentView', $params) || $invalid || $duplicate)) { | |
154 | $url = CRM_Utils_System::url('civicrm/contact/view/rel', | |
155 | "action=view&reset=1&id={$relationship->id}&cid={$relationship->contact_id_a}&context=home" | |
156 | ); | |
157 | ||
158 | ||
159 | $session = CRM_Core_Session::singleton(); | |
160 | $recentOther = array(); | |
161 | if (($session->get('userID') == $relationship->contact_id_a) || | |
162 | CRM_Contact_BAO_Contact_Permission::allow($relationship->contact_id_a, CRM_Core_Permission::EDIT) | |
163 | ) { | |
164 | $rType = substr(CRM_Utils_Array::value('relationship_type_id', $params), -3); | |
165 | $recentOther = array( | |
166 | 'editUrl' => CRM_Utils_System::url('civicrm/contact/view/rel', | |
167 | "action=update&reset=1&id={$relationship->id}&cid={$relationship->contact_id_a}&rtype={$rType}&context=home" | |
168 | ), | |
169 | 'deleteUrl' => CRM_Utils_System::url('civicrm/contact/view/rel', | |
170 | "action=delete&reset=1&id={$relationship->id}&cid={$relationship->contact_id_a}&rtype={$rType}&context=home" | |
171 | ), | |
172 | ); | |
173 | } | |
174 | ||
175 | $title = CRM_Contact_BAO_Contact::displayName($relationship->contact_id_a) . ' (' . CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', | |
176 | $relationship->relationship_type_id, 'label_a_b' | |
177 | ) . ' ' . CRM_Contact_BAO_Contact::displayName($relationship->contact_id_b) . ')'; | |
6a488035 TO |
178 | // add the recently created Relationship |
179 | CRM_Utils_Recent::add($title, | |
180 | $url, | |
181 | $relationship->id, | |
182 | 'Relationship', | |
183 | $relationship->contact_id_a, | |
184 | NULL, | |
185 | $recentOther | |
186 | ); | |
187 | } | |
188 | ||
26dcc9eb | 189 | return array($valid, $invalid, $duplicate, $saved, $relationshipIds, $relationships); |
6a488035 TO |
190 | } |
191 | ||
192 | /** | |
193 | * This is the function that check/add if the relationship created is valid | |
194 | * | |
195 | * @param array $params (reference ) an assoc array of name/value pairs | |
196 | * @param integer $contactId this is contact id for adding relationship | |
197 | * @param array $ids the array that holds all the db ids | |
198 | * | |
199 | * @return object CRM_Contact_BAO_Relationship | |
200 | * @access public | |
201 | * @static | |
202 | */ | |
49f8272d | 203 | static function add(&$params, $ids = array(), $contactId = NULL) { |
f0fc26a5 DL |
204 | $relationshipId = |
205 | CRM_Utils_Array::value('relationship', $ids, CRM_Utils_Array::value('id', $params)); | |
206 | ||
49f8272d E |
207 | $hook = 'create'; |
208 | if($relationshipId) { | |
209 | $hook = 'edit'; | |
6a488035 | 210 | } |
49f8272d E |
211 | //@todo hook are called from create and add - remove one |
212 | CRM_Utils_Hook::pre($hook , 'Relationship', $relationshipId, $params); | |
6a488035 TO |
213 | |
214 | $relationshipTypes = CRM_Utils_Array::value('relationship_type_id', $params); | |
215 | ||
f0fc26a5 DL |
216 | // explode the string with _ to get the relationship type id |
217 | // and to know which contact has to be inserted in | |
6a488035 TO |
218 | // contact_id_a and which one in contact_id_b |
219 | list($type, $first, $second) = explode('_', $relationshipTypes); | |
220 | ||
221 | ${'contact_' . $first} = CRM_Utils_Array::value('contact', $ids); | |
222 | ${'contact_' . $second} = $contactId; | |
223 | ||
f0fc26a5 DL |
224 | // check if the relationship type is Head of Household then update the |
225 | // household's primary contact with this contact. | |
6a488035 TO |
226 | if ($type == 6) { |
227 | CRM_Contact_BAO_Household::updatePrimaryContact($contact_b, $contact_a); | |
228 | } | |
229 | ||
230 | $relationship = new CRM_Contact_BAO_Relationship(); | |
49f8272d E |
231 | //@todo this code needs to be updated for the possibility that not all fields are set |
232 | // (update) | |
6a488035 TO |
233 | $relationship->contact_id_b = $contact_b; |
234 | $relationship->contact_id_a = $contact_a; | |
235 | $relationship->relationship_type_id = $type; | |
49f8272d | 236 | $relationship->id = $relationshipId; |
6a488035 TO |
237 | |
238 | $dateFields = array('end_date', 'start_date'); | |
239 | ||
240 | foreach (self::getdefaults() as $defaultField => $defaultValue){ | |
241 | if(isset($params[$defaultField])){ | |
242 | if(in_array($defaultField, $dateFields)){ | |
243 | $relationship->$defaultField = CRM_Utils_Date::format(CRM_Utils_Array::value($defaultField, $params)); | |
244 | if(!$relationship->$defaultField){ | |
245 | $relationship->$defaultField = 'NULL'; | |
246 | } | |
247 | } | |
248 | else{ | |
249 | $relationship->$defaultField = $params[$defaultField]; | |
250 | } | |
251 | } | |
49f8272d | 252 | elseif(!$relationshipId){ |
6a488035 TO |
253 | $relationship->$defaultField = $defaultValue; |
254 | } | |
255 | } | |
256 | ||
257 | $relationship->save(); | |
258 | ||
259 | // add custom field values | |
260 | if (CRM_Utils_Array::value('custom', $params)) { | |
261 | CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_relationship', $relationship->id); | |
262 | } | |
263 | ||
264 | $relationship->free(); | |
265 | ||
49f8272d | 266 | CRM_Utils_Hook::post($hook, 'Relationship', $relationshipId, $relationship); |
6a488035 TO |
267 | |
268 | return $relationship; | |
269 | } | |
270 | /** | |
271 | * Specifiy defaults for creating a relationship | |
272 | * | |
273 | * @return array $defaults array of defaults for creating relationship | |
274 | * @access public | |
275 | * @static | |
276 | */ | |
277 | static function getdefaults() { | |
278 | return array( | |
279 | 'is_active' => 0, | |
280 | 'is_permission_a_b' => 0, | |
281 | 'is_permission_b_a' => 0, | |
282 | 'description' => '', | |
283 | 'start_date' => 'NULL', | |
284 | 'case_id' => NULL, | |
285 | 'end_date' => 'NULL', | |
286 | ); | |
287 | } | |
288 | ||
289 | ||
290 | /** | |
291 | * Check if there is data to create the object | |
292 | * | |
293 | * @param array $params (reference ) an assoc array of name/value pairs | |
294 | * | |
295 | * @return boolean | |
296 | * @access public | |
297 | * @static | |
298 | */ | |
299 | static function dataExists(&$params) { | |
300 | // return if no data present | |
301 | if (!is_array(CRM_Utils_Array::value('contact_check', $params))) { | |
302 | return FALSE; | |
303 | } | |
304 | return TRUE; | |
305 | } | |
306 | ||
307 | /** | |
308 | * Function to get get list of relationship type based on the contact type. | |
309 | * | |
310 | * @param int $contactId this is the contact id of the current contact. | |
311 | * @param string $strContact this value is currently ignored, keeping it there for legacy reasons | |
312 | * @param string $relationshipId the id of the existing relationship if any | |
313 | * @param string $contactType contact type | |
314 | * @param boolean $all if true returns relationship types in both the direction | |
315 | * @param string $column name/label that going to retrieve from db. | |
316 | * | |
317 | * | |
318 | * @param string $contactSubType includes relationshiptypes between this subtype | |
319 | * | |
320 | * @param boolean $onlySubTypeRelationTypes if set only subtype which is passed by $contactSubType | |
321 | * related relationshiptypes get return | |
322 | * @access public | |
323 | * @static | |
324 | * | |
325 | * @return array - array reference of all relationship types with context to current contact. | |
326 | */ | |
327 | static function getContactRelationshipType($contactId = NULL, | |
328 | $contactSuffix = NULL, | |
329 | $relationshipId = NULL, | |
330 | $contactType = NULL, | |
331 | $all = FALSE, | |
332 | $column = 'label', | |
333 | $biDirectional = TRUE, | |
334 | $contactSubType = NULL, | |
335 | $onlySubTypeRelationTypes = FALSE | |
336 | ) { | |
337 | $allRelationshipType = array(); | |
338 | $relationshipType = array(); | |
339 | $allRelationshipType = CRM_Core_PseudoConstant::relationshipType($column); | |
340 | ||
341 | $otherContactType = NULL; | |
342 | if ($relationshipId) { | |
343 | $relationship = new CRM_Contact_DAO_Relationship(); | |
344 | $relationship->id = $relationshipId; | |
345 | if ($relationship->find(TRUE)) { | |
346 | $contact = new CRM_Contact_DAO_Contact(); | |
347 | $contact->id = ($relationship->contact_id_a === $contactId) ? $relationship->contact_id_b : $relationship->contact_id_a; | |
348 | ||
349 | if ($contact->find(TRUE)) { | |
350 | $otherContactType = $contact->contact_type; | |
351 | //CRM-5125 for contact subtype specific relationshiptypes | |
352 | if ($contact->contact_sub_type) { | |
353 | $otherContactSubType = $contact->contact_sub_type; | |
354 | } | |
355 | } | |
356 | } | |
357 | } | |
358 | ||
359 | $contactSubType = array(); | |
360 | if ($contactId) { | |
361 | $contactType = CRM_Contact_BAO_Contact::getContactType($contactId); | |
362 | $contactSubType = CRM_Contact_BAO_Contact::getContactSubType($contactId); | |
363 | } | |
364 | ||
365 | foreach ($allRelationshipType as $key => $value) { | |
366 | // the contact type is required or matches | |
367 | if (((!$value['contact_type_a']) || | |
368 | $value['contact_type_a'] == $contactType | |
369 | ) && | |
370 | // the other contact type is required or present or matches | |
371 | ((!$value['contact_type_b']) || | |
372 | (!$otherContactType) || | |
373 | $value['contact_type_b'] == $otherContactType | |
374 | ) && | |
375 | (!$contactSubType || | |
376 | (in_array($value['contact_sub_type_a'], $contactSubType) || | |
377 | ((!$value['contact_sub_type_b'] && | |
378 | !$value['contact_sub_type_a'] | |
379 | ) && | |
380 | !$onlySubTypeRelationTypes | |
381 | ) | |
382 | ) | |
383 | ) | |
384 | ) { | |
385 | $relationshipType[$key . '_a_b'] = $value["{$column}_a_b"]; | |
386 | } | |
387 | elseif (empty($value['contact_type_b'])) { | |
388 | // case contact_type_b is "all contacts", CRM-9877 | |
389 | $relationshipType[$key . '_a_b'] = $value["{$column}_a_b"]; | |
390 | } | |
391 | ||
392 | if (((!$value['contact_type_b']) || | |
393 | $value['contact_type_b'] == $contactType | |
394 | ) && | |
395 | ((!$value['contact_type_a']) || | |
396 | (!$otherContactType) || | |
397 | $value['contact_type_a'] == $otherContactType | |
398 | ) && | |
399 | (!$contactSubType || | |
400 | (in_array($value['contact_sub_type_b'], $contactSubType) || | |
401 | ((!$value['contact_sub_type_a'] && | |
402 | !$value['contact_sub_type_b'] | |
403 | ) && | |
404 | !$onlySubTypeRelationTypes | |
405 | ) | |
406 | ) | |
407 | ) | |
408 | ) { | |
409 | $relationshipType[$key . '_b_a'] = $value["{$column}_b_a"]; | |
410 | } | |
411 | elseif (empty($value['contact_type_a'])) { | |
412 | // case contact_type_a is "all contacts", CRM-9877 | |
413 | $relationshipType[$key . '_b_a'] = $value["{$column}_b_a"]; | |
414 | } | |
415 | ||
416 | if ($all) { | |
417 | $relationshipType[$key . '_a_b'] = $value["{$column}_a_b"]; | |
418 | $relationshipType[$key . '_b_a'] = $value["{$column}_b_a"]; | |
419 | } | |
420 | } | |
421 | ||
422 | if ($biDirectional) { | |
423 | // lets clean up the data and eliminate all duplicate values | |
424 | // (i.e. the relationship is bi-directional) | |
425 | $relationshipType = array_unique($relationshipType); | |
426 | } | |
427 | ||
428 | // sort the relationshipType in ascending order CRM-7736 | |
429 | asort($relationshipType); | |
430 | return $relationshipType; | |
431 | } | |
432 | ||
433 | static function clearCurrentEmployer($id, $action) { | |
434 | $relationship = new CRM_Contact_DAO_Relationship(); | |
435 | $relationship->id = $id; | |
436 | $relationship->find(TRUE); | |
437 | ||
438 | //to delete relationship between household and individual \ | |
439 | //or between individual and orgnization | |
440 | if (($action & CRM_Core_Action::DISABLE) || ($action & CRM_Core_Action::DELETE)) { | |
05802b8e DG |
441 | $relTypes = CRM_Utils_Array::index(array('name_a_b'), CRM_Core_PseudoConstant::relationshipType('name')); |
442 | if ($relationship->relationship_type_id == $relTypes['Employee of']['id'] || | |
443 | $relationship->relationship_type_id == $relTypes['Household Member of']['id']) { | |
444 | $sharedContact = new CRM_Contact_DAO_Contact(); | |
445 | $sharedContact->id = $relationship->contact_id_a; | |
446 | $sharedContact->find(TRUE); | |
447 | ||
448 | if ($relationship->relationship_type_id == 4 && $relationship->contact_id_b == $sharedContact->employer_id) { | |
449 | CRM_Contact_BAO_Contact_Utils::clearCurrentEmployer($relationship->contact_id_a); | |
450 | } | |
6a488035 TO |
451 | } |
452 | } | |
6a488035 TO |
453 | return $relationship; |
454 | } | |
455 | ||
456 | /** | |
457 | * Function to delete the relationship | |
458 | * | |
459 | * @param int $id relationship id | |
460 | * | |
461 | * @return null | |
462 | * @access public | |
463 | * | |
464 | * @static | |
465 | */ | |
466 | static function del($id) { | |
467 | // delete from relationship table | |
468 | CRM_Utils_Hook::pre('delete', 'Relationship', $id, CRM_Core_DAO::$_nullArray); | |
469 | ||
470 | $relationship = self::clearCurrentEmployer($id, CRM_Core_Action::DELETE); | |
471 | if (CRM_Core_Permission::access('CiviMember')) { | |
472 | // create $params array which isrequired to delete memberships | |
473 | // of the related contacts. | |
474 | $params = array( | |
475 | 'relationship_type_id' => "{$relationship->relationship_type_id}_a_b", | |
476 | 'contact_check' => array($relationship->contact_id_b => 1), | |
477 | ); | |
478 | ||
479 | $ids = array(); | |
480 | // calling relatedMemberships to delete the memberships of | |
481 | // related contacts. | |
482 | self::relatedMemberships($relationship->contact_id_a, | |
483 | $params, | |
484 | $ids, | |
485 | CRM_Core_Action::DELETE, | |
486 | FALSE | |
487 | ); | |
488 | } | |
489 | ||
490 | $relationship->delete(); | |
491 | CRM_Core_Session::setStatus(ts('Selected relationship has been deleted successfully.'), ts('Record Deleted'), 'success'); | |
492 | ||
493 | CRM_Utils_Hook::post('delete', 'Relationship', $relationship->id, $relationship); | |
494 | ||
495 | // delete the recently created Relationship | |
496 | $relationshipRecent = array( | |
497 | 'id' => $id, | |
498 | 'type' => 'Relationship', | |
499 | ); | |
500 | CRM_Utils_Recent::del($relationshipRecent); | |
501 | ||
502 | return $relationship; | |
503 | } | |
504 | ||
505 | /** | |
506 | * Function to disable/enable the relationship | |
507 | * | |
508 | * @param int $id relationship id | |
509 | * | |
510 | * @return null | |
511 | * @access public | |
512 | ||
513 | * @static | |
514 | */ | |
515 | static function disableEnableRelationship($id, $action) { | |
516 | $relationship = self::clearCurrentEmployer($id, $action); | |
517 | if (CRM_Core_Permission::access('CiviMember')) { | |
518 | // create $params array which isrequired to delete memberships | |
519 | // of the related contacts. | |
520 | $params = array( | |
521 | 'relationship_type_id' => "{$relationship->relationship_type_id}_a_b", | |
522 | 'contact_check' => array($relationship->contact_id_b => 1), | |
523 | ); | |
524 | ||
525 | $ids = array(); | |
526 | // calling relatedMemberships to delete/add the memberships of | |
527 | // related contacts. | |
528 | if ($action & CRM_Core_Action::DISABLE) { | |
529 | CRM_Contact_BAO_Relationship::relatedMemberships($relationship->contact_id_a, | |
530 | $params, | |
531 | $ids, | |
532 | CRM_Core_Action::DELETE, | |
533 | FALSE | |
534 | ); | |
535 | } | |
536 | elseif ($action & CRM_Core_Action::ENABLE) { | |
537 | $ids['contact'] = $relationship->contact_id_a; | |
538 | CRM_Contact_BAO_Relationship::relatedMemberships($relationship->contact_id_a, | |
539 | $params, | |
540 | $ids, | |
541 | CRM_Core_Action::ADD, | |
542 | FALSE | |
543 | ); | |
544 | } | |
545 | } | |
546 | } | |
547 | ||
548 | /** | |
549 | * Delete the object records that are associated with this contact | |
550 | * | |
551 | * @param int $contactId id of the contact to delete | |
552 | * | |
553 | * @return void | |
554 | * @access public | |
555 | * @static | |
556 | */ | |
557 | static function deleteContact($contactId) { | |
558 | $relationship = new CRM_Contact_DAO_Relationship(); | |
559 | $relationship->contact_id_a = $contactId; | |
560 | $relationship->delete(); | |
561 | ||
562 | $relationship = new CRM_Contact_DAO_Relationship(); | |
563 | $relationship->contact_id_b = $contactId; | |
564 | $relationship->delete(); | |
565 | ||
566 | CRM_Contact_BAO_Household::updatePrimaryContact(NULL, $contactId); | |
567 | } | |
568 | ||
569 | /** | |
570 | * Function to get the other contact in a relationship | |
571 | * | |
572 | * @param int $id relationship id | |
573 | * | |
574 | * $returns returns the contact ids in the realtionship | |
575 | * @access public | |
576 | * @static | |
577 | */ | |
578 | static function getContactIds($id) { | |
579 | $relationship = new CRM_Contact_DAO_Relationship(); | |
580 | ||
581 | $relationship->id = $id; | |
582 | $relationship->selectAdd(); | |
583 | $relationship->selectAdd('contact_id_a, contact_id_b'); | |
584 | $relationship->find(TRUE); | |
585 | ||
586 | return $relationship; | |
587 | } | |
588 | ||
589 | /** | |
590 | * Function to check if the relationship type selected between two contacts is correct | |
591 | * | |
592 | * @param int $contact_a 1st contact id | |
593 | * @param int $contact_b 2nd contact id | |
594 | * @param int $relationshipTypeId relationship type id | |
595 | * | |
596 | * @return boolean true if it is valid relationship else false | |
597 | * @access public | |
598 | * @static | |
599 | */ | |
600 | static function checkRelationshipType($contact_a, $contact_b, $relationshipTypeId) { | |
601 | $relationshipType = new CRM_Contact_DAO_RelationshipType(); | |
602 | $relationshipType->id = $relationshipTypeId; | |
603 | $relationshipType->selectAdd(); | |
604 | $relationshipType->selectAdd('contact_type_a, contact_type_b, contact_sub_type_a, contact_sub_type_b'); | |
605 | if ($relationshipType->find(TRUE)) { | |
606 | $contact_type_a = CRM_Contact_BAO_Contact::getContactType($contact_a); | |
607 | $contact_type_b = CRM_Contact_BAO_Contact::getContactType($contact_b); | |
608 | ||
609 | $contact_sub_type_a = CRM_Contact_BAO_Contact::getContactSubType($contact_a); | |
610 | $contact_sub_type_b = CRM_Contact_BAO_Contact::getContactSubType($contact_b); | |
611 | ||
612 | if (((!$relationshipType->contact_type_a) || ($relationshipType->contact_type_a == $contact_type_a)) && | |
613 | ((!$relationshipType->contact_type_b) || ($relationshipType->contact_type_b == $contact_type_b)) && | |
614 | ((!$relationshipType->contact_sub_type_a) || (in_array($relationshipType->contact_sub_type_a, | |
615 | $contact_sub_type_a | |
616 | ))) && | |
617 | ((!$relationshipType->contact_sub_type_b) || (in_array($relationshipType->contact_sub_type_b, | |
618 | $contact_sub_type_b | |
619 | ))) | |
620 | ) { | |
621 | return TRUE; | |
622 | } | |
623 | else { | |
624 | return FALSE; | |
625 | } | |
626 | } | |
627 | return FALSE; | |
628 | } | |
629 | ||
630 | /** | |
631 | * this function does the validtion for valid relationship | |
632 | * | |
633 | * @param array $params this array contains the values there are subitted by the form | |
634 | * @param array $ids the array that holds all the db ids | |
635 | * @param integer $contactId this is contact id for adding relationship | |
636 | * | |
637 | * @return | |
638 | * @access public | |
639 | * @static | |
640 | */ | |
641 | static function checkValidRelationship(&$params, &$ids, $contactId) { | |
642 | $errors = ''; | |
643 | ||
644 | // get the string of relationship type | |
645 | $relationshipTypes = CRM_Utils_Array::value('relationship_type_id', $params); | |
646 | list($type, $first, $second) = explode('_', $relationshipTypes); | |
647 | ${'contact_' . $first} = CRM_Utils_Array::value('contact', $ids); | |
648 | ${'contact_' . $second} = $contactId; | |
649 | // function to check if the relationship selected is correct | |
650 | // i.e. employer relationship can exit between Individual and Organization (not between Individual and Individual) | |
651 | if (!CRM_Contact_BAO_Relationship::checkRelationshipType($contact_a, $contact_b, $type)) { | |
652 | $errors = 'Please select valid relationship between these two contacts.'; | |
653 | } | |
654 | return $errors; | |
655 | } | |
656 | ||
657 | /** | |
658 | * this function checks for duplicate relationship | |
659 | * | |
660 | * @param array $params (reference ) an assoc array of name/value pairs | |
661 | * @param integer $id this the id of the contact whom we are adding relationship | |
662 | * @param integer $contactId this is contact id for adding relationship | |
663 | * @param integer $relationshipId this is relationship id for the contact | |
664 | * | |
665 | * @return boolean true if record exists else false | |
666 | * @access public | |
667 | * @static | |
668 | */ | |
669 | static function checkDuplicateRelationship(&$params, $id, $contactId = 0, $relationshipId = 0) { | |
670 | $relationshipTypeId = CRM_Utils_Array::value('relationship_type_id', $params); | |
671 | list($type, $first, $second) = explode('_', $relationshipTypeId); | |
672 | ||
f0fc26a5 DL |
673 | $queryString = " |
674 | SELECT id | |
675 | FROM civicrm_relationship | |
676 | WHERE relationship_type_id = " . CRM_Utils_Type::escape($type, 'Integer'); | |
6a488035 TO |
677 | |
678 | /* | |
f0fc26a5 DL |
679 | * CRM-11792 - date fields from API are in ISO format, but this function |
680 | * supports date arrays BAO has increasingly standardised to ISO format | |
681 | * so I believe this function should support ISO rather than make API | |
682 | * format it - however, need to support array format for now to avoid breakage | |
6a488035 TO |
683 | * @ time of writing this function is called from Relationship::create (twice) |
684 | * CRM_BAO_Contact_Utils::clearCurrentEmployer (seemingly without dates) | |
685 | * CRM_Contact_Form_Task_AddToOrganization::postProcess & | |
686 | * CRM_Contact_Form_Task_AddToHousehold::postProcess | |
687 | * (I don't think the last 2 support dates but not sure | |
688 | */ | |
689 | ||
690 | $dateFields = array('end_date', 'start_date'); | |
691 | foreach ($dateFields as $dateField){ | |
692 | if(array_key_exists($dateField, $params)) { | |
693 | if (empty($params[$dateField]) || $params[$dateField] == 'null'){ | |
f0fc26a5 DL |
694 | //this is most likely coming from an api call & probably loaded |
695 | // from the DB to deal with some of the | |
696 | // other myriad of excessive checks still in place both in | |
697 | // the api & the create functions | |
6a488035 TO |
698 | $queryString .= " AND $dateField IS NULL"; |
699 | continue; | |
700 | } | |
701 | elseif (is_array($params[$dateField])){ | |
f0fc26a5 DL |
702 | $queryString .= " AND $dateField = " . |
703 | CRM_Utils_Type::escape(CRM_Utils_Date::format($params[$dateField]), 'Date'); | |
6a488035 | 704 | } |
f0fc26a5 DL |
705 | else { |
706 | $queryString .= " AND $dateField = " . | |
707 | CRM_Utils_Type::escape($params[$dateField], 'Date'); | |
6a488035 TO |
708 | } |
709 | } | |
710 | } | |
711 | ||
712 | $queryString .= | |
713 | " AND ( ( contact_id_a = " . CRM_Utils_Type::escape($id, 'Integer') . | |
714 | " AND contact_id_b = " . CRM_Utils_Type::escape($contactId, 'Integer') . | |
715 | " ) OR ( contact_id_a = " . CRM_Utils_Type::escape($contactId, 'Integer') . | |
716 | " AND contact_id_b = " . CRM_Utils_Type::escape($id, 'Integer') . " ) ) "; | |
717 | ||
718 | //if caseId is provided, include it duplicate checking. | |
719 | if ($caseId = CRM_Utils_Array::value('case_id', $params)) { | |
720 | $queryString .= " AND case_id = " . CRM_Utils_Type::escape($caseId, 'Integer'); | |
721 | } | |
722 | ||
723 | if ($relationshipId) { | |
724 | $queryString .= " AND id !=" . CRM_Utils_Type::escape($relationshipId, 'Integer'); | |
725 | } | |
726 | ||
727 | $relationship = new CRM_Contact_BAO_Relationship(); | |
728 | $relationship->query($queryString); | |
729 | $relationship->fetch(); | |
730 | $relationship->free(); | |
731 | return ($relationship->id) ? TRUE : FALSE; | |
732 | } | |
733 | ||
734 | /** | |
735 | * update the is_active flag in the db | |
736 | * | |
737 | * @param int $id id of the database record | |
738 | * @param boolean $is_active value we want to set the is_active field | |
739 | * | |
740 | * @return Object DAO object on success, null otherwise | |
741 | * @static | |
742 | */ | |
743 | static function setIsActive($id, $is_active) { | |
49f8272d E |
744 | // as both the create & add functions have a bunch of logic in them that |
745 | // doesn't seem to cope with a normal update we will call the api which | |
746 | // has tested handling for this | |
747 | // however, a longer term solution would be to simplify the add, create & api functions | |
748 | // to be more standard. It is debatable @ that point whether it's better to call the BAO | |
749 | // direct as the api is more tested. | |
750 | civicrm_api3('relationship', 'create', array('id' => $id, 'is_active' => $is_active)); | |
751 | ||
752 | // call (undocumented possibly deprecated) hook | |
6a488035 TO |
753 | CRM_Utils_Hook::enableDisable('CRM_Contact_BAO_Relationship', $id, $is_active); |
754 | ||
755 | return TRUE; | |
756 | } | |
757 | ||
758 | /** | |
759 | * Given the list of params in the params array, fetch the object | |
760 | * and store the values in the values array | |
761 | * | |
762 | * @param array $params input parameters to find object | |
763 | * @param array $values output values of the object | |
764 | * @param array $ids the array that holds all the db ids | |
765 | * | |
766 | * @return array (reference) the values that could be potentially assigned to smarty | |
767 | * @access public | |
768 | * @static | |
769 | */ | |
770 | static function &getValues(&$params, &$values) { | |
771 | if (empty($params)) { | |
772 | return NULL; | |
773 | } | |
774 | $v = array(); | |
775 | ||
776 | // get the specific number of relationship or all relationships. | |
777 | if (CRM_Utils_Array::value('numRelationship', $params)) { | |
778 | $v['data'] = &CRM_Contact_BAO_Relationship::getRelationship($params['contact_id'], NULL, $params['numRelationship']); | |
779 | } | |
780 | else { | |
781 | $v['data'] = CRM_Contact_BAO_Relationship::getRelationship($params['contact_id']); | |
782 | } | |
783 | ||
784 | // get the total count of relationships | |
785 | $v['totalCount'] = CRM_Contact_BAO_Relationship::getRelationship($params['contact_id'], NULL, NULL, TRUE); | |
786 | ||
787 | $values['relationship']['data'] = &$v['data']; | |
788 | $values['relationship']['totalCount'] = &$v['totalCount']; | |
789 | ||
790 | return $v; | |
791 | } | |
792 | ||
793 | /** | |
794 | * helper function to form the sql for relationship retrieval | |
795 | * | |
796 | * @param int $contactId contact id | |
797 | * @param int $status (check const at top of file) | |
798 | * @param int $numRelationship no of relationships to display (limit) | |
799 | * @param int $count get the no of relationships | |
800 | * $param int $relationshipId relationship id | |
801 | * @param string $direction the direction we are interested in a_b or b_a | |
75638074 | 802 | * @param array $params array of extra values including relationship_type_id per api spec |
6a488035 TO |
803 | * |
804 | * return string the query for this diretion | |
805 | * @static | |
806 | * @access public | |
807 | */ | |
75638074 | 808 | static function makeURLClause($contactId, $status, $numRelationship, $count, $relationshipId, $direction, $params = array()) { |
6a488035 TO |
809 | $select = $from = $where = ''; |
810 | ||
811 | $select = '( '; | |
812 | if ($count) { | |
813 | if ($direction == 'a_b') { | |
814 | $select .= ' SELECT count(DISTINCT civicrm_relationship.id) as cnt1, 0 as cnt2 '; | |
815 | } | |
816 | else { | |
817 | $select .= ' SELECT 0 as cnt1, count(DISTINCT civicrm_relationship.id) as cnt2 '; | |
818 | } | |
819 | } | |
820 | else { | |
821 | $select .= ' SELECT civicrm_relationship.id as civicrm_relationship_id, | |
822 | civicrm_contact.sort_name as sort_name, | |
823 | civicrm_contact.display_name as display_name, | |
824 | civicrm_contact.job_title as job_title, | |
825 | civicrm_contact.employer_id as employer_id, | |
826 | civicrm_contact.organization_name as organization_name, | |
827 | civicrm_address.street_address as street_address, | |
828 | civicrm_address.city as city, | |
829 | civicrm_address.postal_code as postal_code, | |
830 | civicrm_state_province.abbreviation as state, | |
831 | civicrm_country.name as country, | |
832 | civicrm_email.email as email, | |
833 | civicrm_phone.phone as phone, | |
834 | civicrm_contact.id as civicrm_contact_id, | |
835 | civicrm_contact.contact_type as contact_type, | |
836 | civicrm_relationship.contact_id_b as contact_id_b, | |
837 | civicrm_relationship.contact_id_a as contact_id_a, | |
838 | civicrm_relationship_type.id as civicrm_relationship_type_id, | |
839 | civicrm_relationship.start_date as start_date, | |
840 | civicrm_relationship.end_date as end_date, | |
841 | civicrm_relationship.description as description, | |
842 | civicrm_relationship.is_active as is_active, | |
843 | civicrm_relationship.is_permission_a_b as is_permission_a_b, | |
844 | civicrm_relationship.is_permission_b_a as is_permission_b_a, | |
845 | civicrm_relationship.case_id as case_id'; | |
846 | ||
847 | if ($direction == 'a_b') { | |
848 | $select .= ', civicrm_relationship_type.label_a_b as label_a_b, | |
849 | civicrm_relationship_type.label_b_a as relation '; | |
850 | } | |
851 | else { | |
852 | $select .= ', civicrm_relationship_type.label_a_b as label_a_b, | |
853 | civicrm_relationship_type.label_a_b as relation '; | |
854 | } | |
855 | } | |
856 | ||
857 | $from = " | |
858 | FROM civicrm_relationship | |
859 | INNER JOIN civicrm_relationship_type ON ( civicrm_relationship.relationship_type_id = civicrm_relationship_type.id ) | |
860 | INNER JOIN civicrm_contact "; | |
861 | if ($direction == 'a_b') { | |
862 | $from .= 'ON ( civicrm_contact.id = civicrm_relationship.contact_id_a ) '; | |
863 | } | |
864 | else { | |
865 | $from .= 'ON ( civicrm_contact.id = civicrm_relationship.contact_id_b ) '; | |
866 | } | |
867 | $from .= " | |
868 | LEFT JOIN civicrm_address ON (civicrm_address.contact_id = civicrm_contact.id AND civicrm_address.is_primary = 1) | |
869 | LEFT JOIN civicrm_phone ON (civicrm_phone.contact_id = civicrm_contact.id AND civicrm_phone.is_primary = 1) | |
870 | LEFT JOIN civicrm_email ON (civicrm_email.contact_id = civicrm_contact.id AND civicrm_email.is_primary = 1) | |
871 | LEFT JOIN civicrm_state_province ON (civicrm_address.state_province_id = civicrm_state_province.id) | |
872 | LEFT JOIN civicrm_country ON (civicrm_address.country_id = civicrm_country.id) | |
873 | "; | |
874 | $where = 'WHERE ( 1 )'; | |
875 | if ($contactId) { | |
876 | if ($direction == 'a_b') { | |
877 | $where .= ' AND civicrm_relationship.contact_id_b = ' . CRM_Utils_Type::escape($contactId, 'Positive'); | |
878 | } | |
879 | else { | |
880 | $where .= ' AND civicrm_relationship.contact_id_a = ' . CRM_Utils_Type::escape($contactId, 'Positive'); | |
881 | } | |
882 | } | |
883 | if ($relationshipId) { | |
884 | $where .= ' AND civicrm_relationship.id = ' . CRM_Utils_Type::escape($relationshipId, 'Positive'); | |
885 | } | |
886 | ||
887 | $date = date('Y-m-d'); | |
888 | if ($status == self::PAST) { | |
889 | //this case for showing past relationship | |
890 | $where .= ' AND civicrm_relationship.is_active = 1 '; | |
891 | $where .= " AND civicrm_relationship.end_date < '" . $date . "'"; | |
892 | } | |
893 | elseif ($status == self::DISABLED) { | |
894 | // this case for showing disabled relationship | |
895 | $where .= ' AND civicrm_relationship.is_active = 0 '; | |
896 | } | |
897 | elseif ($status == self::CURRENT) { | |
898 | //this case for showing current relationship | |
899 | $where .= ' AND civicrm_relationship.is_active = 1 '; | |
900 | $where .= " AND (civicrm_relationship.end_date >= '" . $date . "' OR civicrm_relationship.end_date IS NULL) "; | |
901 | } | |
902 | elseif ($status == self::INACTIVE) { | |
903 | //this case for showing inactive relationships | |
904 | $where .= " AND (civicrm_relationship.end_date < '" . $date . "'"; | |
905 | $where .= ' OR civicrm_relationship.is_active = 0 )'; | |
906 | } | |
907 | ||
908 | // CRM-6181 | |
909 | $where .= ' AND civicrm_contact.is_deleted = 0'; | |
c9c41397 | 910 | if(!empty($params['membership_type_id']) && empty($params['relationship_type_id'])) { |
911 | $where .= self::membershipTypeToRelationshipTypes($params, $direction); | |
912 | } | |
75638074 | 913 | if(!empty($params['relationship_type_id'])) { |
914 | if(is_array($params['relationship_type_id'])) { | |
faf8c53b | 915 | $where .= " AND " . CRM_Core_DAO::createSQLFilter('relationship_type_id', $params['relationship_type_id'], 'Integer'); |
75638074 | 916 | } |
917 | else { | |
918 | $where .= ' AND relationship_type_id = ' . CRM_Utils_Type::escape($params['relationship_type_id'], 'Positive'); | |
919 | } | |
920 | } | |
6a488035 TO |
921 | if ($direction == 'a_b') { |
922 | $where .= ' ) UNION '; | |
923 | } | |
924 | else { | |
925 | $where .= ' ) '; | |
926 | } | |
927 | ||
928 | return array($select, $from, $where); | |
929 | } | |
930 | ||
931 | /** | |
932 | * This is the function to get the list of relationships | |
933 | * | |
934 | * @param int $contactId contact id | |
935 | * @param int $status 1: Past 2: Disabled 3: Current | |
936 | * @param int $numRelationship no of relationships to display (limit) | |
937 | * @param int $count get the no of relationships | |
938 | * $param int $relationshipId relationship id | |
939 | * $param array $links the list of links to display | |
940 | * $param int $permissionMask the permission mask to be applied for the actions | |
941 | * $param boolean $permissionedContact to return only permissioned Contact | |
75638074 | 942 | * $param array $params array of variables consistent with filters supported by the api |
6a488035 TO |
943 | * return array $values relationship records |
944 | * @static | |
945 | * @access public | |
946 | */ | |
947 | static function getRelationship($contactId = NULL, | |
948 | $status = 0, $numRelationship = 0, | |
949 | $count = 0, $relationshipId = 0, | |
950 | $links = NULL, $permissionMask = NULL, | |
75638074 | 951 | $permissionedContact = FALSE, |
952 | $params = array() | |
6a488035 TO |
953 | ) { |
954 | $values = array(); | |
955 | if (!$contactId && !$relationshipId) { | |
956 | return $values; | |
957 | } | |
958 | ||
959 | list($select1, $from1, $where1) = self::makeURLClause($contactId, $status, $numRelationship, | |
75638074 | 960 | $count, $relationshipId, 'a_b', $params |
6a488035 TO |
961 | ); |
962 | list($select2, $from2, $where2) = self::makeURLClause($contactId, $status, $numRelationship, | |
75638074 | 963 | $count, $relationshipId, 'b_a', $params |
6a488035 TO |
964 | ); |
965 | ||
966 | $order = $limit = ''; | |
967 | if (!$count) { | |
968 | $order = ' ORDER BY civicrm_relationship_type_id, sort_name '; | |
969 | ||
970 | if ($numRelationship) { | |
971 | $limit = " LIMIT 0, $numRelationship"; | |
972 | } | |
973 | } | |
974 | ||
975 | // building the query string | |
976 | $queryString = ''; | |
977 | $queryString = $select1 . $from1 . $where1 . $select2 . $from2 . $where2 . $order . $limit; | |
978 | ||
979 | $relationship = new CRM_Contact_DAO_Relationship(); | |
980 | ||
981 | $relationship->query($queryString); | |
982 | $row = array(); | |
983 | if ($count) { | |
984 | $relationshipCount = 0; | |
985 | while ($relationship->fetch()) { | |
986 | $relationshipCount += $relationship->cnt1 + $relationship->cnt2; | |
987 | } | |
988 | return $relationshipCount; | |
989 | } | |
990 | else { | |
991 | ||
992 | $mask = NULL; | |
993 | if ($status != self::INACTIVE) { | |
994 | if ($links) { | |
995 | $mask = array_sum(array_keys($links)); | |
996 | if ($mask & CRM_Core_Action::DISABLE) { | |
997 | $mask -= CRM_Core_Action::DISABLE; | |
998 | } | |
999 | if ($mask & CRM_Core_Action::ENABLE) { | |
1000 | $mask -= CRM_Core_Action::ENABLE; | |
1001 | } | |
1002 | ||
1003 | if ($status == self::CURRENT) { | |
1004 | $mask |= CRM_Core_Action::DISABLE; | |
1005 | } | |
1006 | elseif ($status == self::DISABLED) { | |
1007 | $mask |= CRM_Core_Action::ENABLE; | |
1008 | } | |
1009 | $mask = $mask & $permissionMask; | |
1010 | } | |
1011 | } | |
1012 | while ($relationship->fetch()) { | |
1013 | $rid = $relationship->civicrm_relationship_id; | |
1014 | $cid = $relationship->civicrm_contact_id; | |
1015 | if (($permissionedContact) && | |
1016 | (!CRM_Contact_BAO_Contact_Permission::relationship($cid, $contactId)) | |
1017 | ) { | |
1018 | continue; | |
1019 | } | |
1020 | $values[$rid]['id'] = $rid; | |
1021 | $values[$rid]['cid'] = $cid; | |
1022 | $values[$rid]['contact_id_a'] = $relationship->contact_id_a; | |
1023 | $values[$rid]['contact_id_b'] = $relationship->contact_id_b; | |
1024 | $values[$rid]['relationship_type_id'] = $relationship->civicrm_relationship_type_id; | |
1025 | $values[$rid]['relation'] = $relationship->relation; | |
1026 | $values[$rid]['name'] = $relationship->sort_name; | |
1027 | $values[$rid]['display_name'] = $relationship->display_name; | |
1028 | $values[$rid]['job_title'] = $relationship->job_title; | |
1029 | $values[$rid]['email'] = $relationship->email; | |
1030 | $values[$rid]['phone'] = $relationship->phone; | |
1031 | $values[$rid]['employer_id'] = $relationship->employer_id; | |
1032 | $values[$rid]['organization_name'] = $relationship->organization_name; | |
1033 | $values[$rid]['country'] = $relationship->country; | |
1034 | $values[$rid]['city'] = $relationship->city; | |
1035 | $values[$rid]['state'] = $relationship->state; | |
1036 | $values[$rid]['start_date'] = $relationship->start_date; | |
1037 | $values[$rid]['end_date'] = $relationship->end_date; | |
1038 | $values[$rid]['description'] = $relationship->description; | |
1039 | $values[$rid]['is_active'] = $relationship->is_active; | |
1040 | $values[$rid]['is_permission_a_b'] = $relationship->is_permission_a_b; | |
1041 | $values[$rid]['is_permission_b_a'] = $relationship->is_permission_b_a; | |
1042 | $values[$rid]['case_id'] = $relationship->case_id; | |
1043 | ||
1044 | if ($status) { | |
1045 | $values[$rid]['status'] = $status; | |
1046 | } | |
1047 | ||
1048 | $values[$rid]['civicrm_relationship_type_id'] = $relationship->civicrm_relationship_type_id; | |
1049 | ||
1050 | if ($relationship->contact_id_a == $contactId) { | |
1051 | $values[$rid]['rtype'] = 'a_b'; | |
1052 | } | |
1053 | else { | |
1054 | $values[$rid]['rtype'] = 'b_a'; | |
1055 | } | |
1056 | ||
1057 | if ($links) { | |
1058 | $replace = array( | |
1059 | 'id' => $rid, | |
1060 | 'rtype' => $values[$rid]['rtype'], | |
1061 | 'cid' => $contactId, | |
1062 | 'cbid' => $values[$rid]['cid'], | |
1063 | 'caseid' => $values[$rid]['case_id'], | |
1064 | 'clientid' => $contactId, | |
1065 | ); | |
1066 | ||
1067 | if ($status == self::INACTIVE) { | |
1068 | // setting links for inactive relationships | |
1069 | $mask = array_sum(array_keys($links)); | |
1070 | if (!$values[$rid]['is_active']) { | |
1071 | $mask -= CRM_Core_Action::DISABLE; | |
1072 | } | |
1073 | else { | |
1074 | $mask -= CRM_Core_Action::ENABLE; | |
1075 | $mask -= CRM_Core_Action::DISABLE; | |
1076 | } | |
1077 | } | |
1078 | ||
1079 | // Give access to manage case link by copying to MAX_ACTION index temporarily, depending on case permission of user. | |
1080 | if ($values[$rid]['case_id']) { | |
1081 | // Borrowed logic from CRM_Case_Page_Tab | |
1082 | $hasCaseAccess = FALSE; | |
1083 | if (CRM_Core_Permission::check('access all cases and activities')) { | |
1084 | $hasCaseAccess = TRUE; | |
1085 | } | |
1086 | else { | |
1087 | $userCases = CRM_Case_BAO_Case::getCases(FALSE); | |
1088 | if (array_key_exists($values[$rid]['case_id'], $userCases)) { | |
1089 | $hasCaseAccess = TRUE; | |
1090 | } | |
1091 | } | |
1092 | ||
1093 | if ($hasCaseAccess) { | |
1094 | // give access by copying to MAX_ACTION temporarily, otherwise leave at NONE which won't display | |
1095 | $links[CRM_Core_Action::MAX_ACTION] = $links[CRM_Core_Action::NONE]; | |
1096 | $links[CRM_Core_Action::MAX_ACTION]['name'] = ts('Manage Case #%1', array(1 => $values[$rid]['case_id'])); | |
1097 | ||
1098 | // Also make sure we have the right client cid since can get here from multiple relationship tabs. | |
1099 | if ($values[$rid]['rtype'] == 'b_a') { | |
1100 | $replace['clientid'] = $values[$rid]['cid']; | |
1101 | } | |
1102 | } | |
1103 | } | |
1104 | ||
1105 | $values[$rid]['action'] = CRM_Core_Action::formLink($links, $mask, $replace); | |
1106 | unset($links[CRM_Core_Action::MAX_ACTION]); | |
1107 | } | |
1108 | } | |
1109 | ||
1110 | $relationship->free(); | |
1111 | return $values; | |
1112 | } | |
1113 | } | |
1114 | ||
1115 | /** | |
1116 | * Function to get get list of relationship type based on the target contact type. | |
1117 | * | |
1118 | * @param string $targetContactType it's valid contact tpye(may be Individual , Organization , Household) | |
1119 | * | |
1120 | * @return array - array reference of all relationship types with context to current contact type . | |
1121 | * | |
1122 | */ | |
1123 | function getRelationType($targetContactType) { | |
1124 | $relationshipType = array(); | |
1125 | $allRelationshipType = CRM_Core_PseudoConstant::relationshipType(); | |
1126 | ||
1127 | foreach ($allRelationshipType as $key => $type) { | |
1128 | if ($type['contact_type_b'] == $targetContactType) { | |
1129 | $relationshipType[$key . '_a_b'] = $type['label_a_b']; | |
1130 | } | |
1131 | } | |
1132 | ||
1133 | return $relationshipType; | |
1134 | } | |
1135 | ||
1136 | /** | |
1137 | * Function to create / update / delete membership for related contacts. | |
1138 | * | |
1139 | * This function will create/update/delete membership for related | |
1140 | * contact based on 1) contact have active membership 2) that | |
1141 | * membership is is extedned by the same relationship type to that | |
1142 | * of the existing relationship. | |
1143 | * | |
1144 | * @param $contactId Int contact id | |
1145 | * @param $params array array of values submitted by POST | |
1146 | * @param $ids array array of ids | |
1147 | * @param $action which action called this function | |
1148 | * | |
1149 | * @static | |
1150 | * | |
1151 | */ | |
1152 | static function relatedMemberships($contactId, &$params, $ids, $action = CRM_Core_Action::ADD, $active = TRUE) { | |
1153 | // Check the end date and set the status of the relationship | |
1154 | // accrodingly. | |
1155 | $status = self::CURRENT; | |
1156 | ||
1157 | if (!empty($params['end_date'])) { | |
1158 | $endDate = CRM_Utils_Date::setDateDefaults(CRM_Utils_Date::format($params['end_date']), NULL, 'Ymd'); | |
1159 | $today = date('Ymd'); | |
1160 | ||
1161 | if ($today > $endDate) { | |
1162 | $status = self::PAST; | |
1163 | } | |
1164 | } | |
1165 | ||
1166 | if (($action & CRM_Core_Action::ADD) && | |
1167 | ($status & self::PAST) | |
1168 | ) { | |
1169 | // if relationship is PAST and action is ADD, no qustion | |
1170 | // of creating RELATED membership and return back to | |
1171 | // calling method | |
1172 | return; | |
1173 | } | |
1174 | ||
1175 | $rel = explode('_', $params['relationship_type_id']); | |
1176 | ||
1177 | $relTypeId = $rel[0]; | |
1178 | $relDirection = "_{$rel[1]}_{$rel[2]}"; | |
1179 | $targetContact = array(); | |
1180 | if (($action & CRM_Core_Action::ADD) || | |
1181 | ($action & CRM_Core_Action::DELETE) | |
1182 | ) { | |
1183 | $contact = $contactId; | |
1184 | $targetContact = CRM_Utils_Array::value('contact_check', $params); | |
1185 | } | |
1186 | elseif ($action & CRM_Core_Action::UPDATE) { | |
1187 | $contact = $ids['contact']; | |
1188 | $targetContact = array($ids['contactTarget'] => 1); | |
1189 | } | |
1190 | ||
1191 | // Build the 'values' array for | |
1192 | // 1. ContactA | |
1193 | // 2. ContactB | |
1194 | // This will allow us to check if either of the contacts in | |
1195 | // relationship have active memberships. | |
1196 | ||
1197 | $values = array(); | |
1198 | ||
1199 | // 1. ContactA | |
1200 | $values[$contact] = array( | |
1201 | 'relatedContacts' => $targetContact, | |
1202 | 'relationshipTypeId' => $relTypeId, | |
1203 | 'relationshipTypeDirection' => $relDirection, | |
1204 | ); | |
1205 | // 2. ContactB | |
1206 | if (!empty($targetContact)) { | |
1207 | foreach ($targetContact as $cid => $donCare) { | |
1208 | $values[$cid] = array( | |
1209 | 'relatedContacts' => array($contact => 1), | |
1210 | 'relationshipTypeId' => $relTypeId, | |
1211 | ); | |
1212 | ||
1213 | $relTypeParams = array('id' => $relTypeId); | |
1214 | $relTypeValues = array(); | |
1215 | CRM_Contact_BAO_RelationshipType::retrieve($relTypeParams, $relTypeValues); | |
1216 | ||
1217 | if (CRM_Utils_Array::value('name_a_b', $relTypeValues) == CRM_Utils_Array::value('name_b_a', $relTypeValues)) { | |
1218 | $values[$cid]['relationshipTypeDirection'] = '_a_b'; | |
1219 | } | |
1220 | else { | |
1221 | $values[$cid]['relationshipTypeDirection'] = ($relDirection == '_a_b') ? '_b_a' : '_a_b'; | |
1222 | } | |
1223 | } | |
1224 | } | |
1225 | ||
1226 | // Now get the active memberships for all the contacts. | |
1227 | // If contact have any valid membership(s), then add it to | |
1228 | // 'values' array. | |
1229 | foreach ($values as $cid => $subValues) { | |
1230 | $memParams = array('contact_id' => $cid); | |
1231 | $memberships = array(); | |
1232 | ||
1233 | CRM_Member_BAO_Membership::getValues($memParams, $memberships, $active); | |
1234 | ||
1235 | if (empty($memberships)) { | |
1236 | continue; | |
1237 | } | |
1238 | ||
1239 | $values[$cid]['memberships'] = $memberships; | |
1240 | } | |
1241 | $deceasedStatusId = array_search('Deceased', CRM_Member_PseudoConstant::membershipStatus()); | |
1242 | ||
1243 | // done with 'values' array. | |
1244 | // Finally add / edit / delete memberships for the related contacts | |
1245 | foreach ($values as $cid => $details) { | |
1246 | if (!array_key_exists('memberships', $details)) { | |
1247 | continue; | |
1248 | } | |
1249 | ||
1250 | $mainRelatedContactId = key(CRM_Utils_Array::value('relatedContacts', $details, array())); | |
1251 | ||
1252 | foreach ($details['memberships'] as $membershipId => $membershipValues) { | |
1253 | $relTypeIds = array(); | |
1254 | if ($action & CRM_Core_Action::DELETE) { | |
1255 | // Delete memberships of the related contacts only if relationship type exists for membership type | |
1256 | $query = " | |
1257 | SELECT relationship_type_id, relationship_direction | |
1258 | FROM civicrm_membership_type | |
1259 | WHERE id = {$membershipValues['membership_type_id']}"; | |
1260 | $dao = CRM_Core_DAO::executeQuery($query); | |
1261 | $relTypeDirs = array(); | |
1262 | while ($dao->fetch()) { | |
1263 | $relTypeId = $dao->relationship_type_id; | |
1264 | $relDirection = $dao->relationship_direction; | |
1265 | } | |
1266 | $relTypeIds = explode(CRM_Core_DAO::VALUE_SEPARATOR, $relTypeId); | |
1267 | if (in_array($values[$cid]['relationshipTypeId'], $relTypeIds)) { | |
1268 | CRM_Member_BAO_Membership::deleteRelatedMemberships($membershipId, $mainRelatedContactId); | |
1269 | } | |
1270 | continue; | |
1271 | } | |
1272 | if (($action & CRM_Core_Action::UPDATE) && | |
1273 | ($status & self::PAST) && | |
1274 | ($membershipValues['owner_membership_id']) | |
1275 | ) { | |
1276 | // If relationship is PAST and action is UPDATE | |
1277 | // then delete the RELATED membership | |
1278 | CRM_Member_BAO_Membership::deleteRelatedMemberships($membershipValues['owner_membership_id'], | |
1279 | $membershipValues['membership_contact_id'] | |
1280 | ); | |
1281 | continue; | |
1282 | } | |
1283 | ||
1284 | // add / edit the memberships for related | |
1285 | // contacts. | |
1286 | ||
1287 | // Get the Membership Type Details. | |
1288 | $membershipType = CRM_Member_BAO_MembershipType::getMembershipTypeDetails($membershipValues['membership_type_id']); | |
1289 | // Check if contact's relationship type exists in membership type | |
1290 | $relTypeDirs = array(); | |
1291 | if (CRM_Utils_Array::value('relationship_type_id', $membershipType)) { | |
1292 | $relTypeIds = explode(CRM_Core_DAO::VALUE_SEPARATOR, $membershipType['relationship_type_id']); | |
1293 | } | |
1294 | if (CRM_Utils_Array::value('relationship_direction', $membershipType)) { | |
1295 | $relDirections = explode(CRM_Core_DAO::VALUE_SEPARATOR, $membershipType['relationship_direction']); | |
1296 | } | |
1297 | foreach ($relTypeIds as $key => $value) { | |
1298 | $relTypeDirs[] = $value . '_' . $relDirections[$key]; | |
1299 | } | |
1300 | $relTypeDir = $details['relationshipTypeId'] . $details['relationshipTypeDirection']; | |
1301 | if (in_array($relTypeDir, $relTypeDirs)) { | |
1302 | // Check if relationship being created/updated is | |
1303 | // similar to that of membership type's | |
1304 | // relationship. | |
1305 | ||
1306 | $membershipValues['owner_membership_id'] = $membershipId; | |
1307 | unset($membershipValues['id']); | |
1308 | unset($membershipValues['membership_contact_id']); | |
1309 | unset($membershipValues['contact_id']); | |
1310 | unset($membershipValues['membership_id']); | |
1311 | foreach ($details['relatedContacts'] as $relatedContactId => $donCare) { | |
1312 | $membershipValues['contact_id'] = $relatedContactId; | |
1313 | if ($deceasedStatusId && | |
1314 | CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $relatedContactId, 'is_deceased') | |
1315 | ) { | |
1316 | $membershipValues['status_id'] = $deceasedStatusId; | |
1317 | $membershipValues['skipStatusCal'] = TRUE; | |
1318 | } | |
1319 | foreach (array( | |
1320 | 'join_date', 'start_date', 'end_date') as $dateField) { | |
1321 | if (CRM_Utils_Array::value($dateField, $membershipValues)) { | |
1322 | $membershipValues[$dateField] = CRM_Utils_Date::processDate($membershipValues[$dateField]); | |
1323 | } | |
1324 | } | |
1325 | ||
1326 | if ($action & CRM_Core_Action::UPDATE) { | |
1327 | //delete the membership record for related | |
1328 | //contact before creating new membership record. | |
1329 | CRM_Member_BAO_Membership::deleteRelatedMemberships($membershipId, $relatedContactId); | |
1330 | } | |
1331 | ||
1332 | // check whether we have some related memberships still available | |
1333 | $query = " | |
1334 | SELECT count(*) | |
1335 | FROM civicrm_membership | |
1336 | LEFT JOIN civicrm_membership_status ON (civicrm_membership_status.id = civicrm_membership.status_id) | |
1337 | WHERE membership_type_id = {$membershipValues['membership_type_id']} AND owner_membership_id = {$membershipValues['owner_membership_id']} | |
1338 | AND is_current_member = 1"; | |
1339 | $result = CRM_Core_DAO::singleValueQuery($query); | |
1340 | if ($result < CRM_Utils_Array::value('max_related', $membershipValues, PHP_INT_MAX)) { | |
43291913 | 1341 | CRM_Member_BAO_Membership::create($membershipValues, CRM_Core_DAO::$_nullArray); |
6a488035 TO |
1342 | } |
1343 | } | |
1344 | } | |
1345 | elseif ($action & CRM_Core_Action::UPDATE) { | |
1346 | // if action is update and updated relationship do | |
1347 | // not match with the existing | |
1348 | // membership=>relationship then we need to | |
1349 | // delete the membership record created for | |
1350 | // previous relationship. | |
1351 | ||
1352 | if (self::isDeleteRelatedMembership($relTypeIds, $contactId, $mainRelatedContactId, $relTypeId, CRM_Utils_Array::value('relationship_ids', $params))) { | |
1353 | CRM_Member_BAO_Membership::deleteRelatedMemberships($membershipId, $mainRelatedContactId); | |
1354 | } | |
1355 | } | |
1356 | } | |
1357 | } | |
1358 | } | |
1359 | ||
1360 | /** | |
1361 | * Helper function to check whether to delete the membership or | |
1362 | * not. | |
1363 | * | |
e9dfde0c DG |
1364 | * @static |
1365 | * | |
6a488035 | 1366 | */ |
e9dfde0c | 1367 | static function isDeleteRelatedMembership($relTypeIds, $contactId, $mainRelatedContactId, $relTypeId, $relIds) { |
6a488035 TO |
1368 | if (in_array($relTypeId, $relTypeIds)) { |
1369 | return TRUE; | |
1370 | } | |
1371 | ||
1372 | if (empty($relIds)) { | |
1373 | return FALSE; | |
1374 | } | |
1375 | ||
1376 | $relParamas = array(1 => array($contactId, 'Integer'), | |
1377 | 2 => array($mainRelatedContactId, 'Integer'), | |
1378 | ); | |
1379 | ||
1380 | if ($contactId == $mainRelatedContactId) { | |
1381 | $recordsFound = (int)CRM_Core_DAO::singleValueQuery("SELECT COUNT(*) FROM civicrm_relationship WHERE relationship_type_id IN ( " . implode(',', $relTypeIds) . " ) AND contact_id_a IN ( %1 ) OR contact_id_b IN ( %1 ) AND id IN (" . implode(',', $relIds) . ")", $relParamas); | |
1382 | if ($recordsFound) { | |
1383 | return FALSE; | |
1384 | } | |
1385 | return TRUE; | |
1386 | } | |
1387 | ||
1388 | $recordsFound = (int)CRM_Core_DAO::singleValueQuery("SELECT COUNT(*) FROM civicrm_relationship WHERE relationship_type_id IN ( " . implode(',', $relTypeIds) . " ) AND contact_id_a IN ( %1, %2 ) AND contact_id_b IN ( %1, %2 ) AND id NOT IN (" . implode(',', $relIds) . ")", $relParamas); | |
1389 | ||
1390 | if ($recordsFound) { | |
1391 | return FALSE; | |
1392 | } | |
1393 | ||
1394 | return TRUE; | |
1395 | } | |
1396 | ||
1397 | /** | |
1398 | * Function to get Current Employer for Contact | |
1399 | * | |
1400 | * @param $contactIds Contact Ids | |
1401 | * | |
1402 | * @return $currentEmployer array of the current employer | |
1403 | * | |
1404 | * @static | |
1405 | * | |
1406 | */ | |
1407 | static function getCurrentEmployer($contactIds) { | |
1408 | $contacts = implode(',', $contactIds); | |
1409 | ||
1410 | $query = " | |
1411 | SELECT organization_name, id, employer_id | |
1412 | FROM civicrm_contact | |
1413 | WHERE id IN ( {$contacts} ) | |
1414 | "; | |
1415 | ||
1416 | $dao = CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray); | |
1417 | $currentEmployer = array(); | |
1418 | while ($dao->fetch()) { | |
1419 | $currentEmployer[$dao->id]['org_id'] = $dao->employer_id; | |
1420 | $currentEmployer[$dao->id]['org_name'] = $dao->organization_name; | |
1421 | } | |
1422 | ||
1423 | return $currentEmployer; | |
1424 | } | |
1425 | ||
1426 | /** | |
1427 | * Function to return list of permissioned employer for a given contact. | |
1428 | * | |
1429 | * @param $contactID int contact id whose employers | |
1430 | * are to be found. | |
1431 | * @param $name string employers sort name | |
1432 | * | |
1433 | * @static | |
1434 | * | |
1435 | * @return array array of employers. | |
1436 | * | |
1437 | */ | |
51e89def | 1438 | static function getPermissionedEmployer($contactID, $name = NULL) { |
6a488035 TO |
1439 | //get the relationship id |
1440 | $relTypeId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', | |
1441 | 'Employee of', 'id', 'name_a_b' | |
1442 | ); | |
1443 | ||
51e89def DS |
1444 | return self::getPermissionedContacts($contactID, $relTypeId, $name); |
1445 | } | |
1446 | ||
1447 | ||
1448 | /** | |
1449 | * Function to return list of permissioned contacts for a given contact and relationship type | |
1450 | * | |
1451 | * @param $contactID int contact id whose permissioned contacts are to be found. | |
1452 | * @param $relTypeId relationship type id | |
1453 | * @param $name string | |
1454 | * | |
1455 | * @static | |
1456 | * | |
1457 | * @return array of contacts | |
1458 | */ | |
1459 | static function getPermissionedContacts($contactID, $relTypeId, $name = NULL) { | |
1460 | $contacts = array(); | |
1461 | ||
6a488035 TO |
1462 | if ($relTypeId) { |
1463 | $query = " | |
1464 | SELECT cc.id as id, cc.sort_name as name | |
1465 | FROM civicrm_relationship cr, civicrm_contact cc | |
49f8272d | 1466 | WHERE |
51e89def DS |
1467 | cr.contact_id_a = %1 AND |
1468 | cr.relationship_type_id = %2 AND | |
1469 | cr.is_permission_a_b = 1 AND | |
6a488035 TO |
1470 | IF(cr.end_date IS NULL, 1, (DATEDIFF( CURDATE( ), cr.end_date ) <= 0)) AND |
1471 | cr.is_active = 1 AND | |
51e89def DS |
1472 | cc.id = cr.contact_id_b"; |
1473 | ||
1474 | if (!empty($name)) { | |
1475 | $name = CRM_Utils_Type::escape($name, 'String'); | |
1476 | $query .= " | |
49f8272d | 1477 | AND cc.sort_name LIKE '%$name%'"; |
51e89def | 1478 | } |
6a488035 | 1479 | |
51e89def DS |
1480 | $args = array(1 => array($contactID, 'Integer'), 2 => array($relTypeId, 'Integer')); |
1481 | $dao = CRM_Core_DAO::executeQuery($query, $args); | |
6a488035 TO |
1482 | |
1483 | while ($dao->fetch()) { | |
51e89def | 1484 | $contacts[$dao->id] = array( |
6a488035 TO |
1485 | 'name' => $dao->name, |
1486 | 'value' => $dao->id, | |
1487 | ); | |
1488 | } | |
1489 | } | |
51e89def | 1490 | return $contacts; |
6a488035 TO |
1491 | } |
1492 | ||
1493 | static function getValidContactTypeList($relType) { | |
1494 | // string looks like 4_a_b | |
1495 | $rel_parts = explode('_', $relType); | |
1496 | $allRelationshipType = CRM_Core_PseudoConstant::relationshipType('label'); | |
1497 | $contactProfiles = CRM_Core_BAO_UFGroup::getReservedProfiles('Contact', NULL); | |
1498 | ||
1499 | if ($rel_parts[1] == 'a') { | |
1500 | $leftType = $allRelationshipType[$rel_parts[0]]['contact_type_b']; | |
1501 | } | |
1502 | else { | |
1503 | $leftType = $allRelationshipType[$rel_parts[0]]['contact_type_a']; | |
1504 | } | |
1505 | ||
1506 | // Handle 'All Contacts' contact type for left side of relationship ($leftType is empty in this case) | |
1507 | // In this case all reserved profiles are available | |
1508 | if ($leftType == '') { | |
1509 | $contactTypes = $contactProfiles; | |
1510 | } else { | |
1511 | $contactTypes = array(); | |
1512 | foreach ($contactProfiles as $key => $value) { | |
c6fd2111 PJ |
1513 | $groupTypes = CRM_Core_BAO_UFGroup::profileGroups($key); |
1514 | if (in_array($leftType, $groupTypes)) { | |
6a488035 TO |
1515 | $contactTypes = array($key => $value); |
1516 | } | |
1517 | } | |
1518 | } | |
1519 | ||
1520 | return $contactTypes; | |
1521 | } | |
1522 | ||
1523 | /** | |
1524 | * Merge relationships from otherContact to mainContact | |
1525 | * Called during contact merge operation | |
1526 | * | |
1527 | * @param int $mainId contact id of main contact record. | |
1528 | * @param int $otherId contact id of record which is going to merge. | |
1529 | * @param array $sqls (reference) array of sql statements to append to. | |
1530 | * | |
1531 | * @see CRM_Dedupe_Merger::cpTables() | |
1532 | * | |
1533 | * @static | |
1534 | */ | |
1535 | static function mergeRelationships($mainId, $otherId, &$sqls) { | |
1536 | // Delete circular relationships | |
1537 | $sqls[] = "DELETE FROM civicrm_relationship | |
1538 | WHERE (contact_id_a = $mainId AND contact_id_b = $otherId) | |
1539 | OR (contact_id_b = $mainId AND contact_id_a = $otherId)"; | |
1540 | ||
1541 | // Delete relationship from other contact if main contact already has that relationship | |
1542 | $sqls[] = "DELETE r2 | |
1543 | FROM civicrm_relationship r1, civicrm_relationship r2 | |
1544 | WHERE r1.relationship_type_id = r2.relationship_type_id | |
1545 | AND r1.id <> r2.id | |
1546 | AND ( | |
1547 | r1.contact_id_a = $mainId AND r2.contact_id_a = $otherId AND r1.contact_id_b = r2.contact_id_b | |
1548 | OR r1.contact_id_b = $mainId AND r2.contact_id_b = $otherId AND r1.contact_id_a = r2.contact_id_a | |
1549 | OR ( | |
1550 | (r1.contact_id_a = $mainId AND r2.contact_id_b = $otherId AND r1.contact_id_b = r2.contact_id_a | |
1551 | OR r1.contact_id_b = $mainId AND r2.contact_id_a = $otherId AND r1.contact_id_a = r2.contact_id_b) | |
1552 | AND r1.relationship_type_id IN (SELECT id FROM civicrm_relationship_type WHERE name_b_a = name_a_b) | |
1553 | ) | |
1554 | )"; | |
1555 | ||
1556 | // Move relationships | |
1557 | $sqls[] = "UPDATE IGNORE civicrm_relationship SET contact_id_a = $mainId WHERE contact_id_a = $otherId"; | |
1558 | $sqls[] = "UPDATE IGNORE civicrm_relationship SET contact_id_b = $mainId WHERE contact_id_b = $otherId"; | |
1559 | ||
1560 | // Move current employer id (name will get updated later) | |
1561 | $sqls[] = "UPDATE civicrm_contact SET employer_id = $mainId WHERE employer_id = $otherId"; | |
1562 | } | |
1563 | ||
1564 | /** | |
1565 | * Set 'is_valid' field to false for all relationships whose end date is in the past, ie. are expired. | |
1566 | * | |
1567 | * @return True on success, false if error is encountered. | |
1568 | */ | |
1569 | static function disableExpiredRelationships() { | |
1570 | $query = "SELECT id FROM civicrm_relationship WHERE is_active = 1 AND end_date < CURDATE()"; | |
1571 | ||
1572 | $dao = CRM_Core_DAO::executeQuery($query); | |
1573 | while ($dao->fetch()) { | |
1574 | $result = CRM_Contact_BAO_Relationship::setIsActive($dao->id, FALSE); | |
1575 | // Result will be NULL if error occurred. We abort early if error detected. | |
1576 | if ($result == NULL) { | |
1577 | return FALSE; | |
1578 | } | |
1579 | } | |
1580 | return TRUE; | |
1581 | } | |
c9c41397 | 1582 | |
1583 | /** | |
1584 | * Function filters the query by possible relationships for the membership type | |
1585 | * It is intended to be called when constructing queries for the api (reciprocal & non-reciprocal) | |
1586 | * and to add clauses to limit the return to those relationships which COULD inherit a membership type | |
1587 | * (as opposed to those who inherit a particular membership | |
1588 | * | |
1589 | * @param array $params api input array | |
1590 | */ | |
1591 | static function membershipTypeToRelationshipTypes(&$params, $direction = NULL) { | |
1592 | $membershipType = civicrm_api3('membership_type', 'getsingle', array('id' => $params['membership_type_id'], 'return' => 'relationship_type_id, relationship_direction')); | |
1593 | $relationshipTypes = $membershipType['relationship_type_id']; | |
f201289d | 1594 | if(empty($relationshipTypes)) { |
1595 | return; | |
1596 | } | |
c9c41397 | 1597 | // if we don't have any contact data we can only filter on type |
1598 | if(empty($params['contact_id']) && empty($params['contact_id_a']) && empty($params['contact_id_a'])) { | |
1599 | $params['relationship_type_id'] = array('IN' => $relationshipTypes); | |
1600 | return; | |
1601 | } | |
1602 | else { | |
1a9b2ee8 | 1603 | $relationshipDirections = (array) $membershipType['relationship_direction']; |
c9c41397 | 1604 | // if we have contact_id_a OR contact_id_b we can make a call here |
1605 | // if we have contact?? | |
1606 | foreach ($relationshipDirections as $index => $mtdirection) { | |
1607 | if(isset($params['contact_id_a']) && $mtdirection == 'a_b' || $direction == 'a_b') { | |
1608 | $types[] = $relationshipTypes[$index]; | |
1609 | } | |
1610 | if(isset($params['contact_id_b']) && $mtdirection == 'b_a' || $direction == 'b_a') { | |
1611 | $types[] = $relationshipTypes[$index]; | |
1612 | } | |
1613 | } | |
1614 | if(!empty($types)) { | |
1615 | $params['relationship_type_id'] = array('IN' => $types); | |
1616 | } | |
1617 | elseif(!empty($clauses)) { | |
1618 | return explode(' OR ', $clauses); | |
1619 | } | |
1620 | else{ | |
1621 | // effectively setting it to return no results | |
1622 | $params['relationship_type_id'] = 0; | |
1623 | } | |
1624 | } | |
1625 | } | |
6a488035 TO |
1626 | } |
1627 |