Merge pull request #4939 from colemanw/CRM-15789
[civicrm-core.git] / CRM / Report / Page / InstanceList.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * Page for invoking report instances
38 */
39 class CRM_Report_Page_InstanceList extends CRM_Core_Page {
40
41 static $_links = NULL;
42
43 static $_exceptions = array('logging/contact/detail');
44
45 /**
46 * Name of component if report list is filtered
47 *
48 * @var string
49 **/
50 protected $_compName = NULL;
51
52 /**
53 * ID of component if report list is filtered
54 *
55 * @var int
56 **/
57 protected $_compID = NULL;
58
59 /**
60 * ID of grouping if report list is filtered
61 *
62 * @var int
63 **/
64 protected $_grouping = NULL;
65
66 /**
67 * ID of parent report template if list is filtered by template
68 *
69 * @var int
70 **/
71 protected $_ovID = NULL;
72
73
74 /**
75 * Title of parent report template if list is filtered by template
76 *
77 * @var string
78 **/
79 protected $_title = NULL;
80
81 /**
82 * Retrieves report instances, optionally filtered by parent report template ($ovID)
83 * or by component ($compID)
84 *
85 * @return array
86 */
87 public function &info() {
88
89 $report = '';
90 if ($this->ovID) {
91 $report .= " AND v.id = {$this->ovID} ";
92 }
93
94 if ($this->compID) {
95 if ($this->compID == 99) {
96 $report .= " AND v.component_id IS NULL ";
97 $this->_compName = 'Contact';
98 }
99 else {
100 $report .= " AND v.component_id = {$this->compID} ";
101 $cmpName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Component', $this->compID,
102 'name', 'id'
103 );
104 $this->_compName = substr($cmpName, 4);
105 if ($this->_compName == 'Contribute') {
106 $this->_compName = 'Contribution';
107 }
108 }
109 }
110 elseif ($this->grouping) {
111 $report .= " AND v.grouping = '{$this->grouping}' ";
112 }
113
114 $sql = "
115 SELECT inst.id, inst.title, inst.report_id, inst.description, v.label, v.grouping,
116 CASE
117 WHEN comp.name IS NOT NULL THEN SUBSTRING(comp.name, 5)
118 WHEN v.grouping IS NOT NULL THEN v.grouping
119 ELSE 'Contact'
120 END as compName
121 FROM civicrm_option_group g
122 LEFT JOIN civicrm_option_value v
123 ON v.option_group_id = g.id AND
124 g.name = 'report_template'
125 LEFT JOIN civicrm_report_instance inst
126 ON v.value = inst.report_id
127 LEFT JOIN civicrm_component comp
128 ON v.component_id = comp.id
129
130 WHERE v.is_active = 1 {$report}
131 AND inst.domain_id = %1
132 ORDER BY v.weight";
133
134 $dao = CRM_Core_DAO::executeQuery($sql, array(
135 1 => array(CRM_Core_Config::domainID(), 'Integer'),
136 ));
137
138 $config = CRM_Core_Config::singleton();
139 $rows = array();
140 $url = 'civicrm/report/instance';
141 while ($dao->fetch()) {
142 if (in_array($dao->report_id, self::$_exceptions)) {
143 continue;
144 }
145
146 $enabled = in_array("Civi{$dao->compName}", $config->enableComponents);
147 if ($dao->compName == 'Contact' || $dao->compName == $dao->grouping) {
148 $enabled = TRUE;
149 }
150 //filter report listings by permissions
151 if (!($enabled && CRM_Report_Utils_Report::isInstancePermissioned($dao->id))) {
152 continue;
153 }
154 //filter report listing by group/role
155 if (!($enabled && CRM_Report_Utils_Report::isInstanceGroupRoleAllowed($dao->id))) {
156 continue;
157 }
158
159 if (trim($dao->title)) {
160 if ($this->ovID) {
161 $this->title = ts("Report(s) created from the template: %1", array(1 => $dao->label));
162 }
163 $rows[$dao->compName][$dao->id]['title'] = $dao->title;
164 $rows[$dao->compName][$dao->id]['label'] = $dao->label;
165 $rows[$dao->compName][$dao->id]['description'] = $dao->description;
166 $rows[$dao->compName][$dao->id]['url'] = CRM_Utils_System::url("{$url}/{$dao->id}", "reset=1");
167 if (CRM_Core_Permission::check('administer Reports')) {
168 $rows[$dao->compName][$dao->id]['deleteUrl'] = CRM_Utils_System::url("{$url}/{$dao->id}", 'action=delete&reset=1');
169 }
170 }
171 }
172 return $rows;
173 }
174
175 /**
176 * Run this page (figure out the action needed and perform it).
177 *
178 * @return void
179 */
180 public function run() {
181 //Filters by source report template or by component
182 $this->ovID = CRM_Utils_Request::retrieve('ovid', 'Positive', $this);
183 $this->compID = CRM_Utils_Request::retrieve('compid', 'Positive', $this);
184 $this->grouping = CRM_Utils_Request::retrieve('grp', 'String', $this);
185
186 $rows = $this->info();
187
188 $this->assign('list', $rows);
189 if ($this->ovID OR $this->compID) {
190 // link to view all reports
191 $reportUrl = CRM_Utils_System::url('civicrm/report/list', "reset=1");
192 $this->assign('reportUrl', $reportUrl);
193 if ($this->ovID) {
194 $this->assign('title', $this->title);
195 }
196 else {
197 CRM_Utils_System::setTitle(ts('%1 Reports', array(1 => $this->_compName)));
198 }
199 }
200 // assign link to template list for users with appropriate permissions
201 if (CRM_Core_Permission::check('administer Reports')) {
202 if ($this->compID) {
203 $newButton = ts('New %1 Report', array(1 => $this->_compName));
204 $templateUrl = CRM_Utils_System::url('civicrm/report/template/list', "reset=1&compid={$this->compID}");
205 }
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 }