Merge pull request #2533 from yashodha/4.4
[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 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 } else {
102 $report .= " AND v.component_id = {$this->compID} ";
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 }
112 elseif ($this->grouping) {
113 $report .= " AND v.grouping = '{$this->grouping}' ";
114 }
115
116 $sql = "
117 SELECT inst.id, inst.title, inst.report_id, inst.description, v.label, v.grouping,
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
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
129 LEFT JOIN civicrm_component comp
130 ON v.component_id = comp.id
131
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(
137 1 => array(CRM_Core_Config::domainID(), 'Integer'),
138 ));
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
176 return $rows;
177 }
178
179 /**
180 * run this page (figure out the action needed and perform it).
181 *
182 * @return void
183 */
184 function run() {
185 //Filters by source report template or by component
186 $this->ovID = CRM_Utils_Request::retrieve('ovid', 'Positive', $this);
187 $this->compID = CRM_Utils_Request::retrieve('compid', 'Positive', $this);
188 $this->grouping = CRM_Utils_Request::retrieve('grp', 'String', $this);
189
190 $rows = $this->info();
191
192 $this->assign('list', $rows);
193 if ($this->ovID OR $this->compID) {
194 // link to view all reports
195 $reportUrl = CRM_Utils_System::url('civicrm/report/list', "reset=1");
196 $this->assign('reportUrl', $reportUrl);
197 if ($this->ovID) {
198 $this->assign('title', $this->title);
199 } else {
200 CRM_Utils_System::setTitle(ts('%1 Reports', array(1 => $this->_compName)));
201 }
202 }
203 // assign link to template list for users with appropriate permissions
204 if (CRM_Core_Permission::check('administer Reports')) {
205 if ($this->compID) {
206 $newButton = ts('New %1 Report', array(1 => $this->_compName));
207 $templateUrl = CRM_Utils_System::url('civicrm/report/template/list', "reset=1&compid={$this->compID}");
208 } else {
209 $newButton = ts('New Report');
210 $templateUrl = CRM_Utils_System::url('civicrm/report/template/list', "reset=1");
211 }
212 $this->assign('newButton', $newButton);
213 $this->assign('templateUrl', $templateUrl);
214 $this->assign('compName', $this->_compName);
215 }
216 return parent::run();
217 }
218 }
219