| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.3 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2013 | |
| 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-2013 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | |
| 36 | /** |
| 37 | * Base class for settings forms |
| 38 | * |
| 39 | */ |
| 40 | class CRM_Admin_Form_Preferences extends CRM_Core_Form { |
| 41 | protected $_system = FALSE; |
| 42 | protected $_contactID = NULL; |
| 43 | protected $_action = NULL; |
| 44 | |
| 45 | protected $_checkbox = NULL; |
| 46 | |
| 47 | protected $_varNames = NULL; |
| 48 | |
| 49 | protected $_config = NULL; |
| 50 | |
| 51 | protected $_params = NULL; |
| 52 | |
| 53 | function preProcess() { |
| 54 | $this->_contactID = CRM_Utils_Request::retrieve('cid', 'Positive', |
| 55 | $this, FALSE |
| 56 | ); |
| 57 | $this->_system = CRM_Utils_Request::retrieve('system', 'Boolean', |
| 58 | $this, FALSE, TRUE |
| 59 | ); |
| 60 | $this->_action = CRM_Utils_Request::retrieve('action', 'String', |
| 61 | $this, FALSE, 'update' |
| 62 | ); |
| 63 | if (isset($action)) { |
| 64 | $this->assign('action', $action); |
| 65 | } |
| 66 | |
| 67 | $session = CRM_Core_Session::singleton(); |
| 68 | |
| 69 | $this->_config = new CRM_Core_DAO(); |
| 70 | |
| 71 | if ($this->_system) { |
| 72 | if (CRM_Core_Permission::check('administer CiviCRM')) { |
| 73 | $this->_contactID = NULL; |
| 74 | } |
| 75 | else { |
| 76 | CRM_Utils_System::fatal('You do not have permission to edit preferences'); |
| 77 | } |
| 78 | $this->_config->contact_id = NULL; |
| 79 | } |
| 80 | else { |
| 81 | if (!$this->_contactID) { |
| 82 | $this->_contactID = $session->get('userID'); |
| 83 | if (!$this->_contactID) { |
| 84 | CRM_Utils_System::fatal('Could not retrieve contact id'); |
| 85 | } |
| 86 | $this->set('cid', $this->_contactID); |
| 87 | } |
| 88 | $this->_config->contact_id = $this->_contactID; |
| 89 | } |
| 90 | |
| 91 | foreach ($this->_varNames as $groupName => $settingNames) { |
| 92 | $values = CRM_Core_BAO_Setting::getItem($groupName); |
| 93 | foreach ($values as $name => $value) { |
| 94 | $this->_config->$name = $value; |
| 95 | } |
| 96 | } |
| 97 | $session->pushUserContext(CRM_Utils_System::url('civicrm/admin', 'reset=1')); |
| 98 | } |
| 99 | |
| 100 | function setDefaultValues() { |
| 101 | $defaults = array(); |
| 102 | |
| 103 | foreach ($this->_varNames as $groupName => $settings) { |
| 104 | foreach ($settings as $settingName => $settingDetails) { |
| 105 | $defaults[$settingName] = isset($this->_config->$settingName) ? $this->_config->$settingName : CRM_Utils_Array::value('default', $settingDetails, NULL); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | return $defaults; |
| 110 | } |
| 111 | |
| 112 | function cbsDefaultValues(&$defaults) { |
| 113 | |
| 114 | foreach ($this->_varNames as $groupName => $groupValues) { |
| 115 | foreach ($groupValues as $settingName => $fieldValue) { |
| 116 | if ($fieldValue['html_type'] == 'checkboxes') { |
| 117 | if (isset($this->_config->$settingName) && |
| 118 | $this->_config->$settingName |
| 119 | ) { |
| 120 | $value = explode(CRM_Core_DAO::VALUE_SEPARATOR, |
| 121 | substr($this->_config->$settingName, 1, -1) |
| 122 | ); |
| 123 | if (!empty($value)) { |
| 124 | $defaults[$settingName] = array(); |
| 125 | foreach ($value as $n => $v) { |
| 126 | $defaults[$settingName][$v] = 1; |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Function to build the form |
| 137 | * |
| 138 | * @return None |
| 139 | * @access public |
| 140 | */ |
| 141 | public function buildQuickForm() { |
| 142 | parent::buildQuickForm(); |
| 143 | |
| 144 | |
| 145 | if (!empty($this->_varNames)) { |
| 146 | foreach ($this->_varNames as $groupName => $groupValues) { |
| 147 | $formName = CRM_Utils_String::titleToVar($groupName); |
| 148 | $this->assign('formName', $formName); |
| 149 | $fields = array(); |
| 150 | foreach ($groupValues as $fieldName => $fieldValue) { |
| 151 | $fields[$fieldName] = $fieldValue; |
| 152 | |
| 153 | switch ($fieldValue['html_type']) { |
| 154 | case 'text': |
| 155 | $this->addElement('text', |
| 156 | $fieldName, |
| 157 | $fieldValue['title'], |
| 158 | array( |
| 159 | 'maxlength' => 64, |
| 160 | 'size' => 32, |
| 161 | ) |
| 162 | ); |
| 163 | break; |
| 164 | |
| 165 | case 'textarea': |
| 166 | $this->addElement('textarea', |
| 167 | $fieldName, |
| 168 | $fieldValue['title'] |
| 169 | ); |
| 170 | break; |
| 171 | |
| 172 | case 'checkbox': |
| 173 | $this->addElement('checkbox', |
| 174 | $fieldName, |
| 175 | $fieldValue['title'] |
| 176 | ); |
| 177 | break; |
| 178 | |
| 179 | case 'checkboxes': |
| 180 | $options = array_flip(CRM_Core_OptionGroup::values($fieldName, FALSE, FALSE, TRUE)); |
| 181 | $newOptions = array(); |
| 182 | foreach ($options as $key => $val) { |
| 183 | $newOptions[$key] = $val; |
| 184 | } |
| 185 | $this->addCheckBox($fieldName, |
| 186 | $fieldValue['title'], |
| 187 | $newOptions, |
| 188 | NULL, NULL, NULL, NULL, |
| 189 | array(' ', ' ', '<br/>') |
| 190 | ); |
| 191 | break; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | $fields = CRM_Utils_Array::crmArraySortByField($fields, 'weight'); |
| 196 | $this->assign('fields', $fields); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | $this->addButtons(array( |
| 201 | array( |
| 202 | 'type' => 'next', |
| 203 | 'name' => ts('Save'), |
| 204 | 'isDefault' => TRUE, |
| 205 | ), |
| 206 | array( |
| 207 | 'type' => 'cancel', |
| 208 | 'name' => ts('Cancel'), |
| 209 | ), |
| 210 | ) |
| 211 | ); |
| 212 | |
| 213 | if ($this->_action == CRM_Core_Action::VIEW) { |
| 214 | $this->freeze(); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Function to process the form |
| 220 | * |
| 221 | * @access public |
| 222 | * |
| 223 | * @return None |
| 224 | */ |
| 225 | public function postProcess() { |
| 226 | $config = CRM_Core_Config::singleton(); |
| 227 | if ($this->_action == CRM_Core_Action::VIEW) { |
| 228 | return; |
| 229 | } |
| 230 | |
| 231 | $this->_params = $this->controller->exportValues($this->_name); |
| 232 | |
| 233 | $this->postProcessCommon(); |
| 234 | } |
| 235 | //end of function |
| 236 | |
| 237 | /** |
| 238 | * Function to process the form |
| 239 | * |
| 240 | * @access public |
| 241 | * |
| 242 | * @return None |
| 243 | */ |
| 244 | public function postProcessCommon() { |
| 245 | foreach ($this->_varNames as $groupName => $groupValues) { |
| 246 | foreach ($groupValues as $settingName => $fieldValue) { |
| 247 | switch ($fieldValue['html_type']) { |
| 248 | case 'checkboxes': |
| 249 | if (CRM_Utils_Array::value($settingName, $this->_params) && |
| 250 | is_array($this->_params[$settingName]) |
| 251 | ) { |
| 252 | $this->_config->$settingName = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, |
| 253 | array_keys($this->_params[$settingName]) |
| 254 | ) . CRM_Core_DAO::VALUE_SEPARATOR; |
| 255 | } |
| 256 | else { |
| 257 | $this->_config->$settingName = NULL; |
| 258 | } |
| 259 | break; |
| 260 | |
| 261 | case 'checkbox': |
| 262 | $this->_config->$settingName = CRM_Utils_Array::value($settingName, $this->_params) ? 1 : 0; |
| 263 | break; |
| 264 | |
| 265 | case 'text': |
| 266 | case 'select': |
| 267 | $this->_config->$settingName = CRM_Utils_Array::value($settingName, $this->_params); |
| 268 | break; |
| 269 | |
| 270 | case 'textarea': |
| 271 | $value = CRM_Utils_Array::value($settingName, $this->_params); |
| 272 | if ($value) { |
| 273 | $value = trim($value); |
| 274 | $value = str_replace(array("\r\n", "\r"), "\n", $value); |
| 275 | } |
| 276 | $this->_config->$settingName = $value; |
| 277 | break; |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | foreach ($this->_varNames as $groupName => $groupValues) { |
| 283 | foreach ($groupValues as $settingName => $fieldValue) { |
| 284 | $settingValue = isset($this->_config->$settingName) ? $this->_config->$settingName : NULL; |
| 285 | CRM_Core_BAO_Setting::setItem($settingValue, |
| 286 | $groupName, |
| 287 | $settingName |
| 288 | ); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | CRM_Core_Session::setStatus(ts('Your changes have been saved.'), ts('Saved'), 'success'); |
| 293 | } |
| 294 | |
| 295 | } |
| 296 | |