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