Update Copywrite year to be 2019
[civicrm-core.git] / CRM / Admin / Form / Setting.php
index 695f6f097df4bcf63c494d270d9bafe460934f46..e03c04db507cb88b95ab9fb9a0e87ad988b2d21a 100644 (file)
@@ -3,7 +3,7 @@
  +--------------------------------------------------------------------+
  | CiviCRM version 5                                                  |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2018                                |
+ | Copyright CiviCRM LLC (c) 2004-2019                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -28,7 +28,7 @@
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2018
+ * @copyright CiviCRM LLC (c) 2004-2019
  */
 
 /**
  */
 class CRM_Admin_Form_Setting extends CRM_Core_Form {
 
+  use CRM_Admin_Form_SettingTrait;
+
   protected $_settings = array();
 
+  protected $includesReadOnlyFields;
+
   /**
    * Set default values for the form.
    *
@@ -52,20 +56,9 @@ class CRM_Admin_Form_Setting extends CRM_Core_Form {
         $formMode = TRUE;
       }
 
-      CRM_Core_BAO_ConfigSetting::retrieve($this->_defaults);
+      $this->setDefaultsForMetadataDefinedFields();
 
-      // we can handle all the ones defined in the metadata here. Others to be converted
-      foreach ($this->_settings as $setting => $group) {
-        $this->_defaults[$setting] = civicrm_api('setting', 'getvalue', array(
-            'version' => 3,
-            'name' => $setting,
-            'group' => $group,
-          )
-        );
-      }
-
-      $this->_defaults['contact_autocomplete_options'] = self::getAutocompleteContactSearch();
-      $this->_defaults['contact_reference_options'] = self::getAutocompleteContactReference();
+      // @todo these should be retrievable from the above function.
       $this->_defaults['enableSSL'] = Civi::settings()->get('enableSSL');
       $this->_defaults['verifySSL'] = Civi::settings()->get('verifySSL');
       $this->_defaults['environment'] = CRM_Core_Config::environment();
@@ -93,80 +86,11 @@ class CRM_Admin_Form_Setting extends CRM_Core_Form {
       )
     );
 
-    $descriptions = array();
-    $settingMetaData = $this->getSettingsMetaData();
-    foreach ($settingMetaData as $setting => $props) {
-      if (isset($props['quick_form_type'])) {
-        if (isset($props['pseudoconstant'])) {
-          $options = civicrm_api3('Setting', 'getoptions', array(
-            'field' => $setting,
-          ));
-        }
-        else {
-          $options = NULL;
-        }
-        //Load input as readonly whose values are overridden in civicrm.settings.php.
-        if (Civi::settings()->getMandatory($setting)) {
-          $props['html_attributes']['readonly'] = TRUE;
-          $setStatus = TRUE;
-        }
-
-        $add = 'add' . $props['quick_form_type'];
-        if ($add == 'addElement') {
-          $this->$add(
-            $props['html_type'],
-            $setting,
-            ts($props['title']),
-            ($options !== NULL) ? $options['values'] : CRM_Utils_Array::value('html_attributes', $props, array()),
-            ($options !== NULL) ? CRM_Utils_Array::value('html_attributes', $props, array()) : NULL
-          );
-        }
-        elseif ($add == 'addSelect') {
-          $this->addElement('select', $setting, ts($props['title']), $options['values'], CRM_Utils_Array::value('html_attributes', $props));
-        }
-        elseif ($add == 'addCheckBox') {
-          $this->addCheckBox($setting, ts($props['title']), $options['values'], NULL, CRM_Utils_Array::value('html_attributes', $props), NULL, NULL, array('  '));
-        }
-        elseif ($add == 'addChainSelect') {
-          $this->addChainSelect($setting, array(
-            'label' => ts($props['title']),
-          ));
-        }
-        elseif ($add == 'addMonthDay') {
-          $this->add('date', $setting, ts($props['title']), CRM_Core_SelectValues::date(NULL, 'M d'));
-        }
-        else {
-          $this->$add($setting, ts($props['title']));
-        }
-        // Migrate to using an array as easier in smart...
-        $descriptions[$setting] = ts($props['description']);
-        $this->assign("{$setting}_description", ts($props['description']));
-        if ($setting == 'max_attachments') {
-          //temp hack @todo fix to get from metadata
-          $this->addRule('max_attachments', ts('Value should be a positive number'), 'positiveInteger');
-        }
-        if ($setting == 'maxFileSize') {
-          //temp hack
-          $this->addRule('maxFileSize', ts('Value should be a positive number'), 'positiveInteger');
-        }
+    $this->addFieldsDefinedInSettingsMetadata();
 
-      }
-    }
-    if (!empty($setStatus)) {
+    if ($this->includesReadOnlyFields) {
       CRM_Core_Session::setStatus(ts("Some fields are loaded as 'readonly' as they have been set (overridden) in civicrm.settings.php."), '', 'info', array('expires' => 0));
     }
-    // setting_description should be deprecated - see Mail.tpl for metadata based tpl.
-    $this->assign('setting_descriptions', $descriptions);
-    $this->assign('settings_fields', $settingMetaData);
-  }
-
-  /**
-   * Get default entity.
-   *
-   * @return string
-   */
-  public function getDefaultEntity() {
-    return 'Setting';
   }
 
   /**
@@ -185,23 +109,10 @@ class CRM_Admin_Form_Setting extends CRM_Core_Form {
    * @todo Document what I do.
    *
    * @param array $params
+   * @throws \CRM_Core_Exception
    */
   public function commonProcess(&$params) {
 
-    // save autocomplete search options
-    if (!empty($params['contact_autocomplete_options'])) {
-      Civi::settings()->set('contact_autocomplete_options',
-        CRM_Utils_Array::implodePadded(array_keys($params['contact_autocomplete_options'])));
-      unset($params['contact_autocomplete_options']);
-    }
-
-    // save autocomplete contact reference options
-    if (!empty($params['contact_reference_options'])) {
-      Civi::settings()->set('contact_reference_options',
-        CRM_Utils_Array::implodePadded(array_keys($params['contact_reference_options'])));
-      unset($params['contact_reference_options']);
-    }
-
     // save components to be enabled
     if (array_key_exists('enableComponents', $params)) {
       civicrm_api3('setting', 'create', array(
@@ -216,20 +127,19 @@ class CRM_Admin_Form_Setting extends CRM_Core_Form {
         unset($params[$name]);
       }
     }
-    $settings = array_intersect_key($params, $this->_settings);
-    $result = civicrm_api('setting', 'create', $settings + array('version' => 3));
-    foreach ($settings as $setting => $settingGroup) {
-      //@todo array_diff this
-      unset($params[$setting]);
+    try {
+      $settings = $this->getSettingsToSetByMetadata($params);
+      $this->saveMetadataDefinedSettings($params);
     }
-    if (!empty($result['error_message'])) {
-      CRM_Core_Session::setStatus($result['error_message'], ts('Save Failed'), 'error');
+    catch (CiviCRM_API3_Exception $e) {
+      CRM_Core_Session::setStatus($e->getMessage(), ts('Save Failed'), 'error');
     }
 
-    //CRM_Core_BAO_ConfigSetting::create($params);
+    $this->filterParamsSetByMetadata($params);
+
     $params = CRM_Core_BAO_ConfigSetting::filterSkipVars($params);
     if (!empty($params)) {
-      CRM_Core_Error::fatal('Unrecognized setting. This may be a config field which has not been properly migrated to a setting. (' . implode(', ', array_keys($params)) . ')');
+      throw new CRM_Core_Exception('Unrecognized setting. This may be a config field which has not been properly migrated to a setting. (' . implode(', ', array_keys($params)) . ')');
     }
 
     CRM_Core_Config::clearDBCache();
@@ -252,64 +162,4 @@ class CRM_Admin_Form_Setting extends CRM_Core_Form {
     @unlink($configFile);
   }
 
-  /**
-   * Ugh, this shouldn't exist.
-   *
-   * Get the selected values of "contact_reference_options" formatted for checkboxes.
-   *
-   * @return array
-   */
-  public static function getAutocompleteContactReference() {
-    $cRlist = array_flip(CRM_Core_OptionGroup::values('contact_reference_options',
-      FALSE, FALSE, TRUE, NULL, 'name'
-    ));
-    $cRlistEnabled = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
-      'contact_reference_options'
-    );
-    $cRSearchFields = array();
-    if (!empty($cRlist) && !empty($cRlistEnabled)) {
-      $cRSearchFields = array_combine($cRlist, $cRlistEnabled);
-    }
-    return array(
-      '1' => 1,
-    ) + $cRSearchFields;
-  }
-
-  /**
-   * Ugh, this shouldn't exist.
-   *
-   * Get the selected values of "contact_autocomplete_options" formatted for checkboxes.
-   *
-   * @return array
-   */
-  public static function getAutocompleteContactSearch() {
-    $list = array_flip(CRM_Core_OptionGroup::values('contact_autocomplete_options',
-      FALSE, FALSE, TRUE, NULL, 'name'
-    ));
-    $listEnabled = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
-      'contact_autocomplete_options'
-    );
-    $autoSearchFields = array();
-    if (!empty($list) && !empty($listEnabled)) {
-      $autoSearchFields = array_combine($list, $listEnabled);
-    }
-    //Set defaults for autocomplete and contact reference options
-    return array(
-      '1' => 1,
-    ) + $autoSearchFields;
-  }
-
-  /**
-   * Get the metadata relating to the settings on the form, ordered by the keys in $this->_settings.
-   *
-   * @return array
-   */
-  protected function getSettingsMetaData() {
-    $allSettingMetaData = civicrm_api3('setting', 'getfields', array());
-    $settingMetaData = array_intersect_key($allSettingMetaData['values'], $this->_settings);
-    // This array_merge re-orders to the key order of $this->_settings.
-    $settingMetaData = array_merge($this->_settings, $settingMetaData);
-    return $settingMetaData;
-  }
-
 }