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