phpcs - Fix error, "CONST keyword must be lowercase; expected const but found CONST"
[civicrm-core.git] / CRM / Admin / Form / Tag.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * This class generates form components for Tag
38 *
39 */
40 class CRM_Admin_Form_Tag extends CRM_Admin_Form {
41 protected $_isTagSet;
42
43 /**
44 * Build the form object
45 *
46 * @return void
47 * @access public
48 */
49 public function buildQuickForm() {
50 $this->setPageTitle($this->_isTagSet ? ts('Tag Set') : ts('Tag'));
51
52 if ($this->_action == CRM_Core_Action::DELETE) {
53 if ($this->_id && $tag = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Tag', $this->_id, 'name', 'parent_id')) {
54 $url = CRM_Utils_System::url('civicrm/admin/tag', "reset=1");
55 CRM_Core_Error::statusBounce(ts("This tag cannot be deleted. You must delete all its child tags ('%1', etc) prior to deleting this tag.", array(1 => $tag)), $url);
56 }
57 if ($this->_values['is_reserved'] == 1 && !CRM_Core_Permission::check('administer reserved tags')) {
58 CRM_Core_Error::statusBounce(ts("You do not have sufficient permission to delete this reserved tag."));
59 }
60 }
61 else {
62 $this->_isTagSet = CRM_Utils_Request::retrieve('tagset', 'Positive', $this);
63
64 if (!$this->_isTagSet &&
65 $this->_id &&
66 CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Tag', $this->_id, 'is_tagset')
67 ) {
68 $this->_isTagSet = TRUE;
69 }
70
71 $allTag = array('' => ts('- select -')) + CRM_Core_BAO_Tag::getTagsNotInTagset();
72
73 if ($this->_id) {
74 unset($allTag[$this->_id]);
75 }
76
77 if (!$this->_isTagSet) {
78 $this->add('select', 'parent_id', ts('Parent Tag'), $allTag, FALSE, array('class' => 'crm-select2'));
79
80 // Tagsets are not selectable by definition so only include the selectable field if NOT a tagset.
81 $selectable = $this->add('checkbox', 'is_selectable', ts('Selectable?'));
82 // Selectable should be checked by default when creating a new tag
83 if ($this->_action == CRM_Core_Action::ADD) {
84 $selectable->setValue(1);
85 }
86
87 }
88
89 $this->assign('isTagSet', $this->_isTagSet);
90
91 $this->applyFilter('__ALL__', 'trim');
92
93 $this->add('text', 'name', ts('Name'),
94 CRM_Core_DAO::getAttribute('CRM_Core_DAO_Tag', 'name'), TRUE
95 );
96 $this->addRule('name', ts('Name already exists in Database.'), 'objectExists', array('CRM_Core_DAO_Tag', $this->_id));
97
98 $this->add('text', 'description', ts('Description'),
99 CRM_Core_DAO::getAttribute('CRM_Core_DAO_Tag', 'description')
100 );
101
102 $isReserved = $this->add('checkbox', 'is_reserved', ts('Reserved?'));
103
104 $usedFor = $this->addSelect('used_for', array('multiple' => TRUE, 'option_url' => NULL));
105
106 if ($this->_id &&
107 CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Tag', $this->_id, 'parent_id')
108 ) {
109 $usedFor->freeze();
110 }
111
112 $adminTagset = TRUE;
113 if (!CRM_Core_Permission::check('administer Tagsets')) {
114 $adminTagset = FALSE;
115 }
116 $this->assign('adminTagset', $adminTagset);
117
118 $adminReservedTags = TRUE;
119 if (!CRM_Core_Permission::check('administer reserved tags')) {
120 $isReserved->freeze();
121 $adminReservedTags = FALSE;
122 }
123 $this->assign('adminReservedTags', $adminReservedTags);
124
125 }
126 parent::buildQuickForm();
127 }
128
129 /**
130 * Process the form submission
131 *
132 * @access public
133 *
134 * @return void
135 */
136 public function postProcess() {
137 $params = $ids = array();
138
139 // store the submitted values in an array
140 $params = $this->exportValues();
141
142 $ids['tag'] = $this->_id;
143 if ($this->_action == CRM_Core_Action::ADD ||
144 $this->_action == CRM_Core_Action::UPDATE
145 ) {
146 $params['used_for'] = implode(",", $params['used_for']);
147 }
148
149 $params['is_tagset'] = 0;
150 if ($this->_isTagSet) {
151 $params['is_tagset'] = 1;
152 }
153
154 if (!isset($params['is_reserved'])) {
155 $params['is_reserved'] = 0;
156 }
157
158 if (!isset($params['is_selectable'])) {
159 $params['is_selectable'] = 0;
160 }
161
162 if ($this->_action == CRM_Core_Action::DELETE) {
163 if ($this->_id > 0) {
164 $tag = civicrm_api3('tag', 'getsingle', array('id' => $this->_id));
165 CRM_Core_BAO_Tag::del($this->_id);
166 CRM_Core_Session::setStatus(ts('The tag \'%1\' has been deleted.', array(1 => $tag['name'])), ts('Deleted'), 'success');
167 }
168 }
169 else {
170 $tag = CRM_Core_BAO_Tag::add($params, $ids);
171 CRM_Core_Session::setStatus(ts('The tag \'%1\' has been saved.', array(1 => $tag->name)), ts('Saved'), 'success');
172 }
173 }
174
175 }
176