CRM-17507 - Cleanup settings setItem calls
[civicrm-core.git] / CRM / Admin / Form / Tag.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
32 */
33
34 /**
35 * This class generates form components for Tag.
36 */
37 class CRM_Admin_Form_Tag extends CRM_Admin_Form {
38 protected $_isTagSet;
39
40 /**
41 * Explicitly declare the entity api name.
42 */
43 public function getDefaultEntity() {
44 return 'Tag';
45 }
46
47 /**
48 * Build the form object.
49 */
50 public function buildQuickForm() {
51 $this->setPageTitle($this->_isTagSet ? ts('Tag Set') : ts('Tag'));
52
53 if ($this->_action == CRM_Core_Action::DELETE) {
54 if ($this->_id && $tag = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Tag', $this->_id, 'name', 'parent_id')) {
55 $url = CRM_Utils_System::url('civicrm/admin/tag', "reset=1");
56 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);
57 }
58 if ($this->_values['is_reserved'] == 1 && !CRM_Core_Permission::check('administer reserved tags')) {
59 CRM_Core_Error::statusBounce(ts("You do not have sufficient permission to delete this reserved tag."));
60 }
61 }
62 else {
63 $this->_isTagSet = CRM_Utils_Request::retrieve('tagset', 'Positive', $this);
64
65 if (!$this->_isTagSet &&
66 $this->_id &&
67 CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Tag', $this->_id, 'is_tagset')
68 ) {
69 $this->_isTagSet = TRUE;
70 }
71
72 $allTag = array('' => ts('- select -')) + CRM_Core_BAO_Tag::getTagsNotInTagset();
73
74 if ($this->_id) {
75 unset($allTag[$this->_id]);
76 }
77
78 if (!$this->_isTagSet) {
79 $this->add('select', 'parent_id', ts('Parent Tag'), $allTag, FALSE, array('class' => 'crm-select2'));
80
81 // Tagsets are not selectable by definition so only include the selectable field if NOT a tagset.
82 $selectable = $this->add('checkbox', 'is_selectable', ts('Selectable?'));
83 // Selectable should be checked by default when creating a new tag
84 if ($this->_action == CRM_Core_Action::ADD) {
85 $selectable->setValue(1);
86 }
87
88 }
89
90 $this->assign('isTagSet', $this->_isTagSet);
91
92 $this->applyFilter('__ALL__', 'trim');
93
94 $this->add('text', 'name', ts('Name'),
95 CRM_Core_DAO::getAttribute('CRM_Core_DAO_Tag', 'name'), TRUE
96 );
97 $this->addRule('name', ts('Name already exists in Database.'), 'objectExists', array(
98 'CRM_Core_DAO_Tag',
99 $this->_id,
100 ));
101
102 $this->add('text', 'description', ts('Description'),
103 CRM_Core_DAO::getAttribute('CRM_Core_DAO_Tag', 'description')
104 );
105
106 $isReserved = $this->add('checkbox', 'is_reserved', ts('Reserved?'));
107
108 $usedFor = $this->addSelect('used_for', array('multiple' => TRUE, 'option_url' => NULL));
109
110 if ($this->_id &&
111 CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Tag', $this->_id, 'parent_id')
112 ) {
113 $usedFor->freeze();
114 }
115
116 $adminTagset = TRUE;
117 if (!CRM_Core_Permission::check('administer Tagsets')) {
118 $adminTagset = FALSE;
119 }
120 $this->assign('adminTagset', $adminTagset);
121
122 $adminReservedTags = TRUE;
123 if (!CRM_Core_Permission::check('administer reserved tags')) {
124 $isReserved->freeze();
125 $adminReservedTags = FALSE;
126 }
127 $this->assign('adminReservedTags', $adminReservedTags);
128
129 }
130 parent::buildQuickForm();
131 }
132
133 /**
134 * Process the form submission.
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 }