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