Merge pull request #13473 from deb1990/C51-384-add-case-token-in-email
[civicrm-core.git] / CRM / UF / Form / Group.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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-2019
32 */
33
34 /**
35 * This class is for UF Group (Profile) configuration.
36 */
37 class CRM_UF_Form_Group extends CRM_Core_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 * - is_freeze (field should be frozen).
54 *
55 * @var array
56 */
57 protected $entityFields = [];
58
59 /**
60 * Set entity fields to be assigned to the form.
61 */
62 protected function setEntityFields() {
63 $this->entityFields = [
64 'title' => ['name' => 'title'],
65 'frontend_title' => ['name' => 'frontend_title'],
66 'description' => ['name' => 'description', 'help' => ['id' => 'id-description', 'file' => 'CRM/UF/Form/Group.hlp']],
67 'uf_group_type' => ['name' => 'uf_group_type', 'not-auto-addable' => TRUE, 'help' => ['id' => 'id-used_for', 'file' => 'CRM/UF/Form/Group.hlp'], 'post_html_text' => ' ' . $this->getOtherModuleString()],
68 'cancel_button_text' => ['name' => 'cancel_button_text', 'help' => ['id' => 'id-cancel_button_text', 'file' => 'CRM/UF/Form/Group.hlp'], 'class' => 'cancel_button_section'],
69 'submit_button_text' => ['name' => 'submit_button_text', 'help' => ['id' => 'id-submit_button_text', 'file' => 'CRM/UF/Form/Group.hlp'], 'class' => ''],
70 ];
71 }
72
73 /**
74 * Explicitly declare the entity api name.
75 */
76 public function getDefaultEntity() {
77 return 'UFGroup';
78 }
79
80 /**
81 * The form id saved to the session for an update.
82 *
83 * @var int
84 */
85 protected $_id;
86
87 /**
88 * The title for group.
89 *
90 * @var int
91 */
92 protected $_title;
93 protected $_groupElement;
94 protected $_group;
95 protected $_allPanes;
96
97 /**
98 * Set variables up before form is built.
99 */
100 public function preProcess() {
101 // current form id
102 $this->_id = $this->get('id');
103 if (!$this->_id) {
104 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE, 0);
105 }
106 $this->assign('gid', $this->_id);
107 $this->_group = CRM_Core_PseudoConstant::group();
108
109 if ($this->_action & (CRM_Core_Action::UPDATE | CRM_Core_Action::DELETE)) {
110 $title = CRM_Core_BAO_UFGroup::getTitle($this->_id);
111 $this->assign('profileTitle', $title);
112 }
113
114 // setting title for html page
115 if ($this->_action & CRM_Core_Action::UPDATE) {
116 CRM_Utils_System::setTitle(ts('Profile Settings') . " - $title");
117 }
118 elseif ($this->_action & (CRM_Core_Action::DISABLE | CRM_Core_Action::DELETE)) {
119 $ufGroup['module'] = implode(' , ', CRM_Core_BAO_UFGroup::getUFJoinRecord($this->_id, TRUE));
120 $status = 0;
121 $status = CRM_Core_BAO_UFGroup::usedByModule($this->_id);
122 if ($this->_action & (CRM_Core_Action::DISABLE)) {
123 if ($status) {
124 $message = 'This profile is currently used for ' . $ufGroup['module'] . '. If you disable the profile - it will be removed from these forms and/or modules. Do you want to continue?';
125 }
126 else {
127 $message = 'Are you sure you want to disable this Profile?';
128 }
129 }
130 else {
131 if ($status) {
132 $message = 'This profile is currently used for ' . $ufGroup['module'] . '. If you delete the profile - it will be removed from these forms and/or modules. This action cannot be undone. Do you want to continue?';
133 }
134 else {
135 $message = 'Are you sure you want to delete this Profile? This action cannot be undone.';
136 }
137 }
138 $this->assign('message', $message);
139 }
140 else {
141 CRM_Utils_System::setTitle(ts('New CiviCRM Profile'));
142 }
143 }
144
145 /**
146 * Build the form object.
147 *
148 * @return void
149 */
150 public function buildQuickForm() {
151 self::buildQuickEntityForm();
152 if ($this->_action & (CRM_Core_Action::DISABLE | CRM_Core_Action::DELETE)) {
153 if ($this->_action & (CRM_Core_Action::DISABLE)) {
154 $display = 'Disable Profile';
155 }
156 else {
157 $display = 'Delete Profile';
158 }
159 $this->addButtons(array(
160 array(
161 'type' => 'next',
162 'name' => $display,
163 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
164 'isDefault' => TRUE,
165 ),
166 array(
167 'type' => 'cancel',
168 'name' => ts('Cancel'),
169 ),
170 ));
171 return;
172 }
173
174 //add checkboxes
175 $uf_group_type = array();
176 $UFGroupType = CRM_Core_SelectValues::ufGroupTypes();
177 foreach ($UFGroupType as $key => $value) {
178 $uf_group_type[] = $this->createElement('checkbox', $key, NULL, $value);
179 }
180 $this->addGroup($uf_group_type, 'uf_group_type', ts('Used For'), '&nbsp;');
181
182 // help text
183 $this->add('wysiwyg', 'help_pre', ts('Pre-form Help'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_UFGroup', 'help_post'));
184 $this->add('wysiwyg', 'help_post', ts('Post-form Help'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_UFGroup', 'help_post'));
185
186 // weight
187 $this->add('text', 'weight', ts('Order'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_UFJoin', 'weight'), TRUE);
188 $this->addRule('weight', ts('is a numeric field'), 'numeric');
189
190 // is this group active ?
191 $this->addElement('checkbox', 'is_active', ts('Is this CiviCRM Profile active?'));
192
193 $paneNames = array('Advanced Settings' => 'buildAdvanceSetting');
194
195 foreach ($paneNames as $name => $type) {
196 if ($this->_id) {
197 $dataURL = "&reset=1&action=update&id={$this->_id}&snippet=4&formType={$type}";
198 }
199 else {
200 $dataURL = "&reset=1&action=add&snippet=4&formType={$type}";
201 }
202
203 $allPanes[$name] = array(
204 'url' => CRM_Utils_System::url('civicrm/admin/uf/group/setting',
205 $dataURL
206 ),
207 'open' => 'false',
208 'id' => $type,
209 );
210
211 CRM_UF_Form_AdvanceSetting::$type($this);
212 }
213
214 $this->addButtons(array(
215 array(
216 'type' => 'next',
217 'name' => ts('Save'),
218 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
219 'isDefault' => TRUE,
220 ),
221 array(
222 'type' => 'cancel',
223 'name' => ts('Cancel'),
224 ),
225 ));
226
227 // views are implemented as frozen form
228 if ($this->_action & CRM_Core_Action::VIEW) {
229 $this->freeze();
230 $this->addElement('button', 'done', ts('Done'), array('onclick' => "location.href='civicrm/admin/uf/group?reset=1&action=browse'"));
231 }
232
233 $this->addFormRule(array('CRM_UF_Form_Group', 'formRule'), $this);
234 }
235
236 /**
237 * Set default values for the form. Note that in edit/view mode
238 * the default values are retrieved from the database
239 *
240 *
241 * @return void
242 */
243 public function setDefaultValues() {
244 $defaults = array();
245 $showHide = new CRM_Core_ShowHideBlocks();
246
247 if ($this->_action == CRM_Core_Action::ADD) {
248 $defaults['weight'] = CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_UFJoin');
249 }
250
251 //id fetched for Dojo Pane
252 $pId = CRM_Utils_Request::retrieve('id', 'Positive', $this);
253 if (isset($pId)) {
254 $this->_id = $pId;
255 }
256
257 if ((isset($this->_id))) {
258
259 $defaults['weight'] = CRM_Core_BAO_UFGroup::getWeight($this->_id);
260
261 $params = array('id' => $this->_id);
262 CRM_Core_BAO_UFGroup::retrieve($params, $defaults);
263 $defaults['group'] = CRM_Utils_Array::value('limit_listings_group_id', $defaults);
264 $defaults['add_contact_to_group'] = CRM_Utils_Array::value('add_to_group_id', $defaults);
265 //get the uf join records for current uf group
266 $ufJoinRecords = CRM_Core_BAO_UFGroup::getUFJoinRecord($this->_id);
267 foreach ($ufJoinRecords as $key => $value) {
268 $checked[$value] = 1;
269 }
270 $defaults['uf_group_type'] = isset($checked) ? $checked : "";
271
272 $showAdvanced = 0;
273 $advFields = array(
274 'group',
275 'post_URL',
276 'cancel_URL',
277 'add_captcha',
278 'is_map',
279 'is_uf_link',
280 'is_edit_link',
281 'is_update_dupe',
282 'is_cms_user',
283 'is_proximity_search',
284 );
285 foreach ($advFields as $key) {
286 if (!empty($defaults[$key])) {
287 $showAdvanced = 1;
288 $this->_allPanes['Advanced Settings']['open'] = 'true';
289 break;
290 }
291 }
292 }
293 else {
294 $defaults['add_cancel_button'] = 1;
295 $defaults['is_active'] = 1;
296 $defaults['is_map'] = 0;
297 $defaults['is_update_dupe'] = 0;
298 $defaults['is_proximity_search'] = 0;
299 }
300 // Don't assign showHide elements to template in DELETE mode (fields to be shown and hidden don't exist)
301 if (!($this->_action & CRM_Core_Action::DELETE) && !($this->_action & CRM_Core_Action::DISABLE)) {
302 $showHide->addToTemplate();
303 }
304 $this->assign('allPanes', $this->_allPanes);
305 return $defaults;
306 }
307
308 /**
309 * Global form rule.
310 *
311 * @param array $fields
312 * The input form values.
313 * @param array $files
314 * The uploaded files if any.
315 * @param array $self
316 * Current form object.
317 *
318 * @return bool|array
319 * true if no errors, else array of errors
320 */
321 public static function formRule($fields, $files, $self) {
322 $errors = array();
323
324 //validate profile title as well as name.
325 $title = $fields['title'];
326 $name = CRM_Utils_String::munge($title, '_', 56);
327 $name .= $self->_id ? '_' . $self->_id : '';
328 $query = 'select count(*) from civicrm_uf_group where ( name like %1 ) and id != %2';
329 $pCnt = CRM_Core_DAO::singleValueQuery($query, array(
330 1 => array($name, 'String'),
331 2 => array((int) $self->_id, 'Integer'),
332 ));
333 if ($pCnt) {
334 $errors['title'] = ts('Profile \'%1\' already exists in Database.', array(1 => $title));
335 }
336
337 return empty($errors) ? TRUE : $errors;
338 }
339
340 /**
341 * Process the form.
342 *
343 * @return void
344 */
345 public function postProcess() {
346 if ($this->_action & CRM_Core_Action::DELETE) {
347 $title = CRM_Core_BAO_UFGroup::getTitle($this->_id);
348 CRM_Core_BAO_UFGroup::del($this->_id);
349 CRM_Core_Session::setStatus(ts("Your CiviCRM Profile '%1' has been deleted.", array(1 => $title)), ts('Profile Deleted'), 'success');
350 }
351 elseif ($this->_action & CRM_Core_Action::DISABLE) {
352 $ufJoinParams = array('uf_group_id' => $this->_id);
353 CRM_Core_BAO_UFGroup::delUFJoin($ufJoinParams);
354
355 CRM_Core_BAO_UFGroup::setIsActive($this->_id, 0);
356 }
357 else {
358 // get the submitted form values.
359 $ids = array();
360 $params = $this->controller->exportValues($this->_name);
361
362 if (!array_key_exists('is_active', $params)) {
363 $params['is_active'] = 0;
364 }
365
366 if ($this->_action & (CRM_Core_Action::UPDATE)) {
367 $ids['ufgroup'] = $this->_id;
368 // CRM-5284
369 // lets skip trying to mess around with profile weights and allow the user to do as needed.
370 }
371 elseif ($this->_action & CRM_Core_Action::ADD) {
372 $session = CRM_Core_Session::singleton();
373 $params['created_id'] = $session->get('userID');
374 $params['created_date'] = date('YmdHis');
375 }
376
377 // create uf group
378 $ufGroup = CRM_Core_BAO_UFGroup::add($params, $ids);
379
380 if (!empty($params['is_active'])) {
381 //make entry in uf join table
382 CRM_Core_BAO_UFGroup::createUFJoin($params, $ufGroup->id);
383 }
384 elseif ($this->_id) {
385 // this profile has been set to inactive, delete all corresponding UF Join's
386 $ufJoinParams = array('uf_group_id' => $this->_id);
387 CRM_Core_BAO_UFGroup::delUFJoin($ufJoinParams);
388 }
389
390 if ($this->_action & CRM_Core_Action::UPDATE) {
391 $url = CRM_Utils_System::url('civicrm/admin/uf/group', 'reset=1&action=browse');
392 CRM_Core_Session::setStatus(ts("Your CiviCRM Profile '%1' has been saved.", array(1 => $ufGroup->title)), ts('Profile Saved'), 'success');
393 }
394 else {
395 // Jump directly to adding a field if popups are disabled
396 $action = CRM_Core_Resources::singleton()->ajaxPopupsEnabled ? '' : '/add';
397 $url = CRM_Utils_System::url("civicrm/admin/uf/group/field$action", 'reset=1&new=1&gid=' . $ufGroup->id . '&action=' . ($action ? 'add' : 'browse'));
398 CRM_Core_Session::setStatus(ts('Your CiviCRM Profile \'%1\' has been added. You can add fields to this profile now.',
399 array(1 => $ufGroup->title)
400 ), ts('Profile Added'), 'success');
401 }
402 $session = CRM_Core_Session::singleton();
403 $session->replaceUserContext($url);
404 }
405
406 // update cms integration with registration / my account
407 CRM_Utils_System::updateCategories();
408 }
409
410 /**
411 * Set the delete message.
412 *
413 * We do this from the constructor in order to do a translation.
414 */
415 public function setDeleteMessage() {}
416
417 /**
418 * Get the string to display next to the used for field indicating unchangeable uses.
419 *
420 * @return string
421 */
422 protected function getOtherModuleString() {
423 $otherModules = CRM_Core_BAO_UFGroup::getUFJoinRecord($this->_id, TRUE, TRUE);
424 if (!empty($otherModules)) {
425 $otherModuleString = NULL;
426 foreach ($otherModules as $key) {
427 $otherModuleString .= " [ x ] <label>" . $key . "</label>";
428 }
429 }
430 return $otherModuleString;
431 }
432
433 }