3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class generates form components for Tag.
21 class CRM_Tag_Form_Edit
extends CRM_Admin_Form
{
25 * Explicitly declare the entity api name.
27 public function getDefaultEntity() {
31 public function preProcess() {
32 CRM_Utils_Request
::retrieve('id', 'Integer', $this, FALSE);
33 $this->set('BAOName', 'CRM_Core_BAO_Tag');
38 * Build the form object.
40 public function buildQuickForm() {
41 $bounceUrl = CRM_Utils_System
::url('civicrm/tag');
42 if ($this->_action
== CRM_Core_Action
::DELETE
) {
44 $this->_id
= explode(',', CRM_Utils_Request
::retrieve('id', 'String'));
46 $this->_id
= (array) $this->_id
;
48 CRM_Core_Error
::statusBounce(ts("Unknown tag."), $bounceUrl);
50 foreach ($this->_id
as $id) {
51 if (!CRM_Utils_Rule
::positiveInteger($id)) {
52 CRM_Core_Error
::statusBounce(ts("Unknown tag."), $bounceUrl);
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);
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);
61 if (count($this->_id
) > 1) {
62 $this->assign('delName', ts('%1 tags', [1 => count($this->_id
)]));
66 $adminTagset = CRM_Core_Permission
::check('administer Tagsets');
67 $adminReservedTags = CRM_Core_Permission
::check('administer reserved tags');
69 $this->_isTagSet
= CRM_Utils_Request
::retrieve('tagset', 'Positive', $this);
71 if (!$this->_isTagSet
&& $this->_id
&&
72 CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Tag', $this->_id
, 'is_tagset')
74 $this->_isTagSet
= TRUE;
76 if ($this->_isTagSet
&& !$adminTagset) {
77 CRM_Core_Error
::statusBounce(ts("You do not have sufficient permission to edit this tagset."), $bounceUrl);
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);
84 $parentId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Tag', $this->_id
, 'parent_id');
87 $parentId = CRM_Utils_Request
::retrieve('parent_id', 'Integer', $this);
89 $isTagSetChild = $parentId ? CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Tag', $parentId, 'is_tagset') : FALSE;
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 -')]);
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);
104 $this->add('color', 'color', ts('Color'));
107 $this->assign('isTagSet', $this->_isTagSet
);
109 $this->applyFilter('__ALL__', 'trim');
111 $this->add('text', 'name', ts('Name'),
112 CRM_Core_DAO
::getAttribute('CRM_Core_DAO_Tag', 'name'), TRUE
114 $this->addRule('name', ts('Name already exists in Database.'), 'objectExists', [
119 $this->add('text', 'description', ts('Description'),
120 CRM_Core_DAO
::getAttribute('CRM_Core_DAO_Tag', 'description')
123 $isReserved = $this->add('checkbox', 'is_reserved', ts('Reserved?'));
125 if (!$isTagSetChild) {
126 $this->addSelect('used_for', ['multiple' => TRUE, 'option_url' => NULL]);
129 $this->assign('adminTagset', $adminTagset);
131 if (!$adminReservedTags) {
132 $isReserved->freeze();
134 $this->assign('adminReservedTags', $adminReservedTags);
136 $this->setPageTitle($this->_isTagSet ?
ts('Tag Set') : ts('Tag'));
137 parent
::buildQuickForm();
141 * Set default values for the form.
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;
155 $defaults = $this->_values
;
157 if (empty($defaults['color'])) {
158 $defaults['color'] = '#ffffff';
160 if (empty($this->_id
) && empty($defaults['used_for'])) {
161 $defaults['used_for'] = 'civicrm_contact';
167 * Process the form submission.
169 public function postProcess() {
170 if ($this->_action
== CRM_Core_Action
::DELETE
) {
172 $tag = civicrm_api3('tag', 'getsingle', ['id' => $this->_id
[0]]);
173 foreach ($this->_id
as $id) {
174 if (CRM_Core_BAO_Tag
::del($id)) {
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');
183 CRM_Core_Session
::setStatus(ts("The tag '%1' has been deleted.", [1 => $tag['name']]), ts('Deleted'), 'success');
187 CRM_Core_Session
::setStatus(ts("Deleted %1 tags.", [1 => $deleted]), ts('Deleted'), 'success');
191 $params = $this->exportValues();
193 $params['id'] = $this->_id
;
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']);
200 $params['is_tagset'] = 0;
201 if ($this->_isTagSet
) {
202 $params['is_tagset'] = 1;
205 if (!isset($params['is_reserved'])) {
206 $params['is_reserved'] = 0;
209 if (!isset($params['parent_id']) && $this->get('parent_id')) {
210 $params['parent_id'] = $this->get('parent_id');
212 if (empty($params['parent_id'])) {
213 $params['parent_id'] = '';
216 if (!isset($params['is_selectable'])) {
217 $params['is_selectable'] = 0;
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();
223 CRM_Core_Session
::singleton()->pushUserContext(CRM_Utils_System
::url('civicrm/tag'));