CRM-19769 - Add color field to tag edit form
[civicrm-core.git] / CRM / Tag / Form / Edit.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
fa938177 6 | Copyright CiviCRM LLC (c) 2004-2016 |
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
fa938177 31 * @copyright CiviCRM LLC (c) 2004-2016
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
TO
46
47 /**
eceb18cc 48 * Build the form object.
6a488035
TO
49 */
50 public function buildQuickForm() {
e2046b33
CW
51 $this->setPageTitle($this->_isTagSet ? ts('Tag Set') : ts('Tag'));
52
6a488035
TO
53 if ($this->_action == CRM_Core_Action::DELETE) {
54 if ($this->_id && $tag = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Tag', $this->_id, 'name', 'parent_id')) {
eaaaef83 55 $url = CRM_Utils_System::url('civicrm/tag', "reset=1");
e2046b33 56 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);
6a488035 57 }
bf4b6f8f
JP
58 if ($this->_values['is_reserved'] == 1 && !CRM_Core_Permission::check('administer reserved tags')) {
59 CRM_Core_Error::statusBounce(ts("You do not have sufficient permission to delete this reserved tag."));
60 }
6a488035
TO
61 }
62 else {
63 $this->_isTagSet = CRM_Utils_Request::retrieve('tagset', 'Positive', $this);
64
65 if (!$this->_isTagSet &&
66 $this->_id &&
67 CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Tag', $this->_id, 'is_tagset')
68 ) {
69 $this->_isTagSet = TRUE;
70 }
71
e2046b33 72 $allTag = array('' => ts('- select -')) + CRM_Core_BAO_Tag::getTagsNotInTagset();
6a488035
TO
73
74 if ($this->_id) {
75 unset($allTag[$this->_id]);
76 }
77
78 if (!$this->_isTagSet) {
4c863787 79 $this->add('select', 'parent_id', ts('Parent Tag'), $allTag, FALSE, array('class' => 'crm-select2'));
69582599
DG
80
81 // Tagsets are not selectable by definition so only include the selectable field if NOT a tagset.
82 $selectable = $this->add('checkbox', 'is_selectable', ts('Selectable?'));
83 // Selectable should be checked by default when creating a new tag
84 if ($this->_action == CRM_Core_Action::ADD) {
02fc859b 85 $selectable->setValue(1);
69582599
DG
86 }
87
1192bd09 88 $this->add('color', 'color', ts('Color'));
6a488035
TO
89 }
90
91 $this->assign('isTagSet', $this->_isTagSet);
92
93 $this->applyFilter('__ALL__', 'trim');
94
95 $this->add('text', 'name', ts('Name'),
96 CRM_Core_DAO::getAttribute('CRM_Core_DAO_Tag', 'name'), TRUE
97 );
353ffa53
TO
98 $this->addRule('name', ts('Name already exists in Database.'), 'objectExists', array(
99 'CRM_Core_DAO_Tag',
795492f3 100 $this->_id,
353ffa53 101 ));
6a488035
TO
102
103 $this->add('text', 'description', ts('Description'),
104 CRM_Core_DAO::getAttribute('CRM_Core_DAO_Tag', 'description')
105 );
106
6a488035
TO
107 $isReserved = $this->add('checkbox', 'is_reserved', ts('Reserved?'));
108
4c863787 109 $usedFor = $this->addSelect('used_for', array('multiple' => TRUE, 'option_url' => NULL));
6a488035
TO
110
111 if ($this->_id &&
112 CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Tag', $this->_id, 'parent_id')
113 ) {
114 $usedFor->freeze();
115 }
116
117 $adminTagset = TRUE;
118 if (!CRM_Core_Permission::check('administer Tagsets')) {
119 $adminTagset = FALSE;
120 }
121 $this->assign('adminTagset', $adminTagset);
122
123 $adminReservedTags = TRUE;
124 if (!CRM_Core_Permission::check('administer reserved tags')) {
125 $isReserved->freeze();
126 $adminReservedTags = FALSE;
127 }
128 $this->assign('adminReservedTags', $adminReservedTags);
129
6a488035 130 }
a1f552a9 131 parent::buildQuickForm();
6a488035
TO
132 }
133
1192bd09
CW
134 public function setDefaultValues() {
135 $defaults = parent::setDefaultValues();
136 $defaults['color'] = '#ffffff';
137 return $defaults;
138 }
139
6a488035 140 /**
eceb18cc 141 * Process the form submission.
6a488035
TO
142 */
143 public function postProcess() {
144 $params = $ids = array();
145
146 // store the submitted values in an array
147 $params = $this->exportValues();
148
149 $ids['tag'] = $this->_id;
150 if ($this->_action == CRM_Core_Action::ADD ||
151 $this->_action == CRM_Core_Action::UPDATE
152 ) {
153 $params['used_for'] = implode(",", $params['used_for']);
154 }
155
156 $params['is_tagset'] = 0;
157 if ($this->_isTagSet) {
158 $params['is_tagset'] = 1;
159 }
160
161 if (!isset($params['is_reserved'])) {
162 $params['is_reserved'] = 0;
163 }
164
f37c6bf1
T
165 if (!isset($params['is_selectable'])) {
166 $params['is_selectable'] = 0;
167 }
168
6a488035
TO
169 if ($this->_action == CRM_Core_Action::DELETE) {
170 if ($this->_id > 0) {
e51af626 171 $tag = civicrm_api3('tag', 'getsingle', array('id' => $this->_id));
6a488035 172 CRM_Core_BAO_Tag::del($this->_id);
72984a52 173 CRM_Core_Session::setStatus(ts('The tag \'%1\' has been deleted.', array(1 => $tag['name'])), ts('Deleted'), 'success');
6a488035
TO
174 }
175 }
176 else {
177 $tag = CRM_Core_BAO_Tag::add($params, $ids);
178 CRM_Core_Session::setStatus(ts('The tag \'%1\' has been saved.', array(1 => $tag->name)), ts('Saved'), 'success');
179 }
180 }
e2046b33 181
6a488035 182}