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