CRM-17640 add follow up comments to discourage random brutal attacks on site performance
[civicrm-core.git] / CRM / Contribute / Selector / Search.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
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
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
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,
190 CRM_Contribute_BAO_Query::defaultReturnProperties(
191 CRM_Contact_BAO_Query::MODE_CONTRIBUTE,
f654cacf 192 FALSE
6a488035
TO
193 ),
194 NULL, FALSE, FALSE,
195 CRM_Contact_BAO_Query::MODE_CONTRIBUTE
196 );
3dbf477c
DS
197 if ($this->_includeSoftCredits) {
198 $this->_query->_rowCountClause = " count(civicrm_contribution.id)";
ed81a415 199 $this->_query->_groupByComponentClause = " GROUP BY contribution_search_scredit_combined.id, contribution_search_scredit_combined.contact_id, contribution_search_scredit_combined.scredit_id ";
0db6c3e1
TO
200 }
201 else {
3dbf477c
DS
202 $this->_query->_distinctComponentClause = " civicrm_contribution.id";
203 $this->_query->_groupByComponentClause = " GROUP BY civicrm_contribution.id ";
204 }
6a488035 205 }
6a488035
TO
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 *
100fef9d 214 * @param int $componentId
fd31fa4c
EM
215 * @param null $componentAction
216 * @param null $key
217 * @param null $compContext
218 *
6a488035 219 * @return array
6a488035 220 */
00be9182 221 public static function &links($componentId = NULL, $componentAction = NULL, $key = NULL, $compContext = NULL) {
6a488035
TO
222 $extraParams = NULL;
223 if ($componentId) {
224 $extraParams = "&compId={$componentId}&compAction={$componentAction}";
225 }
226 if ($compContext) {
227 $extraParams .= "&compContext={$compContext}";
228 }
229 if ($key) {
230 $extraParams .= "&key={$key}";
231 }
232
233 if (!(self::$_links)) {
234 self::$_links = array(
235 CRM_Core_Action::VIEW => array(
236 'name' => ts('View'),
237 'url' => 'civicrm/contact/view/contribution',
238 'qs' => "reset=1&id=%%id%%&cid=%%cid%%&action=view&context=%%cxt%%&selectedChild=contribute{$extraParams}",
239 'title' => ts('View Contribution'),
240 ),
241 CRM_Core_Action::UPDATE => array(
242 'name' => ts('Edit'),
243 'url' => 'civicrm/contact/view/contribution',
244 'qs' => "reset=1&action=update&id=%%id%%&cid=%%cid%%&context=%%cxt%%{$extraParams}",
245 'title' => ts('Edit Contribution'),
246 ),
247 CRM_Core_Action::DELETE => array(
248 'name' => ts('Delete'),
249 'url' => 'civicrm/contact/view/contribution',
250 'qs' => "reset=1&action=delete&id=%%id%%&cid=%%cid%%&context=%%cxt%%{$extraParams}",
251 'title' => ts('Delete Contribution'),
252 ),
253 );
254 }
255 return self::$_links;
256 }
6a488035
TO
257
258 /**
100fef9d 259 * Getter for array of the parameters required for creating pager.
6a488035 260 *
da6b46f4 261 * @param $action
c490a46a 262 * @param array $params
6a488035 263 */
00be9182 264 public function getPagerParams($action, &$params) {
6a488035
TO
265 $params['status'] = ts('Contribution') . ' %%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 }
6a488035
TO
277
278 /**
279 * Returns total number of rows for the query.
280 *
7fe37828 281 * @param string $action
6a488035 282 *
a6c01b45
CW
283 * @return int
284 * Total number of rows
6a488035 285 */
00be9182 286 public function getTotalCount($action) {
6a488035
TO
287 return $this->_query->searchQuery(0, 0, NULL,
288 TRUE, FALSE,
289 FALSE, FALSE,
290 FALSE,
291 $this->_contributionClause
292 );
293 }
294
295 /**
fe482240 296 * Returns all the rows in the given offset and rowCount.
6a488035 297 *
3f8d2862 298 * @param string $action
014c4014
TO
299 * The action being performed.
300 * @param int $offset
301 * The row number to start from.
302 * @param int $rowCount
303 * The number of rows to return.
304 * @param string $sort
305 * The sql string that describes the sort order.
3f8d2862 306 * @param string $output
014c4014 307 * What should the result set include (web/email/csv).
6a488035 308 *
a6c01b45
CW
309 * @return int
310 * the total number of rows for this action
6a488035 311 */
00be9182 312 public function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
4f3846df
DS
313 if ($this->_includeSoftCredits) {
314 // especial sort order when rows include soft credits
315 $sort = "civicrm_contribution.receive_date DESC, civicrm_contribution.id, civicrm_contribution_soft.id";
316 }
6a488035
TO
317 $result = $this->_query->searchQuery($offset, $rowCount, $sort,
318 FALSE, FALSE,
319 FALSE, FALSE,
320 FALSE,
321 $this->_contributionClause
322 );
323 // process the result of the query
324 $rows = array();
325
326 //CRM-4418 check for view/edit/delete
327 $permissions = array(CRM_Core_Permission::VIEW);
328 if (CRM_Core_Permission::check('edit contributions')) {
329 $permissions[] = CRM_Core_Permission::EDIT;
330 }
331 if (CRM_Core_Permission::check('delete in CiviContribute')) {
332 $permissions[] = CRM_Core_Permission::DELETE;
333 }
334 $mask = CRM_Core_Action::mask($permissions);
335
336 $qfKey = $this->_key;
337 $componentId = $componentContext = NULL;
338 if ($this->_context != 'contribute') {
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 ) {
347 $componentContext = $this->_compContext;
348 $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', CRM_Core_DAO::$_nullObject, NULL, FALSE, 'REQUEST');
349 }
350 }
351
352 // get all contribution status
353 $contributionStatuses = CRM_Core_OptionGroup::values('contribution_status',
354 FALSE, FALSE, FALSE, NULL, 'name', FALSE
355 );
356
357 //get all campaigns.
358 $allCampaigns = CRM_Campaign_BAO_Campaign::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE);
359
874c9be7 360 while ($result->fetch()) {
b501fa94
E
361 $links = self::links($componentId,
362 $componentAction,
363 $qfKey,
364 $componentContext
365 );
a964bc8e 366 $checkLineItem = FALSE;
6a488035 367 $row = array();
a964bc8e 368 // Now check for lineItems
4323dc6c
PN
369 if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus()) {
370 $lineItems = CRM_Price_BAO_LineItem::getLineItemsByContributionID($result->id);
40c655aa 371 foreach ($lineItems as $items) {
4323dc6c
PN
372 if (!CRM_Core_Permission::check('view contributions of type ' . CRM_Contribute_PseudoConstant::financialType($items['financial_type_id']))) {
373 $checkLineItem = TRUE;
374 break;
375 }
376 if (!CRM_Core_Permission::check('edit contributions of type ' . CRM_Contribute_PseudoConstant::financialType($items['financial_type_id']))) {
377 unset($links[CRM_Core_Action::UPDATE]);
378 }
379 if (!CRM_Core_Permission::check('delete contributions of type ' . CRM_Contribute_PseudoConstant::financialType($items['financial_type_id']))) {
380 unset($links[CRM_Core_Action::DELETE]);
381 }
a964bc8e 382 }
4323dc6c
PN
383 if ($checkLineItem) {
384 continue;
385 }
386 if (!CRM_Core_Permission::check('edit contributions of type ' . CRM_Contribute_PseudoConstant::financialType($result->financial_type_id))) {
2d0b158e 387 unset($links[CRM_Core_Action::UPDATE]);
48da6700 388 }
4323dc6c 389 if (!CRM_Core_Permission::check('delete contributions of type ' . CRM_Contribute_PseudoConstant::financialType($result->financial_type_id))) {
2d0b158e 390 unset($links[CRM_Core_Action::DELETE]);
48da6700 391 }
a964bc8e 392 }
6a488035
TO
393 // the columns we are interested in
394 foreach (self::$_properties as $property) {
395 if (property_exists($result, $property)) {
396 $row[$property] = $result->$property;
397 }
398 }
399
400 //carry campaign on selectors.
401 $row['campaign'] = CRM_Utils_Array::value($result->contribution_campaign_id, $allCampaigns);
402 $row['campaign_id'] = $result->contribution_campaign_id;
403
404 // add contribution status name
405 $row['contribution_status_name'] = CRM_Utils_Array::value($row['contribution_status_id'],
406 $contributionStatuses
407 );
408
409 if ($result->is_pay_later && CRM_Utils_Array::value('contribution_status_name', $row) == 'Pending') {
410 $row['contribution_status'] .= ' (' . ts('Pay Later') . ')';
411 }
412 elseif (CRM_Utils_Array::value('contribution_status_name', $row) == 'Pending') {
413 $row['contribution_status'] .= ' (' . ts('Incomplete Transaction') . ')';
414 }
415
416 if ($row['is_test']) {
e777c2b6 417 $row['financial_type'] = $row['financial_type'] . ' (' . ts('test') . ')';
6a488035
TO
418 }
419
420 $row['checkbox'] = CRM_Core_Form::CB_PREFIX . $result->contribution_id;
421
6a488035
TO
422 $actions = array(
423 'id' => $result->contribution_id,
424 'cid' => $result->contact_id,
425 'cxt' => $this->_context,
426 );
427
e777c2b6 428 $row['action'] = CRM_Core_Action::formLink(
b501fa94 429 $links,
87dab4a4
AH
430 $mask, $actions,
431 ts('more'),
432 FALSE,
433 'contribution.selector.row',
434 'Contribution',
435 $result->contribution_id
6a488035
TO
436 );
437
874c9be7 438 $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
439 );
440
a7488080 441 if (!empty($row['amount_level'])) {
6a488035
TO
442 CRM_Event_BAO_Participant::fixEventLevel($row['amount_level']);
443 }
444
445 $rows[] = $row;
446 }
447
448 return $rows;
449 }
450
451 /**
1054415f 452 * @inheritDoc
6a488035 453 */
6a488035
TO
454 public function getQILL() {
455 return $this->_query->qill();
456 }
457
458 /**
100fef9d 459 * Returns the column headers as an array of tuples:
6a488035
TO
460 * (name, sortName (key to the sort array))
461 *
014c4014
TO
462 * @param string $action
463 * The action being performed.
3f8d2862 464 * @param string $output
014c4014 465 * What should the result set include (web/email/csv).
6a488035 466 *
a6c01b45
CW
467 * @return array
468 * the column headers that need to be displayed
6a488035
TO
469 */
470 public function &getColumnHeaders($action = NULL, $output = NULL) {
36bf52ef
DS
471 self::$_columnHeaders = array(
472 array(
4f3846df 473 'name' => $this->_includeSoftCredits ? ts('Contribution Amount') : ts('Amount'),
36bf52ef
DS
474 'sort' => 'total_amount',
475 'direction' => CRM_Utils_Sort::DONTCARE,
476 ),
36bf52ef 477 );
4f3846df 478 if ($this->_includeSoftCredits) {
8d7a9d07
CB
479 self::$_columnHeaders
480 = array_merge(
fd31fa4c 481 self::$_columnHeaders,
4f3846df
DS
482 array(
483 array(
484 'name' => ts('Soft Credit Amount'),
485 'sort' => 'contribution_soft_credit_amount',
486 'direction' => CRM_Utils_Sort::DONTCARE,
21dfd5f5 487 ),
4f3846df
DS
488 )
489 );
490 }
8d7a9d07
CB
491 self::$_columnHeaders
492 = array_merge(
fd31fa4c 493 self::$_columnHeaders,
4f3846df
DS
494 array(
495 array(
496 'name' => ts('Type'),
353ffa53 497 'sort' => 'financial_type',
4f3846df
DS
498 'direction' => CRM_Utils_Sort::DONTCARE,
499 ),
500 array(
501 'name' => ts('Source'),
502 'sort' => 'contribution_source',
503 'direction' => CRM_Utils_Sort::DONTCARE,
504 ),
505 array(
506 'name' => ts('Received'),
507 'sort' => 'receive_date',
508 'direction' => CRM_Utils_Sort::DESCENDING,
509 ),
510 array(
511 'name' => ts('Thank-you Sent'),
512 'sort' => 'thankyou_date',
513 'direction' => CRM_Utils_Sort::DONTCARE,
514 ),
515 array(
516 'name' => ts('Status'),
33a5a53d 517 'sort' => 'contribution_status',
4f3846df
DS
518 'direction' => CRM_Utils_Sort::DONTCARE,
519 ),
520 array(
521 'name' => ts('Premium'),
522 'sort' => 'product_name',
523 'direction' => CRM_Utils_Sort::DONTCARE,
524 ),
525 )
526 );
36bf52ef
DS
527 if (!$this->_single) {
528 $pre = array(
529 array('desc' => ts('Contact Type')),
6a488035 530 array(
36bf52ef
DS
531 'name' => ts('Name'),
532 'sort' => 'sort_name',
6a488035
TO
533 'direction' => CRM_Utils_Sort::DONTCARE,
534 ),
6a488035 535 );
36bf52ef
DS
536 self::$_columnHeaders = array_merge($pre, self::$_columnHeaders);
537 }
538 if ($this->_includeSoftCredits) {
006389de 539 self::$_columnHeaders = array_merge(
8d7a9d07
CB
540 self::$_columnHeaders,
541 array(
6a488035 542 array(
8d7a9d07
CB
543 'name' => ts('Soft Credit For'),
544 'sort' => 'contribution_soft_credit_name',
545 'direction' => CRM_Utils_Sort::DONTCARE,
546 ),
547 array(
548 'name' => ts('Soft Credit Type'),
549 'sort' => 'contribution_soft_credit_type',
550 'direction' => CRM_Utils_Sort::ASCENDING,
551 ),
552 )
553 );
6a488035 554 }
8d7a9d07
CB
555 self::$_columnHeaders
556 = array_merge(
4f3846df 557 self::$_columnHeaders, array(
21dfd5f5 558 array('desc' => ts('Actions')),
4f3846df
DS
559 )
560 );
6a488035
TO
561 return self::$_columnHeaders;
562 }
563
186c9c17
EM
564 /**
565 * @return mixed
566 */
00be9182 567 public function alphabetQuery() {
6a488035
TO
568 return $this->_query->searchQuery(NULL, NULL, NULL, FALSE, FALSE, TRUE);
569 }
570
186c9c17
EM
571 /**
572 * @return string
573 */
00be9182 574 public function &getQuery() {
6a488035
TO
575 return $this->_query;
576 }
577
578 /**
100fef9d 579 * Name of export file.
6a488035 580 *
014c4014
TO
581 * @param string $output
582 * Type of output.
6a488035 583 *
a6c01b45
CW
584 * @return string
585 * name of the file
6a488035 586 */
00be9182 587 public function getExportFileName($output = 'csv') {
6a488035
TO
588 return ts('CiviCRM Contribution Search');
589 }
590
186c9c17
EM
591 /**
592 * @return mixed
593 */
00be9182 594 public function getSummary() {
6a488035
TO
595 return $this->_query->summaryContribution($this->_context);
596 }
96025800 597
6a488035 598}