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