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