3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2020 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * This class generates form components for relationship.
31 class CRM_Contact_Form_Relationship
extends CRM_Core_Form
{
34 * The relationship id, used when editing the relationship
38 public $_relationshipId;
41 * The contact id, used when add/edit relationship
48 * This is a string which is either a_b or b_a used to determine the relationship between to contacts
54 * This is a string which is used to determine the relationship between to contacts
60 * Display name of contact a
63 public $_display_name_a;
66 * Display name of contact b
69 public $_display_name_b;
72 * The relationship type id
76 public $_relationshipTypeId;
79 * An array of all relationship names
83 public $_allRelationshipNames;
93 public $_isCurrentEmployer;
101 * The relationship values if Updating relationship
107 * Case id if it called from case context
113 * Explicitly declare the form context.
115 public function getDefaultContext() {
120 * Explicitly declare the entity api name.
122 public function getDefaultEntity() {
123 return 'Relationship';
126 public function preProcess() {
127 $this->_contactId
= $this->get('contactId');
129 $this->_contactType
= CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactId
, 'contact_type');
131 $this->_relationshipId
= $this->get('id');
133 $this->_rtype
= CRM_Utils_Request
::retrieve('rtype', 'String', $this);
135 $this->_rtypeId
= CRM_Utils_Request
::retrieve('relTypeId', 'String', $this);
137 $this->_display_name_a
= CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactId
, 'display_name');
139 $this->assign('display_name_a', $this->_display_name_a
);
140 //get the relationship values.
142 if ($this->_relationshipId
) {
143 $params = ['id' => $this->_relationshipId
];
144 CRM_Core_DAO
::commonRetrieve('CRM_Contact_DAO_Relationship', $params, $this->_values
);
147 // Check for permissions
148 if (in_array($this->_action
, [CRM_Core_Action
::ADD
, CRM_Core_Action
::UPDATE
, CRM_Core_Action
::DELETE
])) {
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
)) {
151 CRM_Core_Error
::statusBounce(ts('You do not have the necessary permission to edit this contact.'));
155 // Set page title based on action
156 switch ($this->_action
) {
157 case CRM_Core_Action
::VIEW
:
158 CRM_Utils_System
::setTitle(ts('View Relationship for %1', [1 => $this->_display_name_a
]));
161 case CRM_Core_Action
::ADD
:
162 CRM_Utils_System
::setTitle(ts('Add Relationship for %1', [1 => $this->_display_name_a
]));
165 case CRM_Core_Action
::UPDATE
:
166 CRM_Utils_System
::setTitle(ts('Edit Relationship for %1', [1 => $this->_display_name_a
]));
169 case CRM_Core_Action
::DELETE
:
170 CRM_Utils_System
::setTitle(ts('Delete Relationship for %1', [1 => $this->_display_name_a
]));
174 $this->_caseId
= CRM_Utils_Request
::retrieve('caseID', 'Integer', $this);
176 if (!$this->_rtypeId
) {
177 $params = CRM_Utils_Request
::exportValues();
178 if (isset($params['relationship_type_id'])) {
179 $this->_rtypeId
= $params['relationship_type_id'];
181 elseif (!empty($this->_values
)) {
182 $this->_rtypeId
= $this->_values
['relationship_type_id'] . '_' . $this->_rtype
;
186 //get the relationship type id
187 $this->_relationshipTypeId
= str_replace(['_a_b', '_b_a'], ['', ''], $this->_rtypeId
);
189 //get the relationship type
190 if (!$this->_rtype
) {
191 $this->_rtype
= str_replace($this->_relationshipTypeId
. '_', '', $this->_rtypeId
);
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
);
199 //use name as it remain constant, CRM-3336
200 $this->_allRelationshipNames
= CRM_Core_PseudoConstant
::relationshipType('name');
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');
209 // when custom data is included in this page
210 if (!empty($_POST['hidden_custom'])) {
211 CRM_Custom_Form_CustomData
::preProcess($this, NULL, $this->_relationshipTypeId
, 1, 'Relationship', $this->_relationshipId
);
212 CRM_Custom_Form_CustomData
::buildQuickForm($this);
213 CRM_Custom_Form_CustomData
::setDefaultValues($this);
218 * Set default values for the form.
220 public function setDefaultValues() {
223 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
224 if (!empty($this->_values
)) {
225 $defaults['relationship_type_id'] = $this->_rtypeId
;
226 $defaults['start_date'] = CRM_Utils_Array
::value('start_date', $this->_values
);
227 $defaults['end_date'] = CRM_Utils_Array
::value('end_date', $this->_values
);
228 $defaults['description'] = CRM_Utils_Array
::value('description', $this->_values
);
229 $defaults['is_active'] = CRM_Utils_Array
::value('is_active', $this->_values
);
231 // The postprocess function will swap these fields if it is a b_a relationship, so we compensate here
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
);
235 $defaults['is_current_employer'] = $this->_isCurrentEmployer
;
237 // Load info about the related contact
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'];
243 $contact->id
= $this->_values
['contact_id_a'];
245 if ($contact->find(TRUE)) {
246 $defaults['related_contact_id'] = $contact->id
;
247 $this->_display_name_b
= $contact->display_name
;
248 $this->assign('display_name_b', $this->_display_name_b
);
252 'entity_id' => $this->_relationshipId
,
253 'entity_table' => 'civicrm_relationship',
257 $note = civicrm_api('Note', 'getsingle', $noteParams);
258 $defaults['note'] = CRM_Utils_Array
::value('note', $note);
262 $defaults['is_active'] = $defaults['is_current_employer'] = 1;
263 $defaults['relationship_type_id'] = $this->_rtypeId
;
264 $defaults['is_permission_a_b'] = $defaults['is_permission_b_a'] = CRM_Contact_BAO_Relationship
::NONE
;
267 $this->_enabled
= $defaults['is_active'];
272 * Add the rules for form.
274 public function addRules() {
275 if (!($this->_action
& CRM_Core_Action
::DELETE
)) {
276 $this->addFormRule(['CRM_Contact_Form_Relationship', 'dateRule']);
281 * Build the form object.
283 public function buildQuickForm() {
284 if ($this->_action
& CRM_Core_Action
::DELETE
) {
288 'name' => ts('Delete'),
293 'name' => ts('Cancel'),
300 $relationshipList = CRM_Contact_BAO_Relationship
::getContactRelationshipType($this->_contactId
, $this->_rtype
, $this->_relationshipId
);
302 $this->assign('contactTypes', CRM_Contact_BAO_ContactType
::contactTypeInfo(TRUE));
304 foreach ($this->_allRelationshipNames
as $id => $vals) {
305 if ($vals['name_a_b'] === 'Employee of') {
306 $this->assign('employmentRelationship', $id);
312 'relationship_type_id',
314 'options' => ['' => ts('- select -')] +
$relationshipList,
316 'placeholder' => '- select -',
317 'option_url' => 'civicrm/admin/reltype',
318 'option_context' => [
319 'contact_id' => $this->_contactId
,
320 'relationship_direction' => $this->_rtype
,
321 'relationship_id' => $this->_relationshipId
,
328 $label = $this->_action
& CRM_Core_Action
::ADD ?
ts('Contact(s)') : ts('Contact');
329 $contactField = $this->addField('related_contact_id', ['label' => $label, 'name' => 'contact_id_b', 'multiple' => TRUE, 'create' => TRUE], TRUE);
330 // This field cannot be updated
331 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
332 $contactField->freeze();
335 $this->add('advcheckbox', 'is_current_employer', $this->_contactType
== 'Organization' ?
ts('Current Employee') : ts('Current Employer'));
337 $this->addField('start_date', ['label' => ts('Start Date')], FALSE, FALSE);
338 $this->addField('end_date', ['label' => ts('End Date')], FALSE, FALSE);
340 $this->addField('is_active', ['label' => ts('Enabled?'), 'type' => 'advcheckbox']);
342 $this->addField('is_permission_a_b', [], TRUE);
343 $this->addField('is_permission_b_a', [], TRUE);
345 $this->addField('description', ['label' => ts('Description')]);
347 CRM_Contact_Form_Edit_Notes
::buildQuickForm($this);
349 if ($this->_action
& CRM_Core_Action
::VIEW
) {
353 'name' => ts('Done'),
358 // make this form an upload since we don't know if the custom data injected dynamically is of type file etc.
362 'name' => ts('Save Relationship'),
367 'name' => ts('Cancel'),
374 * This function is called when the form is submitted and also from unit test.
376 * @param array $params
379 * @throws \CRM_Core_Exception
381 public function submit($params) {
382 switch ($this->getAction()) {
383 case CRM_Core_Action
::DELETE
:
384 $this->deleteAction($this->_relationshipId
);
387 case CRM_Core_Action
::UPDATE
:
388 return $this->updateAction($params);
391 return $this->createAction($params);
396 * This function is called when the form is submitted.
398 public function postProcess() {
399 // Store the submitted values in an array.
400 $params = $this->controller
->exportValues($this->_name
);
402 $values = $this->submit($params);
403 if (empty($values)) {
406 list ($params, $relationshipIds) = $values;
408 // if this is called from case view,
409 //create an activity for case role removal.CRM-4480
410 // @todo this belongs in the BAO.
411 if ($this->_caseId
) {
412 CRM_Case_BAO_Case
::createCaseRoleActivity($this->_caseId
, $relationshipIds, $params['contact_check'], $this->_contactId
);
415 // @todo this belongs in the BAO.
416 $note = !empty($params['note']) ?
$params['note'] : '';
417 $this->saveRelationshipNotes($relationshipIds, $note);
419 // Refresh contact tabs which might have been affected
420 $this->ajaxResponse
= [
421 'reloadBlocks' => ['#crm-contactinfo-content'],
423 '#tab_member' => CRM_Contact_BAO_Contact
::getCountComponent('membership', $this->_contactId
),
424 '#tab_contribute' => CRM_Contact_BAO_Contact
::getCountComponent('contribution', $this->_contactId
),
432 * @param array $params
433 * (reference ) an assoc array of name/value pairs.
436 * mixed true or array of errors
438 public static function dateRule($params) {
441 // check start and end date
442 if (!empty($params['start_date']) && !empty($params['end_date'])) {
443 if ($params['end_date'] < $params['start_date']) {
444 $errors['end_date'] = ts('The relationship end date cannot be prior to the start date.');
448 return empty($errors) ?
TRUE : $errors;
452 * Set Status message to reflect outcome of the update action.
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
461 protected function setMessage($outcome) {
462 if (!empty($outcome['valid']) && empty($outcome['saved'])) {
463 CRM_Core_Session
::setStatus(ts('Relationship created.', [
464 'count' => $outcome['valid'],
465 'plural' => '%count relationships created.',
466 ]), ts('Saved'), 'success');
468 if (!empty($outcome['invalid'])) {
469 CRM_Core_Session
::setStatus(ts('%count relationship record was not created due to an invalid contact type.', [
470 'count' => $outcome['invalid'],
471 'plural' => '%count relationship records were not created due to invalid contact types.',
472 ]), ts('%count invalid relationship record', [
473 'count' => $outcome['invalid'],
474 'plural' => '%count invalid relationship records',
477 if (!empty($outcome['duplicate'])) {
478 CRM_Core_Session
::setStatus(ts('One relationship was not created because it already exists.', [
479 'count' => $outcome['duplicate'],
480 'plural' => '%count relationships were not created because they already exist.',
481 ]), ts('%count duplicate relationship', [
482 'count' => $outcome['duplicate'],
483 'plural' => '%count duplicate relationships',
486 if (!empty($outcome['saved'])) {
487 CRM_Core_Session
::setStatus(ts('Relationship record has been updated.'), ts('Saved'), 'success');
492 * @param $relationshipList
496 public static function getRelationshipTypeMetadata($relationshipList) {
497 $contactTypes = CRM_Contact_BAO_ContactType
::contactTypeInfo(TRUE);
498 $allRelationshipNames = CRM_Core_PseudoConstant
::relationshipType('name');
500 // Get just what we need to keep the dom small
501 $whatWeWant = array_flip([
504 'contact_sub_type_a',
505 'contact_sub_type_b',
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
511 foreach (['a', 'b'] as $x) {
512 $type = !empty($jsData[$id]["contact_sub_type_$x"]) ?
$jsData[$id]["contact_sub_type_$x"] : CRM_Utils_Array
::value("contact_type_$x", $jsData[$id]);
513 $jsData[$id]["placeholder_$x"] = $type ?
ts('- select %1 -', [strtolower($contactTypes[$type]['label'])]) : ts('- select contact -');
521 * Handling 'delete relationship' action
526 private function deleteAction($id) {
527 CRM_Contact_BAO_Relationship
::del($id);
529 // reload all blocks to reflect this change on the user interface.
530 $this->ajaxResponse
['reloadBlocks'] = ['#crm-contactinfo-content'];
534 * Handling updating relationship action
536 * @param array $params
539 * @throws \CRM_Core_Exception
541 private function updateAction($params) {
542 list($params, $_) = $this->preparePostProcessParameters($params);
544 civicrm_api3('relationship', 'create', $params);
546 catch (CiviCRM_API3_Exception
$e) {
547 throw new CRM_Core_Exception('Relationship create error ' . $e->getMessage());
550 $this->setMessage(['saved' => TRUE]);
551 return [$params, [$this->_relationshipId
]];
555 * Handling creating relationship action
557 * @param array $params
560 * @throws \CRM_Core_Exception
562 private function createAction($params) {
563 list($params, $primaryContactLetter) = $this->preparePostProcessParameters($params);
565 $outcome = CRM_Contact_BAO_Relationship
::createMultiple($params, $primaryContactLetter);
567 $relationshipIds = $outcome['relationship_ids'];
569 $this->setMessage($outcome);
571 return [$params, $relationshipIds];
575 * Prepares parameters to be used for create/update actions
577 * @param array $values
581 private function preparePostProcessParameters($values) {
583 list($relationshipTypeId, $a, $b) = explode('_', $params['relationship_type_id']);
585 $params['relationship_type_id'] = $relationshipTypeId;
586 $params['contact_id_' . $a] = $this->_contactId
;
588 if (empty($this->_relationshipId
)) {
589 $params['contact_id_' . $b] = explode(',', $params['related_contact_id']);
592 $params['id'] = $this->_relationshipId
;
593 $params['contact_id_' . $b] = $params['related_contact_id'];
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);
600 return [$params, $a];
604 * Updates/Creates relationship notes
606 * @param array $relationshipIds
607 * @param string $note
609 * @throws \CiviCRM_API3_Exception
611 private function saveRelationshipNotes($relationshipIds, $note) {
612 foreach ($relationshipIds as $id) {
615 'entity_table' => 'civicrm_relationship',
618 $existing = civicrm_api3('note', 'get', $noteParams);
619 if (!empty($existing['id'])) {
620 $noteParams['id'] = $existing['id'];
626 $noteParams['note'] = $note;
627 $noteParams['contact_id'] = $this->_contactId
;
629 elseif (!empty($noteParams['id'])) {
633 if (!empty($action)) {
634 civicrm_api3('note', $action, $noteParams);