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