Setting.php 9.82 KB
Newer Older
totten's avatar
totten committed
1
2
3
<?php
/*
 +--------------------------------------------------------------------+
Kurund Jalmi's avatar
Kurund Jalmi committed
4
 | CiviCRM version 4.7                                                |
totten's avatar
totten committed
5
 +--------------------------------------------------------------------+
colemanw's avatar
colemanw committed
6
 | Copyright CiviCRM LLC (c) 2004-2015                                |
totten's avatar
totten committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 +--------------------------------------------------------------------+
 | This file is a part of CiviCRM.                                    |
 |                                                                    |
 | CiviCRM is free software; you can copy, modify, and distribute it  |
 | under the terms of the GNU Affero General Public License           |
 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
 |                                                                    |
 | CiviCRM is distributed in the hope that it will be useful, but     |
 | WITHOUT ANY WARRANTY; without even the implied warranty of         |
 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
 | See the GNU Affero General Public License for more details.        |
 |                                                                    |
 | You should have received a copy of the GNU Affero General Public   |
 | License and the CiviCRM Licensing Exception along                  |
 | with this program; if not, contact CiviCRM LLC                     |
 | at info[AT]civicrm[DOT]org. If you have questions about the        |
 | GNU Affero General Public License or the licensing of CiviCRM,     |
 | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
 +--------------------------------------------------------------------+
26
 */
totten's avatar
totten committed
27
28
29
30

/**
 *
 * @package CRM
colemanw's avatar
colemanw committed
31
 * @copyright CiviCRM LLC (c) 2004-2015
totten's avatar
totten committed
32
33
34
 */

/**
eileenmcnaugton's avatar
eileenmcnaugton committed
35
 * This class generates form components generic to CiviCRM settings.
totten's avatar
totten committed
36
37
38
39
40
41
 */
class CRM_Admin_Form_Setting extends CRM_Core_Form {

  protected $_settings = array();

  /**
42
   * Set default values for the form.
totten's avatar
totten committed
43
   *
Eileen McNaughton's avatar
Eileen McNaughton committed
44
   * Default values are retrieved from the database.
totten's avatar
totten committed
45
   */
46
  public function setDefaultValues() {
totten's avatar
totten committed
47
48
    if (!$this->_defaults) {
      $this->_defaults = array();
totten's avatar
totten committed
49
50
      $formArray = array('Component', 'Localization');
      $formMode = FALSE;
totten's avatar
totten committed
51
52
53
54
55
56
57
58
59
      if (in_array($this->_name, $formArray)) {
        $formMode = TRUE;
      }

      CRM_Core_BAO_ConfigSetting::retrieve($this->_defaults);

      CRM_Core_Config_Defaults::setValues($this->_defaults, $formMode);

      $list = array_flip(CRM_Core_OptionGroup::values('contact_autocomplete_options',
totten's avatar
totten committed
60
61
        FALSE, FALSE, TRUE, NULL, 'name'
      ));
totten's avatar
totten committed
62
63

      $cRlist = array_flip(CRM_Core_OptionGroup::values('contact_reference_options',
totten's avatar
totten committed
64
65
        FALSE, FALSE, TRUE, NULL, 'name'
      ));
totten's avatar
totten committed
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85

      $listEnabled = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
        'contact_autocomplete_options'
      );
      $cRlistEnabled = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
        'contact_reference_options'
      );

      $autoSearchFields = array();
      if (!empty($list) && !empty($listEnabled)) {
        $autoSearchFields = array_combine($list, $listEnabled);
      }

      $cRSearchFields = array();
      if (!empty($cRlist) && !empty($cRlistEnabled)) {
        $cRSearchFields = array_combine($cRlist, $cRlistEnabled);
      }

      //Set defaults for autocomplete and contact reference options
      $this->_defaults['autocompleteContactSearch'] = array(
Eileen McNaughton's avatar
Eileen McNaughton committed
86
87
        '1' => 1,
      ) + $autoSearchFields;
totten's avatar
totten committed
88
      $this->_defaults['autocompleteContactReference'] = array(
Eileen McNaughton's avatar
Eileen McNaughton committed
89
90
        '1' => 1,
      ) + $cRSearchFields;
totten's avatar
totten committed
91
92

