d9816404258686140201a02b91e2f32567b06c56
[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 * @var bool
25 */
26 public $submitOnce = TRUE;
27
28 /**
29 * Explicitly declare the entity api name.
30 */
31 public function getDefaultEntity() {
32 return 'OptionGroup';
33 }
34
35 /**
36 * Build the form object.
37 */
38 public function buildQuickForm() {
39 parent::buildQuickForm();
40 if ($this->_action & CRM_Core_Action::DELETE) {
41 return;
42 }
43 CRM_Utils_System::setTitle(ts('Dropdown Options'));
44
45 $this->applyFilter('__ALL__', 'trim');
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
59 $this->addSelect('data_type', ['options' => CRM_Utils_Type::dataTypes()], empty($this->_values['is_reserved']));
60
61 $element = $this->add('checkbox', 'is_active', ts('Enabled?'));
62 if ($this->_action & CRM_Core_Action::UPDATE) {
63 if (in_array($this->_values['name'], [
64 'encounter_medium',
65 'case_type',
66 'case_status',
67 ])) {
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 }
77
78 $this->add('checkbox', 'is_reserved', ts('Reserved?'));
79 $this->freeze('is_reserved');
80
81 if (!empty($this->_values['is_reserved'])) {
82 $this->freeze(['is_active', 'data_type']);
83 }
84 }
85
86 $this->assign('id', $this->_id);
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
97 * @param $self
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;
114 }
115
116 /**
117 * Process the form submission.
118 */
119 public function postProcess() {
120 CRM_Utils_System::flushCache();
121
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 {
127 // store the submitted values in an array
128 $params = $this->exportValues();
129
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 }
135 }
136 elseif ($this->_action & CRM_Core_Action::UPDATE) {
137 $params['id'] = $this->_id;
138 }
139
140 $optionGroup = CRM_Core_BAO_OptionGroup::add($params);
141 CRM_Core_Session::setStatus(ts('The Option Group \'%1\' has been saved.', [1 => $optionGroup->title]), ts('Saved'), 'success');
142 }
143 }
144
145 }