Merge pull request #9554 from colemanw/CRM-19769
[civicrm-core.git] / CRM / Tag / Form / Edit.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2016
32 */
33
34 /**
35 * This class generates form components for Tag.
36 */
37 class CRM_Tag_Form_Edit extends CRM_Admin_Form {
38 protected $_isTagSet;
39
40 /**
41 * Explicitly declare the entity api name.
42 */
43 public function getDefaultEntity() {
44 return 'Tag';
45 }
46
47 /**
48 * Build the form object.
49 */
50 public function buildQuickForm() {
51 if ($this->_action == CRM_Core_Action::DELETE) {
52 if ($this->_id && $tag = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Tag', $this->_id, 'name', 'parent_id')) {
53 $url = CRM_Utils_System::url('civicrm/tag', "reset=1");
54 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);
55 }
56 if ($this->_values['is_reserved'] == 1 && !CRM_Core_Permission::check('administer reserved tags')) {
57 CRM_Core_Error::statusBounce(ts("You do not have sufficient permission to delete this reserved tag."));
58 }
59 }
60 else {
61 $parentId = NULL;
62 $isTagsetChild = FALSE;
63
64 $this->_isTagSet = CRM_Utils_Request::retrieve('tagset', 'Positive', $this);
65
66 if (!$this->_isTagSet &&
67 $this->_id &&
68 CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Tag', $this->_id, 'is_tagset')
69 ) {
70 $this->_isTagSet = TRUE;
71 }
72
73 if ($this->_id) {
74 $parentId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Tag', $this->_id, 'parent_id');
75 $isTagSetChild = $parentId ? CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Tag', $parentId, 'is_tagset') : FALSE;
76 }
77
78 if (!$this->_isTagSet) {
79 if (!$isTagSetChild) {
80 $colorTags = CRM_Core_BAO_Tag::getColorTags(NULL, TRUE, $this->_id);
81 $this->add('select2', 'parent_id', ts('Parent Tag'), $colorTags, FALSE, array('placeholder' => ts('- select -')));
82 }
83
84 // Tagsets are not selectable by definition so only include the selectable field if NOT a tagset.
85 $selectable = $this->add('checkbox', 'is_selectable', ts('Selectable?'));
86 // Selectable should be checked by default when creating a new tag
87 if ($this->_action == CRM_Core_Action::ADD) {
88 $selectable->setValue(1);
89 }
90
91 $this->add('color', 'color', ts('Color'));
92 }
93
94 $this->assign('isTagSet', $this->_isTagSet);
95
96 $this->applyFilter('__ALL__', 'trim');
97
98 $this->add('text', 'name', ts('Name'),
99 CRM_Core_DAO::getAttribute('CRM_Core_DAO_Tag', 'name'), TRUE
100 );
101 $this->addRule('name', ts('Name already exists in Database.'), 'objectExists', array(
102 'CRM_Core_DAO_Tag',
103 $this->_id,
104 ));
105
106 $this->add('text', 'description', ts('Description'),
107 CRM_Core_DAO::getAttribute('CRM_Core_DAO_Tag', 'description')
108 );
109
110 $isReserved = $this->add('checkbox', 'is_reserved', ts('Reserved?'));
111
112 $this->addSelect('used_for', array('multiple' => TRUE, 'option_url' => NULL));
113
114 $adminTagset = TRUE;
115 if (!CRM_Core_Permission::check('administer Tagsets')) {
116 $adminTagset = FALSE;
117 }
118 $this->assign('adminTagset', $adminTagset);
119
120 $adminReservedTags = TRUE;
121 if (!CRM_Core_Permission::check('administer reserved tags')) {
122 $isReserved->freeze();
123 $adminReservedTags = FALSE;
124 }
125 $this->assign('adminReservedTags', $adminReservedTags);
126 }
127 $this->setPageTitle($this->_isTagSet ? ts('Tag Set') : ts('Tag'));
128 parent::buildQuickForm();
129 }
130
131 public function setDefaultValues() {
132 $defaults = parent::setDefaultValues();
133 if (empty($this->_id) || !CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Tag', $this->_id, 'color')) {
134 $defaults['color'] = '#ffffff';
135 }
136 if (empty($this->_id)) {
137 $defaults['used_for'] = 'civicrm_contact';
138 }
139 return $defaults;
140 }
141
142 /**
143 * Process the form submission.
144 */
145 public function postProcess() {
146 // store the submitted values in an array
147 $params = $this->exportValues();
148 if ($this->_id) {
149 $params['id'] = $this->_id;
150 }
151
152 if ($this->_action == CRM_Core_Action::ADD ||
153 $this->_action == CRM_Core_Action::UPDATE
154 ) {
155 $params['used_for'] = implode(",", $params['used_for']);
156 }
157
158 $params['is_tagset'] = 0;
159 if ($this->_isTagSet) {
160 $params['is_tagset'] = 1;
161 }
162
163 if (!isset($params['is_reserved'])) {
164 $params['is_reserved'] = 0;
165 }
166
167 if (!isset($params['is_selectable'])) {
168 $params['is_selectable'] = 0;
169 }
170
171 if (strtolower($params['color']) == '#ffffff') {
172 $params['color'] = 'null';
173 }
174
175 if ($this->_action == CRM_Core_Action::DELETE) {
176 if ($this->_id > 0) {
177 $tag = civicrm_api3('tag', 'getsingle', array('id' => $this->_id));
178 CRM_Core_BAO_Tag::del($this->_id);
179 CRM_Core_Session::setStatus(ts("The tag '%1' has been deleted.", array(1 => $tag['name'])), ts('Deleted'), 'success');
180 }
181 }
182 else {
183 $tag = CRM_Core_BAO_Tag::add($params);
184 CRM_Core_Session::setStatus(ts("The tag '%1' has been saved.", array(1 => $tag->name)), ts('Saved'), 'success');
185 }
186 }
187
188 }