      // we can handle all the ones defined in the metadata here. Others to be converted
totten's avatar
totten committed
93
      foreach ($this->_settings as $setting => $group) {
totten's avatar
totten committed
94
95
        $settingMetaData = civicrm_api('setting', 'getfields', array('version' => 3, 'name' => $setting));
        $this->_defaults[$setting] = civicrm_api('setting', 'getvalue', array(
totten's avatar
totten committed
96
97
98
99
            'version' => 3,
            'name' => $setting,
            'group' => $group,
            'default_value' => CRM_Utils_Array::value('default', $settingMetaData['values'][$setting]),
totten's avatar
totten committed
100
101
102
103
          )
        );
      }

104
105
      $this->_defaults['enableSSL'] = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'enableSSL');
      $this->_defaults['verifySSL'] = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'verifySSL');
totten's avatar
totten committed
106
107
108
109
110
111
    }

    return $this->_defaults;
  }

  /**
Eileen McNaughton's avatar
Eileen McNaughton committed
112
   * Build the form object.
totten's avatar
totten committed
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
   */
  public function buildQuickForm() {
    $session = CRM_Core_Session::singleton();
    $session->pushUserContext(CRM_Utils_System::url('civicrm/admin', 'reset=1'));
    $args = func_get_args();
    $check = reset($args);
    $this->addButtons(array(
        array(
          'type' => 'next',
          'name' => ts('Save'),
          'isDefault' => TRUE,
        ),
        array(
          'type' => 'cancel',
          'name' => ts('Cancel'),
        ),
      )
    );

totten's avatar
totten committed
132
    foreach ($this->_settings as $setting => $group) {
totten's avatar
totten committed
133
      $settingMetaData = civicrm_api('setting', 'getfields', array('version' => 3, 'name' => $setting));
134
      $props = $settingMetaData['values'][$setting];
totten's avatar
totten committed
135
      if (isset($props['quick_form_type'])) {
136
        $add = 'add' . $props['quick_form_type'];
totten's avatar
totten committed
137
        if ($add == 'addElement') {
totten's avatar
totten committed
138
          $this->$add(
139
            $props['html_type'],
totten's avatar
totten committed
140
            $setting,
141
142
143
            ts($props['title']),
            CRM_Utils_Array::value($props['html_type'] == 'select' ? 'option_values' : 'html_attributes', $props, array()),
            $props['html_type'] == 'select' ? CRM_Utils_Array::value('html_attributes', $props) : NULL
totten's avatar
totten committed
144
145
          );
        }
146
147
148
149
150
151
        elseif ($add == 'addSelect') {
          $options = civicrm_api3('Setting', 'getoptions', array(
            'field' => $setting,
          ));
          $this->addElement('select', $setting, ts($props['title']), $options['values'], CRM_Utils_Array::value('html_attributes', $props));
        }
152
        else {
153
          $this->$add($setting, ts($props['title']));
totten's avatar
totten committed
154
        }
155
        $this->assign("{$setting}_description", ts($props['description']));
totten's avatar
totten committed
156
        if ($setting == 'max_attachments') {
totten's avatar
totten committed
157
158
159
          //temp hack @todo fix to get from metadata
          $this->addRule('max_attachments', ts('Value should be a positive number'), 'positiveInteger');
        }
totten's avatar
totten committed
160
        if ($setting == 'maxFileSize') {
totten's avatar
totten committed
161
162
163
164
165
166
167
168
          //temp hack
          $this->addRule('maxFileSize', ts('Value should be a positive number'), 'positiveInteger');
        }

      }
    }
  }

eileenmcnaugton's avatar
eileenmcnaugton committed
169
170
171
172
173
  /**
   * Get default entity.
   *
   * @return string
   */
174
175
176
177
  public function getDefaultEntity() {
    return 'Setting';
  }

