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