3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
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;
187 $this->_includeSoftCredits
= CRM_Contribute_BAO_Query
::isSoftCreditOptionEnabled($this->_queryParams
);
188 $this->_query
= new CRM_Contact_BAO_Query(
190 CRM_Contribute_BAO_Query
::defaultReturnProperties(
191 CRM_Contact_BAO_Query
::MODE_CONTRIBUTE
,
195 CRM_Contact_BAO_Query
::MODE_CONTRIBUTE
197 if ($this->_includeSoftCredits
) {
198 $this->_query
->_rowCountClause
= " count(civicrm_contribution.id)";
199 $this->_query
->_groupByComponentClause
= " GROUP BY contribution_search_scredit_combined.id, contribution_search_scredit_combined.contact_id, contribution_search_scredit_combined.scredit_id ";
202 $this->_query
->_distinctComponentClause
= " civicrm_contribution.id";
203 $this->_query
->_groupByComponentClause
= " GROUP BY civicrm_contribution.id ";
208 * This method returns the links that are given for each search row.
209 * currently the links added for each row are
214 * @param int $componentId
215 * @param null $componentAction
217 * @param null $compContext
221 public static function &links($componentId = NULL, $componentAction = NULL, $key = NULL, $compContext = NULL) {
224 $extraParams = "&compId={$componentId}&compAction={$componentAction}";
227 $extraParams .= "&compContext={$compContext}";
230 $extraParams .= "&key={$key}";
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'),
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'),
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'),
255 return self
::$_links;
259 * Getter for array of the parameters required for creating pager.
262 * @param array $params
264 public function getPagerParams($action, &$params) {
265 $params['status'] = ts('Contribution') . ' %%StatusMessage%%';
266 $params['csvString'] = NULL;
268 $params['rowCount'] = $this->_limit
;
271 $params['rowCount'] = CRM_Utils_Pager
::ROWCOUNT
;
274 $params['buttonTop'] = 'PagerTopButton';
275 $params['buttonBottom'] = 'PagerBottomButton';
279 * Returns total number of rows for the query.
281 * @param string $action
284 * Total number of rows
286 public function getTotalCount($action) {
287 return $this->_query
->searchQuery(0, 0, NULL,
291 $this->_contributionClause
296 * Returns all the rows in the given offset and rowCount.
298 * @param string $action
299 * The action being performed.
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.
306 * @param string $output
307 * What should the result set include (web/email/csv).
310 * the total number of rows for this action
312 public function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
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";
317 $result = $this->_query
->searchQuery($offset, $rowCount, $sort,
321 $this->_contributionClause
323 // process the result of the query
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
;
331 if (CRM_Core_Permission
::check('delete in CiviContribute')) {
332 $permissions[] = CRM_Core_Permission
::DELETE
;
334 $mask = CRM_Core_Action
::mask($permissions);
336 $qfKey = $this->_key
;
337 $componentId = $componentContext = NULL;
338 if ($this->_context
!= 'contribute') {
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);
342 $componentContext = CRM_Utils_Request
::retrieve('compContext', 'String', CRM_Core_DAO
::$_nullObject);
344 if (!$componentContext &&
347 $componentContext = $this->_compContext
;
348 $qfKey = CRM_Utils_Request
::retrieve('qfKey', 'String', CRM_Core_DAO
::$_nullObject, NULL, FALSE, 'REQUEST');
352 // get all contribution status
353 $contributionStatuses = CRM_Core_OptionGroup
::values('contribution_status',
354 FALSE, FALSE, FALSE, NULL, 'name', FALSE
358 $allCampaigns = CRM_Campaign_BAO_Campaign
::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE);
360 while ($result->fetch()) {
361 $links = self
::links($componentId,
366 $checkLineItem = FALSE;
368 // Now check for lineItems
369 $lineItems = CRM_Price_BAO_LineItem
::getLineItemsByContributionID($result->id
);
370 foreach ($lineItems as $items) {
371 if (!CRM_Core_Permission
::check('view contributions of type ' . CRM_Contribute_PseudoConstant
::financialType($items['financial_type_id']))) {
372 $checkLineItem = TRUE;
375 if (!CRM_Core_Permission
::check('edit contributions of type ' . CRM_Contribute_PseudoConstant
::financialType($items['financial_type_id']))) {
376 unset($links[CRM_Core_Action
::UPDATE
]);
378 if (!CRM_Core_Permission
::check('delete contributions of type ' . CRM_Contribute_PseudoConstant
::financialType($items['financial_type_id']))) {
379 unset($links[CRM_Core_Action
::DELETE
]);
382 if ($checkLineItem) {
385 if (!CRM_Core_Permission
::check('edit contributions of type ' . CRM_Contribute_PseudoConstant
::financialType($result->financial_type_id
))) {
386 unset($links[CRM_Core_Action
::UPDATE
]);
388 if (!CRM_Core_Permission
::check('delete contributions of type ' . CRM_Contribute_PseudoConstant
::financialType($result->financial_type_id
))) {
389 unset($links[CRM_Core_Action
::DELETE
]);
391 // the columns we are interested in
392 foreach (self
::$_properties as $property) {
393 if (property_exists($result, $property)) {
394 $row[$property] = $result->$property;
398 //carry campaign on selectors.
399 $row['campaign'] = CRM_Utils_Array
::value($result->contribution_campaign_id
, $allCampaigns);
400 $row['campaign_id'] = $result->contribution_campaign_id
;
402 // add contribution status name
403 $row['contribution_status_name'] = CRM_Utils_Array
::value($row['contribution_status_id'],
404 $contributionStatuses
407 if ($result->is_pay_later
&& CRM_Utils_Array
::value('contribution_status_name', $row) == 'Pending') {
408 $row['contribution_status'] .= ' (' . ts('Pay Later') . ')';
410 elseif (CRM_Utils_Array
::value('contribution_status_name', $row) == 'Pending') {
411 $row['contribution_status'] .= ' (' . ts('Incomplete Transaction') . ')';
414 if ($row['is_test']) {
415 $row['financial_type'] = $row['financial_type'] . ' (' . ts('test') . ')';
418 $row['checkbox'] = CRM_Core_Form
::CB_PREFIX
. $result->contribution_id
;
421 'id' => $result->contribution_id
,
422 'cid' => $result->contact_id
,
423 'cxt' => $this->_context
,
426 $row['action'] = CRM_Core_Action
::formLink(
431 'contribution.selector.row',
433 $result->contribution_id
436 $row['contact_type'] = CRM_Contact_BAO_Contact_Utils
::getImage($result->contact_sub_type ?
$result->contact_sub_type
: $result->contact_type
, FALSE, $result->contact_id
439 if (!empty($row['amount_level'])) {
440 CRM_Event_BAO_Participant
::fixEventLevel($row['amount_level']);
452 public function getQILL() {
453 return $this->_query
->qill();
457 * Returns the column headers as an array of tuples:
458 * (name, sortName (key to the sort array))
460 * @param string $action
461 * The action being performed.
462 * @param string $output
463 * What should the result set include (web/email/csv).
466 * the column headers that need to be displayed
468 public function &getColumnHeaders($action = NULL, $output = NULL) {
469 self
::$_columnHeaders = array(
471 'name' => $this->_includeSoftCredits ?
ts('Contribution Amount') : ts('Amount'),
472 'sort' => 'total_amount',
473 'direction' => CRM_Utils_Sort
::DONTCARE
,
476 if ($this->_includeSoftCredits
) {
477 self
::$_columnHeaders
479 self
::$_columnHeaders,
482 'name' => ts('Soft Credit Amount'),
483 'sort' => 'contribution_soft_credit_amount',
484 'direction' => CRM_Utils_Sort
::DONTCARE
,
489 self
::$_columnHeaders
491 self
::$_columnHeaders,
494 'name' => ts('Type'),
495 'sort' => 'financial_type',
496 'direction' => CRM_Utils_Sort
::DONTCARE
,
499 'name' => ts('Source'),
500 'sort' => 'contribution_source',
501 'direction' => CRM_Utils_Sort
::DONTCARE
,
504 'name' => ts('Received'),
505 'sort' => 'receive_date',
506 'direction' => CRM_Utils_Sort
::DESCENDING
,
509 'name' => ts('Thank-you Sent'),
510 'sort' => 'thankyou_date',
511 'direction' => CRM_Utils_Sort
::DONTCARE
,
514 'name' => ts('Status'),
515 'sort' => 'contribution_status',
516 'direction' => CRM_Utils_Sort
::DONTCARE
,
519 'name' => ts('Premium'),
520 'sort' => 'product_name',
521 'direction' => CRM_Utils_Sort
::DONTCARE
,
525 if (!$this->_single
) {
527 array('desc' => ts('Contact Type')),
529 'name' => ts('Name'),
530 'sort' => 'sort_name',
531 'direction' => CRM_Utils_Sort
::DONTCARE
,
534 self
::$_columnHeaders = array_merge($pre, self
::$_columnHeaders);
536 if ($this->_includeSoftCredits
) {
537 self
::$_columnHeaders = array_merge(
538 self
::$_columnHeaders,
541 'name' => ts('Soft Credit For'),
542 'sort' => 'contribution_soft_credit_name',
543 'direction' => CRM_Utils_Sort
::DONTCARE
,
546 'name' => ts('Soft Credit Type'),
547 'sort' => 'contribution_soft_credit_type',
548 'direction' => CRM_Utils_Sort
::ASCENDING
,
553 self
::$_columnHeaders
555 self
::$_columnHeaders, array(
556 array('desc' => ts('Actions')),
559 return self
::$_columnHeaders;
565 public function alphabetQuery() {
566 return $this->_query
->searchQuery(NULL, NULL, NULL, FALSE, FALSE, TRUE);
572 public function &getQuery() {
573 return $this->_query
;
577 * Name of export file.
579 * @param string $output
585 public function getExportFileName($output = 'csv') {
586 return ts('CiviCRM Contribution Search');
592 public function getSummary() {
593 return $this->_query
->summaryContribution($this->_context
);