3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class generates form components for Option Group.
21 class CRM_Admin_Form_OptionGroup
extends CRM_Admin_Form
{
26 public $submitOnce = TRUE;
29 * Explicitly declare the entity api name.
31 public function getDefaultEntity() {
36 * Build the form object.
38 public function buildQuickForm() {
39 parent
::buildQuickForm();
40 if ($this->_action
& CRM_Core_Action
::DELETE
) {
43 $this->setTitle(ts('Dropdown Options'));
45 $this->applyFilter('__ALL__', 'trim');
50 CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionGroup', 'title')
56 CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionGroup', 'description')
59 $this->addSelect('data_type', ['options' => CRM_Utils_Type
::dataTypes()], empty($this->_values
['is_reserved']));
61 $element = $this->add('checkbox', 'is_active', ts('Enabled?'));
62 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
63 if (in_array($this->_values
['name'], [
68 static $caseCount = NULL;
69 if (!isset($caseCount)) {
70 $caseCount = CRM_Case_BAO_Case
::caseCount(NULL, FALSE);
78 $this->add('checkbox', 'is_reserved', ts('Reserved?'));
79 $this->freeze('is_reserved');
81 if (!empty($this->_values
['is_reserved'])) {
82 $this->freeze(['is_active', 'data_type']);
86 $this->assign('id', $this->_id
);
87 $this->addFormRule(['CRM_Admin_Form_OptionGroup', 'formRule'], $this);
93 * @param array $fields
94 * The input form values.
100 * true if no errors, else array of errors
102 public static function formRule($fields, $files, $self) {
105 $name = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', $self->_id
, 'name');
108 $name = CRM_Utils_String
::titleToVar(strtolower($fields['title']));
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');
113 return empty($errors) ?
TRUE : $errors;
117 * Process the form submission.
119 public function postProcess() {
120 CRM_Utils_System
::flushCache();
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');
127 // store the submitted values in an array
128 $params = $this->exportValues();
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;
136 elseif ($this->_action
& CRM_Core_Action
::UPDATE
) {
137 $params['id'] = $this->_id
;
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');