Merge pull request #11868 from eileenmcnaughton/cust_url
[civicrm-core.git] / CRM / Report / Form / Grant / Detail.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
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 $_customGroupExtends = array(
36 'Grant',
37 );
38
39 /**
40 * Class constructor.
41 */
42 public function __construct() {
43 $this->_columns = array(
44 'civicrm_contact' => array(
45 'dao' => 'CRM_Contact_DAO_Contact',
46 'fields' => array(
47 'sort_name' => array(
48 'title' => ts('Contact Name'),
49 'required' => TRUE,
50 'no_repeat' => TRUE,
51 ),
52 'id' => array(
53 'no_display' => TRUE,
54 'required' => TRUE,
55 ),
56 ),
57 'grouping' => 'contact-fields',
58 'filters' => array(
59 'sort_name' => array(
60 'title' => ts('Contact Name'),
61 'operator' => 'like',
62 ),
63 'gender_id' => array(
64 'title' => ts('Gender'),
65 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
66 'options' => CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id'),
67 ),
68 'id' => array(
69 'title' => ts('Contact ID'),
70 'no_display' => TRUE,
71 ),
72 ),
73 ),
74 'civicrm_address' => array(
75 'dao' => 'CRM_Core_DAO_Address',
76 'filters' => array(
77 'country_id' => array(
78 'title' => ts('Country'),
79 'type' => CRM_Utils_Type::T_INT,
80 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
81 'options' => CRM_Core_PseudoConstant::country(),
82 ),
83 'state_province_id' => array(
84 'title' => ts('State/Province'),
85 'type' => CRM_Utils_Type::T_INT,
86 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
87 'options' => CRM_Core_PseudoConstant::stateProvince(),
88 ),
89 ),
90 ),
91 'civicrm_grant' => array(
92 'dao' => 'CRM_Grant_DAO_Grant',
93 'fields' => array(
94 'grant_type_id' => array(
95 'name' => 'grant_type_id',
96 'title' => ts('Grant Type'),
97 ),
98 'status_id' => array(
99 'name' => 'status_id',
100 'title' => ts('Grant Status'),
101 ),
102 'amount_total' => array(
103 'name' => 'amount_total',
104 'title' => ts('Amount Requested'),
105 'type' => CRM_Utils_Type::T_MONEY,
106 ),
107 'amount_granted' => array(
108 'name' => 'amount_granted',
109 'title' => ts('Amount Granted'),
110 ),
111 'application_received_date' => array(
112 'name' => 'application_received_date',
113 'title' => ts('Application Received'),
114 'default' => TRUE,
115 ),
116 'money_transfer_date' => array(
117 'name' => 'money_transfer_date',
118 'title' => ts('Money Transfer Date'),
119 'type' => CRM_Utils_Type::T_DATE,
120 ),
121 'grant_due_date' => array(
122 'name' => 'grant_due_date',
123 'title' => ts('Grant Report Due'),
124 'type' => CRM_Utils_Type::T_DATE,
125 ),
126 'decision_date' => array(
127 'name' => 'decision_date',
128 'title' => ts('Grant Decision Date'),
129 'type' => CRM_Utils_Type::T_DATE,
130 ),
131 'rationale' => array(
132 'name' => 'rationale',
133 'title' => ts('Rationale'),
134 ),
135 'grant_report_received' => array(
136 'name' => 'grant_report_received',
137 'title' => ts('Grant Report Received'),
138 ),
139 ),
140 'filters' => array(
141 'grant_type' => array(
142 'name' => 'grant_type_id',
143 'title' => ts('Grant Type'),
144 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
145 'options' => CRM_Core_PseudoConstant::get('CRM_Grant_DAO_Grant', 'grant_type_id'),
146 ),
147 'status_id' => array(
148 'name' => 'status_id',
149 'title' => ts('Grant Status'),
150 'type' => CRM_Utils_Type::T_INT,
151 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
152 'options' => CRM_Core_PseudoConstant::get('CRM_Grant_DAO_Grant', 'status_id'),
153 ),
154 'amount_granted' => array(
155 'title' => ts('Amount Granted'),
156 'operatorType' => CRM_Report_Form::OP_INT,
157 ),
158 'amount_total' => array(
159 'title' => ts('Amount Requested'),
160 'operatorType' => CRM_Report_Form::OP_INT,
161 ),
162 'application_received_date' => array(
163 'title' => ts('Application Received'),
164 'operatorType' => CRM_Report_Form::OP_DATE,
165 'type' => CRM_Utils_Type::T_DATE,
166 ),
167 'money_transfer_date' => array(
168 'title' => ts('Money Transfer Date'),
169 'operatorType' => CRM_Report_Form::OP_DATE,
170 'type' => CRM_Utils_Type::T_DATE,
171 ),
172 'grant_due_date' => array(
173 'title' => ts('Grant Report Due'),
174 'operatorType' => CRM_Report_Form::OP_DATE,
175 'type' => CRM_Utils_Type::T_DATE,
176 ),
177 'decision_date' => array(
178 'title' => ts('Grant Decision Date'),
179 'operatorType' => CRM_Report_Form::OP_DATE,
180 'type' => CRM_Utils_Type::T_DATE,
181 ),
182 ),
183 'group_bys' => array(
184 'grant_type_id' => array(
185 'title' => ts('Grant Type'),
186 ),
187 'status_id' => array(
188 'title' => ts('Grant Status'),
189 ),
190 'amount_total' => array(
191 'title' => ts('Amount Requested'),
192 ),
193 'amount_granted' => array(
194 'title' => ts('Amount Granted'),
195 ),
196 'application_received_date' => array(
197 'title' => ts('Application Received Date'),
198 ),
199 'money_transfer_date' => array(
200 'title' => ts('Money Transfer Date'),
201 ),
202 'decision_date' => array(
203 'title' => ts('Grant Decision Date'),
204 ),
205 ),
206 ),
207 );
208
209 parent::__construct();
210 }
211
212 public function select() {
213 // @todo remove this override - seems to do nothing parent doesn't.
214 $select = array();
215
216 $this->_columnHeaders = array();
217 foreach ($this->_columns as $tableName => $table) {
218 if (array_key_exists('fields', $table)) {
219 foreach ($table['fields'] as $fieldName => $field) {
220 if (!empty($field['required']) ||
221 !empty($this->_params['fields'][$fieldName])
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 public 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 $this->joinAddressFromContact();
242 }
243
244 public function where() {
245 // @todo this function appears to do nothing more than parent, test & remove
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);
255 $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params);
256 $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params);
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;
275 }
276 }
277 }
278 }
279 if (!empty($clauses)) {
280 $this->_where = "WHERE " . implode(' AND ', $clauses);
281 }
282 }
283
284 public function groupBy() {
285 // @todo this function appears to do nothing more than parent, test & remove
286 $this->_groupBy = "";
287 if (!empty($this->_params['group_bys']) &&
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) {
294 if (!empty($this->_params['group_bys'][$fieldName])) {
295 $this->_groupBy[] = $field['dbAlias'];
296 }
297 }
298 }
299 }
300 }
301 if (!empty($this->_groupBy)) {
302 $this->_groupBy = "ORDER BY " . implode(', ', $this->_groupBy) .
303 ", {$this->_aliases['civicrm_contact']}.sort_name";
304 }
305 else {
306 $this->_groupBy = "ORDER BY {$this->_aliases['civicrm_contact']}.sort_name";
307 }
308 }
309
310 /**
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.
318 */
319 public function alterDisplay(&$rows) {
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',
327 'reset=1&cid=' . $row['civicrm_contact_id'],
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']) {
337 $rows[$rowNum]['civicrm_grant_grant_type_id'] = CRM_Core_PseudoConstant::getLabel('CRM_Grant_DAO_Grant', 'grant_type_id', $value);
338 }
339 $entryFound = TRUE;
340 }
341 if (array_key_exists('civicrm_grant_status_id', $row)) {
342 if ($value = $row['civicrm_grant_status_id']) {
343 $rows[$rowNum]['civicrm_grant_status_id'] = CRM_Core_PseudoConstant::getLabel('CRM_Grant_DAO_Grant', 'status_id', $value);
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 }
364
365 }