Skip to content

Commit

Permalink
Pagination support is completed
Browse files Browse the repository at this point in the history
  • Loading branch information
simoneeconomo committed Jun 13, 2011
1 parent 89ed2e5 commit 542ded7
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 28 deletions.
67 changes: 66 additions & 1 deletion content/content.view.php
Expand Up @@ -48,9 +48,17 @@ public function view(){
'button', NULL, array('accesskey' => 'e'))->generate()
);

/* -----------------------------------------------------
* Fetching
* -----------------------------------------------------
*/

$result = DatasourceEngine::fetch(
$datasources->create($this->_context[0], NULL, false),
Administration::instance()
Administration::instance(),
array(
'startpage' => $_REQUEST['pg']
)
);

$format = new DatasourceFormatHTML($result, $engine);
Expand All @@ -64,6 +72,63 @@ public function view(){
);

$this->Form->appendChild($table);

/* -----------------------------------------------------
* Pagination
* -----------------------------------------------------
*/

$entries = $result['entries'];

if (isset($entries['total-entries']) && $entries['total-entries'] != count($entries['records'])) {
$current_page = (isset($_REQUEST['pg']) ? intval($_REQUEST['pg']) : 1);

$ul = new XMLElement('ul');
$ul->setAttribute('class', 'page');

## First
$li = new XMLElement('li');
if($current_page > 1)
$li->appendChild(Widget::Anchor(__('First'), Administration::instance()->getCurrentPageURL(). '?pg=1'));
else
$li->setValue(__('First'));
$ul->appendChild($li);

## Previous
$li = new XMLElement('li');
if($current_page > 1)
$li->appendChild(Widget::Anchor(__('← Previous'), Administration::instance()->getCurrentPageURL(). '?pg=' . ($current_page - 1)));
else
$li->setValue(__('← Previous'));
$ul->appendChild($li);

## Summary
$li = new XMLElement('li', __('Page %1$s of %2$s', array($current_page, max($current_page, $entries['total-pages']))));
$li->setAttribute('title', __('Viewing %1$s - %2$s of %3$s entries', array(
$entries['start'],
($current_page != $entries['total-pages']) ? $current_page * $entries['limit'] : $entries['total-entries'],
$entries['total-entries']
)));
$ul->appendChild($li);

## Next
$li = new XMLElement('li');
if($current_page < $entries['total-pages'])
$li->appendChild(Widget::Anchor(__('Next &rarr;'), Administration::instance()->getCurrentPageURL(). '?pg=' . ($current_page + 1)));
else
$li->setValue(__('Next &rarr;'));
$ul->appendChild($li);

## Last
$li = new XMLElement('li');
if($current_page < $entries['total-pages'])
$li->appendChild(Widget::Anchor(__('Last'), Administration::instance()->getCurrentPageURL(). '?pg=' . $entries['total-pages']));
else
$li->setValue(__('Last'));
$ul->appendChild($li);

$this->Form->appendChild($ul);
}
}

}
Expand Down
2 changes: 1 addition & 1 deletion extension.driver.php
Expand Up @@ -74,7 +74,7 @@ public function addViewsPreferences($context) {

$options = array();
foreach($datasources as $d) {
if (substr($d['type'], -3) != 'xml')
if (substr($d['type'], -3) != 'xml' || $d['type'] == 'navigation')
$options[] = array($d['handle'], @in_array($d['handle'], $active_views), $d['name']);
}

Expand Down
51 changes: 30 additions & 21 deletions lib/class.datasourceengine.php
Expand Up @@ -2,27 +2,27 @@

Class DatasourceEngine {

public static function fetch(Datasource $ds, $engine) {
public static function fetch(Datasource $ds, $engine, $context = array()) {
switch($ds->getSource()) {
# case 'navigation': {
# return self::__processNavigation($ds, $engine);
# return self::__processNavigation($ds, $engine, $context);
# }
# break;
case 'authors': {
return self::__processAuthors($ds, $engine);
return self::__processAuthors($ds, $engine, $context);
}
break;
default: {
if (preg_match("/^[0-9]*$/", $ds->getSource())) {
return self::__processSection($ds, $engine);
return self::__processSection($ds, $engine, $context);
} else {
throw new DatasourceNotSupportedException;
}
}
}
}

private static function __processNavigation($ds, $engine) {
private static function __processNavigation($ds, $engine, $context) {

/* -----------------------------------------------------
* Filtering
Expand Down Expand Up @@ -81,12 +81,14 @@ private static function __processNavigation($ds, $engine) {

return array(
'source' => 'navigation',
'records' => $pages,
'entries' => array(
'records' => $pages
),
'schema' => NULL,
);
}

private static function __processAuthors($ds, $engine) {
private static function __processAuthors($ds, $engine, $context) {

/* -----------------------------------------------------
* Filtering
Expand Down Expand Up @@ -149,12 +151,14 @@ private static function __processAuthors($ds, $engine) {

return array(
'source' => 'authors',
'records' => $wrappers,
'entries' => array(
'records' => $wrappers
),
'schema' => $ds->dsParamINCLUDEDELEMENTS
);
}

private static function __processSection($ds, $engine) {
private static function __processSection($ds, $engine, $context) {
$where = $joins = NULL;
$group = false;

Expand Down Expand Up @@ -237,12 +241,8 @@ private static function __processSection($ds, $engine) {
* Grouping & Pagination
* -----------------------------------------------------
*/

if(is_array($ds->dsParamINCLUDEDELEMENTS)) {
$include_pagination_element = in_array('system:pagination', $ds->dsParamINCLUDEDELEMENTS);
}

# $datasource_schema = $ds->dsParamINCLUDEDELEMENTS;
$datasource_schema = $ds->dsParamINCLUDEDELEMENTS;

# if (!is_array($datasource_schema))
# $datasource_schema = array();
Expand All @@ -257,6 +257,18 @@ private static function __processSection($ds, $engine) {
$ds->dsParamPAGINATERESULTS = 'yes';
}

if($context['startpage'] && preg_match("/^[1-9]+[0-9]*$/", intval($context['startpage']))) {
$startpage = $context['startpage'];
} else {
$startpage = ($ds->dsParamPAGINATERESULTS == 'yes' && $ds->dsParamSTARTPAGE > 0 ? $ds->dsParamSTARTPAGE : 1);
}

if ($ds->dsParamPAGINATERESULTS == 'yes' && $ds->dsParamLIMIT >= 0) {
$entries_per_page = $ds->dsParamLIMIT;
} else {
$entries_per_page = NULL;
}

/* -----------------------------------------------------
* Fields schema
* -----------------------------------------------------
Expand Down Expand Up @@ -293,13 +305,10 @@ private static function __processSection($ds, $engine) {
*/

$entries = $entryManager->fetchByPage(
($ds->dsParamPAGINATERESULTS == 'yes' && $ds->dsParamSTARTPAGE > 0 ? $ds->dsParamSTARTPAGE : 1),
$ds->getSource(),
($ds->dsParamPAGINATERESULTS == 'yes' && $ds->dsParamLIMIT >= 0 ? $ds->dsParamLIMIT : NULL),
$startpage, $ds->getSource(),
$entries_per_page,
$where, $joins, $group,
(!$include_pagination_element ? true : false),
true
# $datasource_schema
false, true, $datasource_schema
);

/**
Expand All @@ -325,7 +334,7 @@ private static function __processSection($ds, $engine) {

return array(
'source' => $section,
'records' => $entries['records'],
'entries' => $entries,
'schema' => $fields_schema
);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/class.datasourceformat.php
Expand Up @@ -7,7 +7,7 @@ abstract class DatasourceFormat {
protected $_engine;

public function __construct(Array $input, $engine) {
if (!is_array($input) || empty($input) || !isset($input['source']) || !isset($input['records']) || !isset($input['schema'])) {
if (!is_array($input) || empty($input) || array_keys($input) != array('source', 'entries', 'schema')) {
throw new Exception(
__('Invalid input array. Cannot generate output')
);
Expand All @@ -16,7 +16,7 @@ public function __construct(Array $input, $engine) {
$this->_input = $input;
$this->_output = array(
'schema' => NULL,
'records' => NULL
'records' => NULL,
);
$this->_engine = $engine;
}
Expand Down
6 changes: 3 additions & 3 deletions lib/class.datasourceformatHTML.php
Expand Up @@ -52,14 +52,14 @@ protected function __formatSchema() {
}

protected function __formatRecords() {
if(!is_array($this->_input['records']) || empty($this->_input['records'])){
if(!is_array($this->_input['entries']) || empty($this->_input['entries'])){
return $this->_output['records'] = array(
Widget::TableRow(array(Widget::TableData(__('None found.'), 'inactive', NULL, count($aTableHead))), 'odd')
);
}

if ($this->_input['source'] instanceof Section) {
foreach($this->_input['records'] as $entry) {
foreach($this->_input['entries']['records'] as $entry) {
$tableData = array();
$section = $this->_input['source']->get('handle');

Expand Down Expand Up @@ -122,7 +122,7 @@ protected function __formatRecords() {
}
}
else if ($this->_input['source'] == 'authors') {
foreach($this->_input['records'] as $author) {
foreach($this->_input['entries']['records'] as $author) {
$tableData = array();
$values = array();
$id = $author->getEncapsulatedObject()->get('id');
Expand Down

0 comments on commit 542ded7

Please sign in to comment.