Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
18 | /** | |
ce064e4f | 19 | * This class generates form components for Tag. |
6a488035 | 20 | */ |
eaaaef83 | 21 | class CRM_Tag_Form_Edit extends CRM_Admin_Form { |
6a488035 | 22 | protected $_isTagSet; |
d5965a37 | 23 | |
6e62b28c TM |
24 | /** |
25 | * Explicitly declare the entity api name. | |
26 | */ | |
27 | public function getDefaultEntity() { | |
28 | return 'Tag'; | |
29 | } | |
6a488035 | 30 | |
d3cbd0a5 CW |
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 | ||
6a488035 | 37 | /** |
eceb18cc | 38 | * Build the form object. |
6a488035 TO |
39 | */ |
40 | public function buildQuickForm() { | |
b421c13a | 41 | $bounceUrl = CRM_Utils_System::url('civicrm/tag'); |
6a488035 | 42 | if ($this->_action == CRM_Core_Action::DELETE) { |
d3cbd0a5 CW |
43 | if (!$this->_id) { |
44 | $this->_id = explode(',', CRM_Utils_Request::retrieve('id', 'String')); | |
6a488035 | 45 | } |
d3cbd0a5 CW |
46 | $this->_id = (array) $this->_id; |
47 | if (!$this->_id) { | |
b421c13a | 48 | CRM_Core_Error::statusBounce(ts("Unknown tag."), $bounceUrl); |
d3cbd0a5 CW |
49 | } |
50 | foreach ($this->_id as $id) { | |
51 | if (!CRM_Utils_Rule::positiveInteger($id)) { | |
b421c13a | 52 | CRM_Core_Error::statusBounce(ts("Unknown tag."), $bounceUrl); |
d3cbd0a5 CW |
53 | } |
54 | if ($tag = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Tag', $id, 'name', 'parent_id')) { | |
be2fb01f | 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); |
d3cbd0a5 CW |
56 | } |
57 | if (!CRM_Core_Permission::check('administer reserved tags') && CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Tag', $id, 'is_reserved')) { | |
b421c13a | 58 | CRM_Core_Error::statusBounce(ts("You do not have sufficient permission to delete this reserved tag."), $bounceUrl); |
d3cbd0a5 CW |
59 | } |
60 | } | |
61 | if (count($this->_id) > 1) { | |
be2fb01f | 62 | $this->assign('delName', ts('%1 tags', [1 => count($this->_id)])); |
bf4b6f8f | 63 | } |
6a488035 TO |
64 | } |
65 | else { | |
b421c13a CW |
66 | $adminTagset = CRM_Core_Permission::check('administer Tagsets'); |
67 | $adminReservedTags = CRM_Core_Permission::check('administer reserved tags'); | |
68 | ||
6a488035 TO |
69 | $this->_isTagSet = CRM_Utils_Request::retrieve('tagset', 'Positive', $this); |
70 | ||
d3cbd0a5 | 71 | if (!$this->_isTagSet && $this->_id && |
6a488035 TO |
72 | CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Tag', $this->_id, 'is_tagset') |
73 | ) { | |
74 | $this->_isTagSet = TRUE; | |
75 | } | |
b421c13a CW |
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 | } | |
6a488035 | 82 | |
6bed6964 CW |
83 | if ($this->_id) { |
84 | $parentId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Tag', $this->_id, 'parent_id'); | |
6bed6964 | 85 | } |
d3cbd0a5 CW |
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; | |
6bed6964 | 90 | |
6a488035 | 91 | if (!$this->_isTagSet) { |
6bed6964 CW |
92 | if (!$isTagSetChild) { |
93 | $colorTags = CRM_Core_BAO_Tag::getColorTags(NULL, TRUE, $this->_id); | |
be2fb01f | 94 | $this->add('select2', 'parent_id', ts('Parent Tag'), $colorTags, FALSE, ['placeholder' => ts('- select -')]); |
6bed6964 | 95 | } |
69582599 DG |
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) { | |
02fc859b | 101 | $selectable->setValue(1); |
69582599 DG |
102 | } |
103 | ||
1192bd09 | 104 | $this->add('color', 'color', ts('Color')); |
6a488035 TO |
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 | ); | |
be2fb01f | 114 | $this->addRule('name', ts('Name already exists in Database.'), 'objectExists', [ |
3655bea4 SL |
115 | 'CRM_Core_DAO_Tag', |
116 | $this->_id, | |
117 | ]); | |
6a488035 TO |
118 | |
119 | $this->add('text', 'description', ts('Description'), | |
120 | CRM_Core_DAO::getAttribute('CRM_Core_DAO_Tag', 'description') | |
121 | ); | |
122 | ||
6a488035 TO |
123 | $isReserved = $this->add('checkbox', 'is_reserved', ts('Reserved?')); |
124 | ||
d3cbd0a5 | 125 | if (!$isTagSetChild) { |
be2fb01f | 126 | $this->addSelect('used_for', ['multiple' => TRUE, 'option_url' => NULL]); |
d3cbd0a5 | 127 | } |
6a488035 | 128 | |
6a488035 TO |
129 | $this->assign('adminTagset', $adminTagset); |
130 | ||
b421c13a | 131 | if (!$adminReservedTags) { |
6a488035 | 132 | $isReserved->freeze(); |
6a488035 TO |
133 | } |
134 | $this->assign('adminReservedTags', $adminReservedTags); | |
6a488035 | 135 | } |
bd525987 | 136 | $this->setPageTitle($this->_isTagSet ? ts('Tag Set') : ts('Tag')); |
a1f552a9 | 137 | parent::buildQuickForm(); |
6a488035 TO |
138 | } |
139 | ||
f2ac86d1 | 140 | /** |
141 | * Set default values for the form. | |
142 | * | |
143 | * @return array | |
144 | */ | |
1192bd09 CW |
145 | public function setDefaultValues() { |
146 | $defaults = parent::setDefaultValues(); | |
d3cbd0a5 CW |
147 | $cloneFrom = CRM_Utils_Request::retrieve('clone_from', 'Integer'); |
148 | if (empty($this->_id) && $cloneFrom) { | |
be2fb01f | 149 | $params = ['id' => $cloneFrom]; |
d3cbd0a5 CW |
150 | CRM_Core_BAO_Tag::retrieve($params, $this->_values); |
151 | $this->_values['name'] .= ' (' . ts('copy') . ')'; | |
b421c13a CW |
152 | if (!empty($this->_values['is_reserved']) && !CRM_Core_Permission::check('administer reserved tags')) { |
153 | $this->_values['is_reserved'] = 0; | |
154 | } | |
d3cbd0a5 CW |
155 | $defaults = $this->_values; |
156 | } | |
157 | if (empty($defaults['color'])) { | |
d73974ac CW |
158 | $defaults['color'] = '#ffffff'; |
159 | } | |
d3cbd0a5 | 160 | if (empty($this->_id) && empty($defaults['used_for'])) { |
bd525987 CW |
161 | $defaults['used_for'] = 'civicrm_contact'; |
162 | } | |
1192bd09 CW |
163 | return $defaults; |
164 | } | |
165 | ||
6a488035 | 166 | /** |
eceb18cc | 167 | * Process the form submission. |
6a488035 TO |
168 | */ |
169 | public function postProcess() { | |
d3cbd0a5 CW |
170 | if ($this->_action == CRM_Core_Action::DELETE) { |
171 | $deleted = 0; | |
be2fb01f | 172 | $tag = civicrm_api3('tag', 'getsingle', ['id' => $this->_id[0]]); |
d3cbd0a5 CW |
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']) { | |
be2fb01f | 180 | CRM_Core_Session::setStatus(ts("The tag set '%1' has been deleted.", [1 => $tag['name']]), ts('Deleted'), 'success'); |
d3cbd0a5 CW |
181 | } |
182 | else { | |
be2fb01f | 183 | CRM_Core_Session::setStatus(ts("The tag '%1' has been deleted.", [1 => $tag['name']]), ts('Deleted'), 'success'); |
d3cbd0a5 CW |
184 | } |
185 | } | |
186 | else { | |
be2fb01f | 187 | CRM_Core_Session::setStatus(ts("Deleted %1 tags.", [1 => $deleted]), ts('Deleted'), 'success'); |
d3cbd0a5 | 188 | } |
6a488035 | 189 | } |
d3cbd0a5 CW |
190 | else { |
191 | $params = $this->exportValues(); | |
192 | if ($this->_id) { | |
193 | $params['id'] = $this->_id; | |
194 | } | |
6a488035 | 195 | |
d3cbd0a5 CW |
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 | } | |
6a488035 | 199 | |
d3cbd0a5 CW |
200 | $params['is_tagset'] = 0; |
201 | if ($this->_isTagSet) { | |
202 | $params['is_tagset'] = 1; | |
203 | } | |
6a488035 | 204 | |
d3cbd0a5 CW |
205 | if (!isset($params['is_reserved'])) { |
206 | $params['is_reserved'] = 0; | |
207 | } | |
f37c6bf1 | 208 | |
d3cbd0a5 CW |
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 | } | |
d73974ac | 215 | |
d3cbd0a5 CW |
216 | if (!isset($params['is_selectable'])) { |
217 | $params['is_selectable'] = 0; | |
6a488035 | 218 | } |
bd525987 | 219 | $tag = CRM_Core_BAO_Tag::add($params); |
be2fb01f | 220 | CRM_Core_Session::setStatus(ts("The tag '%1' has been saved.", [1 => $tag->name]), ts('Saved'), 'success'); |
d3cbd0a5 | 221 | $this->ajaxResponse['tag'] = $tag->toArray(); |
6a488035 | 222 | } |
d3cbd0a5 | 223 | CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url('civicrm/tag')); |
6a488035 | 224 | } |
e2046b33 | 225 | |
6a488035 | 226 | } |