CRM-12595 fix formatting in remaing files
[civicrm-core.git] / CRM / Grant / Selector / PaymentSearch.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
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 require_once 'CRM/Core/Selector/Base.php';
37 require_once 'CRM/Core/Selector/API.php';
38
39 require_once 'CRM/Utils/Pager.php';
40 require_once 'CRM/Utils/Sort.php';
41
42 require_once 'CRM/Grant/BAO/PaymentSearch.php';
43
44 /**
45 * This class is used to retrieve and display a range of
46 * contacts that match the given criteria (specifically for
47 * results of advanced search options.
48 *
49 */
50 class CRM_Grant_Selector_PaymentSearch extends CRM_Core_Selector_Base implements CRM_Core_Selector_API
51 {
52
53 /**
54 * This defines two actions- View and Edit.
55 *
56 * @var array
57 * @static
58 */
59 static $_links = null;
60
61 /**
62 * we use desc to remind us what that column is, name is used in the tpl
63 *
64 * @var array
65 * @static
66 */
67 static $_columnHeaders;
68
69 /**
70 * Properties of contact we're interested in displaying
71 * @var array
72 * @static
73 */
74 static $_properties = array( 'id',
75 'payable_to_name',
76 'payment_batch_number',
77 'payment_number',
78 'payment_status_id',
79 'payment_created_date',
80 'amount',
81 );
82
83 /**
84 * are we restricting ourselves to a single contact
85 *
86 * @access protected
87 * @var boolean
88 */
89 protected $_single = false;
90
91 /**
92 * are we restricting ourselves to a single contact
93 *
94 * @access protected
95 * @var boolean
96 */
97 protected $_limit = null;
98
99 /**
100 * what context are we being invoked from
101 *
102 * @access protected
103 * @var string
104 */
105 protected $_context = null;
106
107 /**
108 * queryParams is the array returned by exportValues called on
109 * the HTML_QuickForm_Controller for that page.
110 *
111 * @var array
112 * @access protected
113 */
114 public $_queryParams;
115
116 /**
117 * represent the type of selector
118 *
119 * @var int
120 * @access protected
121 */
122 protected $_action;
123
124 /**
125 * The additional clause that we restrict the search with
126 *
127 * @var string
128 */
129 protected $_grantClause = null;
130
131 /**
132 * The query object
133 *
134 * @var string
135 */
136 protected $_query;
137
138 /**
139 * Class constructor
140 *
141 * @param array $queryParams array of parameters for query
142 * @param int $action - action of search basic or advanced.
143 * @param string $grantClause if the caller wants to further restrict the search
144 * @param boolean $single are we dealing only with one contact?
145 * @param int $limit how many participations do we want returned
146 *
147 * @return CRM_Contact_Selector
148 * @access public
149 */
150 function __construct(&$queryParams,
151 $action = CRM_Core_Action::NONE,
152 $grantClause = null,
153 $single = false,
154 $limit = null,
155 $context = 'search' )
156 {
157 // submitted form values
158 $this->_queryParams =& $queryParams;
159
160
161 $this->_single = $single;
162 $this->_limit = $limit;
163 $this->_context = $context;
164
165 $this->_grantClause = $grantClause;
166
167 // type of selector
168 $this->_action = $action;
169
170 $this->_query = new CRM_Grant_BAO_PaymentSearch( $this->_queryParams, null, null, false, false,
171 CRM_Grant_BAO_PaymentSearch::MODE_GRANT_PAYMENT );
172
173
174 $this->_query->_distinctComponentClause = " civicrm_payment.id";
175 $this->_query->_groupByComponentClause = " GROUP BY civicrm_payment.id";
176
177 }//end of constructor
178
179
180 /**
181 * This method returns the links that are given for each search row.
182 * currently the links added for each row are
183 *
184 * - View
185 * - Edit
186 *
187 * @return array
188 * @access public
189 *
190 */
191 static function &links( $key = null )
192 {
193 $id = CRM_Utils_Request::retrieve('id', 'Integer', $this);
194 $extraParams = ( $key ) ? "&key={$key}" : null;
195
196 if (!(self::$_links)) {
197 self::$_links = array(
198 CRM_Core_Action::VIEW => array(
199 'name' => ts('View'),
200 'url' => 'civicrm/grant/payment',
201 'qs' => 'reset=1&id=%%id%%&action=view&context=%%cxt%%&selectedChild=grant'.$extraParams,
202 'title' => ts('View Grant'),
203 ),
204 CRM_Core_Action::STOP => array(
205 'name' => ts('Stop'),
206 'url' => 'civicrm/grant/payment',
207 'qs' => 'reset=1&action=stop&id=%%id%%&context=%%cxt%%'.$extraParams,
208 'title' => ts('Edit Grant'),
209 ),
210 CRM_Core_Action::REPRINT => array(
211 'name' => ts('Reprint'),
212 'url' => 'civicrm/grant/payment',
213 'qs' => 'reset=1&action=reprint&id=%%id%%&context=%%cxt%%'.$extraParams,
214 'title' => ts('Edit Grant'),
215 ),
216 CRM_Core_Action::WITHDRAW => array(
217 'name' => ts('Withdraw'),
218 'url' => 'civicrm/grant/payment',
219 'qs' => 'reset=1&action=withdraw&id=%%id%%&context=%%cxt%%'.$extraParams,
220 'title' => ts('Edit Grant'),
221 )
222 );
223
224 self::$_links = self::$_links ;
225
226 }
227
228
229 return self::$_links;
230 } //end of function
231
232 /**
233 * getter for array of the parameters required for creating pager.
234 *
235 * @param
236 * @access public
237 */
238 function getPagerParams($action, &$params)
239 {
240 $params['status'] = ts('Grant') . ' %%StatusMessage%%';
241 $params['csvString'] = null;
242 if ( $this->_limit ) {
243 $params['rowCount'] = $this->_limit;
244 } else {
245 $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT;
246 }
247
248 $params['buttonTop'] = 'PagerTopButton';
249 $params['buttonBottom'] = 'PagerBottomButton';
250 } //end of function
251
252 /**
253 * Returns total number of rows for the query.
254 *
255 * @param
256 * @return int Total number of rows
257 * @access public
258 */
259 function getTotalCount($action)
260 {
261 return $this->_query->searchQuery( 0, 0, null,
262 true, false,
263 false, false,
264 false,
265 $this->_grantClause );
266
267 }
268
269
270 /**
271 * returns all the rows in the given offset and rowCount *
272 * @param enum $action the action being performed
273 * @param int $offset the row number to start from
274 * @param int $rowCount the number of rows to return
275 * @param string $sort the sql string that describes the sort order
276 * @param enum $output what should the result set include (web/email/csv)
277 *
278 * @return int the total number of rows for this action
279 */
280 function &getRows($action, $offset, $rowCount, $sort, $output = null)
281 {
282 $result = $this->_query->searchQuery( $offset, $rowCount, $sort,
283 false, false,
284 false, false,
285 false,
286 $this->_grantClause );
287
288
289
290
291 // process the result of the query
292 $rows = array( );
293
294 //CRM-4418 check for view, edit, delete
295 $permissions = array( CRM_Core_Permission::VIEW );
296 if ( CRM_Core_Permission::check( 'edit grants' ) ) {
297 $permissions[] = CRM_Core_Permission::EDIT;
298 }
299 if ( CRM_Core_Permission::check( 'delete in CiviGrant' ) ) {
300 $permissions[] = CRM_Core_Permission::DELETE;
301 }
302 $mask = CRM_Core_Action::mask( $permissions );
303
304
305 while ( $result->fetch()) {
306 $row = array();
307
308 // the columns we are interested in
309 foreach (self::$_properties as $property) {
310 if ( isset( $result->$property ) ) {
311 if ( $property == 'payment_status_id' ) {
312 require_once 'CRM/Core/OptionGroup.php';
313 $paymentStatus = CRM_Core_OptionGroup::values( 'grant_payment_status' );
314 $row[$property] = $paymentStatus[$result->$property];
315 } else {
316 $row[$property] = $result->$property;
317 }
318 }
319 }
320 // if ($this->_context == 'search') {
321 // $row['checkbox'] = CRM_Core_Form::CB_PREFIX . $result->id;
322 // }
323 $this->id = $result->id;
324 $link = self::links( $this->_key);
325 if ( $result->payment_status_id == 2 || $result->payment_status_id == 4 ) {
326 unset($link[CRM_Core_Action::STOP]);
327 unset($link[CRM_Core_Action::REPRINT]);
328 unset($link[CRM_Core_Action::WITHDRAW]);
329 }
330
331 $row['action'] = CRM_Core_Action::formLink( $link,
332 $mask,
333 array( 'id' => $result->id,
334 'cxt' => $this->_context ) );
335
336 $rows[] = $row;
337 }
338 return $rows;
339 }
340
341
342 /**
343 * @return array $qill which contains an array of strings
344 * @access public
345 */
346
347 // the current internationalisation is bad, but should more or less work
348 // for most of "European" languages
349 public function getQILL( )
350 {
351 return $this->_query->qill( );
352 }
353
354 /**
355 * returns the column headers as an array of tuples:
356 * (name, sortName (key to the sort array))
357 *
358 * @param string $action the action being performed
359 * @param enum $output what should the result set include (web/email/csv)
360 *
361 * @return array the column headers that need to be displayed
362 * @access public
363 */
364 public function &getColumnHeaders( $action = null, $output = null )
365 {
366 if ( ! isset( self::$_columnHeaders ) ) {
367 self::$_columnHeaders = array(
368 array('name' => ts('Status'),
369 'sort' => 'payment_status_id',
370 'direction' => CRM_Utils_Sort::DONTCARE,
371 ),
372 array(
373 'name' => ts('Batch Number'),
374 'sort' => 'payment_batch_number',
375 'direction' => CRM_Utils_Sort::DONTCARE,
376 ),
377 array(
378 'name' => ts('Payment Number'),
379 'sort' => 'payment_number',
380 'direction' => CRM_Utils_Sort::DONTCARE,
381 ),
382 array(
383 'name' => ts('Date'),
384 'sort' => 'payment_created_date',
385 'direction' => CRM_Utils_Sort::DONTCARE,
386 ),
387 array(
388 'name' => ts('Payee name'),
389 'sort' => 'payable_to_name',
390 'direction' => CRM_Utils_Sort::DONTCARE,
391 ),
392 array(
393 'name' => ts('Amount'),
394 'sort' => 'amount',
395 'direction' => CRM_Utils_Sort::DONTCARE,
396 ),
397 array('desc' => ts('Actions') ),
398 );
399 }
400 return self::$_columnHeaders;
401 }
402
403 function &getQuery( ) {
404 return $this->_query;
405 }
406
407 /**
408 * name of export file.
409 *
410 * @param string $output type of output
411 * @return string name of the file
412 */
413 function getExportFileName( $output = 'csv') {
414 return ts('CiviCRM Grant Search');
415 }
416
417 }//end of class
418
419