Merge remote-tracking branch 'upstream/4.4' into 4.4-master-2014-08-12-10-27-52
[civicrm-core.git] / CRM / Admin / Page / 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 * 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 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 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 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 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 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 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 function deleteForm() {
134 return 'CRM_Admin_Form_Tag';
135 }
136
137 /**
138 * override function browse()
139 */
140 function browse($action = NULL, $sort = NULL) {
141 $adminTagSet = FALSE;
142 if (CRM_Core_Permission::check('administer Tagsets')) {
143 $adminTagSet = TRUE;
144 }
145 $this->assign('adminTagSet', $adminTagSet);
146
147 $reservedClause = !CRM_Core_Permission::check('administer reserved tags') ? "AND t1.is_reserved != 1" : '';
148 $query = "SELECT t1.name, t1.id
149 FROM civicrm_tag t1 LEFT JOIN civicrm_tag t2 ON t1.id = t2.parent_id
150 WHERE t2.id IS NULL {$reservedClause}";
151 $tag = CRM_Core_DAO::executeQuery($query);
152
153 $mergeableTags = array();
154 while ($tag->fetch()) {
155 $mergeableTags[$tag->id] = 1;
156 }
157
158 $usedFor = CRM_Core_OptionGroup::values('tag_used_for');
159
160 $query = "SELECT t1.name, t1.id, t2.name as parent, t1.description, t1.used_for, t1.is_tagset,
161 t1.is_reserved, t1.parent_id, t1.used_for
162 FROM civicrm_tag t1 LEFT JOIN civicrm_tag t2 ON t1.parent_id = t2.id
163 GROUP BY t1.parent_id, t1.id";
164
165 $tag = CRM_Core_DAO::executeQuery($query);
166 $values = array();
167
168 $action = CRM_Core_Action::UPDATE + CRM_Core_Action::DELETE;
169 $permission = CRM_Core_Permission::EDIT;
170
171 while ($tag->fetch()) {
172 $values[$tag->id] = (array) $tag;
173
174 $used = array();
175 if ($values[$tag->id]['used_for']) {
176 $usedArray = explode(",", $values[$tag->id]['used_for']);
177 foreach ($usedArray as $key => $value) {
178 $used[$key] = $usedFor[$value];
179 }
180 }
181
182 if (!empty($used)) {
183 $values[$tag->id]['used_for'] = implode(", ", $used);
184 }
185
186 $newAction = $action;
187 if ($values[$tag->id]['is_reserved']) {
188 $newAction = CRM_Core_Action::UPDATE;
189 }
190
191 if ($values[$tag->id]['is_tagset'] && !CRM_Core_Permission::check('administer Tagsets')) {
192 $newAction = 0;
193 }
194
195 if (array_key_exists($tag->id, $mergeableTags)) {
196 $newAction += CRM_Core_Action::FOLLOWUP;
197 }
198
199 // populate action links
200 if ($newAction) {
201 $this->action($tag, $newAction, $values[$tag->id], self::links(), $permission, TRUE);
202 }
203 else {
204 $values[$tag->id]['action'] = '';
205 }
206 }
207
208 $this->assign('rows', $values);
209 }
210 }
211