Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
18 | /** | |
ce064e4f | 19 | * This class generates form components for Relationship Type. |
6a488035 TO |
20 | */ |
21 | class CRM_Admin_Form_RelationshipType extends CRM_Admin_Form { | |
22 | ||
d84ae5f6 | 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) | |
d5e4784e | 31 | * Auto-added by setEntityFieldsMetadata unless specified here (use description => '' to hide) |
d84ae5f6 | 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 | |
a2fdee87 | 37 | * - required |
4d876348 | 38 | * - is_freeze (field should be frozen). |
39 | * | |
d84ae5f6 | 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', | |
a2fdee87 | 51 | 'description' => ts("Label for the relationship from Contact A to Contact B. EXAMPLE: Contact A is 'Parent of' Contact B."), |
52 | 'required' => TRUE, | |
d84ae5f6 | 53 | ], |
54 | 'label_b_a' => [ | |
55 | 'name' => 'label_b_a', | |
0d48f1cc | 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)."), |
d84ae5f6 | 57 | ], |
d5e4784e MWMC |
58 | 'description' => [ |
59 | 'name' => 'description', | |
0d48f1cc | 60 | 'description' => '', |
d5e4784e | 61 | ], |
d84ae5f6 | 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 | ]; | |
d5e4784e MWMC |
66 | |
67 | self::setEntityFieldsMetadata(); | |
d84ae5f6 | 68 | } |
69 | ||
70 | /** | |
71 | * Deletion message to be assigned to the form. | |
72 | * | |
73 | * @var string | |
74 | */ | |
75 | protected $deleteMessage; | |
76 | ||
a9999eb6 | 77 | /** |
78 | * Explicitly declare the entity api name. | |
79 | */ | |
80 | public function getDefaultEntity() { | |
81 | return 'RelationshipType'; | |
82 | } | |
83 | ||
d84ae5f6 | 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 | ||
6a488035 | 93 | /** |
eceb18cc | 94 | * Build the form object. |
6a488035 TO |
95 | */ |
96 | public function buildQuickForm() { | |
4d876348 | 97 | $isReserved = ($this->_id && CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', $this->_id, 'is_reserved')); |
98 | $this->entityFields['is_active']['is_freeze'] = $isReserved; | |
6a488035 | 99 | |
d84ae5f6 | 100 | self::buildQuickEntityForm(); |
6a488035 | 101 | if ($this->_action & CRM_Core_Action::DELETE) { |
6a488035 TO |
102 | return; |
103 | } | |
104 | ||
6a488035 | 105 | $this->addRule('label_a_b', ts('Label already exists in Database.'), |
be2fb01f | 106 | 'objectExists', ['CRM_Contact_DAO_RelationshipType', $this->_id, 'label_a_b'] |
6a488035 | 107 | ); |
6a488035 | 108 | $this->addRule('label_b_a', ts('Label already exists in Database.'), |
be2fb01f | 109 | 'objectExists', ['CRM_Contact_DAO_RelationshipType', $this->_id, 'label_b_a'] |
6a488035 TO |
110 | ); |
111 | ||
9deb4ed3 | 112 | $contactTypes = CRM_Contact_BAO_ContactType::getSelectElements(FALSE, TRUE, '__'); |
4d876348 | 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 . ' ', | |
be2fb01f | 115 | [ |
4d876348 | 116 | '' => ts('All Contacts'), |
be2fb01f | 117 | ] + $contactTypes |
4d876348 | 118 | ); |
119 | if ($isReserved) { | |
120 | $element->freeze(); | |
02fc859b | 121 | } |
6a488035 TO |
122 | } |
123 | ||
124 | if ($this->_action & CRM_Core_Action::VIEW) { | |
125 | $this->freeze(); | |
6a488035 | 126 | } |
f2f99298 | 127 | |
6a488035 TO |
128 | } |
129 | ||
e0ef6999 EM |
130 | /** |
131 | * @return array | |
132 | */ | |
00be9182 | 133 | public function setDefaultValues() { |
6a488035 TO |
134 | if ($this->_action != CRM_Core_Action::DELETE && |
135 | isset($this->_id) | |
136 | ) { | |
be2fb01f CW |
137 | $defaults = $params = []; |
138 | $params = ['id' => $this->_id]; | |
0e6e8724 DL |
139 | $baoName = $this->_BAOName; |
140 | $baoName::retrieve($params, $defaults); | |
9c1bc317 | 141 | $defaults['contact_types_a'] = $defaults['contact_type_a'] ?? NULL; |
a7488080 | 142 | if (!empty($defaults['contact_sub_type_a'])) { |
9deb4ed3 | 143 | $defaults['contact_types_a'] .= '__' . $defaults['contact_sub_type_a']; |
6a488035 TO |
144 | } |
145 | ||
9c1bc317 | 146 | $defaults['contact_types_b'] = $defaults['contact_type_b'] ?? NULL; |
a7488080 | 147 | if (!empty($defaults['contact_sub_type_b'])) { |
9deb4ed3 | 148 | $defaults['contact_types_b'] .= '__' . $defaults['contact_sub_type_b']; |
6a488035 TO |
149 | } |
150 | return $defaults; | |
151 | } | |
152 | else { | |
153 | return parent::setDefaultValues(); | |
154 | } | |
155 | } | |
156 | ||
157 | /** | |
eceb18cc | 158 | * Process the form submission. |
6a488035 TO |
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 { | |
6a488035 TO |
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) { | |
780df37b | 171 | $params['id'] = $this->_id; |
6a488035 TO |
172 | } |
173 | ||
9deb4ed3 | 174 | $cTypeA = CRM_Utils_System::explode('__', |
6a488035 TO |
175 | $params['contact_types_a'], |
176 | 2 | |
177 | ); | |
9deb4ed3 | 178 | $cTypeB = CRM_Utils_System::explode('__', |
6a488035 TO |
179 | $params['contact_types_b'], |
180 | 2 | |
181 | ); | |
182 | ||
183 | $params['contact_type_a'] = $cTypeA[0]; | |
184 | $params['contact_type_b'] = $cTypeB[0]; | |
185 | ||
a9999eb6 | 186 | $params['contact_sub_type_a'] = $cTypeA[1] ? $cTypeA[1] : 'null'; |
187 | $params['contact_sub_type_b'] = $cTypeB[1] ? $cTypeB[1] : 'null'; | |
6a488035 | 188 | |
780df37b | 189 | if (!strlen(trim(CRM_Utils_Array::value('label_b_a', $params)))) { |
9c1bc317 | 190 | $params['label_b_a'] = $params['label_a_b'] ?? NULL; |
780df37b CW |
191 | } |
192 | ||
a9999eb6 | 193 | if (empty($params['id'])) { |
194 | // Set name on created but don't update on update as the machine name is not exposed. | |
2e1b57b5 AF |
195 | $params['name_b_a'] = $params['label_b_a']; |
196 | $params['name_a_b'] = $params['label_a_b']; | |
a9999eb6 | 197 | } |
198 | ||
199 | $result = civicrm_api3('RelationshipType', 'create', $params); | |
4324b8d7 | 200 | |
a9999eb6 | 201 | $this->ajaxResponse['relationshipType'] = $result['values']; |
6a488035 TO |
202 | |
203 | CRM_Core_Session::setStatus(ts('The Relationship Type has been saved.'), ts('Saved'), 'success'); | |
204 | } | |
205 | } | |
e2046b33 | 206 | |
6a488035 | 207 | } |