3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
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
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
38 * @copyright CiviCRM LLC (c) 2004-2014
42 class CRM_Core_Selector_Controller
{
45 * Constants to determine if we should store
46 * the output in the session or template
49 // move the values from the session to the template
50 const SESSION
= 1, TEMPLATE
= 2,
51 TRANSFER
= 4, EXPORT
= 8, SCREEN
= 16, PDF
= 32;
54 * A CRM Object that implements CRM_Core_Selector_API
65 * The current column to sort on
74 protected $_sortOrder;
77 * @var CRM_Utils_Pager
91 protected $_pagerOffset;
94 * Number of rows to return
97 protected $_pagerRowCount;
100 * Total number of rows
106 * The objectAction for the WebObject
111 * This caches the content for the display system
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)
124 protected $_embedded = FALSE;
127 * Are we in print mode? if so we need to modify the display
128 * functionality to do a minimal display :)
132 protected $_print = FALSE;
135 * The storage object (typically a form or a page)
142 * Output target, session, template or both?
149 * Prefif for the selector variables
156 * Cache the smarty template for efficiency reasons
158 * @var CRM_Core_Smarty
160 public static $_template;
163 * Array of properties that the controller dumps into the output object
168 public static $_properties = array('columnHeaders', 'rows', 'rowsEmpty');
171 * Should we compute actions dynamically (since they are quite verbose)
175 protected $_dynamicAction = FALSE;
180 * @param CRM_Core_Selector_API $object
181 * An object that implements the selector API.
187 * The actions to potentially support.
188 * @param CRM_Core_Page|CRM_Core_Form $store place in session to store some values
190 * What do we so with the output, session/template//both.
192 * @param null $prefix
195 * @return \CRM_Core_Selector_Controller
198 public function __construct($object, $pageID, $sortID, $action, $store = NULL, $output = self
::TEMPLATE
, $prefix = NULL, $case = NULL) {
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;
210 if ($this->_sortID
&& strpos($this->_sortID
, '_') === FALSE) {
211 $this->_sortID
.= '_u';
215 'pageID' => $this->_pageID
,
218 // let the constructor initialize this, should happen only once
219 if (!isset(self
::$_template)) {
220 self
::$_template = CRM_Core_Smarty
::singleton();
223 $this->_sortOrder
= &$this->_object
->getSortOrder($action);
224 $this->_sort
= new CRM_Utils_Sort($this->_sortOrder
, $this->_sortID
);
227 * if we are in transfer mode, do not goto database, use the
228 * session values instead
231 if ($output == self
::TRANSFER
) {
232 $params['total'] = $this->_store
->get($this->_prefix
. 'rowCount');
235 $params['total'] = $this->_object
->getTotalCount($action, $this->_case
);
238 $this->_total
= $params['total'];
239 $this->_object
->getPagerParams($action, $params);
242 * Set the default values of RowsPerPage
245 $storeRowCount = $store->get($this->_prefix
. CRM_Utils_Pager
::PAGE_ROWCOUNT
);
246 if ($storeRowCount) {
247 $params['rowCount'] = $storeRowCount;
249 elseif (!isset($params['rowCount'])) {
250 $params['rowCount'] = CRM_Utils_Pager
::ROWCOUNT
;
253 $this->_pager
= new CRM_Utils_Pager($params);
254 list($this->_pagerOffset
, $this->_pagerRowCount
) = $this->_pager
->getOffsetAndRowCount();
258 * Have the GET vars changed, i.e. pageId or sortId that forces us to recompute the search values
261 * Are we being reset.
263 * @return boolean if the GET params are different from the session params
265 public function hasChanged($reset) {
268 * if we are in reset state, i.e the store is cleaned out, we return false
269 * we also return if we dont have a record of the sort id or page id
272 $this->_store
->get($this->_prefix
. CRM_Utils_Pager
::PAGE_ID
) == NULL ||
273 $this->_store
->get($this->_prefix
. CRM_Utils_Sort
::SORT_ID
) == NULL
278 if ($this->_store
->get($this->_prefix
. CRM_Utils_Pager
::PAGE_ID
) != $this->_pager
->getCurrentPageID() ||
279 $this->_store
->get($this->_prefix
. CRM_Utils_Sort
::SORT_ID
) != $this->_sort
->getCurrentSortID() ||
280 $this->_store
->get($this->_prefix
. CRM_Utils_Sort
::SORT_DIRECTION
) != $this->_sort
->getCurrentSortDirection()
288 * Heart of the Controller. This is where all the action takes place
290 * - The rows are fetched and stored depending on the type of output needed
292 * - For export/printing all rows are selected.
294 * - for displaying on screen paging parameters are used to display the
297 * - also depending on output type of session or template rows are appropriately stored in session
298 * or template variables are updated.
304 public function run() {
306 // get the column headers
307 $columnHeaders = &$this->_object
->getColumnHeaders($this->_action
, $this->_output
);
309 $contextArray = explode('_', get_class($this->_object
));
311 $contextName = strtolower($contextArray[1]);
313 // fix contribute and member
314 if ($contextName == 'contribute') {
315 $contextName = 'contribution';
317 elseif ($contextName == 'member') {
318 $contextName = 'membership';
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
) {
328 CRM_Core_Report_Excel
::writeCSVFile($this->_object
->getExportFileName(),
332 CRM_Utils_System
::civiExit();
335 // assign to template and display them.
336 self
::$_template->assign_by_ref('rows', $rows);
337 self
::$_template->assign_by_ref('columnHeaders', $columnHeaders);
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);
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);
360 self
::$_template->assign_by_ref("{$this->_prefix}pager", $this->_pager
);
361 self
::$_template->assign_by_ref("{$this->_prefix}sort", $this->_sort
);
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);
371 // always store the current pageID and sortID
372 $this->_store
->set($this->_prefix
. CRM_Utils_Pager
::PAGE_ID
,
373 $this->_pager
->getCurrentPageID()
375 $this->_store
->set($this->_prefix
. CRM_Utils_Sort
::SORT_ID
,
376 $this->_sort
->getCurrentSortID()
378 $this->_store
->set($this->_prefix
. CRM_Utils_Sort
::SORT_DIRECTION
,
379 $this->_sort
->getCurrentSortDirection()
381 $this->_store
->set($this->_prefix
. CRM_Utils_Sort
::SORT_ORDER
,
382 $this->_sort
->orderBy()
384 $this->_store
->set($this->_prefix
. CRM_Utils_Pager
::PAGE_ROWCOUNT
,
385 $this->_pager
->_perPage
393 * @param CRM_Core_Form $form
395 * @return array of rows
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
);
403 return $form->_object
->getRows($form->_action
, $form->_pagerOffset
, $form->_pagerRowCount
,
404 $form->_sort
, $form->_output
, $form->_case
410 * Default function for qill, if needed to be implemented, we
411 * expect the subclass to do it
413 * @return string the status message
415 public function getQill() {
416 return $this->_object
->getQill();
422 public function getSummary() {
423 return $this->_object
->getSummary();
429 * @return CRM_Utils_Pager
431 public function getPager() {
432 return $this->_pager
;
438 * @return CRM_Utils_Sort
440 public function getSort() {
445 * Move the variables from the session to the template
449 public function moveFromSessionToTemplate() {
450 self
::$_template->assign_by_ref("{$this->_prefix}pager", $this->_pager
);
452 $rows = $this->_store
->get("{$this->_prefix}rows");
455 if ($this->_dynamicAction
) {
456 $this->_object
->addActions($rows);
459 self
::$_template->assign("{$this->_prefix}aToZ",
460 $this->_store
->get("{$this->_prefix}AToZBar")
464 self
::$_template->assign_by_ref("{$this->_prefix}sort", $this->_sort
);
465 self
::$_template->assign("{$this->_prefix}columnHeaders", $this->_store
->get("{$this->_prefix}columnHeaders"));
466 self
::$_template->assign("{$this->_prefix}rows", $rows);
467 self
::$_template->assign("{$this->_prefix}rowsEmpty", $this->_store
->get("{$this->_prefix}rowsEmpty"));
468 self
::$_template->assign("{$this->_prefix}qill", $this->_store
->get("{$this->_prefix}qill"));
469 self
::$_template->assign("{$this->_prefix}summary", $this->_store
->get("{$this->_prefix}summary"));
471 if ($this->_embedded
) {
475 self
::$_template->assign('tplFile', $this->_object
->getHookedTemplateFileName());
477 $content = self
::$_template->fetch('CRM/common/print.tpl');
480 $config = CRM_Core_Config
::singleton();
481 $content = self
::$_template->fetch('CRM/common/' . strtolower($config->userFramework
) . '.tpl');
483 echo CRM_Utils_System
::theme($content, $this->_print
);
487 * Setter for embedded
489 * @param bool $embedded
493 public function setEmbedded($embedded) {
494 $this->_embedded
= $embedded;
498 * Getter for embedded
500 * @return boolean return the embedded value
502 public function getEmbedded() {
503 return $this->_embedded
;
513 public function setPrint($print) {
514 $this->_print
= $print;
520 * @return boolean return the print value
522 public function getPrint() {
523 return $this->_print
;
529 public function setDynamicAction($value) {
530 $this->_dynamicAction
= $value;
536 public function getDynamicAction() {
537 return $this->_dynamicAction
;