3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
37 * This class generates form components for relationship
40 class CRM_Contact_Form_Relationship
extends CRM_Core_Form
{
43 * max number of contacts we will display for a relationship
45 CONST MAX_RELATIONSHIPS
= 50;
48 * The relationship id, used when editing the relationship
52 protected $_relationshipId;
55 * The contact id, used when add/edit relationship
59 protected $_contactId;
62 * This is a string which is either a_b or b_a used to determine the relationship between to contacts
68 * This is a string which is used to determine the relationship between to contacts
74 * Display name of contact a
77 protected $_display_name_a;
80 * Display name of contact b
83 protected $_display_name_b;
86 * The relationship type id
90 protected $_relationshipTypeId;
93 * an array of all relationship names
97 protected $_allRelationshipNames;
100 * The relationship values if Updating relationship
105 * casid if it called from case context
109 function preProcess() {
110 //custom data related code
111 $this->_cdType
= CRM_Utils_Array
::value('type', $_GET);
112 $this->assign('cdType', FALSE);
113 if ($this->_cdType
) {
114 $this->assign('cdType', TRUE);
115 return CRM_Custom_Form_CustomData
::preProcess($this);
118 $this->_contactId
= $this->get('contactId');
120 $this->_relationshipId
= $this->get('id');
122 $this->_rtype
= CRM_Utils_Request
::retrieve('rtype', 'String', $this);
124 $this->_rtypeId
= CRM_Utils_Request
::retrieve('relTypeId', 'String', $this);
126 $this->_display_name_a
= CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactId
, 'display_name');
128 $this->assign('sort_name_a', $this->_display_name_a
);
129 CRM_Utils_System
::setTitle(ts('Relationships for') . ' ' . $this->_display_name_a
);
131 $this->_caseId
= CRM_Utils_Request
::retrieve('caseID', 'Integer', $this);
133 //get the relationship values.
134 $this->_values
= array();
135 if ($this->_relationshipId
) {
136 $params = array('id' => $this->_relationshipId
);
137 CRM_Core_DAO
::commonRetrieve('CRM_Contact_DAO_Relationship', $params, $this->_values
);
140 if (!$this->_rtypeId
) {
141 $params = $this->controller
->exportValues($this->_name
);
142 if (isset($params['relationship_type_id'])) {
143 $this->_rtypeId
= $params['relationship_type_id'];
145 elseif (!empty($this->_values
)) {
146 $this->_rtypeId
= $this->_values
['relationship_type_id'] . '_' . $this->_rtype
;
150 //get the relationship type id
151 $this->_relationshipTypeId
= str_replace(array('_a_b', '_b_a'), array('', ''), $this->_rtypeId
);
153 //get the relationship type
154 if (!$this->_rtype
) {
155 $this->_rtype
= str_replace($this->_relationshipTypeId
. '_', '', $this->_rtypeId
);
157 $this->assign('rtype', $this->_rtype
);
160 //use name as it remain constant, CRM-3336
161 $this->_allRelationshipNames
= CRM_Core_PseudoConstant
::relationshipType('name');
163 // when custom data is included in this page
164 if (CRM_Utils_Array
::value('hidden_custom', $_POST)) {
165 CRM_Custom_Form_CustomData
::preProcess($this);
166 CRM_Custom_Form_CustomData
::buildQuickForm($this);
167 CRM_Custom_Form_CustomData
::setDefaultValues($this);
172 * This function sets the default values for the form. Relationship that in edit/view mode
173 * the default values are retrieved from the database
179 function setDefaultValues() {
180 if ($this->_cdType
) {
181 return CRM_Custom_Form_CustomData
::setDefaultValues($this);
186 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
187 if (!empty($this->_values
)) {
188 $defaults['relationship_type_id'] = $this->_rtypeId
;
189 if (CRM_Utils_Array
::value('start_date', $this->_values
)) {
190 list($defaults['start_date']) = CRM_Utils_Date
::setDateDefaults($this->_values
['start_date']);
192 if (CRM_Utils_Array
::value('end_date', $this->_values
)) {
193 list($defaults['end_date']) = CRM_Utils_Date
::setDateDefaults($this->_values
['end_date']);
195 $defaults['description'] = CRM_Utils_Array
::value('description', $this->_values
);
196 $defaults['is_active'] = CRM_Utils_Array
::value('is_active', $this->_values
);
197 $defaults['is_permission_a_b'] = CRM_Utils_Array
::value('is_permission_a_b', $this->_values
);
198 $defaults['is_permission_b_a'] = CRM_Utils_Array
::value('is_permission_b_a', $this->_values
);
199 $contact = new CRM_Contact_DAO_Contact();
200 if ($this->_rtype
== 'a_b' && $this->_values
['contact_id_a'] == $this->_contactId
) {
201 $contact->id
= $this->_values
['contact_id_b'];
204 $contact->id
= $this->_values
['contact_id_a'];
206 if ($contact->find(TRUE)) {
207 $this->_display_name_b
= $contact->display_name
;
208 $this->assign('sort_name_b', $this->_display_name_b
);
210 //is current employee/employer.
211 if ($this->_allRelationshipNames
[$this->_relationshipTypeId
]["name_{$this->_rtype}"] == 'Employee of' &&
212 $contact->id
== CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactId
, 'employer_id')
214 $defaults['is_current_employer'] = 1;
215 $this->_values
['current_employee_id'] = $this->_contactId
;
216 $this->_values
['current_employer_id'] = $contact->id
;
218 elseif ($this->_allRelationshipNames
[$this->_relationshipTypeId
]["name_{$this->_rtype}"] == 'Employer of' &&
219 $this->_contactId
== $contact->employer_id
221 $defaults['is_current_employer'] = 1;
222 $this->_values
['current_employee_id'] = $contact->id
;
223 $this->_values
['current_employer_id'] = $this->_contactId
;
227 $relationshipID = $this->_values
['id'];
228 $query = "SELECT id, note FROM civicrm_note where entity_table = 'civicrm_relationship' and entity_id = $relationshipID order by modified_date desc";
229 $dao = new CRM_Core_DAO();
231 if ($dao->fetch($query)) {
232 $defaults['note'] = $dao->note
;
237 $defaults['is_active'] = 1;
238 $defaults['relationship_type_id'] = $this->_rtypeId
;
241 $this->_enabled
= $defaults['is_active'];
246 * This function is used to add the rules for form.
251 function addRules() {
252 if ($this->_cdType
) {
256 if (!($this->_action
& CRM_Core_Action
::DELETE
)) {
257 $this->addRule('relationship_type_id', ts('Please select a relationship type.'), 'required');
259 // add a form rule only when creating a new relationship
260 // edit is severely limited, so add a simpleer form rule
261 if ($this->_action
& CRM_Core_Action
::ADD
) {
262 $this->addFormRule(array('CRM_Contact_Form_Relationship', 'formRule'), $this);
263 $this->addFormRule(array('CRM_Contact_Form_Relationship', 'dateRule'));
265 elseif ($this->_action
& CRM_Core_Action
::UPDATE
) {
266 $this->addFormRule(array('CRM_Contact_Form_Relationship', 'dateRule'));
272 * Function to build the form
277 public function buildQuickForm() {
278 if ($this->_cdType
) {
279 return CRM_Custom_Form_CustomData
::buildQuickForm($this);
282 $relTypeID = explode('_', $this->_rtypeId
, 3);
284 if ($this->_action
& CRM_Core_Action
::DELETE
) {
286 $this->addButtons(array(
289 'name' => ts('Delete'),
294 'name' => ts('Cancel'),
301 $callAjax = $this->get('callAjax');
305 $searchRows = $this->get('searchRows');
309 $this->addElement('hidden', 'store_contacts', '', array('id' => 'store_contacts'));
310 $sourceUrl = 'snippet=4&relType=' . $this->get('relType');
311 $sourceUrl .= '&relContact=' . $this->get('relContact');
312 $sourceUrl .= '&cid=' . $this->_contactId
;
314 $this->assign('searchCount', TRUE);
316 // To handle employee of and employer of
317 if (!empty($this->_relationshipTypeId
) &&
318 !empty($this->_rtype
)
320 $sourceUrl .= '&typeName=' . $this->_allRelationshipNames
[$this->_relationshipTypeId
]["name_{$this->_rtype}"];
322 $this->assign('sourceUrl', CRM_Utils_System
::url('civicrm/ajax/relationshipcontacts', $sourceUrl, FALSE, NULL, FALSE));
325 $this->assign('callAjax', $callAjax);
326 $this->_callAjax
= $callAjax;
328 $this->addElement('select',
329 'relationship_type_id',
330 ts('Relationship Type'),
332 '' => ts('- select -')) +
333 CRM_Contact_BAO_Relationship
::getContactRelationshipType($this->_contactId
,
335 $this->_relationshipId
,
340 // add a ajax facility for searching contacts
341 $dataUrl = CRM_Utils_System
::url('civicrm/ajax/search', 'reset=1', TRUE, NULL, FALSE);
342 $this->assign('dataUrl', $dataUrl);
343 CRM_Contact_Form_NewContact
::buildQuickForm($this);
345 $this->addDate('start_date', ts('Start Date'), FALSE, array('formatType' => 'searchDate'));
346 $this->addDate('end_date', ts('End Date'), FALSE, array('formatType' => 'searchDate'));
347 $this->addElement('checkbox', 'is_active', ts('Enabled?'), NULL, NULL);
349 $this->addElement('checkbox', 'is_permission_a_b', ts('Permission for contact a to view and update information for contact b'), NULL);
350 $this->addElement('checkbox', 'is_permission_b_a', ts('permission for contact b to view and update information for contact a'), NULL);
352 $this->add('text', 'description', ts('Description'), CRM_Core_DAO
::getAttribute('CRM_Contact_DAO_Relationship', 'description'));
354 CRM_Contact_Form_Edit_Notes
::buildQuickForm($this);
356 $searchCount = $this->get('searchCount');
357 $duplicateRelationship = $this->get('duplicateRelationship');
358 $searchDone = $this->get('searchDone');
360 $isEmployeeOf = $isEmployerOf = FALSE;
361 if (!empty($this->_relationshipTypeId
) &&
362 !empty($this->_rtype
)
364 if ($this->_allRelationshipNames
[$this->_relationshipTypeId
]["name_{$this->_rtype}"] == 'Employee of') {
365 $isEmployeeOf = TRUE;
367 elseif ($this->_allRelationshipNames
[$this->_relationshipTypeId
]["name_{$this->_rtype}"] == 'Employer of') {
368 $isEmployerOf = TRUE;
372 $employers = $checkBoxes = $employees = array();
374 foreach ($searchRows as $id => $row) {
375 $checkBoxes[$id] = $this->createElement('checkbox', $id, NULL, '');
377 $employers[$id] = $this->createElement('radio', NULL, $id, NULL, $id);
379 elseif ($isEmployerOf) {
380 $employees[$id] = $this->createElement('checkbox', $id, NULL, '');
384 $this->addGroup($checkBoxes, 'contact_check');
385 $this->assign('searchRows', $searchRows);
389 $this->assign('isEmployeeOf', $isEmployeeOf);
391 $this->addGroup($employers, 'employee_of');
394 elseif ($isEmployerOf) {
395 $this->assign('isEmployerOf', $isEmployerOf);
397 $this->addGroup($employees, 'employer_of');
401 if ($callAjax && ($isEmployeeOf ||
$isEmployerOf)) {
402 $this->addElement('hidden', 'store_employers', '', array('id' => 'store_employers'));
405 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
406 $this->addElement('checkbox', 'is_current_employer');
409 $this->assign('duplicateRelationship', $duplicateRelationship);
410 $this->assign('searchCount', $searchCount);
411 $this->assign('searchDone', $searchDone);
413 if ($this->get('contact_type')) {
414 $typeLabel = CRM_Contact_BAO_ContactType
::getLabel($this->get('contact_type'));
415 $this->assign('contact_type', $this->get('contact_type'));
416 $this->assign('contact_type_display', $typeLabel);
420 $searchBtn = ts('Search Again');
423 $searchBtn = ts('Search');
425 $this->addElement('submit', $this->getButtonName('refresh'), $searchBtn, array('class' => 'form-submit', 'id' => 'search-button'));
426 $this->addElement('submit', $this->getButtonName('refresh', 'save'), 'Quick Save', array('class' => 'form-submit', 'id' => 'quick-save'));
427 $this->addElement('submit', $this->getButtonName('cancel'), ts('Cancel'), array('class' => 'form-submit'));
429 $this->addElement('submit', $this->getButtonName('refresh', 'savedetails'), 'Save Relationship', array('class' => 'form-submit hiddenElement', 'id' => 'details-save'));
430 $this->addElement('checkbox', 'add_current_employer', ts('Current Employer'), NULL);
431 $this->addElement('checkbox', 'add_current_employee', ts('Current Employee'), NULL);
433 //need to assign custom data type and subtype to the template
434 $this->assign('customDataType', 'Relationship');
435 $this->assign('customDataSubType', $this->_relationshipTypeId
);
436 $this->assign('entityID', $this->_relationshipId
);
438 // make this form an upload since we dont know if the custom data injected dynamically
439 // is of type file etc $uploadNames = $this->get(
441 $buttonParams = array(
443 'name' => ts('Save Relationship'),
447 $buttonParams['js'] = array('onclick' => ' submitAjaxData();');
450 $this->addButtons(array(
455 'name' => ts('Cancel'),
462 * This function is called when the form is submitted
468 public function postProcess() {
469 // store the submitted values in an array
470 $params = $this->controller
->exportValues($this->_name
);
472 if (CRM_Utils_Array
::value('_qf_Relationship_refresh_save', $_POST) ||
473 CRM_Utils_Array
::value('_qf_Relationship_refresh_savedetails', $_POST)
478 $this->set('searchDone', 0);
479 $this->set('callAjax', FALSE);
480 if (CRM_Utils_Array
::value('_qf_Relationship_refresh', $_POST) ||
$quickSave) {
481 if (is_numeric($params['contact_select_id'][1])) {
483 $params['contact_check'] = array($params['contact_select_id'][1] => 1);
487 $this->set('callAjax', TRUE);
488 $this->set('relType', $params['relationship_type_id']);
489 $this->set('relContact', $params['contact'][1]);
492 $this->set('searchDone', 1);
498 // action is taken depending upon the mode
500 $ids['contact'] = $this->_contactId
;
502 // modify params for ajax call
503 $this->modifyParams($params);
505 if ($this->_action
& CRM_Core_Action
::DELETE
) {
506 CRM_Contact_BAO_Relationship
::del($this->_relationshipId
);
510 $relationshipTypeId = str_replace(array('_a_b', '_b_a'), array('', ''), $params['relationship_type_id']);
511 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
512 $ids['relationship'] = $this->_relationshipId
;
513 $relation = CRM_Contact_BAO_Relationship
::getContactIds($this->_relationshipId
);
514 $ids['contactTarget'] = ($relation->contact_id_a
== $this->_contactId
) ?
$relation->contact_id_b
: $relation->contact_id_a
;
516 //if relationship type change and previously it was
517 //employer / emplyee relationship with current employer
518 //than clear the current employer. CRM-3235.
520 //make sure we has to have employer id before firing queries, CRM-7306
521 $employerId = CRM_Utils_Array
::value('current_employee_id', $this->_values
);
523 if (CRM_Utils_Array
::value('is_active', $params)) {
527 if ($relationshipTypeId == $this->_values
['relationship_type_id']) {
530 if ($employerId && ($isDisabled ||
$relChanged)) {
531 CRM_Contact_BAO_Contact_Utils
::clearCurrentEmployer($this->_values
['current_employee_id']);
534 //if field key doesn't exists in params that means the user has unchecked checkbox,
535 //hence fill FALSE to params
536 $params['is_active'] = $isDisabled ?
FALSE : TRUE;
537 $params['is_permission_a_b'] = CRM_Utils_Array
::value('is_permission_a_b', $params, FALSE);
538 $params['is_permission_b_a'] = CRM_Utils_Array
::value('is_permission_b_a', $params, FALSE);
540 elseif ($quickSave) {
541 if (CRM_Utils_Array
::value('add_current_employee', $params) &&
542 $this->_allRelationshipNames
[$relationshipTypeId]['name_a_b'] == 'Employee of'
544 $params['employee_of'] = $params['contact_select_id'][1];
546 elseif (CRM_Utils_Array
::value('add_current_employer', $params) &&
547 $this->_allRelationshipNames
[$relationshipTypeId]['name_b_a'] == 'Employer of'
549 $params['employer_of'] = array($params['contact_select_id'][1] => 1);
551 if (!$this->_rtype
) {
552 $this->_rtype
= str_replace($relationshipTypeId . '_', '', $params['relationship_type_id']);
556 if (!$params['note']) {
557 $params['note'] = 'null';
559 $params['start_date'] = CRM_Utils_Date
::processDate($params['start_date'], NULL, TRUE);
560 $params['end_date'] = CRM_Utils_Date
::processDate($params['end_date'], NULL, TRUE);
562 //special case to handle if all checkboxes are unchecked
563 $customFields = CRM_Core_BAO_CustomField
::getFields('Relationship', FALSE, FALSE, $relationshipTypeId);
564 $params['custom'] = CRM_Core_BAO_CustomField
::postProcess(
567 $this->_relationshipId
,
571 list($valid, $invalid, $duplicate, $saved, $relationshipIds) = CRM_Contact_BAO_Relationship
::create($params, $ids);
573 // if this is called from case view,
574 //create an activity for case role removal.CRM-4480
575 if ($this->_caseId
) {
576 CRM_Case_BAO_Case
::createCaseRoleActivity($this->_caseId
, $relationshipIds, $params['contact_check'], $this->_contactId
);
581 CRM_Core_Session
::setStatus(ts('New relationship created.', array('count' => $valid, 'plural' => '%count new relationships created.')), ts('Saved'), 'success');
584 CRM_Core_Session
::setStatus(ts('%count relationship record was not created due to an invalid target contact type.', array('count' => $invalid, 'plural' => '%count relationship records were not created due to invalid target contact types.')), ts('%count invalid relationship record', array('count' => $invalid, 'plural' => '%count invalid relationship records')));
587 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')));
590 CRM_Core_Session
::setStatus(ts('Relationship record has been updated.'), ts('Saved'), 'success');
593 if (!empty($relationshipIds)) {
594 $note = new CRM_Core_DAO_Note();
595 $note->entity_id
= $relationshipIds[0];
596 $note->entity_table
= 'civicrm_relationship';
598 if ($note->find(TRUE)) {
600 $noteIds['id'] = $id;
604 'entity_id' => $relationshipIds[0],
605 'entity_table' => 'civicrm_relationship',
606 'note' => $params['note'],
607 'contact_id' => $this->_contactId
,
609 CRM_Core_BAO_Note
::add($noteParams, $noteIds);
611 $params['relationship_ids'] = $relationshipIds;
614 // Membership for related contacts CRM-1657
615 if (CRM_Core_Permission
::access('CiviMember') && (!$duplicate)) {
616 if ($this->_action
& CRM_Core_Action
::ADD
) {
617 CRM_Contact_BAO_Relationship
::relatedMemberships($this->_contactId
,
622 elseif ($this->_action
& CRM_Core_Action
::UPDATE
) {
624 //only if the relationship has been toggled to enable /disable
625 if (CRM_Utils_Array
::value('is_active', $params) != $this->_enabled
) {
626 $active = CRM_Utils_Array
::value('is_active', $params) ? CRM_Core_Action
::ENABLE
: CRM_Core_Action
::DISABLE
;
627 CRM_Contact_BAO_Relationship
::disableEnableRelationship($this->_relationshipId
, $active);
631 //handle current employee/employer relationship, CRM-3532
632 if ($this->_allRelationshipNames
[$relationshipTypeId]["name_{$this->_rtype}"] == 'Employee of') {
634 if (CRM_Utils_Array
::value('employee_of', $params)) {
635 $orgId = $params['employee_of'];
637 elseif ($this->_action
& CRM_Core_Action
::UPDATE
) {
638 if (CRM_Utils_Array
::value('is_current_employer', $params) &&
639 CRM_Utils_Array
::value('is_active', $params)
641 if (CRM_Utils_Array
::value('contactTarget', $ids) !=
642 CRM_Utils_Array
::value('current_employer_id', $this->_values
)
644 $orgId = CRM_Utils_Array
::value('contactTarget', $ids);
647 elseif (CRM_Utils_Array
::value('contactTarget', $ids) ==
648 CRM_Utils_Array
::value('current_employer_id', $this->_values
)
650 //clear current employer.
651 CRM_Contact_BAO_Contact_Utils
::clearCurrentEmployer($this->_contactId
);
655 //set current employer
657 $currentEmpParams[$this->_contactId
] = $orgId;
658 CRM_Contact_BAO_Contact_Utils
::setCurrentEmployer($currentEmpParams);
661 elseif ($this->_allRelationshipNames
[$relationshipTypeId]["name_{$this->_rtype}"] == 'Employer of') {
662 $individualIds = array();
663 if (CRM_Utils_Array
::value('employer_of', $params)) {
664 $individualIds = array_keys($params['employer_of']);
666 elseif ($this->_action
& CRM_Core_Action
::UPDATE
) {
667 if (CRM_Utils_Array
::value('is_current_employer', $params)) {
668 if (CRM_Utils_Array
::value('contactTarget', $ids) !=
669 CRM_Utils_Array
::value('current_employee_id', $this->_values
)
671 $individualIds[] = CRM_Utils_Array
::value('contactTarget', $ids);
674 elseif (CRM_Utils_Array
::value('contactTarget', $ids) ==
675 CRM_Utils_Array
::value('current_employee_id', $this->_values
)
677 // clear current employee
678 CRM_Contact_BAO_Contact_Utils
::clearCurrentEmployer($ids['contactTarget']);
682 //set current employee
683 if (!empty($individualIds)) {
685 //build the employee params.
686 foreach ($individualIds as $key => $Id) {
687 $currentEmpParams[$Id] = $this->_contactId
;
690 CRM_Contact_BAO_Contact_Utils
::setCurrentEmployer($currentEmpParams);
695 $session = CRM_Core_Session
::singleton();
696 CRM_Utils_System
::redirect($session->popUserContext());
702 * function for validation
704 * @param array $params (reference ) an assoc array of name/value pairs
706 * @return mixed true or array of errors
710 static function formRule($params, $files, $form) {
711 // hack, no error check for refresh
712 if (CRM_Utils_Array
::value('_qf_Relationship_refresh', $_POST) ||
713 CRM_Utils_Array
::value('_qf_Relationship_refresh_save', $_POST) ||
714 CRM_Utils_Array
::value('_qf_Relationship_refresh_savedetails', $_POST)
719 $form->modifyParams($params);
722 $session = CRM_Core_Session
::singleton();
723 $ids['contact'] = $form->get('contactId');
724 $ids['relationship'] = $form->get('relationshipId');
728 if (CRM_Utils_Array
::value('contact_check', $params) && is_array($params['contact_check'])) {
729 foreach ($params['contact_check'] as $cid => $dontCare) {
730 $message = CRM_Contact_BAO_Relationship
::checkValidRelationship($params, $ids, $cid);
732 $errors['relationship_type_id'] = $message;
736 if ($cid == CRM_Utils_Array
::value('employee_of', $params)) {
742 if ($form->_callAjax
) {
743 $errors['store_contacts'] = ts('Select select at least one contact from Target Contact(s).');
746 $errors['contact_check'] = ts('Please select at least one contact.');
750 if (CRM_Utils_Array
::value('employee_of', $params) &&
753 if ($form->_callAjax
) {
754 $errors['store_employer'] = ts('Current employer should be one of the selected contacts.');
757 $errors['employee_of'] = ts('Current employer should be one of the selected contacts.');
761 if (CRM_Utils_Array
::value('employer_of', $params) &&
762 CRM_Utils_Array
::value('contact_check', $params) &&
763 array_diff(array_keys($params['employer_of']), array_keys($params['contact_check']))
765 if ($form->_callAjax
) {
766 $errors['store_employer'] = ts('Current employee should be among the selected contacts.');
769 $errors['employer_of'] = ts('Current employee should be among the selected contacts.');
773 return empty($errors) ?
TRUE : $errors;
777 * function for date validation
779 * @param array $params (reference ) an assoc array of name/value pairs
781 * @return mixed true or array of errors
785 static function dateRule($params) {
788 // check start and end date
789 if (CRM_Utils_Array
::value('start_date', $params) &&
790 CRM_Utils_Array
::value('end_date', $params)
792 $start_date = CRM_Utils_Date
::format(CRM_Utils_Array
::value('start_date', $params));
793 $end_date = CRM_Utils_Date
::format(CRM_Utils_Array
::value('end_date', $params));
794 if ($start_date && $end_date && (int ) $end_date < (int ) $start_date) {
795 $errors['end_date'] = ts('The relationship end date cannot be prior to the start date.');
799 return empty($errors) ?
TRUE : $errors;
802 function modifyParams(&$params) {
803 if (!$this->_callAjax
) {
807 if (CRM_Utils_Array
::value('store_contacts', $params)) {
808 $storedContacts = array();
809 foreach (explode(',', $params['store_contacts']) as $value) {
811 $storedContacts[$value] = 1;
814 $params['contact_check'] = $storedContacts;
817 if (CRM_Utils_Array
::value('store_employers', $params)) {
818 $employeeContacts = array();
819 foreach (explode(',', $params['store_employers']) as $value) {
821 $employeeContacts[$value] = $value;
824 if ($this->_allRelationshipNames
[$this->_relationshipTypeId
]["name_{$this->_rtype}"] == 'Employee of') {
825 $params['employee_of'] = current($employeeContacts);
828 $params['employer_of'] = $employeeContacts;