Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
62e492d4 | 29 | * This class generates form components for relationship. |
6a488035 TO |
30 | */ |
31 | class CRM_Contact_Form_Relationship extends CRM_Core_Form { | |
32 | ||
6a488035 TO |
33 | /** |
34 | * The relationship id, used when editing the relationship | |
35 | * | |
36 | * @var int | |
37 | */ | |
d97a2fc1 | 38 | public $_relationshipId; |
6a488035 TO |
39 | |
40 | /** | |
41 | * The contact id, used when add/edit relationship | |
42 | * | |
43 | * @var int | |
44 | */ | |
d97a2fc1 | 45 | public $_contactId; |
6a488035 TO |
46 | |
47 | /** | |
48 | * This is a string which is either a_b or b_a used to determine the relationship between to contacts | |
69078420 | 49 | * @var string |
6a488035 | 50 | */ |
d97a2fc1 | 51 | public $_rtype; |
6a488035 TO |
52 | |
53 | /** | |
54 | * This is a string which is used to determine the relationship between to contacts | |
69078420 | 55 | * @var string |
6a488035 | 56 | */ |
d97a2fc1 | 57 | public $_rtypeId; |
6a488035 TO |
58 | |
59 | /** | |
60 | * Display name of contact a | |
69078420 | 61 | * @var string |
6a488035 | 62 | */ |
d97a2fc1 | 63 | public $_display_name_a; |
6a488035 TO |
64 | |
65 | /** | |
66 | * Display name of contact b | |
69078420 | 67 | * @var string |
6a488035 | 68 | */ |
d97a2fc1 | 69 | public $_display_name_b; |
6a488035 TO |
70 | |
71 | /** | |
72 | * The relationship type id | |
73 | * | |
74 | * @var int | |
75 | */ | |
d97a2fc1 | 76 | public $_relationshipTypeId; |
6a488035 TO |
77 | |
78 | /** | |
100fef9d | 79 | * An array of all relationship names |
6a488035 TO |
80 | * |
81 | * @var array | |
82 | */ | |
d97a2fc1 | 83 | public $_allRelationshipNames; |
6a488035 | 84 | |
239dac31 CW |
85 | /** |
86 | * @var bool | |
87 | */ | |
d97a2fc1 | 88 | public $_enabled; |
239dac31 CW |
89 | |
90 | /** | |
91 | * @var bool | |
92 | */ | |
d97a2fc1 | 93 | public $_isCurrentEmployer; |
239dac31 CW |
94 | |
95 | /** | |
96 | * @var string | |
97 | */ | |
d97a2fc1 | 98 | public $_contactType; |
239dac31 | 99 | |
6a488035 TO |
100 | /** |
101 | * The relationship values if Updating relationship | |
69078420 | 102 | * @var array |
6a488035 | 103 | */ |
d97a2fc1 | 104 | public $_values; |
6a488035 TO |
105 | |
106 | /** | |
62118c6d | 107 | * Case id if it called from case context |
69078420 | 108 | * @var int |
6a488035 | 109 | */ |
d97a2fc1 | 110 | public $_caseId; |
6a488035 | 111 | |
0efbca68 TM |
112 | /** |
113 | * Explicitly declare the form context. | |
114 | */ | |
115 | public function getDefaultContext() { | |
116 | return 'create'; | |
117 | } | |
118 | ||
119 | /** | |
120 | * Explicitly declare the entity api name. | |
121 | */ | |
122 | public function getDefaultEntity() { | |
123 | return 'Relationship'; | |
124 | } | |
125 | ||
00be9182 | 126 | public function preProcess() { |
6a488035 TO |
127 | $this->_contactId = $this->get('contactId'); |
128 | ||
239dac31 CW |
129 | $this->_contactType = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactId, 'contact_type'); |
130 | ||
6a488035 TO |
131 | $this->_relationshipId = $this->get('id'); |
132 | ||
133 | $this->_rtype = CRM_Utils_Request::retrieve('rtype', 'String', $this); | |
134 | ||
135 | $this->_rtypeId = CRM_Utils_Request::retrieve('relTypeId', 'String', $this); | |
136 | ||
137 | $this->_display_name_a = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactId, 'display_name'); | |
138 | ||
239dac31 | 139 | $this->assign('display_name_a', $this->_display_name_a); |
75b35151 | 140 | //get the relationship values. |
be2fb01f | 141 | $this->_values = []; |
75b35151 | 142 | if ($this->_relationshipId) { |
be2fb01f | 143 | $params = ['id' => $this->_relationshipId]; |
75b35151 | 144 | CRM_Core_DAO::commonRetrieve('CRM_Contact_DAO_Relationship', $params, $this->_values); |
145 | } | |
239dac31 | 146 | |
cd056f13 | 147 | // Check for permissions |
be2fb01f | 148 | if (in_array($this->_action, [CRM_Core_Action::ADD, CRM_Core_Action::UPDATE, CRM_Core_Action::DELETE])) { |
75b35151 | 149 | if (!CRM_Contact_BAO_Contact_Permission::allow($this->_contactId, CRM_Core_Permission::EDIT) |
150 | && !CRM_Contact_BAO_Contact_Permission::allow($this->_values['contact_id_b'], CRM_Core_Permission::EDIT)) { | |
cd056f13 KL |
151 | CRM_Core_Error::statusBounce(ts('You do not have the necessary permission to edit this contact.')); |
152 | } | |
153 | } | |
154 | ||
239dac31 CW |
155 | // Set page title based on action |
156 | switch ($this->_action) { | |
157 | case CRM_Core_Action::VIEW: | |
be2fb01f | 158 | CRM_Utils_System::setTitle(ts('View Relationship for %1', [1 => $this->_display_name_a])); |
239dac31 | 159 | break; |
ea100cb5 | 160 | |
239dac31 | 161 | case CRM_Core_Action::ADD: |
be2fb01f | 162 | CRM_Utils_System::setTitle(ts('Add Relationship for %1', [1 => $this->_display_name_a])); |
239dac31 | 163 | break; |
ea100cb5 | 164 | |
239dac31 | 165 | case CRM_Core_Action::UPDATE: |
be2fb01f | 166 | CRM_Utils_System::setTitle(ts('Edit Relationship for %1', [1 => $this->_display_name_a])); |
239dac31 | 167 | break; |
ea100cb5 | 168 | |
239dac31 | 169 | case CRM_Core_Action::DELETE: |
be2fb01f | 170 | CRM_Utils_System::setTitle(ts('Delete Relationship for %1', [1 => $this->_display_name_a])); |
239dac31 CW |
171 | break; |
172 | } | |
6a488035 TO |
173 | |
174 | $this->_caseId = CRM_Utils_Request::retrieve('caseID', 'Integer', $this); | |
175 | ||
6a488035 | 176 | if (!$this->_rtypeId) { |
26b82b94 | 177 | $params = CRM_Utils_Request::exportValues(); |
6a488035 TO |
178 | if (isset($params['relationship_type_id'])) { |
179 | $this->_rtypeId = $params['relationship_type_id']; | |
180 | } | |
181 | elseif (!empty($this->_values)) { | |
182 | $this->_rtypeId = $this->_values['relationship_type_id'] . '_' . $this->_rtype; | |
183 | } | |
184 | } | |
185 | ||
186 | //get the relationship type id | |
be2fb01f | 187 | $this->_relationshipTypeId = str_replace(['_a_b', '_b_a'], ['', ''], $this->_rtypeId); |
6a488035 TO |
188 | |
189 | //get the relationship type | |
190 | if (!$this->_rtype) { | |
191 | $this->_rtype = str_replace($this->_relationshipTypeId . '_', '', $this->_rtypeId); | |
192 | } | |
6a488035 | 193 | |
239dac31 CW |
194 | //need to assign custom data type and subtype to the template - FIXME: explain why |
195 | $this->assign('customDataType', 'Relationship'); | |
196 | $this->assign('customDataSubType', $this->_relationshipTypeId); | |
197 | $this->assign('entityID', $this->_relationshipId); | |
6a488035 TO |
198 | |
199 | //use name as it remain constant, CRM-3336 | |
200 | $this->_allRelationshipNames = CRM_Core_PseudoConstant::relationshipType('name'); | |
201 | ||
239dac31 CW |
202 | // Current employer? |
203 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
204 | if ($this->_allRelationshipNames[$this->_relationshipTypeId]["name_a_b"] == 'Employee of') { | |
205 | $this->_isCurrentEmployer = $this->_values['contact_id_b'] == CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_values['contact_id_a'], 'employer_id'); | |
206 | } | |
207 | } | |
208 | ||
6a488035 | 209 | // when custom data is included in this page |
a7488080 | 210 | if (!empty($_POST['hidden_custom'])) { |
da4b8382 | 211 | CRM_Custom_Form_CustomData::preProcess($this, NULL, $this->_relationshipTypeId, 1, 'Relationship', $this->_relationshipId); |
6a488035 TO |
212 | CRM_Custom_Form_CustomData::buildQuickForm($this); |
213 | CRM_Custom_Form_CustomData::setDefaultValues($this); | |
214 | } | |
215 | } | |
216 | ||
217 | /** | |
574d211d | 218 | * Set default values for the form. |
6a488035 | 219 | */ |
00be9182 | 220 | public function setDefaultValues() { |
be2fb01f | 221 | $defaults = []; |
6a488035 TO |
222 | |
223 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
224 | if (!empty($this->_values)) { | |
225 | $defaults['relationship_type_id'] = $this->_rtypeId; | |
425d6064 MWMC |
226 | $defaults['start_date'] = CRM_Utils_Array::value('start_date', $this->_values); |
227 | $defaults['end_date'] = CRM_Utils_Array::value('end_date', $this->_values); | |
6a488035 TO |
228 | $defaults['description'] = CRM_Utils_Array::value('description', $this->_values); |
229 | $defaults['is_active'] = CRM_Utils_Array::value('is_active', $this->_values); | |
239dac31 | 230 | |
2a798409 | 231 | // The postprocess function will swap these fields if it is a b_a relationship, so we compensate here |
239dac31 CW |
232 | $defaults['is_permission_a_b'] = CRM_Utils_Array::value('is_permission_' . $this->_rtype, $this->_values); |
233 | $defaults['is_permission_b_a'] = CRM_Utils_Array::value('is_permission_' . strrev($this->_rtype), $this->_values); | |
234 | ||
235 | $defaults['is_current_employer'] = $this->_isCurrentEmployer; | |
236 | ||
237 | // Load info about the related contact | |
6a488035 TO |
238 | $contact = new CRM_Contact_DAO_Contact(); |
239 | if ($this->_rtype == 'a_b' && $this->_values['contact_id_a'] == $this->_contactId) { | |
240 | $contact->id = $this->_values['contact_id_b']; | |
241 | } | |
242 | else { | |
243 | $contact->id = $this->_values['contact_id_a']; | |
244 | } | |
245 | if ($contact->find(TRUE)) { | |
239dac31 | 246 | $defaults['related_contact_id'] = $contact->id; |
6a488035 | 247 | $this->_display_name_b = $contact->display_name; |
239dac31 | 248 | $this->assign('display_name_b', $this->_display_name_b); |
6a488035 TO |
249 | } |
250 | ||
be2fb01f | 251 | $noteParams = [ |
239dac31 CW |
252 | 'entity_id' => $this->_relationshipId, |
253 | 'entity_table' => 'civicrm_relationship', | |
254 | 'limit' => 1, | |
21dfd5f5 | 255 | 'version' => 3, |
be2fb01f | 256 | ]; |
d3e86119 | 257 | $note = civicrm_api('Note', 'getsingle', $noteParams); |
239dac31 | 258 | $defaults['note'] = CRM_Utils_Array::value('note', $note); |
6a488035 TO |
259 | } |
260 | } | |
261 | else { | |
239dac31 | 262 | $defaults['is_active'] = $defaults['is_current_employer'] = 1; |
6a488035 | 263 | $defaults['relationship_type_id'] = $this->_rtypeId; |
f871c3a9 | 264 | $defaults['is_permission_a_b'] = $defaults['is_permission_b_a'] = CRM_Contact_BAO_Relationship::NONE; |
6a488035 TO |
265 | } |
266 | ||
267 | $this->_enabled = $defaults['is_active']; | |
268 | return $defaults; | |
269 | } | |
270 | ||
271 | /** | |
62e492d4 | 272 | * Add the rules for form. |
6a488035 | 273 | */ |
00be9182 | 274 | public function addRules() { |
6a488035 | 275 | if (!($this->_action & CRM_Core_Action::DELETE)) { |
be2fb01f | 276 | $this->addFormRule(['CRM_Contact_Form_Relationship', 'dateRule']); |
6a488035 TO |
277 | } |
278 | } | |
279 | ||
280 | /** | |
fe482240 | 281 | * Build the form object. |
6a488035 TO |
282 | */ |
283 | public function buildQuickForm() { | |
6a488035 | 284 | if ($this->_action & CRM_Core_Action::DELETE) { |
be2fb01f | 285 | $this->addButtons([ |
69078420 SL |
286 | [ |
287 | 'type' => 'next', | |
288 | 'name' => ts('Delete'), | |
289 | 'isDefault' => TRUE, | |
290 | ], | |
291 | [ | |
292 | 'type' => 'cancel', | |
293 | 'name' => ts('Cancel'), | |
294 | ], | |
295 | ]); | |
6a488035 TO |
296 | return; |
297 | } | |
239dac31 CW |
298 | |
299 | // Select list | |
300 | $relationshipList = CRM_Contact_BAO_Relationship::getContactRelationshipType($this->_contactId, $this->_rtype, $this->_relationshipId); | |
301 | ||
8e383c2f MD |
302 | $this->assign('contactTypes', CRM_Contact_BAO_ContactType::contactTypeInfo(TRUE)); |
303 | ||
22e263ad | 304 | foreach ($this->_allRelationshipNames as $id => $vals) { |
239dac31 CW |
305 | if ($vals['name_a_b'] === 'Employee of') { |
306 | $this->assign('employmentRelationship', $id); | |
0004ae05 | 307 | break; |
6a488035 | 308 | } |
6a488035 TO |
309 | } |
310 | ||
b3ee84c9 MD |
311 | $this->addField( |
312 | 'relationship_type_id', | |
be2fb01f CW |
313 | [ |
314 | 'options' => ['' => ts('- select -')] + $relationshipList, | |
b3ee84c9 MD |
315 | 'class' => 'huge', |
316 | 'placeholder' => '- select -', | |
317 | 'option_url' => 'civicrm/admin/reltype', | |
be2fb01f | 318 | 'option_context' => [ |
b3ee84c9 MD |
319 | 'contact_id' => $this->_contactId, |
320 | 'relationship_direction' => $this->_rtype, | |
321 | 'relationship_id' => $this->_relationshipId, | |
322 | 'is_form' => TRUE, | |
be2fb01f CW |
323 | ], |
324 | ], | |
b3ee84c9 MD |
325 | TRUE |
326 | ); | |
6a488035 | 327 | |
239dac31 | 328 | $label = $this->_action & CRM_Core_Action::ADD ? ts('Contact(s)') : ts('Contact'); |
be2fb01f | 329 | $contactField = $this->addField('related_contact_id', ['label' => $label, 'name' => 'contact_id_b', 'multiple' => TRUE, 'create' => TRUE], TRUE); |
239dac31 CW |
330 | // This field cannot be updated |
331 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
332 | $contactField->freeze(); | |
333 | } | |
334 | ||
71331d2a | 335 | $this->add('advcheckbox', 'is_current_employer', $this->_contactType == 'Organization' ? ts('Current Employee') : ts('Current Employer')); |
6a488035 | 336 | |
be2fb01f CW |
337 | $this->addField('start_date', ['label' => ts('Start Date')], FALSE, FALSE); |
338 | $this->addField('end_date', ['label' => ts('End Date')], FALSE, FALSE); | |
6a488035 | 339 | |
be2fb01f | 340 | $this->addField('is_active', ['label' => ts('Enabled?'), 'type' => 'advcheckbox']); |
239dac31 | 341 | |
be2fb01f CW |
342 | $this->addField('is_permission_a_b', [], TRUE); |
343 | $this->addField('is_permission_b_a', [], TRUE); | |
6a488035 | 344 | |
be2fb01f | 345 | $this->addField('description', ['label' => ts('Description')]); |
6a488035 TO |
346 | |
347 | CRM_Contact_Form_Edit_Notes::buildQuickForm($this); | |
348 | ||
239dac31 | 349 | if ($this->_action & CRM_Core_Action::VIEW) { |
be2fb01f CW |
350 | $this->addButtons([ |
351 | [ | |
239dac31 CW |
352 | 'type' => 'cancel', |
353 | 'name' => ts('Done'), | |
be2fb01f CW |
354 | ], |
355 | ]); | |
6a488035 TO |
356 | } |
357 | else { | |
239dac31 | 358 | // make this form an upload since we don't know if the custom data injected dynamically is of type file etc. |
be2fb01f CW |
359 | $this->addButtons([ |
360 | [ | |
239dac31 CW |
361 | 'type' => 'upload', |
362 | 'name' => ts('Save Relationship'), | |
363 | 'isDefault' => TRUE, | |
be2fb01f CW |
364 | ], |
365 | [ | |
6a488035 TO |
366 | 'type' => 'cancel', |
367 | 'name' => ts('Cancel'), | |
be2fb01f CW |
368 | ], |
369 | ]); | |
239dac31 | 370 | } |
6a488035 TO |
371 | } |
372 | ||
373 | /** | |
00d84e9b | 374 | * This function is called when the form is submitted and also from unit test. |
425d6064 | 375 | * |
00d84e9b JP |
376 | * @param array $params |
377 | * | |
378 | * @return array | |
425d6064 | 379 | * @throws \CRM_Core_Exception |
6a488035 | 380 | */ |
00d84e9b | 381 | public function submit($params) { |
f55fb370 | 382 | switch ($this->getAction()) { |
383 | case CRM_Core_Action::DELETE: | |
384 | $this->deleteAction($this->_relationshipId); | |
be2fb01f | 385 | return []; |
eff45dce | 386 | |
f55fb370 | 387 | case CRM_Core_Action::UPDATE: |
00d84e9b | 388 | return $this->updateAction($params); |
6a488035 | 389 | |
f55fb370 | 390 | default: |
00d84e9b | 391 | return $this->createAction($params); |
6a488035 | 392 | } |
00d84e9b JP |
393 | } |
394 | ||
395 | /** | |
396 | * This function is called when the form is submitted. | |
397 | */ | |
398 | public function postProcess() { | |
399 | // Store the submitted values in an array. | |
400 | $params = $this->controller->exportValues($this->_name); | |
401 | ||
402 | $values = $this->submit($params); | |
403 | if (empty($values)) { | |
404 | return; | |
405 | } | |
406 | list ($params, $relationshipIds) = $values; | |
6a488035 | 407 | |
6a488035 TO |
408 | // if this is called from case view, |
409 | //create an activity for case role removal.CRM-4480 | |
2da59b29 | 410 | // @todo this belongs in the BAO. |
6a488035 TO |
411 | if ($this->_caseId) { |
412 | CRM_Case_BAO_Case::createCaseRoleActivity($this->_caseId, $relationshipIds, $params['contact_check'], $this->_contactId); | |
413 | } | |
414 | ||
2da59b29 | 415 | // @todo this belongs in the BAO. |
45caf31c | 416 | $note = !empty($params['note']) ? $params['note'] : ''; |
417 | $this->saveRelationshipNotes($relationshipIds, $note); | |
6a488035 | 418 | |
7e98675f | 419 | // Refresh contact tabs which might have been affected |
be2fb01f CW |
420 | $this->ajaxResponse = [ |
421 | 'reloadBlocks' => ['#crm-contactinfo-content'], | |
422 | 'updateTabs' => [ | |
df0c42cc JP |
423 | '#tab_member' => CRM_Contact_BAO_Contact::getCountComponent('membership', $this->_contactId), |
424 | '#tab_contribute' => CRM_Contact_BAO_Contact::getCountComponent('contribution', $this->_contactId), | |
be2fb01f CW |
425 | ], |
426 | ]; | |
6a488035 TO |
427 | } |
428 | ||
429 | /** | |
fe482240 | 430 | * Date validation. |
6a488035 | 431 | * |
77c5b619 TO |
432 | * @param array $params |
433 | * (reference ) an assoc array of name/value pairs. | |
6a488035 | 434 | * |
72b3a70c CW |
435 | * @return bool|array |
436 | * mixed true or array of errors | |
6a488035 | 437 | */ |
00be9182 | 438 | public static function dateRule($params) { |
be2fb01f | 439 | $errors = []; |
6a488035 TO |
440 | |
441 | // check start and end date | |
8cc574cf | 442 | if (!empty($params['start_date']) && !empty($params['end_date'])) { |
425d6064 | 443 | if ($params['end_date'] < $params['start_date']) { |
6a488035 TO |
444 | $errors['end_date'] = ts('The relationship end date cannot be prior to the start date.'); |
445 | } | |
446 | } | |
447 | ||
448 | return empty($errors) ? TRUE : $errors; | |
449 | } | |
450 | ||
2da59b29 EM |
451 | /** |
452 | * Set Status message to reflect outcome of the update action. | |
453 | * | |
607fa308 EM |
454 | * @param array $outcome |
455 | * Outcome of save action - including | |
456 | * - 'valid' : Number of valid relationships attempted. | |
457 | * - 'invalid' : Number of invalid relationships attempted. | |
458 | * - 'duplicate' : Number of duplicate relationships attempted. | |
459 | * - 'saved' : boolean of whether save was successful | |
2da59b29 | 460 | */ |
607fa308 | 461 | protected function setMessage($outcome) { |
782bfb8a | 462 | if (!empty($outcome['valid']) && empty($outcome['saved'])) { |
be2fb01f | 463 | CRM_Core_Session::setStatus(ts('Relationship created.', [ |
607fa308 | 464 | 'count' => $outcome['valid'], |
2da59b29 | 465 | 'plural' => '%count relationships created.', |
be2fb01f | 466 | ]), ts('Saved'), 'success'); |
2da59b29 | 467 | } |
607fa308 | 468 | if (!empty($outcome['invalid'])) { |
be2fb01f | 469 | CRM_Core_Session::setStatus(ts('%count relationship record was not created due to an invalid contact type.', [ |
607fa308 | 470 | 'count' => $outcome['invalid'], |
2da59b29 | 471 | 'plural' => '%count relationship records were not created due to invalid contact types.', |
be2fb01f | 472 | ]), ts('%count invalid relationship record', [ |
607fa308 | 473 | 'count' => $outcome['invalid'], |
2da59b29 | 474 | 'plural' => '%count invalid relationship records', |
be2fb01f | 475 | ])); |
2da59b29 | 476 | } |
607fa308 | 477 | if (!empty($outcome['duplicate'])) { |
be2fb01f | 478 | CRM_Core_Session::setStatus(ts('One relationship was not created because it already exists.', [ |
607fa308 | 479 | 'count' => $outcome['duplicate'], |
2da59b29 | 480 | 'plural' => '%count relationships were not created because they already exist.', |
be2fb01f | 481 | ]), ts('%count duplicate relationship', [ |
607fa308 | 482 | 'count' => $outcome['duplicate'], |
2da59b29 | 483 | 'plural' => '%count duplicate relationships', |
be2fb01f | 484 | ])); |
2da59b29 | 485 | } |
607fa308 | 486 | if (!empty($outcome['saved'])) { |
2da59b29 EM |
487 | CRM_Core_Session::setStatus(ts('Relationship record has been updated.'), ts('Saved'), 'success'); |
488 | } | |
489 | } | |
490 | ||
0004ae05 CW |
491 | /** |
492 | * @param $relationshipList | |
425d6064 | 493 | * |
0004ae05 CW |
494 | * @return array |
495 | */ | |
496 | public static function getRelationshipTypeMetadata($relationshipList) { | |
497 | $contactTypes = CRM_Contact_BAO_ContactType::contactTypeInfo(TRUE); | |
498 | $allRelationshipNames = CRM_Core_PseudoConstant::relationshipType('name'); | |
be2fb01f | 499 | $jsData = []; |
0004ae05 | 500 | // Get just what we need to keep the dom small |
be2fb01f | 501 | $whatWeWant = array_flip([ |
0004ae05 CW |
502 | 'contact_type_a', |
503 | 'contact_type_b', | |
504 | 'contact_sub_type_a', | |
505 | 'contact_sub_type_b', | |
be2fb01f | 506 | ]); |
0004ae05 CW |
507 | foreach ($allRelationshipNames as $id => $vals) { |
508 | if (isset($relationshipList["{$id}_a_b"]) || isset($relationshipList["{$id}_b_a"])) { | |
509 | $jsData[$id] = array_filter(array_intersect_key($allRelationshipNames[$id], $whatWeWant)); | |
510 | // Add user-friendly placeholder | |
be2fb01f | 511 | foreach (['a', 'b'] as $x) { |
0004ae05 | 512 | $type = !empty($jsData[$id]["contact_sub_type_$x"]) ? $jsData[$id]["contact_sub_type_$x"] : CRM_Utils_Array::value("contact_type_$x", $jsData[$id]); |
be2fb01f | 513 | $jsData[$id]["placeholder_$x"] = $type ? ts('- select %1 -', [strtolower($contactTypes[$type]['label'])]) : ts('- select contact -'); |
0004ae05 CW |
514 | } |
515 | } | |
516 | } | |
517 | return $jsData; | |
518 | } | |
519 | ||
f55fb370 | 520 | /** |
521 | * Handling 'delete relationship' action | |
522 | * | |
523 | * @param int $id | |
524 | * Relationship ID | |
525 | */ | |
526 | private function deleteAction($id) { | |
527 | CRM_Contact_BAO_Relationship::del($id); | |
528 | ||
529 | // reload all blocks to reflect this change on the user interface. | |
be2fb01f | 530 | $this->ajaxResponse['reloadBlocks'] = ['#crm-contactinfo-content']; |
f55fb370 | 531 | } |
532 | ||
533 | /** | |
534 | * Handling updating relationship action | |
535 | * | |
536 | * @param array $params | |
537 | * | |
538 | * @return array | |
425d6064 | 539 | * @throws \CRM_Core_Exception |
f55fb370 | 540 | */ |
541 | private function updateAction($params) { | |
425d6064 | 542 | list($params, $_) = $this->preparePostProcessParameters($params); |
00d84e9b JP |
543 | try { |
544 | civicrm_api3('relationship', 'create', $params); | |
545 | } | |
546 | catch (CiviCRM_API3_Exception $e) { | |
547 | throw new CRM_Core_Exception('Relationship create error ' . $e->getMessage()); | |
548 | } | |
f55fb370 | 549 | |
be2fb01f CW |
550 | $this->setMessage(['saved' => TRUE]); |
551 | return [$params, [$this->_relationshipId]]; | |
f55fb370 | 552 | } |
553 | ||
554 | /** | |
555 | * Handling creating relationship action | |
556 | * | |
557 | * @param array $params | |
558 | * | |
559 | * @return array | |
425d6064 | 560 | * @throws \CRM_Core_Exception |
f55fb370 | 561 | */ |
562 | private function createAction($params) { | |
563 | list($params, $primaryContactLetter) = $this->preparePostProcessParameters($params); | |
564 | ||
565 | $outcome = CRM_Contact_BAO_Relationship::createMultiple($params, $primaryContactLetter); | |
566 | ||
567 | $relationshipIds = $outcome['relationship_ids']; | |
568 | ||
569 | $this->setMessage($outcome); | |
570 | ||
be2fb01f | 571 | return [$params, $relationshipIds]; |
f55fb370 | 572 | } |
573 | ||
f55fb370 | 574 | /** |
575 | * Prepares parameters to be used for create/update actions | |
576 | * | |
78f30a19 | 577 | * @param array $values |
f55fb370 | 578 | * |
579 | * @return array | |
580 | */ | |
78f30a19 MWMC |
581 | private function preparePostProcessParameters($values) { |
582 | $params = $values; | |
2a798409 | 583 | list($relationshipTypeId, $a, $b) = explode('_', $params['relationship_type_id']); |
f55fb370 | 584 | |
78f30a19 | 585 | $params['relationship_type_id'] = $relationshipTypeId; |
2a798409 | 586 | $params['contact_id_' . $a] = $this->_contactId; |
f55fb370 | 587 | |
588 | if (empty($this->_relationshipId)) { | |
2a798409 | 589 | $params['contact_id_' . $b] = explode(',', $params['related_contact_id']); |
f55fb370 | 590 | } |
591 | else { | |
592 | $params['id'] = $this->_relationshipId; | |
2a798409 | 593 | $params['contact_id_' . $b] = $params['related_contact_id']; |
f55fb370 | 594 | } |
595 | ||
2a798409 CW |
596 | // If this is a b_a relationship these form elements are flipped |
597 | $params['is_permission_a_b'] = CRM_Utils_Array::value("is_permission_{$a}_{$b}", $values, 0); | |
598 | $params['is_permission_b_a'] = CRM_Utils_Array::value("is_permission_{$b}_{$a}", $values, 0); | |
f55fb370 | 599 | |
be2fb01f | 600 | return [$params, $a]; |
f55fb370 | 601 | } |
602 | ||
603 | /** | |
604 | * Updates/Creates relationship notes | |
605 | * | |
606 | * @param array $relationshipIds | |
607 | * @param string $note | |
425d6064 MWMC |
608 | * |
609 | * @throws \CiviCRM_API3_Exception | |
f55fb370 | 610 | */ |
611 | private function saveRelationshipNotes($relationshipIds, $note) { | |
612 | foreach ($relationshipIds as $id) { | |
be2fb01f | 613 | $noteParams = [ |
f55fb370 | 614 | 'entity_id' => $id, |
615 | 'entity_table' => 'civicrm_relationship', | |
be2fb01f | 616 | ]; |
45caf31c | 617 | |
f55fb370 | 618 | $existing = civicrm_api3('note', 'get', $noteParams); |
619 | if (!empty($existing['id'])) { | |
620 | $noteParams['id'] = $existing['id']; | |
621 | } | |
45caf31c | 622 | |
623 | $action = NULL; | |
624 | if (!empty($note)) { | |
625 | $action = 'create'; | |
626 | $noteParams['note'] = $note; | |
627 | $noteParams['contact_id'] = $this->_contactId; | |
628 | } | |
629 | elseif (!empty($noteParams['id'])) { | |
630 | $action = 'delete'; | |
631 | } | |
632 | ||
633 | if (!empty($action)) { | |
f55fb370 | 634 | civicrm_api3('note', $action, $noteParams); |
635 | } | |
636 | } | |
637 | } | |
638 | ||
6a488035 | 639 | } |