CRM-20369 mailing bounce report
[civicrm-core.git] / CRM / Report / Page / InstanceList.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
0f03f337 6 | Copyright CiviCRM LLC (c) 2004-2017 |
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
0f03f337 31 * @copyright CiviCRM LLC (c) 2004-2017
6a488035
TO
32 */
33
34/**
04994ffc 35 * Page for invoking report instances.
6a488035
TO
36 */
37class CRM_Report_Page_InstanceList extends CRM_Core_Page {
38
39 static $_links = NULL;
40
481a74f4 41 static $_exceptions = array('logging/contact/detail');
2f4c2f5d 42
6a488035 43 /**
fe482240 44 * Name of component if report list is filtered.
6a488035
TO
45 *
46 * @var string
ae5ffbb7 47 */
6a488035
TO
48 protected $_compName = NULL;
49
50 /**
fe482240 51 * ID of component if report list is filtered.
6a488035
TO
52 *
53 * @var int
ae5ffbb7 54 */
6a488035
TO
55 protected $_compID = NULL;
56
2066e389 57 /**
fe482240 58 * ID of grouping if report list is filtered.
2066e389 59 *
60 * @var int
ae5ffbb7 61 */
37c20553 62 protected $_grouping = NULL;
2066e389 63
6a488035 64 /**
fe482240 65 * ID of parent report template if list is filtered by template.
6a488035
TO
66 *
67 * @var int
ae5ffbb7 68 */
6a488035
TO
69 protected $_ovID = NULL;
70
6a488035 71 /**
fe482240 72 * Title of parent report template if list is filtered by template.
6a488035
TO
73 *
74 * @var string
ae5ffbb7 75 */
6a488035
TO
76 protected $_title = NULL;
77
78 /**
04994ffc 79 * Retrieves report instances, optionally filtered.
80 *
81 * Filtering available by parent report template ($ovID) or by component ($compID).
6a488035 82 *
a6c01b45 83 * @return array
6a488035 84 */
182f5081 85 public function info() {
6a488035
TO
86
87 $report = '';
88 if ($this->ovID) {
89 $report .= " AND v.id = {$this->ovID} ";
90 }
2f4c2f5d 91
6a488035
TO
92 if ($this->compID) {
93 if ($this->compID == 99) {
94 $report .= " AND v.component_id IS NULL ";
95 $this->_compName = 'Contact';
37c20553 96 }
97 else {
2f4c2f5d 98 $report .= " AND v.component_id = {$this->compID} ";
6a488035
TO
99 $cmpName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Component', $this->compID,
100 'name', 'id'
101 );
102 $this->_compName = substr($cmpName, 4);
103 if ($this->_compName == 'Contribute') {
104 $this->_compName = 'Contribution';
105 }
106 }
107 }
37c20553 108 elseif ($this->grouping) {
109 $report .= " AND v.grouping = '{$this->grouping}' ";
110 }
0f8c6e58 111 elseif ($this->myReports) {
112 $report .= " AND inst.owner_id = " . CRM_Core_Session::getLoggedInContactID();
113 }
6a488035
TO
114
115 $sql = "
0f8c6e58 116 SELECT inst.id, inst.title, inst.report_id, inst.description, inst.owner_id, v.label, v.grouping, v.name as class_name,
2066e389 117 CASE
118 WHEN comp.name IS NOT NULL THEN SUBSTRING(comp.name, 5)
119 WHEN v.grouping IS NOT NULL THEN v.grouping
120 ELSE 'Contact'
121 END as compName
6a488035
TO
122 FROM civicrm_option_group g
123 LEFT JOIN civicrm_option_value v
124 ON v.option_group_id = g.id AND
125 g.name = 'report_template'
126 LEFT JOIN civicrm_report_instance inst
127 ON v.value = inst.report_id
2f4c2f5d 128 LEFT JOIN civicrm_component comp
6a488035 129 ON v.component_id = comp.id
2f4c2f5d 130
6a488035
TO
131 WHERE v.is_active = 1 {$report}
132 AND inst.domain_id = %1
133 ORDER BY v.weight";
134
135 $dao = CRM_Core_DAO::executeQuery($sql, array(
37c20553 136 1 => array(CRM_Core_Config::domainID(), 'Integer'),
137 ));
6a488035
TO
138
139 $config = CRM_Core_Config::singleton();
37c20553 140 $rows = array();
141 $url = 'civicrm/report/instance';
0f8c6e58 142 $my_reports_grouping = 'My';
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 }
0f8c6e58 152
153 // filter report listings for private reports
154 if (!empty($dao->owner_id) && CRM_Core_Session::getLoggedInContactID() != $dao->owner_id) {
155 continue;
156 }
157
6a488035
TO
158 //filter report listings by permissions
159 if (!($enabled && CRM_Report_Utils_Report::isInstancePermissioned($dao->id))) {
160 continue;
161 }
162 //filter report listing by group/role
163 if (!($enabled && CRM_Report_Utils_Report::isInstanceGroupRoleAllowed($dao->id))) {
164 continue;
165 }
166
167 if (trim($dao->title)) {
168 if ($this->ovID) {
169 $this->title = ts("Report(s) created from the template: %1", array(1 => $dao->label));
170 }
0f8c6e58 171
172 $report_grouping = $dao->compName;
173 if ($dao->owner_id != NULL) {
174 $report_grouping = $my_reports_grouping;
175 }
176 $rows[$report_grouping][$dao->id]['title'] = $dao->title;
177 $rows[$report_grouping][$dao->id]['label'] = $dao->label;
178 $rows[$report_grouping][$dao->id]['description'] = $dao->description;
179 $rows[$report_grouping][$dao->id]['url'] = CRM_Utils_System::url("{$url}/{$dao->id}", "reset=1&output=criteria");
180 $rows[$report_grouping][$dao->id]['viewUrl'] = CRM_Utils_System::url("{$url}/{$dao->id}", 'force=1&reset=1');
181 $rows[$report_grouping][$dao->id]['actions'] = $this->getActionLinks($dao->id, $dao->class_name);
6a488035
TO
182 }
183 }
0f8c6e58 184 // Move My Reports to the beginning of the reports list
185 if (isset($rows[$my_reports_grouping])) {
186 $my_reports = $rows[$my_reports_grouping];
187 unset($rows[$my_reports_grouping]);
188 $rows = array($my_reports_grouping => $my_reports) + $rows;
189 }
6a488035
TO
190 return $rows;
191 }
192
193 /**
100fef9d 194 * Run this page (figure out the action needed and perform it).
6a488035 195 */
00be9182 196 public function run() {
6a488035 197 //Filters by source report template or by component
353ffa53 198 $this->ovID = CRM_Utils_Request::retrieve('ovid', 'Positive', $this);
0f8c6e58 199 $this->myReports = CRM_Utils_Request::retrieve('myreports', 'String', $this);
6a488035 200 $this->compID = CRM_Utils_Request::retrieve('compid', 'Positive', $this);
37c20553 201 $this->grouping = CRM_Utils_Request::retrieve('grp', 'String', $this);
202
353ffa53 203 $rows = $this->info();
6a488035
TO
204
205 $this->assign('list', $rows);
206 if ($this->ovID OR $this->compID) {
207 // link to view all reports
208 $reportUrl = CRM_Utils_System::url('civicrm/report/list', "reset=1");
209 $this->assign('reportUrl', $reportUrl);
210 if ($this->ovID) {
211 $this->assign('title', $this->title);
0db6c3e1
TO
212 }
213 else {
0c34116c 214 CRM_Utils_System::setTitle(ts('%1 Reports', array(1 => $this->_compName)));
6a488035
TO
215 }
216 }
217 // assign link to template list for users with appropriate permissions
218 if (CRM_Core_Permission::check('administer Reports')) {
219 if ($this->compID) {
220 $newButton = ts('New %1 Report', array(1 => $this->_compName));
221 $templateUrl = CRM_Utils_System::url('civicrm/report/template/list', "reset=1&compid={$this->compID}");
0db6c3e1
TO
222 }
223 else {
6a488035
TO
224 $newButton = ts('New Report');
225 $templateUrl = CRM_Utils_System::url('civicrm/report/template/list', "reset=1");
226 }
227 $this->assign('newButton', $newButton);
228 $this->assign('templateUrl', $templateUrl);
229 $this->assign('compName', $this->_compName);
0f8c6e58 230 $this->assign('myReports', $this->myReports);
6a488035
TO
231 }
232 return parent::run();
233 }
96025800 234
182f5081 235 /**
236 * Get action links.
237 *
238 * @param int $instanceID
239 * @param string $className
240 *
241 * @return array
242 */
243 protected function getActionLinks($instanceID, $className) {
244 $urlCommon = 'civicrm/report/instance/' . $instanceID;
245 $actions = array(
246 'copy' => array(
247 'url' => CRM_Utils_System::url($urlCommon, 'reset=1&output=copy'),
248 'label' => ts('Save a Copy'),
249 ),
250 'pdf' => array(
251 'url' => CRM_Utils_System::url($urlCommon, 'reset=1&force=1&output=pdf'),
252 'label' => ts('View as pdf'),
253 ),
254 'print' => array(
255 'url' => CRM_Utils_System::url($urlCommon, 'reset=1&force=1&output=print'),
256 'label' => ts('Print report'),
257 ),
258 );
259 // Hackery, Hackera, Hacker ahahahahahaha a super nasty hack.
260 // Almost all report classes support csv & loading each class to call the method seems too
261 // expensive. We also have on our later list 'do they support charts' which is instance specific
262 // e.g use of group by might affect it. So, lets just skip for the few that don't for now.
263 $csvBlackList = array(
264 'CRM_Report_Form_Contact_Detail',
265 'CRM_Report_Form_Event_Income',
266 );
267 if (!in_array($className, $csvBlackList)) {
268 $actions['csv'] = array(
269 'url' => CRM_Utils_System::url($urlCommon, 'reset=1&force=1&output=csv'),
270 'label' => ts('Export to csv'),
271 );
272 }
273 if (CRM_Core_Permission::check('administer Reports')) {
274 $actions['delete'] = array(
275 'url' => CRM_Utils_System::url($urlCommon, 'reset=1&action=delete'),
276 'label' => ts('Delete report'),
277 'confirm_message' => ts('Are you sure you want delete this report? This action cannot be undone.'),
278 );
279 }
280
281 return $actions;
282 }
283
6a488035 284}