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