Merge pull request #1625 from pradpnayak/CRM-13340
[civicrm-core.git] / CRM / Contact / Form / Relationship.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2013
32 * $Id$
33 *
34 */
35
36 /**
37 * This class generates form components for relationship
38 *
39 */
40 class CRM_Contact_Form_Relationship extends CRM_Core_Form {
41
42 /**
43 * max number of contacts we will display for a relationship
44 */
45 CONST MAX_RELATIONSHIPS = 50;
46
47 /**
48 * The relationship id, used when editing the relationship
49 *
50 * @var int
51 */
52 protected $_relationshipId;
53
54 /**
55 * The contact id, used when add/edit relationship
56 *
57 * @var int
58 */
59 protected $_contactId;
60
61 /**
62 * This is a string which is either a_b or b_a used to determine the relationship between to contacts
63 *
64 */
65 protected $_rtype;
66
67 /**
68 * This is a string which is used to determine the relationship between to contacts
69 *
70 */
71 protected $_rtypeId;
72
73 /**
74 * Display name of contact a
75 *
76 */
77 protected $_display_name_a;
78
79 /**
80 * Display name of contact b
81 *
82 */
83 protected $_display_name_b;
84
85 /**
86 * The relationship type id
87 *
88 * @var int
89 */
90 protected $_relationshipTypeId;
91
92 /**
93 * an array of all relationship names
94 *
95 * @var array
96 */
97 protected $_allRelationshipNames;
98
99 /**
100 * The relationship values if Updating relationship
101 */
102 protected $_values;
103
104 /**
105 * casid if it called from case context
106 */
107 protected $_caseId;
108
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);
116 }
117
118 $this->_contactId = $this->get('contactId');
119
120 $this->_relationshipId = $this->get('id');
121
122 $this->_rtype = CRM_Utils_Request::retrieve('rtype', 'String', $this);
123
124 $this->_rtypeId = CRM_Utils_Request::retrieve('relTypeId', 'String', $this);
125
126 $this->_display_name_a = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactId, 'display_name');
127
128 $this->assign('sort_name_a', $this->_display_name_a);
129 CRM_Utils_System::setTitle(ts('Relationships for') . ' ' . $this->_display_name_a);
130
131 $this->_caseId = CRM_Utils_Request::retrieve('caseID', 'Integer', $this);
132
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);
138 }
139
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'];
144 }
145 elseif (!empty($this->_values)) {
146 $this->_rtypeId = $this->_values['relationship_type_id'] . '_' . $this->_rtype;
147 }
148 }
149
150 //get the relationship type id
151 $this->_relationshipTypeId = str_replace(array('_a_b', '_b_a'), array('', ''), $this->_rtypeId);
152
153 //get the relationship type
154 if (!$this->_rtype) {
155 $this->_rtype = str_replace($this->_relationshipTypeId . '_', '', $this->_rtypeId);
156 }
157 $this->assign('rtype', $this->_rtype);
158
159
160 //use name as it remain constant, CRM-3336
161 $this->_allRelationshipNames = CRM_Core_PseudoConstant::relationshipType('name');
162
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);
168 }
169 }
170
171 /**
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
174 *
175 * @access public
176 *
177 * @return None
178 */
179 function setDefaultValues() {
180 if ($this->_cdType) {
181 return CRM_Custom_Form_CustomData::setDefaultValues($this);
182 }
183
184 $defaults = array();
185
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']);
191 }
192 if (CRM_Utils_Array::value('end_date', $this->_values)) {
193 list($defaults['end_date']) = CRM_Utils_Date::setDateDefaults($this->_values['end_date']);
194 }
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'];
202 }
203 else {
204 $contact->id = $this->_values['contact_id_a'];
205 }
206 if ($contact->find(TRUE)) {
207 $this->_display_name_b = $contact->display_name;
208 $this->assign('sort_name_b', $this->_display_name_b);
209
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')
213 ) {
214 $defaults['is_current_employer'] = 1;
215 $this->_values['current_employee_id'] = $this->_contactId;
216 $this->_values['current_employer_id'] = $contact->id;
217 }
218 elseif ($this->_allRelationshipNames[$this->_relationshipTypeId]["name_{$this->_rtype}"] == 'Employer of' &&
219 $this->_contactId == $contact->employer_id
220 ) {
221 $defaults['is_current_employer'] = 1;
222 $this->_values['current_employee_id'] = $contact->id;
223 $this->_values['current_employer_id'] = $this->_contactId;
224 }
225 }
226
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();
230 $dao->query($query);
231 if ($dao->fetch($query)) {
232 $defaults['note'] = $dao->note;
233 }
234 }
235 }
236 else {
237 $defaults['is_active'] = 1;
238 $defaults['relationship_type_id'] = $this->_rtypeId;
239 }
240
241 $this->_enabled = $defaults['is_active'];
242 return $defaults;
243 }
244
245 /**
246 * This function is used to add the rules for form.
247 *
248 * @return None
249 * @access public
250 */
251 function addRules() {
252 if ($this->_cdType) {
253 return;
254 }
255
256 if (!($this->_action & CRM_Core_Action::DELETE)) {
257 $this->addRule('relationship_type_id', ts('Please select a relationship type.'), 'required');
258
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'));
264 }
265 elseif ($this->_action & CRM_Core_Action::UPDATE) {
266 $this->addFormRule(array('CRM_Contact_Form_Relationship', 'dateRule'));
267 }
268 }
269 }
270
271 /**
272 * Function to build the form
273 *
274 * @return None
275 * @access public
276 */
277 public function buildQuickForm() {
278 if ($this->_cdType) {
279 return CRM_Custom_Form_CustomData::buildQuickForm($this);
280 }
281
282 $relTypeID = explode('_', $this->_rtypeId, 3);
283
284 if ($this->_action & CRM_Core_Action::DELETE) {
285
286 $this->addButtons(array(
287 array(
288 'type' => 'next',
289 'name' => ts('Delete'),
290 'isDefault' => TRUE,
291 ),
292 array(
293 'type' => 'cancel',
294 'name' => ts('Cancel'),
295 ),
296 )
297 );
298 return;
299 }
300
301 $callAjax = $this->get('callAjax');
302
303 $searchRows = NULL;
304 if (!$callAjax) {
305 $searchRows = $this->get('searchRows');
306 }
307 else {
308
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;
313
314 $this->assign('searchCount', TRUE);
315
316 // To handle employee of and employer of
317 if (!empty($this->_relationshipTypeId) &&
318 !empty($this->_rtype)
319 ) {
320 $sourceUrl .= '&typeName=' . $this->_allRelationshipNames[$this->_relationshipTypeId]["name_{$this->_rtype}"];
321 }
322 $this->assign('sourceUrl', CRM_Utils_System::url('civicrm/ajax/relationshipcontacts', $sourceUrl, FALSE, NULL, FALSE));
323 }
324
325 $this->assign('callAjax', $callAjax);
326 $this->_callAjax = $callAjax;
327
328 $this->addElement('select',
329 'relationship_type_id',
330 ts('Relationship Type'),
331 array(
332 '' => ts('- select -')) +
333 CRM_Contact_BAO_Relationship::getContactRelationshipType($this->_contactId,
334 $this->_rtype,
335 $this->_relationshipId,
336 NULL, FALSE, 'label'
337 )
338 );
339
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);
344
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);
348
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);
351
352 $this->add('text', 'description', ts('Description'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Relationship', 'description'));
353
354 CRM_Contact_Form_Edit_Notes::buildQuickForm($this);
355
356 $searchCount = $this->get('searchCount');
357 $duplicateRelationship = $this->get('duplicateRelationship');
358 $searchDone = $this->get('searchDone');
359
360 $isEmployeeOf = $isEmployerOf = FALSE;
361 if (!empty($this->_relationshipTypeId) &&
362 !empty($this->_rtype)
363 ) {
364 if ($this->_allRelationshipNames[$this->_relationshipTypeId]["name_{$this->_rtype}"] == 'Employee of') {
365 $isEmployeeOf = TRUE;
366 }
367 elseif ($this->_allRelationshipNames[$this->_relationshipTypeId]["name_{$this->_rtype}"] == 'Employer of') {
368 $isEmployerOf = TRUE;
369 }
370 }
371
372 $employers = $checkBoxes = $employees = array();
373 if ($searchRows) {
374 foreach ($searchRows as $id => $row) {
375 $checkBoxes[$id] = $this->createElement('checkbox', $id, NULL, '');
376 if ($isEmployeeOf) {
377 $employers[$id] = $this->createElement('radio', NULL, $id, NULL, $id);
378 }
379 elseif ($isEmployerOf) {
380 $employees[$id] = $this->createElement('checkbox', $id, NULL, '');
381 }
382 }
383
384 $this->addGroup($checkBoxes, 'contact_check');
385 $this->assign('searchRows', $searchRows);
386 }
387
388 if ($isEmployeeOf) {
389 $this->assign('isEmployeeOf', $isEmployeeOf);
390 if (!$callAjax) {
391 $this->addGroup($employers, 'employee_of');
392 }
393 }
394 elseif ($isEmployerOf) {
395 $this->assign('isEmployerOf', $isEmployerOf);
396 if (!$callAjax) {
397 $this->addGroup($employees, 'employer_of');
398 }
399 }
400
401 if ($callAjax && ($isEmployeeOf || $isEmployerOf)) {
402 $this->addElement('hidden', 'store_employers', '', array('id' => 'store_employers'));
403 }
404
405 if ($this->_action & CRM_Core_Action::UPDATE) {
406 $this->addElement('checkbox', 'is_current_employer');
407 }
408
409 $this->assign('duplicateRelationship', $duplicateRelationship);
410 $this->assign('searchCount', $searchCount);
411 $this->assign('searchDone', $searchDone);
412
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);
417 }
418
419 if ($searchDone) {
420 $searchBtn = ts('Search Again');
421 }
422 else {
423 $searchBtn = ts('Search');
424 }
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'));
428
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);
432
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);
437
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(
440 // 'uploadNames' );
441 $buttonParams = array(
442 'type' => 'upload',
443 'name' => ts('Save Relationship'),
444 'isDefault' => TRUE,
445 );
446 if ($callAjax) {
447 $buttonParams['js'] = array('onclick' => ' submitAjaxData();');
448 }
449
450 $this->addButtons(array(
451 $buttonParams
452 ,
453 array(
454 'type' => 'cancel',
455 'name' => ts('Cancel'),
456 ),
457 )
458 );
459 }
460
461 /**
462 * This function is called when the form is submitted
463 *
464 * @access public
465 *
466 * @return None
467 */
468 public function postProcess() {
469 // store the submitted values in an array
470 $params = $this->controller->exportValues($this->_name);
471 $quickSave = FALSE;
472 if (CRM_Utils_Array::value('_qf_Relationship_refresh_save', $_POST) ||
473 CRM_Utils_Array::value('_qf_Relationship_refresh_savedetails', $_POST)
474 ) {
475 $quickSave = TRUE;
476 }
477
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])) {
482 if ($quickSave) {
483 $params['contact_check'] = array($params['contact_select_id'][1] => 1);
484 }
485 }
486 else {
487 $this->set('callAjax', TRUE);
488 $this->set('relType', $params['relationship_type_id']);
489 $this->set('relContact', $params['contact'][1]);
490 $quickSave = FALSE;
491 }
492 $this->set('searchDone', 1);
493 if (!$quickSave) {
494 return;
495 }
496 }
497
498 // action is taken depending upon the mode
499 $ids = array();
500 $ids['contact'] = $this->_contactId;
501
502 // modify params for ajax call
503 $this->modifyParams($params);
504
505 if ($this->_action & CRM_Core_Action::DELETE) {
506 CRM_Contact_BAO_Relationship::del($this->_relationshipId);
507 return;
508 }
509
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;
515
516 //if relationship type change and previously it was
517 //employer / emplyee relationship with current employer
518 //than clear the current employer. CRM-3235.
519
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);
522 $isDisabled = TRUE;
523 if (CRM_Utils_Array::value('is_active', $params)) {
524 $isDisabled = FALSE;
525 }
526 $relChanged = TRUE;
527 if ($relationshipTypeId == $this->_values['relationship_type_id']) {
528 $relChanged = FALSE;
529 }
530 if ($employerId && ($isDisabled || $relChanged)) {
531 CRM_Contact_BAO_Contact_Utils::clearCurrentEmployer($this->_values['current_employee_id']);
532 }
533
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);
539 }
540 elseif ($quickSave) {
541 if (CRM_Utils_Array::value('add_current_employee', $params) &&
542 $this->_allRelationshipNames[$relationshipTypeId]['name_a_b'] == 'Employee of'
543 ) {
544 $params['employee_of'] = $params['contact_select_id'][1];
545 }
546 elseif (CRM_Utils_Array::value('add_current_employer', $params) &&
547 $this->_allRelationshipNames[$relationshipTypeId]['name_b_a'] == 'Employer of'
548 ) {
549 $params['employer_of'] = array($params['contact_select_id'][1] => 1);
550 }
551 if (!$this->_rtype) {
552 $this->_rtype = str_replace($relationshipTypeId . '_', '', $params['relationship_type_id']);
553 }
554 }
555
556 if (!$params['note']) {
557 $params['note'] = 'null';
558 }
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);
561
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(
565 $params,
566 $customFields,
567 $this->_relationshipId,
568 'Relationship'
569 );
570
571 list($valid, $invalid, $duplicate, $saved, $relationshipIds) = CRM_Contact_BAO_Relationship::create($params, $ids);
572
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);
577 }
578
579 $status = '';
580 if ($valid) {
581 CRM_Core_Session::setStatus(ts('New relationship created.', array('count' => $valid, 'plural' => '%count new relationships created.')), ts('Saved'), 'success');
582 }
583 if ($invalid) {
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')));
585 }
586 if ($duplicate) {
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')));
588 }
589 if ($saved) {
590 CRM_Core_Session::setStatus(ts('Relationship record has been updated.'), ts('Saved'), 'success');
591 }
592
593 if (!empty($relationshipIds)) {
594 $note = new CRM_Core_DAO_Note();
595 $note->entity_id = $relationshipIds[0];
596 $note->entity_table = 'civicrm_relationship';
597 $noteIds = array();
598 if ($note->find(TRUE)) {
599 $id = $note->id;
600 $noteIds['id'] = $id;
601 }
602
603 $noteParams = array(
604 'entity_id' => $relationshipIds[0],
605 'entity_table' => 'civicrm_relationship',
606 'note' => $params['note'],
607 'contact_id' => $this->_contactId,
608 );
609 CRM_Core_BAO_Note::add($noteParams, $noteIds);
610
611 $params['relationship_ids'] = $relationshipIds;
612 }
613
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,
618 $params, $ids,
619 $this->_action
620 );
621 }
622 elseif ($this->_action & CRM_Core_Action::UPDATE) {
623 //fixes for CRM-7985
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);
628 }
629 }
630 }
631 //handle current employee/employer relationship, CRM-3532
632 if ($this->_allRelationshipNames[$relationshipTypeId]["name_{$this->_rtype}"] == 'Employee of') {
633 $orgId = NULL;
634 if (CRM_Utils_Array::value('employee_of', $params)) {
635 $orgId = $params['employee_of'];
636 }
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)
640 ) {
641 if (CRM_Utils_Array::value('contactTarget', $ids) !=
642 CRM_Utils_Array::value('current_employer_id', $this->_values)
643 ) {
644 $orgId = CRM_Utils_Array::value('contactTarget', $ids);
645 }
646 }
647 elseif (CRM_Utils_Array::value('contactTarget', $ids) ==
648 CRM_Utils_Array::value('current_employer_id', $this->_values)
649 ) {
650 //clear current employer.
651 CRM_Contact_BAO_Contact_Utils::clearCurrentEmployer($this->_contactId);
652 }
653 }
654
655 //set current employer
656 if ($orgId) {
657 $currentEmpParams[$this->_contactId] = $orgId;
658 CRM_Contact_BAO_Contact_Utils::setCurrentEmployer($currentEmpParams);
659 }
660 }
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']);
665 }
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)
670 ) {
671 $individualIds[] = CRM_Utils_Array::value('contactTarget', $ids);
672 }
673 }
674 elseif (CRM_Utils_Array::value('contactTarget', $ids) ==
675 CRM_Utils_Array::value('current_employee_id', $this->_values)
676 ) {
677 // clear current employee
678 CRM_Contact_BAO_Contact_Utils::clearCurrentEmployer($ids['contactTarget']);
679 }
680 }
681
682 //set current employee
683 if (!empty($individualIds)) {
684
685 //build the employee params.
686 foreach ($individualIds as $key => $Id) {
687 $currentEmpParams[$Id] = $this->_contactId;
688 }
689
690 CRM_Contact_BAO_Contact_Utils::setCurrentEmployer($currentEmpParams);
691 }
692 }
693
694 if ($quickSave) {
695 $session = CRM_Core_Session::singleton();
696 CRM_Utils_System::redirect($session->popUserContext());
697 }
698 }
699 //end of function
700
701 /**
702 * function for validation
703 *
704 * @param array $params (reference ) an assoc array of name/value pairs
705 *
706 * @return mixed true or array of errors
707 * @access public
708 * @static
709 */
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)
715 ) {
716 return TRUE;
717 }
718
719 $form->modifyParams($params);
720
721 $ids = array();
722 $session = CRM_Core_Session::singleton();
723 $ids['contact'] = $form->get('contactId');
724 $ids['relationship'] = $form->get('relationshipId');
725
726 $errors = array();
727 $employerId = NULL;
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);
731 if ($message) {
732 $errors['relationship_type_id'] = $message;
733 break;
734 }
735
736 if ($cid == CRM_Utils_Array::value('employee_of', $params)) {
737 $employerId = $cid;
738 }
739 }
740 }
741 else {
742 if ($form->_callAjax) {
743 $errors['store_contacts'] = ts('Select select at least one contact from Target Contact(s).');
744 }
745 else {
746 $errors['contact_check'] = ts('Please select at least one contact.');
747 }
748 }
749
750 if (CRM_Utils_Array::value('employee_of', $params) &&
751 !$employerId
752 ) {
753 if ($form->_callAjax) {
754 $errors['store_employer'] = ts('Current employer should be one of the selected contacts.');
755 }
756 else {
757 $errors['employee_of'] = ts('Current employer should be one of the selected contacts.');
758 }
759 }
760
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']))
764 ) {
765 if ($form->_callAjax) {
766 $errors['store_employer'] = ts('Current employee should be among the selected contacts.');
767 }
768 else {
769 $errors['employer_of'] = ts('Current employee should be among the selected contacts.');
770 }
771 }
772
773 return empty($errors) ? TRUE : $errors;
774 }
775
776 /**
777 * function for date validation
778 *
779 * @param array $params (reference ) an assoc array of name/value pairs
780 *
781 * @return mixed true or array of errors
782 * @access public
783 * @static
784 */
785 static function dateRule($params) {
786 $errors = array();
787
788 // check start and end date
789 if (CRM_Utils_Array::value('start_date', $params) &&
790 CRM_Utils_Array::value('end_date', $params)
791 ) {
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.');
796 }
797 }
798
799 return empty($errors) ? TRUE : $errors;
800 }
801
802 function modifyParams(&$params) {
803 if (!$this->_callAjax) {
804 return;
805 }
806
807 if (CRM_Utils_Array::value('store_contacts', $params)) {
808 $storedContacts = array();
809 foreach (explode(',', $params['store_contacts']) as $value) {
810 if ($value) {
811 $storedContacts[$value] = 1;
812 }
813 }
814 $params['contact_check'] = $storedContacts;
815 }
816
817 if (CRM_Utils_Array::value('store_employers', $params)) {
818 $employeeContacts = array();
819 foreach (explode(',', $params['store_employers']) as $value) {
820 if ($value) {
821 $employeeContacts[$value] = $value;
822 }
823 }
824 if ($this->_allRelationshipNames[$this->_relationshipTypeId]["name_{$this->_rtype}"] == 'Employee of') {
825 $params['employee_of'] = current($employeeContacts);
826 }
827 else {
828 $params['employer_of'] = $employeeContacts;
829 }
830 }
831 }
832 }
833