Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
6a488035 TO |
32 | */ |
33 | ||
34 | /** | |
ce064e4f | 35 | * This class generates form components for Tag. |
6a488035 | 36 | */ |
eaaaef83 | 37 | class CRM_Tag_Form_Edit extends CRM_Admin_Form { |
6a488035 | 38 | protected $_isTagSet; |
d5965a37 | 39 | |
6e62b28c TM |
40 | /** |
41 | * Explicitly declare the entity api name. | |
42 | */ | |
43 | public function getDefaultEntity() { | |
44 | return 'Tag'; | |
45 | } | |
6a488035 | 46 | |
d3cbd0a5 CW |
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 | ||
6a488035 | 53 | /** |
eceb18cc | 54 | * Build the form object. |
6a488035 TO |
55 | */ |
56 | public function buildQuickForm() { | |
b421c13a | 57 | $bounceUrl = CRM_Utils_System::url('civicrm/tag'); |
6a488035 | 58 | if ($this->_action == CRM_Core_Action::DELETE) { |
d3cbd0a5 CW |
59 | if (!$this->_id) { |
60 | $this->_id = explode(',', CRM_Utils_Request::retrieve('id', 'String')); | |
6a488035 | 61 | } |
d3cbd0a5 CW |
62 | $this->_id = (array) $this->_id; |
63 | if (!$this->_id) { | |
b421c13a | 64 | CRM_Core_Error::statusBounce(ts("Unknown tag."), $bounceUrl); |
d3cbd0a5 CW |
65 | } |
66 | foreach ($this->_id as $id) { | |
67 | if (!CRM_Utils_Rule::positiveInteger($id)) { | |
b421c13a | 68 | CRM_Core_Error::statusBounce(ts("Unknown tag."), $bounceUrl); |
d3cbd0a5 CW |
69 | } |
70 | if ($tag = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Tag', $id, 'name', 'parent_id')) { | |
be2fb01f | 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.", [1 => $tag]), $bounceUrl); |
d3cbd0a5 CW |
72 | } |
73 | if (!CRM_Core_Permission::check('administer reserved tags') && CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Tag', $id, 'is_reserved')) { | |
b421c13a | 74 | CRM_Core_Error::statusBounce(ts("You do not have sufficient permission to delete this reserved tag."), $bounceUrl); |
d3cbd0a5 CW |
75 | } |
76 | } | |
77 | if (count($this->_id) > 1) { | |
be2fb01f | 78 | $this->assign('delName', ts('%1 tags', [1 => count($this->_id)])); |
bf4b6f8f | 79 | } |
6a488035 TO |
80 | } |
81 | else { | |
b421c13a CW |
82 | $adminTagset = CRM_Core_Permission::check('administer Tagsets'); |
83 | $adminReservedTags = CRM_Core_Permission::check('administer reserved tags'); | |
84 | ||
6a488035 TO |
85 | $this->_isTagSet = CRM_Utils_Request::retrieve('tagset', 'Positive', $this); |
86 | ||
d3cbd0a5 | 87 | if (!$this->_isTagSet && $this->_id && |
6a488035 TO |
88 | CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Tag', $this->_id, 'is_tagset') |
89 | ) { | |
90 | $this->_isTagSet = TRUE; | |
91 | } | |
b421c13a CW |
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 | } | |
6a488035 | 98 | |
6bed6964 CW |
99 | if ($this->_id) { |
100 | $parentId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Tag', $this->_id, 'parent_id'); | |
6bed6964 | 101 | } |
d3cbd0a5 CW |
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; | |
6bed6964 | 106 | |
6a488035 | 107 | if (!$this->_isTagSet) { |
6bed6964 CW |
108 | if (!$isTagSetChild) { |
109 | $colorTags = CRM_Core_BAO_Tag::getColorTags(NULL, TRUE, $this->_id); | |
be2fb01f | 110 | $this->add('select2', 'parent_id', ts('Parent Tag'), $colorTags, FALSE, ['placeholder' => ts('- select -')]); |
6bed6964 | 111 | } |
69582599 DG |
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) { | |
02fc859b | 117 | $selectable->setValue(1); |
69582599 DG |
118 | } |
119 | ||
1192bd09 | 120 | $this->add('color', 'color', ts('Color')); |
6a488035 TO |
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 | ); | |
be2fb01f | 130 | $this->addRule('name', ts('Name already exists in Database.'), 'objectExists', [ |
3655bea4 SL |
131 | 'CRM_Core_DAO_Tag', |
132 | $this->_id, | |
133 | ]); | |
6a488035 TO |
134 | |
135 | $this->add('text', 'description', ts('Description'), | |
136 | CRM_Core_DAO::getAttribute('CRM_Core_DAO_Tag', 'description') | |
137 | ); | |
138 | ||
6a488035 TO |
139 | $isReserved = $this->add('checkbox', 'is_reserved', ts('Reserved?')); |
140 | ||
d3cbd0a5 | 141 | if (!$isTagSetChild) { |
be2fb01f | 142 | $this->addSelect('used_for', ['multiple' => TRUE, 'option_url' => NULL]); |
d3cbd0a5 | 143 | } |
6a488035 | 144 | |
6a488035 TO |
145 | $this->assign('adminTagset', $adminTagset); |
146 | ||
b421c13a | 147 | if (!$adminReservedTags) { |
6a488035 | 148 | $isReserved->freeze(); |
6a488035 TO |
149 | } |
150 | $this->assign('adminReservedTags', $adminReservedTags); | |
6a488035 | 151 | } |
bd525987 | 152 | $this->setPageTitle($this->_isTagSet ? ts('Tag Set') : ts('Tag')); |
a1f552a9 | 153 | parent::buildQuickForm(); |
6a488035 TO |
154 | } |
155 | ||
f2ac86d1 | 156 | /** |
157 | * Set default values for the form. | |
158 | * | |
159 | * @return array | |
160 | */ | |
1192bd09 CW |
161 | public function setDefaultValues() { |
162 | $defaults = parent::setDefaultValues(); | |
d3cbd0a5 CW |
163 | $cloneFrom = CRM_Utils_Request::retrieve('clone_from', 'Integer'); |
164 | if (empty($this->_id) && $cloneFrom) { | |
be2fb01f | 165 | $params = ['id' => $cloneFrom]; |
d3cbd0a5 CW |
166 | CRM_Core_BAO_Tag::retrieve($params, $this->_values); |
167 | $this->_values['name'] .= ' (' . ts('copy') . ')'; | |
b421c13a CW |
168 | if (!empty($this->_values['is_reserved']) && !CRM_Core_Permission::check('administer reserved tags')) { |
169 | $this->_values['is_reserved'] = 0; | |
170 | } | |
d3cbd0a5 CW |
171 | $defaults = $this->_values; |
172 | } | |
173 | if (empty($defaults['color'])) { | |
d73974ac CW |
174 | $defaults['color'] = '#ffffff'; |
175 | } | |
d3cbd0a5 | 176 | if (empty($this->_id) && empty($defaults['used_for'])) { |
bd525987 CW |
177 | $defaults['used_for'] = 'civicrm_contact'; |
178 | } | |
1192bd09 CW |
179 | return $defaults; |
180 | } | |
181 | ||
6a488035 | 182 | /** |
eceb18cc | 183 | * Process the form submission. |
6a488035 TO |
184 | */ |
185 | public function postProcess() { | |
d3cbd0a5 CW |
186 | if ($this->_action == CRM_Core_Action::DELETE) { |
187 | $deleted = 0; | |
be2fb01f | 188 | $tag = civicrm_api3('tag', 'getsingle', ['id' => $this->_id[0]]); |
d3cbd0a5 CW |
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']) { | |
be2fb01f | 196 | CRM_Core_Session::setStatus(ts("The tag set '%1' has been deleted.", [1 => $tag['name']]), ts('Deleted'), 'success'); |
d3cbd0a5 CW |
197 | } |
198 | else { | |
be2fb01f | 199 | CRM_Core_Session::setStatus(ts("The tag '%1' has been deleted.", [1 => $tag['name']]), ts('Deleted'), 'success'); |
d3cbd0a5 CW |
200 | } |
201 | } | |
202 | else { | |
be2fb01f | 203 | CRM_Core_Session::setStatus(ts("Deleted %1 tags.", [1 => $deleted]), ts('Deleted'), 'success'); |
d3cbd0a5 | 204 | } |
6a488035 | 205 | } |
d3cbd0a5 CW |
206 | else { |
207 | $params = $this->exportValues(); | |
208 | if ($this->_id) { | |
209 | $params['id'] = $this->_id; | |
210 | } | |
6a488035 | 211 | |
d3cbd0a5 CW |
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 | } | |
6a488035 | 215 | |
d3cbd0a5 CW |
216 | $params['is_tagset'] = 0; |
217 | if ($this->_isTagSet) { | |
218 | $params['is_tagset'] = 1; | |
219 | } | |
6a488035 | 220 | |
d3cbd0a5 CW |
221 | if (!isset($params['is_reserved'])) { |
222 | $params['is_reserved'] = 0; | |
223 | } | |
f37c6bf1 | 224 | |
d3cbd0a5 CW |
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 | } | |
d73974ac | 231 | |
d3cbd0a5 CW |
232 | if (!isset($params['is_selectable'])) { |
233 | $params['is_selectable'] = 0; | |
6a488035 | 234 | } |
bd525987 | 235 | $tag = CRM_Core_BAO_Tag::add($params); |
be2fb01f | 236 | CRM_Core_Session::setStatus(ts("The tag '%1' has been saved.", [1 => $tag->name]), ts('Saved'), 'success'); |
d3cbd0a5 | 237 | $this->ajaxResponse['tag'] = $tag->toArray(); |
6a488035 | 238 | } |
d3cbd0a5 | 239 | CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url('civicrm/tag')); |
6a488035 | 240 | } |
e2046b33 | 241 | |
6a488035 | 242 | } |