Fix indentation
[civicrm-core.git] / tools / extensions / org.civicrm.report.grant / Grant.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.1 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2011 |
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-2011
32 * $Id$
33 *
34 */
35
36 require_once 'CRM/Report/Form.php';
37 require_once 'CRM/Grant/PseudoConstant.php';
38 class org_civicrm_report_grant extends CRM_Report_Form {
39
40 protected $_addressField = FALSE;
41
42 function __construct() {
43 $this->_columns = array(
44 'civicrm_contact' =>
45 array('dao' => 'CRM_Contact_DAO_Contact',
46 'fields' =>
47 array('display_name' =>
48 array('title' => ts('Contact Name'),
49 'required' => TRUE,
50 'no_repeat' => TRUE,
51 ),
52 ),
53 'grouping' => 'contact-fields',
54 'filters' =>
55 array('display_name' =>
56 array('title' => ts('Contact Name'),
57 'operator' => 'like',
58 ),
59 'gender_id' =>
60 array('title' => ts('Gender'),
61 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
62 'options' => CRM_Core_PseudoConstant::gender(),
63 ),
64 ),
65 ),
66 'civicrm_address' =>
67 array('dao' => 'CRM_Core_DAO_Address',
68 'filters' =>
69 array('country_id' =>
70 array('title' => ts('Country'),
71 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
72 'options' => CRM_Core_PseudoConstant::country(),
73 ),
74 'state_province_id' =>
75 array('title' => ts('State/Province'),
76 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
77 'options' => CRM_Core_PseudoConstant::stateProvince(),
78 ),
79 ),
80 ),
81 'civicrm_grant' =>
82 array('dao' => 'CRM_Grant_DAO_Grant',
83 'fields' =>
84 array(
85 'grant_type_id' =>
86 array(
87 'name' => 'grant_type_id',
88 'title' => ts('Grant Type'),
89 ),
90 'status_id' =>
91 array(
92 'name' => 'status_id',
93 'title' => ts('Grant Status'),
94 ),
95 'amount_requested' =>
96 array(
97 'name' => 'amount_requested',
98 'title' => ts('Amount Requested'),
99 'type' => CRM_Utils_Type::T_MONEY,
100 ),
101 'amount_granted' =>
102 array(
103 'name' => 'amount_granted',
104 'title' => ts('Amount Granted'),
105 ),
106 'application_received_date' =>
107 array(
108 'name' => 'application_received_date',
109 'title' => ts('Application Received Date'),
110 'default' => TRUE,
111 ),
112 'money_transfer_date' =>
113 array(
114 'name' => 'money_transfer_date',
115 'title' => ts('Money Transfer Date'),
116 'type' => CRM_Utils_Type::T_DATE,
117 ),
118 'grant_due_date' =>
119 array(
120 'name' => 'grant_due_date',
121 'title' => ts('Grant Due Date'),
122 'type' => CRM_Utils_Type::T_DATE,
123 ),
124 'rationale' =>
125 array(
126 'name' => 'rationale',
127 'title' => ts('Rationale'),
128 ),
129 'grant_report_received' =>
130 array(
131 'name' => 'grant_report_received',
132 'title' => ts('Grant Report Received'),
133 ),
134 ),
135 'filters' =>
136 array('grant_type' =>
137 array(
138 'name' => 'grant_type_id',
139 'title' => ts('Grant Type'),
140 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
141 'options' => CRM_Grant_PseudoConstant::grantType(),
142 ),
143 'status_id' =>
144 array(
145 'name' => 'status_id',
146 'title' => ts('Grant Status'),
147 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
148 'options' => CRM_Grant_PseudoConstant::grantStatus(),
149 ),
150 'amount_granted' =>
151 array(
152 'title' => ts('Amount Granted'),
153 'operatorType' => CRM_Report_Form::OP_INT,
154 ),
155 'amount_requested' =>
156 array(
157 'title' => ts('Amount Requested'),
158 'operatorType' => CRM_Report_Form::OP_INT,
159 ),
160 'application_received_date' =>
161 array(
162 'title' => ts('Application Received Date'),
163 'operatorType' => CRM_Report_Form::OP_DATE,
164 ),
165 'money_transfer_date' =>
166 array(
167 'title' => ts('Money Transfer Date'),
168 'operatorType' => CRM_Report_Form::OP_DATE,
169 ),
170 'grant_due_date' =>
171 array(
172 'title' => ts('Grant Due Date'),
173 'operatorType' => CRM_Report_Form::OP_DATE,
174 'type' => CRM_Report_Form::OP_DATE,
175 ),
176 ),
177 'group_bys' =>
178 array(
179 'grant_type_id' =>
180 array(
181 'title' => ts('Grant Type'),
182 ),
183 'status_id' =>
184 array(
185 'title' => ts('Grant Status'),
186 ),
187 'amount_requested' =>
188 array(
189 'title' => ts('Amount Requested'),
190 ),
191 'amount_granted' =>
192 array(
193 'title' => ts('Amount Granted'),
194 ),
195 'application_received_date' =>
196 array(
197 'title' => ts('Application Received Date'),
198 ),
199 'money_transfer_date' =>
200 array(
201 'title' => ts('Money Transfer Date'),
202 ),
203 ),
204 ),
205 );
206
207 parent::__construct();
208 }
209
210 function select() {
211 $select = array();
212
213 $this->_columnHeaders = array();
214 foreach ($this->_columns as $tableName => $table) {
215 if ($tableName == 'civicrm_address') {
216 $this->_addressField = TRUE;
217 }
218 if (array_key_exists('fields', $table)) {
219 foreach ($table['fields'] as $fieldName => $field) {
220 if (CRM_Utils_Array::value('required', $field) ||
221 CRM_Utils_Array::value($fieldName, $this->_params['fields'])
222 ) {
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
236 function from() {
237 $this->_from = "
238 FROM civicrm_grant {$this->_aliases['civicrm_grant']}
239 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact']}
240 ON ({$this->_aliases['civicrm_grant']}.contact_id = {$this->_aliases['civicrm_contact']}.id ) ";
241 if ($this->_addressField) {
242 $this->_from .= "
243 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
244 ON {$this->_aliases['civicrm_contact']}.id =
245 {$this->_aliases['civicrm_address']}.contact_id AND
246 {$this->_aliases['civicrm_address']}.is_primary = 1\n";
247 }
248 }
249
250 function where() {
251 $clauses = array();
252 foreach ($this->_columns as $tableName => $table) {
253 if (array_key_exists('filters', $table)) {
254 foreach ($table['filters'] as $fieldName => $field) {
255
256 $clause = NULL;
257 if (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE) {
258 $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params);
259 $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params);
260 $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params);
261
262 if ($relative || $from || $to) {
263 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
264 }
265 }
266 else {
267 $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params);
268 if ($op) {
269 $clause = $this->whereClause($field,
270 $op,
271 CRM_Utils_Array::value("{$fieldName}_value", $this->_params),
272 CRM_Utils_Array::value("{$fieldName}_min", $this->_params),
273 CRM_Utils_Array::value("{$fieldName}_max", $this->_params)
274 );
275 }
276 }
277 if (!empty($clause)) {
278 $clauses[] = $clause;
279 $this->_where = "WHERE" . implode(' AND ', $clauses);
280 }
281 }
282 }
283 }
284 }
285
286 function groupBy() {
287 $this->_groupBy = "";
288 if (CRM_Utils_Array::value('group_bys', $this->_params) &&
289 is_array($this->_params['group_bys']) &&
290 !empty($this->_params['group_bys'])
291 ) {
292 foreach ($this->_columns as $tableName => $table) {
293 if (array_key_exists('group_bys', $table)) {
294 foreach ($table['group_bys'] as $fieldName => $field) {
295 if (CRM_Utils_Array::value($fieldName, $this->_params['group_bys'])) {
296 $this->_groupBy[] = $field['dbAlias'];
297 }
298 }
299 }
300 }
301 }
302 if (!empty($this->_groupBy)) {
303 $this->_groupBy = "ORDER BY " . implode(', ', $this->_groupBy) . ", {$this->_aliases['civicrm_contact']}.sort_name";
304 }
305 }
306
307 function alterDisplay(&$rows) {
308 // custom code to alter rows
309 $entryFound = FALSE;
310 foreach ($rows as $rowNum => $row) {
311 if (array_key_exists('civicrm_grant_grant_type_id', $row)) {
312 if ($value = $row['civicrm_grant_grant_type_id']) {
313 $rows[$rowNum]['civicrm_grant_grant_type_id'] = CRM_Grant_PseudoConstant::grantType($value);
314 }
315 $entryFound = TRUE;
316 }
317 if (array_key_exists('civicrm_grant_status_id', $row)) {
318 if ($value = $row['civicrm_grant_status_id']) {
319 $rows[$rowNum]['civicrm_grant_status_id'] = CRM_Grant_PseudoConstant::grantStatus($value);
320 }
321 $entryFound = TRUE;
322 }
323 if (array_key_exists('civicrm_grant_grant_report_received', $row)) {
324 if ($value = $row['civicrm_grant_grant_report_received']) {
325 if ($value == 1) {
326 $value = 'Yes';
327 }
328 else {
329 $value = 'No';
330 }
331 $rows[$rowNum]['civicrm_grant_grant_report_received'] = $value;
332 }
333 $entryFound = TRUE;
334 }
335 if (!$entryFound) {
336 break;
337 }
338 }
339 }
340 }
341