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