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