totten's avatar
totten committed
178
  /**
Eileen McNaughton's avatar
Eileen McNaughton committed
179
   * Process the form submission.
totten's avatar
totten committed
180
181
182
183
184
185
186
187
   */
  public function postProcess() {
    // store the submitted values in an array
    $params = $this->controller->exportValues($this->_name);

    self::commonProcess($params);
  }

188
  /**
Eileen McNaughton's avatar
Eileen McNaughton committed
189
190
191
192
   * Common Process.
   *
   * @todo Document what I do.
   *
193
   * @param array $params
194
   */
totten's avatar
totten committed
195
196
197
  public function commonProcess(&$params) {

    // save autocomplete search options
198
    if (!empty($params['autocompleteContactSearch'])) {
totten's avatar
totten committed
199
      $value = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR,
totten's avatar
totten committed
200
201
          array_keys($params['autocompleteContactSearch'])
        ) . CRM_Core_DAO::VALUE_SEPARATOR;
totten's avatar
totten committed
202
203
204
205
206
207
208
209
210
211

      CRM_Core_BAO_Setting::setItem($value,
        CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
        'contact_autocomplete_options'
      );

      unset($params['autocompleteContactSearch']);
    }

    // save autocomplete contact reference options
212
    if (!empty($params['autocompleteContactReference'])) {
totten's avatar
totten committed
213
      $value = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR,
totten's avatar
totten committed
214
215
          array_keys($params['autocompleteContactReference'])
        ) . CRM_Core_DAO::VALUE_SEPARATOR;
totten's avatar
totten committed
216
217
218
219
220
221
222
223
224

      CRM_Core_BAO_Setting::setItem($value,
        CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
        'contact_reference_options'
      );

      unset($params['autocompleteContactReference']);
    }

225
    // save components to be enabled
226
    if (array_key_exists('enableComponents', $params)) {
227
228
229
230
      civicrm_api3('setting', 'create', array(
        'enable_components' => $params['enableComponents'],
      ));
      unset($params['enableComponents']);
Deepak Srivastava's avatar
Deepak Srivastava committed
231
232
    }

totten's avatar
totten committed
233
    // save checksum timeout
234
    if (!empty($params['checksumTimeout'])) {
totten's avatar
totten committed
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
      CRM_Core_BAO_Setting::setItem($params['checksumTimeout'],
        CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
        'checksum_timeout'
      );
    }

    // verify ssl peer option
    if (isset($params['verifySSL'])) {
      CRM_Core_BAO_Setting::setItem($params['verifySSL'],
        CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
        'verifySSL'
      );
      unset($params['verifySSL']);
    }

    // force secure URLs
    if (isset($params['enableSSL'])) {
      CRM_Core_BAO_Setting::setItem($params['enableSSL'],
        CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
        'enableSSL'
      );
      unset($params['enableSSL']);
    }
    $settings = array_intersect_key($params, $this->_settings);
    $result = civicrm_api('setting', 'create', $settings + array('version' => 3));
totten's avatar
totten committed
260
    foreach ($settings as $setting => $settingGroup) {
totten's avatar
totten committed
261
262
263
      //@todo array_diff this
      unset($params[$setting]);
    }
264
265
266
267
    if (!empty($result['error_message'])) {
      CRM_Core_Session::setStatus($result['error_message'], ts('Save Failed'), 'error');
    }

totten's avatar
totten committed
268
    CRM_Core_BAO_ConfigSetting::create($params);
269
270
271
272
273

    CRM_Core_Config::clearDBCache();
    CRM_Utils_System::flushCache();
    CRM_Core_Resources::singleton()->resetCacheCode();

274
    CRM_Core_Session::setStatus(" ", ts('Changes Saved'), "success");
totten's avatar
totten committed
275
276
277
278
279
280
281
282
283
284
285
286
287
  }

  public function rebuildMenu() {
    // ensure config is set with new values
    $config = CRM_Core_Config::singleton(TRUE, TRUE);

    // rebuild menu items
    CRM_Core_Menu::store();

    // also delete the IDS file so we can write a new correct one on next load
    $configFile = $config->uploadDir . 'Config.IDS.ini';
    @unlink($configFile);
  }
288

totten's avatar
totten committed
289
}