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