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