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