3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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
53 * This is a string which is used to determine the relationship between to contacts
58 * Display name of contact a
60 public $_display_name_a;
63 * Display name of contact b
65 public $_display_name_b;
68 * The relationship type id
72 public $_relationshipTypeId;
75 * An array of all relationship names
79 public $_allRelationshipNames;
89 public $_isCurrentEmployer;
97 * The relationship values if Updating relationship
102 * Case id if it called from case context
107 * Explicitly declare the form context.
109 public function getDefaultContext() {
114 * Explicitly declare the entity api name.
116 public function getDefaultEntity() {
117 return 'Relationship';
120 public function preProcess() {
121 $this->_contactId
= $this->get('contactId');
123 $this->_contactType
= CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactId
, 'contact_type');
125 $this->_relationshipId
= $this->get('id');
127 $this->_rtype
= CRM_Utils_Request
::retrieve('rtype', 'String', $this);
129 $this->_rtypeId
= CRM_Utils_Request
::retrieve('relTypeId', 'String', $this);
131 $this->_display_name_a
= CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactId
, 'display_name');
133 $this->assign('display_name_a', $this->_display_name_a
);
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
);
141 // Check for permissions
142 if (in_array($this->_action
, array(CRM_Core_Action
::ADD
, CRM_Core_Action
::UPDATE
, CRM_Core_Action
::DELETE
))) {
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
)) {
145 CRM_Core_Error
::statusBounce(ts('You do not have the necessary permission to edit this contact.'));
149 // Set page title based on action
150 switch ($this->_action
) {
151 case CRM_Core_Action
::VIEW
:
152 CRM_Utils_System
::setTitle(ts('View Relationship for %1', array(1 => $this->_display_name_a
)));
155 case CRM_Core_Action
::ADD
:
156 CRM_Utils_System
::setTitle(ts('Add Relationship for %1', array(1 => $this->_display_name_a
)));
159 case CRM_Core_Action
::UPDATE
:
160 CRM_Utils_System
::setTitle(ts('Edit Relationship for %1', array(1 => $this->_display_name_a
)));
163 case CRM_Core_Action
::DELETE
:
164 CRM_Utils_System
::setTitle(ts('Delete Relationship for %1', array(1 => $this->_display_name_a
)));
168 $this->_caseId
= CRM_Utils_Request
::retrieve('caseID', 'Integer', $this);
170 if (!$this->_rtypeId
) {
171 $params = CRM_Utils_Request
::exportValues();
172 if (isset($params['relationship_type_id'])) {
173 $this->_rtypeId
= $params['relationship_type_id'];
175 elseif (!empty($this->_values
)) {
176 $this->_rtypeId
= $this->_values
['relationship_type_id'] . '_' . $this->_rtype
;
180 //get the relationship type id
181 $this->_relationshipTypeId
= str_replace(array('_a_b', '_b_a'), array('', ''), $this->_rtypeId
);
183 //get the relationship type
184 if (!$this->_rtype
) {
185 $this->_rtype
= str_replace($this->_relationshipTypeId
. '_', '', $this->_rtypeId
);
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
);
193 //use name as it remain constant, CRM-3336
194 $this->_allRelationshipNames
= CRM_Core_PseudoConstant
::relationshipType('name');
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');
203 // when custom data is included in this page
204 if (!empty($_POST['hidden_custom'])) {
205 CRM_Custom_Form_CustomData
::preProcess($this, NULL, $this->_relationshipTypeId
, 1, 'Relationship', $this->_relationshipId
);
206 CRM_Custom_Form_CustomData
::buildQuickForm($this);
207 CRM_Custom_Form_CustomData
::setDefaultValues($this);
212 * Set default values for the form.
214 public function setDefaultValues() {
218 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
219 if (!empty($this->_values
)) {
220 $defaults['relationship_type_id'] = $this->_rtypeId
;
221 if (!empty($this->_values
['start_date'])) {
222 list($defaults['start_date']) = CRM_Utils_Date
::setDateDefaults($this->_values
['start_date']);
224 if (!empty($this->_values
['end_date'])) {
225 list($defaults['end_date']) = CRM_Utils_Date
::setDateDefaults($this->_values
['end_date']);
227 $defaults['description'] = CRM_Utils_Array
::value('description', $this->_values
);
228 $defaults['is_active'] = CRM_Utils_Array
::value('is_active', $this->_values
);
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
);
234 $defaults['is_current_employer'] = $this->_isCurrentEmployer
;
236 // Load info about the related contact
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'];
242 $contact->id
= $this->_values
['contact_id_a'];
244 if ($contact->find(TRUE)) {
245 $defaults['related_contact_id'] = $contact->id
;
246 $this->_display_name_b
= $contact->display_name
;
247 $this->assign('display_name_b', $this->_display_name_b
);
251 'entity_id' => $this->_relationshipId
,
252 'entity_table' => 'civicrm_relationship',
256 $note = civicrm_api('Note', 'getsingle', $noteParams);
257 $defaults['note'] = CRM_Utils_Array
::value('note', $note);
261 $defaults['is_active'] = $defaults['is_current_employer'] = 1;
262 $defaults['relationship_type_id'] = $this->_rtypeId
;
265 $this->_enabled
= $defaults['is_active'];
270 * Add the rules for form.
272 public function addRules() {
274 if (!($this->_action
& CRM_Core_Action
::DELETE
)) {
275 $this->addFormRule(array('CRM_Contact_Form_Relationship', 'dateRule'));
280 * Build the form object.
282 public function buildQuickForm() {
283 if ($this->_action
& CRM_Core_Action
::DELETE
) {
284 $this->addButtons(array(
287 'name' => ts('Delete'),
292 'name' => ts('Cancel'),
300 $relationshipList = CRM_Contact_BAO_Relationship
::getContactRelationshipType($this->_contactId
, $this->_rtype
, $this->_relationshipId
);
302 // Metadata needed on clientside
303 $this->assign('relationshipData', self
::getRelationshipTypeMetadata($relationshipList));
305 foreach ($this->_allRelationshipNames
as $id => $vals) {
306 if ($vals['name_a_b'] === 'Employee of') {
307 $this->assign('employmentRelationship', $id);
312 $this->addField('relationship_type_id', array('options' => array('' => ts('- select -')) +
$relationshipList, 'class' => 'huge', 'placeholder' => '- select -'), TRUE);
314 $label = $this->_action
& CRM_Core_Action
::ADD ?
ts('Contact(s)') : ts('Contact');
315 $contactField = $this->addField('related_contact_id', array('label' => $label, 'name' => 'contact_id_b', 'multiple' => TRUE, 'create' => TRUE), TRUE);
316 // This field cannot be updated
317 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
318 $contactField->freeze();
321 $this->add('advcheckbox', 'is_current_employer', $this->_contactType
== 'Organization' ?
ts('Current Employee') : ts('Current Employer'));
323 $this->addField('start_date', array('label' => ts('Start Date'), 'formatType' => 'searchDate'));
324 $this->addField('end_date', array('label' => ts('End Date'), 'formatType' => 'searchDate'));
326 $this->addField('is_active', array('label' => ts('Enabled?'), 'type' => 'advcheckbox'));
328 $this->addField('is_permission_a_b');
329 $this->addField('is_permission_b_a');
331 $this->addField('description', array('label' => ts('Description')));
333 CRM_Contact_Form_Edit_Notes
::buildQuickForm($this);
335 if ($this->_action
& CRM_Core_Action
::VIEW
) {
336 $this->addButtons(array(
339 'name' => ts('Done'),
344 // make this form an upload since we don't know if the custom data injected dynamically is of type file etc.
345 $this->addButtons(array(
348 'name' => ts('Save Relationship'),
353 'name' => ts('Cancel'),
360 * This function is called when the form is submitted and also from unit test.
361 * @param array $params
365 public function submit($params) {
366 switch ($this->getAction()) {
367 case CRM_Core_Action
::DELETE
:
368 $this->deleteAction($this->_relationshipId
);
371 case CRM_Core_Action
::UPDATE
:
372 return $this->updateAction($params);
375 return $this->createAction($params);
380 * This function is called when the form is submitted.
382 public function postProcess() {
383 // Store the submitted values in an array.
384 $params = $this->controller
->exportValues($this->_name
);
386 $values = $this->submit($params);
387 if (empty($values)) {
390 list ($params, $relationshipIds) = $values;
392 // if this is called from case view,
393 //create an activity for case role removal.CRM-4480
394 // @todo this belongs in the BAO.
395 if ($this->_caseId
) {
396 CRM_Case_BAO_Case
::createCaseRoleActivity($this->_caseId
, $relationshipIds, $params['contact_check'], $this->_contactId
);
399 // @todo this belongs in the BAO.
400 $note = !empty($params['note']) ?
$params['note'] : '';
401 $this->saveRelationshipNotes($relationshipIds, $note);
403 $this->setEmploymentRelationship($params, $relationshipIds);
405 // Refresh contact tabs which might have been affected
406 $this->ajaxResponse
['updateTabs'] = array(
407 '#tab_member' => CRM_Contact_BAO_Contact
::getCountComponent('membership', $this->_contactId
),
408 '#tab_contribute' => CRM_Contact_BAO_Contact
::getCountComponent('contribution', $this->_contactId
),
415 * @param array $params
416 * (reference ) an assoc array of name/value pairs.
419 * mixed true or array of errors
421 public static function dateRule($params) {
424 // check start and end date
425 if (!empty($params['start_date']) && !empty($params['end_date'])) {
426 $start_date = CRM_Utils_Date
::format(CRM_Utils_Array
::value('start_date', $params));
427 $end_date = CRM_Utils_Date
::format(CRM_Utils_Array
::value('end_date', $params));
428 if ($start_date && $end_date && (int ) $end_date < (int ) $start_date) {
429 $errors['end_date'] = ts('The relationship end date cannot be prior to the start date.');
433 return empty($errors) ?
TRUE : $errors;
437 * Set Status message to reflect outcome of the update action.
439 * @param array $outcome
440 * Outcome of save action - including
441 * - 'valid' : Number of valid relationships attempted.
442 * - 'invalid' : Number of invalid relationships attempted.
443 * - 'duplicate' : Number of duplicate relationships attempted.
444 * - 'saved' : boolean of whether save was successful
446 protected function setMessage($outcome) {
447 if (!empty($outcome['valid']) && empty($outcome['saved'])) {
448 CRM_Core_Session
::setStatus(ts('Relationship created.', array(
449 'count' => $outcome['valid'],
450 'plural' => '%count relationships created.',
451 )), ts('Saved'), 'success');
453 if (!empty($outcome['invalid'])) {
454 CRM_Core_Session
::setStatus(ts('%count relationship record was not created due to an invalid contact type.', array(
455 'count' => $outcome['invalid'],
456 'plural' => '%count relationship records were not created due to invalid contact types.',
457 )), ts('%count invalid relationship record', array(
458 'count' => $outcome['invalid'],
459 'plural' => '%count invalid relationship records',
462 if (!empty($outcome['duplicate'])) {
463 CRM_Core_Session
::setStatus(ts('One relationship was not created because it already exists.', array(
464 'count' => $outcome['duplicate'],
465 'plural' => '%count relationships were not created because they already exist.',
466 )), ts('%count duplicate relationship', array(
467 'count' => $outcome['duplicate'],
468 'plural' => '%count duplicate relationships',
471 if (!empty($outcome['saved'])) {
472 CRM_Core_Session
::setStatus(ts('Relationship record has been updated.'), ts('Saved'), 'success');
477 * @param $relationshipList
480 public static function getRelationshipTypeMetadata($relationshipList) {
481 $contactTypes = CRM_Contact_BAO_ContactType
::contactTypeInfo(TRUE);
482 $allRelationshipNames = CRM_Core_PseudoConstant
::relationshipType('name');
484 // Get just what we need to keep the dom small
485 $whatWeWant = array_flip(array(
488 'contact_sub_type_a',
489 'contact_sub_type_b',
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
495 foreach (array('a', 'b') as $x) {
496 $type = !empty($jsData[$id]["contact_sub_type_$x"]) ?
$jsData[$id]["contact_sub_type_$x"] : CRM_Utils_Array
::value("contact_type_$x", $jsData[$id]);
497 $jsData[$id]["placeholder_$x"] = $type ?
ts('- select %1 -', array(strtolower($contactTypes[$type]['label']))) : ts('- select contact -');
505 * Handling 'delete relationship' action
510 private function deleteAction($id) {
511 CRM_Contact_BAO_Relationship
::del($id);
513 // reload all blocks to reflect this change on the user interface.
514 $this->ajaxResponse
['reloadBlocks'] = array('#crm-contactinfo-content');
518 * Handling updating relationship action
520 * @param array $params
524 private function updateAction($params) {
525 $params = $this->preparePostProcessParameters($params);
526 $params = $params[0];
529 civicrm_api3('relationship', 'create', $params);
531 catch (CiviCRM_API3_Exception
$e) {
532 throw new CRM_Core_Exception('Relationship create error ' . $e->getMessage());
535 $this->clearCurrentEmployer($params);
537 $this->setMessage(array('saved' => TRUE));
539 return array($params, array($this->_relationshipId
));
543 * Handling creating relationship action
545 * @param array $params
549 private function createAction($params) {
550 list($params, $primaryContactLetter) = $this->preparePostProcessParameters($params);
552 $outcome = CRM_Contact_BAO_Relationship
::createMultiple($params, $primaryContactLetter);
554 $relationshipIds = $outcome['relationship_ids'];
556 $this->setMessage($outcome);
558 return array($params, $relationshipIds);
563 * Prepares parameters to be used for create/update actions
565 * @param array $params
569 private function preparePostProcessParameters($params) {
570 $relationshipTypeParts = explode('_', $params['relationship_type_id']);
572 $params['relationship_type_id'] = $relationshipTypeParts[0];
573 $params['contact_id_' . $relationshipTypeParts[1]] = $this->_contactId
;
575 if (empty($this->_relationshipId
)) {
576 $params['contact_id_' . $relationshipTypeParts[2]] = explode(',', $params['related_contact_id']);
579 $params['id'] = $this->_relationshipId
;
580 $params['contact_id_' . $relationshipTypeParts[2]] = $params['related_contact_id'];
582 foreach (array('start_date', 'end_date') as $dateParam) {
583 if (!empty($params[$dateParam])) {
584 $params[$dateParam] = CRM_Utils_Date
::processDate($params[$dateParam]);
589 // CRM-14612 - Don't use adv-checkbox as it interferes with the form js
590 $params['is_permission_a_b'] = CRM_Utils_Array
::value('is_permission_a_b', $params, 0);
591 $params['is_permission_b_a'] = CRM_Utils_Array
::value('is_permission_b_a', $params, 0);
593 return array($params, $relationshipTypeParts[1]);
597 * Updates/Creates relationship notes
599 * @param array $relationshipIds
600 * @param string $note
602 private function saveRelationshipNotes($relationshipIds, $note) {
603 foreach ($relationshipIds as $id) {
606 'entity_table' => 'civicrm_relationship',
609 $existing = civicrm_api3('note', 'get', $noteParams);
610 if (!empty($existing['id'])) {
611 $noteParams['id'] = $existing['id'];
617 $noteParams['note'] = $note;
618 $noteParams['contact_id'] = $this->_contactId
;
620 elseif (!empty($noteParams['id'])) {
624 if (!empty($action)) {
625 civicrm_api3('note', $action, $noteParams);
631 * Sets current employee/employer relationship
634 * @param array $relationshipIds
636 private function setEmploymentRelationship($params, $relationshipIds) {
638 !empty($params['is_current_employer']) &&
639 $this->_allRelationshipNames
[$params['relationship_type_id']]["name_a_b"] == 'Employee of') {
640 $employerParams = array();
641 foreach ($relationshipIds as $id) {
642 // Fixme this is dumb why do we have to look this up again?
643 $rel = CRM_Contact_BAO_Relationship
::getRelationshipByID($id);
644 $employerParams[$rel->contact_id_a
] = $rel->contact_id_b
;
646 // @todo this belongs in the BAO.
647 CRM_Contact_BAO_Contact_Utils
::setCurrentEmployer($employerParams);
648 // Refresh contact summary if in ajax mode
649 $this->ajaxResponse
['reloadBlocks'] = array('#crm-contactinfo-content');
654 * Clears the current employer if the relationship type
655 * get changed, disabled or 'current employer' checkbox get unchecked.
659 private function clearCurrentEmployer($params) {
660 // @todo this belongs in the BAO.
661 if ($this->_isCurrentEmployer
) {
662 $relChanged = $params['relationship_type_id'] != $this->_values
['relationship_type_id'];
663 if (!$params['is_active'] ||
!$params['is_current_employer'] ||
$relChanged) {
664 CRM_Contact_BAO_Contact_Utils
::clearCurrentEmployer($this->_values
['contact_id_a']);
666 // Refresh contact summary if in ajax mode
667 $this->ajaxResponse
['reloadBlocks'] = array('#crm-contactinfo-content');