Settings.php 5.28 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
/*
 +--------------------------------------------------------------------+
 | Copyright CiviCRM LLC. All rights reserved.                        |
 |                                                                    |
 | This work is published under the GNU AGPLv3 license with some      |
 | permitted exceptions and without any warranty. For full license    |
 | and copyright information, see https://civicrm.org/licensing       |
 +--------------------------------------------------------------------+
 */

use CRM_Civigiftaid_ExtensionUtil as E;

/**
 * Form controller class
 *
 * @see http://wiki.civicrm.org/confluence/display/CRMDOC43/QuickForm+Reference
 */
class CRM_Civigiftaid_Form_Settings extends CRM_Core_Form {

  function buildQuickForm() {
    parent::buildQuickForm();

    $className = E::CLASS_PREFIX . '_Settings';
    CRM_Utils_System::setTitle($className::TITLE . ' - ' . E::ts('Settings'));

    $className = E::CLASS_PREFIX . '_Form_SettingsCustom';
    if (method_exists($className, 'buildQuickFormPre')) {
      $className::buildQuickFormPre($this);
    }

    $settings = $this->getFormSettings();

    foreach ($settings as $name => $setting) {
      if (isset($setting['html_type'])) {
        Switch (strtolower($setting['html_type'])) {
          case 'text':
            $this->addElement('text', $name, ts($setting['description']), $setting['html_attributes'], []);
            break;
          case 'checkbox':
            $this->addElement('checkbox', $name, ts($setting['description']), '', '');
            break;
          case 'datepicker':
            foreach ($setting['html_extra'] as $key => $value) {
              if ($key == 'minDate') {
                $minDate = new DateTime('now');
                $minDate->modify($value);
                $setting['html_extra'][$key] = $minDate->format('Y-m-d');
              }
            }
            $this->add('datepicker', $name, ts($setting['description']), $setting['html_attributes'], FALSE, $setting['html_extra']);
            break;
          case 'select2':
            $className = E::CLASS_PREFIX . '_Form_SettingsCustom';
            if (method_exists($className, 'addSelect2Element')) {
              $className::addSelect2Element($this, $name, $setting);
            }
            break;
          case 'select':
            $className = E::CLASS_PREFIX . '_Form_SettingsCustom';
            if (method_exists($className, 'addSelectElement')) {
              $className::addSelectElement($this, $name, $setting);
            }
            break;
          case 'hidden':
            $hidden = TRUE;
        }

        if (isset($hidden)) {
          continue;
        }

        $adminGroup = isset($setting['admin_group']) ? $setting['admin_group'] : 'default';
        $elementGroups[$adminGroup]['elementNames'][] = $name;
        // Title and description may not be defined on all elements (they only need to be on one)
        if (!empty($setting['admin_grouptitle'])) {
          $elementGroups[$setting['admin_group']]['title'] = $setting['admin_grouptitle'];
        }
        if (!empty($setting['admin_groupdescription'])) {
          $elementGroups[$setting['admin_group']]['description'] = $setting['admin_groupdescription'];
        }
      }
    }

    $this->addButtons([
      [
        'type' => 'submit',
        'name' => ts('Submit'),
        'isDefault' => TRUE,
      ],
      [
        'type' => 'cancel',
        'name' => ts('Cancel'),
      ]
    ]);

    // export form elements
    $this->assign('elementGroups', $elementGroups);

  }

  function postProcess() {
    $className = E::CLASS_PREFIX . '_Settings';
    $changed = $this->_submitValues;
    $settings = $this->getFormSettings(TRUE);
    foreach ($settings as &$setting) {
      if ($setting['html_type'] == 'Checkbox') {
        $setting = false;
      }
      else {
        $setting = NULL;
      }
    }
    // Make sure we have all settings elements set (boolean settings will be unset by default and wouldn't be saved)
    $settingsToSave = array_merge($settings, array_intersect_key($changed, $settings));
    $className::save($settingsToSave);
    parent::postProcess();
    CRM_Core_Session::singleton()->setStatus('Configuration Updated', $className::TITLE, 'success');
  }

  /**
   * Get the settings we are going to allow to be set on this form.
   *
   * @return array
   */
  function getFormSettings($metadata=TRUE) {
    $className = E::CLASS_PREFIX . '_Settings';
    $unprefixedSettings = [];
    $settings = civicrm_api3('setting', 'getfields', ['filters' => $className::getFilter()]);
    if (!empty($settings['values'])) {
      foreach ($settings['values'] as $name => $values) {
        if ($metadata) {
          $unprefixedSettings[$className::getName($name, FALSE)] = $values;
        }
        else {
          $unprefixedSettings[$className::getName($name, FALSE)] = NULL;
        }
      }
    }
    return $unprefixedSettings;
  }

  /**
   * Set defaults for form.
   *
   * @see CRM_Core_Form::setDefaultValues()
   */
  function setDefaultValues() {
    $className = E::CLASS_PREFIX . '_Settings';
    $settings = $this->getFormSettings(FALSE);
    $defaults = [];

    $existing = $className::get(array_keys($settings));
    if ($existing) {
      foreach ($existing as $name => $value) {
        $defaults[$name] = $value;
      }
    }
    return $defaults;
  }

}