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