Merge pull request #15796 from seamuslee001/dev_core_183_requirements
[civicrm-core.git] / CRM / Event / 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_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
6a488035 32 */
90b461f1 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 */
90b461f1 40 public static $_columnHeaders;
6a488035
TO
41
42 /**
43 * Properties of contact we're interested in displaying
44 * @var array
6a488035 45 */
90b461f1 46 public static $_properties = [
6a488035
TO
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',
be2fb01f 67 ];
6a488035
TO
68
69 /**
100fef9d 70 * Are we restricting ourselves to a single contact
6a488035 71 *
d51c6add 72 * @var bool
6a488035
TO
73 */
74 protected $_single = FALSE;
75
76 /**
100fef9d 77 * Are we restricting ourselves to a single contact
6a488035 78 *
d51c6add 79 * @var bool
6a488035
TO
80 */
81 protected $_limit = NULL;
82
83 /**
100fef9d 84 * What context are we being invoked from
6a488035 85 *
6a488035
TO
86 * @var string
87 */
88 protected $_context = NULL;
89
90 /**
100fef9d 91 * What component context are we being invoked from
6a488035 92 *
6a488035
TO
93 * @var string
94 */
95 protected $_compContext = NULL;
96
97 /**
100fef9d 98 * QueryParams is the array returned by exportValues called on
6a488035
TO
99 * the HTML_QuickForm_Controller for that page.
100 *
101 * @var array
6a488035
TO
102 */
103 public $_queryParams;
104
105 /**
fe482240 106 * Represent the type of selector.
6a488035
TO
107 *
108 * @var int
6a488035
TO
109 */
110 protected $_action;
111
112 /**
fe482240 113 * The additional clause that we restrict the search with.
6a488035
TO
114 *
115 * @var string
116 */
117 protected $_eventClause = NULL;
118
119 /**
fe482240 120 * The query object.
6a488035
TO
121 *
122 * @var string
123 */
124 protected $_query;
125
126 /**
fe482240 127 * Class constructor.
6a488035 128 *
d4dd1e85
TO
129 * @param array $queryParams
130 * Array of parameters for query.
da6b46f4 131 * @param \const|int $action - action of search basic or advanced.
d4dd1e85
TO
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.
6a488035 138 *
da6b46f4
EM
139 * @param string $context
140 * @param null $compContext
141 *
142 * @return \CRM_Event_Selector_Search
6a488035 143 */
2da40d21 144 public function __construct(
ddca8f33 145 &$queryParams,
0479b4c8 146 $action = CRM_Core_Action::NONE,
6a488035 147 $eventClause = NULL,
0479b4c8
TO
148 $single = FALSE,
149 $limit = NULL,
150 $context = 'search',
6a488035
TO
151 $compContext = NULL
152 ) {
153 // submitted form values
154 $this->_queryParams = &$queryParams;
155
353ffa53
TO
156 $this->_single = $single;
157 $this->_limit = $limit;
158 $this->_context = $context;
6a488035
TO
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 }
6a488035
TO
176
177 /**
fe482240 178 * Can be used to alter the number of participation returned from a buildForm hook.
6a488035 179 *
d4dd1e85
TO
180 * @param int $limit
181 * How many participations do we want returned.
6a488035 182 */
00be9182 183 public function setLimit($limit) {
6a488035
TO
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 *
77b97be7
EM
194 * @param null $qfKey
195 * @param null $context
196 * @param null $compContext
197 *
6a488035 198 * @return array
6a488035 199 */
00be9182 200 public static function &links($qfKey = NULL, $context = NULL, $compContext = NULL) {
6a488035
TO
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
6a488035 213 if (!(self::$_links)) {
be2fb01f
CW
214 self::$_links = [
215 CRM_Core_Action::VIEW => [
6a488035
TO
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'),
be2fb01f
CW
220 ],
221 CRM_Core_Action::UPDATE => [
6a488035
TO
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'),
be2fb01f
CW
226 ],
227 CRM_Core_Action::DELETE => [
6a488035
TO
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'),
be2fb01f
CW
232 ],
233 ];
6a488035
TO
234 }
235 return self::$_links;
236 }
6a488035
TO
237
238 /**
100fef9d 239 * Getter for array of the parameters required for creating pager.
6a488035 240 *
fd31fa4c 241 * @param $action
c490a46a 242 * @param array $params
6a488035 243 */
00be9182 244 public function getPagerParams($action, &$params) {
6a488035
TO
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 }
6a488035
TO
257
258 /**
259 * Returns total number of rows for the query.
260 *
ad37ac8e 261 * @param int $action
6a488035 262 *
a6c01b45
CW
263 * @return int
264 * Total number of rows
6a488035 265 */
00be9182 266 public function getTotalCount($action) {
6a488035
TO
267 return $this->_query->searchQuery(0, 0, NULL,
268 TRUE, FALSE,
269 FALSE, FALSE,
270 FALSE,
271 $this->_eventClause
272 );
273 }
274
275 /**
fe482240 276 * Returns all the rows in the given offset and rowCount.
6a488035 277 *
3f8d2862 278 * @param string $action
d4dd1e85
TO
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.
3f8d2862 286 * @param string $output
d4dd1e85 287 * What should the result set include (web/email/csv).
6a488035 288 *
a6c01b45
CW
289 * @return array
290 * rows in the given offset and rowCount
6a488035 291 */
00be9182 292 public function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
6a488035
TO
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
be2fb01f 300 $rows = [];
6a488035
TO
301
302 //lets handle view, edit and delete separately. CRM-4418
be2fb01f 303 $permissions = [CRM_Core_Permission::VIEW];
6a488035
TO
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
353ffa53
TO
312 $statusTypes = CRM_Event_PseudoConstant::participantStatus();
313 $statusClasses = CRM_Event_PseudoConstant::participantStatusClass();
6a488035 314 $participantRoles = CRM_Event_PseudoConstant::participantRole();
353ffa53 315 $sep = CRM_Core_DAO::VALUE_SEPARATOR;
6a488035
TO
316
317 //get all campaigns.
318 $allCampaigns = CRM_Campaign_BAO_Campaign::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE);
319
320 while ($result->fetch()) {
be2fb01f 321 $row = [];
6a488035
TO
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
cf79ac58
MW
329 // Skip registration if event_id is NULL
330 if (empty($row['event_id'])) {
23380ec7 331 Civi::log()->warning('Participant record without event ID. You have invalid data in your database!');
cf79ac58
MW
332 continue;
333 }
334
6a488035 335 //carry campaign on selectors.
9c1bc317 336 $row['campaign'] = $allCampaigns[$result->participant_campaign_id] ?? NULL;
6a488035
TO
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
f7dbf5d9 347 $row['showConfirmUrl'] = $statusClass == 'Pending';
6a488035 348
a7488080 349 if (!empty($row['participant_is_test'])) {
cd355351 350 $row['participant_status'] = CRM_Core_TestEntity::appendTestText($row['participant_status']);
6a488035
TO
351 }
352
353 $row['checkbox'] = CRM_Core_Form::CB_PREFIX . $result->participant_id;
fbc54416
PJ
354 $links = self::links($this->_key, $this->_context, $this->_compContext);
355
356 if ($statusTypes[$row['participant_status_id']] == 'Partially paid') {
be2fb01f 357 $links[CRM_Core_Action::ADD] = [
fbc54416 358 'name' => ts('Record Payment'),
81f3d017 359 'url' => 'civicrm/payment',
fbc54416
PJ
360 'qs' => 'reset=1&id=%%id%%&cid=%%cid%%&action=add&component=event',
361 'title' => ts('Record Payment'),
be2fb01f 362 ];
059326f3 363 if (CRM_Core_Config::isEnabledBackOfficeCreditCardPayments()) {
be2fb01f 364 $links[CRM_Core_Action::BASIC] = [
059326f3
E
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'),
be2fb01f 369 ];
059326f3 370 }
fbc54416 371 }
6a488035 372
e8cf3013 373 if ($statusTypes[$row['participant_status_id']] == 'Pending refund') {
be2fb01f 374 $links[CRM_Core_Action::ADD] = [
e8cf3013 375 'name' => ts('Record Refund'),
81f3d017 376 'url' => 'civicrm/payment',
e8cf3013
PJ
377 'qs' => 'reset=1&id=%%id%%&cid=%%cid%%&action=add&component=event',
378 'title' => ts('Record Refund'),
be2fb01f 379 ];
e8cf3013
PJ
380 }
381
e219d53b 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) &&
be2fb01f 386 !in_array($statusTypes[$row['participant_status_id']], ['Cancelled', 'Transferred'])
e219d53b 387 ) {
be2fb01f 388 $links[] = [
e219d53b 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'),
be2fb01f 393 ];
e219d53b 394 }
395
fbc54416 396 $row['action'] = CRM_Core_Action::formLink($links,
6a488035 397 $mask,
be2fb01f 398 [
6a488035
TO
399 'id' => $result->participant_id,
400 'cid' => $result->contact_id,
401 'cxt' => $this->_context,
be2fb01f 402 ],
87dab4a4
AH
403 ts('more'),
404 FALSE,
405 'participant.selector.row',
406 'Participant',
407 $result->participant_id
6a488035
TO
408 );
409
0479b4c8 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
6a488035
TO
411 );
412
413 $row['paid'] = CRM_Event_BAO_Event::isMonetary($row['event_id']);
414
a7488080 415 if (!empty($row['participant_fee_level'])) {
6a488035
TO
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'])) {
be2fb01f 425 $viewRoles = [];
6a488035
TO
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 /**
1054415f 439 * @inheritDoc
6a488035 440 */
6a488035
TO
441 public function getQILL() {
442 return $this->_query->qill();
443 }
444
445 /**
100fef9d 446 * Returns the column headers as an array of tuples:
6a488035
TO
447 * (name, sortName (key to the sort array))
448 *
d4dd1e85
TO
449 * @param string $action
450 * The action being performed.
3f8d2862 451 * @param string $output
d4dd1e85 452 * What should the result set include (web/email/csv).
6a488035 453 *
a6c01b45
CW
454 * @return array
455 * the column headers that need to be displayed
6a488035
TO
456 */
457 public function &getColumnHeaders($action = NULL, $output = NULL) {
458 if (!isset(self::$_columnHeaders)) {
be2fb01f
CW
459 self::$_columnHeaders = [
460 [
353ffa53 461 'name' => ts('Event'),
6a488035
TO
462 'sort' => 'event_title',
463 'direction' => CRM_Utils_Sort::DONTCARE,
be2fb01f
CW
464 ],
465 [
6a488035 466 'name' => ts('Fee Level'),
33a5a53d 467 'sort' => 'participant_fee_level',
6a488035 468 'direction' => CRM_Utils_Sort::DONTCARE,
be2fb01f
CW
469 ],
470 [
6a488035 471 'name' => ts('Amount'),
33a5a53d 472 'sort' => 'participant_fee_amount',
6a488035 473 'direction' => CRM_Utils_Sort::DONTCARE,
be2fb01f
CW
474 ],
475 [
6a488035
TO
476 'name' => ts('Registered'),
477 'sort' => 'participant_register_date',
478 'direction' => CRM_Utils_Sort::DESCENDING,
be2fb01f
CW
479 ],
480 [
6a488035
TO
481 'name' => ts('Event Date(s)'),
482 'sort' => 'event_start_date',
483 'direction' => CRM_Utils_Sort::DESCENDING,
be2fb01f
CW
484 ],
485 [
6a488035
TO
486 'name' => ts('Status'),
487 'sort' => 'participant_status',
488 'direction' => CRM_Utils_Sort::DONTCARE,
be2fb01f
CW
489 ],
490 [
6a488035
TO
491 'name' => ts('Role'),
492 'sort' => 'participant_role_id',
493 'direction' => CRM_Utils_Sort::DONTCARE,
be2fb01f
CW
494 ],
495 ['desc' => ts('Actions')],
496 ];
6a488035
TO
497
498 if (!$this->_single) {
be2fb01f
CW
499 $pre = [
500 ['desc' => ts('Contact Type')],
501 [
6a488035
TO
502 'name' => ts('Participant'),
503 'sort' => 'sort_name',
504 'direction' => CRM_Utils_Sort::DONTCARE,
be2fb01f
CW
505 ],
506 ];
6a488035
TO
507 self::$_columnHeaders = array_merge($pre, self::$_columnHeaders);
508 }
509 }
510 return self::$_columnHeaders;
511 }
512
0cf587a7
EM
513 /**
514 * @return mixed
515 */
00be9182 516 public function alphabetQuery() {
52cda5dc 517 return $this->_query->alphabetQuery();
6a488035
TO
518 }
519
0cf587a7
EM
520 /**
521 * @return string
522 */
00be9182 523 public function &getQuery() {
6a488035
TO
524 return $this->_query;
525 }
526
527 /**
100fef9d 528 * Name of export file.
6a488035 529 *
d4dd1e85
TO
530 * @param string $output
531 * Type of output.
6a488035 532 *
a6c01b45
CW
533 * @return string
534 * name of the file
6a488035 535 */
00be9182 536 public function getExportFileName($output = 'csv') {
6a488035
TO
537 return ts('CiviCRM Event Search');
538 }
96025800 539
6a488035 540}