Merge pull request #5097 from PalanteJon/CRM-15917
[civicrm-core.git] / CRM / Contact / Form / Relationship.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
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 * The relationship id, used when editing the relationship
44 *
45 * @var int
46 */
47 public $_relationshipId;
48
49 /**
50 * The contact id, used when add/edit relationship
51 *
52 * @var int
53 */
54 public $_contactId;
55
56 /**
57 * This is a string which is either a_b or b_a used to determine the relationship between to contacts
58 */
59 public $_rtype;
60
61 /**
62 * This is a string which is used to determine the relationship between to contacts
63 */
64 public $_rtypeId;
65
66 /**
67 * Display name of contact a
68 */
69 public $_display_name_a;
70
71 /**
72 * Display name of contact b
73 */
74 public $_display_name_b;
75
76 /**
77 * The relationship type id
78 *
79 * @var int
80 */
81 public $_relationshipTypeId;
82
83 /**
84 * An array of all relationship names
85 *
86 * @var array
87 */
88 public $_allRelationshipNames;
89
90 /**
91 * @var bool
92 */
93 public $_enabled;
94
95 /**
96 * @var bool
97 */
98 public $_isCurrentEmployer;
99
100 /**
101 * @var string
102 */
103 public $_contactType;
104
105 /**
106 * The relationship values if Updating relationship
107 */
108 public $_values;
109
110 /**
111 * Casid if it called from case context
112 */
113 public $_caseId;
114
115 /**
116 * @var mixed
117 */
118 public $_cdType;
119
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);
127 }
128
129 $this->_contactId = $this->get('contactId');
130
131 $this->_contactType = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactId, 'contact_type');
132
133 $this->_relationshipId = $this->get('id');
134
135 $this->_rtype = CRM_Utils_Request::retrieve('rtype', 'String', $this);
136
137 $this->_rtypeId = CRM_Utils_Request::retrieve('relTypeId', 'String', $this);
138
139 $this->_display_name_a = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactId, 'display_name');
140
141 $this->assign('display_name_a', $this->_display_name_a);
142
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)));
147 break;
148
149 case CRM_Core_Action::ADD:
150 CRM_Utils_System::setTitle(ts('Add Relationship for %1', array(1 => $this->_display_name_a)));
151 break;
152
153 case CRM_Core_Action::UPDATE:
154 CRM_Utils_System::setTitle(ts('Edit Relationship for %1', array(1 => $this->_display_name_a)));
155 break;
156
157 case CRM_Core_Action::DELETE:
158 CRM_Utils_System::setTitle(ts('Delete Relationship for %1', array(1 => $this->_display_name_a)));
159 break;
160 }
161
162 $this->_caseId = CRM_Utils_Request::retrieve('caseID', 'Integer', $this);
163
164 //get the relationship values.
165 $this->_values = array();
166 if ($this->_relationshipId) {
167 $params = array('id' => $this->_relationshipId);
168 CRM_Core_DAO::commonRetrieve('CRM_Contact_DAO_Relationship', $params, $this->_values);
169 }
170
171 if (!$this->_rtypeId) {
172 $params = $this->controller->exportValues($this->_name);
173 if (isset($params['relationship_type_id'])) {
174 $this->_rtypeId = $params['relationship_type_id'];
175 }
176 elseif (!empty($this->_values)) {
177 $this->_rtypeId = $this->_values['relationship_type_id'] . '_' . $this->_rtype;
178 }
179 }
180
181 //get the relationship type id
182 $this->_relationshipTypeId = str_replace(array('_a_b', '_b_a'), array('', ''), $this->_rtypeId);
183
184 //get the relationship type
185 if (!$this->_rtype) {
186 $this->_rtype = str_replace($this->_relationshipTypeId . '_', '', $this->_rtypeId);
187 }
188
189 //need to assign custom data type and subtype to the template - FIXME: explain why
190 $this->assign('customDataType', 'Relationship');
191 $this->assign('customDataSubType', $this->_relationshipTypeId);
192 $this->assign('entityID', $this->_relationshipId);
193
194 //use name as it remain constant, CRM-3336
195 $this->_allRelationshipNames = CRM_Core_PseudoConstant::relationshipType('name');
196
197 // Current employer?
198 if ($this->_action & CRM_Core_Action::UPDATE) {
199 if ($this->_allRelationshipNames[$this->_relationshipTypeId]["name_a_b"] == 'Employee of') {
200 $this->_isCurrentEmployer = $this->_values['contact_id_b'] == CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_values['contact_id_a'], 'employer_id');
201 }
202 }
203
204 // when custom data is included in this page
205 if (!empty($_POST['hidden_custom'])) {
206 CRM_Custom_Form_CustomData::preProcess($this);
207 CRM_Custom_Form_CustomData::buildQuickForm($this);
208 CRM_Custom_Form_CustomData::setDefaultValues($this);
209 }
210 }
211
212 /**
213 * Set default values for the form. Relationship that in edit/view mode
214 * the default values are retrieved from the database
215 *
216 *
217 * @return void
218 */
219 public function setDefaultValues() {
220 if ($this->_cdType) {
221 return CRM_Custom_Form_CustomData::setDefaultValues($this);
222 }
223
224 $defaults = array();
225
226 if ($this->_action & CRM_Core_Action::UPDATE) {
227 if (!empty($this->_values)) {
228 $defaults['relationship_type_id'] = $this->_rtypeId;
229 if (!empty($this->_values['start_date'])) {
230 list($defaults['start_date']) = CRM_Utils_Date::setDateDefaults($this->_values['start_date']);
231 }
232 if (!empty($this->_values['end_date'])) {
233 list($defaults['end_date']) = CRM_Utils_Date::setDateDefaults($this->_values['end_date']);
234 }
235 $defaults['description'] = CRM_Utils_Array::value('description', $this->_values);
236 $defaults['is_active'] = CRM_Utils_Array::value('is_active', $this->_values);
237
238 // The javascript on the form will swap these fields if it is a b_a relationship, so we compensate here
239 $defaults['is_permission_a_b'] = CRM_Utils_Array::value('is_permission_' . $this->_rtype, $this->_values);
240 $defaults['is_permission_b_a'] = CRM_Utils_Array::value('is_permission_' . strrev($this->_rtype), $this->_values);
241
242 $defaults['is_current_employer'] = $this->_isCurrentEmployer;
243
244 // Load info about the related contact
245 $contact = new CRM_Contact_DAO_Contact();
246 if ($this->_rtype == 'a_b' && $this->_values['contact_id_a'] == $this->_contactId) {
247 $contact->id = $this->_values['contact_id_b'];
248 }
249 else {
250 $contact->id = $this->_values['contact_id_a'];
251 }
252 if ($contact->find(TRUE)) {
253 $defaults['related_contact_id'] = $contact->id;
254 $this->_display_name_b = $contact->display_name;
255 $this->assign('display_name_b', $this->_display_name_b);
256 }
257
258 $noteParams = array(
259 'entity_id' => $this->_relationshipId,
260 'entity_table' => 'civicrm_relationship',
261 'limit' => 1,
262 'version' => 3,
263 );
264 $note = civicrm_api('Note', 'getsingle', $noteParams);
265 $defaults['note'] = CRM_Utils_Array::value('note', $note);
266 }
267 }
268 else {
269 $defaults['is_active'] = $defaults['is_current_employer'] = 1;
270 $defaults['relationship_type_id'] = $this->_rtypeId;
271 }
272
273 $this->_enabled = $defaults['is_active'];
274 return $defaults;
275 }
276
277 /**
278 * add the rules for form.
279 *
280 * @return void
281 */
282 public function addRules() {
283 if ($this->_cdType) {
284 return;
285 }
286
287 if (!($this->_action & CRM_Core_Action::DELETE)) {
288 $this->addFormRule(array('CRM_Contact_Form_Relationship', 'dateRule'));
289 }
290 }
291
292 /**
293 * Build the form object.
294 *
295 * @return void
296 */
297 public function buildQuickForm() {
298 if ($this->_cdType) {
299 return CRM_Custom_Form_CustomData::buildQuickForm($this);
300 }
301
302 if ($this->_action & CRM_Core_Action::DELETE) {
303 $this->addButtons(array(
304 array(
305 'type' => 'next',
306 'name' => ts('Delete'),
307 'isDefault' => TRUE,
308 ),
309 array(
310 'type' => 'cancel',
311 'name' => ts('Cancel'),
312 ),
313 )
314 );
315 return;
316 }
317 // Just in case custom data includes a rich text field
318 $this->assign('includeWysiwygEditor', TRUE);
319
320 // Select list
321 $relationshipList = CRM_Contact_BAO_Relationship::getContactRelationshipType($this->_contactId, $this->_rtype, $this->_relationshipId);
322
323 // Metadata needed on clientside
324 $contactTypes = CRM_Contact_BAO_ContactType::contactTypeInfo(TRUE);
325 $jsData = array();
326 // Get just what we need to keep the dom small
327 $whatWeWant = array_flip(array('contact_type_a', 'contact_type_b', 'contact_sub_type_a', 'contact_sub_type_b'));
328 foreach ($this->_allRelationshipNames as $id => $vals) {
329 if ($vals['name_a_b'] === 'Employee of') {
330 $this->assign('employmentRelationship', $id);
331 }
332 if (isset($relationshipList["{$id}_a_b"]) || isset($relationshipList["{$id}_b_a"])) {
333 $jsData[$id] = array_filter(array_intersect_key($this->_allRelationshipNames[$id], $whatWeWant));
334 // Add user-friendly placeholder
335 foreach (array('a', 'b') as $x) {
336 $type = !empty($jsData[$id]["contact_sub_type_$x"]) ? $jsData[$id]["contact_sub_type_$x"] : CRM_Utils_Array::value("contact_type_$x", $jsData[$id]);
337 $jsData[$id]["placeholder_$x"] = $type ? ts('- select %1 -', array(strtolower($contactTypes[$type]['label']))) : ts('- select contact -');
338 }
339 }
340 }
341 $this->assign('relationshipData', $jsData);
342
343 $this->add(
344 'select',
345 'relationship_type_id',
346 ts('Relationship Type'),
347 array('' => ts('- select -')) + $relationshipList,
348 TRUE,
349 array('class' => 'crm-select2 huge')
350 );
351
352 $label = $this->_action & CRM_Core_Action::ADD ? ts('Contact(s)') : ts('Contact');
353 $contactField = $this->addEntityRef('related_contact_id', $label, array(
354 'multiple' => TRUE,
355 'create' => TRUE,
356 ), TRUE);
357 // This field cannot be updated
358 if ($this->_action & CRM_Core_Action::UPDATE) {
359 $contactField->freeze();
360 }
361
362 $this->add('advcheckbox', 'is_current_employer', $this->_contactType == 'Organization' ? ts('Current Employee') : ts('Current Employer'));
363
364 $this->addDate('start_date', ts('Start Date'), FALSE, array('formatType' => 'searchDate'));
365 $this->addDate('end_date', ts('End Date'), FALSE, array('formatType' => 'searchDate'));
366
367 $this->add('advcheckbox', 'is_active', ts('Enabled?'));
368
369 // CRM-14612 - Don't use adv-checkbox as it interferes with the form js
370 $this->add('checkbox', 'is_permission_a_b');
371 $this->add('checkbox', 'is_permission_b_a');
372
373 $this->add('text', 'description', ts('Description'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Relationship', 'description'));
374
375 CRM_Contact_Form_Edit_Notes::buildQuickForm($this);
376
377 if ($this->_action & CRM_Core_Action::VIEW) {
378 $this->addButtons(array(
379 array(
380 'type' => 'cancel',
381 'name' => ts('Done'),
382 ),
383 ));
384 }
385 else {
386 // make this form an upload since we don't know if the custom data injected dynamically is of type file etc.
387 $this->addButtons(array(
388 array(
389 'type' => 'upload',
390 'name' => ts('Save Relationship'),
391 'isDefault' => TRUE,
392 ),
393 array(
394 'type' => 'cancel',
395 'name' => ts('Cancel'),
396 ),
397 ));
398 }
399 }
400
401 /**
402 * This function is called when the form is submitted.
403 *
404 *
405 * @return void
406 */
407 public function postProcess() {
408 // store the submitted values in an array
409 $params = $this->controller->exportValues($this->_name);
410
411 // action is taken depending upon the mode
412 if ($this->_action & CRM_Core_Action::DELETE) {
413 CRM_Contact_BAO_Relationship::del($this->_relationshipId);
414 return;
415 }
416
417 $ids = array('contact' => $this->_contactId);
418
419 $relationshipTypeId = str_replace(array('_', 'a', 'b'), '', $params['relationship_type_id']);
420
421 // CRM-14612 - Don't use adv-checkbox as it interferes with the form js
422 $params['is_permission_a_b'] = CRM_Utils_Array::value('is_permission_a_b', $params, 0);
423 $params['is_permission_b_a'] = CRM_Utils_Array::value('is_permission_b_a', $params, 0);
424
425 // Update mode (always single)
426 if ($this->_action & CRM_Core_Action::UPDATE) {
427 $ids['relationship'] = $this->_relationshipId;
428 $relation = CRM_Contact_BAO_Relationship::getContactIds($this->_relationshipId);
429 $ids['contactTarget'] = ($relation->contact_id_a == $this->_contactId) ? $relation->contact_id_b : $relation->contact_id_a;
430
431 if ($this->_isCurrentEmployer) {
432 // if relationship type changes, relationship is disabled, or "current employer" is unchecked,
433 // clear the current employer. CRM-3235.
434 $relChanged = $relationshipTypeId != $this->_values['relationship_type_id'];
435 if (!$params['is_active'] || !$params['is_current_employer'] || $relChanged) {
436 CRM_Contact_BAO_Contact_Utils::clearCurrentEmployer($this->_values['contact_id_a']);
437 // Refresh contact summary if in ajax mode
438 $this->ajaxResponse['reloadBlocks'] = array('#crm-contactinfo-content');
439 }
440 }
441 }
442 // Create mode (could be 1 or more relationships)
443 else {
444 // Fill up this weird param with contact ids like the weird relationship bao expects
445 $params['contact_check'] = array_fill_keys(explode(',', $params['related_contact_id']), 1);
446 if (!$this->_rtype) {
447 list(, $this->_rtype) = explode('_', $params['relationship_type_id'], 2);
448 }
449 }
450 $params['start_date'] = CRM_Utils_Date::processDate($params['start_date'], NULL, TRUE);
451 $params['end_date'] = CRM_Utils_Date::processDate($params['end_date'], NULL, TRUE);
452
453 // Process custom data
454 $customFields = CRM_Core_BAO_CustomField::getFields('Relationship', FALSE, FALSE, $relationshipTypeId);
455 $params['custom'] = CRM_Core_BAO_CustomField::postProcess(
456 $params,
457 $customFields,
458 $this->_relationshipId,
459 'Relationship'
460 );
461
462 // Save relationships
463 list($valid, $invalid, $duplicate, $saved, $relationshipIds) = CRM_Contact_BAO_Relationship::createMultiple($params, $ids);
464
465 // if this is called from case view,
466 //create an activity for case role removal.CRM-4480
467 if ($this->_caseId) {
468 CRM_Case_BAO_Case::createCaseRoleActivity($this->_caseId, $relationshipIds, $params['contact_check'], $this->_contactId);
469 }
470
471 if ($valid) {
472 CRM_Core_Session::setStatus(ts('Relationship created.', array(
473 'count' => $valid,
474 'plural' => '%count relationships created.',
475 )), ts('Saved'), 'success');
476 }
477 if ($invalid) {
478 CRM_Core_Session::setStatus(ts('%count relationship record was not created due to an invalid contact type.', array(
479 'count' => $invalid,
480 'plural' => '%count relationship records were not created due to invalid contact types.',
481 )), ts('%count invalid relationship record', array(
482 'count' => $invalid,
483 'plural' => '%count invalid relationship records',
484 )));
485 }
486 if ($duplicate) {
487 CRM_Core_Session::setStatus(ts('One relationship was not created because it already exists.', array(
488 'count' => $duplicate,
489 'plural' => '%count relationships were not created because they already exist.',
490 )), ts('%count duplicate relationship', array(
491 'count' => $duplicate,
492 'plural' => '%count duplicate relationships',
493 )));
494 }
495 if ($saved) {
496 CRM_Core_Session::setStatus(ts('Relationship record has been updated.'), ts('Saved'), 'success');
497 }
498
499 // Save notes
500 if ($this->_action & CRM_Core_Action::UPDATE || $params['note']) {
501 foreach ($relationshipIds as $id) {
502 $noteParams = array(
503 'entity_id' => $id,
504 'entity_table' => 'civicrm_relationship',
505 );
506 $existing = civicrm_api3('note', 'get', $noteParams);
507 if (!empty($existing['id'])) {
508 $noteParams['id'] = $existing['id'];
509 }
510 $noteParams['note'] = $params['note'];
511 $noteParams['contact_id'] = $this->_contactId;
512 if (!empty($existing['id']) || $params['note']) {
513 $action = $params['note'] ? 'create' : 'delete';
514 civicrm_api3('note', $action, $noteParams);
515 }
516 }
517 }
518
519 // Membership for related contacts CRM-1657
520 if (CRM_Core_Permission::access('CiviMember') && (!$duplicate)) {
521 $params['relationship_ids'] = $relationshipIds;
522 if ($this->_action & CRM_Core_Action::ADD) {
523 CRM_Contact_BAO_Relationship::relatedMemberships($this->_contactId,
524 $params, $ids,
525 $this->_action
526 );
527 }
528 elseif ($this->_action & CRM_Core_Action::UPDATE) {
529 //fixes for CRM-7985
530 //only if the relationship has been toggled to enable /disable
531 if (CRM_Utils_Array::value('is_active', $params) != $this->_enabled) {
532 $active = !empty($params['is_active']) ? CRM_Core_Action::ENABLE : CRM_Core_Action::DISABLE;
533 CRM_Contact_BAO_Relationship::disableEnableRelationship($this->_relationshipId, $active);
534 }
535 }
536 // Refresh contact tabs which might have been affected
537 $this->ajaxResponse['updateTabs'] = array(
538 '#tab_member' => CRM_Contact_BAO_Contact::getCountComponent('membership', $this->_contactId),
539 '#tab_contribute' => CRM_Contact_BAO_Contact::getCountComponent('contribution', $this->_contactId),
540 );
541 }
542 // Set current employee/employer relationship, CRM-3532
543 if ($params['is_current_employer'] && $this->_allRelationshipNames[$relationshipTypeId]["name_a_b"] == 'Employee of') {
544 $employerParams = array();
545 foreach ($relationshipIds as $id) {
546 // Fixme this is dumb why do we have to look this up again?
547 $rel = CRM_Contact_BAO_Relationship::getContactIds($id);
548 $employerParams[$rel->contact_id_a] = $rel->contact_id_b;
549 }
550 CRM_Contact_BAO_Contact_Utils::setCurrentEmployer($employerParams);
551 // Refresh contact summary if in ajax mode
552 $this->ajaxResponse['reloadBlocks'] = array('#crm-contactinfo-content');
553 }
554 }
555
556 /**
557 * Date validation.
558 *
559 * @param array $params
560 * (reference ) an assoc array of name/value pairs.
561 *
562 * @return bool|array
563 * mixed true or array of errors
564 */
565 public static function dateRule($params) {
566 $errors = array();
567
568 // check start and end date
569 if (!empty($params['start_date']) && !empty($params['end_date'])) {
570 $start_date = CRM_Utils_Date::format(CRM_Utils_Array::value('start_date', $params));
571 $end_date = CRM_Utils_Date::format(CRM_Utils_Array::value('end_date', $params));
572 if ($start_date && $end_date && (int ) $end_date < (int ) $start_date) {
573 $errors['end_date'] = ts('The relationship end date cannot be prior to the start date.');
574 }
575 }
576
577 return empty($errors) ? TRUE : $errors;
578 }
579
580 }