Update copyright date for 2020
[civicrm-core.git] / CRM / Grant / Selector / Search.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
f299f7db 6 | Copyright CiviCRM LLC (c) 2004-2020 |
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
f299f7db 31 * @copyright CiviCRM LLC (c) 2004-2020
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * This class is used to retrieve and display a range of
38 * contacts that match the given criteria (specifically for
39 * results of advanced search options.
40 *
41 */
42class CRM_Grant_Selector_Search extends CRM_Core_Selector_Base implements CRM_Core_Selector_API {
43
44 /**
45 * This defines two actions- View and Edit.
46 *
47 * @var array
6a488035 48 */
7e8c8317 49 public static $_links = NULL;
6a488035
TO
50
51 /**
100fef9d 52 * We use desc to remind us what that column is, name is used in the tpl
6a488035
TO
53 *
54 * @var array
6a488035 55 */
7e8c8317 56 public static $_columnHeaders;
6a488035
TO
57
58 /**
59 * Properties of contact we're interested in displaying
60 * @var array
6a488035 61 */
7e8c8317 62 public static $_properties = [
6a488035
TO
63 'contact_id',
64 'contact_type',
65 'sort_name',
66 'grant_id',
67 'grant_status_id',
68 'grant_status',
69 'grant_type_id',
70 'grant_type',
71 'grant_amount_total',
72 'grant_amount_requested',
73 'grant_amount_granted',
74 'grant_application_received_date',
75 'grant_report_received',
76 'grant_money_transfer_date',
be2fb01f 77 ];
6a488035
TO
78
79 /**
fe482240 80 * Are we restricting ourselves to a single contact.
6a488035 81 *
d51c6add 82 * @var bool
6a488035
TO
83 */
84 protected $_single = FALSE;
85
86 /**
fe482240 87 * Are we restricting ourselves to a single contact.
6a488035 88 *
d51c6add 89 * @var bool
6a488035
TO
90 */
91 protected $_limit = NULL;
92
93 /**
fe482240 94 * What context are we being invoked from.
6a488035 95 *
6a488035
TO
96 * @var string
97 */
98 protected $_context = NULL;
99
100 /**
fe482240 101 * QueryParams is the array returned by exportValues called on.
6a488035
TO
102 * the HTML_QuickForm_Controller for that page.
103 *
104 * @var array
6a488035
TO
105 */
106 public $_queryParams;
107
108 /**
fe482240 109 * Represent the type of selector.
6a488035
TO
110 *
111 * @var int
6a488035
TO
112 */
113 protected $_action;
114
115 /**
fe482240 116 * The additional clause that we restrict the search with.
6a488035
TO
117 *
118 * @var string
119 */
120 protected $_grantClause = NULL;
121
122 /**
fe482240 123 * The query object.
6a488035
TO
124 *
125 * @var string
126 */
127 protected $_query;
128
129 /**
fe482240 130 * Class constructor.
6a488035 131 *
85511635
TO
132 * @param array $queryParams
133 * Array of parameters for query.
77b97be7 134 * @param \const|int $action - action of search basic or advanced.
85511635
TO
135 * @param string $grantClause
136 * If the caller wants to further restrict the search.
137 * @param bool $single
138 * Are we dealing only with one contact?.
139 * @param int $limit
140 * How many participations do we want returned.
6a488035 141 *
77b97be7
EM
142 * @param string $context
143 *
144 * @return \CRM_Grant_Selector_Search
6a488035 145 */
3bdca100 146 public function __construct(
73714296 147 &$queryParams,
1afc557a 148 $action = CRM_Core_Action::NONE,
6a488035 149 $grantClause = NULL,
1afc557a
TO
150 $single = FALSE,
151 $limit = NULL,
152 $context = 'search'
6a488035
TO
153 ) {
154 // submitted form values
155 $this->_queryParams = &$queryParams;
156
353ffa53
TO
157 $this->_single = $single;
158 $this->_limit = $limit;
6a488035
TO
159 $this->_context = $context;
160
161 $this->_grantClause = $grantClause;
162
163 // type of selector
164 $this->_action = $action;
165
166 $this->_query = new CRM_Contact_BAO_Query($this->_queryParams, NULL, NULL, FALSE, FALSE,
167 CRM_Contact_BAO_Query::MODE_GRANT
168 );
169 $this->_query->_distinctComponentClause = " civicrm_grant.id";
170 $this->_query->_groupByComponentClause = " GROUP BY civicrm_grant.id ";
171 }
6a488035
TO
172
173 /**
174 * This method returns the links that are given for each search row.
175 * currently the links added for each row are
176 *
177 * - View
178 * - Edit
179 *
546b78fa 180 * @param string|null $key
77b97be7 181 *
6a488035 182 * @return array
6a488035 183 */
00be9182 184 public static function &links($key = NULL) {
b9c81d6a 185 $cid = CRM_Utils_Request::retrieve('cid', 'Integer');
6a488035
TO
186 $extraParams = ($key) ? "&key={$key}" : NULL;
187
188 if (!(self::$_links)) {
be2fb01f
CW
189 self::$_links = [
190 CRM_Core_Action::VIEW => [
6a488035
TO
191 'name' => ts('View'),
192 'url' => 'civicrm/contact/view/grant',
193 'qs' => 'reset=1&id=%%id%%&cid=%%cid%%&action=view&context=%%cxt%%&selectedChild=grant' . $extraParams,
194 'title' => ts('View Grant'),
be2fb01f
CW
195 ],
196 CRM_Core_Action::UPDATE => [
6a488035
TO
197 'name' => ts('Edit'),
198 'url' => 'civicrm/contact/view/grant',
199 'qs' => 'reset=1&action=update&id=%%id%%&cid=%%cid%%&context=%%cxt%%' . $extraParams,
200 'title' => ts('Edit Grant'),
be2fb01f
CW
201 ],
202 ];
6a488035
TO
203
204 if ($cid) {
be2fb01f
CW
205 $delLink = [
206 CRM_Core_Action::DELETE => [
353ffa53 207 'name' => ts('Delete'),
6a488035
TO
208 'url' => 'civicrm/contact/view/grant',
209 'qs' => 'action=delete&reset=1&cid=%%cid%%&id=%%id%%&selectedChild=grant' . $extraParams,
6a488035 210 'title' => ts('Delete Grant'),
be2fb01f
CW
211 ],
212 ];
6a488035
TO
213 self::$_links = self::$_links + $delLink;
214 }
215 }
216 return self::$_links;
217 }
6a488035
TO
218
219 /**
100fef9d 220 * Getter for array of the parameters required for creating pager.
6a488035 221 *
77b97be7 222 * @param $action
c490a46a 223 * @param array $params
6a488035 224 */
00be9182 225 public function getPagerParams($action, &$params) {
6a488035
TO
226 $params['status'] = ts('Grant') . ' %%StatusMessage%%';
227 $params['csvString'] = NULL;
228 if ($this->_limit) {
229 $params['rowCount'] = $this->_limit;
230 }
231 else {
232 $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT;
233 }
234
235 $params['buttonTop'] = 'PagerTopButton';
236 $params['buttonBottom'] = 'PagerBottomButton';
237 }
6a488035
TO
238
239 /**
240 * Returns total number of rows for the query.
241 *
54957108 242 * @param int $action
6a488035 243 *
a6c01b45
CW
244 * @return int
245 * Total number of rows
6a488035 246 */
00be9182 247 public function getTotalCount($action) {
6a488035
TO
248 return $this->_query->searchQuery(0, 0, NULL,
249 TRUE, FALSE,
250 FALSE, FALSE,
251 FALSE,
252 $this->_grantClause
253 );
254 }
255
256 /**
100fef9d 257 * Returns all the rows in the given offset and rowCount *
6a488035 258 *
3f8d2862 259 * @param string $action
85511635
TO
260 * The action being performed.
261 * @param int $offset
262 * The row number to start from.
263 * @param int $rowCount
264 * The number of rows to return.
265 * @param string $sort
266 * The sql string that describes the sort order.
3f8d2862 267 * @param string $output
85511635 268 * What should the result set include (web/email/csv).
6a488035 269 *
a6c01b45
CW
270 * @return int
271 * the total number of rows for this action
6a488035 272 */
00be9182 273 public function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
6a488035
TO
274 $result = $this->_query->searchQuery($offset, $rowCount, $sort,
275 FALSE, FALSE,
276 FALSE, FALSE,
277 FALSE,
278 $this->_grantClause
279 );
280
281 // process the result of the query
be2fb01f 282 $rows = [];
6a488035
TO
283
284 //CRM-4418 check for view, edit, delete
be2fb01f 285 $permissions = [CRM_Core_Permission::VIEW];
6a488035
TO
286 if (CRM_Core_Permission::check('edit grants')) {
287 $permissions[] = CRM_Core_Permission::EDIT;
288 }
289 if (CRM_Core_Permission::check('delete in CiviGrant')) {
290 $permissions[] = CRM_Core_Permission::DELETE;
291 }
292 $mask = CRM_Core_Action::mask($permissions);
293
294 while ($result->fetch()) {
be2fb01f 295 $row = [];
6a488035
TO
296 // the columns we are interested in
297 foreach (self::$_properties as $property) {
298 if (isset($result->$property)) {
299 $row[$property] = $result->$property;
300 }
301 }
302
303 if ($this->_context == 'search') {
304 $row['checkbox'] = CRM_Core_Form::CB_PREFIX . $result->grant_id;
305 }
306
307 $row['action'] = CRM_Core_Action::formLink(self::links($this->_key),
308 $mask,
be2fb01f 309 [
6a488035
TO
310 'id' => $result->grant_id,
311 'cid' => $result->contact_id,
312 'cxt' => $this->_context,
be2fb01f 313 ],
87dab4a4
AH
314 ts('more'),
315 FALSE,
316 'grant.selector.row',
317 'Grant',
318 $result->grant_id
6a488035
TO
319 );
320
1afc557a 321 $row['contact_type'] = CRM_Contact_BAO_Contact_Utils::getImage($result->contact_sub_type ? $result->contact_sub_type : $result->contact_type, FALSE, $result->contact_id
6a488035
TO
322 );
323
324 $rows[] = $row;
325 }
326
327 return $rows;
328 }
329
330 /**
1054415f 331 * @inheritDoc
6a488035 332 */
6a488035
TO
333 public function getQILL() {
334 return $this->_query->qill();
335 }
336
337 /**
100fef9d 338 * Returns the column headers as an array of tuples:
6a488035
TO
339 * (name, sortName (key to the sort array))
340 *
85511635
TO
341 * @param string $action
342 * The action being performed.
3f8d2862 343 * @param string $output
85511635 344 * What should the result set include (web/email/csv).
6a488035 345 *
a6c01b45
CW
346 * @return array
347 * the column headers that need to be displayed
6a488035
TO
348 */
349 public function &getColumnHeaders($action = NULL, $output = NULL) {
350 if (!isset(self::$_columnHeaders)) {
be2fb01f
CW
351 self::$_columnHeaders = [
352 [
353ffa53 353 'name' => ts('Status'),
6a488035
TO
354 'sort' => 'grant_status',
355 'direction' => CRM_Utils_Sort::DONTCARE,
be2fb01f
CW
356 ],
357 [
6a488035
TO
358 'name' => ts('Type'),
359 'sort' => 'grant_type_id',
360 'direction' => CRM_Utils_Sort::DONTCARE,
be2fb01f
CW
361 ],
362 [
6a488035
TO
363 'name' => ts('Requested'),
364 'sort' => 'grant_amount_total',
365 'direction' => CRM_Utils_Sort::DONTCARE,
be2fb01f
CW
366 ],
367 [
6a488035
TO
368 'name' => ts('Granted'),
369 'sort' => 'grant_amount_granted',
370 'direction' => CRM_Utils_Sort::DONTCARE,
be2fb01f
CW
371 ],
372 [
6a488035
TO
373 'name' => ts('Application Received'),
374 'sort' => 'grant_application_received_date',
375 'direction' => CRM_Utils_Sort::DONTCARE,
be2fb01f
CW
376 ],
377 [
6a488035
TO
378 'name' => ts('Report Received'),
379 'sort' => 'grant_report_received',
380 'direction' => CRM_Utils_Sort::DONTCARE,
be2fb01f
CW
381 ],
382 [
6a488035
TO
383 'name' => ts('Money Transferred'),
384 'sort' => 'money_transfer_date',
385 'direction' => CRM_Utils_Sort::DONTCARE,
be2fb01f
CW
386 ],
387 ['desc' => ts('Actions')],
388 ];
6a488035
TO
389
390 if (!$this->_single) {
be2fb01f
CW
391 $pre = [
392 ['desc' => ts('Contact Type')],
393 [
6a488035
TO
394 'name' => ts('Name'),
395 'sort' => 'sort_name',
396 'direction' => CRM_Utils_Sort::ASCENDING,
be2fb01f
CW
397 ],
398 ];
6a488035
TO
399 self::$_columnHeaders = array_merge($pre, self::$_columnHeaders);
400 }
401 }
402 return self::$_columnHeaders;
403 }
404
e0ef6999
EM
405 /**
406 * @return string
407 */
00be9182 408 public function &getQuery() {
6a488035
TO
409 return $this->_query;
410 }
411
412 /**
100fef9d 413 * Name of export file.
6a488035 414 *
85511635
TO
415 * @param string $output
416 * Type of output.
6a488035 417 *
a6c01b45
CW
418 * @return string
419 * name of the file
6a488035 420 */
00be9182 421 public function getExportFileName($output = 'csv') {
6a488035
TO
422 return ts('CiviCRM Grant Search');
423 }
96025800 424
6a488035 425}