3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2017
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.
40 class CRM_Contribute_Selector_Search
extends CRM_Core_Selector_Base
implements CRM_Core_Selector_API
{
43 * This defines two actions- View and Edit.
47 static $_links = NULL;
50 * We use desc to remind us what that column is, name is used in the tpl
54 static $_columnHeaders;
57 * Properties of contact we're interested in displaying
60 static $_properties = array(
68 'contribution_source',
71 'contribution_status_id',
72 'contribution_status',
76 'contribution_recur_id',
80 'contribution_campaign_id',
81 'contribution_soft_credit_name',
82 'contribution_soft_credit_contact_id',
83 'contribution_soft_credit_amount',
84 'contribution_soft_credit_type',
88 * Are we restricting ourselves to a single contact
92 protected $_single = FALSE;
95 * Are we restricting ourselves to a single contact
99 protected $_limit = NULL;
102 * What context are we being invoked from
106 protected $_context = NULL;
109 * What component context are we being invoked from
113 protected $_compContext = NULL;
116 * QueryParams is the array returned by exportValues called on
117 * the HTML_QuickForm_Controller for that page.
121 public $_queryParams;
124 * Represent the type of selector
131 * The additional clause that we restrict the search with
135 protected $_contributionClause = NULL;
144 protected $_includeSoftCredits = FALSE;
149 * @param array $queryParams
150 * Array of parameters for query.
151 * @param \const|int $action - action of search basic or advanced.
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?.
157 * How many contributions do we want returned.
159 * @param string $context
160 * @param null $compContext
162 * @return \CRM_Contribute_Selector_Search
164 public function __construct(
166 $action = CRM_Core_Action
::NONE
,
167 $contributionClause = NULL,
174 // submitted form values
175 $this->_queryParams
= &$queryParams;
177 $this->_single
= $single;
178 $this->_limit
= $limit;
179 $this->_context
= $context;
180 $this->_compContext
= $compContext;
182 $this->_contributionClause
= $contributionClause;
185 $this->_action
= $action;
186 $returnProperties = CRM_Contribute_BAO_Query
::selectorReturnProperties();
187 $this->_includeSoftCredits
= CRM_Contribute_BAO_Query
::isSoftCreditOptionEnabled($this->_queryParams
);
188 $this->_query
= new CRM_Contact_BAO_Query(
192 CRM_Contact_BAO_Query
::MODE_CONTRIBUTE
194 // @todo the function CRM_Contribute_BAO_Query::isSoftCreditOptionEnabled should handle this
195 // can we remove? if not why not?
196 if ($this->_includeSoftCredits
) {
197 $this->_query
->_rowCountClause
= " count(civicrm_contribution.id)";
198 $this->_query
->_groupByComponentClause
= " GROUP BY contribution_search_scredit_combined.id, contribution_search_scredit_combined.contact_id, contribution_search_scredit_combined.scredit_id ";
201 $this->_query
->_distinctComponentClause
= " civicrm_contribution.id";
202 $this->_query
->_groupByComponentClause
= " GROUP BY civicrm_contribution.id ";
207 * This method returns the links that are given for each search row.
208 * currently the links added for each row are
213 * @param int $componentId
214 * @param null $componentAction
216 * @param null $compContext
220 public static function &links($componentId = NULL, $componentAction = NULL, $key = NULL, $compContext = NULL) {
223 $extraParams = "&compId={$componentId}&compAction={$componentAction}";
226 $extraParams .= "&compContext={$compContext}";
229 $extraParams .= "&key={$key}";
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'),
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'),
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'),
254 return self
::$_links;
258 * Getter for array of the parameters required for creating pager.
261 * @param array $params
263 public function getPagerParams($action, &$params) {
264 $params['status'] = ts('Contribution') . ' %%StatusMessage%%';
265 $params['csvString'] = NULL;
267 $params['rowCount'] = $this->_limit
;
270 $params['rowCount'] = CRM_Utils_Pager
::ROWCOUNT
;
273 $params['buttonTop'] = 'PagerTopButton';
274 $params['buttonBottom'] = 'PagerBottomButton';
278 * Returns total number of rows for the query.
280 * @param string $action
283 * Total number of rows
285 public function getTotalCount($action) {
286 return $this->_query
->searchQuery(0, 0, NULL,
290 $this->_contributionClause
295 * Returns all the rows in the given offset and rowCount.
297 * @param string $action
298 * The action being performed.
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.
305 * @param string $output
306 * What should the result set include (web/email/csv).
309 * the total number of rows for this action
311 public function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
312 if ($this->_includeSoftCredits
) {
313 // especial sort order when rows include soft credits
314 $sort = $sort->orderBy() . ", civicrm_contribution.id, civicrm_contribution_soft.id";
316 $result = $this->_query
->searchQuery($offset, $rowCount, $sort,
320 $this->_contributionClause
322 // process the result of the query
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
;
330 if (CRM_Core_Permission
::check('delete in CiviContribute')) {
331 $permissions[] = CRM_Core_Permission
::DELETE
;
333 $mask = CRM_Core_Action
::mask($permissions);
335 $qfKey = $this->_key
;
336 $componentId = $componentContext = NULL;
337 if ($this->_context
!= 'contribute') {
338 // @todo explain the significance of context & why we do not get these i that context.
339 $qfKey = CRM_Utils_Request
::retrieve('key', 'String');
340 $componentId = CRM_Utils_Request
::retrieve('id', 'Positive');
341 $componentAction = CRM_Utils_Request
::retrieve('action', 'String');
342 $componentContext = CRM_Utils_Request
::retrieve('compContext', 'String');
344 if (!$componentContext &&
347 // @todo explain when this condition might occur.
348 $componentContext = $this->_compContext
;
349 $qfKey = CRM_Utils_Request
::retrieve('qfKey', 'String', CRM_Core_DAO
::$_nullObject, NULL, FALSE, 'REQUEST');
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.
356 $qfKey = CRM_Utils_Request
::retrieve('qfKey', 'String', CRM_Core_DAO
::$_nullObject, NULL, FALSE, 'REQUEST');
360 // get all contribution status
361 $contributionStatuses = CRM_Core_OptionGroup
::values('contribution_status',
362 FALSE, FALSE, FALSE, NULL, 'name', FALSE
366 $allCampaigns = CRM_Campaign_BAO_Campaign
::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE);
368 while ($result->fetch()) {
369 $this->_query
->convertToPseudoNames($result);
370 $links = self
::links($componentId,
375 $checkLineItem = FALSE;
377 // Now check for lineItems
378 if (CRM_Financial_BAO_FinancialType
::isACLFinancialTypeStatus()) {
379 $lineItems = CRM_Price_BAO_LineItem
::getLineItemsByContributionID($result->id
);
380 foreach ($lineItems as $items) {
381 if (!CRM_Core_Permission
::check('view contributions of type ' . CRM_Contribute_PseudoConstant
::financialType($items['financial_type_id']))) {
382 $checkLineItem = TRUE;
385 if (!CRM_Core_Permission
::check('edit contributions of type ' . CRM_Contribute_PseudoConstant
::financialType($items['financial_type_id']))) {
386 unset($links[CRM_Core_Action
::UPDATE
]);
388 if (!CRM_Core_Permission
::check('delete contributions of type ' . CRM_Contribute_PseudoConstant
::financialType($items['financial_type_id']))) {
389 unset($links[CRM_Core_Action
::DELETE
]);
392 if ($checkLineItem) {
395 if (!CRM_Core_Permission
::check('edit contributions of type ' . CRM_Contribute_PseudoConstant
::financialType($result->financial_type_id
))) {
396 unset($links[CRM_Core_Action
::UPDATE
]);
398 if (!CRM_Core_Permission
::check('delete contributions of type ' . CRM_Contribute_PseudoConstant
::financialType($result->financial_type_id
))) {
399 unset($links[CRM_Core_Action
::DELETE
]);
402 // the columns we are interested in
403 foreach (self
::$_properties as $property) {
404 if (property_exists($result, $property)) {
405 $row[$property] = $result->$property;
409 //carry campaign on selectors.
410 // @todo - I can't find any evidence that 'carrying' the campaign on selectors actually
411 // results in it being displayed anywhere so why do we do this???
412 $row['campaign'] = CRM_Utils_Array
::value($result->contribution_campaign_id
, $allCampaigns);
413 $row['campaign_id'] = $result->contribution_campaign_id
;
415 // add contribution status name
416 $row['contribution_status_name'] = CRM_Utils_Array
::value($row['contribution_status_id'],
417 $contributionStatuses
421 if ($result->is_pay_later
&& CRM_Utils_Array
::value('contribution_status_name', $row) == 'Pending') {
423 $row['contribution_status'] .= ' (' . ts('Pay Later') . ')';
424 $links[CRM_Core_Action
::ADD
] = array(
425 'name' => ts('Pay with Credit Card'),
426 'url' => 'civicrm/contact/view/contribution',
427 'qs' => 'reset=1&action=update&id=%%id%%&cid=%%cid%%&context=%%cxt%%&mode=live',
428 'title' => ts('Pay with Credit Card'),
431 elseif (CRM_Utils_Array
::value('contribution_status_name', $row) == 'Pending') {
432 $row['contribution_status'] .= ' (' . ts('Incomplete Transaction') . ')';
435 if ($row['is_test']) {
436 $row['financial_type'] = $row['financial_type'] . ' (' . ts('test') . ')';
439 $row['checkbox'] = CRM_Core_Form
::CB_PREFIX
. $result->contribution_id
;
442 'id' => $result->contribution_id
,
443 'cid' => $result->contact_id
,
444 'cxt' => $this->_context
,
447 if (in_array($row['contribution_status_name'], array('Partially paid', 'Pending refund')) ||
$isPayLater) {
448 $buttonName = ts('Record Payment');
449 if ($row['contribution_status_name'] == 'Pending refund') {
450 $buttonName = ts('Record Refund');
452 $links[CRM_Core_Action
::ADD
] = array(
453 'name' => $buttonName,
454 'url' => 'civicrm/payment',
455 'qs' => 'reset=1&id=%%id%%&cid=%%cid%%&action=add&component=contribution',
456 'title' => $buttonName,
460 $row['action'] = CRM_Core_Action
::formLink(
465 'contribution.selector.row',
467 $result->contribution_id
470 $row['contact_type'] = CRM_Contact_BAO_Contact_Utils
::getImage($result->contact_sub_type ?
$result->contact_sub_type
: $result->contact_type
, FALSE, $result->contact_id
473 if (!empty($row['amount_level'])) {
474 CRM_Event_BAO_Participant
::fixEventLevel($row['amount_level']);
486 public function getQILL() {
487 return $this->_query
->qill();
491 * Returns the column headers as an array of tuples:
492 * (name, sortName (key to the sort array))
494 * @param string $action
495 * The action being performed.
496 * @param string $output
497 * What should the result set include (web/email/csv).
500 * the column headers that need to be displayed
502 public function &getColumnHeaders($action = NULL, $output = NULL) {
504 self
::$_columnHeaders = array(
506 'name' => $this->_includeSoftCredits ?
ts('Contribution Amount') : ts('Amount'),
507 'sort' => 'total_amount',
508 'direction' => CRM_Utils_Sort
::DONTCARE
,
511 if ($this->_includeSoftCredits
) {
512 self
::$_columnHeaders
514 self
::$_columnHeaders,
517 'name' => ts('Soft Credit Amount'),
518 'sort' => 'contribution_soft_credit_amount',
519 'direction' => CRM_Utils_Sort
::DONTCARE
,
524 self
::$_columnHeaders
526 self
::$_columnHeaders,
529 'name' => ts('Type'),
530 'sort' => 'financial_type',
531 'direction' => CRM_Utils_Sort
::DONTCARE
,
534 'name' => ts('Source'),
535 'sort' => 'contribution_source',
536 'direction' => CRM_Utils_Sort
::DONTCARE
,
539 'name' => ts('Received'),
540 'sort' => 'receive_date',
541 'direction' => CRM_Utils_Sort
::DESCENDING
,
544 'name' => ts('Thank-you Sent'),
545 'sort' => 'thankyou_date',
546 'direction' => CRM_Utils_Sort
::DONTCARE
,
549 'name' => ts('Status'),
550 'sort' => 'contribution_status',
551 'direction' => CRM_Utils_Sort
::DONTCARE
,
554 'name' => ts('Premium'),
555 'sort' => 'product_name',
556 'direction' => CRM_Utils_Sort
::DONTCARE
,
560 if (!$this->_single
) {
563 'name' => ts('Name'),
564 'sort' => 'sort_name',
565 'direction' => CRM_Utils_Sort
::DONTCARE
,
569 self
::$_columnHeaders = array_merge($pre, self
::$_columnHeaders);
570 if ($this->_includeSoftCredits
) {
571 self
::$_columnHeaders = array_merge(
572 self
::$_columnHeaders,
575 'name' => ts('Soft Credit For'),
576 'sort' => 'contribution_soft_credit_name',
577 'direction' => CRM_Utils_Sort
::DONTCARE
,
580 'name' => ts('Soft Credit Type'),
581 'sort' => 'contribution_soft_credit_type',
582 'direction' => CRM_Utils_Sort
::ASCENDING
,
587 self
::$_columnHeaders
589 self
::$_columnHeaders, array(
590 array('desc' => ts('Actions')),
593 CRM_Core_Smarty
::singleton()->assign('softCreditColumns', $this->_includeSoftCredits
);
594 return self
::$_columnHeaders;
600 public function alphabetQuery() {
601 return $this->_query
->searchQuery(NULL, NULL, NULL, FALSE, FALSE, TRUE);
607 public function &getQuery() {
608 return $this->_query
;
612 * Name of export file.
614 * @param string $output
620 public function getExportFileName($output = 'csv') {
621 return ts('CiviCRM Contribution Search');
627 public function getSummary() {
628 return $this->_query
->summaryContribution($this->_context
);