Merge pull request #4606 from johanv/CRM-15636-price_set_event_and_contribution
[civicrm-core.git] / CRM / Admin / Page / Tag.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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 * Page for displaying list of categories
38 */
39 class CRM_Admin_Page_Tag extends CRM_Core_Page_Basic {
40
41 public $useLivePageJS = TRUE;
42
43 /**
44 * The action links that we need to display for the browse screen
45 *
46 * @var array
47 * @static
48 */
49 static $_links = NULL;
50
51 /**
52 * Get BAO
53 *
54 * @return string Classname of BAO.
55 */
56 public function getBAOName() {
57 return 'CRM_Core_BAO_Tag';
58 }
59
60 /**
61 * Get action Links
62 *
63 * @return array (reference) of action links
64 */
65 public function &links() {
66 if (!(self::$_links)) {
67 self::$_links = array(
68 CRM_Core_Action::UPDATE => array(
69 'name' => ts('Edit'),
70 'url' => 'civicrm/admin/tag',
71 'qs' => 'action=update&id=%%id%%&reset=1',
72 'title' => ts('Edit Tag'),
73 ),
74 CRM_Core_Action::DELETE => array(
75 'name' => ts('Delete'),
76 'url' => 'civicrm/admin/tag',
77 'qs' => 'action=delete&id=%%id%%',
78 'title' => ts('Delete Tag'),
79 ),
80 CRM_Core_Action::FOLLOWUP => array(
81 'name' => ts('Merge'),
82 'class' => 'merge_tag',
83 'title' => ts('Merge Tag'),
84 ),
85 );
86 }
87 return self::$_links;
88 }
89
90 /**
91 * Get name of edit form
92 *
93 * @return string Classname of edit form.
94 */
95 public function editForm() {
96 return 'CRM_Admin_Form_Tag';
97 }
98
99 /**
100 * Get form name for edit form
101 *
102 * @return string name of this page.
103 */
104 public function editName() {
105 return 'Tag';
106 }
107
108 /**
109 * Get form name for delete form
110 *
111 * @return string name of this page.
112 */
113 public function deleteName() {
114 return 'Tag';
115 }
116
117 /**
118 * Get user context.
119 *
120 * @param null $mode
121 *
122 * @return string user context.
123 */
124 public function userContext($mode = NULL) {
125 return 'civicrm/admin/tag';
126 }
127
128 /**
129 * Get name of delete form
130 *
131 * @return string Classname of delete form.
132 */
133 public function deleteForm() {
134 return 'CRM_Admin_Form_Tag';
135 }
136
137 /**
138 * Override function browse()
139 *
140 * @param null $action
141 * @param null $sort
142 */
143 public function browse($action = NULL, $sort = NULL) {
144 $adminTagSet = FALSE;
145 if (CRM_Core_Permission::check('administer Tagsets')) {
146 $adminTagSet = TRUE;
147 }
148 $this->assign('adminTagSet', $adminTagSet);
149
150 $reservedClause = !CRM_Core_Permission::check('administer reserved tags') ? "AND t1.is_reserved != 1" : '';
151 $query = "SELECT t1.name, t1.id
152 FROM civicrm_tag t1 LEFT JOIN civicrm_tag t2 ON t1.id = t2.parent_id
153 WHERE t2.id IS NULL {$reservedClause}";
154 $tag = CRM_Core_DAO::executeQuery($query);
155
156 $mergeableTags = array();
157 while ($tag->fetch()) {
158 $mergeableTags[$tag->id] = 1;
159 }
160
161 $usedFor = CRM_Core_OptionGroup::values('tag_used_for');
162
163 $query = "SELECT t1.name, t1.id, t2.name as parent, t1.description, t1.used_for, t1.is_tagset,
164 t1.is_reserved, t1.parent_id, t1.used_for
165 FROM civicrm_tag t1 LEFT JOIN civicrm_tag t2 ON t1.parent_id = t2.id
166 GROUP BY t1.parent_id, t1.id";
167
168 $tag = CRM_Core_DAO::executeQuery($query);
169 $values = array();
170
171 $action = CRM_Core_Action::UPDATE + CRM_Core_Action::DELETE;
172 $permission = CRM_Core_Permission::EDIT;
173
174 while ($tag->fetch()) {
175 $values[$tag->id] = (array) $tag;
176
177 $used = array();
178 if ($values[$tag->id]['used_for']) {
179 $usedArray = explode(",", $values[$tag->id]['used_for']);
180 foreach ($usedArray as $key => $value) {
181 $used[$key] = $usedFor[$value];
182 }
183 }
184
185 if (!empty($used)) {
186 $values[$tag->id]['used_for'] = implode(", ", $used);
187 }
188
189 $newAction = $action;
190 if ($values[$tag->id]['is_reserved']) {
191 $newAction = CRM_Core_Action::UPDATE;
192 }
193
194 if ($values[$tag->id]['is_tagset'] && !CRM_Core_Permission::check('administer Tagsets')) {
195 $newAction = 0;
196 }
197
198 if (array_key_exists($tag->id, $mergeableTags)) {
199 $newAction += CRM_Core_Action::FOLLOWUP;
200 }
201
202 // populate action links
203 if ($newAction) {
204 $this->action($tag, $newAction, $values[$tag->id], self::links(), $permission, TRUE);
205 }
206 else {
207 $values[$tag->id]['action'] = '';
208 }
209 }
210
211 $this->assign('rows', $values);
212 }
213 }