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