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 Location Type. |
6a488035 TO |
20 | */ |
21 | class CRM_Admin_Form_PreferencesDate extends CRM_Admin_Form { | |
22 | ||
23 | /** | |
eceb18cc | 24 | * Build the form object. |
6a488035 TO |
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 | ||
395d8dc6 | 49 | $formatType = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_PreferencesDate', $this->_id, 'name'); |
6a488035 TO |
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'), | |
be2fb01f | 56 | ['' => ts('- default input format -')] + CRM_Core_SelectValues::getDatePluginInputFormats() |
6a488035 TO |
57 | ); |
58 | $this->add('select', 'time_format', ts('Time'), | |
be2fb01f | 59 | ['' => ts('- none -')] + CRM_Core_SelectValues::getTimeFormats() |
6a488035 TO |
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 | |
be2fb01f | 66 | $this->addFormRule(['CRM_Admin_Form_PreferencesDate', 'formRule']); |
6a488035 TO |
67 | } |
68 | ||
69 | /** | |
eceb18cc | 70 | * Global validation rules for the form. |
6a488035 | 71 | * |
5173bd95 TO |
72 | * @param array $fields |
73 | * Posted values of the form. | |
6a488035 | 74 | * |
a6c01b45 CW |
75 | * @return array |
76 | * if errors then list of errors to be posted back to the form, | |
6a488035 | 77 | * true otherwise |
6a488035 | 78 | */ |
00be9182 | 79 | public static function formRule($fields) { |
be2fb01f | 80 | $errors = []; |
6a488035 TO |
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 | /** | |
eceb18cc | 90 | * Process the form submission. |
6a488035 TO |
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 | |
353ffa53 TO |
102 | $dao = new CRM_Core_DAO_PreferencesDate(); |
103 | $dao->id = $this->_id; | |
6a488035 | 104 | $dao->description = $params['description']; |
353ffa53 TO |
105 | $dao->start = $params['start']; |
106 | $dao->end = $params['end']; | |
6a488035 TO |
107 | $dao->date_format = $params['date_format']; |
108 | $dao->time_format = $params['time_format']; | |
109 | ||
110 | $dao->save(); | |
111 | ||
112 | CRM_Core_Session::setStatus(ts("The date type '%1' has been saved.", | |
be2fb01f | 113 | [1 => $params['name']] |
353ffa53 | 114 | ), ts('Saved'), 'success'); |
6a488035 | 115 | } |
96025800 | 116 | |
6a488035 | 117 | } |