Make label field on relationship type required again (accidentally removed
[civicrm-core.git] / CRM / Admin / Form / RelationshipType.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2018
32 */
33
34 /**
35 * This class generates form components for Relationship Type.
36 */
37 class CRM_Admin_Form_RelationshipType extends CRM_Admin_Form {
38
39 use CRM_Core_Form_EntityFormTrait;
40
41 /**
42 * Fields for the entity to be assigned to the template.
43 *
44 * Fields may have keys
45 * - name (required to show in tpl from the array)
46 * - description (optional, will appear below the field)
47 * - not-auto-addable - this class will not attempt to add the field using addField.
48 * (this will be automatically set if the field does not have html in it's metadata
49 * or is not a core field on the form's entity).
50 * - help (option) add help to the field - e.g ['id' => 'id-source', 'file' => 'CRM/Contact/Form/Contact']]
51 * - template - use a field specific template to render this field
52 * - required
53 * @var array
54 */
55 protected $entityFields = [];
56
57 /**
58 * Set entity fields to be assigned to the form.
59 */
60 protected function setEntityFields() {
61 $this->entityFields = [
62 'label_a_b' => [
63 'name' => 'label_a_b',
64 'description' => ts("Label for the relationship from Contact A to Contact B. EXAMPLE: Contact A is 'Parent of' Contact B."),
65 'required' => TRUE,
66 ],
67 'label_b_a' => [
68 'name' => 'label_b_a',
69 '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).")
70 ],
71 'description' => ['name' => 'description'],
72 'contact_types_a' => ['name' => 'contact_types_a', 'not-auto-addable' => TRUE],
73 'contact_types_b' => ['name' => 'contact_types_b', 'not-auto-addable' => TRUE],
74 'is_active' => ['name' => 'is_active'],
75 ];
76 }
77
78 /**
79 * Deletion message to be assigned to the form.
80 *
81 * @var string
82 */
83 protected $deleteMessage;
84
85 /**
86 * Explicitly declare the entity api name.
87 */
88 public function getDefaultEntity() {
89 return 'RelationshipType';
90 }
91
92 /**
93 * Set the delete message.
94 *
95 * We do this from the constructor in order to do a translation.
96 */
97 public function setDeleteMessage() {
98 $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?');
99 }
100
101 /**
102 * Build the form object.
103 */
104 public function buildQuickForm() {
105
106 self::buildQuickEntityForm();
107 if ($this->_action & CRM_Core_Action::DELETE) {
108 return;
109 }
110
111 $this->addRule('label_a_b', ts('Label already exists in Database.'),
112 'objectExists', array('CRM_Contact_DAO_RelationshipType', $this->_id, 'label_a_b')
113 );
114 $this->addRule('label_b_a', ts('Label already exists in Database.'),
115 'objectExists', array('CRM_Contact_DAO_RelationshipType', $this->_id, 'label_b_a')
116 );
117
118 $contactTypes = CRM_Contact_BAO_ContactType::getSelectElements(FALSE, TRUE, '__');
119
120 // add select for contact type
121 $this->add('select', 'contact_types_a', ts('Contact Type A') . ' ',
122 array(
123 '' => ts('All Contacts'),
124 ) + $contactTypes
125 );
126 $this->add('select', 'contact_types_b', ts('Contact Type B') . ' ',
127 array(
128 '' => ts('All Contacts'),
129 ) + $contactTypes
130 );
131
132 //only selected field should be allow for edit, CRM-4888
133 if ($this->_id &&
134 CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', $this->_id, 'is_reserved')
135 ) {
136 foreach (array('contactTypeA', 'contactTypeB', 'isActive') as $field) {
137 $$field->freeze();
138 }
139 }
140
141 if ($this->_action & CRM_Core_Action::VIEW) {
142 $this->freeze();
143 }
144
145 }
146
147 /**
148 * @return array
149 */
150 public function setDefaultValues() {
151 if ($this->_action != CRM_Core_Action::DELETE &&
152 isset($this->_id)
153 ) {
154 $defaults = $params = array();
155 $params = array('id' => $this->_id);
156 $baoName = $this->_BAOName;
157 $baoName::retrieve($params, $defaults);
158 $defaults['contact_types_a'] = CRM_Utils_Array::value('contact_type_a', $defaults);
159 if (!empty($defaults['contact_sub_type_a'])) {
160 $defaults['contact_types_a'] .= '__' . $defaults['contact_sub_type_a'];
161 }
162
163 $defaults['contact_types_b'] = CRM_Utils_Array::value('contact_type_b', $defaults);
164 if (!empty($defaults['contact_sub_type_b'])) {
165 $defaults['contact_types_b'] .= '__' . $defaults['contact_sub_type_b'];
166 }
167 return $defaults;
168 }
169 else {
170 return parent::setDefaultValues();
171 }
172 }
173
174 /**
175 * Process the form submission.
176 */
177 public function postProcess() {
178 if ($this->_action & CRM_Core_Action::DELETE) {
179 CRM_Contact_BAO_RelationshipType::del($this->_id);
180 CRM_Core_Session::setStatus(ts('Selected Relationship type has been deleted.'), ts('Record Deleted'), 'success');
181 }
182 else {
183 // store the submitted values in an array
184 $params = $this->exportValues();
185 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
186
187 if ($this->_action & CRM_Core_Action::UPDATE) {
188 $params['id'] = $this->_id;
189 }
190
191 $cTypeA = CRM_Utils_System::explode('__',
192 $params['contact_types_a'],
193 2
194 );
195 $cTypeB = CRM_Utils_System::explode('__',
196 $params['contact_types_b'],
197 2
198 );
199
200 $params['contact_type_a'] = $cTypeA[0];
201 $params['contact_type_b'] = $cTypeB[0];
202
203 $params['contact_sub_type_a'] = $cTypeA[1] ? $cTypeA[1] : 'null';
204 $params['contact_sub_type_b'] = $cTypeB[1] ? $cTypeB[1] : 'null';
205
206 if (!strlen(trim(CRM_Utils_Array::value('label_b_a', $params)))) {
207 $params['label_b_a'] = CRM_Utils_Array::value('label_a_b', $params);
208 }
209
210 if (empty($params['id'])) {
211 // Set name on created but don't update on update as the machine name is not exposed.
212 $params['name_b_a'] = CRM_Utils_String::munge($params['label_b_a']);
213 $params['name_a_b'] = CRM_Utils_String::munge($params['label_a_b']);
214 }
215
216 $result = civicrm_api3('RelationshipType', 'create', $params);
217
218 $this->ajaxResponse['relationshipType'] = $result['values'];
219
220 CRM_Core_Session::setStatus(ts('The Relationship Type has been saved.'), ts('Saved'), 'success');
221 }
222 }
223
224 }