Merge pull request #15826 from seamuslee001/dev_core_183_dedupe
[civicrm-core.git] / CRM / Admin / Form / PreferencesDate.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 Location Type.
20 */
21 class CRM_Admin_Form_PreferencesDate extends CRM_Admin_Form {
22
23 /**
24 * Build the form object.
25 */
26 public function buildQuickForm() {
27
28 parent::buildQuickForm();
29
30 if ($this->_action & CRM_Core_Action::DELETE) {
31 return;
32 }
33
34 $attributes = CRM_Core_DAO::getAttribute('CRM_Core_DAO_PreferencesDate');
35
36 $this->applyFilter('__ALL__', 'trim');
37 $name = &$this->add('text',
38 'name',
39 ts('Name'),
40 $attributes['name'],
41 TRUE
42 );
43 $name->freeze();
44
45 $this->add('text', 'description', ts('Description'), $attributes['description'], FALSE);
46 $this->add('text', 'start', ts('Start Offset'), $attributes['start'], TRUE);
47 $this->add('text', 'end', ts('End Offset'), $attributes['end'], TRUE);
48
49 $formatType = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_PreferencesDate', $this->_id, 'name');
50
51 if ($formatType == 'creditCard') {
52 $this->add('text', 'date_format', ts('Format'), $attributes['date_format'], TRUE);
53 }
54 else {
55 $this->add('select', 'date_format', ts('Format'),
56 ['' => ts('- default input format -')] + CRM_Core_SelectValues::getDatePluginInputFormats()
57 );
58 $this->add('select', 'time_format', ts('Time'),
59 ['' => ts('- none -')] + CRM_Core_SelectValues::getTimeFormats()
60 );
61 }
62 $this->addRule('start', ts('Value must be an integer.'), 'integer');
63 $this->addRule('end', ts('Value must be an integer.'), 'integer');
64
65 // add a form rule
66 $this->addFormRule(['CRM_Admin_Form_PreferencesDate', 'formRule']);
67 }
68
69 /**
70 * Global validation rules for the form.
71 *
72 * @param array $fields
73 * Posted values of the form.
74 *
75 * @return array
76 * if errors then list of errors to be posted back to the form,
77 * true otherwise
78 */
79 public static function formRule($fields) {
80 $errors = [];
81
82 if ($fields['name'] == 'activityDateTime' && !$fields['time_format']) {
83 $errors['time_format'] = ts('Time is required for this format.');
84 }
85
86 return empty($errors) ? TRUE : $errors;
87 }
88
89 /**
90 * Process the form submission.
91 */
92 public function postProcess() {
93 if (!($this->_action & CRM_Core_Action::UPDATE)) {
94 CRM_Core_Session::setStatus(ts('Preferences Date Options can only be updated'), ts('Sorry'), 'error');
95 return;
96 }
97
98 // store the submitted values in an array
99 $params = $this->controller->exportValues($this->_name);
100
101 // action is taken depending upon the mode
102 $dao = new CRM_Core_DAO_PreferencesDate();
103 $dao->id = $this->_id;
104 $dao->description = $params['description'];
105 $dao->start = $params['start'];
106 $dao->end = $params['end'];
107 $dao->date_format = $params['date_format'];
108 $dao->time_format = $params['time_format'];
109
110 $dao->save();
111
112 // Update dynamic js to reflect new date settings
113 CRM_Core_Resources::singleton()->resetCacheCode();
114
115 CRM_Core_Session::setStatus(ts("The date type '%1' has been saved.",
116 [1 => $params['name']]
117 ), ts('Saved'), 'success');
118 }
119
120 }