Merge pull request #15796 from seamuslee001/dev_core_183_requirements
[civicrm-core.git] / CRM / Event / 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_Event_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 'event_id',
51 'participant_status_id',
52 'event_title',
53 'participant_fee_level',
54 'participant_id',
55 'event_start_date',
56 'event_end_date',
57 'event_type_id',
58 'modified_date',
59 'participant_is_test',
60 'participant_role_id',
61 'participant_register_date',
62 'participant_fee_amount',
63 'participant_fee_currency',
64 'participant_status',
65 'participant_role',
66 'participant_campaign_id',
67 ];
68
69 /**
70 * Are we restricting ourselves to a single contact
71 *
72 * @var bool
73 */
74 protected $_single = FALSE;
75
76 /**
77 * Are we restricting ourselves to a single contact
78 *
79 * @var bool
80 */
81 protected $_limit = NULL;
82
83 /**
84 * What context are we being invoked from
85 *
86 * @var string
87 */
88 protected $_context = NULL;
89
90 /**
91 * What component context are we being invoked from
92 *
93 * @var string
94 */
95 protected $_compContext = NULL;
96
97 /**
98 * QueryParams is the array returned by exportValues called on
99 * the HTML_QuickForm_Controller for that page.
100 *
101 * @var array
102 */
103 public $_queryParams;
104
105 /**
106 * Represent the type of selector.
107 *
108 * @var int
109 */
110 protected $_action;
111
112 /**
113 * The additional clause that we restrict the search with.
114 *
115 * @var string
116 */
117 protected $_eventClause = NULL;
118
119 /**
120 * The query object.
121 *
122 * @var string
123 */
124 protected $_query;
125
126 /**
127 * Class constructor.
128 *
129 * @param array $queryParams
130 * Array of parameters for query.
131 * @param \const|int $action - action of search basic or advanced.
132 * @param string $eventClause
133 * If the caller wants to further restrict the search (used in participations).
134 * @param bool $single
135 * Are we dealing only with one contact?.
136 * @param int $limit
137 * How many participations do we want returned.
138 *
139 * @param string $context
140 * @param null $compContext
141 *
142 * @return \CRM_Event_Selector_Search
143 */
144 public function __construct(
145 &$queryParams,
146 $action = CRM_Core_Action::NONE,
147 $eventClause = NULL,
148 $single = FALSE,
149 $limit = NULL,
150 $context = 'search',
151 $compContext = NULL
152 ) {
153 // submitted form values
154 $this->_queryParams = &$queryParams;
155
156 $this->_single = $single;
157 $this->_limit = $limit;
158 $this->_context = $context;
159 $this->_compContext = $compContext;
160
161 $this->_eventClause = $eventClause;
162
163 // type of selector
164 $this->_action = $action;
165
166 $this->_query = new CRM_Contact_BAO_Query($this->_queryParams,
167 CRM_Event_BAO_Query::defaultReturnProperties(CRM_Contact_BAO_Query::MODE_EVENT,
168 FALSE
169 ),
170 NULL, FALSE, FALSE,
171 CRM_Contact_BAO_Query::MODE_EVENT
172 );
173 $this->_query->_distinctComponentClause = " civicrm_participant.id";
174 $this->_query->_groupByComponentClause = " GROUP BY civicrm_participant.id ";
175 }
176
177 /**
178 * Can be used to alter the number of participation returned from a buildForm hook.
179 *
180 * @param int $limit
181 * How many participations do we want returned.
182 */
183 public function setLimit($limit) {
184 $this->_limit = $limit;
185 }
186
187 /**
188 * This method returns the links that are given for each search row.
189 * currently the links added for each row are
190 *
191 * - View
192 * - Edit
193 *
194 * @param null $qfKey
195 * @param null $context
196 * @param null $compContext
197 *
198 * @return array
199 */
200 public static function &links($qfKey = NULL, $context = NULL, $compContext = NULL) {
201 $extraParams = NULL;
202 if ($compContext) {
203 $extraParams .= "&compContext={$compContext}";
204 }
205 elseif ($context == 'search') {
206 $extraParams .= '&compContext=participant';
207 }
208
209 if ($qfKey) {
210 $extraParams .= "&key={$qfKey}";
211 }
212
213 if (!(self::$_links)) {
214 self::$_links = [
215 CRM_Core_Action::VIEW => [
216 'name' => ts('View'),
217 'url' => 'civicrm/contact/view/participant',
218 'qs' => 'reset=1&id=%%id%%&cid=%%cid%%&action=view&context=%%cxt%%&selectedChild=event' . $extraParams,
219 'title' => ts('View Participation'),
220 ],
221 CRM_Core_Action::UPDATE => [
222 'name' => ts('Edit'),
223 'url' => 'civicrm/contact/view/participant',
224 'qs' => 'reset=1&action=update&id=%%id%%&cid=%%cid%%&context=%%cxt%%' . $extraParams,
225 'title' => ts('Edit Participation'),
226 ],
227 CRM_Core_Action::DELETE => [
228 'name' => ts('Delete'),
229 'url' => 'civicrm/contact/view/participant',
230 'qs' => 'reset=1&action=delete&id=%%id%%&cid=%%cid%%&context=%%cxt%%' . $extraParams,
231 'title' => ts('Delete Participation'),
232 ],
233 ];
234 }
235 return self::$_links;
236 }
237
238 /**
239 * Getter for array of the parameters required for creating pager.
240 *
241 * @param $action
242 * @param array $params
243 */
244 public function getPagerParams($action, &$params) {
245 $params['status'] = ts('Event') . ' %%StatusMessage%%';
246 $params['csvString'] = NULL;
247 if ($this->_limit) {
248 $params['rowCount'] = $this->_limit;
249 }
250 else {
251 $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT;
252 }
253
254 $params['buttonTop'] = 'PagerTopButton';
255 $params['buttonBottom'] = 'PagerBottomButton';
256 }
257
258 /**
259 * Returns total number of rows for the query.
260 *
261 * @param int $action
262 *
263 * @return int
264 * Total number of rows
265 */
266 public function getTotalCount($action) {
267 return $this->_query->searchQuery(0, 0, NULL,
268 TRUE, FALSE,
269 FALSE, FALSE,
270 FALSE,
271 $this->_eventClause
272 );
273 }
274
275 /**
276 * Returns all the rows in the given offset and rowCount.
277 *
278 * @param string $action
279 * The action being performed.
280 * @param int $offset
281 * The row number to start from.
282 * @param int $rowCount
283 * The number of rows to return.
284 * @param string $sort
285 * The sql string that describes the sort order.
286 * @param string $output
287 * What should the result set include (web/email/csv).
288 *
289 * @return array
290 * rows in the given offset and rowCount
291 */
292 public function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
293 $result = $this->_query->searchQuery($offset, $rowCount, $sort,
294 FALSE, FALSE,
295 FALSE, FALSE,
296 FALSE,
297 $this->_eventClause
298 );
299 // process the result of the query
300 $rows = [];
301
302 //lets handle view, edit and delete separately. CRM-4418
303 $permissions = [CRM_Core_Permission::VIEW];
304 if (CRM_Core_Permission::check('edit event participants')) {
305 $permissions[] = CRM_Core_Permission::EDIT;
306 }
307 if (CRM_Core_Permission::check('delete in CiviEvent')) {
308 $permissions[] = CRM_Core_Permission::DELETE;
309 }
310 $mask = CRM_Core_Action::mask($permissions);
311
312 $statusTypes = CRM_Event_PseudoConstant::participantStatus();
313 $statusClasses = CRM_Event_PseudoConstant::participantStatusClass();
314 $participantRoles = CRM_Event_PseudoConstant::participantRole();
315 $sep = CRM_Core_DAO::VALUE_SEPARATOR;
316
317 //get all campaigns.
318 $allCampaigns = CRM_Campaign_BAO_Campaign::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE);
319
320 while ($result->fetch()) {
321 $row = [];
322 // the columns we are interested in
323 foreach (self::$_properties as $property) {
324 if (isset($result->$property)) {
325 $row[$property] = $result->$property;
326 }
327 }
328
329 // Skip registration if event_id is NULL
330 if (empty($row['event_id'])) {
331 Civi::log()->warning('Participant record without event ID. You have invalid data in your database!');
332 continue;
333 }
334
335 //carry campaign on selectors.
336 $row['campaign'] = $allCampaigns[$result->participant_campaign_id] ?? NULL;
337 $row['campaign_id'] = $result->participant_campaign_id;
338
339 // gross hack to show extra information for pending status
340 $statusClass = NULL;
341 if ((isset($row['participant_status_id'])) &&
342 ($statusId = array_search($row['participant_status_id'], $statusTypes))
343 ) {
344 $statusClass = $statusClasses[$statusId];
345 }
346
347 $row['showConfirmUrl'] = $statusClass == 'Pending';
348
349 if (!empty($row['participant_is_test'])) {
350 $row['participant_status'] = CRM_Core_TestEntity::appendTestText($row['participant_status']);
351 }
352
353 $row['checkbox'] = CRM_Core_Form::CB_PREFIX . $result->participant_id;
354 $links = self::links($this->_key, $this->_context, $this->_compContext);
355
356 if ($statusTypes[$row['participant_status_id']] == 'Partially paid') {
357 $links[CRM_Core_Action::ADD] = [
358 'name' => ts('Record Payment'),
359 'url' => 'civicrm/payment',
360 'qs' => 'reset=1&id=%%id%%&cid=%%cid%%&action=add&component=event',
361 'title' => ts('Record Payment'),
362 ];
363 if (CRM_Core_Config::isEnabledBackOfficeCreditCardPayments()) {
364 $links[CRM_Core_Action::BASIC] = [
365 'name' => ts('Submit Credit Card payment'),
366 'url' => 'civicrm/payment/add',
367 'qs' => 'reset=1&id=%%id%%&cid=%%cid%%&action=add&component=event&mode=live',
368 'title' => ts('Submit Credit Card payment'),
369 ];
370 }
371 }
372
373 if ($statusTypes[$row['participant_status_id']] == 'Pending refund') {
374 $links[CRM_Core_Action::ADD] = [
375 'name' => ts('Record Refund'),
376 'url' => 'civicrm/payment',
377 'qs' => 'reset=1&id=%%id%%&cid=%%cid%%&action=add&component=event',
378 'title' => ts('Record Refund'),
379 ];
380 }
381
382 // CRM-20879: Show 'Transfer or Cancel' action only if logged in user
383 // have 'edit event participants' permission and participant status
384 // is not Cancelled or Transferred
385 if (in_array(CRM_Core_Permission::EDIT, $permissions) &&
386 !in_array($statusTypes[$row['participant_status_id']], ['Cancelled', 'Transferred'])
387 ) {
388 $links[] = [
389 'name' => ts('Transfer or Cancel'),
390 'url' => 'civicrm/event/selfsvcupdate',
391 'qs' => 'reset=1&pid=%%id%%&is_backoffice=1&cs=' . CRM_Contact_BAO_Contact_Utils::generateChecksum($result->contact_id, NULL, 'inf'),
392 'title' => ts('Transfer or Cancel'),
393 ];
394 }
395
396 $row['action'] = CRM_Core_Action::formLink($links,
397 $mask,
398 [
399 'id' => $result->participant_id,
400 'cid' => $result->contact_id,
401 'cxt' => $this->_context,
402 ],
403 ts('more'),
404 FALSE,
405 'participant.selector.row',
406 'Participant',
407 $result->participant_id
408 );
409
410 $row['contact_type'] = CRM_Contact_BAO_Contact_Utils::getImage($result->contact_sub_type ? $result->contact_sub_type : $result->contact_type, FALSE, $result->contact_id
411 );
412
413 $row['paid'] = CRM_Event_BAO_Event::isMonetary($row['event_id']);
414
415 if (!empty($row['participant_fee_level'])) {
416 CRM_Event_BAO_Participant::fixEventLevel($row['participant_fee_level']);
417 }
418
419 if (CRM_Event_BAO_Event::usesPriceSet($row['event_id'])) {
420 // add line item details if applicable
421 $lineItems[$row['participant_id']] = CRM_Price_BAO_LineItem::getLineItems($row['participant_id']);
422 }
423
424 if (!empty($row['participant_role_id'])) {
425 $viewRoles = [];
426 foreach (explode($sep, $row['participant_role_id']) as $k => $v) {
427 $viewRoles[] = $participantRoles[$v];
428 }
429 $row['participant_role_id'] = implode(', ', $viewRoles);
430 }
431 $rows[] = $row;
432 }
433 CRM_Core_Selector_Controller::$_template->assign_by_ref('lineItems', $lineItems);
434
435 return $rows;
436 }
437
438 /**
439 * @inheritDoc
440 */
441 public function getQILL() {
442 return $this->_query->qill();
443 }
444
445 /**
446 * Returns the column headers as an array of tuples:
447 * (name, sortName (key to the sort array))
448 *
449 * @param string $action
450 * The action being performed.
451 * @param string $output
452 * What should the result set include (web/email/csv).
453 *
454 * @return array
455 * the column headers that need to be displayed
456 */
457 public function &getColumnHeaders($action = NULL, $output = NULL) {
458 if (!isset(self::$_columnHeaders)) {
459 self::$_columnHeaders = [
460 [
461 'name' => ts('Event'),
462 'sort' => 'event_title',
463 'direction' => CRM_Utils_Sort::DONTCARE,
464 ],
465 [
466 'name' => ts('Fee Level'),
467 'sort' => 'participant_fee_level',
468 'direction' => CRM_Utils_Sort::DONTCARE,
469 ],
470 [
471 'name' => ts('Amount'),
472 'sort' => 'participant_fee_amount',
473 'direction' => CRM_Utils_Sort::DONTCARE,
474 ],
475 [
476 'name' => ts('Registered'),
477 'sort' => 'participant_register_date',
478 'direction' => CRM_Utils_Sort::DESCENDING,
479 ],
480 [
481 'name' => ts('Event Date(s)'),
482 'sort' => 'event_start_date',
483 'direction' => CRM_Utils_Sort::DESCENDING,
484 ],
485 [
486 'name' => ts('Status'),
487 'sort' => 'participant_status',
488 'direction' => CRM_Utils_Sort::DONTCARE,
489 ],
490 [
491 'name' => ts('Role'),
492 'sort' => 'participant_role_id',
493 'direction' => CRM_Utils_Sort::DONTCARE,
494 ],
495 ['desc' => ts('Actions')],
496 ];
497
498 if (!$this->_single) {
499 $pre = [
500 ['desc' => ts('Contact Type')],
501 [
502 'name' => ts('Participant'),
503 'sort' => 'sort_name',
504 'direction' => CRM_Utils_Sort::DONTCARE,
505 ],
506 ];
507 self::$_columnHeaders = array_merge($pre, self::$_columnHeaders);
508 }
509 }
510 return self::$_columnHeaders;
511 }
512
513 /**
514 * @return mixed
515 */
516 public function alphabetQuery() {
517 return $this->_query->alphabetQuery();
518 }
519
520 /**
521 * @return string
522 */
523 public function &getQuery() {
524 return $this->_query;
525 }
526
527 /**
528 * Name of export file.
529 *
530 * @param string $output
531 * Type of output.
532 *
533 * @return string
534 * name of the file
535 */
536 public function getExportFileName($output = 'csv') {
537 return ts('CiviCRM Event Search');
538 }
539
540 }