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