Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
232624b1 | 4 | | CiviCRM version 4.4 | |
6a488035 TO |
5 | +--------------------------------------------------------------------+ |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
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 | |
31 | * @copyright CiviCRM LLC (c) 2004-2013 | |
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 | /** | |
53 | * we use desc to remind us what that column is, name is used in the tpl | |
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', | |
86 | ); | |
87 | ||
88 | /** | |
89 | * are we restricting ourselves to a single contact | |
90 | * | |
91 | * @access protected | |
92 | * @var boolean | |
93 | */ | |
94 | protected $_single = FALSE; | |
95 | ||
96 | /** | |
97 | * are we restricting ourselves to a single contact | |
98 | * | |
99 | * @access protected | |
100 | * @var boolean | |
101 | */ | |
102 | protected $_limit = NULL; | |
103 | ||
104 | /** | |
105 | * what context are we being invoked from | |
106 | * | |
107 | * @access protected | |
108 | * @var string | |
109 | */ | |
110 | protected $_context = NULL; | |
111 | ||
112 | /** | |
113 | * what component context are we being invoked from | |
114 | * | |
115 | * @access protected | |
116 | * @var string | |
117 | */ | |
118 | protected $_compContext = NULL; | |
119 | ||
120 | /** | |
121 | * queryParams is the array returned by exportValues called on | |
122 | * the HTML_QuickForm_Controller for that page. | |
123 | * | |
124 | * @var array | |
125 | * @access protected | |
126 | */ | |
127 | public $_queryParams; | |
128 | ||
129 | /** | |
130 | * represent the type of selector | |
131 | * | |
132 | * @var int | |
133 | * @access protected | |
134 | */ | |
135 | protected $_action; | |
136 | ||
137 | /** | |
138 | * The additional clause that we restrict the search with | |
139 | * | |
140 | * @var string | |
141 | */ | |
142 | protected $_contributionClause = NULL; | |
143 | ||
144 | /** | |
145 | * The query object | |
146 | * | |
147 | * @var string | |
148 | */ | |
149 | protected $_query; | |
150 | ||
151 | /** | |
152 | * Class constructor | |
153 | * | |
154 | * @param array $queryParams array of parameters for query | |
155 | * @param int $action - action of search basic or advanced. | |
156 | * @param string $contributionClause if the caller wants to further restrict the search (used in contributions) | |
157 | * @param boolean $single are we dealing only with one contact? | |
158 | * @param int $limit how many contributions do we want returned | |
159 | * | |
160 | * @return CRM_Contact_Selector | |
161 | * @access public | |
162 | */ | |
163 | function __construct(&$queryParams, | |
164 | $action = CRM_Core_Action::NONE, | |
165 | $contributionClause = NULL, | |
166 | $single = FALSE, | |
167 | $limit = NULL, | |
168 | $context = 'search', | |
169 | $compContext = NULL | |
170 | ) { | |
171 | ||
172 | // submitted form values | |
173 | $this->_queryParams = &$queryParams; | |
174 | ||
175 | $this->_single = $single; | |
176 | $this->_limit = $limit; | |
177 | $this->_context = $context; | |
178 | $this->_compContext = $compContext; | |
179 | ||
180 | $this->_contributionClause = $contributionClause; | |
181 | ||
182 | // type of selector | |
183 | $this->_action = $action; | |
184 | ||
185 | $this->_query = new CRM_Contact_BAO_Query($this->_queryParams, | |
186 | CRM_Contribute_BAO_Query::defaultReturnProperties(CRM_Contact_BAO_Query::MODE_CONTRIBUTE, | |
187 | FALSE | |
188 | ), | |
189 | NULL, FALSE, FALSE, | |
190 | CRM_Contact_BAO_Query::MODE_CONTRIBUTE | |
191 | ); | |
192 | $this->_query->_distinctComponentClause = " civicrm_contribution.id"; | |
193 | $this->_query->_groupByComponentClause = " GROUP BY civicrm_contribution.id "; | |
194 | } | |
195 | //end of constructor | |
196 | ||
197 | /** | |
198 | * This method returns the links that are given for each search row. | |
199 | * currently the links added for each row are | |
200 | * | |
201 | * - View | |
202 | * - Edit | |
203 | * | |
204 | * @return array | |
205 | * @access public | |
206 | * | |
207 | */ | |
208 | static function &links($componentId = NULL, $componentAction = NULL, $key = NULL, $compContext = NULL) { | |
209 | $extraParams = NULL; | |
210 | if ($componentId) { | |
211 | $extraParams = "&compId={$componentId}&compAction={$componentAction}"; | |
212 | } | |
213 | if ($compContext) { | |
214 | $extraParams .= "&compContext={$compContext}"; | |
215 | } | |
216 | if ($key) { | |
217 | $extraParams .= "&key={$key}"; | |
218 | } | |
219 | ||
220 | if (!(self::$_links)) { | |
221 | self::$_links = array( | |
222 | CRM_Core_Action::VIEW => array( | |
223 | 'name' => ts('View'), | |
224 | 'url' => 'civicrm/contact/view/contribution', | |
225 | 'qs' => "reset=1&id=%%id%%&cid=%%cid%%&action=view&context=%%cxt%%&selectedChild=contribute{$extraParams}", | |
226 | 'title' => ts('View Contribution'), | |
227 | ), | |
228 | CRM_Core_Action::UPDATE => array( | |
229 | 'name' => ts('Edit'), | |
230 | 'url' => 'civicrm/contact/view/contribution', | |
231 | 'qs' => "reset=1&action=update&id=%%id%%&cid=%%cid%%&context=%%cxt%%{$extraParams}", | |
232 | 'title' => ts('Edit Contribution'), | |
233 | ), | |
234 | CRM_Core_Action::DELETE => array( | |
235 | 'name' => ts('Delete'), | |
236 | 'url' => 'civicrm/contact/view/contribution', | |
237 | 'qs' => "reset=1&action=delete&id=%%id%%&cid=%%cid%%&context=%%cxt%%{$extraParams}", | |
238 | 'title' => ts('Delete Contribution'), | |
239 | ), | |
240 | ); | |
241 | } | |
242 | return self::$_links; | |
243 | } | |
244 | //end of function | |
245 | ||
246 | /** | |
247 | * getter for array of the parameters required for creating pager. | |
248 | * | |
249 | * @param | |
250 | * @access public | |
251 | */ | |
252 | function getPagerParams($action, &$params) { | |
253 | $params['status'] = ts('Contribution') . ' %%StatusMessage%%'; | |
254 | $params['csvString'] = NULL; | |
255 | if ($this->_limit) { | |
256 | $params['rowCount'] = $this->_limit; | |
257 | } | |
258 | else { | |
259 | $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT; | |
260 | } | |
261 | ||
262 | $params['buttonTop'] = 'PagerTopButton'; | |
263 | $params['buttonBottom'] = 'PagerBottomButton'; | |
264 | } | |
265 | //end of function | |
266 | ||
267 | /** | |
268 | * Returns total number of rows for the query. | |
269 | * | |
270 | * @param | |
271 | * | |
272 | * @return int Total number of rows | |
273 | * @access public | |
274 | */ | |
275 | function getTotalCount($action) { | |
276 | return $this->_query->searchQuery(0, 0, NULL, | |
277 | TRUE, FALSE, | |
278 | FALSE, FALSE, | |
279 | FALSE, | |
280 | $this->_contributionClause | |
281 | ); | |
282 | } | |
283 | ||
284 | /** | |
285 | * returns all the rows in the given offset and rowCount | |
286 | * | |
287 | * @param enum $action the action being performed | |
288 | * @param int $offset the row number to start from | |
289 | * @param int $rowCount the number of rows to return | |
290 | * @param string $sort the sql string that describes the sort order | |
291 | * @param enum $output what should the result set include (web/email/csv) | |
292 | * | |
293 | * @return int the total number of rows for this action | |
294 | */ | |
295 | function &getRows($action, $offset, $rowCount, $sort, $output = NULL) { | |
296 | $result = $this->_query->searchQuery($offset, $rowCount, $sort, | |
297 | FALSE, FALSE, | |
298 | FALSE, FALSE, | |
299 | FALSE, | |
300 | $this->_contributionClause | |
301 | ); | |
302 | // process the result of the query | |
303 | $rows = array(); | |
304 | ||
305 | //CRM-4418 check for view/edit/delete | |
306 | $permissions = array(CRM_Core_Permission::VIEW); | |
307 | if (CRM_Core_Permission::check('edit contributions')) { | |
308 | $permissions[] = CRM_Core_Permission::EDIT; | |
309 | } | |
310 | if (CRM_Core_Permission::check('delete in CiviContribute')) { | |
311 | $permissions[] = CRM_Core_Permission::DELETE; | |
312 | } | |
313 | $mask = CRM_Core_Action::mask($permissions); | |
314 | ||
315 | $qfKey = $this->_key; | |
316 | $componentId = $componentContext = NULL; | |
317 | if ($this->_context != 'contribute') { | |
318 | $qfKey = CRM_Utils_Request::retrieve('key', 'String', CRM_Core_DAO::$_nullObject); | |
319 | $componentId = CRM_Utils_Request::retrieve('id', 'Positive', CRM_Core_DAO::$_nullObject); | |
320 | $componentAction = CRM_Utils_Request::retrieve('action', 'String', CRM_Core_DAO::$_nullObject); | |
321 | $componentContext = CRM_Utils_Request::retrieve('compContext', 'String', CRM_Core_DAO::$_nullObject); | |
322 | ||
323 | if (!$componentContext && | |
324 | $this->_compContext | |
325 | ) { | |
326 | $componentContext = $this->_compContext; | |
327 | $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', CRM_Core_DAO::$_nullObject, NULL, FALSE, 'REQUEST'); | |
328 | } | |
329 | } | |
330 | ||
331 | // get all contribution status | |
332 | $contributionStatuses = CRM_Core_OptionGroup::values('contribution_status', | |
333 | FALSE, FALSE, FALSE, NULL, 'name', FALSE | |
334 | ); | |
335 | ||
336 | //get all campaigns. | |
337 | $allCampaigns = CRM_Campaign_BAO_Campaign::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE); | |
338 | ||
339 | While ($result->fetch()) { | |
340 | $row = array(); | |
341 | // the columns we are interested in | |
342 | foreach (self::$_properties as $property) { | |
343 | if (property_exists($result, $property)) { | |
344 | $row[$property] = $result->$property; | |
345 | } | |
346 | } | |
347 | ||
348 | //carry campaign on selectors. | |
349 | $row['campaign'] = CRM_Utils_Array::value($result->contribution_campaign_id, $allCampaigns); | |
350 | $row['campaign_id'] = $result->contribution_campaign_id; | |
351 | ||
352 | // add contribution status name | |
353 | $row['contribution_status_name'] = CRM_Utils_Array::value($row['contribution_status_id'], | |
354 | $contributionStatuses | |
355 | ); | |
356 | ||
357 | if ($result->is_pay_later && CRM_Utils_Array::value('contribution_status_name', $row) == 'Pending') { | |
358 | $row['contribution_status'] .= ' (' . ts('Pay Later') . ')'; | |
359 | } | |
360 | elseif (CRM_Utils_Array::value('contribution_status_name', $row) == 'Pending') { | |
361 | $row['contribution_status'] .= ' (' . ts('Incomplete Transaction') . ')'; | |
362 | } | |
363 | ||
364 | if ($row['is_test']) { | |
e777c2b6 | 365 | $row['financial_type'] = $row['financial_type'] . ' (' . ts('test') . ')'; |
6a488035 TO |
366 | } |
367 | ||
368 | $row['checkbox'] = CRM_Core_Form::CB_PREFIX . $result->contribution_id; | |
369 | ||
6a488035 TO |
370 | $actions = array( |
371 | 'id' => $result->contribution_id, | |
372 | 'cid' => $result->contact_id, | |
373 | 'cxt' => $this->_context, | |
374 | ); | |
375 | ||
e777c2b6 KJ |
376 | $row['action'] = CRM_Core_Action::formLink( |
377 | self::links($componentId, | |
378 | $componentAction, | |
379 | $qfKey, | |
380 | $componentContext | |
381 | ), | |
87dab4a4 AH |
382 | $mask, $actions, |
383 | ts('more'), | |
384 | FALSE, | |
385 | 'contribution.selector.row', | |
386 | 'Contribution', | |
387 | $result->contribution_id | |
6a488035 TO |
388 | ); |
389 | ||
390 | $row['contact_type'] = CRM_Contact_BAO_Contact_Utils::getImage($result->contact_sub_type ? | |
e777c2b6 | 391 | $result->contact_sub_type : $result->contact_type, FALSE, $result->contact_id |
6a488035 TO |
392 | ); |
393 | ||
a7488080 | 394 | if (!empty($row['amount_level'])) { |
6a488035 TO |
395 | CRM_Event_BAO_Participant::fixEventLevel($row['amount_level']); |
396 | } | |
397 | ||
398 | $rows[] = $row; | |
399 | } | |
400 | ||
401 | return $rows; | |
402 | } | |
403 | ||
404 | /** | |
405 | * | |
406 | * @return array $qill which contains an array of strings | |
407 | * @access public | |
408 | */ | |
409 | ||
410 | // the current internationalisation is bad, but should more or less work | |
411 | // for most of "European" languages | |
412 | public function getQILL() { | |
413 | return $this->_query->qill(); | |
414 | } | |
415 | ||
416 | /** | |
417 | * returns the column headers as an array of tuples: | |
418 | * (name, sortName (key to the sort array)) | |
419 | * | |
420 | * @param string $action the action being performed | |
421 | * @param enum $output what should the result set include (web/email/csv) | |
422 | * | |
423 | * @return array the column headers that need to be displayed | |
424 | * @access public | |
425 | */ | |
426 | public function &getColumnHeaders($action = NULL, $output = NULL) { | |
427 | if (!isset(self::$_columnHeaders)) { | |
428 | self::$_columnHeaders = array( | |
429 | array( | |
430 | 'name' => ts('Amount'), | |
431 | 'sort' => 'total_amount', | |
432 | 'direction' => CRM_Utils_Sort::DONTCARE, | |
433 | ), | |
434 | array('name' => ts('Type'), | |
8607c3f2 | 435 | 'sort' => 'financial_type_id', |
6a488035 TO |
436 | 'direction' => CRM_Utils_Sort::DONTCARE, |
437 | ), | |
438 | array( | |
439 | 'name' => ts('Source'), | |
440 | 'sort' => 'contribution_source', | |
441 | 'direction' => CRM_Utils_Sort::DONTCARE, | |
442 | ), | |
443 | array( | |
444 | 'name' => ts('Received'), | |
445 | 'sort' => 'receive_date', | |
446 | 'direction' => CRM_Utils_Sort::DESCENDING, | |
447 | ), | |
448 | array( | |
449 | 'name' => ts('Thank-you Sent'), | |
450 | 'sort' => 'thankyou_date', | |
451 | 'direction' => CRM_Utils_Sort::DONTCARE, | |
452 | ), | |
453 | array( | |
454 | 'name' => ts('Status'), | |
455 | 'sort' => 'contribution_status_id', | |
456 | 'direction' => CRM_Utils_Sort::DONTCARE, | |
457 | ), | |
458 | array( | |
459 | 'name' => ts('Premium'), | |
460 | 'sort' => 'product_name', | |
461 | 'direction' => CRM_Utils_Sort::DONTCARE, | |
462 | ), | |
463 | array('desc' => ts('Actions')), | |
464 | ); | |
465 | ||
466 | if (!$this->_single) { | |
467 | $pre = array( | |
468 | array('desc' => ts('Contact Type')), | |
469 | array( | |
470 | 'name' => ts('Name'), | |
471 | 'sort' => 'sort_name', | |
472 | 'direction' => CRM_Utils_Sort::DONTCARE, | |
473 | ), | |
474 | ); | |
475 | self::$_columnHeaders = array_merge($pre, self::$_columnHeaders); | |
476 | } | |
477 | } | |
478 | return self::$_columnHeaders; | |
479 | } | |
480 | ||
481 | function alphabetQuery() { | |
482 | return $this->_query->searchQuery(NULL, NULL, NULL, FALSE, FALSE, TRUE); | |
483 | } | |
484 | ||
485 | function &getQuery() { | |
486 | return $this->_query; | |
487 | } | |
488 | ||
489 | /** | |
490 | * name of export file. | |
491 | * | |
492 | * @param string $output type of output | |
493 | * | |
494 | * @return string name of the file | |
495 | */ | |
496 | function getExportFileName($output = 'csv') { | |
497 | return ts('CiviCRM Contribution Search'); | |
498 | } | |
499 | ||
500 | function getSummary() { | |
501 | return $this->_query->summaryContribution($this->_context); | |
502 | } | |
503 | } | |
6a488035 | 504 |