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