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