Ian province abbreviation patch - issue 724
[civicrm-core.git] / CRM / Event / Selector / Search.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * This class is used to retrieve and display a range of
38 * contacts that match the given criteria (specifically for
39 * results of advanced search options.
40 *
41 */
42class CRM_Event_Selector_Search extends CRM_Core_Selector_Base implements CRM_Core_Selector_API {
43
44 /**
45 * This defines two actions- View and Edit.
46 *
47 * @var array
6a488035
TO
48 */
49 static $_links = NULL;
50
51 /**
100fef9d 52 * We use desc to remind us what that column is, name is used in the tpl
6a488035
TO
53 *
54 * @var array
6a488035
TO
55 */
56 static $_columnHeaders;
57
58 /**
59 * Properties of contact we're interested in displaying
60 * @var array
6a488035
TO
61 */
62 static $_properties = array(
63 'contact_id',
64 'contact_type',
65 'sort_name',
66 'event_id',
67 'participant_status_id',
68 'event_title',
69 'participant_fee_level',
70 'participant_id',
71 'event_start_date',
72 'event_end_date',
73 'event_type_id',
74 'modified_date',
75 'participant_is_test',
76 'participant_role_id',
77 'participant_register_date',
78 'participant_fee_amount',
79 'participant_fee_currency',
80 'participant_status',
81 'participant_role',
82 'participant_campaign_id',
83 );
84
85 /**
100fef9d 86 * Are we restricting ourselves to a single contact
6a488035 87 *
6a488035
TO
88 * @var boolean
89 */
90 protected $_single = FALSE;
91
92 /**
100fef9d 93 * Are we restricting ourselves to a single contact
6a488035 94 *
6a488035
TO
95 * @var boolean
96 */
97 protected $_limit = NULL;
98
99 /**
100fef9d 100 * What context are we being invoked from
6a488035 101 *
6a488035
TO
102 * @var string
103 */
104 protected $_context = NULL;
105
106 /**
100fef9d 107 * What component context are we being invoked from
6a488035 108 *
6a488035
TO
109 * @var string
110 */
111 protected $_compContext = NULL;
112
113 /**
100fef9d 114 * QueryParams is the array returned by exportValues called on
6a488035
TO
115 * the HTML_QuickForm_Controller for that page.
116 *
117 * @var array
6a488035
TO
118 */
119 public $_queryParams;
120
121 /**
fe482240 122 * Represent the type of selector.
6a488035
TO
123 *
124 * @var int
6a488035
TO
125 */
126 protected $_action;
127
128 /**
fe482240 129 * The additional clause that we restrict the search with.
6a488035
TO
130 *
131 * @var string
132 */
133 protected $_eventClause = NULL;
134
135 /**
fe482240 136 * The query object.
6a488035
TO
137 *
138 * @var string
139 */
140 protected $_query;
141
142 /**
fe482240 143 * Class constructor.
6a488035 144 *
d4dd1e85
TO
145 * @param array $queryParams
146 * Array of parameters for query.
da6b46f4 147 * @param \const|int $action - action of search basic or advanced.
d4dd1e85
TO
148 * @param string $eventClause
149 * If the caller wants to further restrict the search (used in participations).
150 * @param bool $single
151 * Are we dealing only with one contact?.
152 * @param int $limit
153 * How many participations do we want returned.
6a488035 154 *
da6b46f4
EM
155 * @param string $context
156 * @param null $compContext
157 *
158 * @return \CRM_Event_Selector_Search
6a488035 159 */
2da40d21 160 public function __construct(
ddca8f33 161 &$queryParams,
0479b4c8 162 $action = CRM_Core_Action::NONE,
6a488035 163 $eventClause = NULL,
0479b4c8
TO
164 $single = FALSE,
165 $limit = NULL,
166 $context = 'search',
6a488035
TO
167 $compContext = NULL
168 ) {
169 // submitted form values
170 $this->_queryParams = &$queryParams;
171
353ffa53
TO
172 $this->_single = $single;
173 $this->_limit = $limit;
174 $this->_context = $context;
6a488035
TO
175 $this->_compContext = $compContext;
176
177 $this->_eventClause = $eventClause;
178
179 // type of selector
180 $this->_action = $action;
181
182 $this->_query = new CRM_Contact_BAO_Query($this->_queryParams,
183 CRM_Event_BAO_Query::defaultReturnProperties(CRM_Contact_BAO_Query::MODE_EVENT,
184 FALSE
185 ),
186 NULL, FALSE, FALSE,
187 CRM_Contact_BAO_Query::MODE_EVENT
188 );
189 $this->_query->_distinctComponentClause = " civicrm_participant.id";
190 $this->_query->_groupByComponentClause = " GROUP BY civicrm_participant.id ";
191 }
6a488035
TO
192
193 /**
fe482240 194 * Can be used to alter the number of participation returned from a buildForm hook.
6a488035 195 *
d4dd1e85
TO
196 * @param int $limit
197 * How many participations do we want returned.
6a488035 198 */
00be9182 199 public function setLimit($limit) {
6a488035
TO
200 $this->_limit = $limit;
201 }
202
203 /**
204 * This method returns the links that are given for each search row.
205 * currently the links added for each row are
206 *
207 * - View
208 * - Edit
209 *
77b97be7
EM
210 * @param null $qfKey
211 * @param null $context
212 * @param null $compContext
213 *
6a488035 214 * @return array
6a488035 215 */
00be9182 216 public static function &links($qfKey = NULL, $context = NULL, $compContext = NULL) {
6a488035
TO
217 $extraParams = NULL;
218 if ($compContext) {
219 $extraParams .= "&compContext={$compContext}";
220 }
221 elseif ($context == 'search') {
222 $extraParams .= '&compContext=participant';
223 }
224
225 if ($qfKey) {
226 $extraParams .= "&key={$qfKey}";
227 }
228
6a488035
TO
229 if (!(self::$_links)) {
230 self::$_links = array(
231 CRM_Core_Action::VIEW => array(
232 'name' => ts('View'),
233 'url' => 'civicrm/contact/view/participant',
234 'qs' => 'reset=1&id=%%id%%&cid=%%cid%%&action=view&context=%%cxt%%&selectedChild=event' . $extraParams,
235 'title' => ts('View Participation'),
236 ),
237 CRM_Core_Action::UPDATE => array(
238 'name' => ts('Edit'),
239 'url' => 'civicrm/contact/view/participant',
240 'qs' => 'reset=1&action=update&id=%%id%%&cid=%%cid%%&context=%%cxt%%' . $extraParams,
241 'title' => ts('Edit Participation'),
242 ),
243 CRM_Core_Action::DELETE => array(
244 'name' => ts('Delete'),
245 'url' => 'civicrm/contact/view/participant',
246 'qs' => 'reset=1&action=delete&id=%%id%%&cid=%%cid%%&context=%%cxt%%' . $extraParams,
247 'title' => ts('Delete Participation'),
248 ),
249 );
250 }
251 return self::$_links;
252 }
6a488035
TO
253
254 /**
100fef9d 255 * Getter for array of the parameters required for creating pager.
6a488035 256 *
fd31fa4c 257 * @param $action
c490a46a 258 * @param array $params
6a488035 259 */
00be9182 260 public function getPagerParams($action, &$params) {
6a488035
TO
261 $params['status'] = ts('Event') . ' %%StatusMessage%%';
262 $params['csvString'] = NULL;
263 if ($this->_limit) {
264 $params['rowCount'] = $this->_limit;
265 }
266 else {
267 $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT;
268 }
269
270 $params['buttonTop'] = 'PagerTopButton';
271 $params['buttonBottom'] = 'PagerBottomButton';
272 }
6a488035
TO
273
274 /**
275 * Returns total number of rows for the query.
276 *
277 * @param
278 *
a6c01b45
CW
279 * @return int
280 * Total number of rows
6a488035 281 */
00be9182 282 public function getTotalCount($action) {
6a488035
TO
283 return $this->_query->searchQuery(0, 0, NULL,
284 TRUE, FALSE,
285 FALSE, FALSE,
286 FALSE,
287 $this->_eventClause
288 );
289 }
290
291 /**
fe482240 292 * Returns all the rows in the given offset and rowCount.
6a488035 293 *
3f8d2862 294 * @param string $action
d4dd1e85
TO
295 * The action being performed.
296 * @param int $offset
297 * The row number to start from.
298 * @param int $rowCount
299 * The number of rows to return.
300 * @param string $sort
301 * The sql string that describes the sort order.
3f8d2862 302 * @param string $output
d4dd1e85 303 * What should the result set include (web/email/csv).
6a488035 304 *
a6c01b45
CW
305 * @return array
306 * rows in the given offset and rowCount
6a488035 307 */
00be9182 308 public function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
6a488035
TO
309 $result = $this->_query->searchQuery($offset, $rowCount, $sort,
310 FALSE, FALSE,
311 FALSE, FALSE,
312 FALSE,
313 $this->_eventClause
314 );
315 // process the result of the query
316 $rows = array();
317
318 //lets handle view, edit and delete separately. CRM-4418
319 $permissions = array(CRM_Core_Permission::VIEW);
320 if (CRM_Core_Permission::check('edit event participants')) {
321 $permissions[] = CRM_Core_Permission::EDIT;
322 }
323 if (CRM_Core_Permission::check('delete in CiviEvent')) {
324 $permissions[] = CRM_Core_Permission::DELETE;
325 }
326 $mask = CRM_Core_Action::mask($permissions);
327
353ffa53
TO
328 $statusTypes = CRM_Event_PseudoConstant::participantStatus();
329 $statusClasses = CRM_Event_PseudoConstant::participantStatusClass();
6a488035 330 $participantRoles = CRM_Event_PseudoConstant::participantRole();
353ffa53 331 $sep = CRM_Core_DAO::VALUE_SEPARATOR;
6a488035
TO
332
333 //get all campaigns.
334 $allCampaigns = CRM_Campaign_BAO_Campaign::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE);
335
336 while ($result->fetch()) {
337 $row = array();
338 // the columns we are interested in
339 foreach (self::$_properties as $property) {
340 if (isset($result->$property)) {
341 $row[$property] = $result->$property;
342 }
343 }
344
345 //carry campaign on selectors.
346 $row['campaign'] = CRM_Utils_Array::value($result->participant_campaign_id, $allCampaigns);
347 $row['campaign_id'] = $result->participant_campaign_id;
348
349 // gross hack to show extra information for pending status
350 $statusClass = NULL;
351 if ((isset($row['participant_status_id'])) &&
352 ($statusId = array_search($row['participant_status_id'], $statusTypes))
353 ) {
354 $statusClass = $statusClasses[$statusId];
355 }
356
357 $row['showConfirmUrl'] = ($statusClass == 'Pending') ? TRUE : FALSE;
358
a7488080 359 if (!empty($row['participant_is_test'])) {
6a488035
TO
360 $row['participant_status'] .= ' (' . ts('test') . ')';
361 }
362
363 $row['checkbox'] = CRM_Core_Form::CB_PREFIX . $result->participant_id;
fbc54416
PJ
364 $links = self::links($this->_key, $this->_context, $this->_compContext);
365
366 if ($statusTypes[$row['participant_status_id']] == 'Partially paid') {
367 $links[CRM_Core_Action::ADD] = array(
368 'name' => ts('Record Payment'),
81f3d017 369 'url' => 'civicrm/payment',
fbc54416
PJ
370 'qs' => 'reset=1&id=%%id%%&cid=%%cid%%&action=add&component=event',
371 'title' => ts('Record Payment'),
372 );
373 }
6a488035 374
e8cf3013
PJ
375 if ($statusTypes[$row['participant_status_id']] == 'Pending refund') {
376 $links[CRM_Core_Action::ADD] = array(
377 'name' => ts('Record Refund'),
81f3d017 378 'url' => 'civicrm/payment',
e8cf3013
PJ
379 'qs' => 'reset=1&id=%%id%%&cid=%%cid%%&action=add&component=event',
380 'title' => ts('Record Refund'),
381 );
382 }
383
fbc54416 384 $row['action'] = CRM_Core_Action::formLink($links,
6a488035
TO
385 $mask,
386 array(
387 'id' => $result->participant_id,
388 'cid' => $result->contact_id,
389 'cxt' => $this->_context,
87dab4a4
AH
390 ),
391 ts('more'),
392 FALSE,
393 'participant.selector.row',
394 'Participant',
395 $result->participant_id
6a488035
TO
396 );
397
0479b4c8 398 $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
399 );
400
401 $row['paid'] = CRM_Event_BAO_Event::isMonetary($row['event_id']);
402
a7488080 403 if (!empty($row['participant_fee_level'])) {
6a488035
TO
404 CRM_Event_BAO_Participant::fixEventLevel($row['participant_fee_level']);
405 }
406
407 if (CRM_Event_BAO_Event::usesPriceSet($row['event_id'])) {
408 // add line item details if applicable
409 $lineItems[$row['participant_id']] = CRM_Price_BAO_LineItem::getLineItems($row['participant_id']);
410 }
411
412 if (!empty($row['participant_role_id'])) {
413 $viewRoles = array();
414 foreach (explode($sep, $row['participant_role_id']) as $k => $v) {
415 $viewRoles[] = $participantRoles[$v];
416 }
417 $row['participant_role_id'] = implode(', ', $viewRoles);
418 }
419 $rows[] = $row;
420 }
421 CRM_Core_Selector_Controller::$_template->assign_by_ref('lineItems', $lineItems);
422
423 return $rows;
424 }
425
426 /**
1054415f 427 * @inheritDoc
6a488035 428 */
6a488035
TO
429 public function getQILL() {
430 return $this->_query->qill();
431 }
432
433 /**
100fef9d 434 * Returns the column headers as an array of tuples:
6a488035
TO
435 * (name, sortName (key to the sort array))
436 *
d4dd1e85
TO
437 * @param string $action
438 * The action being performed.
3f8d2862 439 * @param string $output
d4dd1e85 440 * What should the result set include (web/email/csv).
6a488035 441 *
a6c01b45
CW
442 * @return array
443 * the column headers that need to be displayed
6a488035
TO
444 */
445 public function &getColumnHeaders($action = NULL, $output = NULL) {
446 if (!isset(self::$_columnHeaders)) {
447 self::$_columnHeaders = array(
0479b4c8 448 array(
353ffa53 449 'name' => ts('Event'),
6a488035
TO
450 'sort' => 'event_title',
451 'direction' => CRM_Utils_Sort::DONTCARE,
452 ),
453 array(
454 'name' => ts('Fee Level'),
33a5a53d 455 'sort' => 'participant_fee_level',
6a488035
TO
456 'direction' => CRM_Utils_Sort::DONTCARE,
457 ),
458 array(
459 'name' => ts('Amount'),
33a5a53d 460 'sort' => 'participant_fee_amount',
6a488035
TO
461 'direction' => CRM_Utils_Sort::DONTCARE,
462 ),
463 array(
464 'name' => ts('Registered'),
465 'sort' => 'participant_register_date',
466 'direction' => CRM_Utils_Sort::DESCENDING,
467 ),
468 array(
469 'name' => ts('Event Date(s)'),
470 'sort' => 'event_start_date',
471 'direction' => CRM_Utils_Sort::DESCENDING,
472 ),
473 array(
474 'name' => ts('Status'),
475 'sort' => 'participant_status',
476 'direction' => CRM_Utils_Sort::DONTCARE,
477 ),
478 array(
479 'name' => ts('Role'),
480 'sort' => 'participant_role_id',
481 'direction' => CRM_Utils_Sort::DONTCARE,
482 ),
483 array('desc' => ts('Actions')),
484 );
485
486 if (!$this->_single) {
487 $pre = array(
488 array('desc' => ts('Contact Type')),
489 array(
490 'name' => ts('Participant'),
491 'sort' => 'sort_name',
492 'direction' => CRM_Utils_Sort::DONTCARE,
493 ),
494 );
495 self::$_columnHeaders = array_merge($pre, self::$_columnHeaders);
496 }
497 }
498 return self::$_columnHeaders;
499 }
500
0cf587a7
EM
501 /**
502 * @return mixed
503 */
00be9182 504 public function alphabetQuery() {
6a488035
TO
505 return $this->_query->searchQuery(NULL, NULL, NULL, FALSE, FALSE, TRUE);
506 }
507
0cf587a7
EM
508 /**
509 * @return string
510 */
00be9182 511 public function &getQuery() {
6a488035
TO
512 return $this->_query;
513 }
514
515 /**
100fef9d 516 * Name of export file.
6a488035 517 *
d4dd1e85
TO
518 * @param string $output
519 * Type of output.
6a488035 520 *
a6c01b45
CW
521 * @return string
522 * name of the file
6a488035 523 */
00be9182 524 public function getExportFileName($output = 'csv') {
6a488035
TO
525 return ts('CiviCRM Event Search');
526 }
96025800 527
6a488035 528}