Merge pull request #14227 from civicrm/5.14
[civicrm-core.git] / CRM / Contact / Form / Relationship.php
CommitLineData
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 */
31class 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
f55fb370 419 $this->setEmploymentRelationship($params, $relationshipIds);
7e98675f 420
7e98675f 421 // Refresh contact tabs which might have been affected
be2fb01f
CW
422 $this->ajaxResponse = [
423 'reloadBlocks' => ['#crm-contactinfo-content'],
424 'updateTabs' => [
df0c42cc
JP
425 '#tab_member' => CRM_Contact_BAO_Contact::getCountComponent('membership', $this->_contactId),
426 '#tab_contribute' => CRM_Contact_BAO_Contact::getCountComponent('contribution', $this->_contactId),
be2fb01f
CW
427 ],
428 ];
6a488035
TO
429 }
430
431 /**
fe482240 432 * Date validation.
6a488035 433 *
77c5b619
TO
434 * @param array $params
435 * (reference ) an assoc array of name/value pairs.
6a488035 436 *
72b3a70c
CW
437 * @return bool|array
438 * mixed true or array of errors
6a488035 439 */
00be9182 440 public static function dateRule($params) {
be2fb01f 441 $errors = [];
6a488035
TO
442
443 // check start and end date
8cc574cf 444 if (!empty($params['start_date']) && !empty($params['end_date'])) {
425d6064 445 if ($params['end_date'] < $params['start_date']) {
6a488035
TO
446 $errors['end_date'] = ts('The relationship end date cannot be prior to the start date.');
447 }
448 }
449
450 return empty($errors) ? TRUE : $errors;
451 }
452
2da59b29
EM
453 /**
454 * Set Status message to reflect outcome of the update action.
455 *
607fa308
EM
456 * @param array $outcome
457 * Outcome of save action - including
458 * - 'valid' : Number of valid relationships attempted.
459 * - 'invalid' : Number of invalid relationships attempted.
460 * - 'duplicate' : Number of duplicate relationships attempted.
461 * - 'saved' : boolean of whether save was successful
2da59b29 462 */
607fa308 463 protected function setMessage($outcome) {
782bfb8a 464 if (!empty($outcome['valid']) && empty($outcome['saved'])) {
be2fb01f 465 CRM_Core_Session::setStatus(ts('Relationship created.', [
607fa308 466 'count' => $outcome['valid'],
2da59b29 467 'plural' => '%count relationships created.',
be2fb01f 468 ]), ts('Saved'), 'success');
2da59b29 469 }
607fa308 470 if (!empty($outcome['invalid'])) {
be2fb01f 471 CRM_Core_Session::setStatus(ts('%count relationship record was not created due to an invalid contact type.', [
607fa308 472 'count' => $outcome['invalid'],
2da59b29 473 'plural' => '%count relationship records were not created due to invalid contact types.',
be2fb01f 474 ]), ts('%count invalid relationship record', [
607fa308 475 'count' => $outcome['invalid'],
2da59b29 476 'plural' => '%count invalid relationship records',
be2fb01f 477 ]));
2da59b29 478 }
607fa308 479 if (!empty($outcome['duplicate'])) {
be2fb01f 480 CRM_Core_Session::setStatus(ts('One relationship was not created because it already exists.', [
607fa308 481 'count' => $outcome['duplicate'],
2da59b29 482 'plural' => '%count relationships were not created because they already exist.',
be2fb01f 483 ]), ts('%count duplicate relationship', [
607fa308 484 'count' => $outcome['duplicate'],
2da59b29 485 'plural' => '%count duplicate relationships',
be2fb01f 486 ]));
2da59b29 487 }
607fa308 488 if (!empty($outcome['saved'])) {
2da59b29
EM
489 CRM_Core_Session::setStatus(ts('Relationship record has been updated.'), ts('Saved'), 'success');
490 }
491 }
492
0004ae05
CW
493 /**
494 * @param $relationshipList
425d6064 495 *
0004ae05
CW
496 * @return array
497 */
498 public static function getRelationshipTypeMetadata($relationshipList) {
499 $contactTypes = CRM_Contact_BAO_ContactType::contactTypeInfo(TRUE);
500 $allRelationshipNames = CRM_Core_PseudoConstant::relationshipType('name');
be2fb01f 501 $jsData = [];
0004ae05 502 // Get just what we need to keep the dom small
be2fb01f 503 $whatWeWant = array_flip([
0004ae05
CW
504 'contact_type_a',
505 'contact_type_b',
506 'contact_sub_type_a',
507 'contact_sub_type_b',
be2fb01f 508 ]);
0004ae05
CW
509 foreach ($allRelationshipNames as $id => $vals) {
510 if (isset($relationshipList["{$id}_a_b"]) || isset($relationshipList["{$id}_b_a"])) {
511 $jsData[$id] = array_filter(array_intersect_key($allRelationshipNames[$id], $whatWeWant));
512 // Add user-friendly placeholder
be2fb01f 513 foreach (['a', 'b'] as $x) {
0004ae05 514 $type = !empty($jsData[$id]["contact_sub_type_$x"]) ? $jsData[$id]["contact_sub_type_$x"] : CRM_Utils_Array::value("contact_type_$x", $jsData[$id]);
be2fb01f 515 $jsData[$id]["placeholder_$x"] = $type ? ts('- select %1 -', [strtolower($contactTypes[$type]['label'])]) : ts('- select contact -');
0004ae05
CW
516 }
517 }
518 }
519 return $jsData;
520 }
521
f55fb370 522 /**
523 * Handling 'delete relationship' action
524 *
525 * @param int $id
526 * Relationship ID
527 */
528 private function deleteAction($id) {
529 CRM_Contact_BAO_Relationship::del($id);
530
531 // reload all blocks to reflect this change on the user interface.
be2fb01f 532 $this->ajaxResponse['reloadBlocks'] = ['#crm-contactinfo-content'];
f55fb370 533 }
534
535 /**
536 * Handling updating relationship action
537 *
538 * @param array $params
539 *
540 * @return array
425d6064 541 * @throws \CRM_Core_Exception
f55fb370 542 */
543 private function updateAction($params) {
425d6064 544 list($params, $_) = $this->preparePostProcessParameters($params);
00d84e9b
JP
545 try {
546 civicrm_api3('relationship', 'create', $params);
547 }
548 catch (CiviCRM_API3_Exception $e) {
549 throw new CRM_Core_Exception('Relationship create error ' . $e->getMessage());
550 }
f55fb370 551
be2fb01f
CW
552 $this->setMessage(['saved' => TRUE]);
553 return [$params, [$this->_relationshipId]];
f55fb370 554 }
555
556 /**
557 * Handling creating relationship action
558 *
559 * @param array $params
560 *
561 * @return array
425d6064 562 * @throws \CRM_Core_Exception
f55fb370 563 */
564 private function createAction($params) {
565 list($params, $primaryContactLetter) = $this->preparePostProcessParameters($params);
566
567 $outcome = CRM_Contact_BAO_Relationship::createMultiple($params, $primaryContactLetter);
568
569 $relationshipIds = $outcome['relationship_ids'];
570
571 $this->setMessage($outcome);
572
be2fb01f 573 return [$params, $relationshipIds];
f55fb370 574 }
575
f55fb370 576 /**
577 * Prepares parameters to be used for create/update actions
578 *
78f30a19 579 * @param array $values
f55fb370 580 *
581 * @return array
582 */
78f30a19
MWMC
583 private function preparePostProcessParameters($values) {
584 $params = $values;
2a798409 585 list($relationshipTypeId, $a, $b) = explode('_', $params['relationship_type_id']);
f55fb370 586
78f30a19 587 $params['relationship_type_id'] = $relationshipTypeId;
2a798409 588 $params['contact_id_' . $a] = $this->_contactId;
f55fb370 589
590 if (empty($this->_relationshipId)) {
2a798409 591 $params['contact_id_' . $b] = explode(',', $params['related_contact_id']);
f55fb370 592 }
593 else {
594 $params['id'] = $this->_relationshipId;
2a798409 595 $params['contact_id_' . $b] = $params['related_contact_id'];
f55fb370 596 }
597
2a798409
CW
598 // If this is a b_a relationship these form elements are flipped
599 $params['is_permission_a_b'] = CRM_Utils_Array::value("is_permission_{$a}_{$b}", $values, 0);
600 $params['is_permission_b_a'] = CRM_Utils_Array::value("is_permission_{$b}_{$a}", $values, 0);
f55fb370 601
be2fb01f 602 return [$params, $a];
f55fb370 603 }
604
605 /**
606 * Updates/Creates relationship notes
607 *
608 * @param array $relationshipIds
609 * @param string $note
425d6064
MWMC
610 *
611 * @throws \CiviCRM_API3_Exception
f55fb370 612 */
613 private function saveRelationshipNotes($relationshipIds, $note) {
614 foreach ($relationshipIds as $id) {
be2fb01f 615 $noteParams = [
f55fb370 616 'entity_id' => $id,
617 'entity_table' => 'civicrm_relationship',
be2fb01f 618 ];
45caf31c 619
f55fb370 620 $existing = civicrm_api3('note', 'get', $noteParams);
621 if (!empty($existing['id'])) {
622 $noteParams['id'] = $existing['id'];
623 }
45caf31c 624
625 $action = NULL;
626 if (!empty($note)) {
627 $action = 'create';
628 $noteParams['note'] = $note;
629 $noteParams['contact_id'] = $this->_contactId;
630 }
631 elseif (!empty($noteParams['id'])) {
632 $action = 'delete';
633 }
634
635 if (!empty($action)) {
f55fb370 636 civicrm_api3('note', $action, $noteParams);
637 }
638 }
639 }
640
641 /**
642 * Sets current employee/employer relationship
643 *
644 * @param $params
645 * @param array $relationshipIds
646 */
647 private function setEmploymentRelationship($params, $relationshipIds) {
be2fb01f 648 $employerParams = [];
df0c42cc
JP
649 foreach ($relationshipIds as $id) {
650 if (!CRM_Contact_BAO_Relationship::isCurrentEmployerNeedingToBeCleared($params, $id)
651 //don't think this is required to check again.
652 && $this->_allRelationshipNames[$params['relationship_type_id']]["name_a_b"] == 'Employee of') {
f55fb370 653 // Fixme this is dumb why do we have to look this up again?
654 $rel = CRM_Contact_BAO_Relationship::getRelationshipByID($id);
655 $employerParams[$rel->contact_id_a] = $rel->contact_id_b;
656 }
df0c42cc
JP
657 }
658 if (!empty($employerParams)) {
f55fb370 659 // @todo this belongs in the BAO.
660 CRM_Contact_BAO_Contact_Utils::setCurrentEmployer($employerParams);
f55fb370 661 }
662 }
663
6a488035 664}