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