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 +--------------------------------------------------------------------+
30 * @copyright CiviCRM LLC (c) 2004-2017
34 * Class to render contribution search results.
36 class CRM_Contribute_Selector_Search
extends CRM_Core_Selector_Base
implements CRM_Core_Selector_API
{
39 * Array of action links.
43 static $_links = NULL;
46 * We use desc to remind us what that column is, name is used in the tpl
50 static $_columnHeaders;
53 * Properties of contact we're interested in displaying
56 static $_properties = array(
64 'contribution_source',
67 'contribution_status_id',
68 'contribution_status',
72 'contribution_recur_id',
76 'contribution_campaign_id',
77 'contribution_soft_credit_name',
78 'contribution_soft_credit_contact_id',
79 'contribution_soft_credit_amount',
80 'contribution_soft_credit_type',
84 * Are we restricting ourselves to a single contact
88 protected $_single = FALSE;
91 * Are we restricting ourselves to a single contact
95 protected $_limit = NULL;
98 * What context are we being invoked from
102 protected $_context = NULL;
105 * What component context are we being invoked from
109 protected $_compContext = NULL;
112 * QueryParams is the array returned by exportValues called on
113 * the HTML_QuickForm_Controller for that page.
117 public $_queryParams;
120 * Represent the type of selector
127 * The additional clause that we restrict the search with
131 protected $_contributionClause = NULL;
140 protected $_includeSoftCredits = FALSE;
145 * @param array $queryParams
146 * Array of parameters for query.
147 * @param \const|int $action - action of search basic or advanced.
148 * @param string $contributionClause
149 * If the caller wants to further restrict the search (used in contributions).
150 * @param bool $single
151 * Are we dealing only with one contact?.
153 * How many contributions do we want returned.
155 * @param string $context
156 * @param null $compContext
158 * @return \CRM_Contribute_Selector_Search
160 public function __construct(
162 $action = CRM_Core_Action
::NONE
,
163 $contributionClause = NULL,
170 // submitted form values
171 $this->_queryParams
= &$queryParams;
173 $this->_single
= $single;
174 $this->_limit
= $limit;
175 $this->_context
= $context;
176 $this->_compContext
= $compContext;
178 $this->_contributionClause
= $contributionClause;
181 $this->_action
= $action;
182 $returnProperties = CRM_Contribute_BAO_Query
::selectorReturnProperties();
183 $this->_includeSoftCredits
= CRM_Contribute_BAO_Query
::isSoftCreditOptionEnabled($this->_queryParams
);
184 $this->_query
= new CRM_Contact_BAO_Query(
188 CRM_Contact_BAO_Query
::MODE_CONTRIBUTE
190 // @todo the function CRM_Contribute_BAO_Query::isSoftCreditOptionEnabled should handle this
191 // can we remove? if not why not?
192 if ($this->_includeSoftCredits
) {
193 $this->_query
->_rowCountClause
= " count(civicrm_contribution.id)";
194 $this->_query
->_groupByComponentClause
= " GROUP BY contribution_search_scredit_combined.id, contribution_search_scredit_combined.contact_id, contribution_search_scredit_combined.scredit_id ";
197 $this->_query
->_distinctComponentClause
= " civicrm_contribution.id";
198 $this->_query
->_groupByComponentClause
= " GROUP BY civicrm_contribution.id ";
203 * This method returns the links that are given for each search row.
204 * currently the links added for each row are
209 * @param int $componentId
210 * @param null $componentAction
212 * @param null $compContext
216 public static function &links($componentId = NULL, $componentAction = NULL, $key = NULL, $compContext = NULL) {
219 $extraParams = "&compId={$componentId}&compAction={$componentAction}";
222 $extraParams .= "&compContext={$compContext}";
225 $extraParams .= "&key={$key}";
228 if (!(self
::$_links)) {
229 self
::$_links = array(
230 CRM_Core_Action
::VIEW
=> array(
231 'name' => ts('View'),
232 'url' => 'civicrm/contact/view/contribution',
233 'qs' => "reset=1&id=%%id%%&cid=%%cid%%&action=view&context=%%cxt%%&selectedChild=contribute{$extraParams}",
234 'title' => ts('View Contribution'),
236 CRM_Core_Action
::UPDATE
=> array(
237 'name' => ts('Edit'),
238 'url' => 'civicrm/contact/view/contribution',
239 'qs' => "reset=1&action=update&id=%%id%%&cid=%%cid%%&context=%%cxt%%{$extraParams}",
240 'title' => ts('Edit Contribution'),
242 CRM_Core_Action
::DELETE
=> array(
243 'name' => ts('Delete'),
244 'url' => 'civicrm/contact/view/contribution',
245 'qs' => "reset=1&action=delete&id=%%id%%&cid=%%cid%%&context=%%cxt%%{$extraParams}",
246 'title' => ts('Delete Contribution'),
250 return self
::$_links;
254 * Getter for array of the parameters required for creating pager.
257 * @param array $params
259 public function getPagerParams($action, &$params) {
260 $params['status'] = ts('Contribution') . ' %%StatusMessage%%';
261 $params['csvString'] = NULL;
263 $params['rowCount'] = $this->_limit
;
266 $params['rowCount'] = CRM_Utils_Pager
::ROWCOUNT
;
269 $params['buttonTop'] = 'PagerTopButton';
270 $params['buttonBottom'] = 'PagerBottomButton';
274 * Returns total number of rows for the query.
276 * @param string $action
279 * Total number of rows
281 public function getTotalCount($action) {
282 return $this->_query
->searchQuery(0, 0, NULL,
286 $this->_contributionClause
291 * Returns all the rows in the given offset and rowCount.
293 * @param string $action
294 * The action being performed.
296 * The row number to start from.
297 * @param int $rowCount
298 * The number of rows to return.
299 * @param string $sort
300 * The sql string that describes the sort order.
301 * @param string $output
302 * What should the result set include (web/email/csv).
305 * the total number of rows for this action
307 public function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
308 if ($this->_includeSoftCredits
) {
309 // especial sort order when rows include soft credits
310 $sort = $sort->orderBy() . ", civicrm_contribution.id, civicrm_contribution_soft.id";
312 $result = $this->_query
->searchQuery($offset, $rowCount, $sort,
316 $this->_contributionClause
318 // process the result of the query
321 //CRM-4418 check for view/edit/delete
322 $permissions = array(CRM_Core_Permission
::VIEW
);
323 if (CRM_Core_Permission
::check('edit contributions')) {
324 $permissions[] = CRM_Core_Permission
::EDIT
;
326 if (CRM_Core_Permission
::check('delete in CiviContribute')) {
327 $permissions[] = CRM_Core_Permission
::DELETE
;
329 $mask = CRM_Core_Action
::mask($permissions);
331 $qfKey = $this->_key
;
332 $componentId = $componentContext = NULL;
333 if ($this->_context
!= 'contribute') {
334 // @todo explain the significance of context & why we do not get these i that context.
335 $qfKey = CRM_Utils_Request
::retrieve('key', 'String');
336 $componentId = CRM_Utils_Request
::retrieve('id', 'Positive');
337 $componentAction = CRM_Utils_Request
::retrieve('action', 'String');
338 $componentContext = CRM_Utils_Request
::retrieve('compContext', 'String');
340 if (!$componentContext &&
343 // @todo explain when this condition might occur.
344 $componentContext = $this->_compContext
;
345 $qfKey = CRM_Utils_Request
::retrieve('qfKey', 'String', CRM_Core_DAO
::$_nullObject, NULL, FALSE, 'REQUEST');
347 // CRM-17628 for some reason qfKey is not always set when searching from contribution search.
348 // as a result if the edit link is opened using right-click + open in new tab
349 // then the browser is not returned to the search results on save.
350 // This is an effort to getting the qfKey without, sadly, understanding the intent of those who came before me.
352 $qfKey = CRM_Utils_Request
::retrieve('qfKey', 'String', CRM_Core_DAO
::$_nullObject, NULL, FALSE, 'REQUEST');
356 // get all contribution status
357 $contributionStatuses = CRM_Core_OptionGroup
::values('contribution_status',
358 FALSE, FALSE, FALSE, NULL, 'name', FALSE
362 $allCampaigns = CRM_Campaign_BAO_Campaign
::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE);
364 while ($result->fetch()) {
365 $this->_query
->convertToPseudoNames($result);
366 $links = self
::links($componentId,
371 $checkLineItem = FALSE;
373 // Now check for lineItems
374 if (CRM_Financial_BAO_FinancialType
::isACLFinancialTypeStatus()) {
375 $lineItems = CRM_Price_BAO_LineItem
::getLineItemsByContributionID($result->id
);
376 foreach ($lineItems as $items) {
377 if (!CRM_Core_Permission
::check('view contributions of type ' . CRM_Contribute_PseudoConstant
::financialType($items['financial_type_id']))) {
378 $checkLineItem = TRUE;
381 if (!CRM_Core_Permission
::check('edit contributions of type ' . CRM_Contribute_PseudoConstant
::financialType($items['financial_type_id']))) {
382 unset($links[CRM_Core_Action
::UPDATE
]);
384 if (!CRM_Core_Permission
::check('delete contributions of type ' . CRM_Contribute_PseudoConstant
::financialType($items['financial_type_id']))) {
385 unset($links[CRM_Core_Action
::DELETE
]);
388 if ($checkLineItem) {
391 if (!CRM_Core_Permission
::check('edit contributions of type ' . CRM_Contribute_PseudoConstant
::financialType($result->financial_type_id
))) {
392 unset($links[CRM_Core_Action
::UPDATE
]);
394 if (!CRM_Core_Permission
::check('delete contributions of type ' . CRM_Contribute_PseudoConstant
::financialType($result->financial_type_id
))) {
395 unset($links[CRM_Core_Action
::DELETE
]);
398 // the columns we are interested in
399 foreach (self
::$_properties as $property) {
400 if (property_exists($result, $property)) {
401 $row[$property] = $result->$property;
405 //carry campaign on selectors.
406 // @todo - I can't find any evidence that 'carrying' the campaign on selectors actually
407 // results in it being displayed anywhere so why do we do this???
408 $row['campaign'] = CRM_Utils_Array
::value($result->contribution_campaign_id
, $allCampaigns);
409 $row['campaign_id'] = $result->contribution_campaign_id
;
411 // add contribution status name
412 $row['contribution_status_name'] = CRM_Utils_Array
::value($row['contribution_status_id'],
413 $contributionStatuses
417 if ($result->is_pay_later
&& CRM_Utils_Array
::value('contribution_status_name', $row) == 'Pending') {
419 $row['contribution_status'] .= ' (' . ts('Pay Later') . ')';
420 $links[CRM_Core_Action
::ADD
] = array(
421 'name' => ts('Pay with Credit Card'),
422 'url' => 'civicrm/contact/view/contribution',
423 'qs' => 'reset=1&action=update&id=%%id%%&cid=%%cid%%&context=%%cxt%%&mode=live',
424 'title' => ts('Pay with Credit Card'),
427 elseif (CRM_Utils_Array
::value('contribution_status_name', $row) == 'Pending') {
428 $row['contribution_status'] .= ' (' . ts('Incomplete Transaction') . ')';
431 if ($row['is_test']) {
432 $row['financial_type'] = $row['financial_type'] . ' (' . ts('test') . ')';
435 $row['checkbox'] = CRM_Core_Form
::CB_PREFIX
. $result->contribution_id
;
438 'id' => $result->contribution_id
,
439 'cid' => $result->contact_id
,
440 'cxt' => $this->_context
,
443 if (in_array($row['contribution_status_name'], array('Partially paid', 'Pending refund')) ||
$isPayLater) {
444 $buttonName = ts('Record Payment');
445 if ($row['contribution_status_name'] == 'Pending refund') {
446 $buttonName = ts('Record Refund');
448 elseif (CRM_Core_Config
::isEnabledBackOfficeCreditCardPayments()) {
449 $links[CRM_Core_Action
::BASIC
] = array(
450 'name' => ts('Submit Credit Card payment'),
451 'url' => 'civicrm/payment/add',
452 'qs' => 'reset=1&id=%%id%%&cid=%%cid%%&action=add&component=contribution&mode=live',
453 'title' => ts('Submit Credit Card payment'),
456 $links[CRM_Core_Action
::ADD
] = array(
457 'name' => $buttonName,
458 'url' => 'civicrm/payment',
459 'qs' => 'reset=1&id=%%id%%&cid=%%cid%%&action=add&component=contribution',
460 'title' => $buttonName,
464 $row['action'] = CRM_Core_Action
::formLink(
469 'contribution.selector.row',
471 $result->contribution_id
474 $row['contact_type'] = CRM_Contact_BAO_Contact_Utils
::getImage($result->contact_sub_type ?
$result->contact_sub_type
: $result->contact_type
, FALSE, $result->contact_id
477 if (!empty($row['amount_level'])) {
478 CRM_Event_BAO_Participant
::fixEventLevel($row['amount_level']);
490 public function getQILL() {
491 return $this->_query
->qill();
495 * Returns the column headers as an array of tuples:
496 * (name, sortName (key to the sort array))
498 * @param string $action
499 * The action being performed.
500 * @param string $output
501 * What should the result set include (web/email/csv).
504 * the column headers that need to be displayed
506 public function &getColumnHeaders($action = NULL, $output = NULL) {
508 self
::$_columnHeaders = array(
510 'name' => $this->_includeSoftCredits ?
ts('Contribution Amount') : ts('Amount'),
511 'sort' => 'total_amount',
512 'direction' => CRM_Utils_Sort
::DONTCARE
,
515 if ($this->_includeSoftCredits
) {
516 self
::$_columnHeaders
518 self
::$_columnHeaders,
521 'name' => ts('Soft Credit Amount'),
522 'sort' => 'contribution_soft_credit_amount',
523 'direction' => CRM_Utils_Sort
::DONTCARE
,
528 self
::$_columnHeaders
530 self
::$_columnHeaders,
533 'name' => ts('Type'),
534 'sort' => 'financial_type',
535 'direction' => CRM_Utils_Sort
::DONTCARE
,
538 'name' => ts('Source'),
539 'sort' => 'contribution_source',
540 'direction' => CRM_Utils_Sort
::DONTCARE
,
543 'name' => ts('Received'),
544 'sort' => 'receive_date',
545 'direction' => CRM_Utils_Sort
::DESCENDING
,
548 'name' => ts('Thank-you Sent'),
549 'sort' => 'thankyou_date',
550 'direction' => CRM_Utils_Sort
::DONTCARE
,
553 'name' => ts('Status'),
554 'sort' => 'contribution_status',
555 'direction' => CRM_Utils_Sort
::DONTCARE
,
558 'name' => ts('Premium'),
559 'sort' => 'product_name',
560 'direction' => CRM_Utils_Sort
::DONTCARE
,
564 if (!$this->_single
) {
567 'name' => ts('Name'),
568 'sort' => 'sort_name',
569 'direction' => CRM_Utils_Sort
::DONTCARE
,
573 self
::$_columnHeaders = array_merge($pre, self
::$_columnHeaders);
574 if ($this->_includeSoftCredits
) {
575 self
::$_columnHeaders = array_merge(
576 self
::$_columnHeaders,
579 'name' => ts('Soft Credit For'),
580 'sort' => 'contribution_soft_credit_name',
581 'direction' => CRM_Utils_Sort
::DONTCARE
,
584 'name' => ts('Soft Credit Type'),
585 'sort' => 'contribution_soft_credit_type',
586 'direction' => CRM_Utils_Sort
::ASCENDING
,
591 self
::$_columnHeaders
593 self
::$_columnHeaders, array(
594 array('desc' => ts('Actions')),
597 CRM_Core_Smarty
::singleton()->assign('softCreditColumns', $this->_includeSoftCredits
);
598 return self
::$_columnHeaders;
604 public function alphabetQuery() {
605 return $this->_query
->searchQuery(NULL, NULL, NULL, FALSE, FALSE, TRUE);
611 public function &getQuery() {
612 return $this->_query
;
616 * Name of export file.
618 * @param string $output
624 public function getExportFileName($output = 'csv') {
625 return ts('CiviCRM Contribution Search');
631 public function getSummary() {
632 return $this->_query
->summaryContribution($this->_context
);