Import from SVN (r45945, r596)
[civicrm-core.git] / CRM / Report / Form / Grant / Detail.php
1 <?php
2 // $Id$
3
4 /*
5 +--------------------------------------------------------------------+
6 | CiviCRM version 4.3 |
7 +--------------------------------------------------------------------+
8 | Copyright CiviCRM LLC (c) 2004-2013 |
9 +--------------------------------------------------------------------+
10 | This file is a part of CiviCRM. |
11 | |
12 | CiviCRM is free software; you can copy, modify, and distribute it |
13 | under the terms of the GNU Affero General Public License |
14 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
15 | |
16 | CiviCRM is distributed in the hope that it will be useful, but |
17 | WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
19 | See the GNU Affero General Public License for more details. |
20 | |
21 | You should have received a copy of the GNU Affero General Public |
22 | License and the CiviCRM Licensing Exception along |
23 | with this program; if not, contact CiviCRM LLC |
24 | at info[AT]civicrm[DOT]org. If you have questions about the |
25 | GNU Affero General Public License or the licensing of CiviCRM, |
26 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
27 +--------------------------------------------------------------------+
28 */
29
30 /**
31 *
32 * @package CRM
33 * @copyright CiviCRM LLC (c) 2004-2013
34 * $Id$
35 *
36 */
37 class CRM_Report_Form_Grant_Detail extends CRM_Report_Form {
38
39 protected $_addressField = FALSE;
40
41 protected $_customGroupExtends = array(
42 'Grant');
43
44 function __construct() {
45 $this->_columns = array(
46 'civicrm_contact' =>
47 array(
48 'dao' => 'CRM_Contact_DAO_Contact',
49 'fields' =>
50 array(
51 'sort_name' =>
52 array('title' => ts('Contact Name'),
53 'required' => TRUE,
54 'no_repeat' => TRUE,
55 ),
56 'id' =>
57 array(
58 'no_display' => TRUE,
59 'required' => TRUE,
60 ),
61 ),
62 'grouping' => 'contact-fields',
63 'filters' =>
64 array(
65 'sort_name' =>
66 array('title' => ts('Contact Name'),
67 'operator' => 'like',
68 ),
69 'gender_id' =>
70 array('title' => ts('Gender'),
71 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
72 'options' => CRM_Core_PseudoConstant::gender(),
73 ),
74 'id' =>
75 array('title' => ts('Contact ID'),
76 'no_display' => TRUE,
77 ),
78 ),
79 ),
80 'civicrm_address' =>
81 array(
82 'dao' => 'CRM_Core_DAO_Address',
83 'filters' =>
84 array(
85 'country_id' =>
86 array('title' => ts('Country'),
87 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
88 'options' => CRM_Core_PseudoConstant::country(),
89 ),
90 'state_province_id' =>
91 array('title' => ts('State/Province'),
92 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
93 'options' => CRM_Core_PseudoConstant::stateProvince(),
94 ),
95 ),
96 ),
97 'civicrm_grant' =>
98 array(
99 'dao' => 'CRM_Grant_DAO_Grant',
100 'fields' =>
101 array(
102 'grant_type_id' =>
103 array(
104 'name' => 'grant_type_id',
105 'title' => ts('Grant Type'),
106 ),
107 'grant_program_id' =>
108 array(
109 'name' => 'grant_program_id' ,
110 'title' => ts( 'Grant Program' ),
111 ),
112 'status_id' =>
113 array(
114 'name' => 'status_id',
115 'title' => ts('Grant Status'),
116 ),
117 'amount_total' =>
118 array(
119 'name' => 'amount_total',
120 'title' => ts('Amount Requested'),
121 'type' => CRM_Utils_Type::T_MONEY,
122 ),
123 'amount_granted' =>
124 array(
125 'name' => 'amount_granted',
126 'title' => ts('Amount Granted'),
127 ),
128 'application_received_date' =>
129 array(
130 'name' => 'application_received_date',
131 'title' => ts('Application Received'),
132 'default' => TRUE,
133 ),
134 'money_transfer_date' =>
135 array(
136 'name' => 'money_transfer_date',
137 'title' => ts('Money Transfer Date'),
138 'type' => CRM_Utils_Type::T_DATE,
139 ),
140 'grant_due_date' =>
141 array(
142 'name' => 'grant_due_date',
143 'title' => ts('Grant Report Due'),
144 'type' => CRM_Utils_Type::T_DATE,
145 ),
146 'decision_date' =>
147 array(
148 'name' => 'decision_date',
149 'title' => ts('Grant Decision Date'),
150 'type' => CRM_Utils_Type::T_DATE,
151 ),
152 'rationale' =>
153 array(
154 'name' => 'rationale',
155 'title' => ts('Rationale'),
156 ),
157 'grant_report_received' =>
158 array(
159 'name' => 'grant_report_received',
160 'title' => ts('Grant Report Received'),
161 ),
162 ),
163 'filters' =>
164 array(
165 'grant_type' =>
166 array(
167 'name' => 'grant_type_id',
168 'title' => ts('Grant Type'),
169 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
170 'options' => CRM_Grant_PseudoConstant::grantType(),
171 ),
172 'grant_program' =>
173 array(
174 'name' =>'grant_program_id' ,
175 'title' => ts( 'Grant Program' ),
176 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
177 'options' => CRM_Grant_BAO_GrantProgram::grantPrograms( ),
178 ),
179 'status_id' =>
180 array(
181 'name' => 'status_id',
182 'title' => ts('Grant Status'),
183 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
184 'options' => CRM_Grant_PseudoConstant::grantStatus(),
185 ),
186 'amount_granted' =>
187 array(
188 'title' => ts('Amount Granted'),
189 'operatorType' => CRM_Report_Form::OP_INT,
190 ),
191 'amount_total' =>
192 array(
193 'title' => ts('Amount Requested'),
194 'operatorType' => CRM_Report_Form::OP_INT,
195 ),
196 'application_received_date' =>
197 array(
198 'title' => ts('Application Received'),
199 'operatorType' => CRM_Report_Form::OP_DATE,
200 ),
201 'money_transfer_date' =>
202 array(
203 'title' => ts('Money Transfer Date'),
204 'operatorType' => CRM_Report_Form::OP_DATE,
205 ),
206 'grant_due_date' =>
207 array(
208 'title' => ts('Grant Report Due'),
209 'operatorType' => CRM_Report_Form::OP_DATE,
210 'type' => CRM_Report_Form::OP_DATE,
211 ),
212 'decision_date' =>
213 array(
214 'title' => ts('Grant Decision Date'),
215 'operatorType' => CRM_Report_Form::OP_DATE,
216 'type' => CRM_Utils_Type::T_DATE,
217 ),
218 ),
219 'group_bys' =>
220 array(
221 'grant_type_id' =>
222 array(
223 'title' => ts('Grant Type'),
224 ),
225 'grant_program_id' =>
226 array(
227 'title' => ts( 'Grant Program' )
228 ),
229 'status_id' =>
230 array(
231 'title' => ts('Grant Status'),
232 ),
233 'amount_total' =>
234 array(
235 'title' => ts('Amount Requested'),
236 ),
237 'amount_granted' =>
238 array(
239 'title' => ts('Amount Granted'),
240 ),
241 'application_received_date' =>
242 array(
243 'title' => ts('Application Received Date'),
244 ),
245 'money_transfer_date' =>
246 array(
247 'title' => ts('Money Transfer Date'),
248 ),
249 'decision_date' =>
250 array(
251 'title' => ts('Grant Decision Date'),
252 ),
253 ),
254 ),
255 );
256
257 parent::__construct();
258 }
259
260 function select() {
261 $select = array();
262
263 $this->_columnHeaders = array();
264 foreach ($this->_columns as $tableName => $table) {
265 if ($tableName == 'civicrm_address') {
266 $this->_addressField = TRUE;
267 }
268 if (array_key_exists('fields', $table)) {
269 foreach ($table['fields'] as $fieldName => $field) {
270 if (CRM_Utils_Array::value('required', $field) ||
271 CRM_Utils_Array::value($fieldName, $this->_params['fields'])
272 ) {
273
274 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
275
276 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title'];
277 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
278 }
279 }
280 }
281 }
282
283 $this->_select = "SELECT " . implode(', ', $select) . " ";
284 }
285
286 function from() {
287 $this->_from = "
288 FROM civicrm_grant {$this->_aliases['civicrm_grant']}
289 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact']}
290 ON ({$this->_aliases['civicrm_grant']}.contact_id = {$this->_aliases['civicrm_contact']}.id ) ";
291 if ($this->_addressField) {
292 $this->_from .= "
293 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
294 ON {$this->_aliases['civicrm_contact']}.id =
295 {$this->_aliases['civicrm_address']}.contact_id AND
296 {$this->_aliases['civicrm_address']}.is_primary = 1\n";
297 }
298 }
299
300 function where() {
301 $clauses = array();
302 $this->_where = '';
303 foreach ($this->_columns as $tableName => $table) {
304 if (array_key_exists('filters', $table)) {
305 foreach ($table['filters'] as $fieldName => $field) {
306
307 $clause = NULL;
308 if (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE) {
309 $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params);
310 $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params);
311 $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params);
312
313 if ($relative || $from || $to) {
314 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
315 }
316 }
317 else {
318 $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params);
319 if ($op) {
320 $clause = $this->whereClause($field,
321 $op,
322 CRM_Utils_Array::value("{$fieldName}_value", $this->_params),
323 CRM_Utils_Array::value("{$fieldName}_min", $this->_params),
324 CRM_Utils_Array::value("{$fieldName}_max", $this->_params)
325 );
326 }
327 }
328 if (!empty($clause)) {
329 $clauses[] = $clause;
330 $this->_where = "WHERE " . implode(' AND ', $clauses);
331 }
332 }
333 }
334 }
335 }
336
337 function groupBy() {
338 $this->_groupBy = "";
339 if (CRM_Utils_Array::value('group_bys', $this->_params) &&
340 is_array($this->_params['group_bys']) &&
341 !empty($this->_params['group_bys'])
342 ) {
343 foreach ($this->_columns as $tableName => $table) {
344 if (array_key_exists('group_bys', $table)) {
345 foreach ($table['group_bys'] as $fieldName => $field) {
346 if (CRM_Utils_Array::value($fieldName, $this->_params['group_bys'])) {
347 $this->_groupBy[] = $field['dbAlias'];
348 }
349 }
350 }
351 }
352 }
353 if (!empty($this->_groupBy)) {
354 $this->_groupBy = "ORDER BY " . implode(', ', $this->_groupBy) . ", {$this->_aliases['civicrm_contact']}.sort_name";
355 }
356 else {
357 $this->_groupBy = "ORDER BY {$this->_aliases['civicrm_contact']}.sort_name";
358 }
359 }
360
361 function alterDisplay(&$rows) {
362 // custom code to alter rows
363 $entryFound = FALSE;
364 foreach ($rows as $rowNum => $row) {
365 // convert display name to links
366 if (array_key_exists('civicrm_contact_sort_name', $row) &&
367 array_key_exists('civicrm_contact_id', $row)
368 ) {
369 $url = CRM_Utils_System::url('civicrm/contact/view',
370 'reset=1&cid=' . $row['civicrm_contact_id'],
371 $this->_absoluteUrl
372 );
373 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
374 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View contact details for this record.");
375 $entryFound = TRUE;
376 }
377
378 if (array_key_exists('civicrm_grant_grant_type_id', $row)) {
379 if ($value = $row['civicrm_grant_grant_type_id']) {
380 $rows[$rowNum]['civicrm_grant_grant_type_id'] = CRM_Grant_PseudoConstant::grantType($value);
381 }
382 $entryFound = TRUE;
383 }
384 if ( array_key_exists('civicrm_grant_grant_program_id', $row) ) {
385 if ( $value = $row['civicrm_grant_grant_program_id'] ) {
386 $rows[$rowNum]['civicrm_grant_grant_program_id'] = CRM_Grant_BAO_GrantProgram::grantPrograms( $value );
387 }
388 $entryFound = true;
389 }
390 if (array_key_exists('civicrm_grant_status_id', $row)) {
391 if ($value = $row['civicrm_grant_status_id']) {
392 $rows[$rowNum]['civicrm_grant_status_id'] = CRM_Grant_PseudoConstant::grantStatus($value);
393 }
394 $entryFound = TRUE;
395 }
396 if (array_key_exists('civicrm_grant_grant_report_received', $row)) {
397 if ($value = $row['civicrm_grant_grant_report_received']) {
398 if ($value == 1) {
399 $value = 'Yes';
400 }
401 else {
402 $value = 'No';
403 }
404 $rows[$rowNum]['civicrm_grant_grant_report_received'] = $value;
405 }
406 $entryFound = TRUE;
407 }
408 if (!$entryFound) {
409 break;
410 }
411 }
412 }
413 }
414