fix a few comments
[civicrm-core.git] / CRM / Report / Form / Grant / Detail.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35class CRM_Report_Form_Grant_Detail extends CRM_Report_Form {
36
37 protected $_addressField = FALSE;
38
39 protected $_customGroupExtends = array(
21dfd5f5 40 'Grant',
9d72cede 41 );
6a488035 42
74cf4551 43 /**
74cf4551
EM
44 */
45 /**
74cf4551 46 */
00be9182 47 public function __construct() {
6a488035 48 $this->_columns = array(
9d72cede 49 'civicrm_contact' => array(
6a488035 50 'dao' => 'CRM_Contact_DAO_Contact',
9d72cede
EM
51 'fields' => array(
52 'sort_name' => array(
53 'title' => ts('Contact Name'),
6a488035
TO
54 'required' => TRUE,
55 'no_repeat' => TRUE,
56 ),
9d72cede 57 'id' => array(
6a488035
TO
58 'no_display' => TRUE,
59 'required' => TRUE,
60 ),
61 ),
62 'grouping' => 'contact-fields',
9d72cede
EM
63 'filters' => array(
64 'sort_name' => array(
65 'title' => ts('Contact Name'),
6a488035
TO
66 'operator' => 'like',
67 ),
9d72cede
EM
68 'gender_id' => array(
69 'title' => ts('Gender'),
6a488035 70 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
26cf88b5 71 'options' => CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id'),
6a488035 72 ),
9d72cede
EM
73 'id' => array(
74 'title' => ts('Contact ID'),
6a488035
TO
75 'no_display' => TRUE,
76 ),
77 ),
78 ),
9d72cede 79 'civicrm_address' => array(
6a488035 80 'dao' => 'CRM_Core_DAO_Address',
9d72cede
EM
81 'filters' => array(
82 'country_id' => array(
83 'title' => ts('Country'),
6a488035
TO
84 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
85 'options' => CRM_Core_PseudoConstant::country(),
86 ),
9d72cede
EM
87 'state_province_id' => array(
88 'title' => ts('State/Province'),
6a488035
TO
89 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
90 'options' => CRM_Core_PseudoConstant::stateProvince(),
91 ),
92 ),
93 ),
9d72cede 94 'civicrm_grant' => array(
6a488035 95 'dao' => 'CRM_Grant_DAO_Grant',
9d72cede
EM
96 'fields' => array(
97 'grant_type_id' => array(
6a488035
TO
98 'name' => 'grant_type_id',
99 'title' => ts('Grant Type'),
100 ),
9d72cede 101 'status_id' => array(
6a488035
TO
102 'name' => 'status_id',
103 'title' => ts('Grant Status'),
104 ),
9d72cede 105 'amount_total' => array(
6a488035
TO
106 'name' => 'amount_total',
107 'title' => ts('Amount Requested'),
108 'type' => CRM_Utils_Type::T_MONEY,
109 ),
9d72cede 110 'amount_granted' => array(
6a488035
TO
111 'name' => 'amount_granted',
112 'title' => ts('Amount Granted'),
113 ),
9d72cede 114 'application_received_date' => array(
6a488035
TO
115 'name' => 'application_received_date',
116 'title' => ts('Application Received'),
117 'default' => TRUE,
118 ),
9d72cede 119 'money_transfer_date' => array(
6a488035
TO
120 'name' => 'money_transfer_date',
121 'title' => ts('Money Transfer Date'),
122 'type' => CRM_Utils_Type::T_DATE,
123 ),
9d72cede 124 'grant_due_date' => array(
6a488035
TO
125 'name' => 'grant_due_date',
126 'title' => ts('Grant Report Due'),
127 'type' => CRM_Utils_Type::T_DATE,
128 ),
9d72cede 129 'decision_date' => array(
6a488035
TO
130 'name' => 'decision_date',
131 'title' => ts('Grant Decision Date'),
132 'type' => CRM_Utils_Type::T_DATE,
133 ),
9d72cede 134 'rationale' => array(
6a488035
TO
135 'name' => 'rationale',
136 'title' => ts('Rationale'),
137 ),
9d72cede 138 'grant_report_received' => array(
6a488035
TO
139 'name' => 'grant_report_received',
140 'title' => ts('Grant Report Received'),
141 ),
142 ),
9d72cede
EM
143 'filters' => array(
144 'grant_type' => array(
6a488035
TO
145 'name' => 'grant_type_id',
146 'title' => ts('Grant Type'),
147 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
fb1fd730 148 'options' => CRM_Core_PseudoConstant::get('CRM_Grant_DAO_Grant', 'grant_type_id'),
6a488035 149 ),
9d72cede 150 'status_id' => array(
6a488035
TO
151 'name' => 'status_id',
152 'title' => ts('Grant Status'),
153 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
fb1fd730 154 'options' => CRM_Core_PseudoConstant::get('CRM_Grant_DAO_Grant', 'status_id'),
6a488035 155 ),
9d72cede 156 'amount_granted' => array(
6a488035
TO
157 'title' => ts('Amount Granted'),
158 'operatorType' => CRM_Report_Form::OP_INT,
159 ),
9d72cede 160 'amount_total' => array(
6a488035
TO
161 'title' => ts('Amount Requested'),
162 'operatorType' => CRM_Report_Form::OP_INT,
163 ),
9d72cede 164 'application_received_date' => array(
6a488035
TO
165 'title' => ts('Application Received'),
166 'operatorType' => CRM_Report_Form::OP_DATE,
167 ),
9d72cede 168 'money_transfer_date' => array(
6a488035
TO
169 'title' => ts('Money Transfer Date'),
170 'operatorType' => CRM_Report_Form::OP_DATE,
171 ),
9d72cede 172 'grant_due_date' => array(
6a488035
TO
173 'title' => ts('Grant Report Due'),
174 'operatorType' => CRM_Report_Form::OP_DATE,
175 'type' => CRM_Report_Form::OP_DATE,
176 ),
9d72cede 177 'decision_date' => array(
6a488035
TO
178 'title' => ts('Grant Decision Date'),
179 'operatorType' => CRM_Report_Form::OP_DATE,
180 'type' => CRM_Utils_Type::T_DATE,
181 ),
182 ),
9d72cede
EM
183 'group_bys' => array(
184 'grant_type_id' => array(
6a488035
TO
185 'title' => ts('Grant Type'),
186 ),
9d72cede 187 'status_id' => array(
6a488035
TO
188 'title' => ts('Grant Status'),
189 ),
9d72cede 190 'amount_total' => array(
6a488035
TO
191 'title' => ts('Amount Requested'),
192 ),
9d72cede 193 'amount_granted' => array(
6a488035
TO
194 'title' => ts('Amount Granted'),
195 ),
9d72cede 196 'application_received_date' => array(
6a488035
TO
197 'title' => ts('Application Received Date'),
198 ),
9d72cede 199 'money_transfer_date' => array(
6a488035
TO
200 'title' => ts('Money Transfer Date'),
201 ),
9d72cede 202 'decision_date' => array(
6a488035
TO
203 'title' => ts('Grant Decision Date'),
204 ),
205 ),
206 ),
207 );
208
209 parent::__construct();
210 }
211
00be9182 212 public function select() {
6a488035
TO
213 $select = array();
214
215 $this->_columnHeaders = array();
216 foreach ($this->_columns as $tableName => $table) {
217 if ($tableName == 'civicrm_address') {
218 $this->_addressField = TRUE;
219 }
220 if (array_key_exists('fields', $table)) {
221 foreach ($table['fields'] as $fieldName => $field) {
9d72cede
EM
222 if (!empty($field['required']) ||
223 !empty($this->_params['fields'][$fieldName])
224 ) {
6a488035
TO
225
226 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
227
228 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title'];
229 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
230 }
231 }
232 }
233 }
234
235 $this->_select = "SELECT " . implode(', ', $select) . " ";
236 }
237
00be9182 238 public function from() {
6a488035
TO
239 $this->_from = "
240 FROM civicrm_grant {$this->_aliases['civicrm_grant']}
b2708486 241 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact']}
6a488035
TO
242 ON ({$this->_aliases['civicrm_grant']}.contact_id = {$this->_aliases['civicrm_contact']}.id ) ";
243 if ($this->_addressField) {
244 $this->_from .= "
b2708486
DL
245 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
246 ON {$this->_aliases['civicrm_contact']}.id =
247 {$this->_aliases['civicrm_address']}.contact_id AND
6a488035
TO
248 {$this->_aliases['civicrm_address']}.is_primary = 1\n";
249 }
250 }
251
00be9182 252 public function where() {
6a488035
TO
253 $clauses = array();
254 $this->_where = '';
255 foreach ($this->_columns as $tableName => $table) {
256 if (array_key_exists('filters', $table)) {
257 foreach ($table['filters'] as $fieldName => $field) {
258
259 $clause = NULL;
260 if (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE) {
261 $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params);
9d72cede
EM
262 $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params);
263 $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params);
6a488035
TO
264
265 if ($relative || $from || $to) {
266 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
267 }
268 }
269 else {
270 $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params);
271 if ($op) {
272 $clause = $this->whereClause($field,
273 $op,
274 CRM_Utils_Array::value("{$fieldName}_value", $this->_params),
275 CRM_Utils_Array::value("{$fieldName}_min", $this->_params),
276 CRM_Utils_Array::value("{$fieldName}_max", $this->_params)
277 );
278 }
279 }
280 if (!empty($clause)) {
281 $clauses[] = $clause;
282 $this->_where = "WHERE " . implode(' AND ', $clauses);
283 }
284 }
285 }
286 }
287 }
288
00be9182 289 public function groupBy() {
6a488035 290 $this->_groupBy = "";
a7488080 291 if (!empty($this->_params['group_bys']) &&
6a488035
TO
292 is_array($this->_params['group_bys']) &&
293 !empty($this->_params['group_bys'])
294 ) {
295 foreach ($this->_columns as $tableName => $table) {
296 if (array_key_exists('group_bys', $table)) {
297 foreach ($table['group_bys'] as $fieldName => $field) {
a7488080 298 if (!empty($this->_params['group_bys'][$fieldName])) {
6a488035
TO
299 $this->_groupBy[] = $field['dbAlias'];
300 }
301 }
302 }
303 }
304 }
305 if (!empty($this->_groupBy)) {
9d72cede
EM
306 $this->_groupBy = "ORDER BY " . implode(', ', $this->_groupBy) .
307 ", {$this->_aliases['civicrm_contact']}.sort_name";
6a488035
TO
308 }
309 else {
310 $this->_groupBy = "ORDER BY {$this->_aliases['civicrm_contact']}.sort_name";
311 }
312 }
313
74cf4551
EM
314 /**
315 * @param $rows
316 */
00be9182 317 public function alterDisplay(&$rows) {
6a488035
TO
318 // custom code to alter rows
319 $entryFound = FALSE;
320 foreach ($rows as $rowNum => $row) {
321 // convert display name to links
322 if (array_key_exists('civicrm_contact_sort_name', $row) &&
323 array_key_exists('civicrm_contact_id', $row)
324 ) {
325 $url = CRM_Utils_System::url('civicrm/contact/view',
b2708486 326 'reset=1&cid=' . $row['civicrm_contact_id'],
6a488035
TO
327 $this->_absoluteUrl
328 );
329 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
330 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View contact details for this record.");
331 $entryFound = TRUE;
332 }
333
334 if (array_key_exists('civicrm_grant_grant_type_id', $row)) {
335 if ($value = $row['civicrm_grant_grant_type_id']) {
a8c23526 336 $rows[$rowNum]['civicrm_grant_grant_type_id'] = CRM_Core_PseudoConstant::getLabel('CRM_Grant_DAO_Grant', 'grant_type_id', $value);
6a488035
TO
337 }
338 $entryFound = TRUE;
339 }
6a488035
TO
340 if (array_key_exists('civicrm_grant_status_id', $row)) {
341 if ($value = $row['civicrm_grant_status_id']) {
a8c23526 342 $rows[$rowNum]['civicrm_grant_status_id'] = CRM_Core_PseudoConstant::getLabel('CRM_Grant_DAO_Grant', 'status_id', $value);
6a488035
TO
343 }
344 $entryFound = TRUE;
345 }
346 if (array_key_exists('civicrm_grant_grant_report_received', $row)) {
347 if ($value = $row['civicrm_grant_grant_report_received']) {
348 if ($value == 1) {
349 $value = 'Yes';
350 }
351 else {
352 $value = 'No';
353 }
354 $rows[$rowNum]['civicrm_grant_grant_report_received'] = $value;
355 }
356 $entryFound = TRUE;
357 }
358 if (!$entryFound) {
359 break;
360 }
361 }
362 }
96025800 363
6a488035 364}