Merge pull request #17540 from colemanw/getActionsPerm
[civicrm-core.git] / CRM / Admin / Form / RelationshipType.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * This class generates form components for Relationship Type.
20 */
21 class CRM_Admin_Form_RelationshipType extends CRM_Admin_Form {
22
23 use CRM_Core_Form_EntityFormTrait;
24
25 /**
26 * Fields for the entity to be assigned to the template.
27 *
28 * Fields may have keys
29 * - name (required to show in tpl from the array)
30 * - description (optional, will appear below the field)
31 * Auto-added by setEntityFieldsMetadata unless specified here (use description => '' to hide)
32 * - not-auto-addable - this class will not attempt to add the field using addField.
33 * (this will be automatically set if the field does not have html in it's metadata
34 * or is not a core field on the form's entity).
35 * - help (option) add help to the field - e.g ['id' => 'id-source', 'file' => 'CRM/Contact/Form/Contact']]
36 * - template - use a field specific template to render this field
37 * - required
38 * - is_freeze (field should be frozen).
39 *
40 * @var array
41 */
42 protected $entityFields = [];
43
44 /**
45 * Set entity fields to be assigned to the form.
46 */
47 protected function setEntityFields() {
48 $this->entityFields = [
49 'label_a_b' => [
50 'name' => 'label_a_b',
51 'description' => ts("Label for the relationship from Contact A to Contact B. EXAMPLE: Contact A is 'Parent of' Contact B."),
52 'required' => TRUE,
53 ],
54 'label_b_a' => [
55 'name' => 'label_b_a',
56 'description' => ts("Label for the relationship from Contact B to Contact A. EXAMPLE: Contact B is 'Child of' Contact A. You may leave this blank for relationships where the name is the same in both directions (e.g. Spouse)."),
57 ],
58 'description' => [
59 'name' => 'description',
60 'description' => '',
61 ],
62 'contact_types_a' => ['name' => 'contact_types_a', 'not-auto-addable' => TRUE],
63 'contact_types_b' => ['name' => 'contact_types_b', 'not-auto-addable' => TRUE],
64 'is_active' => ['name' => 'is_active'],
65 ];
66
67 self::setEntityFieldsMetadata();
68 }
69
70 /**
71 * Deletion message to be assigned to the form.
72 *
73 * @var string
74 */
75 protected $deleteMessage;
76
77 /**
78 * Explicitly declare the entity api name.
79 */
80 public function getDefaultEntity() {
81 return 'RelationshipType';
82 }
83
84 /**
85 * Set the delete message.
86 *
87 * We do this from the constructor in order to do a translation.
88 */
89 public function setDeleteMessage() {
90 $this->deleteMessage = ts('WARNING: Deleting this option will result in the loss of all Relationship records of this type.') . ts('This may mean the loss of a substantial amount of data, and the action cannot be undone.') . ts('Do you want to continue?');
91 }
92
93 /**
94 * Build the form object.
95 */
96 public function buildQuickForm() {
97 $isReserved = ($this->_id && CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', $this->_id, 'is_reserved'));
98 $this->entityFields['is_active']['is_freeze'] = $isReserved;
99
100 self::buildQuickEntityForm();
101 if ($this->_action & CRM_Core_Action::DELETE) {
102 return;
103 }
104
105 $this->addRule('label_a_b', ts('Label already exists in Database.'),
106 'objectExists', ['CRM_Contact_DAO_RelationshipType', $this->_id, 'label_a_b']
107 );
108 $this->addRule('label_b_a', ts('Label already exists in Database.'),
109 'objectExists', ['CRM_Contact_DAO_RelationshipType', $this->_id, 'label_b_a']
110 );
111
112 $contactTypes = CRM_Contact_BAO_ContactType::getSelectElements(FALSE, TRUE, '__');
113 foreach (['contact_types_a' => ts('Contact Type A'), 'contact_types_b' => ts('Contact Type B')] as $name => $label) {
114 $element = $this->add('select', $name, $label . ' ',
115 [
116 '' => ts('All Contacts'),
117 ] + $contactTypes
118 );
119 if ($isReserved) {
120 $element->freeze();
121 }
122 }
123
124 if ($this->_action & CRM_Core_Action::VIEW) {
125 $this->freeze();
126 }
127
128 }
129
130 /**
131 * @return array
132 */
133 public function setDefaultValues() {
134 if ($this->_action != CRM_Core_Action::DELETE &&
135 isset($this->_id)
136 ) {
137 $defaults = $params = [];
138 $params = ['id' => $this->_id];
139 $baoName = $this->_BAOName;
140 $baoName::retrieve($params, $defaults);
141 $defaults['contact_types_a'] = $defaults['contact_type_a'] ?? NULL;
142 if (!empty($defaults['contact_sub_type_a'])) {
143 $defaults['contact_types_a'] .= '__' . $defaults['contact_sub_type_a'];
144 }
145
146 $defaults['contact_types_b'] = $defaults['contact_type_b'] ?? NULL;
147 if (!empty($defaults['contact_sub_type_b'])) {
148 $defaults['contact_types_b'] .= '__' . $defaults['contact_sub_type_b'];
149 }
150 return $defaults;
151 }
152 else {
153 return parent::setDefaultValues();
154 }
155 }
156
157 /**
158 * Process the form submission.
159 */
160 public function postProcess() {
161 if ($this->_action & CRM_Core_Action::DELETE) {
162 CRM_Contact_BAO_RelationshipType::del($this->_id);
163 CRM_Core_Session::setStatus(ts('Selected Relationship type has been deleted.'), ts('Record Deleted'), 'success');
164 }
165 else {
166 // store the submitted values in an array
167 $params = $this->exportValues();
168 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
169
170 if ($this->_action & CRM_Core_Action::UPDATE) {
171 $params['id'] = $this->_id;
172 }
173
174 $cTypeA = CRM_Utils_System::explode('__',
175 $params['contact_types_a'],
176 2
177 );
178 $cTypeB = CRM_Utils_System::explode('__',
179 $params['contact_types_b'],
180 2
181 );
182
183 $params['contact_type_a'] = $cTypeA[0];
184 $params['contact_type_b'] = $cTypeB[0];
185
186 $params['contact_sub_type_a'] = $cTypeA[1] ? $cTypeA[1] : 'null';
187 $params['contact_sub_type_b'] = $cTypeB[1] ? $cTypeB[1] : 'null';
188
189 if (!strlen(trim(CRM_Utils_Array::value('label_b_a', $params)))) {
190 $params['label_b_a'] = $params['label_a_b'] ?? NULL;
191 }
192
193 if (empty($params['id'])) {
194 // Set name on created but don't update on update as the machine name is not exposed.
195 $params['name_b_a'] = $params['label_b_a'];
196 $params['name_a_b'] = $params['label_a_b'];
197 }
198
199 $result = civicrm_api3('RelationshipType', 'create', $params);
200
201 $this->ajaxResponse['relationshipType'] = $result['values'];
202
203 CRM_Core_Session::setStatus(ts('The Relationship Type has been saved.'), ts('Saved'), 'success');
204 }
205 }
206
207 }