3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
37 * This class generates form components for relationship
40 class CRM_Contact_Form_Relationship
extends CRM_Core_Form
{
43 * The relationship id, used when editing the relationship
47 public $_relationshipId;
50 * The contact id, used when add/edit relationship
57 * This is a string which is either a_b or b_a used to determine the relationship between to contacts
62 * This is a string which is used to determine the relationship between to contacts
67 * Display name of contact a
69 public $_display_name_a;
72 * Display name of contact b
74 public $_display_name_b;
77 * The relationship type id
81 public $_relationshipTypeId;
84 * An array of all relationship names
88 public $_allRelationshipNames;
98 public $_isCurrentEmployer;
103 public $_contactType;
106 * The relationship values if Updating relationship
111 * Casid if it called from case context
120 public function preProcess() {
121 //custom data related code
122 $this->_cdType
= CRM_Utils_Array
::value('type', $_GET);
123 $this->assign('cdType', FALSE);
124 if ($this->_cdType
) {
125 $this->assign('cdType', TRUE);
126 return CRM_Custom_Form_CustomData
::preProcess($this);
129 $this->_contactId
= $this->get('contactId');
131 $this->_contactType
= CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactId
, 'contact_type');
133 $this->_relationshipId
= $this->get('id');
135 $this->_rtype
= CRM_Utils_Request
::retrieve('rtype', 'String', $this);
137 $this->_rtypeId
= CRM_Utils_Request
::retrieve('relTypeId', 'String', $this);
139 $this->_display_name_a
= CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactId
, 'display_name');
141 $this->assign('display_name_a', $this->_display_name_a
);
143 // Set page title based on action
144 switch ($this->_action
) {
145 case CRM_Core_Action
::VIEW
:
146 CRM_Utils_System
::setTitle(ts('View Relationship for %1', array(1 => $this->_display_name_a
)));
148 case CRM_Core_Action
::ADD
:
149 CRM_Utils_System
::setTitle(ts('Add Relationship for %1', array(1 => $this->_display_name_a
)));
151 case CRM_Core_Action
::UPDATE
:
152 CRM_Utils_System
::setTitle(ts('Edit Relationship for %1', array(1 => $this->_display_name_a
)));
154 case CRM_Core_Action
::DELETE
:
155 CRM_Utils_System
::setTitle(ts('Delete Relationship for %1', array(1 => $this->_display_name_a
)));
159 $this->_caseId
= CRM_Utils_Request
::retrieve('caseID', 'Integer', $this);
161 //get the relationship values.
162 $this->_values
= array();
163 if ($this->_relationshipId
) {
164 $params = array('id' => $this->_relationshipId
);
165 CRM_Core_DAO
::commonRetrieve('CRM_Contact_DAO_Relationship', $params, $this->_values
);
168 if (!$this->_rtypeId
) {
169 $params = $this->controller
->exportValues($this->_name
);
170 if (isset($params['relationship_type_id'])) {
171 $this->_rtypeId
= $params['relationship_type_id'];
173 elseif (!empty($this->_values
)) {
174 $this->_rtypeId
= $this->_values
['relationship_type_id'] . '_' . $this->_rtype
;
178 //get the relationship type id
179 $this->_relationshipTypeId
= str_replace(array('_a_b', '_b_a'), array('', ''), $this->_rtypeId
);
181 //get the relationship type
182 if (!$this->_rtype
) {
183 $this->_rtype
= str_replace($this->_relationshipTypeId
. '_', '', $this->_rtypeId
);
186 //need to assign custom data type and subtype to the template - FIXME: explain why
187 $this->assign('customDataType', 'Relationship');
188 $this->assign('customDataSubType', $this->_relationshipTypeId
);
189 $this->assign('entityID', $this->_relationshipId
);
191 //use name as it remain constant, CRM-3336
192 $this->_allRelationshipNames
= CRM_Core_PseudoConstant
::relationshipType('name');
195 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
196 if ($this->_allRelationshipNames
[$this->_relationshipTypeId
]["name_a_b"] == 'Employee of') {
197 $this->_isCurrentEmployer
= $this->_values
['contact_id_b'] == CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $this->_values
['contact_id_a'], 'employer_id');
201 // when custom data is included in this page
202 if (!empty($_POST['hidden_custom'])) {
203 CRM_Custom_Form_CustomData
::preProcess($this);
204 CRM_Custom_Form_CustomData
::buildQuickForm($this);
205 CRM_Custom_Form_CustomData
::setDefaultValues($this);
210 * Set default values for the form. Relationship that in edit/view mode
211 * the default values are retrieved from the database
216 public function setDefaultValues() {
217 if ($this->_cdType
) {
218 return CRM_Custom_Form_CustomData
::setDefaultValues($this);
223 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
224 if (!empty($this->_values
)) {
225 $defaults['relationship_type_id'] = $this->_rtypeId
;
226 if (!empty($this->_values
['start_date'])) {
227 list($defaults['start_date']) = CRM_Utils_Date
::setDateDefaults($this->_values
['start_date']);
229 if (!empty($this->_values
['end_date'])) {
230 list($defaults['end_date']) = CRM_Utils_Date
::setDateDefaults($this->_values
['end_date']);
232 $defaults['description'] = CRM_Utils_Array
::value('description', $this->_values
);
233 $defaults['is_active'] = CRM_Utils_Array
::value('is_active', $this->_values
);
235 // The javascript on the form will swap these fields if it is a b_a relationship, so we compensate here
236 $defaults['is_permission_a_b'] = CRM_Utils_Array
::value('is_permission_' . $this->_rtype
, $this->_values
);
237 $defaults['is_permission_b_a'] = CRM_Utils_Array
::value('is_permission_' . strrev($this->_rtype
), $this->_values
);
239 $defaults['is_current_employer'] = $this->_isCurrentEmployer
;
241 // Load info about the related contact
242 $contact = new CRM_Contact_DAO_Contact();
243 if ($this->_rtype
== 'a_b' && $this->_values
['contact_id_a'] == $this->_contactId
) {
244 $contact->id
= $this->_values
['contact_id_b'];
247 $contact->id
= $this->_values
['contact_id_a'];
249 if ($contact->find(TRUE)) {
250 $defaults['related_contact_id'] = $contact->id
;
251 $this->_display_name_b
= $contact->display_name
;
252 $this->assign('display_name_b', $this->_display_name_b
);
256 'entity_id' => $this->_relationshipId
,
257 'entity_table' => 'civicrm_relationship',
261 $note = civicrm_api('Note', 'getsingle', $noteParams);
262 $defaults['note'] = CRM_Utils_Array
::value('note', $note);
266 $defaults['is_active'] = $defaults['is_current_employer'] = 1;
267 $defaults['relationship_type_id'] = $this->_rtypeId
;
270 $this->_enabled
= $defaults['is_active'];
275 * add the rules for form.
279 public function addRules() {
280 if ($this->_cdType
) {
284 if (!($this->_action
& CRM_Core_Action
::DELETE
)) {
285 $this->addFormRule(array('CRM_Contact_Form_Relationship', 'dateRule'));
290 * Build the form object
294 public function buildQuickForm() {
295 if ($this->_cdType
) {
296 return CRM_Custom_Form_CustomData
::buildQuickForm($this);
299 if ($this->_action
& CRM_Core_Action
::DELETE
) {
300 $this->addButtons(array(
303 'name' => ts('Delete'),
308 'name' => ts('Cancel'),
314 // Just in case custom data includes a rich text field
315 $this->assign('includeWysiwygEditor', TRUE);
318 $relationshipList = CRM_Contact_BAO_Relationship
::getContactRelationshipType($this->_contactId
, $this->_rtype
, $this->_relationshipId
);
320 // Metadata needed on clientside
321 $contactTypes = CRM_Contact_BAO_ContactType
::contactTypeInfo(TRUE);
323 // Get just what we need to keep the dom small
324 $whatWeWant = array_flip(array('contact_type_a', 'contact_type_b', 'contact_sub_type_a', 'contact_sub_type_b'));
325 foreach ($this->_allRelationshipNames
as $id => $vals) {
326 if ($vals['name_a_b'] === 'Employee of') {
327 $this->assign('employmentRelationship', $id);
329 if (isset($relationshipList["{$id}_a_b"]) ||
isset($relationshipList["{$id}_b_a"])) {
330 $jsData[$id] = array_filter(array_intersect_key($this->_allRelationshipNames
[$id], $whatWeWant));
331 // Add user-friendly placeholder
332 foreach (array('a', 'b') as $x) {
333 $type = !empty($jsData[$id]["contact_sub_type_$x"]) ?
$jsData[$id]["contact_sub_type_$x"] : CRM_Utils_Array
::value("contact_type_$x", $jsData[$id]);
334 $jsData[$id]["placeholder_$x"] = $type ?
ts('- select %1 -', array(strtolower($contactTypes[$type]['label']))) : ts('- select contact -');
338 $this->assign('relationshipData', $jsData);
342 'relationship_type_id',
343 ts('Relationship Type'),
344 array('' => ts('- select -')) +
$relationshipList,
346 array('class' => 'crm-select2 huge')
349 $label = $this->_action
& CRM_Core_Action
::ADD ?
ts('Contact(s)') : ts('Contact');
350 $contactField = $this->addEntityRef('related_contact_id', $label, array('multiple' => TRUE, 'create' => TRUE), TRUE);
351 // This field cannot be updated
352 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
353 $contactField->freeze();
356 $this->add('advcheckbox', 'is_current_employer', $this->_contactType
== 'Organization' ?
ts('Current Employee') : ts('Current Employer'));
358 $this->addDate('start_date', ts('Start Date'), FALSE, array('formatType' => 'searchDate'));
359 $this->addDate('end_date', ts('End Date'), FALSE, array('formatType' => 'searchDate'));
361 $this->add('advcheckbox', 'is_active', ts('Enabled?'));
363 // CRM-14612 - Don't use adv-checkbox as it interferes with the form js
364 $this->add('checkbox', 'is_permission_a_b');
365 $this->add('checkbox', 'is_permission_b_a');
367 $this->add('text', 'description', ts('Description'), CRM_Core_DAO
::getAttribute('CRM_Contact_DAO_Relationship', 'description'));
369 CRM_Contact_Form_Edit_Notes
::buildQuickForm($this);
371 if ($this->_action
& CRM_Core_Action
::VIEW
) {
372 $this->addButtons(array(
375 'name' => ts('Done'),
380 // make this form an upload since we don't know if the custom data injected dynamically is of type file etc.
381 $this->addButtons(array(
384 'name' => ts('Save Relationship'),
389 'name' => ts('Cancel'),
396 * This function is called when the form is submitted
401 public function postProcess() {
402 // store the submitted values in an array
403 $params = $this->controller
->exportValues($this->_name
);
405 // action is taken depending upon the mode
406 if ($this->_action
& CRM_Core_Action
::DELETE
) {
407 CRM_Contact_BAO_Relationship
::del($this->_relationshipId
);
411 $ids = array('contact' => $this->_contactId
);
413 $relationshipTypeId = str_replace(array('_', 'a', 'b'), '', $params['relationship_type_id']);
415 // CRM-14612 - Don't use adv-checkbox as it interferes with the form js
416 $params['is_permission_a_b'] = CRM_Utils_Array
::value('is_permission_a_b', $params, 0);
417 $params['is_permission_b_a'] = CRM_Utils_Array
::value('is_permission_b_a', $params, 0);
419 // Update mode (always single)
420 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
421 $ids['relationship'] = $this->_relationshipId
;
422 $relation = CRM_Contact_BAO_Relationship
::getContactIds($this->_relationshipId
);
423 $ids['contactTarget'] = ($relation->contact_id_a
== $this->_contactId
) ?
$relation->contact_id_b
: $relation->contact_id_a
;
425 if ($this->_isCurrentEmployer
) {
426 // if relationship type changes, relationship is disabled, or "current employer" is unchecked,
427 // clear the current employer. CRM-3235.
428 $relChanged = $relationshipTypeId != $this->_values
['relationship_type_id'];
429 if (!$params['is_active'] ||
!$params['is_current_employer'] ||
$relChanged) {
430 CRM_Contact_BAO_Contact_Utils
::clearCurrentEmployer($this->_values
['contact_id_a']);
431 // Refresh contact summary if in ajax mode
432 $this->ajaxResponse
['reloadBlocks'] = array('#crm-contactinfo-content');
436 // Create mode (could be 1 or more relationships)
438 // Fill up this weird param with contact ids like the weird relationship bao expects
439 $params['contact_check'] = array_fill_keys(explode(',', $params['related_contact_id']), 1);
440 if (!$this->_rtype
) {
441 list(, $this->_rtype
) = explode('_', $params['relationship_type_id'], 2);
444 $params['start_date'] = CRM_Utils_Date
::processDate($params['start_date'], NULL, TRUE);
445 $params['end_date'] = CRM_Utils_Date
::processDate($params['end_date'], NULL, TRUE);
447 // Process custom data
448 $customFields = CRM_Core_BAO_CustomField
::getFields('Relationship', FALSE, FALSE, $relationshipTypeId);
449 $params['custom'] = CRM_Core_BAO_CustomField
::postProcess(
452 $this->_relationshipId
,
456 // Save relationships
457 list($valid, $invalid, $duplicate, $saved, $relationshipIds) = CRM_Contact_BAO_Relationship
::createMultiple($params, $ids);
459 // if this is called from case view,
460 //create an activity for case role removal.CRM-4480
461 if ($this->_caseId
) {
462 CRM_Case_BAO_Case
::createCaseRoleActivity($this->_caseId
, $relationshipIds, $params['contact_check'], $this->_contactId
);
466 CRM_Core_Session
::setStatus(ts('Relationship created.', array('count' => $valid, 'plural' => '%count relationships created.')), ts('Saved'), 'success');
469 CRM_Core_Session
::setStatus(ts('%count relationship record was not created due to an invalid contact type.', array('count' => $invalid, 'plural' => '%count relationship records were not created due to invalid contact types.')), ts('%count invalid relationship record', array('count' => $invalid, 'plural' => '%count invalid relationship records')));
472 CRM_Core_Session
::setStatus(ts('One relationship was not created because it already exists.', array('count' => $duplicate, 'plural' => '%count relationships were not created because they already exist.')), ts('%count duplicate relationship', array('count' => $duplicate, 'plural' => '%count duplicate relationships')));
475 CRM_Core_Session
::setStatus(ts('Relationship record has been updated.'), ts('Saved'), 'success');
479 if ($this->_action
& CRM_Core_Action
::UPDATE ||
$params['note']) {
480 foreach ($relationshipIds as $id) {
483 'entity_table' => 'civicrm_relationship',
485 $existing = civicrm_api3('note', 'get', $noteParams);
486 if (!empty($existing['id'])) {
487 $noteParams['id'] = $existing['id'];
489 $noteParams['note'] = $params['note'];
490 $noteParams['contact_id'] = $this->_contactId
;
491 if (!empty($existing['id']) ||
$params['note']) {
492 $action = $params['note'] ?
'create' : 'delete';
493 civicrm_api3('note', $action, $noteParams);
498 // Membership for related contacts CRM-1657
499 if (CRM_Core_Permission
::access('CiviMember') && (!$duplicate)) {
500 $params['relationship_ids'] = $relationshipIds;
501 if ($this->_action
& CRM_Core_Action
::ADD
) {
502 CRM_Contact_BAO_Relationship
::relatedMemberships($this->_contactId
,
507 elseif ($this->_action
& CRM_Core_Action
::UPDATE
) {
509 //only if the relationship has been toggled to enable /disable
510 if (CRM_Utils_Array
::value('is_active', $params) != $this->_enabled
) {
511 $active = !empty($params['is_active']) ? CRM_Core_Action
::ENABLE
: CRM_Core_Action
::DISABLE
;
512 CRM_Contact_BAO_Relationship
::disableEnableRelationship($this->_relationshipId
, $active);
515 // Refresh contact tabs which might have been affected
516 $this->ajaxResponse
['updateTabs'] = array(
517 '#tab_member' => CRM_Contact_BAO_Contact
::getCountComponent('membership', $this->_contactId
),
518 '#tab_contribute' => CRM_Contact_BAO_Contact
::getCountComponent('contribution', $this->_contactId
),
521 // Set current employee/employer relationship, CRM-3532
522 if ($params['is_current_employer'] && $this->_allRelationshipNames
[$relationshipTypeId]["name_a_b"] == 'Employee of') {
523 $employerParams = array();
524 foreach ($relationshipIds as $id) {
525 // Fixme this is dumb why do we have to look this up again?
526 $rel = CRM_Contact_BAO_Relationship
::getContactIds($id);
527 $employerParams[$rel->contact_id_a
] = $rel->contact_id_b
;
529 CRM_Contact_BAO_Contact_Utils
::setCurrentEmployer($employerParams);
530 // Refresh contact summary if in ajax mode
531 $this->ajaxResponse
['reloadBlocks'] = array('#crm-contactinfo-content');
538 * @param array $params
539 * (reference ) an assoc array of name/value pairs.
541 * @return mixed true or array of errors
544 public static function dateRule($params) {
547 // check start and end date
548 if (!empty($params['start_date']) && !empty($params['end_date'])) {
549 $start_date = CRM_Utils_Date
::format(CRM_Utils_Array
::value('start_date', $params));
550 $end_date = CRM_Utils_Date
::format(CRM_Utils_Array
::value('end_date', $params));
551 if ($start_date && $end_date && (int ) $end_date < (int ) $start_date) {
552 $errors['end_date'] = ts('The relationship end date cannot be prior to the start date.');
556 return empty($errors) ?
TRUE : $errors;