Merge pull request #4822 from kurund/indentation-fixes
[civicrm-core.git] / CRM / Report / Page / InstanceList.php
CommitLineData
6a488035 1<?php
6a488035
TO
2
3/*
4 +--------------------------------------------------------------------+
39de6fd5 5 | CiviCRM version 4.6 |
6a488035 6 +--------------------------------------------------------------------+
06b69b18 7 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27*/
28
29/**
30 *
31 * @package CRM
06b69b18 32 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
33 * $Id$
34 *
35 */
36
37/**
38 * Page for invoking report instances
39 */
40class CRM_Report_Page_InstanceList extends CRM_Core_Page {
41
42 static $_links = NULL;
43
44 static $_exceptions = array( 'logging/contact/detail' );
2f4c2f5d 45
6a488035
TO
46 /**
47 * Name of component if report list is filtered
48 *
49 * @var string
50 **/
51 protected $_compName = NULL;
52
53 /**
54 * ID of component if report list is filtered
55 *
56 * @var int
57 **/
58 protected $_compID = NULL;
59
2066e389 60 /**
61 * ID of grouping if report list is filtered
62 *
63 * @var int
64 **/
37c20553 65 protected $_grouping = NULL;
2066e389 66
6a488035
TO
67 /**
68 * ID of parent report template if list is filtered by template
69 *
70 * @var int
71 **/
72 protected $_ovID = NULL;
73
74
75 /**
76 * Title of parent report template if list is filtered by template
77 *
78 * @var string
79 **/
80 protected $_title = NULL;
81
82 /**
83 * Retrieves report instances, optionally filtered by parent report template ($ovID)
84 * or by component ($compID)
85 *
86 * @return array $rows
6a488035
TO
87 * @static
88 */
89 public function &info() {
90
91 $report = '';
92 if ($this->ovID) {
93 $report .= " AND v.id = {$this->ovID} ";
94 }
2f4c2f5d 95
6a488035
TO
96 if ($this->compID) {
97 if ($this->compID == 99) {
98 $report .= " AND v.component_id IS NULL ";
99 $this->_compName = 'Contact';
37c20553 100 }
101 else {
2f4c2f5d 102 $report .= " AND v.component_id = {$this->compID} ";
6a488035
TO
103 $cmpName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Component', $this->compID,
104 'name', 'id'
105 );
106 $this->_compName = substr($cmpName, 4);
107 if ($this->_compName == 'Contribute') {
108 $this->_compName = 'Contribution';
109 }
110 }
111 }
37c20553 112 elseif ($this->grouping) {
113 $report .= " AND v.grouping = '{$this->grouping}' ";
114 }
6a488035
TO
115
116 $sql = "
37c20553 117 SELECT inst.id, inst.title, inst.report_id, inst.description, v.label, v.grouping,
2066e389 118 CASE
119 WHEN comp.name IS NOT NULL THEN SUBSTRING(comp.name, 5)
120 WHEN v.grouping IS NOT NULL THEN v.grouping
121 ELSE 'Contact'
122 END as compName
6a488035
TO
123 FROM civicrm_option_group g
124 LEFT JOIN civicrm_option_value v
125 ON v.option_group_id = g.id AND
126 g.name = 'report_template'
127 LEFT JOIN civicrm_report_instance inst
128 ON v.value = inst.report_id
2f4c2f5d 129 LEFT JOIN civicrm_component comp
6a488035 130 ON v.component_id = comp.id
2f4c2f5d 131
6a488035
TO
132 WHERE v.is_active = 1 {$report}
133 AND inst.domain_id = %1
134 ORDER BY v.weight";
135
136 $dao = CRM_Core_DAO::executeQuery($sql, array(
37c20553 137 1 => array(CRM_Core_Config::domainID(), 'Integer'),
138 ));
6a488035
TO
139
140 $config = CRM_Core_Config::singleton();
37c20553 141 $rows = array();
142 $url = 'civicrm/report/instance';
6a488035
TO
143 while ($dao->fetch()) {
144 if (in_array($dao->report_id, self::$_exceptions)) {
145 continue;
146 }
147
148 $enabled = in_array("Civi{$dao->compName}", $config->enableComponents);
37c20553 149 if ($dao->compName == 'Contact' || $dao->compName == $dao->grouping) {
6a488035
TO
150 $enabled = TRUE;
151 }
152 //filter report listings by permissions
153 if (!($enabled && CRM_Report_Utils_Report::isInstancePermissioned($dao->id))) {
154 continue;
155 }
156 //filter report listing by group/role
157 if (!($enabled && CRM_Report_Utils_Report::isInstanceGroupRoleAllowed($dao->id))) {
158 continue;
159 }
160
161 if (trim($dao->title)) {
162 if ($this->ovID) {
163 $this->title = ts("Report(s) created from the template: %1", array(1 => $dao->label));
164 }
165 $rows[$dao->compName][$dao->id]['title'] = $dao->title;
166 $rows[$dao->compName][$dao->id]['label'] = $dao->label;
167 $rows[$dao->compName][$dao->id]['description'] = $dao->description;
168 $rows[$dao->compName][$dao->id]['url'] = CRM_Utils_System::url("{$url}/{$dao->id}", "reset=1");
169 if (CRM_Core_Permission::check('administer Reports')) {
170 $rows[$dao->compName][$dao->id]['deleteUrl'] = CRM_Utils_System::url("{$url}/{$dao->id}", 'action=delete&reset=1');
171 }
172 }
173 }
6a488035
TO
174 return $rows;
175 }
176
177 /**
100fef9d 178 * Run this page (figure out the action needed and perform it).
6a488035
TO
179 *
180 * @return void
181 */
00be9182 182 public function run() {
6a488035
TO
183 //Filters by source report template or by component
184 $this->ovID = CRM_Utils_Request::retrieve('ovid', 'Positive', $this);
185 $this->compID = CRM_Utils_Request::retrieve('compid', 'Positive', $this);
37c20553 186 $this->grouping = CRM_Utils_Request::retrieve('grp', 'String', $this);
187
6a488035
TO
188 $rows = $this->info();
189
190 $this->assign('list', $rows);
191 if ($this->ovID OR $this->compID) {
192 // link to view all reports
193 $reportUrl = CRM_Utils_System::url('civicrm/report/list', "reset=1");
194 $this->assign('reportUrl', $reportUrl);
195 if ($this->ovID) {
196 $this->assign('title', $this->title);
0c34116c
DG
197 } else {
198 CRM_Utils_System::setTitle(ts('%1 Reports', array(1 => $this->_compName)));
6a488035
TO
199 }
200 }
201 // assign link to template list for users with appropriate permissions
202 if (CRM_Core_Permission::check('administer Reports')) {
203 if ($this->compID) {
204 $newButton = ts('New %1 Report', array(1 => $this->_compName));
205 $templateUrl = CRM_Utils_System::url('civicrm/report/template/list', "reset=1&compid={$this->compID}");
206 } else {
207 $newButton = ts('New Report');
208 $templateUrl = CRM_Utils_System::url('civicrm/report/template/list', "reset=1");
209 }
210 $this->assign('newButton', $newButton);
211 $this->assign('templateUrl', $templateUrl);
212 $this->assign('compName', $this->_compName);
213 }
214 return parent::run();
215 }
216}