CRM-19943 - Merge multiple tags from new UI
[civicrm-core.git] / CRM / Tag / Form / Edit.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
0f03f337 6 | Copyright CiviCRM LLC (c) 2004-2017 |
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
0f03f337 31 * @copyright CiviCRM LLC (c) 2004-2017
6a488035
TO
32 */
33
34/**
ce064e4f 35 * This class generates form components for Tag.
6a488035 36 */
eaaaef83 37class 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() {
57 if ($this->_action == CRM_Core_Action::DELETE) {
d3cbd0a5
CW
58 $url = CRM_Utils_System::url('civicrm/tag');
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) {
64 CRM_Core_Error::statusBounce(ts("Unknown tag."), $url);
65 }
66 foreach ($this->_id as $id) {
67 if (!CRM_Utils_Rule::positiveInteger($id)) {
68 CRM_Core_Error::statusBounce(ts("Unknown tag."), $url);
69 }
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)), $url);
72 }
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."), $url);
75 }
76 }
77 if (count($this->_id) > 1) {
78 $this->assign('delName', ts('%1 tags', array(1 => count($this->_id))));
bf4b6f8f 79 }
6a488035
TO
80 }
81 else {
82 $this->_isTagSet = CRM_Utils_Request::retrieve('tagset', 'Positive', $this);
83
d3cbd0a5 84 if (!$this->_isTagSet && $this->_id &&
6a488035
TO
85 CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Tag', $this->_id, 'is_tagset')
86 ) {
87 $this->_isTagSet = TRUE;
88 }
89
6bed6964
CW
90 if ($this->_id) {
91 $parentId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Tag', $this->_id, 'parent_id');
6bed6964 92 }
d3cbd0a5
CW
93 else {
94 $parentId = CRM_Utils_Request::retrieve('parent_id', 'Integer', $this);
95 }
96 $isTagSetChild = $parentId ? CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Tag', $parentId, 'is_tagset') : FALSE;
6bed6964 97
6a488035 98 if (!$this->_isTagSet) {
6bed6964
CW
99 if (!$isTagSetChild) {
100 $colorTags = CRM_Core_BAO_Tag::getColorTags(NULL, TRUE, $this->_id);
101 $this->add('select2', 'parent_id', ts('Parent Tag'), $colorTags, FALSE, array('placeholder' => ts('- select -')));
102 }
69582599
DG
103
104 // Tagsets are not selectable by definition so only include the selectable field if NOT a tagset.
105 $selectable = $this->add('checkbox', 'is_selectable', ts('Selectable?'));
106 // Selectable should be checked by default when creating a new tag
107 if ($this->_action == CRM_Core_Action::ADD) {
02fc859b 108 $selectable->setValue(1);
69582599
DG
109 }
110
1192bd09 111 $this->add('color', 'color', ts('Color'));
6a488035
TO
112 }
113
114 $this->assign('isTagSet', $this->_isTagSet);
115
116 $this->applyFilter('__ALL__', 'trim');
117
118 $this->add('text', 'name', ts('Name'),
119 CRM_Core_DAO::getAttribute('CRM_Core_DAO_Tag', 'name'), TRUE
120 );
353ffa53
TO
121 $this->addRule('name', ts('Name already exists in Database.'), 'objectExists', array(
122 'CRM_Core_DAO_Tag',
795492f3 123 $this->_id,
353ffa53 124 ));
6a488035
TO
125
126 $this->add('text', 'description', ts('Description'),
127 CRM_Core_DAO::getAttribute('CRM_Core_DAO_Tag', 'description')
128 );
129
6a488035
TO
130 $isReserved = $this->add('checkbox', 'is_reserved', ts('Reserved?'));
131
d3cbd0a5
CW
132 if (!$isTagSetChild) {
133 $this->addSelect('used_for', array('multiple' => TRUE, 'option_url' => NULL));
134 }
6a488035
TO
135
136 $adminTagset = TRUE;
137 if (!CRM_Core_Permission::check('administer Tagsets')) {
138 $adminTagset = FALSE;
139 }
140 $this->assign('adminTagset', $adminTagset);
141
142 $adminReservedTags = TRUE;
143 if (!CRM_Core_Permission::check('administer reserved tags')) {
144 $isReserved->freeze();
145 $adminReservedTags = FALSE;
146 }
147 $this->assign('adminReservedTags', $adminReservedTags);
6a488035 148 }
bd525987 149 $this->setPageTitle($this->_isTagSet ? ts('Tag Set') : ts('Tag'));
a1f552a9 150 parent::buildQuickForm();
6a488035
TO
151 }
152
f2ac86d1 153 /**
154 * Set default values for the form.
155 *
156 * @return array
157 */
1192bd09
CW
158 public function setDefaultValues() {
159 $defaults = parent::setDefaultValues();
d3cbd0a5
CW
160 $cloneFrom = CRM_Utils_Request::retrieve('clone_from', 'Integer');
161 if (empty($this->_id) && $cloneFrom) {
162 $params = array('id' => $cloneFrom);
163 CRM_Core_BAO_Tag::retrieve($params, $this->_values);
164 $this->_values['name'] .= ' (' . ts('copy') . ')';
165 $defaults = $this->_values;
166 }
167 if (empty($defaults['color'])) {
d73974ac
CW
168 $defaults['color'] = '#ffffff';
169 }
d3cbd0a5 170 if (empty($this->_id) && empty($defaults['used_for'])) {
bd525987
CW
171 $defaults['used_for'] = 'civicrm_contact';
172 }
1192bd09
CW
173 return $defaults;
174 }
175
6a488035 176 /**
eceb18cc 177 * Process the form submission.
6a488035
TO
178 */
179 public function postProcess() {
d3cbd0a5
CW
180 if ($this->_action == CRM_Core_Action::DELETE) {
181 $deleted = 0;
182 $tag = civicrm_api3('tag', 'getsingle', array('id' => $this->_id[0]));
183 foreach ($this->_id as $id) {
184 if (CRM_Core_BAO_Tag::del($id)) {
185 $deleted++;
186 }
187 }
188 if (count($this->_id) == 1 && $deleted == 1) {
189 if ($tag['is_tagset']) {
190 CRM_Core_Session::setStatus(ts("The tag set '%1' has been deleted.", array(1 => $tag['name'])), ts('Deleted'), 'success');
191 }
192 else {
193 CRM_Core_Session::setStatus(ts("The tag '%1' has been deleted.", array(1 => $tag['name'])), ts('Deleted'), 'success');
194 }
195 }
196 else {
197 CRM_Core_Session::setStatus(ts("Deleted %1 tags.", array(1 => $deleted)), ts('Deleted'), 'success');
198 }
6a488035 199 }
d3cbd0a5
CW
200 else {
201 $params = $this->exportValues();
202 if ($this->_id) {
203 $params['id'] = $this->_id;
204 }
6a488035 205
d3cbd0a5
CW
206 if (isset($params['used_for']) && ($this->_action == CRM_Core_Action::ADD || $this->_action == CRM_Core_Action::UPDATE)) {
207 $params['used_for'] = implode(",", $params['used_for']);
208 }
6a488035 209
d3cbd0a5
CW
210 $params['is_tagset'] = 0;
211 if ($this->_isTagSet) {
212 $params['is_tagset'] = 1;
213 }
6a488035 214
d3cbd0a5
CW
215 if (!isset($params['is_reserved'])) {
216 $params['is_reserved'] = 0;
217 }
f37c6bf1 218
d3cbd0a5
CW
219 if (!isset($params['parent_id']) && $this->get('parent_id')) {
220 $params['parent_id'] = $this->get('parent_id');
221 }
222 if (empty($params['parent_id'])) {
223 $params['parent_id'] = '';
224 }
d73974ac 225
d3cbd0a5
CW
226 if (!isset($params['is_selectable'])) {
227 $params['is_selectable'] = 0;
6a488035 228 }
bd525987
CW
229 $tag = CRM_Core_BAO_Tag::add($params);
230 CRM_Core_Session::setStatus(ts("The tag '%1' has been saved.", array(1 => $tag->name)), ts('Saved'), 'success');
d3cbd0a5 231 $this->ajaxResponse['tag'] = $tag->toArray();
6a488035 232 }
d3cbd0a5 233 CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url('civicrm/tag'));
6a488035 234 }
e2046b33 235
6a488035 236}