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