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