Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
18 | /** | |
ce064e4f | 19 | * This class generates form components for Option Group. |
6a488035 TO |
20 | */ |
21 | class CRM_Admin_Form_OptionGroup extends CRM_Admin_Form { | |
22 | ||
88aae6d4 A |
23 | /** |
24 | * @var bool | |
25 | */ | |
26 | public $submitOnce = TRUE; | |
27 | ||
c62c37c7 SL |
28 | /** |
29 | * Explicitly declare the entity api name. | |
30 | */ | |
31 | public function getDefaultEntity() { | |
32 | return 'OptionGroup'; | |
33 | } | |
34 | ||
6a488035 | 35 | /** |
eceb18cc | 36 | * Build the form object. |
6a488035 TO |
37 | */ |
38 | public function buildQuickForm() { | |
39 | parent::buildQuickForm(); | |
40 | if ($this->_action & CRM_Core_Action::DELETE) { | |
41 | return; | |
42 | } | |
483cfbc4 | 43 | $this->setTitle(ts('Dropdown Options')); |
6a488035 TO |
44 | |
45 | $this->applyFilter('__ALL__', 'trim'); | |
6a488035 TO |
46 | |
47 | $this->add('text', | |
48 | 'title', | |
49 | ts('Group Title'), | |
50 | CRM_Core_DAO::getAttribute('CRM_Core_DAO_OptionGroup', 'title') | |
51 | ); | |
52 | ||
53 | $this->add('text', | |
54 | 'description', | |
55 | ts('Description'), | |
56 | CRM_Core_DAO::getAttribute('CRM_Core_DAO_OptionGroup', 'description') | |
57 | ); | |
58 | ||
be2fb01f | 59 | $this->addSelect('data_type', ['options' => CRM_Utils_Type::dataTypes()], empty($this->_values['is_reserved'])); |
eaecfa20 | 60 | |
6a488035 TO |
61 | $element = $this->add('checkbox', 'is_active', ts('Enabled?')); |
62 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
be2fb01f | 63 | if (in_array($this->_values['name'], [ |
353ffa53 TO |
64 | 'encounter_medium', |
65 | 'case_type', | |
af9b09df | 66 | 'case_status', |
be2fb01f | 67 | ])) { |
6a488035 TO |
68 | static $caseCount = NULL; |
69 | if (!isset($caseCount)) { | |
70 | $caseCount = CRM_Case_BAO_Case::caseCount(NULL, FALSE); | |
71 | } | |
72 | ||
73 | if ($caseCount > 0) { | |
74 | $element->freeze(); | |
75 | } | |
76 | } | |
62fbdfc0 MW |
77 | |
78 | $this->add('checkbox', 'is_reserved', ts('Reserved?')); | |
79 | $this->freeze('is_reserved'); | |
80 | ||
a7488080 | 81 | if (!empty($this->_values['is_reserved'])) { |
f892ea14 | 82 | $this->freeze(['is_active', 'data_type']); |
6a488035 TO |
83 | } |
84 | } | |
85 | ||
86 | $this->assign('id', $this->_id); | |
6ebc7a89 SL |
87 | $this->addFormRule(['CRM_Admin_Form_OptionGroup', 'formRule'], $this); |
88 | } | |
89 | ||
90 | /** | |
91 | * Global form rule. | |
92 | * | |
93 | * @param array $fields | |
94 | * The input form values. | |
95 | * | |
96 | * @param $files | |
e8cf95b4 | 97 | * @param self $self |
6ebc7a89 SL |
98 | * |
99 | * @return bool|array | |
100 | * true if no errors, else array of errors | |
101 | */ | |
102 | public static function formRule($fields, $files, $self) { | |
103 | $errors = []; | |
104 | if ($self->_id) { | |
105 | $name = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', $self->_id, 'name'); | |
106 | } | |
107 | else { | |
108 | $name = CRM_Utils_String::titleToVar(strtolower($fields['title'])); | |
109 | } | |
110 | if (!CRM_Core_DAO::objectExists($name, 'CRM_Core_DAO_OptionGroup', $self->_id)) { | |
111 | $errors['title'] = ts('Option Group name ' . $name . ' already exists in the database. Option Group Names need to be unique'); | |
112 | } | |
113 | return empty($errors) ? TRUE : $errors; | |
6a488035 TO |
114 | } |
115 | ||
116 | /** | |
eceb18cc | 117 | * Process the form submission. |
6a488035 TO |
118 | */ |
119 | public function postProcess() { | |
120 | CRM_Utils_System::flushCache(); | |
121 | ||
6a488035 TO |
122 | if ($this->_action & CRM_Core_Action::DELETE) { |
123 | CRM_Core_BAO_OptionGroup::del($this->_id); | |
124 | CRM_Core_Session::setStatus(ts('Selected option group has been deleted.'), ts('Record Deleted'), 'success'); | |
125 | } | |
126 | else { | |
6a488035 TO |
127 | // store the submitted values in an array |
128 | $params = $this->exportValues(); | |
129 | ||
62fbdfc0 MW |
130 | if ($this->_action & CRM_Core_Action::ADD) { |
131 | // If we are adding option group via UI it should not be marked reserved. | |
132 | if (!isset($params['is_reserved'])) { | |
133 | $params['is_reserved'] = 0; | |
134 | } | |
3593e0f8 | 135 | } |
62fbdfc0 | 136 | elseif ($this->_action & CRM_Core_Action::UPDATE) { |
3593e0f8 | 137 | $params['id'] = $this->_id; |
6a488035 TO |
138 | } |
139 | ||
3593e0f8 | 140 | $optionGroup = CRM_Core_BAO_OptionGroup::add($params); |
fbd40c08 | 141 | CRM_Core_Session::setStatus(ts('The Option Group \'%1\' has been saved.', [1 => $optionGroup->title]), ts('Saved'), 'success'); |
6a488035 TO |
142 | } |
143 | } | |
96025800 | 144 | |
6a488035 | 145 | } |