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
{
24 * Explicitly declare the entity api name.
26 public function getDefaultEntity() {
31 * Build the form object.
33 public function buildQuickForm() {
34 parent
::buildQuickForm();
35 if ($this->_action
& CRM_Core_Action
::DELETE
) {
38 CRM_Utils_System
::setTitle(ts('Dropdown Options'));
40 $this->applyFilter('__ALL__', 'trim');
45 CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionGroup', 'title')
51 CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionGroup', 'description')
54 $this->addSelect('data_type', ['options' => CRM_Utils_Type
::dataTypes()], empty($this->_values
['is_reserved']));
56 $element = $this->add('checkbox', 'is_active', ts('Enabled?'));
57 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
58 if (in_array($this->_values
['name'], [
63 static $caseCount = NULL;
64 if (!isset($caseCount)) {
65 $caseCount = CRM_Case_BAO_Case
::caseCount(NULL, FALSE);
73 $this->add('checkbox', 'is_reserved', ts('Reserved?'));
74 $this->freeze('is_reserved');
76 if (!empty($this->_values
['is_reserved'])) {
77 $this->freeze(['is_active', 'data_type']);
81 $this->assign('id', $this->_id
);
82 $this->addFormRule(['CRM_Admin_Form_OptionGroup', 'formRule'], $this);
88 * @param array $fields
89 * The input form values.
95 * true if no errors, else array of errors
97 public static function formRule($fields, $files, $self) {
100 $name = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', $self->_id
, 'name');
103 $name = CRM_Utils_String
::titleToVar(strtolower($fields['title']));
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');
108 return empty($errors) ?
TRUE : $errors;
112 * Process the form submission.
114 public function postProcess() {
115 CRM_Utils_System
::flushCache();
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');
122 // store the submitted values in an array
123 $params = $this->exportValues();
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;
131 elseif ($this->_action
& CRM_Core_Action
::UPDATE
) {
132 $params['id'] = $this->_id
;
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');