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_Pledge_Selector_Search extends CRM_Core_Selector_Base { | |
43 | ||
44 | /** | |
45 | * This defines two actions- View and Edit. | |
46 | * | |
47 | * @var array | |
6a488035 TO |
48 | */ |
49 | static $_links = NULL; | |
50 | ||
51 | /** | |
100fef9d | 52 | * We use desc to remind us what that column is, name is used in the tpl |
6a488035 TO |
53 | * |
54 | * @var array | |
6a488035 TO |
55 | */ |
56 | static $_columnHeaders; | |
57 | ||
58 | /** | |
59 | * Properties of contact we're interested in displaying | |
60 | * @var array | |
6a488035 TO |
61 | */ |
62 | static $_properties = array( | |
63 | 'contact_id', | |
64 | 'sort_name', | |
65 | 'display_name', | |
66 | 'pledge_id', | |
67 | 'pledge_amount', | |
68 | 'pledge_create_date', | |
69 | 'pledge_total_paid', | |
70 | 'pledge_next_pay_date', | |
71 | 'pledge_next_pay_amount', | |
72 | 'pledge_outstanding_amount', | |
73 | 'pledge_status_id', | |
74 | 'pledge_status', | |
75 | 'pledge_is_test', | |
76 | 'pledge_contribution_page_id', | |
6d3864ef | 77 | 'pledge_financial_type', |
6a488035 TO |
78 | 'pledge_campaign_id', |
79 | 'pledge_currency', | |
80 | ); | |
81 | ||
82 | /** | |
100fef9d | 83 | * Are we restricting ourselves to a single contact |
6a488035 | 84 | * |
6a488035 TO |
85 | * @var boolean |
86 | */ | |
87 | protected $_single = FALSE; | |
88 | ||
89 | /** | |
100fef9d | 90 | * Are we restricting ourselves to a single contact |
6a488035 | 91 | * |
6a488035 TO |
92 | * @var boolean |
93 | */ | |
94 | protected $_limit = NULL; | |
95 | ||
96 | /** | |
100fef9d | 97 | * What context are we being invoked from |
6a488035 | 98 | * |
6a488035 TO |
99 | * @var string |
100 | */ | |
101 | protected $_context = NULL; | |
102 | ||
103 | /** | |
100fef9d | 104 | * QueryParams is the array returned by exportValues called on |
6a488035 TO |
105 | * the HTML_QuickForm_Controller for that page. |
106 | * | |
107 | * @var array | |
6a488035 TO |
108 | */ |
109 | public $_queryParams; | |
110 | ||
111 | /** | |
100fef9d | 112 | * Represent the type of selector |
6a488035 TO |
113 | * |
114 | * @var int | |
6a488035 TO |
115 | */ |
116 | protected $_action; | |
117 | ||
118 | /** | |
119 | * The additional clause that we restrict the search with | |
120 | * | |
121 | * @var string | |
122 | */ | |
123 | protected $_additionalClause = NULL; | |
124 | ||
125 | /** | |
126 | * The query object | |
127 | * | |
128 | * @var string | |
129 | */ | |
130 | protected $_query; | |
131 | ||
132 | /** | |
133 | * Class constructor | |
134 | * | |
3a1617b6 TO |
135 | * @param array $queryParams |
136 | * Array of parameters for query. | |
fd31fa4c | 137 | * @param \const|int $action - action of search basic or advanced. |
3a1617b6 TO |
138 | * @param string $additionalClause |
139 | * If the caller wants to further restrict the search (used in participations). | |
140 | * @param bool $single | |
141 | * Are we dealing only with one contact?. | |
142 | * @param int $limit | |
143 | * How many signers do we want returned. | |
6a488035 | 144 | * |
fd31fa4c EM |
145 | * @param string $context |
146 | * | |
147 | * @return \CRM_Pledge_Selector_Search | |
d60f50a8 | 148 | * @access public |
6a488035 | 149 | */ |
3295515a TO |
150 | function __construct( |
151 | &$queryParams, | |
098201d8 | 152 | $action = CRM_Core_Action::NONE, |
6a488035 | 153 | $additionalClause = NULL, |
098201d8 TO |
154 | $single = FALSE, |
155 | $limit = NULL, | |
156 | $context = 'search' | |
6a488035 TO |
157 | ) { |
158 | // submitted form values | |
159 | $this->_queryParams = &$queryParams; | |
160 | ||
353ffa53 TO |
161 | $this->_single = $single; |
162 | $this->_limit = $limit; | |
6a488035 TO |
163 | $this->_context = $context; |
164 | ||
165 | $this->_additionalClause = $additionalClause; | |
166 | ||
167 | // type of selector | |
168 | $this->_action = $action; | |
169 | ||
170 | $this->_query = new CRM_Contact_BAO_Query($this->_queryParams, NULL, NULL, FALSE, FALSE, | |
171 | CRM_Contact_BAO_Query::MODE_PLEDGE | |
172 | ); | |
173 | ||
174 | $this->_query->_distinctComponentClause = "civicrm_pledge.id"; | |
175 | $this->_query->_groupByComponentClause = " GROUP BY civicrm_pledge.id "; | |
176 | } | |
6a488035 TO |
177 | |
178 | /** | |
179 | * This method returns the links that are given for each search row. | |
180 | * currently the links added for each row are | |
181 | * | |
182 | * - View | |
183 | * - Edit | |
184 | * | |
185 | * @return array | |
6a488035 | 186 | */ |
00be9182 | 187 | public static function &links() { |
9f6f062c DL |
188 | $args = func_get_args(); |
189 | $hideOption = CRM_Utils_Array::value(0, $args); | |
190 | $key = CRM_Utils_Array::value(1, $args); | |
191 | ||
6a488035 TO |
192 | $extraParams = ($key) ? "&key={$key}" : NULL; |
193 | ||
194 | $cancelExtra = ts('Cancelling this pledge will also cancel any scheduled (and not completed) pledge payments.') . ' ' . ts('This action cannot be undone.') . ' ' . ts('Do you want to continue?'); | |
195 | self::$_links = array( | |
196 | CRM_Core_Action::VIEW => array( | |
197 | 'name' => ts('View'), | |
198 | 'url' => 'civicrm/contact/view/pledge', | |
199 | 'qs' => 'reset=1&id=%%id%%&cid=%%cid%%&action=view&context=%%cxt%%&selectedChild=pledge' . $extraParams, | |
200 | 'title' => ts('View Pledge'), | |
201 | ), | |
202 | CRM_Core_Action::UPDATE => array( | |
203 | 'name' => ts('Edit'), | |
204 | 'url' => 'civicrm/contact/view/pledge', | |
205 | 'qs' => 'reset=1&action=update&id=%%id%%&cid=%%cid%%&context=%%cxt%%' . $extraParams, | |
206 | 'title' => ts('Edit Pledge'), | |
207 | ), | |
208 | CRM_Core_Action::DETACH => array( | |
209 | 'name' => ts('Cancel'), | |
210 | 'url' => 'civicrm/contact/view/pledge', | |
211 | 'qs' => 'reset=1&action=detach&id=%%id%%&cid=%%cid%%&context=%%cxt%%' . $extraParams, | |
212 | 'extra' => 'onclick = "return confirm(\'' . $cancelExtra . '\');"', | |
213 | 'title' => ts('Cancel Pledge'), | |
214 | ), | |
215 | CRM_Core_Action::DELETE => array( | |
216 | 'name' => ts('Delete'), | |
217 | 'url' => 'civicrm/contact/view/pledge', | |
218 | 'qs' => 'reset=1&action=delete&id=%%id%%&cid=%%cid%%&context=%%cxt%%' . $extraParams, | |
219 | 'title' => ts('Delete Pledge'), | |
220 | ), | |
221 | ); | |
222 | ||
6a488035 TO |
223 | if (in_array('Cancel', $hideOption)) { |
224 | unset(self::$_links[CRM_Core_Action::DETACH]); | |
225 | } | |
226 | ||
227 | return self::$_links; | |
228 | } | |
6a488035 TO |
229 | |
230 | /** | |
100fef9d | 231 | * Getter for array of the parameters required for creating pager. |
6a488035 | 232 | * |
da6b46f4 | 233 | * @param $action |
c490a46a | 234 | * @param array $params |
6a488035 | 235 | */ |
00be9182 | 236 | public function getPagerParams($action, &$params) { |
6a488035 TO |
237 | $params['status'] = ts('Pledge') . ' %%StatusMessage%%'; |
238 | $params['csvString'] = NULL; | |
239 | if ($this->_limit) { | |
240 | $params['rowCount'] = $this->_limit; | |
241 | } | |
242 | else { | |
243 | $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT; | |
244 | } | |
245 | ||
246 | $params['buttonTop'] = 'PagerTopButton'; | |
247 | $params['buttonBottom'] = 'PagerBottomButton'; | |
248 | } | |
6a488035 TO |
249 | |
250 | /** | |
251 | * Returns total number of rows for the query. | |
252 | * | |
253 | * @param | |
254 | * | |
a6c01b45 CW |
255 | * @return int |
256 | * Total number of rows | |
6a488035 | 257 | */ |
00be9182 | 258 | public function getTotalCount($action) { |
6a488035 TO |
259 | return $this->_query->searchQuery(0, 0, NULL, |
260 | TRUE, FALSE, | |
261 | FALSE, FALSE, | |
262 | FALSE, | |
263 | $this->_additionalClause | |
264 | ); | |
265 | } | |
266 | ||
267 | /** | |
100fef9d | 268 | * Returns all the rows in the given offset and rowCount |
6a488035 | 269 | * |
3f8d2862 | 270 | * @param string $action |
3a1617b6 TO |
271 | * The action being performed. |
272 | * @param int $offset | |
273 | * The row number to start from. | |
274 | * @param int $rowCount | |
275 | * The number of rows to return. | |
276 | * @param string $sort | |
277 | * The sql string that describes the sort order. | |
3f8d2862 | 278 | * @param string $output |
3a1617b6 | 279 | * What should the result set include (web/email/csv). |
6a488035 | 280 | * |
a6c01b45 CW |
281 | * @return int |
282 | * the total number of rows for this action | |
6a488035 | 283 | */ |
00be9182 | 284 | public function &getRows($action, $offset, $rowCount, $sort, $output = NULL) { |
6a488035 TO |
285 | $result = $this->_query->searchQuery($offset, $rowCount, $sort, |
286 | FALSE, FALSE, | |
287 | FALSE, FALSE, | |
288 | FALSE, | |
289 | $this->_additionalClause | |
290 | ); | |
291 | ||
292 | // process the result of the query | |
293 | $rows = array(); | |
294 | ||
295 | // get all pledge status | |
296 | $pledgeStatuses = CRM_Core_OptionGroup::values('contribution_status', | |
297 | FALSE, FALSE, FALSE, NULL, 'name', FALSE | |
298 | ); | |
299 | ||
300 | //get all campaigns. | |
301 | $allCampaigns = CRM_Campaign_BAO_Campaign::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE); | |
302 | ||
303 | //4418 check for view, edit and delete | |
304 | $permissions = array(CRM_Core_Permission::VIEW); | |
305 | if (CRM_Core_Permission::check('edit pledges')) { | |
306 | $permissions[] = CRM_Core_Permission::EDIT; | |
307 | } | |
308 | if (CRM_Core_Permission::check('delete in CiviPledge')) { | |
309 | $permissions[] = CRM_Core_Permission::DELETE; | |
310 | } | |
311 | $mask = CRM_Core_Action::mask($permissions); | |
312 | ||
313 | while ($result->fetch()) { | |
314 | $row = array(); | |
315 | // the columns we are interested in | |
316 | foreach (self::$_properties as $property) { | |
317 | if (isset($result->$property)) { | |
318 | $row[$property] = $result->$property; | |
319 | } | |
320 | } | |
321 | ||
322 | //carry campaign on selectors. | |
323 | $row['campaign'] = CRM_Utils_Array::value($result->pledge_campaign_id, $allCampaigns); | |
324 | $row['campaign_id'] = $result->pledge_campaign_id; | |
325 | ||
326 | // add pledge status name | |
327 | $row['pledge_status_name'] = CRM_Utils_Array::value($row['pledge_status_id'], | |
328 | $pledgeStatuses | |
329 | ); | |
330 | // append (test) to status label | |
a7488080 | 331 | if (!empty($row['pledge_is_test'])) { |
6a488035 TO |
332 | $row['pledge_status'] .= ' (test)'; |
333 | } | |
334 | ||
335 | $hideOption = array(); | |
336 | if (CRM_Utils_Array::key('Cancelled', $row) || | |
337 | CRM_Utils_Array::key('Completed', $row) | |
338 | ) { | |
339 | $hideOption[] = 'Cancel'; | |
340 | } | |
341 | ||
342 | $row['checkbox'] = CRM_Core_Form::CB_PREFIX . $result->pledge_id; | |
343 | ||
344 | $row['action'] = CRM_Core_Action::formLink(self::links($hideOption, $this->_key), | |
345 | $mask, | |
346 | array( | |
347 | 'id' => $result->pledge_id, | |
348 | 'cid' => $result->contact_id, | |
349 | 'cxt' => $this->_context, | |
87dab4a4 AH |
350 | ), |
351 | ts('more'), | |
352 | FALSE, | |
353 | 'pledge.selector.row', | |
354 | 'Pledge', | |
355 | $result->pledge_id | |
6a488035 TO |
356 | ); |
357 | ||
098201d8 | 358 | $row['contact_type'] = CRM_Contact_BAO_Contact_Utils::getImage($result->contact_sub_type ? $result->contact_sub_type : $result->contact_type, FALSE, $result->contact_id |
6a488035 TO |
359 | ); |
360 | $rows[] = $row; | |
361 | } | |
362 | return $rows; | |
363 | } | |
364 | ||
365 | /** | |
1054415f | 366 | * @inheritDoc |
6a488035 | 367 | */ |
6a488035 TO |
368 | public function getQILL() { |
369 | return $this->_query->qill(); | |
370 | } | |
371 | ||
372 | /** | |
100fef9d | 373 | * Returns the column headers as an array of tuples: |
6a488035 TO |
374 | * (name, sortName (key to the sort array)) |
375 | * | |
3a1617b6 TO |
376 | * @param string $action |
377 | * The action being performed. | |
3f8d2862 | 378 | * @param string $output |
3a1617b6 | 379 | * What should the result set include (web/email/csv). |
6a488035 | 380 | * |
a6c01b45 CW |
381 | * @return array |
382 | * the column headers that need to be displayed | |
6a488035 TO |
383 | */ |
384 | public function &getColumnHeaders($action = NULL, $output = NULL) { | |
385 | if (!isset(self::$_columnHeaders)) { | |
386 | self::$_columnHeaders = array( | |
387 | array( | |
388 | 'name' => ts('Pledged'), | |
389 | 'sort' => 'pledge_amount', | |
390 | 'direction' => CRM_Utils_Sort::DONTCARE, | |
391 | ), | |
392 | array( | |
393 | 'name' => ts('Total Paid'), | |
394 | 'sort' => 'pledge_total_paid', | |
395 | 'direction' => CRM_Utils_Sort::DONTCARE, | |
396 | ), | |
397 | array( | |
398 | 'name' => ts('Balance'), | |
399 | ), | |
400 | array( | |
401 | 'name' => ts('Pledged For'), | |
6d3864ef | 402 | 'sort' => 'pledge_financial_type', |
6a488035 TO |
403 | 'direction' => CRM_Utils_Sort::DONTCARE, |
404 | ), | |
405 | array( | |
406 | 'name' => ts('Pledge Made'), | |
407 | 'sort' => 'pledge_create_date', | |
408 | 'direction' => CRM_Utils_Sort::DESCENDING, | |
409 | ), | |
410 | array( | |
411 | 'name' => ts('Next Pay Date'), | |
412 | 'sort' => 'pledge_next_pay_date', | |
413 | 'direction' => CRM_Utils_Sort::DONTCARE, | |
414 | ), | |
415 | array( | |
416 | 'name' => ts('Next Amount'), | |
417 | 'sort' => 'pledge_next_pay_amount', | |
418 | 'direction' => CRM_Utils_Sort::DONTCARE, | |
419 | ), | |
420 | array( | |
421 | 'name' => ts('Status'), | |
422 | 'sort' => 'pledge_status', | |
423 | 'direction' => CRM_Utils_Sort::DONTCARE, | |
424 | ), | |
425 | array('desc' => ts('Actions')), | |
426 | ); | |
427 | ||
428 | if (!$this->_single) { | |
429 | $pre = array( | |
7b99ead3 | 430 | array('desc' => ts('Contact ID')), |
6a488035 TO |
431 | array( |
432 | 'name' => ts('Name'), | |
433 | 'sort' => 'sort_name', | |
434 | 'direction' => CRM_Utils_Sort::DONTCARE, | |
435 | ), | |
436 | ); | |
437 | ||
438 | self::$_columnHeaders = array_merge($pre, self::$_columnHeaders); | |
439 | } | |
440 | } | |
441 | return self::$_columnHeaders; | |
442 | } | |
443 | ||
ffd93213 EM |
444 | /** |
445 | * @return string | |
446 | */ | |
00be9182 | 447 | public function &getQuery() { |
6a488035 TO |
448 | return $this->_query; |
449 | } | |
450 | ||
451 | /** | |
100fef9d | 452 | * Name of export file. |
6a488035 | 453 | * |
3a1617b6 TO |
454 | * @param string $output |
455 | * Type of output. | |
6a488035 | 456 | * |
a6c01b45 CW |
457 | * @return string |
458 | * name of the file | |
6a488035 | 459 | */ |
00be9182 | 460 | public function getExportFileName($output = 'csv') { |
6a488035 TO |
461 | return ts('Pledge Search'); |
462 | } | |
463 | } |