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 | * This class is a generic class to be used when we want to display | |
31 | * a list of rows along with a set of associated actions | |
32 | * | |
33 | * Centralizing this code enables us to write a generic lister and enables | |
34 | * us to automate the export process. To use this class, the object has to | |
35 | * implement the Selector/Api.interface.php class | |
36 | * | |
37 | * @package CRM | |
06b69b18 | 38 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
39 | * $Id$ |
40 | * | |
41 | */ | |
42 | class CRM_Core_Selector_Controller { | |
43 | ||
44 | /** | |
100fef9d | 45 | * Constants to determine if we should store |
6a488035 TO |
46 | * the output in the session or template |
47 | * @var int | |
48 | */ | |
49 | // move the values from the session to the template | |
7da04cde | 50 | const SESSION = 1, TEMPLATE = 2, |
6a488035 TO |
51 | TRANSFER = 4, EXPORT = 8, SCREEN = 16, PDF = 32; |
52 | ||
53 | /** | |
100fef9d | 54 | * A CRM Object that implements CRM_Core_Selector_API |
6a488035 TO |
55 | * @var object |
56 | */ | |
57 | protected $_object; | |
58 | ||
c490a46a CW |
59 | /** |
60 | * @var CRM_Utils_Sort | |
61 | */ | |
6a488035 TO |
62 | protected $_sort; |
63 | ||
c490a46a | 64 | /** |
100fef9d | 65 | * The current column to sort on |
c490a46a CW |
66 | * @var int |
67 | */ | |
6a488035 TO |
68 | protected $_sortID; |
69 | ||
c490a46a | 70 | /** |
100fef9d | 71 | * The sortOrder array |
c490a46a CW |
72 | * @var array |
73 | */ | |
6a488035 TO |
74 | protected $_sortOrder; |
75 | ||
c490a46a CW |
76 | /** |
77 | * @var CRM_Utils_Pager | |
78 | */ | |
6a488035 TO |
79 | protected $_pager; |
80 | ||
c490a46a | 81 | /** |
100fef9d | 82 | * The pageID |
c490a46a CW |
83 | * @var int |
84 | */ | |
6a488035 TO |
85 | protected $_pageID; |
86 | ||
c490a46a | 87 | /** |
100fef9d | 88 | * Offset |
c490a46a CW |
89 | * @var int |
90 | */ | |
6a488035 TO |
91 | protected $_pagerOffset; |
92 | ||
93 | /** | |
100fef9d | 94 | * Number of rows to return |
6a488035 TO |
95 | * @var int |
96 | */ | |
97 | protected $_pagerRowCount; | |
98 | ||
99 | /** | |
100fef9d | 100 | * Total number of rows |
6a488035 TO |
101 | * @var int |
102 | */ | |
103 | protected $_total; | |
104 | ||
c490a46a | 105 | /** |
100fef9d | 106 | * The objectAction for the WebObject |
c490a46a | 107 | */ |
6a488035 TO |
108 | protected $_action; |
109 | ||
110 | /** | |
111 | * This caches the content for the display system | |
112 | * | |
113 | * @var string | |
114 | */ | |
115 | protected $_content; | |
116 | ||
117 | /** | |
118 | * Is this object being embedded in another object. If | |
119 | * so the display routine needs to not do any work. (The | |
120 | * parent object takes care of the display) | |
121 | * | |
122 | * @var boolean | |
123 | */ | |
124 | protected $_embedded = FALSE; | |
125 | ||
126 | /** | |
127 | * Are we in print mode? if so we need to modify the display | |
128 | * functionality to do a minimal display :) | |
129 | * | |
130 | * @var boolean | |
131 | */ | |
132 | protected $_print = FALSE; | |
133 | ||
134 | /** | |
135 | * The storage object (typically a form or a page) | |
136 | * | |
137 | * @var Object | |
138 | */ | |
139 | protected $_store; | |
140 | ||
141 | /** | |
142 | * Output target, session, template or both? | |
143 | * | |
144 | * @var int | |
145 | */ | |
146 | protected $_output; | |
147 | ||
148 | /** | |
149 | * Prefif for the selector variables | |
150 | * | |
151 | * @var int | |
152 | */ | |
153 | protected $_prefix; | |
154 | ||
155 | /** | |
100fef9d | 156 | * Cache the smarty template for efficiency reasons |
6a488035 TO |
157 | * |
158 | * @var CRM_Core_Smarty | |
159 | */ | |
160 | public static $_template; | |
161 | ||
162 | /** | |
163 | * Array of properties that the controller dumps into the output object | |
164 | * | |
165 | * @var array | |
166 | * @static | |
167 | */ | |
168 | public static $_properties = array('columnHeaders', 'rows', 'rowsEmpty'); | |
169 | ||
170 | /** | |
171 | * Should we compute actions dynamically (since they are quite verbose) | |
172 | * | |
173 | * @var boolean | |
174 | */ | |
175 | protected $_dynamicAction = FALSE; | |
176 | ||
177 | /** | |
178 | * Class constructor | |
179 | * | |
6a0b768e TO |
180 | * @param CRM_Core_Selector_API $object |
181 | * An object that implements the selector API. | |
182 | * @param int $pageID | |
183 | * Default pageID. | |
184 | * @param int $sortID | |
185 | * Default sortID. | |
186 | * @param int $action | |
187 | * The actions to potentially support. | |
da6b46f4 | 188 | * @param CRM_Core_Page|CRM_Core_Form $store place in session to store some values |
6a0b768e TO |
189 | * @param int $output |
190 | * What do we so with the output, session/template//both. | |
6a488035 | 191 | * |
da6b46f4 EM |
192 | * @param null $prefix |
193 | * @param null $case | |
194 | * | |
195 | * @return \CRM_Core_Selector_Controller | |
196 | @access public | |
197 | */ | |
00be9182 | 198 | public function __construct($object, $pageID, $sortID, $action, $store = NULL, $output = self::TEMPLATE, $prefix = NULL, $case = NULL) { |
6a488035 TO |
199 | |
200 | $this->_object = $object; | |
201 | $this->_pageID = $pageID ? $pageID : 1; | |
202 | $this->_sortID = $sortID ? $sortID : NULL; | |
203 | $this->_action = $action; | |
204 | $this->_store = $store; | |
205 | $this->_output = $output; | |
206 | $this->_prefix = $prefix; | |
207 | $this->_case = $case; | |
208 | ||
209 | // fix sortID | |
210 | if ($this->_sortID && strpos($this->_sortID, '_') === FALSE) { | |
211 | $this->_sortID .= '_u'; | |
212 | } | |
213 | ||
214 | $params = array( | |
215 | 'pageID' => $this->_pageID, | |
216 | ); | |
217 | ||
218 | // let the constructor initialize this, should happen only once | |
219 | if (!isset(self::$_template)) { | |
220 | self::$_template = CRM_Core_Smarty::singleton(); | |
221 | } | |
222 | ||
223 | $this->_sortOrder = &$this->_object->getSortOrder($action); | |
224 | $this->_sort = new CRM_Utils_Sort($this->_sortOrder, $this->_sortID); | |
225 | ||
226 | /* | |
f4bff68a CW |
227 | * if we are in transfer mode, do not goto database, use the |
228 | * session values instead | |
229 | */ | |
6a488035 TO |
230 | |
231 | if ($output == self::TRANSFER) { | |
232 | $params['total'] = $this->_store->get($this->_prefix . 'rowCount'); | |
233 | } | |
234 | else { | |
235 | $params['total'] = $this->_object->getTotalCount($action, $this->_case); | |
236 | } | |
237 | ||
238 | $this->_total = $params['total']; | |
239 | $this->_object->getPagerParams($action, $params); | |
240 | ||
241 | /* | |
f4bff68a CW |
242 | * Set the default values of RowsPerPage |
243 | */ | |
6a488035 TO |
244 | |
245 | $storeRowCount = $store->get($this->_prefix . CRM_Utils_Pager::PAGE_ROWCOUNT); | |
246 | if ($storeRowCount) { | |
247 | $params['rowCount'] = $storeRowCount; | |
248 | } | |
249 | elseif (!isset($params['rowCount'])) { | |
250 | $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT; | |
251 | } | |
252 | ||
253 | $this->_pager = new CRM_Utils_Pager($params); | |
254 | list($this->_pagerOffset, $this->_pagerRowCount) = $this->_pager->getOffsetAndRowCount(); | |
255 | } | |
256 | ||
257 | /** | |
100fef9d | 258 | * Have the GET vars changed, i.e. pageId or sortId that forces us to recompute the search values |
6a488035 | 259 | * |
6a0b768e TO |
260 | * @param int $reset |
261 | * Are we being reset. | |
6a488035 | 262 | * |
a6c01b45 CW |
263 | * @return boolean |
264 | * if the GET params are different from the session params | |
6a488035 | 265 | */ |
00be9182 | 266 | public function hasChanged($reset) { |
6a488035 TO |
267 | |
268 | /** | |
269 | * if we are in reset state, i.e the store is cleaned out, we return false | |
270 | * we also return if we dont have a record of the sort id or page id | |
271 | */ | |
272 | if ($reset || | |
273 | $this->_store->get($this->_prefix . CRM_Utils_Pager::PAGE_ID) == NULL || | |
274 | $this->_store->get($this->_prefix . CRM_Utils_Sort::SORT_ID) == NULL | |
275 | ) { | |
276 | return FALSE; | |
277 | } | |
278 | ||
279 | if ($this->_store->get($this->_prefix . CRM_Utils_Pager::PAGE_ID) != $this->_pager->getCurrentPageID() || | |
280 | $this->_store->get($this->_prefix . CRM_Utils_Sort::SORT_ID) != $this->_sort->getCurrentSortID() || | |
281 | $this->_store->get($this->_prefix . CRM_Utils_Sort::SORT_DIRECTION) != $this->_sort->getCurrentSortDirection() | |
282 | ) { | |
283 | return TRUE; | |
284 | } | |
285 | return FALSE; | |
286 | } | |
287 | ||
288 | /** | |
289 | * Heart of the Controller. This is where all the action takes place | |
290 | * | |
291 | * - The rows are fetched and stored depending on the type of output needed | |
292 | * | |
293 | * - For export/printing all rows are selected. | |
294 | * | |
295 | * - for displaying on screen paging parameters are used to display the | |
296 | * required rows. | |
297 | * | |
298 | * - also depending on output type of session or template rows are appropriately stored in session | |
299 | * or template variables are updated. | |
300 | * | |
301 | * | |
302 | * @return void | |
6a488035 | 303 | */ |
00be9182 | 304 | public function run() { |
6a488035 TO |
305 | |
306 | // get the column headers | |
307 | $columnHeaders = &$this->_object->getColumnHeaders($this->_action, $this->_output); | |
308 | ||
309 | $contextArray = explode('_', get_class($this->_object)); | |
310 | ||
311 | $contextName = strtolower($contextArray[1]); | |
312 | ||
313 | // fix contribute and member | |
314 | if ($contextName == 'contribute') { | |
315 | $contextName = 'contribution'; | |
316 | } | |
317 | elseif ($contextName == 'member') { | |
318 | $contextName = 'membership'; | |
319 | } | |
320 | ||
321 | // we need to get the rows if we are exporting or printing them | |
322 | if ($this->_output == self::EXPORT || $this->_output == self::SCREEN) { | |
323 | // get rows (without paging criteria) | |
324 | $rows = self::getRows($this); | |
325 | CRM_Utils_Hook::searchColumns($contextName, $columnHeaders, $rows, $this); | |
326 | if ($this->_output == self::EXPORT) { | |
327 | // export the rows. | |
328 | CRM_Core_Report_Excel::writeCSVFile($this->_object->getExportFileName(), | |
329 | $columnHeaders, | |
330 | $rows | |
331 | ); | |
332 | CRM_Utils_System::civiExit(); | |
333 | } | |
334 | else { | |
335 | // assign to template and display them. | |
336 | self::$_template->assign_by_ref('rows', $rows); | |
337 | self::$_template->assign_by_ref('columnHeaders', $columnHeaders); | |
338 | } | |
339 | } | |
340 | else { | |
341 | // output requires paging/sorting capability | |
342 | $rows = self::getRows($this); | |
343 | CRM_Utils_Hook::searchColumns($contextName, $columnHeaders, $rows, $this); | |
344 | $rowsEmpty = count($rows) ? FALSE : TRUE; | |
345 | $qill = $this->getQill(); | |
346 | $summary = $this->getSummary(); | |
347 | // if we need to store in session, lets update session | |
348 | if ($this->_output & self::SESSION) { | |
349 | $this->_store->set("{$this->_prefix}columnHeaders", $columnHeaders); | |
350 | if ($this->_dynamicAction) { | |
351 | $this->_object->removeActions($rows); | |
352 | } | |
353 | $this->_store->set("{$this->_prefix}rows", $rows); | |
354 | $this->_store->set("{$this->_prefix}rowCount", $this->_total); | |
355 | $this->_store->set("{$this->_prefix}rowsEmpty", $rowsEmpty); | |
356 | $this->_store->set("{$this->_prefix}qill", $qill); | |
357 | $this->_store->set("{$this->_prefix}summary", $summary); | |
358 | } | |
359 | else { | |
360 | self::$_template->assign_by_ref("{$this->_prefix}pager", $this->_pager); | |
361 | self::$_template->assign_by_ref("{$this->_prefix}sort", $this->_sort); | |
362 | ||
363 | self::$_template->assign_by_ref("{$this->_prefix}columnHeaders", $columnHeaders); | |
364 | self::$_template->assign_by_ref("{$this->_prefix}rows", $rows); | |
365 | self::$_template->assign("{$this->_prefix}rowsEmpty", $rowsEmpty); | |
366 | self::$_template->assign("{$this->_prefix}qill", $qill); | |
367 | self::$_template->assign("{$this->_prefix}summary", $summary); | |
368 | } | |
369 | ||
6a488035 TO |
370 | // always store the current pageID and sortID |
371 | $this->_store->set($this->_prefix . CRM_Utils_Pager::PAGE_ID, | |
372 | $this->_pager->getCurrentPageID() | |
373 | ); | |
374 | $this->_store->set($this->_prefix . CRM_Utils_Sort::SORT_ID, | |
375 | $this->_sort->getCurrentSortID() | |
376 | ); | |
377 | $this->_store->set($this->_prefix . CRM_Utils_Sort::SORT_DIRECTION, | |
378 | $this->_sort->getCurrentSortDirection() | |
379 | ); | |
380 | $this->_store->set($this->_prefix . CRM_Utils_Sort::SORT_ORDER, | |
381 | $this->_sort->orderBy() | |
382 | ); | |
383 | $this->_store->set($this->_prefix . CRM_Utils_Pager::PAGE_ROWCOUNT, | |
384 | $this->_pager->_perPage | |
385 | ); | |
386 | } | |
387 | } | |
388 | ||
389 | /** | |
100fef9d | 390 | * Retrieve rows. |
6a488035 | 391 | * |
c490a46a | 392 | * @param CRM_Core_Form $form |
2a6da8d7 | 393 | * |
a6c01b45 CW |
394 | * @return array |
395 | * of rows | |
6a488035 TO |
396 | */ |
397 | public function getRows($form) { | |
398 | if ($form->_output == self::EXPORT || $form->_output == self::SCREEN) { | |
399 | //get rows (without paging criteria) | |
400 | return $form->_object->getRows($form->_action, 0, 0, $form->_sort, $form->_output); | |
401 | } | |
402 | else { | |
403 | return $form->_object->getRows($form->_action, $form->_pagerOffset, $form->_pagerRowCount, | |
404 | $form->_sort, $form->_output, $form->_case | |
405 | ); | |
406 | } | |
407 | } | |
408 | ||
409 | /** | |
100fef9d | 410 | * Default function for qill, if needed to be implemented, we |
6a488035 TO |
411 | * expect the subclass to do it |
412 | * | |
a6c01b45 CW |
413 | * @return string |
414 | * the status message | |
6a488035 TO |
415 | */ |
416 | public function getQill() { | |
417 | return $this->_object->getQill(); | |
418 | } | |
419 | ||
a0ee3941 EM |
420 | /** |
421 | * @return mixed | |
422 | */ | |
6a488035 TO |
423 | public function getSummary() { |
424 | return $this->_object->getSummary(); | |
425 | } | |
426 | ||
427 | /** | |
100fef9d | 428 | * Getter for pager |
6a488035 | 429 | * |
c490a46a | 430 | * @return CRM_Utils_Pager |
6a488035 | 431 | */ |
00be9182 | 432 | public function getPager() { |
6a488035 TO |
433 | return $this->_pager; |
434 | } | |
435 | ||
436 | /** | |
100fef9d | 437 | * Getter for sort |
6a488035 | 438 | * |
c490a46a | 439 | * @return CRM_Utils_Sort |
6a488035 | 440 | */ |
00be9182 | 441 | public function getSort() { |
6a488035 TO |
442 | return $this->_sort; |
443 | } | |
444 | ||
445 | /** | |
446 | * Move the variables from the session to the template | |
447 | * | |
448 | * @return void | |
6a488035 | 449 | */ |
00be9182 | 450 | public function moveFromSessionToTemplate() { |
6a488035 TO |
451 | self::$_template->assign_by_ref("{$this->_prefix}pager", $this->_pager); |
452 | ||
453 | $rows = $this->_store->get("{$this->_prefix}rows"); | |
454 | ||
455 | if ($rows) { | |
456 | if ($this->_dynamicAction) { | |
457 | $this->_object->addActions($rows); | |
458 | } | |
459 | ||
460 | self::$_template->assign("{$this->_prefix}aToZ", | |
461 | $this->_store->get("{$this->_prefix}AToZBar") | |
462 | ); | |
463 | } | |
464 | ||
465 | self::$_template->assign_by_ref("{$this->_prefix}sort", $this->_sort); | |
466 | self::$_template->assign("{$this->_prefix}columnHeaders", $this->_store->get("{$this->_prefix}columnHeaders")); | |
467 | self::$_template->assign("{$this->_prefix}rows", $rows); | |
468 | self::$_template->assign("{$this->_prefix}rowsEmpty", $this->_store->get("{$this->_prefix}rowsEmpty")); | |
469 | self::$_template->assign("{$this->_prefix}qill", $this->_store->get("{$this->_prefix}qill")); | |
470 | self::$_template->assign("{$this->_prefix}summary", $this->_store->get("{$this->_prefix}summary")); | |
471 | ||
472 | if ($this->_embedded) { | |
473 | return; | |
474 | } | |
475 | ||
8aac22c8 | 476 | self::$_template->assign('tplFile', $this->_object->getHookedTemplateFileName()); |
6a488035 TO |
477 | if ($this->_print) { |
478 | $content = self::$_template->fetch('CRM/common/print.tpl'); | |
479 | } | |
480 | else { | |
481 | $config = CRM_Core_Config::singleton(); | |
482 | $content = self::$_template->fetch('CRM/common/' . strtolower($config->userFramework) . '.tpl'); | |
483 | } | |
484 | echo CRM_Utils_System::theme($content, $this->_print); | |
485 | } | |
486 | ||
487 | /** | |
100fef9d | 488 | * Setter for embedded |
6a488035 | 489 | * |
6a0b768e | 490 | * @param bool $embedded |
6a488035 TO |
491 | * |
492 | * @return void | |
6a488035 | 493 | */ |
00be9182 | 494 | public function setEmbedded($embedded) { |
6a488035 TO |
495 | $this->_embedded = $embedded; |
496 | } | |
497 | ||
498 | /** | |
100fef9d | 499 | * Getter for embedded |
6a488035 | 500 | * |
a6c01b45 CW |
501 | * @return boolean |
502 | * return the embedded value | |
6a488035 | 503 | */ |
00be9182 | 504 | public function getEmbedded() { |
6a488035 TO |
505 | return $this->_embedded; |
506 | } | |
507 | ||
508 | /** | |
100fef9d | 509 | * Setter for print |
6a488035 | 510 | * |
6a0b768e | 511 | * @param bool $print |
6a488035 TO |
512 | * |
513 | * @return void | |
6a488035 | 514 | */ |
00be9182 | 515 | public function setPrint($print) { |
6a488035 TO |
516 | $this->_print = $print; |
517 | } | |
518 | ||
519 | /** | |
100fef9d | 520 | * Getter for print |
6a488035 | 521 | * |
a6c01b45 CW |
522 | * @return boolean |
523 | * return the print value | |
6a488035 | 524 | */ |
00be9182 | 525 | public function getPrint() { |
6a488035 TO |
526 | return $this->_print; |
527 | } | |
528 | ||
a0ee3941 EM |
529 | /** |
530 | * @param $value | |
531 | */ | |
00be9182 | 532 | public function setDynamicAction($value) { |
6a488035 TO |
533 | $this->_dynamicAction = $value; |
534 | } | |
535 | ||
a0ee3941 EM |
536 | /** |
537 | * @return bool | |
538 | */ | |
00be9182 | 539 | public function getDynamicAction() { |
6a488035 TO |
540 | return $this->_dynamicAction; |
541 | } | |
542 | } |