Merge pull request #9554 from colemanw/CRM-19769
[civicrm-core.git] / CRM / Tag / Page / Tag.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
fa938177 6 | Copyright CiviCRM LLC (c) 2004-2016 |
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
fa938177 31 * @copyright CiviCRM LLC (c) 2004-2016
6a488035
TO
32 */
33
34/**
ce064e4f 35 * Page for displaying list of categories.
6a488035 36 */
eaaaef83 37class CRM_Tag_Page_Tag extends CRM_Core_Page_Basic {
6a488035 38
96f50de2
CW
39 public $useLivePageJS = TRUE;
40
6a488035 41 /**
eceb18cc 42 * The action links that we need to display for the browse screen.
6a488035
TO
43 *
44 * @var array
6a488035
TO
45 */
46 static $_links = NULL;
47
48 /**
eceb18cc 49 * Get BAO.
6a488035 50 *
a6c01b45
CW
51 * @return string
52 * Classname of BAO.
6a488035 53 */
00be9182 54 public function getBAOName() {
6a488035
TO
55 return 'CRM_Core_BAO_Tag';
56 }
57
58 /**
eceb18cc 59 * Get action Links.
6a488035 60 *
a6c01b45
CW
61 * @return array
62 * (reference) of action links
6a488035 63 */
00be9182 64 public function &links() {
6a488035
TO
65 if (!(self::$_links)) {
66 self::$_links = array(
67 CRM_Core_Action::UPDATE => array(
68 'name' => ts('Edit'),
eaaaef83 69 'url' => 'civicrm/tag',
6a488035
TO
70 'qs' => 'action=update&id=%%id%%&reset=1',
71 'title' => ts('Edit Tag'),
72 ),
73 CRM_Core_Action::DELETE => array(
74 'name' => ts('Delete'),
eaaaef83 75 'url' => 'civicrm/tag',
6a488035
TO
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',
6a488035
TO
82 'title' => ts('Merge Tag'),
83 ),
84 );
85 }
86 return self::$_links;
87 }
88
89 /**
eceb18cc 90 * Get name of edit form.
6a488035 91 *
a6c01b45
CW
92 * @return string
93 * Classname of edit form.
6a488035 94 */
00be9182 95 public function editForm() {
eaaaef83 96 return 'CRM_Tag_Form_Edit';
6a488035
TO
97 }
98
99 /**
eceb18cc 100 * Get form name for edit form.
6a488035 101 *
a6c01b45
CW
102 * @return string
103 * name of this page.
6a488035 104 */
00be9182 105 public function editName() {
6a488035
TO
106 return 'Tag';
107 }
108
109 /**
eceb18cc 110 * Get form name for delete form.
6a488035 111 *
a6c01b45
CW
112 * @return string
113 * name of this page.
6a488035 114 */
00be9182 115 public function deleteName() {
6a488035
TO
116 return 'Tag';
117 }
118
119 /**
120 * Get user context.
121 *
fd31fa4c
EM
122 * @param null $mode
123 *
a6c01b45
CW
124 * @return string
125 * user context.
6a488035 126 */
00be9182 127 public function userContext($mode = NULL) {
eaaaef83 128 return 'civicrm/tag';
6a488035
TO
129 }
130
131 /**
eceb18cc 132 * Get name of delete form.
6a488035 133 *
a6c01b45
CW
134 * @return string
135 * Classname of delete form.
6a488035 136 */
00be9182 137 public function deleteForm() {
eaaaef83 138 return 'CRM_Tag_Form_Edit';
6a488035
TO
139 }
140
141 /**
100fef9d 142 * Override function browse()
02fc859b 143 *
100fef9d
CW
144 * @param null $action
145 * @param null $sort
6a488035 146 */
00be9182 147 public function browse($action = NULL, $sort = NULL) {
6a488035
TO
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
156FROM civicrm_tag t1 LEFT JOIN civicrm_tag t2 ON t1.id = t2.parent_id
157WHERE 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
6bed6964
CW
167 $query = " SELECT t1.name, t1.id, t2.name as parent, t1.description, t1.used_for, t1.is_tagset as is_tagset,
168 t1.is_reserved, t1.parent_id, t1.used_for, t1.color, t2.is_tagset as is_tagset_child, t2.parent_id as grandparent_id
169 FROM civicrm_tag t1
170 LEFT JOIN civicrm_tag t2 ON t1.parent_id = t2.id
171 LEFT JOIN civicrm_tag t3 ON t2.parent_id = t3.id
172 ORDER BY CONCAT(IFNULL(t3.name, ''), IFNULL(t2.name, ''), t1.name)";
6a488035
TO
173
174 $tag = CRM_Core_DAO::executeQuery($query);
175 $values = array();
176
177 $action = CRM_Core_Action::UPDATE + CRM_Core_Action::DELETE;
178 $permission = CRM_Core_Permission::EDIT;
179
180 while ($tag->fetch()) {
181 $values[$tag->id] = (array) $tag;
182
183 $used = array();
184 if ($values[$tag->id]['used_for']) {
185 $usedArray = explode(",", $values[$tag->id]['used_for']);
186 foreach ($usedArray as $key => $value) {
187 $used[$key] = $usedFor[$value];
188 }
189 }
190
6bed6964 191 $values[$tag->id]['used_for'] = implode(", ", $used);
6a488035
TO
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 }
96025800 217
6a488035 218}