Merge pull request #13972 from eileenmcnaughton/array_format_5
[civicrm-core.git] / CRM / Case / Form / AddToCaseAsRole.php
1 <?php
2
3 /**
4 * Class CRM_Case_Form_AddToCaseAsRole
5 */
6 class CRM_Case_Form_AddToCaseAsRole extends CRM_Contact_Form_Task {
7
8 /**
9 * @inheritdoc
10 */
11 public function buildQuickForm() {
12
13 $roleTypes = $this->getRoleTypes();
14 $this->add(
15 'select',
16 'role_type',
17 ts('Relationship Type'),
18 ['' => ts('- select type -')] + $roleTypes,
19 TRUE,
20 ['class' => 'crm-select2 twenty']
21 );
22
23 $this->addEntityRef(
24 'assign_to',
25 ts('Assign to'),
26 ['entity' => 'Case'],
27 TRUE
28 );
29
30 $this->addButtons([
31 [
32 'type' => 'submit',
33 'name' => ts('Submit'),
34 'isDefault' => TRUE,
35 ],
36 [
37 'type' => 'cancel',
38 'name' => ts('Cancel'),
39 ],
40 ]);
41 }
42
43 /**
44 * Returns list of configured role types for individuals.
45 *
46 * @return array
47 * List of role types
48 */
49 private function getRoleTypes() {
50 $relType = CRM_Contact_BAO_Relationship::getRelationType('Individual');
51 $roleTypes = [];
52
53 foreach ($relType as $k => $v) {
54 $roleTypes[substr($k, 0, strpos($k, '_'))] = $v;
55 }
56
57 return $roleTypes;
58 }
59
60 /**
61 * @inheritdoc
62 */
63 public function postProcess() {
64 $values = $this->controller->exportValues();
65
66 $caseId = (int) $values['assign_to'];
67 $roleTypeId = (int) $values['role_type'];
68 $contacts = $this->_contactIds;
69
70 $clients = CRM_Case_BAO_Case::getCaseClients($caseId);
71
72 $params = [
73 'contact_id_a' => $clients[0],
74 'contact_id_b' => $contacts,
75 'case_id' => $caseId,
76 'relationship_type_id' => $roleTypeId,
77 ];
78
79 CRM_Contact_BAO_Relationship::createMultiple($params, 'a');
80
81 $url = CRM_Utils_System::url(
82 'civicrm/contact/view/case',
83 [
84 'cid' => $clients[0],
85 'id' => $caseId,
86 'reset' => 1,
87 'action' => 'view',
88 ]
89 );
90 CRM_Utils_System::redirect($url);
91 }
92
93 }