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