Merge remote-tracking branch 'origin/abtest' into master-abtest
[civicrm-core.git] / CRM / Contribute / Selector / Search.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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_Contribute_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 'contribution_id',
68 'contact_type',
69 'sort_name',
70 'amount_level',
71 'total_amount',
72 'financial_type',
73 'contribution_source',
74 'receive_date',
75 'thankyou_date',
76 'contribution_status_id',
77 'contribution_status',
78 'cancel_date',
79 'product_name',
80 'is_test',
81 'contribution_recur_id',
82 'receipt_date',
83 'membership_id',
84 'currency',
85 'contribution_campaign_id',
86 'contribution_soft_credit_name',
87 'contribution_soft_credit_contact_id',
88 'contribution_soft_credit_amount',
89 'contribution_soft_credit_type',
90 );
91
92 /**
93 * are we restricting ourselves to a single contact
94 *
95 * @access protected
96 * @var boolean
97 */
98 protected $_single = FALSE;
99
100 /**
101 * are we restricting ourselves to a single contact
102 *
103 * @access protected
104 * @var boolean
105 */
106 protected $_limit = NULL;
107
108 /**
109 * what context are we being invoked from
110 *
111 * @access protected
112 * @var string
113 */
114 protected $_context = NULL;
115
116 /**
117 * what component context are we being invoked from
118 *
119 * @access protected
120 * @var string
121 */
122 protected $_compContext = NULL;
123
124 /**
125 * queryParams is the array returned by exportValues called on
126 * the HTML_QuickForm_Controller for that page.
127 *
128 * @var array
129 * @access protected
130 */
131 public $_queryParams;
132
133 /**
134 * represent the type of selector
135 *
136 * @var int
137 * @access protected
138 */
139 protected $_action;
140
141 /**
142 * The additional clause that we restrict the search with
143 *
144 * @var string
145 */
146 protected $_contributionClause = NULL;
147
148 /**
149 * The query object
150 *
151 * @var string
152 */
153 protected $_query;
154
155 protected $_includeSoftCredits = FALSE;
156
157 /**
158 * Class constructor
159 *
160 * @param array $queryParams array of parameters for query
161 * @param \const|int $action - action of search basic or advanced.
162 * @param string $contributionClause if the caller wants to further restrict the search (used in contributions)
163 * @param boolean $single are we dealing only with one contact?
164 * @param int $limit how many contributions do we want returned
165 *
166 * @param string $context
167 * @param null $compContext
168 *
169 * @return \CRM_Contribute_Selector_Search
170 @access public
171 */
172 function __construct(&$queryParams,
173 $action = CRM_Core_Action::NONE,
174 $contributionClause = NULL,
175 $single = FALSE,
176 $limit = NULL,
177 $context = 'search',
178 $compContext = NULL
179 ) {
180
181 // submitted form values
182 $this->_queryParams = &$queryParams;
183
184 $this->_single = $single;
185 $this->_limit = $limit;
186 $this->_context = $context;
187 $this->_compContext = $compContext;
188
189 $this->_contributionClause = $contributionClause;
190
191 // type of selector
192 $this->_action = $action;
193
194 $this->_includeSoftCredits = CRM_Contribute_BAO_Query::isSoftCreditOptionEnabled($this->_queryParams);
195 $this->_query = new CRM_Contact_BAO_Query(
196 $this->_queryParams,
197 CRM_Contribute_BAO_Query::defaultReturnProperties(
198 CRM_Contact_BAO_Query::MODE_CONTRIBUTE,
199 FALSE
200 ),
201 NULL, FALSE, FALSE,
202 CRM_Contact_BAO_Query::MODE_CONTRIBUTE
203 );
204 if ($this->_includeSoftCredits) {
205 $this->_query->_rowCountClause = " count(civicrm_contribution.id)";
206 $this->_query->_groupByComponentClause = " GROUP BY contribution_search_scredit_combined.id, contribution_search_scredit_combined.contact_id, contribution_search_scredit_combined.scredit_id ";
207 } else {
208 $this->_query->_distinctComponentClause = " civicrm_contribution.id";
209 $this->_query->_groupByComponentClause = " GROUP BY civicrm_contribution.id ";
210 }
211 }
212 //end of constructor
213
214 /**
215 * This method returns the links that are given for each search row.
216 * currently the links added for each row are
217 *
218 * - View
219 * - Edit
220 *
221 * @param null $componentId
222 * @param null $componentAction
223 * @param null $key
224 * @param null $compContext
225 *
226 * @return array
227 * @access public
228 */
229 static function &links($componentId = NULL, $componentAction = NULL, $key = NULL, $compContext = NULL) {
230 $extraParams = NULL;
231 if ($componentId) {
232 $extraParams = "&compId={$componentId}&compAction={$componentAction}";
233 }
234 if ($compContext) {
235 $extraParams .= "&compContext={$compContext}";
236 }
237 if ($key) {
238 $extraParams .= "&key={$key}";
239 }
240
241 if (!(self::$_links)) {
242 self::$_links = array(
243 CRM_Core_Action::VIEW => array(
244 'name' => ts('View'),
245 'url' => 'civicrm/contact/view/contribution',
246 'qs' => "reset=1&id=%%id%%&cid=%%cid%%&action=view&context=%%cxt%%&selectedChild=contribute{$extraParams}",
247 'title' => ts('View Contribution'),
248 ),
249 CRM_Core_Action::UPDATE => array(
250 'name' => ts('Edit'),
251 'url' => 'civicrm/contact/view/contribution',
252 'qs' => "reset=1&action=update&id=%%id%%&cid=%%cid%%&context=%%cxt%%{$extraParams}",
253 'title' => ts('Edit Contribution'),
254 ),
255 CRM_Core_Action::DELETE => array(
256 'name' => ts('Delete'),
257 'url' => 'civicrm/contact/view/contribution',
258 'qs' => "reset=1&action=delete&id=%%id%%&cid=%%cid%%&context=%%cxt%%{$extraParams}",
259 'title' => ts('Delete Contribution'),
260 ),
261 );
262 }
263 return self::$_links;
264 }
265 //end of function
266
267 /**
268 * getter for array of the parameters required for creating pager.
269 *
270 * @param $action
271 * @param $params
272 * @internal param $
273 * @access public
274 */
275 function getPagerParams($action, &$params) {
276 $params['status'] = ts('Contribution') . ' %%StatusMessage%%';
277 $params['csvString'] = NULL;
278 if ($this->_limit) {
279 $params['rowCount'] = $this->_limit;
280 }
281 else {
282 $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT;
283 }
284
285 $params['buttonTop'] = 'PagerTopButton';
286 $params['buttonBottom'] = 'PagerBottomButton';
287 }
288 //end of function
289
290 /**
291 * Returns total number of rows for the query.
292 *
293 * @param
294 *
295 * @return int Total number of rows
296 * @access public
297 */
298 function getTotalCount($action) {
299 return $this->_query->searchQuery(0, 0, NULL,
300 TRUE, FALSE,
301 FALSE, FALSE,
302 FALSE,
303 $this->_contributionClause
304 );
305 }
306
307 /**
308 * returns all the rows in the given offset and rowCount
309 *
310 * @param enum $action the action being performed
311 * @param int $offset the row number to start from
312 * @param int $rowCount the number of rows to return
313 * @param string $sort the sql string that describes the sort order
314 * @param enum $output what should the result set include (web/email/csv)
315 *
316 * @return int the total number of rows for this action
317 */
318 function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
319 if ($this->_includeSoftCredits) {
320 // especial sort order when rows include soft credits
321 $sort = "civicrm_contribution.receive_date DESC, civicrm_contribution.id, civicrm_contribution_soft.id";
322 }
323 $result = $this->_query->searchQuery($offset, $rowCount, $sort,
324 FALSE, FALSE,
325 FALSE, FALSE,
326 FALSE,
327 $this->_contributionClause
328 );
329 // process the result of the query
330 $rows = array();
331
332 //CRM-4418 check for view/edit/delete
333 $permissions = array(CRM_Core_Permission::VIEW);
334 if (CRM_Core_Permission::check('edit contributions')) {
335 $permissions[] = CRM_Core_Permission::EDIT;
336 }
337 if (CRM_Core_Permission::check('delete in CiviContribute')) {
338 $permissions[] = CRM_Core_Permission::DELETE;
339 }
340 $mask = CRM_Core_Action::mask($permissions);
341
342 $qfKey = $this->_key;
343 $componentId = $componentContext = NULL;
344 if ($this->_context != 'contribute') {
345 $qfKey = CRM_Utils_Request::retrieve('key', 'String', CRM_Core_DAO::$_nullObject);
346 $componentId = CRM_Utils_Request::retrieve('id', 'Positive', CRM_Core_DAO::$_nullObject);
347 $componentAction = CRM_Utils_Request::retrieve('action', 'String', CRM_Core_DAO::$_nullObject);
348 $componentContext = CRM_Utils_Request::retrieve('compContext', 'String', CRM_Core_DAO::$_nullObject);
349
350 if (!$componentContext &&
351 $this->_compContext
352 ) {
353 $componentContext = $this->_compContext;
354 $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', CRM_Core_DAO::$_nullObject, NULL, FALSE, 'REQUEST');
355 }
356 }
357
358 // get all contribution status
359 $contributionStatuses = CRM_Core_OptionGroup::values('contribution_status',
360 FALSE, FALSE, FALSE, NULL, 'name', FALSE
361 );
362
363 //get all campaigns.
364 $allCampaigns = CRM_Campaign_BAO_Campaign::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE);
365
366 While ($result->fetch()) {
367 $row = array();
368 // the columns we are interested in
369 foreach (self::$_properties as $property) {
370 if (property_exists($result, $property)) {
371 $row[$property] = $result->$property;
372 }
373 }
374
375 //carry campaign on selectors.
376 $row['campaign'] = CRM_Utils_Array::value($result->contribution_campaign_id, $allCampaigns);
377 $row['campaign_id'] = $result->contribution_campaign_id;
378
379 // add contribution status name
380 $row['contribution_status_name'] = CRM_Utils_Array::value($row['contribution_status_id'],
381 $contributionStatuses
382 );
383
384 if ($result->is_pay_later && CRM_Utils_Array::value('contribution_status_name', $row) == 'Pending') {
385 $row['contribution_status'] .= ' (' . ts('Pay Later') . ')';
386 }
387 elseif (CRM_Utils_Array::value('contribution_status_name', $row) == 'Pending') {
388 $row['contribution_status'] .= ' (' . ts('Incomplete Transaction') . ')';
389 }
390
391 if ($row['is_test']) {
392 $row['financial_type'] = $row['financial_type'] . ' (' . ts('test') . ')';
393 }
394
395 $row['checkbox'] = CRM_Core_Form::CB_PREFIX . $result->contribution_id;
396
397 $actions = array(
398 'id' => $result->contribution_id,
399 'cid' => $result->contact_id,
400 'cxt' => $this->_context,
401 );
402
403 $row['action'] = CRM_Core_Action::formLink(
404 self::links($componentId,
405 $componentAction,
406 $qfKey,
407 $componentContext
408 ),
409 $mask, $actions,
410 ts('more'),
411 FALSE,
412 'contribution.selector.row',
413 'Contribution',
414 $result->contribution_id
415 );
416
417 $row['contact_type'] = CRM_Contact_BAO_Contact_Utils::getImage($result->contact_sub_type ?
418 $result->contact_sub_type : $result->contact_type, FALSE, $result->contact_id
419 );
420
421 if (!empty($row['amount_level'])) {
422 CRM_Event_BAO_Participant::fixEventLevel($row['amount_level']);
423 }
424
425 $rows[] = $row;
426 }
427
428 return $rows;
429 }
430
431 /**
432 *
433 * @return array $qill which contains an array of strings
434 * @access public
435 */
436
437 // the current internationalisation is bad, but should more or less work
438 // for most of "European" languages
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 self::$_columnHeaders = array(
455 array(
456 'name' => $this->_includeSoftCredits ? ts('Contribution Amount') : ts('Amount'),
457 'sort' => 'total_amount',
458 'direction' => CRM_Utils_Sort::DONTCARE,
459 ),
460 );
461 if ($this->_includeSoftCredits) {
462 self::$_columnHeaders =
463 array_merge(
464 self::$_columnHeaders,
465 array(
466 array(
467 'name' => ts('Soft Credit Amount'),
468 'sort' => 'contribution_soft_credit_amount',
469 'direction' => CRM_Utils_Sort::DONTCARE,
470 )
471 )
472 );
473 }
474 self::$_columnHeaders =
475 array_merge(
476 self::$_columnHeaders,
477 array(
478 array(
479 'name' => ts('Type'),
480 'sort' => 'financial_type',
481 'direction' => CRM_Utils_Sort::DONTCARE,
482 ),
483 array(
484 'name' => ts('Source'),
485 'sort' => 'contribution_source',
486 'direction' => CRM_Utils_Sort::DONTCARE,
487 ),
488 array(
489 'name' => ts('Received'),
490 'sort' => 'receive_date',
491 'direction' => CRM_Utils_Sort::DESCENDING,
492 ),
493 array(
494 'name' => ts('Thank-you Sent'),
495 'sort' => 'thankyou_date',
496 'direction' => CRM_Utils_Sort::DONTCARE,
497 ),
498 array(
499 'name' => ts('Status'),
500 'sort' => 'contribution_status',
501 'direction' => CRM_Utils_Sort::DONTCARE,
502 ),
503 array(
504 'name' => ts('Premium'),
505 'sort' => 'product_name',
506 'direction' => CRM_Utils_Sort::DONTCARE,
507 ),
508 )
509 );
510 if (!$this->_single) {
511 $pre = array(
512 array('desc' => ts('Contact Type')),
513 array(
514 'name' => ts('Name'),
515 'sort' => 'sort_name',
516 'direction' => CRM_Utils_Sort::DONTCARE,
517 ),
518 );
519 self::$_columnHeaders = array_merge($pre, self::$_columnHeaders);
520 }
521 if ($this->_includeSoftCredits) {
522 self::$_columnHeaders =
523 array_merge(
524 self::$_columnHeaders,
525 array(
526 array(
527 'name' => ts('Soft Credit For'),
528 'sort' => 'contribution_soft_credit_name',
529 'direction' => CRM_Utils_Sort::DONTCARE,
530 ),
531 array(
532 'name' => ts('Soft Credit Type'),
533 'sort' => 'contribution_soft_credit_type',
534 'direction' => CRM_Utils_Sort::ASCENDING,
535 ),
536 )
537 );
538 }
539 self::$_columnHeaders =
540 array_merge(
541 self::$_columnHeaders, array(
542 array('desc' => ts('Actions'))
543 )
544 );
545 return self::$_columnHeaders;
546 }
547
548 /**
549 * @return mixed
550 */
551 function alphabetQuery() {
552 return $this->_query->searchQuery(NULL, NULL, NULL, FALSE, FALSE, TRUE);
553 }
554
555 /**
556 * @return string
557 */
558 function &getQuery() {
559 return $this->_query;
560 }
561
562 /**
563 * name of export file.
564 *
565 * @param string $output type of output
566 *
567 * @return string name of the file
568 */
569 function getExportFileName($output = 'csv') {
570 return ts('CiviCRM Contribution Search');
571 }
572
573 /**
574 * @return mixed
575 */
576 function getSummary() {
577 return $this->_query->summaryContribution($this->_context);
578 }
579 }
580