3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2018
35 * This class generates form components for Tag.
37 class CRM_Tag_Form_Edit
extends CRM_Admin_Form
{
41 * Explicitly declare the entity api name.
43 public function getDefaultEntity() {
47 public function preProcess() {
48 CRM_Utils_Request
::retrieve('id', 'Integer', $this, FALSE);
49 $this->set('BAOName', 'CRM_Core_BAO_Tag');
54 * Build the form object.
56 public function buildQuickForm() {
57 $bounceUrl = CRM_Utils_System
::url('civicrm/tag');
58 if ($this->_action
== CRM_Core_Action
::DELETE
) {
60 $this->_id
= explode(',', CRM_Utils_Request
::retrieve('id', 'String'));
62 $this->_id
= (array) $this->_id
;
64 CRM_Core_Error
::statusBounce(ts("Unknown tag."), $bounceUrl);
66 foreach ($this->_id
as $id) {
67 if (!CRM_Utils_Rule
::positiveInteger($id)) {
68 CRM_Core_Error
::statusBounce(ts("Unknown tag."), $bounceUrl);
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);
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);
77 if (count($this->_id
) > 1) {
78 $this->assign('delName', ts('%1 tags', array(1 => count($this->_id
))));
82 $adminTagset = CRM_Core_Permission
::check('administer Tagsets');
83 $adminReservedTags = CRM_Core_Permission
::check('administer reserved tags');
85 $this->_isTagSet
= CRM_Utils_Request
::retrieve('tagset', 'Positive', $this);
87 if (!$this->_isTagSet
&& $this->_id
&&
88 CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Tag', $this->_id
, 'is_tagset')
90 $this->_isTagSet
= TRUE;
92 if ($this->_isTagSet
&& !$adminTagset) {
93 CRM_Core_Error
::statusBounce(ts("You do not have sufficient permission to edit this tagset."), $bounceUrl);
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);
100 $parentId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Tag', $this->_id
, 'parent_id');
103 $parentId = CRM_Utils_Request
::retrieve('parent_id', 'Integer', $this);
105 $isTagSetChild = $parentId ? CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Tag', $parentId, 'is_tagset') : FALSE;
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 -')));
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);
120 $this->add('color', 'color', ts('Color'));
123 $this->assign('isTagSet', $this->_isTagSet
);
125 $this->applyFilter('__ALL__', 'trim');
127 $this->add('text', 'name', ts('Name'),
128 CRM_Core_DAO
::getAttribute('CRM_Core_DAO_Tag', 'name'), TRUE
130 $this->addRule('name', ts('Name already exists in Database.'), 'objectExists', array(
135 $this->add('text', 'description', ts('Description'),
136 CRM_Core_DAO
::getAttribute('CRM_Core_DAO_Tag', 'description')
139 $isReserved = $this->add('checkbox', 'is_reserved', ts('Reserved?'));
141 if (!$isTagSetChild) {
142 $this->addSelect('used_for', array('multiple' => TRUE, 'option_url' => NULL));
145 $this->assign('adminTagset', $adminTagset);
147 if (!$adminReservedTags) {
148 $isReserved->freeze();
150 $this->assign('adminReservedTags', $adminReservedTags);
152 $this->setPageTitle($this->_isTagSet ?
ts('Tag Set') : ts('Tag'));
153 parent
::buildQuickForm();
157 * Set default values for the form.
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;
171 $defaults = $this->_values
;
173 if (empty($defaults['color'])) {
174 $defaults['color'] = '#ffffff';
176 if (empty($this->_id
) && empty($defaults['used_for'])) {
177 $defaults['used_for'] = 'civicrm_contact';
183 * Process the form submission.
185 public function postProcess() {
186 if ($this->_action
== CRM_Core_Action
::DELETE
) {
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)) {
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');
199 CRM_Core_Session
::setStatus(ts("The tag '%1' has been deleted.", array(1 => $tag['name'])), ts('Deleted'), 'success');
203 CRM_Core_Session
::setStatus(ts("Deleted %1 tags.", array(1 => $deleted)), ts('Deleted'), 'success');
207 $params = $this->exportValues();
209 $params['id'] = $this->_id
;
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']);
216 $params['is_tagset'] = 0;
217 if ($this->_isTagSet
) {
218 $params['is_tagset'] = 1;
221 if (!isset($params['is_reserved'])) {
222 $params['is_reserved'] = 0;
225 if (!isset($params['parent_id']) && $this->get('parent_id')) {
226 $params['parent_id'] = $this->get('parent_id');
228 if (empty($params['parent_id'])) {
229 $params['parent_id'] = '';
232 if (!isset($params['is_selectable'])) {
233 $params['is_selectable'] = 0;
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();
239 CRM_Core_Session
::singleton()->pushUserContext(CRM_Utils_System
::url('civicrm/tag'));