Merge pull request #13259 from agh1/disabled-expired-mem
[civicrm-core.git] / CRM / Tag / Form / Edit.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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-2019
32 */
33
34 /**
35 * This class generates form components for Tag.
36 */
37 class CRM_Tag_Form_Edit 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 public function preProcess() {
48 CRM_Utils_Request::retrieve('id', 'Integer', $this, FALSE);
49 $this->set('BAOName', 'CRM_Core_BAO_Tag');
50 parent::preProcess();
51 }
52
53 /**
54 * Build the form object.
55 */
56 public function buildQuickForm() {
57 $bounceUrl = CRM_Utils_System::url('civicrm/tag');
58 if ($this->_action == CRM_Core_Action::DELETE) {
59 if (!$this->_id) {
60 $this->_id = explode(',', CRM_Utils_Request::retrieve('id', 'String'));
61 }
62 $this->_id = (array) $this->_id;
63 if (!$this->_id) {
64 CRM_Core_Error::statusBounce(ts("Unknown tag."), $bounceUrl);
65 }
66 foreach ($this->_id as $id) {
67 if (!CRM_Utils_Rule::positiveInteger($id)) {
68 CRM_Core_Error::statusBounce(ts("Unknown tag."), $bounceUrl);
69 }
70 if ($tag = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Tag', $id, 'name', 'parent_id')) {
71 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)), $bounceUrl);
72 }
73 if (!CRM_Core_Permission::check('administer reserved tags') && CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Tag', $id, 'is_reserved')) {
74 CRM_Core_Error::statusBounce(ts("You do not have sufficient permission to delete this reserved tag."), $bounceUrl);
75 }
76 }
77 if (count($this->_id) > 1) {
78 $this->assign('delName', ts('%1 tags', array(1 => count($this->_id))));
79 }
80 }
81 else {
82 $adminTagset = CRM_Core_Permission::check('administer Tagsets');
83 $adminReservedTags = CRM_Core_Permission::check('administer reserved tags');
84
85 $this->_isTagSet = CRM_Utils_Request::retrieve('tagset', 'Positive', $this);
86
87 if (!$this->_isTagSet && $this->_id &&
88 CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Tag', $this->_id, 'is_tagset')
89 ) {
90 $this->_isTagSet = TRUE;
91 }
92 if ($this->_isTagSet && !$adminTagset) {
93 CRM_Core_Error::statusBounce(ts("You do not have sufficient permission to edit this tagset."), $bounceUrl);
94 }
95 if ($this->_id && !$adminReservedTags && CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Tag', $this->_id, 'is_reserved')) {
96 CRM_Core_Error::statusBounce(ts("You do not have sufficient permission to edit this reserved tag."), $bounceUrl);
97 }
98
99 if ($this->_id) {
100 $parentId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Tag', $this->_id, 'parent_id');
101 }
102 else {
103 $parentId = CRM_Utils_Request::retrieve('parent_id', 'Integer', $this);
104 }
105 $isTagSetChild = $parentId ? CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Tag', $parentId, 'is_tagset') : FALSE;
106
107 if (!$this->_isTagSet) {
108 if (!$isTagSetChild) {
109 $colorTags = CRM_Core_BAO_Tag::getColorTags(NULL, TRUE, $this->_id);
110 $this->add('select2', 'parent_id', ts('Parent Tag'), $colorTags, FALSE, array('placeholder' => ts('- select -')));
111 }
112
113 // Tagsets are not selectable by definition so only include the selectable field if NOT a tagset.
114 $selectable = $this->add('checkbox', 'is_selectable', ts('Selectable?'));
115 // Selectable should be checked by default when creating a new tag
116 if ($this->_action == CRM_Core_Action::ADD) {
117 $selectable->setValue(1);
118 }
119
120 $this->add('color', 'color', ts('Color'));
121 }
122
123 $this->assign('isTagSet', $this->_isTagSet);
124
125 $this->applyFilter('__ALL__', 'trim');
126
127 $this->add('text', 'name', ts('Name'),
128 CRM_Core_DAO::getAttribute('CRM_Core_DAO_Tag', 'name'), TRUE
129 );
130 $this->addRule('name', ts('Name already exists in Database.'), 'objectExists', array(
131 'CRM_Core_DAO_Tag',
132 $this->_id,
133 ));
134
135 $this->add('text', 'description', ts('Description'),
136 CRM_Core_DAO::getAttribute('CRM_Core_DAO_Tag', 'description')
137 );
138
139 $isReserved = $this->add('checkbox', 'is_reserved', ts('Reserved?'));
140
141 if (!$isTagSetChild) {
142 $this->addSelect('used_for', array('multiple' => TRUE, 'option_url' => NULL));
143 }
144
145 $this->assign('adminTagset', $adminTagset);
146
147 if (!$adminReservedTags) {
148 $isReserved->freeze();
149 }
150 $this->assign('adminReservedTags', $adminReservedTags);
151 }
152 $this->setPageTitle($this->_isTagSet ? ts('Tag Set') : ts('Tag'));
153 parent::buildQuickForm();
154 }
155
156 /**
157 * Set default values for the form.
158 *
159 * @return array
160 */
161 public function setDefaultValues() {
162 $defaults = parent::setDefaultValues();
163 $cloneFrom = CRM_Utils_Request::retrieve('clone_from', 'Integer');
164 if (empty($this->_id) && $cloneFrom) {
165 $params = array('id' => $cloneFrom);
166 CRM_Core_BAO_Tag::retrieve($params, $this->_values);
167 $this->_values['name'] .= ' (' . ts('copy') . ')';
168 if (!empty($this->_values['is_reserved']) && !CRM_Core_Permission::check('administer reserved tags')) {
169 $this->_values['is_reserved'] = 0;
170 }
171 $defaults = $this->_values;
172 }
173 if (empty($defaults['color'])) {
174 $defaults['color'] = '#ffffff';
175 }
176 if (empty($this->_id) && empty($defaults['used_for'])) {
177 $defaults['used_for'] = 'civicrm_contact';
178 }
179 return $defaults;
180 }
181
182 /**
183 * Process the form submission.
184 */
185 public function postProcess() {
186 if ($this->_action == CRM_Core_Action::DELETE) {
187 $deleted = 0;
188 $tag = civicrm_api3('tag', 'getsingle', array('id' => $this->_id[0]));
189 foreach ($this->_id as $id) {
190 if (CRM_Core_BAO_Tag::del($id)) {
191 $deleted++;
192 }
193 }
194 if (count($this->_id) == 1 && $deleted == 1) {
195 if ($tag['is_tagset']) {
196 CRM_Core_Session::setStatus(ts("The tag set '%1' has been deleted.", array(1 => $tag['name'])), ts('Deleted'), 'success');
197 }
198 else {
199 CRM_Core_Session::setStatus(ts("The tag '%1' has been deleted.", array(1 => $tag['name'])), ts('Deleted'), 'success');
200 }
201 }
202 else {
203 CRM_Core_Session::setStatus(ts("Deleted %1 tags.", array(1 => $deleted)), ts('Deleted'), 'success');
204 }
205 }
206 else {
207 $params = $this->exportValues();
208 if ($this->_id) {
209 $params['id'] = $this->_id;
210 }
211
212 if (isset($params['used_for']) && ($this->_action == CRM_Core_Action::ADD || $this->_action == CRM_Core_Action::UPDATE)) {
213 $params['used_for'] = implode(",", $params['used_for']);
214 }
215
216 $params['is_tagset'] = 0;
217 if ($this->_isTagSet) {
218 $params['is_tagset'] = 1;
219 }
220
221 if (!isset($params['is_reserved'])) {
222 $params['is_reserved'] = 0;
223 }
224
225 if (!isset($params['parent_id']) && $this->get('parent_id')) {
226 $params['parent_id'] = $this->get('parent_id');
227 }
228 if (empty($params['parent_id'])) {
229 $params['parent_id'] = '';
230 }
231
232 if (!isset($params['is_selectable'])) {
233 $params['is_selectable'] = 0;
234 }
235 $tag = CRM_Core_BAO_Tag::add($params);
236 CRM_Core_Session::setStatus(ts("The tag '%1' has been saved.", array(1 => $tag->name)), ts('Saved'), 'success');
237 $this->ajaxResponse['tag'] = $tag->toArray();
238 }
239 CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url('civicrm/tag'));
240 }
241
242 }