| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
| 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-2015 |
| 32 | */ |
| 33 | |
| 34 | /** |
| 35 | * This class generates form components generic to CiviCRM settings. |
| 36 | */ |
| 37 | class CRM_Admin_Form_Setting extends CRM_Core_Form { |
| 38 | |
| 39 | protected $_settings = array(); |
| 40 | |
| 41 | /** |
| 42 | * Set default values for the form. |
| 43 | * |
| 44 | * Default values are retrieved from the database. |
| 45 | */ |
| 46 | public function setDefaultValues() { |
| 47 | if (!$this->_defaults) { |
| 48 | $this->_defaults = array(); |
| 49 | $formArray = array('Component', 'Localization'); |
| 50 | $formMode = FALSE; |
| 51 | if (in_array($this->_name, $formArray)) { |
| 52 | $formMode = TRUE; |
| 53 | } |
| 54 | |
| 55 | CRM_Core_BAO_ConfigSetting::retrieve($this->_defaults); |
| 56 | |
| 57 | CRM_Core_Config_Defaults::setValues($this->_defaults, $formMode); |
| 58 | |
| 59 | $list = array_flip(CRM_Core_OptionGroup::values('contact_autocomplete_options', |
| 60 | FALSE, FALSE, TRUE, NULL, 'name' |
| 61 | )); |
| 62 | |
| 63 | $cRlist = array_flip(CRM_Core_OptionGroup::values('contact_reference_options', |
| 64 | FALSE, FALSE, TRUE, NULL, 'name' |
| 65 | )); |
| 66 | |
| 67 | $listEnabled = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, |
| 68 | 'contact_autocomplete_options' |
| 69 | ); |
| 70 | $cRlistEnabled = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, |
| 71 | 'contact_reference_options' |
| 72 | ); |
| 73 | |
| 74 | $autoSearchFields = array(); |
| 75 | if (!empty($list) && !empty($listEnabled)) { |
| 76 | $autoSearchFields = array_combine($list, $listEnabled); |
| 77 | } |
| 78 | |
| 79 | $cRSearchFields = array(); |
| 80 | if (!empty($cRlist) && !empty($cRlistEnabled)) { |
| 81 | $cRSearchFields = array_combine($cRlist, $cRlistEnabled); |
| 82 | } |
| 83 | |
| 84 | //Set defaults for autocomplete and contact reference options |
| 85 | $this->_defaults['autocompleteContactSearch'] = array( |
| 86 | '1' => 1, |
| 87 | ) + $autoSearchFields; |
| 88 | $this->_defaults['autocompleteContactReference'] = array( |
| 89 | '1' => 1, |
| 90 | ) + $cRSearchFields; |
| 91 | |
| 92 | // we can handle all the ones defined in the metadata here. Others to be converted |
| 93 | foreach ($this->_settings as $setting => $group) { |
| 94 | $settingMetaData = civicrm_api('setting', 'getfields', array('version' => 3, 'name' => $setting)); |
| 95 | $this->_defaults[$setting] = civicrm_api('setting', 'getvalue', array( |
| 96 | 'version' => 3, |
| 97 | 'name' => $setting, |
| 98 | 'group' => $group, |
| 99 | 'default_value' => CRM_Utils_Array::value('default', $settingMetaData['values'][$setting]), |
| 100 | ) |
| 101 | ); |
| 102 | } |
| 103 | |
| 104 | $this->_defaults['enableSSL'] = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'enableSSL'); |
| 105 | $this->_defaults['verifySSL'] = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'verifySSL'); |
| 106 | } |
| 107 | |
| 108 | return $this->_defaults; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Build the form object. |
| 113 | */ |
| 114 | public function buildQuickForm() { |
| 115 | $session = CRM_Core_Session::singleton(); |
| 116 | $session->pushUserContext(CRM_Utils_System::url('civicrm/admin', 'reset=1')); |
| 117 | $args = func_get_args(); |
| 118 | $check = reset($args); |
| 119 | $this->addButtons(array( |
| 120 | array( |
| 121 | 'type' => 'next', |
| 122 | 'name' => ts('Save'), |
| 123 | 'isDefault' => TRUE, |
| 124 | ), |
| 125 | array( |
| 126 | 'type' => 'cancel', |
| 127 | 'name' => ts('Cancel'), |
| 128 | ), |
| 129 | ) |
| 130 | ); |
| 131 | |
| 132 | foreach ($this->_settings as $setting => $group) { |
| 133 | $settingMetaData = civicrm_api('setting', 'getfields', array('version' => 3, 'name' => $setting)); |
| 134 | $props = $settingMetaData['values'][$setting]; |
| 135 | if (isset($props['quick_form_type'])) { |
| 136 | $add = 'add' . $props['quick_form_type']; |
| 137 | if ($add == 'addElement') { |
| 138 | $this->$add( |
| 139 | $props['html_type'], |
| 140 | $setting, |
| 141 | ts($props['title']), |
| 142 | CRM_Utils_Array::value($props['html_type'] == 'select' ? 'option_values' : 'html_attributes', $props, array()), |
| 143 | $props['html_type'] == 'select' ? CRM_Utils_Array::value('html_attributes', $props) : NULL |
| 144 | ); |
| 145 | } |
| 146 | elseif ($add == 'addSelect') { |
| 147 | $options = civicrm_api3('Setting', 'getoptions', array( |
| 148 | 'field' => $setting, |
| 149 | )); |
| 150 | $this->addElement('select', $setting, ts($props['title']), $options['values'], CRM_Utils_Array::value('html_attributes', $props)); |
| 151 | } |
| 152 | else { |
| 153 | $this->$add($setting, ts($props['title'])); |
| 154 | } |
| 155 | $this->assign("{$setting}_description", ts($props['description'])); |
| 156 | if ($setting == 'max_attachments') { |
| 157 | //temp hack @todo fix to get from metadata |
| 158 | $this->addRule('max_attachments', ts('Value should be a positive number'), 'positiveInteger'); |
| 159 | } |
| 160 | if ($setting == 'maxFileSize') { |
| 161 | //temp hack |
| 162 | $this->addRule('maxFileSize', ts('Value should be a positive number'), 'positiveInteger'); |
| 163 | } |
| 164 | |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Get default entity. |
| 171 | * |
| 172 | * @return string |
| 173 | */ |
| 174 | public function getDefaultEntity() { |
| 175 | return 'Setting'; |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Process the form submission. |
| 180 | */ |
| 181 | public function postProcess() { |
| 182 | // store the submitted values in an array |
| 183 | $params = $this->controller->exportValues($this->_name); |
| 184 | |
| 185 | self::commonProcess($params); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Common Process. |
| 190 | * |
| 191 | * @todo Document what I do. |
| 192 | * |
| 193 | * @param array $params |
| 194 | */ |
| 195 | public function commonProcess(&$params) { |
| 196 | |
| 197 | // save autocomplete search options |
| 198 | if (!empty($params['autocompleteContactSearch'])) { |
| 199 | $value = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, |
| 200 | array_keys($params['autocompleteContactSearch']) |
| 201 | ) . CRM_Core_DAO::VALUE_SEPARATOR; |
| 202 | |
| 203 | CRM_Core_BAO_Setting::setItem($value, |
| 204 | CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, |
| 205 | 'contact_autocomplete_options' |
| 206 | ); |
| 207 | |
| 208 | unset($params['autocompleteContactSearch']); |
| 209 | } |
| 210 | |
| 211 | // save autocomplete contact reference options |
| 212 | if (!empty($params['autocompleteContactReference'])) { |
| 213 | $value = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, |
| 214 | array_keys($params['autocompleteContactReference']) |
| 215 | ) . CRM_Core_DAO::VALUE_SEPARATOR; |
| 216 | |
| 217 | CRM_Core_BAO_Setting::setItem($value, |
| 218 | CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, |
| 219 | 'contact_reference_options' |
| 220 | ); |
| 221 | |
| 222 | unset($params['autocompleteContactReference']); |
| 223 | } |
| 224 | |
| 225 | // save components to be enabled |
| 226 | if (array_key_exists('enableComponents', $params)) { |
| 227 | civicrm_api3('setting', 'create', array( |
| 228 | 'enable_components' => $params['enableComponents'], |
| 229 | )); |
| 230 | unset($params['enableComponents']); |
| 231 | } |
| 232 | |
| 233 | // verify ssl peer option |
| 234 | if (isset($params['verifySSL'])) { |
| 235 | CRM_Core_BAO_Setting::setItem($params['verifySSL'], |
| 236 | CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, |
| 237 | 'verifySSL' |
| 238 | ); |
| 239 | unset($params['verifySSL']); |
| 240 | } |
| 241 | |
| 242 | // force secure URLs |
| 243 | if (isset($params['enableSSL'])) { |
| 244 | CRM_Core_BAO_Setting::setItem($params['enableSSL'], |
| 245 | CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, |
| 246 | 'enableSSL' |
| 247 | ); |
| 248 | unset($params['enableSSL']); |
| 249 | } |
| 250 | $settings = array_intersect_key($params, $this->_settings); |
| 251 | $result = civicrm_api('setting', 'create', $settings + array('version' => 3)); |
| 252 | foreach ($settings as $setting => $settingGroup) { |
| 253 | //@todo array_diff this |
| 254 | unset($params[$setting]); |
| 255 | } |
| 256 | if (!empty($result['error_message'])) { |
| 257 | CRM_Core_Session::setStatus($result['error_message'], ts('Save Failed'), 'error'); |
| 258 | } |
| 259 | |
| 260 | CRM_Core_BAO_ConfigSetting::create($params); |
| 261 | |
| 262 | CRM_Core_Config::clearDBCache(); |
| 263 | CRM_Utils_System::flushCache(); |
| 264 | CRM_Core_Resources::singleton()->resetCacheCode(); |
| 265 | |
| 266 | CRM_Core_Session::setStatus(" ", ts('Changes Saved'), "success"); |
| 267 | } |
| 268 | |
| 269 | public function rebuildMenu() { |
| 270 | // ensure config is set with new values |
| 271 | $config = CRM_Core_Config::singleton(TRUE, TRUE); |
| 272 | |
| 273 | // rebuild menu items |
| 274 | CRM_Core_Menu::store(); |
| 275 | |
| 276 | // also delete the IDS file so we can write a new correct one on next load |
| 277 | $configFile = $config->uploadDir . 'Config.IDS.ini'; |
| 278 | @unlink($configFile); |
| 279 | } |
| 280 | |
| 281 | } |