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