Merge pull request #15840 from yashodha/participant_edit
[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 * $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 */
26 class 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
32 */
33 public static $_links = NULL;
34
35 /**
36 * We use desc to remind us what that column is, name is used in the tpl
37 *
38 * @var array
39 */
40 public static $_columnHeaders;
41
42 /**
43 * Properties of contact we're interested in displaying
44 * @var array
45 */
46 public static $_properties = [
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',
61 ];
62
63 /**
64 * Are we restricting ourselves to a single contact.
65 *
66 * @var bool
67 */
68 protected $_single = FALSE;
69
70 /**
71 * Are we restricting ourselves to a single contact.
72 *
73 * @var bool
74 */
75 protected $_limit = NULL;
76
77 /**
78 * What context are we being invoked from.
79 *
80 * @var string
81 */
82 protected $_context = NULL;
83
84 /**
85 * QueryParams is the array returned by exportValues called on.
86 * the HTML_QuickForm_Controller for that page.
87 *
88 * @var array
89 */
90 public $_queryParams;
91
92 /**
93 * Represent the type of selector.
94 *
95 * @var int
96 */
97 protected $_action;
98
99 /**
100 * The additional clause that we restrict the search with.
101 *
102 * @var string
103 */
104 protected $_grantClause = NULL;
105
106 /**
107 * The query object.
108 *
109 * @var string
110 */
111 protected $_query;
112
113 /**
114 * Class constructor.
115 *
116 * @param array $queryParams
117 * Array of parameters for query.
118 * @param \const|int $action - action of search basic or advanced.
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.
125 *
126 * @param string $context
127 *
128 * @return \CRM_Grant_Selector_Search
129 */
130 public function __construct(
131 &$queryParams,
132 $action = CRM_Core_Action::NONE,
133 $grantClause = NULL,
134 $single = FALSE,
135 $limit = NULL,
136 $context = 'search'
137 ) {
138 // submitted form values
139 $this->_queryParams = &$queryParams;
140
141 $this->_single = $single;
142 $this->_limit = $limit;
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 }
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 *
164 * @param string|null $key
165 *
166 * @return array
167 */
168 public static function &links($key = NULL) {
169 $cid = CRM_Utils_Request::retrieve('cid', 'Integer');
170 $extraParams = ($key) ? "&key={$key}" : NULL;
171
172 if (!(self::$_links)) {
173 self::$_links = [
174 CRM_Core_Action::VIEW => [
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'),
179 ],
180 CRM_Core_Action::UPDATE => [
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'),
185 ],
186 ];
187
188 if ($cid) {
189 $delLink = [
190 CRM_Core_Action::DELETE => [
191 'name' => ts('Delete'),
192 'url' => 'civicrm/contact/view/grant',
193 'qs' => 'action=delete&reset=1&cid=%%cid%%&id=%%id%%&selectedChild=grant' . $extraParams,
194 'title' => ts('Delete Grant'),
195 ],
196 ];
197 self::$_links = self::$_links + $delLink;
198 }
199 }
200 return self::$_links;
201 }
202
203 /**
204 * Getter for array of the parameters required for creating pager.
205 *
206 * @param $action
207 * @param array $params
208 */
209 public function getPagerParams($action, &$params) {
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 }
222
223 /**
224 * Returns total number of rows for the query.
225 *
226 * @param int $action
227 *
228 * @return int
229 * Total number of rows
230 */
231 public function getTotalCount($action) {
232 return $this->_query->searchQuery(0, 0, NULL,
233 TRUE, FALSE,
234 FALSE, FALSE,
235 FALSE,
236 $this->_grantClause
237 );
238 }
239
240 /**
241 * Returns all the rows in the given offset and rowCount *
242 *
243 * @param string $action
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.
251 * @param string $output
252 * What should the result set include (web/email/csv).
253 *
254 * @return int
255 * the total number of rows for this action
256 */
257 public function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
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
266 $rows = [];
267
268 //CRM-4418 check for view, edit, delete
269 $permissions = [CRM_Core_Permission::VIEW];
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()) {
279 $row = [];
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,
293 [
294 'id' => $result->grant_id,
295 'cid' => $result->contact_id,
296 'cxt' => $this->_context,
297 ],
298 ts('more'),
299 FALSE,
300 'grant.selector.row',
301 'Grant',
302 $result->grant_id
303 );
304
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
306 );
307
308 $rows[] = $row;
309 }
310
311 return $rows;
312 }
313
314 /**
315 * @inheritDoc
316 */
317 public function getQILL() {
318 return $this->_query->qill();
319 }
320
321 /**
322 * Returns the column headers as an array of tuples:
323 * (name, sortName (key to the sort array))
324 *
325 * @param string $action
326 * The action being performed.
327 * @param string $output
328 * What should the result set include (web/email/csv).
329 *
330 * @return array
331 * the column headers that need to be displayed
332 */
333 public function &getColumnHeaders($action = NULL, $output = NULL) {
334 if (!isset(self::$_columnHeaders)) {
335 self::$_columnHeaders = [
336 [
337 'name' => ts('Status'),
338 'sort' => 'grant_status',
339 'direction' => CRM_Utils_Sort::DONTCARE,
340 ],
341 [
342 'name' => ts('Type'),
343 'sort' => 'grant_type_id',
344 'direction' => CRM_Utils_Sort::DONTCARE,
345 ],
346 [
347 'name' => ts('Requested'),
348 'sort' => 'grant_amount_total',
349 'direction' => CRM_Utils_Sort::DONTCARE,
350 ],
351 [
352 'name' => ts('Granted'),
353 'sort' => 'grant_amount_granted',
354 'direction' => CRM_Utils_Sort::DONTCARE,
355 ],
356 [
357 'name' => ts('Application Received'),
358 'sort' => 'grant_application_received_date',
359 'direction' => CRM_Utils_Sort::DONTCARE,
360 ],
361 [
362 'name' => ts('Report Received'),
363 'sort' => 'grant_report_received',
364 'direction' => CRM_Utils_Sort::DONTCARE,
365 ],
366 [
367 'name' => ts('Money Transferred'),
368 'sort' => 'money_transfer_date',
369 'direction' => CRM_Utils_Sort::DONTCARE,
370 ],
371 ['desc' => ts('Actions')],
372 ];
373
374 if (!$this->_single) {
375 $pre = [
376 ['desc' => ts('Contact Type')],
377 [
378 'name' => ts('Name'),
379 'sort' => 'sort_name',
380 'direction' => CRM_Utils_Sort::ASCENDING,
381 ],
382 ];
383 self::$_columnHeaders = array_merge($pre, self::$_columnHeaders);
384 }
385 }
386 return self::$_columnHeaders;
387 }
388
389 /**
390 * @return string
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 }