Update Copywrite year to be 2019
[civicrm-core.git] / CRM / Admin / Form / OptionGroup.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
6b83d5bd 31 * @copyright CiviCRM LLC (c) 2004-2019
6a488035
TO
32 */
33
34/**
ce064e4f 35 * This class generates form components for Option Group.
6a488035
TO
36 */
37class CRM_Admin_Form_OptionGroup extends CRM_Admin_Form {
38
c62c37c7
SL
39 /**
40 * Explicitly declare the entity api name.
41 */
42 public function getDefaultEntity() {
43 return 'OptionGroup';
44 }
45
6a488035 46 /**
eceb18cc 47 * Build the form object.
6a488035
TO
48 */
49 public function buildQuickForm() {
50 parent::buildQuickForm();
51 if ($this->_action & CRM_Core_Action::DELETE) {
52 return;
53 }
54 CRM_Utils_System::setTitle(ts('Dropdown Options'));
55
56 $this->applyFilter('__ALL__', 'trim');
57 $this->add('text',
58 'name',
59 ts('Name'),
60 CRM_Core_DAO::getAttribute('CRM_Core_DAO_OptionGroup', 'name'),
61 TRUE
62 );
63 $this->addRule('name',
64 ts('Name already exists in Database.'),
65 'objectExists',
66 array('CRM_Core_DAO_OptionGroup', $this->_id)
67 );
68
69 $this->add('text',
70 'title',
71 ts('Group Title'),
72 CRM_Core_DAO::getAttribute('CRM_Core_DAO_OptionGroup', 'title')
73 );
74
75 $this->add('text',
76 'description',
77 ts('Description'),
78 CRM_Core_DAO::getAttribute('CRM_Core_DAO_OptionGroup', 'description')
79 );
80
62fbdfc0 81 $this->addSelect('data_type', array('options' => CRM_Utils_Type::dataTypes()), empty($this->_values['is_reserved']));
eaecfa20 82
6a488035
TO
83 $element = $this->add('checkbox', 'is_active', ts('Enabled?'));
84 if ($this->_action & CRM_Core_Action::UPDATE) {
85 if (in_array($this->_values['name'], array(
353ffa53
TO
86 'encounter_medium',
87 'case_type',
af9b09df 88 'case_status',
353ffa53 89 ))) {
6a488035
TO
90 static $caseCount = NULL;
91 if (!isset($caseCount)) {
92 $caseCount = CRM_Case_BAO_Case::caseCount(NULL, FALSE);
93 }
94
95 if ($caseCount > 0) {
96 $element->freeze();
97 }
98 }
62fbdfc0
MW
99
100 $this->add('checkbox', 'is_reserved', ts('Reserved?'));
101 $this->freeze('is_reserved');
102
a7488080 103 if (!empty($this->_values['is_reserved'])) {
62fbdfc0 104 $this->freeze(array('name', 'is_active', 'data_type'));
6a488035
TO
105 }
106 }
107
108 $this->assign('id', $this->_id);
109 }
110
111 /**
eceb18cc 112 * Process the form submission.
6a488035
TO
113 */
114 public function postProcess() {
115 CRM_Utils_System::flushCache();
116
6a488035
TO
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 {
6a488035
TO
122 // store the submitted values in an array
123 $params = $this->exportValues();
124
62fbdfc0
MW
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 }
3593e0f8 130 }
62fbdfc0 131 elseif ($this->_action & CRM_Core_Action::UPDATE) {
3593e0f8 132 $params['id'] = $this->_id;
6a488035
TO
133 }
134
3593e0f8 135 $optionGroup = CRM_Core_BAO_OptionGroup::add($params);
6a488035
TO
136 CRM_Core_Session::setStatus(ts('The Option Group \'%1\' has been saved.', array(1 => $optionGroup->name)), ts('Saved'), 'success');
137 }
138 }
96025800 139
6a488035 140}