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