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