Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
8c9251b3 | 6 | | Copyright CiviCRM LLC (c) 2004-2018 | |
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 | |
0efbca68 TM |
106 | /** |
107 | * Explicitly declare the form context. | |
108 | */ | |
109 | public function getDefaultContext() { | |
110 | return 'create'; | |
111 | } | |
112 | ||
113 | /** | |
114 | * Explicitly declare the entity api name. | |
115 | */ | |
116 | public function getDefaultEntity() { | |
117 | return 'Relationship'; | |
118 | } | |
119 | ||
00be9182 | 120 | public function preProcess() { |
6a488035 TO |
121 | $this->_contactId = $this->get('contactId'); |
122 | ||
239dac31 CW |
123 | $this->_contactType = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactId, 'contact_type'); |
124 | ||
6a488035 TO |
125 | $this->_relationshipId = $this->get('id'); |
126 | ||
127 | $this->_rtype = CRM_Utils_Request::retrieve('rtype', 'String', $this); | |
128 | ||
129 | $this->_rtypeId = CRM_Utils_Request::retrieve('relTypeId', 'String', $this); | |
130 | ||
131 | $this->_display_name_a = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactId, 'display_name'); | |
132 | ||
239dac31 | 133 | $this->assign('display_name_a', $this->_display_name_a); |
75b35151 | 134 | //get the relationship values. |
135 | $this->_values = array(); | |
136 | if ($this->_relationshipId) { | |
137 | $params = array('id' => $this->_relationshipId); | |
138 | CRM_Core_DAO::commonRetrieve('CRM_Contact_DAO_Relationship', $params, $this->_values); | |
139 | } | |
239dac31 | 140 | |
cd056f13 | 141 | // Check for permissions |
bf5fe547 | 142 | if (in_array($this->_action, array(CRM_Core_Action::ADD, CRM_Core_Action::UPDATE, CRM_Core_Action::DELETE))) { |
75b35151 | 143 | if (!CRM_Contact_BAO_Contact_Permission::allow($this->_contactId, CRM_Core_Permission::EDIT) |
144 | && !CRM_Contact_BAO_Contact_Permission::allow($this->_values['contact_id_b'], CRM_Core_Permission::EDIT)) { | |
cd056f13 KL |
145 | CRM_Core_Error::statusBounce(ts('You do not have the necessary permission to edit this contact.')); |
146 | } | |
147 | } | |
148 | ||
239dac31 CW |
149 | // Set page title based on action |
150 | switch ($this->_action) { | |
151 | case CRM_Core_Action::VIEW: | |
4dc79403 | 152 | CRM_Utils_System::setTitle(ts('View Relationship for %1', array(1 => $this->_display_name_a))); |
239dac31 | 153 | break; |
ea100cb5 | 154 | |
239dac31 | 155 | case CRM_Core_Action::ADD: |
4dc79403 | 156 | CRM_Utils_System::setTitle(ts('Add Relationship for %1', array(1 => $this->_display_name_a))); |
239dac31 | 157 | break; |
ea100cb5 | 158 | |
239dac31 | 159 | case CRM_Core_Action::UPDATE: |
4dc79403 | 160 | CRM_Utils_System::setTitle(ts('Edit Relationship for %1', array(1 => $this->_display_name_a))); |
239dac31 | 161 | break; |
ea100cb5 | 162 | |
239dac31 | 163 | case CRM_Core_Action::DELETE: |
4dc79403 | 164 | CRM_Utils_System::setTitle(ts('Delete Relationship for %1', array(1 => $this->_display_name_a))); |
239dac31 CW |
165 | break; |
166 | } | |
6a488035 TO |
167 | |
168 | $this->_caseId = CRM_Utils_Request::retrieve('caseID', 'Integer', $this); | |
169 | ||
6a488035 | 170 | if (!$this->_rtypeId) { |
26b82b94 | 171 | $params = CRM_Utils_Request::exportValues(); |
6a488035 TO |
172 | if (isset($params['relationship_type_id'])) { |
173 | $this->_rtypeId = $params['relationship_type_id']; | |
174 | } | |
175 | elseif (!empty($this->_values)) { | |
176 | $this->_rtypeId = $this->_values['relationship_type_id'] . '_' . $this->_rtype; | |
177 | } | |
178 | } | |
179 | ||
180 | //get the relationship type id | |
181 | $this->_relationshipTypeId = str_replace(array('_a_b', '_b_a'), array('', ''), $this->_rtypeId); | |
182 | ||
183 | //get the relationship type | |
184 | if (!$this->_rtype) { | |
185 | $this->_rtype = str_replace($this->_relationshipTypeId . '_', '', $this->_rtypeId); | |
186 | } | |
6a488035 | 187 | |
239dac31 CW |
188 | //need to assign custom data type and subtype to the template - FIXME: explain why |
189 | $this->assign('customDataType', 'Relationship'); | |
190 | $this->assign('customDataSubType', $this->_relationshipTypeId); | |
191 | $this->assign('entityID', $this->_relationshipId); | |
6a488035 TO |
192 | |
193 | //use name as it remain constant, CRM-3336 | |
194 | $this->_allRelationshipNames = CRM_Core_PseudoConstant::relationshipType('name'); | |
195 | ||
239dac31 CW |
196 | // Current employer? |
197 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
198 | if ($this->_allRelationshipNames[$this->_relationshipTypeId]["name_a_b"] == 'Employee of') { | |
199 | $this->_isCurrentEmployer = $this->_values['contact_id_b'] == CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_values['contact_id_a'], 'employer_id'); | |
200 | } | |
201 | } | |
202 | ||
6a488035 | 203 | // when custom data is included in this page |
a7488080 | 204 | if (!empty($_POST['hidden_custom'])) { |
da4b8382 | 205 | CRM_Custom_Form_CustomData::preProcess($this, NULL, $this->_relationshipTypeId, 1, 'Relationship', $this->_relationshipId); |
6a488035 TO |
206 | CRM_Custom_Form_CustomData::buildQuickForm($this); |
207 | CRM_Custom_Form_CustomData::setDefaultValues($this); | |
208 | } | |
209 | } | |
210 | ||
211 | /** | |
574d211d | 212 | * Set default values for the form. |
6a488035 | 213 | */ |
00be9182 | 214 | public function setDefaultValues() { |
6a488035 TO |
215 | $defaults = array(); |
216 | ||
217 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
218 | if (!empty($this->_values)) { | |
219 | $defaults['relationship_type_id'] = $this->_rtypeId; | |
425d6064 MWMC |
220 | $defaults['start_date'] = CRM_Utils_Array::value('start_date', $this->_values); |
221 | $defaults['end_date'] = CRM_Utils_Array::value('end_date', $this->_values); | |
6a488035 TO |
222 | $defaults['description'] = CRM_Utils_Array::value('description', $this->_values); |
223 | $defaults['is_active'] = CRM_Utils_Array::value('is_active', $this->_values); | |
239dac31 | 224 | |
2a798409 | 225 | // The postprocess function will swap these fields if it is a b_a relationship, so we compensate here |
239dac31 CW |
226 | $defaults['is_permission_a_b'] = CRM_Utils_Array::value('is_permission_' . $this->_rtype, $this->_values); |
227 | $defaults['is_permission_b_a'] = CRM_Utils_Array::value('is_permission_' . strrev($this->_rtype), $this->_values); | |
228 | ||
229 | $defaults['is_current_employer'] = $this->_isCurrentEmployer; | |
230 | ||
231 | // Load info about the related contact | |
6a488035 TO |
232 | $contact = new CRM_Contact_DAO_Contact(); |
233 | if ($this->_rtype == 'a_b' && $this->_values['contact_id_a'] == $this->_contactId) { | |
234 | $contact->id = $this->_values['contact_id_b']; | |
235 | } | |
236 | else { | |
237 | $contact->id = $this->_values['contact_id_a']; | |
238 | } | |
239 | if ($contact->find(TRUE)) { | |
239dac31 | 240 | $defaults['related_contact_id'] = $contact->id; |
6a488035 | 241 | $this->_display_name_b = $contact->display_name; |
239dac31 | 242 | $this->assign('display_name_b', $this->_display_name_b); |
6a488035 TO |
243 | } |
244 | ||
239dac31 CW |
245 | $noteParams = array( |
246 | 'entity_id' => $this->_relationshipId, | |
247 | 'entity_table' => 'civicrm_relationship', | |
248 | 'limit' => 1, | |
21dfd5f5 | 249 | 'version' => 3, |
239dac31 | 250 | ); |
d3e86119 | 251 | $note = civicrm_api('Note', 'getsingle', $noteParams); |
239dac31 | 252 | $defaults['note'] = CRM_Utils_Array::value('note', $note); |
6a488035 TO |
253 | } |
254 | } | |
255 | else { | |
239dac31 | 256 | $defaults['is_active'] = $defaults['is_current_employer'] = 1; |
6a488035 | 257 | $defaults['relationship_type_id'] = $this->_rtypeId; |
f871c3a9 | 258 | $defaults['is_permission_a_b'] = $defaults['is_permission_b_a'] = CRM_Contact_BAO_Relationship::NONE; |
6a488035 TO |
259 | } |
260 | ||
261 | $this->_enabled = $defaults['is_active']; | |
262 | return $defaults; | |
263 | } | |
264 | ||
265 | /** | |
62e492d4 | 266 | * Add the rules for form. |
6a488035 | 267 | */ |
00be9182 | 268 | public function addRules() { |
6a488035 | 269 | if (!($this->_action & CRM_Core_Action::DELETE)) { |
239dac31 | 270 | $this->addFormRule(array('CRM_Contact_Form_Relationship', 'dateRule')); |
6a488035 TO |
271 | } |
272 | } | |
273 | ||
274 | /** | |
fe482240 | 275 | * Build the form object. |
6a488035 TO |
276 | */ |
277 | public function buildQuickForm() { | |
6a488035 | 278 | if ($this->_action & CRM_Core_Action::DELETE) { |
6a488035 TO |
279 | $this->addButtons(array( |
280 | array( | |
281 | 'type' => 'next', | |
282 | 'name' => ts('Delete'), | |
283 | 'isDefault' => TRUE, | |
284 | ), | |
285 | array( | |
286 | 'type' => 'cancel', | |
287 | 'name' => ts('Cancel'), | |
288 | ), | |
289 | ) | |
290 | ); | |
291 | return; | |
292 | } | |
239dac31 CW |
293 | |
294 | // Select list | |
295 | $relationshipList = CRM_Contact_BAO_Relationship::getContactRelationshipType($this->_contactId, $this->_rtype, $this->_relationshipId); | |
296 | ||
8e383c2f MD |
297 | $this->assign('contactTypes', CRM_Contact_BAO_ContactType::contactTypeInfo(TRUE)); |
298 | ||
22e263ad | 299 | foreach ($this->_allRelationshipNames as $id => $vals) { |
239dac31 CW |
300 | if ($vals['name_a_b'] === 'Employee of') { |
301 | $this->assign('employmentRelationship', $id); | |
0004ae05 | 302 | break; |
6a488035 | 303 | } |
6a488035 TO |
304 | } |
305 | ||
b3ee84c9 MD |
306 | $this->addField( |
307 | 'relationship_type_id', | |
308 | array( | |
309 | 'options' => array('' => ts('- select -')) + $relationshipList, | |
310 | 'class' => 'huge', | |
311 | 'placeholder' => '- select -', | |
312 | 'option_url' => 'civicrm/admin/reltype', | |
313 | 'option_context' => array( | |
314 | 'contact_id' => $this->_contactId, | |
315 | 'relationship_direction' => $this->_rtype, | |
316 | 'relationship_id' => $this->_relationshipId, | |
317 | 'is_form' => TRUE, | |
318 | ), | |
319 | ), | |
320 | TRUE | |
321 | ); | |
6a488035 | 322 | |
239dac31 | 323 | $label = $this->_action & CRM_Core_Action::ADD ? ts('Contact(s)') : ts('Contact'); |
9824c342 | 324 | $contactField = $this->addField('related_contact_id', array('label' => $label, 'name' => 'contact_id_b', 'multiple' => TRUE, 'create' => TRUE), TRUE); |
239dac31 CW |
325 | // This field cannot be updated |
326 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
327 | $contactField->freeze(); | |
328 | } | |
329 | ||
71331d2a | 330 | $this->add('advcheckbox', 'is_current_employer', $this->_contactType == 'Organization' ? ts('Current Employee') : ts('Current Employer')); |
6a488035 | 331 | |
425d6064 MWMC |
332 | $this->addField('start_date', array('label' => ts('Start Date')), FALSE, FALSE); |
333 | $this->addField('end_date', array('label' => ts('End Date')), FALSE, FALSE); | |
6a488035 | 334 | |
cb089e63 | 335 | $this->addField('is_active', array('label' => ts('Enabled?'), 'type' => 'advcheckbox')); |
239dac31 | 336 | |
f871c3a9 AS |
337 | $this->addField('is_permission_a_b', array(), TRUE); |
338 | $this->addField('is_permission_b_a', array(), TRUE); | |
6a488035 | 339 | |
db3ec100 | 340 | $this->addField('description', array('label' => ts('Description'))); |
6a488035 TO |
341 | |
342 | CRM_Contact_Form_Edit_Notes::buildQuickForm($this); | |
343 | ||
239dac31 CW |
344 | if ($this->_action & CRM_Core_Action::VIEW) { |
345 | $this->addButtons(array( | |
346 | array( | |
347 | 'type' => 'cancel', | |
348 | 'name' => ts('Done'), | |
349 | ), | |
350 | )); | |
6a488035 TO |
351 | } |
352 | else { | |
239dac31 CW |
353 | // make this form an upload since we don't know if the custom data injected dynamically is of type file etc. |
354 | $this->addButtons(array( | |
355 | array( | |
356 | 'type' => 'upload', | |
357 | 'name' => ts('Save Relationship'), | |
358 | 'isDefault' => TRUE, | |
359 | ), | |
6a488035 TO |
360 | array( |
361 | 'type' => 'cancel', | |
362 | 'name' => ts('Cancel'), | |
363 | ), | |
239dac31 CW |
364 | )); |
365 | } | |
6a488035 TO |
366 | } |
367 | ||
368 | /** | |
00d84e9b | 369 | * This function is called when the form is submitted and also from unit test. |
425d6064 | 370 | * |
00d84e9b JP |
371 | * @param array $params |
372 | * | |
373 | * @return array | |
425d6064 | 374 | * @throws \CRM_Core_Exception |
6a488035 | 375 | */ |
00d84e9b | 376 | public function submit($params) { |
f55fb370 | 377 | switch ($this->getAction()) { |
378 | case CRM_Core_Action::DELETE: | |
379 | $this->deleteAction($this->_relationshipId); | |
00d84e9b | 380 | return array(); |
eff45dce | 381 | |
f55fb370 | 382 | case CRM_Core_Action::UPDATE: |
00d84e9b | 383 | return $this->updateAction($params); |
6a488035 | 384 | |
f55fb370 | 385 | default: |
00d84e9b | 386 | return $this->createAction($params); |
6a488035 | 387 | } |
00d84e9b JP |
388 | } |
389 | ||
390 | /** | |
391 | * This function is called when the form is submitted. | |
392 | */ | |
393 | public function postProcess() { | |
394 | // Store the submitted values in an array. | |
395 | $params = $this->controller->exportValues($this->_name); | |
396 | ||
397 | $values = $this->submit($params); | |
398 | if (empty($values)) { | |
399 | return; | |
400 | } | |
401 | list ($params, $relationshipIds) = $values; | |
6a488035 | 402 | |
6a488035 TO |
403 | // if this is called from case view, |
404 | //create an activity for case role removal.CRM-4480 | |
2da59b29 | 405 | // @todo this belongs in the BAO. |
6a488035 TO |
406 | if ($this->_caseId) { |
407 | CRM_Case_BAO_Case::createCaseRoleActivity($this->_caseId, $relationshipIds, $params['contact_check'], $this->_contactId); | |
408 | } | |
409 | ||
2da59b29 | 410 | // @todo this belongs in the BAO. |
45caf31c | 411 | $note = !empty($params['note']) ? $params['note'] : ''; |
412 | $this->saveRelationshipNotes($relationshipIds, $note); | |
6a488035 | 413 | |
f55fb370 | 414 | $this->setEmploymentRelationship($params, $relationshipIds); |
7e98675f | 415 | |
7e98675f | 416 | // Refresh contact tabs which might have been affected |
df0c42cc JP |
417 | $this->ajaxResponse = array( |
418 | 'reloadBlocks' => array('#crm-contactinfo-content'), | |
419 | 'updateTabs' => array( | |
420 | '#tab_member' => CRM_Contact_BAO_Contact::getCountComponent('membership', $this->_contactId), | |
421 | '#tab_contribute' => CRM_Contact_BAO_Contact::getCountComponent('contribution', $this->_contactId), | |
422 | ), | |
7e98675f | 423 | ); |
6a488035 TO |
424 | } |
425 | ||
426 | /** | |
fe482240 | 427 | * Date validation. |
6a488035 | 428 | * |
77c5b619 TO |
429 | * @param array $params |
430 | * (reference ) an assoc array of name/value pairs. | |
6a488035 | 431 | * |
72b3a70c CW |
432 | * @return bool|array |
433 | * mixed true or array of errors | |
6a488035 | 434 | */ |
00be9182 | 435 | public static function dateRule($params) { |
6a488035 TO |
436 | $errors = array(); |
437 | ||
438 | // check start and end date | |
8cc574cf | 439 | if (!empty($params['start_date']) && !empty($params['end_date'])) { |
425d6064 | 440 | if ($params['end_date'] < $params['start_date']) { |
6a488035 TO |
441 | $errors['end_date'] = ts('The relationship end date cannot be prior to the start date.'); |
442 | } | |
443 | } | |
444 | ||
445 | return empty($errors) ? TRUE : $errors; | |
446 | } | |
447 | ||
2da59b29 EM |
448 | /** |
449 | * Set Status message to reflect outcome of the update action. | |
450 | * | |
607fa308 EM |
451 | * @param array $outcome |
452 | * Outcome of save action - including | |
453 | * - 'valid' : Number of valid relationships attempted. | |
454 | * - 'invalid' : Number of invalid relationships attempted. | |
455 | * - 'duplicate' : Number of duplicate relationships attempted. | |
456 | * - 'saved' : boolean of whether save was successful | |
2da59b29 | 457 | */ |
607fa308 | 458 | protected function setMessage($outcome) { |
782bfb8a | 459 | if (!empty($outcome['valid']) && empty($outcome['saved'])) { |
2da59b29 | 460 | CRM_Core_Session::setStatus(ts('Relationship created.', array( |
607fa308 | 461 | 'count' => $outcome['valid'], |
2da59b29 EM |
462 | 'plural' => '%count relationships created.', |
463 | )), ts('Saved'), 'success'); | |
464 | } | |
607fa308 | 465 | if (!empty($outcome['invalid'])) { |
2da59b29 | 466 | CRM_Core_Session::setStatus(ts('%count relationship record was not created due to an invalid contact type.', array( |
607fa308 | 467 | 'count' => $outcome['invalid'], |
2da59b29 EM |
468 | 'plural' => '%count relationship records were not created due to invalid contact types.', |
469 | )), ts('%count invalid relationship record', array( | |
607fa308 | 470 | 'count' => $outcome['invalid'], |
2da59b29 EM |
471 | 'plural' => '%count invalid relationship records', |
472 | ))); | |
473 | } | |
607fa308 | 474 | if (!empty($outcome['duplicate'])) { |
2da59b29 | 475 | CRM_Core_Session::setStatus(ts('One relationship was not created because it already exists.', array( |
607fa308 | 476 | 'count' => $outcome['duplicate'], |
2da59b29 EM |
477 | 'plural' => '%count relationships were not created because they already exist.', |
478 | )), ts('%count duplicate relationship', array( | |
607fa308 | 479 | 'count' => $outcome['duplicate'], |
2da59b29 EM |
480 | 'plural' => '%count duplicate relationships', |
481 | ))); | |
482 | } | |
607fa308 | 483 | if (!empty($outcome['saved'])) { |
2da59b29 EM |
484 | CRM_Core_Session::setStatus(ts('Relationship record has been updated.'), ts('Saved'), 'success'); |
485 | } | |
486 | } | |
487 | ||
0004ae05 CW |
488 | /** |
489 | * @param $relationshipList | |
425d6064 | 490 | * |
0004ae05 CW |
491 | * @return array |
492 | */ | |
493 | public static function getRelationshipTypeMetadata($relationshipList) { | |
494 | $contactTypes = CRM_Contact_BAO_ContactType::contactTypeInfo(TRUE); | |
495 | $allRelationshipNames = CRM_Core_PseudoConstant::relationshipType('name'); | |
496 | $jsData = array(); | |
497 | // Get just what we need to keep the dom small | |
498 | $whatWeWant = array_flip(array( | |
499 | 'contact_type_a', | |
500 | 'contact_type_b', | |
501 | 'contact_sub_type_a', | |
502 | 'contact_sub_type_b', | |
503 | )); | |
504 | foreach ($allRelationshipNames as $id => $vals) { | |
505 | if (isset($relationshipList["{$id}_a_b"]) || isset($relationshipList["{$id}_b_a"])) { | |
506 | $jsData[$id] = array_filter(array_intersect_key($allRelationshipNames[$id], $whatWeWant)); | |
507 | // Add user-friendly placeholder | |
508 | foreach (array('a', 'b') as $x) { | |
509 | $type = !empty($jsData[$id]["contact_sub_type_$x"]) ? $jsData[$id]["contact_sub_type_$x"] : CRM_Utils_Array::value("contact_type_$x", $jsData[$id]); | |
510 | $jsData[$id]["placeholder_$x"] = $type ? ts('- select %1 -', array(strtolower($contactTypes[$type]['label']))) : ts('- select contact -'); | |
511 | } | |
512 | } | |
513 | } | |
514 | return $jsData; | |
515 | } | |
516 | ||
f55fb370 | 517 | /** |
518 | * Handling 'delete relationship' action | |
519 | * | |
520 | * @param int $id | |
521 | * Relationship ID | |
522 | */ | |
523 | private function deleteAction($id) { | |
524 | CRM_Contact_BAO_Relationship::del($id); | |
525 | ||
526 | // reload all blocks to reflect this change on the user interface. | |
527 | $this->ajaxResponse['reloadBlocks'] = array('#crm-contactinfo-content'); | |
528 | } | |
529 | ||
530 | /** | |
531 | * Handling updating relationship action | |
532 | * | |
533 | * @param array $params | |
534 | * | |
535 | * @return array | |
425d6064 | 536 | * @throws \CRM_Core_Exception |
f55fb370 | 537 | */ |
538 | private function updateAction($params) { | |
425d6064 | 539 | list($params, $_) = $this->preparePostProcessParameters($params); |
00d84e9b JP |
540 | try { |
541 | civicrm_api3('relationship', 'create', $params); | |
542 | } | |
543 | catch (CiviCRM_API3_Exception $e) { | |
544 | throw new CRM_Core_Exception('Relationship create error ' . $e->getMessage()); | |
545 | } | |
f55fb370 | 546 | |
f55fb370 | 547 | $this->setMessage(array('saved' => TRUE)); |
f55fb370 | 548 | return array($params, array($this->_relationshipId)); |
549 | } | |
550 | ||
551 | /** | |
552 | * Handling creating relationship action | |
553 | * | |
554 | * @param array $params | |
555 | * | |
556 | * @return array | |
425d6064 | 557 | * @throws \CRM_Core_Exception |
f55fb370 | 558 | */ |
559 | private function createAction($params) { | |
560 | list($params, $primaryContactLetter) = $this->preparePostProcessParameters($params); | |
561 | ||
562 | $outcome = CRM_Contact_BAO_Relationship::createMultiple($params, $primaryContactLetter); | |
563 | ||
564 | $relationshipIds = $outcome['relationship_ids']; | |
565 | ||
566 | $this->setMessage($outcome); | |
567 | ||
568 | return array($params, $relationshipIds); | |
569 | } | |
570 | ||
571 | ||
572 | /** | |
573 | * Prepares parameters to be used for create/update actions | |
574 | * | |
78f30a19 | 575 | * @param array $values |
f55fb370 | 576 | * |
577 | * @return array | |
578 | */ | |
78f30a19 MWMC |
579 | private function preparePostProcessParameters($values) { |
580 | $params = $values; | |
2a798409 | 581 | list($relationshipTypeId, $a, $b) = explode('_', $params['relationship_type_id']); |
f55fb370 | 582 | |
78f30a19 | 583 | $params['relationship_type_id'] = $relationshipTypeId; |
2a798409 | 584 | $params['contact_id_' . $a] = $this->_contactId; |
f55fb370 | 585 | |
586 | if (empty($this->_relationshipId)) { | |
2a798409 | 587 | $params['contact_id_' . $b] = explode(',', $params['related_contact_id']); |
f55fb370 | 588 | } |
589 | else { | |
590 | $params['id'] = $this->_relationshipId; | |
2a798409 | 591 | $params['contact_id_' . $b] = $params['related_contact_id']; |
f55fb370 | 592 | } |
593 | ||
2a798409 CW |
594 | // If this is a b_a relationship these form elements are flipped |
595 | $params['is_permission_a_b'] = CRM_Utils_Array::value("is_permission_{$a}_{$b}", $values, 0); | |
596 | $params['is_permission_b_a'] = CRM_Utils_Array::value("is_permission_{$b}_{$a}", $values, 0); | |
f55fb370 | 597 | |
2a798409 | 598 | return array($params, $a); |
f55fb370 | 599 | } |
600 | ||
601 | /** | |
602 | * Updates/Creates relationship notes | |
603 | * | |
604 | * @param array $relationshipIds | |
605 | * @param string $note | |
425d6064 MWMC |
606 | * |
607 | * @throws \CiviCRM_API3_Exception | |
f55fb370 | 608 | */ |
609 | private function saveRelationshipNotes($relationshipIds, $note) { | |
610 | foreach ($relationshipIds as $id) { | |
611 | $noteParams = array( | |
612 | 'entity_id' => $id, | |
613 | 'entity_table' => 'civicrm_relationship', | |
614 | ); | |
45caf31c | 615 | |
f55fb370 | 616 | $existing = civicrm_api3('note', 'get', $noteParams); |
617 | if (!empty($existing['id'])) { | |
618 | $noteParams['id'] = $existing['id']; | |
619 | } | |
45caf31c | 620 | |
621 | $action = NULL; | |
622 | if (!empty($note)) { | |
623 | $action = 'create'; | |
624 | $noteParams['note'] = $note; | |
625 | $noteParams['contact_id'] = $this->_contactId; | |
626 | } | |
627 | elseif (!empty($noteParams['id'])) { | |
628 | $action = 'delete'; | |
629 | } | |
630 | ||
631 | if (!empty($action)) { | |
f55fb370 | 632 | civicrm_api3('note', $action, $noteParams); |
633 | } | |
634 | } | |
635 | } | |
636 | ||
637 | /** | |
638 | * Sets current employee/employer relationship | |
639 | * | |
640 | * @param $params | |
641 | * @param array $relationshipIds | |
642 | */ | |
643 | private function setEmploymentRelationship($params, $relationshipIds) { | |
df0c42cc JP |
644 | $employerParams = array(); |
645 | foreach ($relationshipIds as $id) { | |
646 | if (!CRM_Contact_BAO_Relationship::isCurrentEmployerNeedingToBeCleared($params, $id) | |
647 | //don't think this is required to check again. | |
648 | && $this->_allRelationshipNames[$params['relationship_type_id']]["name_a_b"] == 'Employee of') { | |
f55fb370 | 649 | // Fixme this is dumb why do we have to look this up again? |
650 | $rel = CRM_Contact_BAO_Relationship::getRelationshipByID($id); | |
651 | $employerParams[$rel->contact_id_a] = $rel->contact_id_b; | |
652 | } | |
df0c42cc JP |
653 | } |
654 | if (!empty($employerParams)) { | |
f55fb370 | 655 | // @todo this belongs in the BAO. |
656 | CRM_Contact_BAO_Contact_Utils::setCurrentEmployer($employerParams); | |
f55fb370 | 657 | } |
658 | } | |
659 | ||
6a488035 | 660 | } |