Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
62e492d4 | 29 | * This class generates form components for relationship. |
6a488035 TO |
30 | */ |
31 | class CRM_Contact_Form_Relationship extends CRM_Core_Form { | |
32 | ||
6a488035 TO |
33 | /** |
34 | * The relationship id, used when editing the relationship | |
35 | * | |
36 | * @var int | |
37 | */ | |
d97a2fc1 | 38 | public $_relationshipId; |
6a488035 TO |
39 | |
40 | /** | |
41 | * The contact id, used when add/edit relationship | |
42 | * | |
43 | * @var int | |
44 | */ | |
d97a2fc1 | 45 | public $_contactId; |
6a488035 TO |
46 | |
47 | /** | |
48 | * This is a string which is either a_b or b_a used to determine the relationship between to contacts | |
6a488035 | 49 | */ |
d97a2fc1 | 50 | public $_rtype; |
6a488035 TO |
51 | |
52 | /** | |
53 | * This is a string which is used to determine the relationship between to contacts | |
6a488035 | 54 | */ |
d97a2fc1 | 55 | public $_rtypeId; |
6a488035 TO |
56 | |
57 | /** | |
58 | * Display name of contact a | |
6a488035 | 59 | */ |
d97a2fc1 | 60 | public $_display_name_a; |
6a488035 TO |
61 | |
62 | /** | |
63 | * Display name of contact b | |
6a488035 | 64 | */ |
d97a2fc1 | 65 | public $_display_name_b; |
6a488035 TO |
66 | |
67 | /** | |
68 | * The relationship type id | |
69 | * | |
70 | * @var int | |
71 | */ | |
d97a2fc1 | 72 | public $_relationshipTypeId; |
6a488035 TO |
73 | |
74 | /** | |
100fef9d | 75 | * An array of all relationship names |
6a488035 TO |
76 | * |
77 | * @var array | |
78 | */ | |
d97a2fc1 | 79 | public $_allRelationshipNames; |
6a488035 | 80 | |
239dac31 CW |
81 | /** |
82 | * @var bool | |
83 | */ | |
d97a2fc1 | 84 | public $_enabled; |
239dac31 CW |
85 | |
86 | /** | |
87 | * @var bool | |
88 | */ | |
d97a2fc1 | 89 | public $_isCurrentEmployer; |
239dac31 CW |
90 | |
91 | /** | |
92 | * @var string | |
93 | */ | |
d97a2fc1 | 94 | public $_contactType; |
239dac31 | 95 | |
6a488035 TO |
96 | /** |
97 | * The relationship values if Updating relationship | |
98 | */ | |
d97a2fc1 | 99 | public $_values; |
6a488035 TO |
100 | |
101 | /** | |
62118c6d | 102 | * Case id if it called from case context |
6a488035 | 103 | */ |
d97a2fc1 | 104 | public $_caseId; |
6a488035 | 105 | |
239dac31 CW |
106 | /** |
107 | * @var mixed | |
108 | */ | |
109 | public $_cdType; | |
110 | ||
0efbca68 TM |
111 | /** |
112 | * Explicitly declare the form context. | |
113 | */ | |
114 | public function getDefaultContext() { | |
115 | return 'create'; | |
116 | } | |
117 | ||
118 | /** | |
119 | * Explicitly declare the entity api name. | |
120 | */ | |
121 | public function getDefaultEntity() { | |
122 | return 'Relationship'; | |
123 | } | |
124 | ||
00be9182 | 125 | public function preProcess() { |
6a488035 TO |
126 | //custom data related code |
127 | $this->_cdType = CRM_Utils_Array::value('type', $_GET); | |
128 | $this->assign('cdType', FALSE); | |
129 | if ($this->_cdType) { | |
130 | $this->assign('cdType', TRUE); | |
131 | return CRM_Custom_Form_CustomData::preProcess($this); | |
132 | } | |
133 | ||
134 | $this->_contactId = $this->get('contactId'); | |
135 | ||
239dac31 CW |
136 | $this->_contactType = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactId, 'contact_type'); |
137 | ||
6a488035 TO |
138 | $this->_relationshipId = $this->get('id'); |
139 | ||
140 | $this->_rtype = CRM_Utils_Request::retrieve('rtype', 'String', $this); | |
141 | ||
142 | $this->_rtypeId = CRM_Utils_Request::retrieve('relTypeId', 'String', $this); | |
143 | ||
144 | $this->_display_name_a = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactId, 'display_name'); | |
145 | ||
239dac31 CW |
146 | $this->assign('display_name_a', $this->_display_name_a); |
147 | ||
148 | // Set page title based on action | |
149 | switch ($this->_action) { | |
150 | case CRM_Core_Action::VIEW: | |
4dc79403 | 151 | CRM_Utils_System::setTitle(ts('View Relationship for %1', array(1 => $this->_display_name_a))); |
239dac31 | 152 | break; |
ea100cb5 | 153 | |
239dac31 | 154 | case CRM_Core_Action::ADD: |
4dc79403 | 155 | CRM_Utils_System::setTitle(ts('Add Relationship for %1', array(1 => $this->_display_name_a))); |
239dac31 | 156 | break; |
ea100cb5 | 157 | |
239dac31 | 158 | case CRM_Core_Action::UPDATE: |
4dc79403 | 159 | CRM_Utils_System::setTitle(ts('Edit Relationship for %1', array(1 => $this->_display_name_a))); |
239dac31 | 160 | break; |
ea100cb5 | 161 | |
239dac31 | 162 | case CRM_Core_Action::DELETE: |
4dc79403 | 163 | CRM_Utils_System::setTitle(ts('Delete Relationship for %1', array(1 => $this->_display_name_a))); |
239dac31 CW |
164 | break; |
165 | } | |
6a488035 TO |
166 | |
167 | $this->_caseId = CRM_Utils_Request::retrieve('caseID', 'Integer', $this); | |
168 | ||
169 | //get the relationship values. | |
170 | $this->_values = array(); | |
171 | if ($this->_relationshipId) { | |
172 | $params = array('id' => $this->_relationshipId); | |
173 | CRM_Core_DAO::commonRetrieve('CRM_Contact_DAO_Relationship', $params, $this->_values); | |
174 | } | |
175 | ||
176 | if (!$this->_rtypeId) { | |
177 | $params = $this->controller->exportValues($this->_name); | |
178 | if (isset($params['relationship_type_id'])) { | |
179 | $this->_rtypeId = $params['relationship_type_id']; | |
180 | } | |
181 | elseif (!empty($this->_values)) { | |
182 | $this->_rtypeId = $this->_values['relationship_type_id'] . '_' . $this->_rtype; | |
183 | } | |
184 | } | |
185 | ||
186 | //get the relationship type id | |
187 | $this->_relationshipTypeId = str_replace(array('_a_b', '_b_a'), array('', ''), $this->_rtypeId); | |
188 | ||
189 | //get the relationship type | |
190 | if (!$this->_rtype) { | |
191 | $this->_rtype = str_replace($this->_relationshipTypeId . '_', '', $this->_rtypeId); | |
192 | } | |
6a488035 | 193 | |
239dac31 CW |
194 | //need to assign custom data type and subtype to the template - FIXME: explain why |
195 | $this->assign('customDataType', 'Relationship'); | |
196 | $this->assign('customDataSubType', $this->_relationshipTypeId); | |
197 | $this->assign('entityID', $this->_relationshipId); | |
6a488035 TO |
198 | |
199 | //use name as it remain constant, CRM-3336 | |
200 | $this->_allRelationshipNames = CRM_Core_PseudoConstant::relationshipType('name'); | |
201 | ||
239dac31 CW |
202 | // Current employer? |
203 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
204 | if ($this->_allRelationshipNames[$this->_relationshipTypeId]["name_a_b"] == 'Employee of') { | |
205 | $this->_isCurrentEmployer = $this->_values['contact_id_b'] == CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_values['contact_id_a'], 'employer_id'); | |
206 | } | |
207 | } | |
208 | ||
6a488035 | 209 | // when custom data is included in this page |
a7488080 | 210 | if (!empty($_POST['hidden_custom'])) { |
6a488035 TO |
211 | CRM_Custom_Form_CustomData::preProcess($this); |
212 | CRM_Custom_Form_CustomData::buildQuickForm($this); | |
213 | CRM_Custom_Form_CustomData::setDefaultValues($this); | |
214 | } | |
215 | } | |
216 | ||
217 | /** | |
574d211d | 218 | * Set default values for the form. |
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, | |
21dfd5f5 | 263 | 'version' => 3, |
239dac31 | 264 | ); |
d3e86119 | 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 | /** | |
62e492d4 | 279 | * Add the rules for form. |
6a488035 | 280 | */ |
00be9182 | 281 | public function addRules() { |
6a488035 TO |
282 | if ($this->_cdType) { |
283 | return; | |
284 | } | |
285 | ||
286 | if (!($this->_action & CRM_Core_Action::DELETE)) { | |
239dac31 | 287 | $this->addFormRule(array('CRM_Contact_Form_Relationship', 'dateRule')); |
6a488035 TO |
288 | } |
289 | } | |
290 | ||
291 | /** | |
fe482240 | 292 | * Build the form object. |
6a488035 TO |
293 | */ |
294 | public function buildQuickForm() { | |
295 | if ($this->_cdType) { | |
296 | return CRM_Custom_Form_CustomData::buildQuickForm($this); | |
297 | } | |
298 | ||
6a488035 | 299 | if ($this->_action & CRM_Core_Action::DELETE) { |
6a488035 TO |
300 | $this->addButtons(array( |
301 | array( | |
302 | 'type' => 'next', | |
303 | 'name' => ts('Delete'), | |
304 | 'isDefault' => TRUE, | |
305 | ), | |
306 | array( | |
307 | 'type' => 'cancel', | |
308 | 'name' => ts('Cancel'), | |
309 | ), | |
310 | ) | |
311 | ); | |
312 | return; | |
313 | } | |
239dac31 CW |
314 | |
315 | // Select list | |
316 | $relationshipList = CRM_Contact_BAO_Relationship::getContactRelationshipType($this->_contactId, $this->_rtype, $this->_relationshipId); | |
317 | ||
318 | // Metadata needed on clientside | |
319 | $contactTypes = CRM_Contact_BAO_ContactType::contactTypeInfo(TRUE); | |
320 | $jsData = array(); | |
d97a2fc1 | 321 | // Get just what we need to keep the dom small |
239dac31 | 322 | $whatWeWant = array_flip(array('contact_type_a', 'contact_type_b', 'contact_sub_type_a', 'contact_sub_type_b')); |
22e263ad | 323 | foreach ($this->_allRelationshipNames as $id => $vals) { |
239dac31 CW |
324 | if ($vals['name_a_b'] === 'Employee of') { |
325 | $this->assign('employmentRelationship', $id); | |
326 | } | |
327 | if (isset($relationshipList["{$id}_a_b"]) || isset($relationshipList["{$id}_b_a"])) { | |
328 | $jsData[$id] = array_filter(array_intersect_key($this->_allRelationshipNames[$id], $whatWeWant)); | |
329 | // Add user-friendly placeholder | |
330 | foreach (array('a', 'b') as $x) { | |
331 | $type = !empty($jsData[$id]["contact_sub_type_$x"]) ? $jsData[$id]["contact_sub_type_$x"] : CRM_Utils_Array::value("contact_type_$x", $jsData[$id]); | |
332 | $jsData[$id]["placeholder_$x"] = $type ? ts('- select %1 -', array(strtolower($contactTypes[$type]['label']))) : ts('- select contact -'); | |
333 | } | |
6a488035 | 334 | } |
6a488035 | 335 | } |
239dac31 | 336 | $this->assign('relationshipData', $jsData); |
6a488035 | 337 | |
db3ec100 | 338 | $this->addField('relationship_type_id', array('options' => array('' => ts('- select -')) + $relationshipList, 'class' => 'huge', 'placeholder' => '- select -'), TRUE); |
6a488035 | 339 | |
239dac31 | 340 | $label = $this->_action & CRM_Core_Action::ADD ? ts('Contact(s)') : ts('Contact'); |
9824c342 | 341 | $contactField = $this->addField('related_contact_id', array('label' => $label, 'name' => 'contact_id_b', 'multiple' => TRUE, 'create' => TRUE), TRUE); |
239dac31 CW |
342 | // This field cannot be updated |
343 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
344 | $contactField->freeze(); | |
345 | } | |
346 | ||
71331d2a | 347 | $this->add('advcheckbox', 'is_current_employer', $this->_contactType == 'Organization' ? ts('Current Employee') : ts('Current Employer')); |
6a488035 | 348 | |
db3ec100 TM |
349 | $this->addField('start_date', array('label' => ts('Start Date'), 'formatType' => 'searchDate')); |
350 | $this->addField('end_date', array('label' => ts('End Date'), 'formatType' => 'searchDate')); | |
6a488035 | 351 | |
db3ec100 | 352 | $this->addField('is_active', array('label' => ts('Enabled?'))); |
239dac31 | 353 | |
db3ec100 TM |
354 | $this->addField('is_permission_a_b'); |
355 | $this->addField('is_permission_b_a'); | |
6a488035 | 356 | |
db3ec100 | 357 | $this->addField('description', array('label' => ts('Description'))); |
6a488035 TO |
358 | |
359 | CRM_Contact_Form_Edit_Notes::buildQuickForm($this); | |
360 | ||
239dac31 CW |
361 | if ($this->_action & CRM_Core_Action::VIEW) { |
362 | $this->addButtons(array( | |
363 | array( | |
364 | 'type' => 'cancel', | |
365 | 'name' => ts('Done'), | |
366 | ), | |
367 | )); | |
6a488035 TO |
368 | } |
369 | else { | |
239dac31 CW |
370 | // make this form an upload since we don't know if the custom data injected dynamically is of type file etc. |
371 | $this->addButtons(array( | |
372 | array( | |
373 | 'type' => 'upload', | |
374 | 'name' => ts('Save Relationship'), | |
375 | 'isDefault' => TRUE, | |
376 | ), | |
6a488035 TO |
377 | array( |
378 | 'type' => 'cancel', | |
379 | 'name' => ts('Cancel'), | |
380 | ), | |
239dac31 CW |
381 | )); |
382 | } | |
6a488035 TO |
383 | } |
384 | ||
385 | /** | |
62e492d4 | 386 | * This function is called when the form is submitted. |
6a488035 TO |
387 | */ |
388 | public function postProcess() { | |
62e492d4 | 389 | // Store the submitted values in an array. |
6a488035 | 390 | $params = $this->controller->exportValues($this->_name); |
6a488035 | 391 | |
cd4a7081 | 392 | // CRM-14612 - Don't use adv-checkbox as it interferes with the form js |
393 | $params['is_permission_a_b'] = CRM_Utils_Array::value('is_permission_a_b', $params, 0); | |
394 | $params['is_permission_b_a'] = CRM_Utils_Array::value('is_permission_b_a', $params, 0); | |
395 | ||
6a488035 | 396 | // action is taken depending upon the mode |
6a488035 TO |
397 | if ($this->_action & CRM_Core_Action::DELETE) { |
398 | CRM_Contact_BAO_Relationship::del($this->_relationshipId); | |
bd655ee6 MV |
399 | |
400 | // CRM-15881 UPDATES | |
401 | // Since the line above nullifies the organization_name and employer_id fiels in the contact record, we need to reload all blocks to reflect this chage on the user interface. | |
402 | $this->ajaxResponse['reloadBlocks'] = array('#crm-contactinfo-content'); | |
403 | ||
6a488035 TO |
404 | return; |
405 | } | |
406 | ||
eff45dce EM |
407 | $relationshipTypeParts = explode('_', $params['relationship_type_id']); |
408 | $params['relationship_type_id'] = $relationshipTypeParts[0]; | |
409 | if (!$this->_rtype) { | |
410 | // Do we need to wrap this in an if - when is rtype used & is relationship_type_id always set then? | |
411 | $this->_rtype = $params['relationship_type_id']; | |
412 | } | |
413 | $params['contact_id_' . $relationshipTypeParts[1]] = $this->_contactId; | |
414 | ||
71331d2a | 415 | // Update mode (always single) |
6a488035 | 416 | if ($this->_action & CRM_Core_Action::UPDATE) { |
782bfb8a | 417 | $update = TRUE; |
19e5c7f7 | 418 | $params['id'] = $this->_relationshipId; |
6a488035 | 419 | $ids['relationship'] = $this->_relationshipId; |
eff45dce | 420 | $relation = CRM_Contact_BAO_Relationship::getRelationshipByID($this->_relationshipId); |
19e5c7f7 | 421 | if ($relation->contact_id_a == $this->_contactId) { |
6616f7c1 | 422 | // I couldn't replicate this path in testing. See below. |
19e5c7f7 EM |
423 | $params['contact_id_a'] = $this->_contactId; |
424 | $params['contact_id_b'] = array($params['related_contact_id']); | |
6616f7c1 EM |
425 | $outcome = CRM_Contact_BAO_Relationship::createMultiple($params, $relationshipTypeParts[1]); |
426 | $relationshipIds = $outcome['relationship_ids']; | |
19e5c7f7 EM |
427 | } |
428 | else { | |
6616f7c1 EM |
429 | // The only reason we have changed this to use the api & not the above is that this was broken. |
430 | // Recommend extracting all of update into a function that uses the api | |
431 | // and ensuring api / bao take care of 'other stuff' in this form | |
432 | // the contact_id_a & b can't be changed on this form so don't really need setting. | |
19e5c7f7 | 433 | $params['contact_id_b'] = $this->_contactId; |
6616f7c1 EM |
434 | $params['contact_id_a'] = $params['related_contact_id']; |
435 | $result = civicrm_api3('relationship', 'create', $params); | |
436 | $relationshipIds = array($result['id']); | |
19e5c7f7 | 437 | } |
6a488035 TO |
438 | $ids['contactTarget'] = ($relation->contact_id_a == $this->_contactId) ? $relation->contact_id_b : $relation->contact_id_a; |
439 | ||
2da59b29 | 440 | // @todo this belongs in the BAO. |
71331d2a CW |
441 | if ($this->_isCurrentEmployer) { |
442 | // if relationship type changes, relationship is disabled, or "current employer" is unchecked, | |
443 | // clear the current employer. CRM-3235. | |
eff45dce | 444 | $relChanged = $params['relationship_type_id'] != $this->_values['relationship_type_id']; |
71331d2a | 445 | if (!$params['is_active'] || !$params['is_current_employer'] || $relChanged) { |
bd655ee6 MV |
446 | |
447 | // CRM-15881 UPDATES | |
448 | // If not is_active then is_current_employer needs to be set false as well! Logically a contact cannot be a current employee of a disabled employer relationship. | |
449 | // If this is not done, then the below process will go ahead and disable the organization_name and employer_id fields in the contact record (which is what is wanted) but then further down will be re-enabled becuase is_current_employer is not false, therefore undoing what was done correctly. | |
450 | if (!$params['is_active']) { | |
451 | $params['is_current_employer'] = FALSE; | |
452 | } | |
453 | ||
71331d2a | 454 | CRM_Contact_BAO_Contact_Utils::clearCurrentEmployer($this->_values['contact_id_a']); |
2840a035 CW |
455 | // Refresh contact summary if in ajax mode |
456 | $this->ajaxResponse['reloadBlocks'] = array('#crm-contactinfo-content'); | |
71331d2a | 457 | } |
6a488035 | 458 | } |
0cd647f0 | 459 | if (empty($outcome['saved']) && !empty($update)) { |
460 | $outcome['saved'] = $update; | |
461 | } | |
462 | $this->setMessage($outcome); | |
6a488035 | 463 | } |
71331d2a | 464 | // Create mode (could be 1 or more relationships) |
239dac31 | 465 | else { |
62e492d4 | 466 | $params['contact_id_' . $relationshipTypeParts[2]] = explode(',', $params['related_contact_id']); |
6616f7c1 EM |
467 | $outcome = CRM_Contact_BAO_Relationship::createMultiple($params, $relationshipTypeParts[1]); |
468 | $relationshipIds = $outcome['relationship_ids']; | |
469 | if (empty($outcome['saved']) && !empty($update)) { | |
470 | $outcome['saved'] = $update; | |
471 | } | |
472 | $this->setMessage($outcome); | |
6a488035 | 473 | } |
6a488035 | 474 | |
6a488035 TO |
475 | // if this is called from case view, |
476 | //create an activity for case role removal.CRM-4480 | |
2da59b29 | 477 | // @todo this belongs in the BAO. |
6a488035 TO |
478 | if ($this->_caseId) { |
479 | CRM_Case_BAO_Case::createCaseRoleActivity($this->_caseId, $relationshipIds, $params['contact_check'], $this->_contactId); | |
480 | } | |
481 | ||
239dac31 | 482 | // Save notes |
2da59b29 | 483 | // @todo this belongs in the BAO. |
30b335f1 | 484 | if ($this->_action & CRM_Core_Action::UPDATE || $params['note']) { |
239dac31 CW |
485 | foreach ($relationshipIds as $id) { |
486 | $noteParams = array( | |
487 | 'entity_id' => $id, | |
488 | 'entity_table' => 'civicrm_relationship', | |
489 | ); | |
d3e86119 | 490 | $existing = civicrm_api3('note', 'get', $noteParams); |
239dac31 CW |
491 | if (!empty($existing['id'])) { |
492 | $noteParams['id'] = $existing['id']; | |
493 | } | |
494 | $noteParams['note'] = $params['note']; | |
495 | $noteParams['contact_id'] = $this->_contactId; | |
496 | if (!empty($existing['id']) || $params['note']) { | |
497 | $action = $params['note'] ? 'create' : 'delete'; | |
498 | civicrm_api3('note', $action, $noteParams); | |
499 | } | |
6a488035 | 500 | } |
6a488035 TO |
501 | } |
502 | ||
7e98675f EM |
503 | $params['relationship_ids'] = $relationshipIds; |
504 | ||
7e98675f EM |
505 | // Refresh contact tabs which might have been affected |
506 | $this->ajaxResponse['updateTabs'] = array( | |
507 | '#tab_member' => CRM_Contact_BAO_Contact::getCountComponent('membership', $this->_contactId), | |
508 | '#tab_contribute' => CRM_Contact_BAO_Contact::getCountComponent('contribution', $this->_contactId), | |
509 | ); | |
510 | ||
239dac31 | 511 | // Set current employee/employer relationship, CRM-3532 |
2da59b29 EM |
512 | if ($params['is_current_employer'] && $this->_allRelationshipNames[$params['relationship_type_id']]["name_a_b"] == |
513 | 'Employee of') { | |
239dac31 CW |
514 | $employerParams = array(); |
515 | foreach ($relationshipIds as $id) { | |
516 | // Fixme this is dumb why do we have to look this up again? | |
eff45dce | 517 | $rel = CRM_Contact_BAO_Relationship::getRelationshipByID($id); |
239dac31 | 518 | $employerParams[$rel->contact_id_a] = $rel->contact_id_b; |
6a488035 | 519 | } |
2da59b29 | 520 | // @todo this belongs in the BAO. |
239dac31 | 521 | CRM_Contact_BAO_Contact_Utils::setCurrentEmployer($employerParams); |
2840a035 CW |
522 | // Refresh contact summary if in ajax mode |
523 | $this->ajaxResponse['reloadBlocks'] = array('#crm-contactinfo-content'); | |
6a488035 | 524 | } |
6a488035 TO |
525 | } |
526 | ||
527 | /** | |
fe482240 | 528 | * Date validation. |
6a488035 | 529 | * |
77c5b619 TO |
530 | * @param array $params |
531 | * (reference ) an assoc array of name/value pairs. | |
6a488035 | 532 | * |
72b3a70c CW |
533 | * @return bool|array |
534 | * mixed true or array of errors | |
6a488035 | 535 | */ |
00be9182 | 536 | public static function dateRule($params) { |
6a488035 TO |
537 | $errors = array(); |
538 | ||
539 | // check start and end date | |
8cc574cf | 540 | if (!empty($params['start_date']) && !empty($params['end_date'])) { |
6a488035 TO |
541 | $start_date = CRM_Utils_Date::format(CRM_Utils_Array::value('start_date', $params)); |
542 | $end_date = CRM_Utils_Date::format(CRM_Utils_Array::value('end_date', $params)); | |
543 | if ($start_date && $end_date && (int ) $end_date < (int ) $start_date) { | |
544 | $errors['end_date'] = ts('The relationship end date cannot be prior to the start date.'); | |
545 | } | |
546 | } | |
547 | ||
548 | return empty($errors) ? TRUE : $errors; | |
549 | } | |
550 | ||
2da59b29 EM |
551 | /** |
552 | * Set Status message to reflect outcome of the update action. | |
553 | * | |
607fa308 EM |
554 | * @param array $outcome |
555 | * Outcome of save action - including | |
556 | * - 'valid' : Number of valid relationships attempted. | |
557 | * - 'invalid' : Number of invalid relationships attempted. | |
558 | * - 'duplicate' : Number of duplicate relationships attempted. | |
559 | * - 'saved' : boolean of whether save was successful | |
2da59b29 | 560 | */ |
607fa308 | 561 | protected function setMessage($outcome) { |
782bfb8a | 562 | if (!empty($outcome['valid']) && empty($outcome['saved'])) { |
2da59b29 | 563 | CRM_Core_Session::setStatus(ts('Relationship created.', array( |
607fa308 | 564 | 'count' => $outcome['valid'], |
2da59b29 EM |
565 | 'plural' => '%count relationships created.', |
566 | )), ts('Saved'), 'success'); | |
567 | } | |
607fa308 | 568 | if (!empty($outcome['invalid'])) { |
2da59b29 | 569 | CRM_Core_Session::setStatus(ts('%count relationship record was not created due to an invalid contact type.', array( |
607fa308 | 570 | 'count' => $outcome['invalid'], |
2da59b29 EM |
571 | 'plural' => '%count relationship records were not created due to invalid contact types.', |
572 | )), ts('%count invalid relationship record', array( | |
607fa308 | 573 | 'count' => $outcome['invalid'], |
2da59b29 EM |
574 | 'plural' => '%count invalid relationship records', |
575 | ))); | |
576 | } | |
607fa308 | 577 | if (!empty($outcome['duplicate'])) { |
2da59b29 | 578 | CRM_Core_Session::setStatus(ts('One relationship was not created because it already exists.', array( |
607fa308 | 579 | 'count' => $outcome['duplicate'], |
2da59b29 EM |
580 | 'plural' => '%count relationships were not created because they already exist.', |
581 | )), ts('%count duplicate relationship', array( | |
607fa308 | 582 | 'count' => $outcome['duplicate'], |
2da59b29 EM |
583 | 'plural' => '%count duplicate relationships', |
584 | ))); | |
585 | } | |
607fa308 | 586 | if (!empty($outcome['saved'])) { |
2da59b29 EM |
587 | CRM_Core_Session::setStatus(ts('Relationship record has been updated.'), ts('Saved'), 'success'); |
588 | } | |
589 | } | |
590 | ||
6a488035 | 591 | } |