Merge pull request #15314 from jitendrapurohit/dev-1255
[civicrm-core.git] / CRM / Pledge / Selector / Search.php
... / ...
CommitLineData
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 */
23class CRM_Pledge_Selector_Search extends CRM_Core_Selector_Base {
24
25 /**
26 * This defines two actions- View and Edit.
27 *
28 * @var array
29 */
30 public static $_links = NULL;
31
32 /**
33 * We use desc to remind us what that column is, name is used in the tpl
34 *
35 * @var array
36 */
37 public static $_columnHeaders;
38
39 /**
40 * Properties of contact we're interested in displaying
41 *
42 * @var array
43 */
44 public static $_properties = [
45 'contact_id',
46 'sort_name',
47 'display_name',
48 'pledge_id',
49 'pledge_amount',
50 'pledge_create_date',
51 'pledge_total_paid',
52 'pledge_next_pay_date',
53 'pledge_next_pay_amount',
54 'pledge_outstanding_amount',
55 'pledge_status_id',
56 'pledge_status',
57 'pledge_is_test',
58 'pledge_contribution_page_id',
59 'pledge_financial_type',
60 'pledge_campaign_id',
61 'pledge_currency',
62 ];
63
64 /**
65 * Are we restricting ourselves to a single contact
66 *
67 * @var bool
68 */
69 protected $_single = FALSE;
70
71 /**
72 * Are we restricting ourselves to a single contact
73 *
74 * @var bool
75 */
76 protected $_limit = NULL;
77
78 /**
79 * What context are we being invoked from
80 *
81 * @var string
82 */
83 protected $_context = NULL;
84
85 /**
86 * QueryParams is the array returned by exportValues called on
87 * the HTML_QuickForm_Controller for that page.
88 *
89 * @var array
90 */
91 public $_queryParams;
92
93 /**
94 * Represent the type of selector
95 *
96 * @var int
97 */
98 protected $_action;
99
100 /**
101 * The additional clause that we restrict the search with
102 *
103 * @var string
104 */
105 protected $_additionalClause = NULL;
106
107 /**
108 * The query object
109 *
110 * @var string
111 */
112 protected $_query;
113
114 /**
115 * Class constructor.
116 *
117 * @param array $queryParams
118 * Array of parameters for query.
119 * @param \const|int $action - action of search basic or advanced.
120 * @param string $additionalClause
121 * If the caller wants to further restrict the search (used in participations).
122 * @param bool $single
123 * Are we dealing only with one contact?.
124 * @param int $limit
125 * How many signers do we want returned.
126 *
127 * @param string $context
128 *
129 * @return \CRM_Pledge_Selector_Search
130 */
131 public function __construct(
132 &$queryParams,
133 $action = CRM_Core_Action::NONE,
134 $additionalClause = NULL,
135 $single = FALSE,
136 $limit = NULL,
137 $context = 'search'
138 ) {
139 // submitted form values
140 $this->_queryParams = &$queryParams;
141
142 $this->_single = $single;
143 $this->_limit = $limit;
144 $this->_context = $context;
145
146 $this->_additionalClause = $additionalClause;
147
148 // type of selector
149 $this->_action = $action;
150
151 $this->_query = new CRM_Contact_BAO_Query($this->_queryParams, NULL, NULL, FALSE, FALSE,
152 CRM_Contact_BAO_Query::MODE_PLEDGE
153 );
154
155 $this->_query->_distinctComponentClause = "civicrm_pledge.id";
156 $this->_query->_groupByComponentClause = " GROUP BY civicrm_pledge.id ";
157 }
158
159 /**
160 * This method returns the links that are given for each search row.
161 *
162 * Currently the links added for each row are:
163 * - View
164 * - Edit
165 *
166 * @return array
167 */
168 public static function &links() {
169 $args = func_get_args();
170 $hideOption = CRM_Utils_Array::value(0, $args);
171 $key = CRM_Utils_Array::value(1, $args);
172
173 $extraParams = ($key) ? "&key={$key}" : NULL;
174
175 $cancelExtra = ts('Cancelling this pledge will also cancel any scheduled (and not completed) pledge payments.') . ' ' . ts('This action cannot be undone.') . ' ' . ts('Do you want to continue?');
176 self::$_links = [
177 CRM_Core_Action::VIEW => [
178 'name' => ts('View'),
179 'url' => 'civicrm/contact/view/pledge',
180 'qs' => 'reset=1&id=%%id%%&cid=%%cid%%&action=view&context=%%cxt%%&selectedChild=pledge' . $extraParams,
181 'title' => ts('View Pledge'),
182 ],
183 CRM_Core_Action::UPDATE => [
184 'name' => ts('Edit'),
185 'url' => 'civicrm/contact/view/pledge',
186 'qs' => 'reset=1&action=update&id=%%id%%&cid=%%cid%%&context=%%cxt%%' . $extraParams,
187 'title' => ts('Edit Pledge'),
188 ],
189 CRM_Core_Action::DETACH => [
190 'name' => ts('Cancel'),
191 'url' => 'civicrm/contact/view/pledge',
192 'qs' => 'reset=1&action=detach&id=%%id%%&cid=%%cid%%&context=%%cxt%%' . $extraParams,
193 'extra' => 'onclick = "return confirm(\'' . $cancelExtra . '\');"',
194 'title' => ts('Cancel Pledge'),
195 ],
196 CRM_Core_Action::DELETE => [
197 'name' => ts('Delete'),
198 'url' => 'civicrm/contact/view/pledge',
199 'qs' => 'reset=1&action=delete&id=%%id%%&cid=%%cid%%&context=%%cxt%%' . $extraParams,
200 'title' => ts('Delete Pledge'),
201 ],
202 ];
203
204 if (in_array('Cancel', $hideOption)) {
205 unset(self::$_links[CRM_Core_Action::DETACH]);
206 }
207
208 return self::$_links;
209 }
210
211 /**
212 * Getter for array of the parameters required for creating pager.
213 *
214 * @param $action
215 * @param array $params
216 */
217 public function getPagerParams($action, &$params) {
218 $params['status'] = ts('Pledge') . ' %%StatusMessage%%';
219 $params['csvString'] = NULL;
220 if ($this->_limit) {
221 $params['rowCount'] = $this->_limit;
222 }
223 else {
224 $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT;
225 }
226
227 $params['buttonTop'] = 'PagerTopButton';
228 $params['buttonBottom'] = 'PagerBottomButton';
229 }
230
231 /**
232 * Returns total number of rows for the query.
233 *
234 * @param int $action
235 *
236 * @return int
237 * Total number of rows
238 */
239 public function getTotalCount($action) {
240 return $this->_query->searchQuery(0, 0, NULL,
241 TRUE, FALSE,
242 FALSE, FALSE,
243 FALSE,
244 $this->_additionalClause
245 );
246 }
247
248 /**
249 * Returns all the rows in the given offset and rowCount.
250 *
251 * @param string $action
252 * The action being performed.
253 * @param int $offset
254 * The row number to start from.
255 * @param int $rowCount
256 * The number of rows to return.
257 * @param string $sort
258 * The sql string that describes the sort order.
259 * @param string $output
260 * What should the result set include (web/email/csv).
261 *
262 * @return int
263 * the total number of rows for this action
264 */
265 public function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
266 $result = $this->_query->searchQuery($offset, $rowCount, $sort,
267 FALSE, FALSE,
268 FALSE, FALSE,
269 FALSE,
270 $this->_additionalClause
271 );
272
273 // process the result of the query
274 $rows = [];
275
276 // get all pledge status
277 $pledgeStatuses = CRM_Pledge_BAO_Pledge::buildOptions('status_id');
278
279 // get all campaigns.
280 $allCampaigns = CRM_Campaign_BAO_Campaign::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE);
281
282 // CRM-4418 check for view, edit and delete
283 $permissions = [CRM_Core_Permission::VIEW];
284 if (CRM_Core_Permission::check('edit pledges')) {
285 $permissions[] = CRM_Core_Permission::EDIT;
286 }
287 if (CRM_Core_Permission::check('delete in CiviPledge')) {
288 $permissions[] = CRM_Core_Permission::DELETE;
289 }
290 $mask = CRM_Core_Action::mask($permissions);
291
292 while ($result->fetch()) {
293 $row = [];
294 // the columns we are interested in
295 foreach (self::$_properties as $property) {
296 if (isset($result->$property)) {
297 $row[$property] = $result->$property;
298 }
299 }
300
301 // carry campaign on selectors.
302 $row['campaign'] = CRM_Utils_Array::value($result->pledge_campaign_id, $allCampaigns);
303 $row['campaign_id'] = $result->pledge_campaign_id;
304
305 // add pledge status name
306 if (!empty($row['pledge_status_id'])) {
307 $row['pledge_status_name'] = CRM_Utils_Array::value($row['pledge_status_id'],
308 $pledgeStatuses
309 );
310 }
311 // append (test) to status label
312 if (!empty($row['pledge_is_test'])) {
313 $row['pledge_status'] = CRM_Core_TestEntity::appendTestText($row['pledge_status']);
314 }
315
316 $hideOption = [];
317 if (CRM_Utils_Array::key('Cancelled', $row) ||
318 CRM_Utils_Array::key('Completed', $row)
319 ) {
320 $hideOption[] = 'Cancel';
321 }
322
323 $row['checkbox'] = CRM_Core_Form::CB_PREFIX . $result->pledge_id;
324
325 $row['action'] = CRM_Core_Action::formLink(self::links($hideOption, $this->_key),
326 $mask,
327 [
328 'id' => $result->pledge_id,
329 'cid' => $result->contact_id,
330 'cxt' => $this->_context,
331 ],
332 ts('more'),
333 FALSE,
334 'pledge.selector.row',
335 'Pledge',
336 $result->pledge_id
337 );
338
339 $row['contact_type'] = CRM_Contact_BAO_Contact_Utils::getImage($result->contact_sub_type ? $result->contact_sub_type : $result->contact_type, FALSE, $result->contact_id
340 );
341 $rows[] = $row;
342 }
343 return $rows;
344 }
345
346 /**
347 * Get qill (display what was searched on).
348 *
349 * @inheritDoc
350 */
351 public function getQILL() {
352 return $this->_query->qill();
353 }
354
355 /**
356 * Returns the column headers as an array of tuples.
357 *
358 * Keys are name, sortName, key to the sort array
359 *
360 * @param string $action
361 * The action being performed.
362 * @param string $output
363 * What should the result set include (web/email/csv).
364 *
365 * @return array
366 * the column headers that need to be displayed
367 */
368 public function &getColumnHeaders($action = NULL, $output = NULL) {
369 if (!isset(self::$_columnHeaders)) {
370 self::$_columnHeaders = [
371 [
372 'name' => ts('Pledged'),
373 'sort' => 'pledge_amount',
374 'direction' => CRM_Utils_Sort::DONTCARE,
375 ],
376 [
377 'name' => ts('Total Paid'),
378 'sort' => 'pledge_total_paid',
379 'direction' => CRM_Utils_Sort::DONTCARE,
380 ],
381 [
382 'name' => ts('Balance'),
383 ],
384 [
385 'name' => ts('Pledged For'),
386 'sort' => 'pledge_financial_type',
387 'direction' => CRM_Utils_Sort::DONTCARE,
388 ],
389 [
390 'name' => ts('Pledge Made'),
391 'sort' => 'pledge_create_date',
392 'direction' => CRM_Utils_Sort::DESCENDING,
393 ],
394 [
395 'name' => ts('Next Pay Date'),
396 'sort' => 'pledge_next_pay_date',
397 'direction' => CRM_Utils_Sort::DONTCARE,
398 ],
399 [
400 'name' => ts('Next Amount'),
401 'sort' => 'pledge_next_pay_amount',
402 'direction' => CRM_Utils_Sort::DONTCARE,
403 ],
404 [
405 'name' => ts('Status'),
406 'sort' => 'pledge_status',
407 'direction' => CRM_Utils_Sort::DONTCARE,
408 ],
409 ['desc' => ts('Actions')],
410 ];
411
412 if (!$this->_single) {
413 $pre = [
414 ['desc' => ts('Contact ID')],
415 [
416 'name' => ts('Name'),
417 'sort' => 'sort_name',
418 'direction' => CRM_Utils_Sort::DONTCARE,
419 ],
420 ];
421
422 self::$_columnHeaders = array_merge($pre, self::$_columnHeaders);
423 }
424 }
425 return self::$_columnHeaders;
426 }
427
428 /**
429 * Get sql query string.
430 *
431 * @return string
432 */
433 public function &getQuery() {
434 return $this->_query;
435 }
436
437 /**
438 * Name of export file.
439 *
440 * @param string $output
441 * Type of output.
442 *
443 * @return string
444 * name of the file
445 */
446 public function getExportFileName($output = 'csv') {
447 return ts('Pledge Search');
448 }
449
450}