fix version and year
[civicrm-core.git] / CRM / Contribute / Selector / Search.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
fa938177 6 | Copyright CiviCRM LLC (c) 2004-2016 |
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
fa938177 31 * @copyright CiviCRM LLC (c) 2004-2016
6a488035
TO
32 */
33
34/**
35 * This class is used to retrieve and display a range of
36 * contacts that match the given criteria (specifically for
37 * results of advanced search options.
38 *
39 */
40class CRM_Contribute_Selector_Search extends CRM_Core_Selector_Base implements CRM_Core_Selector_API {
41
42 /**
43 * This defines two actions- View and Edit.
44 *
45 * @var array
6a488035
TO
46 */
47 static $_links = NULL;
48
49 /**
100fef9d 50 * We use desc to remind us what that column is, name is used in the tpl
6a488035
TO
51 *
52 * @var array
6a488035
TO
53 */
54 static $_columnHeaders;
55
56 /**
57 * Properties of contact we're interested in displaying
58 * @var array
6a488035
TO
59 */
60 static $_properties = array(
61 'contact_id',
62 'contribution_id',
63 'contact_type',
64 'sort_name',
65 'amount_level',
66 'total_amount',
e777c2b6 67 'financial_type',
6a488035
TO
68 'contribution_source',
69 'receive_date',
70 'thankyou_date',
71 'contribution_status_id',
72 'contribution_status',
73 'cancel_date',
74 'product_name',
75 'is_test',
76 'contribution_recur_id',
77 'receipt_date',
78 'membership_id',
79 'currency',
80 'contribution_campaign_id',
36bf52ef 81 'contribution_soft_credit_name',
d4e2b978 82 'contribution_soft_credit_contact_id',
36bf52ef
DS
83 'contribution_soft_credit_amount',
84 'contribution_soft_credit_type',
6a488035
TO
85 );
86
87 /**
100fef9d 88 * Are we restricting ourselves to a single contact
6a488035 89 *
6a488035
TO
90 * @var boolean
91 */
92 protected $_single = FALSE;
93
94 /**
100fef9d 95 * Are we restricting ourselves to a single contact
6a488035 96 *
6a488035
TO
97 * @var boolean
98 */
99 protected $_limit = NULL;
100
101 /**
100fef9d 102 * What context are we being invoked from
6a488035 103 *
6a488035
TO
104 * @var string
105 */
106 protected $_context = NULL;
107
108 /**
100fef9d 109 * What component context are we being invoked from
6a488035 110 *
6a488035
TO
111 * @var string
112 */
113 protected $_compContext = NULL;
114
115 /**
100fef9d 116 * QueryParams is the array returned by exportValues called on
6a488035
TO
117 * the HTML_QuickForm_Controller for that page.
118 *
119 * @var array
6a488035
TO
120 */
121 public $_queryParams;
122
123 /**
100fef9d 124 * Represent the type of selector
6a488035
TO
125 *
126 * @var int
6a488035
TO
127 */
128 protected $_action;
129
130 /**
131 * The additional clause that we restrict the search with
132 *
133 * @var string
134 */
135 protected $_contributionClause = NULL;
136
137 /**
138 * The query object
139 *
140 * @var string
141 */
142 protected $_query;
143
36bf52ef
DS
144 protected $_includeSoftCredits = FALSE;
145
6a488035 146 /**
fe482240 147 * Class constructor.
6a488035 148 *
014c4014
TO
149 * @param array $queryParams
150 * Array of parameters for query.
da6b46f4 151 * @param \const|int $action - action of search basic or advanced.
014c4014
TO
152 * @param string $contributionClause
153 * If the caller wants to further restrict the search (used in contributions).
154 * @param bool $single
155 * Are we dealing only with one contact?.
156 * @param int $limit
157 * How many contributions do we want returned.
6a488035 158 *
da6b46f4
EM
159 * @param string $context
160 * @param null $compContext
161 *
162 * @return \CRM_Contribute_Selector_Search
6a488035 163 */
8d7a9d07 164 public function __construct(
a13f3d8c 165 &$queryParams,
874c9be7 166 $action = CRM_Core_Action::NONE,
6a488035 167 $contributionClause = NULL,
874c9be7
TO
168 $single = FALSE,
169 $limit = NULL,
170 $context = 'search',
171 $compContext = NULL
6a488035
TO
172 ) {
173
174 // submitted form values
175 $this->_queryParams = &$queryParams;
176
353ffa53
TO
177 $this->_single = $single;
178 $this->_limit = $limit;
179 $this->_context = $context;
6a488035
TO
180 $this->_compContext = $compContext;
181
182 $this->_contributionClause = $contributionClause;
183
184 // type of selector
185 $this->_action = $action;
186
d4e2b978 187 $this->_includeSoftCredits = CRM_Contribute_BAO_Query::isSoftCreditOptionEnabled($this->_queryParams);
36bf52ef
DS
188 $this->_query = new CRM_Contact_BAO_Query(
189 $this->_queryParams,
c7940124 190 CRM_Contribute_BAO_Query::selectorReturnProperties(),
6a488035
TO
191 NULL, FALSE, FALSE,
192 CRM_Contact_BAO_Query::MODE_CONTRIBUTE
193 );
17caeafe 194 // @todo the function CRM_Contribute_BAO_Query::isSoftCreditOptionEnabled should handle this
195 // can we remove? if not why not?
3dbf477c
DS
196 if ($this->_includeSoftCredits) {
197 $this->_query->_rowCountClause = " count(civicrm_contribution.id)";
ed81a415 198 $this->_query->_groupByComponentClause = " GROUP BY contribution_search_scredit_combined.id, contribution_search_scredit_combined.contact_id, contribution_search_scredit_combined.scredit_id ";
0db6c3e1
TO
199 }
200 else {
3dbf477c
DS
201 $this->_query->_distinctComponentClause = " civicrm_contribution.id";
202 $this->_query->_groupByComponentClause = " GROUP BY civicrm_contribution.id ";
203 }
6a488035 204 }
6a488035
TO
205
206 /**
207 * This method returns the links that are given for each search row.
208 * currently the links added for each row are
209 *
210 * - View
211 * - Edit
212 *
100fef9d 213 * @param int $componentId
fd31fa4c
EM
214 * @param null $componentAction
215 * @param null $key
216 * @param null $compContext
217 *
6a488035 218 * @return array
6a488035 219 */
00be9182 220 public static function &links($componentId = NULL, $componentAction = NULL, $key = NULL, $compContext = NULL) {
6a488035
TO
221 $extraParams = NULL;
222 if ($componentId) {
223 $extraParams = "&compId={$componentId}&compAction={$componentAction}";
224 }
225 if ($compContext) {
226 $extraParams .= "&compContext={$compContext}";
227 }
228 if ($key) {
229 $extraParams .= "&key={$key}";
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/contribution',
237 'qs' => "reset=1&id=%%id%%&cid=%%cid%%&action=view&context=%%cxt%%&selectedChild=contribute{$extraParams}",
238 'title' => ts('View Contribution'),
239 ),
240 CRM_Core_Action::UPDATE => array(
241 'name' => ts('Edit'),
242 'url' => 'civicrm/contact/view/contribution',
243 'qs' => "reset=1&action=update&id=%%id%%&cid=%%cid%%&context=%%cxt%%{$extraParams}",
244 'title' => ts('Edit Contribution'),
245 ),
246 CRM_Core_Action::DELETE => array(
247 'name' => ts('Delete'),
248 'url' => 'civicrm/contact/view/contribution',
249 'qs' => "reset=1&action=delete&id=%%id%%&cid=%%cid%%&context=%%cxt%%{$extraParams}",
250 'title' => ts('Delete Contribution'),
251 ),
252 );
253 }
254 return self::$_links;
255 }
6a488035
TO
256
257 /**
100fef9d 258 * Getter for array of the parameters required for creating pager.
6a488035 259 *
da6b46f4 260 * @param $action
c490a46a 261 * @param array $params
6a488035 262 */
00be9182 263 public function getPagerParams($action, &$params) {
6a488035
TO
264 $params['status'] = ts('Contribution') . ' %%StatusMessage%%';
265 $params['csvString'] = NULL;
266 if ($this->_limit) {
267 $params['rowCount'] = $this->_limit;
268 }
269 else {
270 $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT;
271 }
272
273 $params['buttonTop'] = 'PagerTopButton';
274 $params['buttonBottom'] = 'PagerBottomButton';
275 }
6a488035
TO
276
277 /**
278 * Returns total number of rows for the query.
279 *
7fe37828 280 * @param string $action
6a488035 281 *
a6c01b45
CW
282 * @return int
283 * Total number of rows
6a488035 284 */
00be9182 285 public function getTotalCount($action) {
6a488035
TO
286 return $this->_query->searchQuery(0, 0, NULL,
287 TRUE, FALSE,
288 FALSE, FALSE,
289 FALSE,
290 $this->_contributionClause
291 );
292 }
293
294 /**
fe482240 295 * Returns all the rows in the given offset and rowCount.
6a488035 296 *
3f8d2862 297 * @param string $action
014c4014
TO
298 * The action being performed.
299 * @param int $offset
300 * The row number to start from.
301 * @param int $rowCount
302 * The number of rows to return.
303 * @param string $sort
304 * The sql string that describes the sort order.
3f8d2862 305 * @param string $output
014c4014 306 * What should the result set include (web/email/csv).
6a488035 307 *
a6c01b45
CW
308 * @return int
309 * the total number of rows for this action
6a488035 310 */
00be9182 311 public function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
4f3846df
DS
312 if ($this->_includeSoftCredits) {
313 // especial sort order when rows include soft credits
8f7c2652 314 $sort = $sort->orderBy() . ", civicrm_contribution.id, civicrm_contribution_soft.id";
4f3846df 315 }
6a488035
TO
316 $result = $this->_query->searchQuery($offset, $rowCount, $sort,
317 FALSE, FALSE,
318 FALSE, FALSE,
319 FALSE,
320 $this->_contributionClause
321 );
322 // process the result of the query
323 $rows = array();
324
325 //CRM-4418 check for view/edit/delete
326 $permissions = array(CRM_Core_Permission::VIEW);
327 if (CRM_Core_Permission::check('edit contributions')) {
328 $permissions[] = CRM_Core_Permission::EDIT;
329 }
330 if (CRM_Core_Permission::check('delete in CiviContribute')) {
331 $permissions[] = CRM_Core_Permission::DELETE;
332 }
333 $mask = CRM_Core_Action::mask($permissions);
334
335 $qfKey = $this->_key;
336 $componentId = $componentContext = NULL;
337 if ($this->_context != 'contribute') {
98df3f98 338 // @todo explain the significance of context & why we do not get these i that context.
353ffa53
TO
339 $qfKey = CRM_Utils_Request::retrieve('key', 'String', CRM_Core_DAO::$_nullObject);
340 $componentId = CRM_Utils_Request::retrieve('id', 'Positive', CRM_Core_DAO::$_nullObject);
341 $componentAction = CRM_Utils_Request::retrieve('action', 'String', CRM_Core_DAO::$_nullObject);
6a488035
TO
342 $componentContext = CRM_Utils_Request::retrieve('compContext', 'String', CRM_Core_DAO::$_nullObject);
343
344 if (!$componentContext &&
345 $this->_compContext
346 ) {
98df3f98 347 // @todo explain when this condition might occur.
6a488035
TO
348 $componentContext = $this->_compContext;
349 $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', CRM_Core_DAO::$_nullObject, NULL, FALSE, 'REQUEST');
350 }
98df3f98 351 // CRM-17628 for some reason qfKey is not always set when searching from contribution search.
352 // as a result if the edit link is opened using right-click + open in new tab
353 // then the browser is not returned to the search results on save.
354 // This is an effort to getting the qfKey without, sadly, understanding the intent of those who came before me.
355 if (empty($qfKey)) {
356 $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', CRM_Core_DAO::$_nullObject, NULL, FALSE, 'REQUEST');
357 }
6a488035
TO
358 }
359
360 // get all contribution status
361 $contributionStatuses = CRM_Core_OptionGroup::values('contribution_status',
362 FALSE, FALSE, FALSE, NULL, 'name', FALSE
363 );
364
365 //get all campaigns.
366 $allCampaigns = CRM_Campaign_BAO_Campaign::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE);
367
874c9be7 368 while ($result->fetch()) {
b501fa94
E
369 $links = self::links($componentId,
370 $componentAction,
371 $qfKey,
372 $componentContext
373 );
a964bc8e 374 $checkLineItem = FALSE;
6a488035 375 $row = array();
a964bc8e 376 // Now check for lineItems
4323dc6c
PN
377 if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus()) {
378 $lineItems = CRM_Price_BAO_LineItem::getLineItemsByContributionID($result->id);
40c655aa 379 foreach ($lineItems as $items) {
4323dc6c
PN
380 if (!CRM_Core_Permission::check('view contributions of type ' . CRM_Contribute_PseudoConstant::financialType($items['financial_type_id']))) {
381 $checkLineItem = TRUE;
382 break;
383 }
384 if (!CRM_Core_Permission::check('edit contributions of type ' . CRM_Contribute_PseudoConstant::financialType($items['financial_type_id']))) {
385 unset($links[CRM_Core_Action::UPDATE]);
386 }
387 if (!CRM_Core_Permission::check('delete contributions of type ' . CRM_Contribute_PseudoConstant::financialType($items['financial_type_id']))) {
388 unset($links[CRM_Core_Action::DELETE]);
389 }
a964bc8e 390 }
4323dc6c
PN
391 if ($checkLineItem) {
392 continue;
393 }
394 if (!CRM_Core_Permission::check('edit contributions of type ' . CRM_Contribute_PseudoConstant::financialType($result->financial_type_id))) {
2d0b158e 395 unset($links[CRM_Core_Action::UPDATE]);
48da6700 396 }
4323dc6c 397 if (!CRM_Core_Permission::check('delete contributions of type ' . CRM_Contribute_PseudoConstant::financialType($result->financial_type_id))) {
2d0b158e 398 unset($links[CRM_Core_Action::DELETE]);
48da6700 399 }
a964bc8e 400 }
6a488035
TO
401 // the columns we are interested in
402 foreach (self::$_properties as $property) {
403 if (property_exists($result, $property)) {
404 $row[$property] = $result->$property;
405 }
406 }
407
408 //carry campaign on selectors.
5deb2f24 409 // @todo - I can't find any evidence that 'carrying' the campaign on selectors actually
410 // results in it being displayed anywhere so why do we do this???
6a488035
TO
411 $row['campaign'] = CRM_Utils_Array::value($result->contribution_campaign_id, $allCampaigns);
412 $row['campaign_id'] = $result->contribution_campaign_id;
413
414 // add contribution status name
415 $row['contribution_status_name'] = CRM_Utils_Array::value($row['contribution_status_id'],
416 $contributionStatuses
417 );
418
419 if ($result->is_pay_later && CRM_Utils_Array::value('contribution_status_name', $row) == 'Pending') {
420 $row['contribution_status'] .= ' (' . ts('Pay Later') . ')';
421 }
422 elseif (CRM_Utils_Array::value('contribution_status_name', $row) == 'Pending') {
423 $row['contribution_status'] .= ' (' . ts('Incomplete Transaction') . ')';
424 }
425
426 if ($row['is_test']) {
e777c2b6 427 $row['financial_type'] = $row['financial_type'] . ' (' . ts('test') . ')';
6a488035
TO
428 }
429
430 $row['checkbox'] = CRM_Core_Form::CB_PREFIX . $result->contribution_id;
431
6a488035
TO
432 $actions = array(
433 'id' => $result->contribution_id,
434 'cid' => $result->contact_id,
435 'cxt' => $this->_context,
436 );
437
e777c2b6 438 $row['action'] = CRM_Core_Action::formLink(
b501fa94 439 $links,
87dab4a4
AH
440 $mask, $actions,
441 ts('more'),
442 FALSE,
443 'contribution.selector.row',
444 'Contribution',
445 $result->contribution_id
6a488035
TO
446 );
447
874c9be7 448 $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
449 );
450
a7488080 451 if (!empty($row['amount_level'])) {
6a488035
TO
452 CRM_Event_BAO_Participant::fixEventLevel($row['amount_level']);
453 }
454
455 $rows[] = $row;
456 }
457
458 return $rows;
459 }
460
461 /**
1054415f 462 * @inheritDoc
6a488035 463 */
6a488035
TO
464 public function getQILL() {
465 return $this->_query->qill();
466 }
467
468 /**
100fef9d 469 * Returns the column headers as an array of tuples:
6a488035
TO
470 * (name, sortName (key to the sort array))
471 *
014c4014
TO
472 * @param string $action
473 * The action being performed.
3f8d2862 474 * @param string $output
014c4014 475 * What should the result set include (web/email/csv).
6a488035 476 *
a6c01b45
CW
477 * @return array
478 * the column headers that need to be displayed
6a488035
TO
479 */
480 public function &getColumnHeaders($action = NULL, $output = NULL) {
054785dc 481 $pre = array();
36bf52ef
DS
482 self::$_columnHeaders = array(
483 array(
4f3846df 484 'name' => $this->_includeSoftCredits ? ts('Contribution Amount') : ts('Amount'),
36bf52ef
DS
485 'sort' => 'total_amount',
486 'direction' => CRM_Utils_Sort::DONTCARE,
487 ),
36bf52ef 488 );
4f3846df 489 if ($this->_includeSoftCredits) {
8d7a9d07
CB
490 self::$_columnHeaders
491 = array_merge(
fd31fa4c 492 self::$_columnHeaders,
4f3846df
DS
493 array(
494 array(
495 'name' => ts('Soft Credit Amount'),
496 'sort' => 'contribution_soft_credit_amount',
497 'direction' => CRM_Utils_Sort::DONTCARE,
21dfd5f5 498 ),
4f3846df
DS
499 )
500 );
501 }
8d7a9d07
CB
502 self::$_columnHeaders
503 = array_merge(
fd31fa4c 504 self::$_columnHeaders,
4f3846df
DS
505 array(
506 array(
507 'name' => ts('Type'),
353ffa53 508 'sort' => 'financial_type',
4f3846df
DS
509 'direction' => CRM_Utils_Sort::DONTCARE,
510 ),
511 array(
512 'name' => ts('Source'),
513 'sort' => 'contribution_source',
514 'direction' => CRM_Utils_Sort::DONTCARE,
515 ),
516 array(
517 'name' => ts('Received'),
518 'sort' => 'receive_date',
519 'direction' => CRM_Utils_Sort::DESCENDING,
520 ),
521 array(
522 'name' => ts('Thank-you Sent'),
523 'sort' => 'thankyou_date',
524 'direction' => CRM_Utils_Sort::DONTCARE,
525 ),
526 array(
527 'name' => ts('Status'),
33a5a53d 528 'sort' => 'contribution_status',
4f3846df
DS
529 'direction' => CRM_Utils_Sort::DONTCARE,
530 ),
531 array(
532 'name' => ts('Premium'),
533 'sort' => 'product_name',
534 'direction' => CRM_Utils_Sort::DONTCARE,
535 ),
536 )
537 );
36bf52ef
DS
538 if (!$this->_single) {
539 $pre = array(
6a488035 540 array(
36bf52ef
DS
541 'name' => ts('Name'),
542 'sort' => 'sort_name',
6a488035
TO
543 'direction' => CRM_Utils_Sort::DONTCARE,
544 ),
6a488035 545 );
36bf52ef 546 }
d4c0653f 547 self::$_columnHeaders = array_merge($pre, self::$_columnHeaders);
36bf52ef 548 if ($this->_includeSoftCredits) {
006389de 549 self::$_columnHeaders = array_merge(
8d7a9d07
CB
550 self::$_columnHeaders,
551 array(
6a488035 552 array(
8d7a9d07
CB
553 'name' => ts('Soft Credit For'),
554 'sort' => 'contribution_soft_credit_name',
555 'direction' => CRM_Utils_Sort::DONTCARE,
556 ),
557 array(
558 'name' => ts('Soft Credit Type'),
559 'sort' => 'contribution_soft_credit_type',
560 'direction' => CRM_Utils_Sort::ASCENDING,
561 ),
562 )
563 );
6a488035 564 }
8d7a9d07
CB
565 self::$_columnHeaders
566 = array_merge(
4f3846df 567 self::$_columnHeaders, array(
21dfd5f5 568 array('desc' => ts('Actions')),
4f3846df
DS
569 )
570 );
c740f57b 571 CRM_Core_Smarty::singleton()->assign('softCreditColumns', $this->_includeSoftCredits);
6a488035
TO
572 return self::$_columnHeaders;
573 }
574
186c9c17
EM
575 /**
576 * @return mixed
577 */
00be9182 578 public function alphabetQuery() {
6a488035
TO
579 return $this->_query->searchQuery(NULL, NULL, NULL, FALSE, FALSE, TRUE);
580 }
581
186c9c17
EM
582 /**
583 * @return string
584 */
00be9182 585 public function &getQuery() {
6a488035
TO
586 return $this->_query;
587 }
588
589 /**
100fef9d 590 * Name of export file.
6a488035 591 *
014c4014
TO
592 * @param string $output
593 * Type of output.
6a488035 594 *
a6c01b45
CW
595 * @return string
596 * name of the file
6a488035 597 */
00be9182 598 public function getExportFileName($output = 'csv') {
6a488035
TO
599 return ts('CiviCRM Contribution Search');
600 }
601
186c9c17
EM
602 /**
603 * @return mixed
604 */
00be9182 605 public function getSummary() {
6a488035
TO
606 return $this->_query->summaryContribution($this->_context);
607 }
96025800 608
6a488035 609}