Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * This class is used to retrieve and display a range of | |
38 | * contacts that match the given criteria (specifically for | |
39 | * results of advanced search options. | |
40 | * | |
41 | */ | |
42 | class CRM_Contribute_Selector_Search extends CRM_Core_Selector_Base implements CRM_Core_Selector_API { | |
43 | ||
44 | /** | |
45 | * This defines two actions- View and Edit. | |
46 | * | |
47 | * @var array | |
48 | * @static | |
49 | */ | |
50 | static $_links = NULL; | |
51 | ||
52 | /** | |
100fef9d | 53 | * We use desc to remind us what that column is, name is used in the tpl |
6a488035 TO |
54 | * |
55 | * @var array | |
56 | * @static | |
57 | */ | |
58 | static $_columnHeaders; | |
59 | ||
60 | /** | |
61 | * Properties of contact we're interested in displaying | |
62 | * @var array | |
63 | * @static | |
64 | */ | |
65 | static $_properties = array( | |
66 | 'contact_id', | |
67 | 'contribution_id', | |
68 | 'contact_type', | |
69 | 'sort_name', | |
70 | 'amount_level', | |
71 | 'total_amount', | |
e777c2b6 | 72 | 'financial_type', |
6a488035 TO |
73 | 'contribution_source', |
74 | 'receive_date', | |
75 | 'thankyou_date', | |
76 | 'contribution_status_id', | |
77 | 'contribution_status', | |
78 | 'cancel_date', | |
79 | 'product_name', | |
80 | 'is_test', | |
81 | 'contribution_recur_id', | |
82 | 'receipt_date', | |
83 | 'membership_id', | |
84 | 'currency', | |
85 | 'contribution_campaign_id', | |
36bf52ef | 86 | 'contribution_soft_credit_name', |
d4e2b978 | 87 | 'contribution_soft_credit_contact_id', |
36bf52ef DS |
88 | 'contribution_soft_credit_amount', |
89 | 'contribution_soft_credit_type', | |
6a488035 TO |
90 | ); |
91 | ||
92 | /** | |
100fef9d | 93 | * Are we restricting ourselves to a single contact |
6a488035 | 94 | * |
6a488035 TO |
95 | * @var boolean |
96 | */ | |
97 | protected $_single = FALSE; | |
98 | ||
99 | /** | |
100fef9d | 100 | * Are we restricting ourselves to a single contact |
6a488035 | 101 | * |
6a488035 TO |
102 | * @var boolean |
103 | */ | |
104 | protected $_limit = NULL; | |
105 | ||
106 | /** | |
100fef9d | 107 | * What context are we being invoked from |
6a488035 | 108 | * |
6a488035 TO |
109 | * @var string |
110 | */ | |
111 | protected $_context = NULL; | |
112 | ||
113 | /** | |
100fef9d | 114 | * What component context are we being invoked from |
6a488035 | 115 | * |
6a488035 TO |
116 | * @var string |
117 | */ | |
118 | protected $_compContext = NULL; | |
119 | ||
120 | /** | |
100fef9d | 121 | * QueryParams is the array returned by exportValues called on |
6a488035 TO |
122 | * the HTML_QuickForm_Controller for that page. |
123 | * | |
124 | * @var array | |
6a488035 TO |
125 | */ |
126 | public $_queryParams; | |
127 | ||
128 | /** | |
100fef9d | 129 | * Represent the type of selector |
6a488035 TO |
130 | * |
131 | * @var int | |
6a488035 TO |
132 | */ |
133 | protected $_action; | |
134 | ||
135 | /** | |
136 | * The additional clause that we restrict the search with | |
137 | * | |
138 | * @var string | |
139 | */ | |
140 | protected $_contributionClause = NULL; | |
141 | ||
142 | /** | |
143 | * The query object | |
144 | * | |
145 | * @var string | |
146 | */ | |
147 | protected $_query; | |
148 | ||
36bf52ef DS |
149 | protected $_includeSoftCredits = FALSE; |
150 | ||
6a488035 TO |
151 | /** |
152 | * Class constructor | |
153 | * | |
154 | * @param array $queryParams array of parameters for query | |
da6b46f4 EM |
155 | * @param \const|int $action - action of search basic or advanced. |
156 | * @param string $contributionClause if the caller wants to further restrict the search (used in contributions) | |
6a488035 | 157 | * @param boolean $single are we dealing only with one contact? |
da6b46f4 | 158 | * @param int $limit how many contributions do we want returned |
6a488035 | 159 | * |
da6b46f4 EM |
160 | * @param string $context |
161 | * @param null $compContext | |
162 | * | |
163 | * @return \CRM_Contribute_Selector_Search | |
164 | @access public | |
6a488035 TO |
165 | */ |
166 | function __construct(&$queryParams, | |
167 | $action = CRM_Core_Action::NONE, | |
168 | $contributionClause = NULL, | |
169 | $single = FALSE, | |
170 | $limit = NULL, | |
171 | $context = 'search', | |
172 | $compContext = NULL | |
173 | ) { | |
174 | ||
175 | // submitted form values | |
176 | $this->_queryParams = &$queryParams; | |
177 | ||
178 | $this->_single = $single; | |
179 | $this->_limit = $limit; | |
180 | $this->_context = $context; | |
181 | $this->_compContext = $compContext; | |
182 | ||
183 | $this->_contributionClause = $contributionClause; | |
184 | ||
185 | // type of selector | |
186 | $this->_action = $action; | |
187 | ||
d4e2b978 | 188 | $this->_includeSoftCredits = CRM_Contribute_BAO_Query::isSoftCreditOptionEnabled($this->_queryParams); |
36bf52ef DS |
189 | $this->_query = new CRM_Contact_BAO_Query( |
190 | $this->_queryParams, | |
191 | CRM_Contribute_BAO_Query::defaultReturnProperties( | |
192 | CRM_Contact_BAO_Query::MODE_CONTRIBUTE, | |
f654cacf | 193 | FALSE |
6a488035 TO |
194 | ), |
195 | NULL, FALSE, FALSE, | |
196 | CRM_Contact_BAO_Query::MODE_CONTRIBUTE | |
197 | ); | |
3dbf477c DS |
198 | if ($this->_includeSoftCredits) { |
199 | $this->_query->_rowCountClause = " count(civicrm_contribution.id)"; | |
ed81a415 | 200 | $this->_query->_groupByComponentClause = " GROUP BY contribution_search_scredit_combined.id, contribution_search_scredit_combined.contact_id, contribution_search_scredit_combined.scredit_id "; |
3dbf477c DS |
201 | } else { |
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 | * | |
281 | * @param | |
282 | * | |
283 | * @return int 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 | /** | |
100fef9d | 295 | * Returns all the rows in the given offset and rowCount |
6a488035 TO |
296 | * |
297 | * @param enum $action the action being performed | |
298 | * @param int $offset the row number to start from | |
299 | * @param int $rowCount the number of rows to return | |
300 | * @param string $sort the sql string that describes the sort order | |
301 | * @param enum $output what should the result set include (web/email/csv) | |
302 | * | |
303 | * @return int the total number of rows for this action | |
304 | */ | |
00be9182 | 305 | public function &getRows($action, $offset, $rowCount, $sort, $output = NULL) { |
4f3846df DS |
306 | if ($this->_includeSoftCredits) { |
307 | // especial sort order when rows include soft credits | |
308 | $sort = "civicrm_contribution.receive_date DESC, civicrm_contribution.id, civicrm_contribution_soft.id"; | |
309 | } | |
6a488035 TO |
310 | $result = $this->_query->searchQuery($offset, $rowCount, $sort, |
311 | FALSE, FALSE, | |
312 | FALSE, FALSE, | |
313 | FALSE, | |
314 | $this->_contributionClause | |
315 | ); | |
316 | // process the result of the query | |
317 | $rows = array(); | |
318 | ||
319 | //CRM-4418 check for view/edit/delete | |
320 | $permissions = array(CRM_Core_Permission::VIEW); | |
321 | if (CRM_Core_Permission::check('edit contributions')) { | |
322 | $permissions[] = CRM_Core_Permission::EDIT; | |
323 | } | |
324 | if (CRM_Core_Permission::check('delete in CiviContribute')) { | |
325 | $permissions[] = CRM_Core_Permission::DELETE; | |
326 | } | |
327 | $mask = CRM_Core_Action::mask($permissions); | |
328 | ||
329 | $qfKey = $this->_key; | |
330 | $componentId = $componentContext = NULL; | |
331 | if ($this->_context != 'contribute') { | |
332 | $qfKey = CRM_Utils_Request::retrieve('key', 'String', CRM_Core_DAO::$_nullObject); | |
333 | $componentId = CRM_Utils_Request::retrieve('id', 'Positive', CRM_Core_DAO::$_nullObject); | |
334 | $componentAction = CRM_Utils_Request::retrieve('action', 'String', CRM_Core_DAO::$_nullObject); | |
335 | $componentContext = CRM_Utils_Request::retrieve('compContext', 'String', CRM_Core_DAO::$_nullObject); | |
336 | ||
337 | if (!$componentContext && | |
338 | $this->_compContext | |
339 | ) { | |
340 | $componentContext = $this->_compContext; | |
341 | $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', CRM_Core_DAO::$_nullObject, NULL, FALSE, 'REQUEST'); | |
342 | } | |
343 | } | |
344 | ||
345 | // get all contribution status | |
346 | $contributionStatuses = CRM_Core_OptionGroup::values('contribution_status', | |
347 | FALSE, FALSE, FALSE, NULL, 'name', FALSE | |
348 | ); | |
349 | ||
350 | //get all campaigns. | |
351 | $allCampaigns = CRM_Campaign_BAO_Campaign::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE); | |
352 | ||
353 | While ($result->fetch()) { | |
354 | $row = array(); | |
355 | // the columns we are interested in | |
356 | foreach (self::$_properties as $property) { | |
357 | if (property_exists($result, $property)) { | |
358 | $row[$property] = $result->$property; | |
359 | } | |
360 | } | |
361 | ||
362 | //carry campaign on selectors. | |
363 | $row['campaign'] = CRM_Utils_Array::value($result->contribution_campaign_id, $allCampaigns); | |
364 | $row['campaign_id'] = $result->contribution_campaign_id; | |
365 | ||
366 | // add contribution status name | |
367 | $row['contribution_status_name'] = CRM_Utils_Array::value($row['contribution_status_id'], | |
368 | $contributionStatuses | |
369 | ); | |
370 | ||
371 | if ($result->is_pay_later && CRM_Utils_Array::value('contribution_status_name', $row) == 'Pending') { | |
372 | $row['contribution_status'] .= ' (' . ts('Pay Later') . ')'; | |
373 | } | |
374 | elseif (CRM_Utils_Array::value('contribution_status_name', $row) == 'Pending') { | |
375 | $row['contribution_status'] .= ' (' . ts('Incomplete Transaction') . ')'; | |
376 | } | |
377 | ||
378 | if ($row['is_test']) { | |
e777c2b6 | 379 | $row['financial_type'] = $row['financial_type'] . ' (' . ts('test') . ')'; |
6a488035 TO |
380 | } |
381 | ||
382 | $row['checkbox'] = CRM_Core_Form::CB_PREFIX . $result->contribution_id; | |
383 | ||
6a488035 TO |
384 | $actions = array( |
385 | 'id' => $result->contribution_id, | |
386 | 'cid' => $result->contact_id, | |
387 | 'cxt' => $this->_context, | |
388 | ); | |
389 | ||
e777c2b6 KJ |
390 | $row['action'] = CRM_Core_Action::formLink( |
391 | self::links($componentId, | |
392 | $componentAction, | |
393 | $qfKey, | |
394 | $componentContext | |
395 | ), | |
87dab4a4 AH |
396 | $mask, $actions, |
397 | ts('more'), | |
398 | FALSE, | |
399 | 'contribution.selector.row', | |
400 | 'Contribution', | |
401 | $result->contribution_id | |
6a488035 TO |
402 | ); |
403 | ||
404 | $row['contact_type'] = CRM_Contact_BAO_Contact_Utils::getImage($result->contact_sub_type ? | |
e777c2b6 | 405 | $result->contact_sub_type : $result->contact_type, FALSE, $result->contact_id |
6a488035 TO |
406 | ); |
407 | ||
a7488080 | 408 | if (!empty($row['amount_level'])) { |
6a488035 TO |
409 | CRM_Event_BAO_Participant::fixEventLevel($row['amount_level']); |
410 | } | |
411 | ||
412 | $rows[] = $row; | |
413 | } | |
414 | ||
415 | return $rows; | |
416 | } | |
417 | ||
418 | /** | |
6a488035 | 419 | * @return array $qill which contains an array of strings |
6a488035 TO |
420 | */ |
421 | ||
422 | // the current internationalisation is bad, but should more or less work | |
423 | // for most of "European" languages | |
424 | public function getQILL() { | |
425 | return $this->_query->qill(); | |
426 | } | |
427 | ||
428 | /** | |
100fef9d | 429 | * Returns the column headers as an array of tuples: |
6a488035 TO |
430 | * (name, sortName (key to the sort array)) |
431 | * | |
432 | * @param string $action the action being performed | |
433 | * @param enum $output what should the result set include (web/email/csv) | |
434 | * | |
435 | * @return array the column headers that need to be displayed | |
6a488035 TO |
436 | */ |
437 | public function &getColumnHeaders($action = NULL, $output = NULL) { | |
36bf52ef DS |
438 | self::$_columnHeaders = array( |
439 | array( | |
4f3846df | 440 | 'name' => $this->_includeSoftCredits ? ts('Contribution Amount') : ts('Amount'), |
36bf52ef DS |
441 | 'sort' => 'total_amount', |
442 | 'direction' => CRM_Utils_Sort::DONTCARE, | |
443 | ), | |
36bf52ef | 444 | ); |
4f3846df | 445 | if ($this->_includeSoftCredits) { |
fd31fa4c | 446 | self::$_columnHeaders = |
4f3846df | 447 | array_merge( |
fd31fa4c | 448 | self::$_columnHeaders, |
4f3846df DS |
449 | array( |
450 | array( | |
451 | 'name' => ts('Soft Credit Amount'), | |
452 | 'sort' => 'contribution_soft_credit_amount', | |
453 | 'direction' => CRM_Utils_Sort::DONTCARE, | |
454 | ) | |
455 | ) | |
456 | ); | |
457 | } | |
fd31fa4c | 458 | self::$_columnHeaders = |
4f3846df | 459 | array_merge( |
fd31fa4c | 460 | self::$_columnHeaders, |
4f3846df DS |
461 | array( |
462 | array( | |
463 | 'name' => ts('Type'), | |
33a5a53d | 464 | 'sort' => 'financial_type', |
4f3846df DS |
465 | 'direction' => CRM_Utils_Sort::DONTCARE, |
466 | ), | |
467 | array( | |
468 | 'name' => ts('Source'), | |
469 | 'sort' => 'contribution_source', | |
470 | 'direction' => CRM_Utils_Sort::DONTCARE, | |
471 | ), | |
472 | array( | |
473 | 'name' => ts('Received'), | |
474 | 'sort' => 'receive_date', | |
475 | 'direction' => CRM_Utils_Sort::DESCENDING, | |
476 | ), | |
477 | array( | |
478 | 'name' => ts('Thank-you Sent'), | |
479 | 'sort' => 'thankyou_date', | |
480 | 'direction' => CRM_Utils_Sort::DONTCARE, | |
481 | ), | |
482 | array( | |
483 | 'name' => ts('Status'), | |
33a5a53d | 484 | 'sort' => 'contribution_status', |
4f3846df DS |
485 | 'direction' => CRM_Utils_Sort::DONTCARE, |
486 | ), | |
487 | array( | |
488 | 'name' => ts('Premium'), | |
489 | 'sort' => 'product_name', | |
490 | 'direction' => CRM_Utils_Sort::DONTCARE, | |
491 | ), | |
492 | ) | |
493 | ); | |
36bf52ef DS |
494 | if (!$this->_single) { |
495 | $pre = array( | |
496 | array('desc' => ts('Contact Type')), | |
6a488035 | 497 | array( |
36bf52ef DS |
498 | 'name' => ts('Name'), |
499 | 'sort' => 'sort_name', | |
6a488035 TO |
500 | 'direction' => CRM_Utils_Sort::DONTCARE, |
501 | ), | |
6a488035 | 502 | ); |
36bf52ef DS |
503 | self::$_columnHeaders = array_merge($pre, self::$_columnHeaders); |
504 | } | |
505 | if ($this->_includeSoftCredits) { | |
fd31fa4c | 506 | self::$_columnHeaders = |
4f3846df | 507 | array_merge( |
fd31fa4c | 508 | self::$_columnHeaders, |
6a488035 | 509 | array( |
4f3846df DS |
510 | array( |
511 | 'name' => ts('Soft Credit For'), | |
512 | 'sort' => 'contribution_soft_credit_name', | |
513 | 'direction' => CRM_Utils_Sort::DONTCARE, | |
514 | ), | |
515 | array( | |
516 | 'name' => ts('Soft Credit Type'), | |
517 | 'sort' => 'contribution_soft_credit_type', | |
518 | 'direction' => CRM_Utils_Sort::ASCENDING, | |
519 | ), | |
520 | ) | |
6a488035 | 521 | ); |
6a488035 | 522 | } |
fd31fa4c | 523 | self::$_columnHeaders = |
4f3846df DS |
524 | array_merge( |
525 | self::$_columnHeaders, array( | |
526 | array('desc' => ts('Actions')) | |
527 | ) | |
528 | ); | |
6a488035 TO |
529 | return self::$_columnHeaders; |
530 | } | |
531 | ||
186c9c17 EM |
532 | /** |
533 | * @return mixed | |
534 | */ | |
00be9182 | 535 | public function alphabetQuery() { |
6a488035 TO |
536 | return $this->_query->searchQuery(NULL, NULL, NULL, FALSE, FALSE, TRUE); |
537 | } | |
538 | ||
186c9c17 EM |
539 | /** |
540 | * @return string | |
541 | */ | |
00be9182 | 542 | public function &getQuery() { |
6a488035 TO |
543 | return $this->_query; |
544 | } | |
545 | ||
546 | /** | |
100fef9d | 547 | * Name of export file. |
6a488035 TO |
548 | * |
549 | * @param string $output type of output | |
550 | * | |
551 | * @return string name of the file | |
552 | */ | |
00be9182 | 553 | public function getExportFileName($output = 'csv') { |
6a488035 TO |
554 | return ts('CiviCRM Contribution Search'); |
555 | } | |
556 | ||
186c9c17 EM |
557 | /** |
558 | * @return mixed | |
559 | */ | |
00be9182 | 560 | public function getSummary() { |
6a488035 TO |
561 | return $this->_query->summaryContribution($this->_context); |
562 | } | |
563 | } |