Merge pull request #2308 from civicrm/4.4
[civicrm-core.git] / CRM / Admin / Form / OptionGroup.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2013
32 * $Id$
33 *
34 */
35
36 /**
37 * This class generates form components for Option Group
38 *
39 */
40 class CRM_Admin_Form_OptionGroup extends CRM_Admin_Form {
41
42 /**
43 * Function to build the form
44 *
45 * @return void
46 * @access public
47 */
48 public function buildQuickForm() {
49 parent::buildQuickForm();
50 if ($this->_action & CRM_Core_Action::DELETE) {
51 return;
52 }
53 CRM_Utils_System::setTitle(ts('Dropdown Options'));
54
55 $this->applyFilter('__ALL__', 'trim');
56 $this->add('text',
57 'name',
58 ts('Name'),
59 CRM_Core_DAO::getAttribute('CRM_Core_DAO_OptionGroup', 'name'),
60 TRUE
61 );
62 $this->addRule('name',
63 ts('Name already exists in Database.'),
64 'objectExists',
65 array('CRM_Core_DAO_OptionGroup', $this->_id)
66 );
67
68 $this->add('text',
69 'title',
70 ts('Group Title'),
71 CRM_Core_DAO::getAttribute('CRM_Core_DAO_OptionGroup', 'title')
72 );
73
74 $this->add('text',
75 'description',
76 ts('Description'),
77 CRM_Core_DAO::getAttribute('CRM_Core_DAO_OptionGroup', 'description')
78 );
79
80 $element = $this->add('checkbox', 'is_active', ts('Enabled?'));
81 if ($this->_action & CRM_Core_Action::UPDATE) {
82 if (in_array($this->_values['name'], array(
83 'encounter_medium', 'case_type', 'case_status'))) {
84 static $caseCount = NULL;
85 if (!isset($caseCount)) {
86 $caseCount = CRM_Case_BAO_Case::caseCount(NULL, FALSE);
87 }
88
89 if ($caseCount > 0) {
90 $element->freeze();
91 }
92 }
93 if (CRM_Utils_Array::value('is_reserved', $this->_values)) {
94 $this->freeze(array('name', 'is_active'));
95 }
96 }
97
98 $this->assign('id', $this->_id);
99 }
100
101 /**
102 * Function to process the form
103 *
104 * @access public
105 *
106 * @return void
107 */
108 public function postProcess() {
109 CRM_Utils_System::flushCache();
110
111 $params = $this->exportValues();
112 if ($this->_action & CRM_Core_Action::DELETE) {
113 CRM_Core_BAO_OptionGroup::del($this->_id);
114 CRM_Core_Session::setStatus(ts('Selected option group has been deleted.'), ts('Record Deleted'), 'success');
115 }
116 else {
117
118 $params = $ids = array();
119 // store the submitted values in an array
120 $params = $this->exportValues();
121
122 if ($this->_action & CRM_Core_Action::UPDATE) {
123 $ids['optionGroup'] = $this->_id;
124 }
125
126 $optionGroup = CRM_Core_BAO_OptionGroup::add($params, $ids);
127 CRM_Core_Session::setStatus(ts('The Option Group \'%1\' has been saved.', array(1 => $optionGroup->name)), ts('Saved'), 'success');
128 }
129 }
130 }