Commit 1ed69a1e authored by merlinofchaos's avatar merlinofchaos

Pager settings in and working.

parent 999b2e53
...@@ -20,6 +20,7 @@ function views_views_plugins() { ...@@ -20,6 +20,7 @@ function views_views_plugins() {
'no ui' => TRUE, 'no ui' => TRUE,
'no remove' => TRUE, 'no remove' => TRUE,
'js' => array('misc/collapse.js', 'misc/textarea.js', 'misc/tabledrag.js'), 'js' => array('misc/collapse.js', 'misc/textarea.js', 'misc/tabledrag.js'),
'use pager' => TRUE,
), ),
'page' => array( 'page' => array(
'title' => t('Page'), 'title' => t('Page'),
...@@ -27,6 +28,7 @@ function views_views_plugins() { ...@@ -27,6 +28,7 @@ function views_views_plugins() {
'handler' => 'views_display_plugin_page', 'handler' => 'views_display_plugin_page',
'uses hook menu' => TRUE, 'uses hook menu' => TRUE,
'has path' => TRUE, 'has path' => TRUE,
'use pager' => TRUE,
), ),
'block' => array( 'block' => array(
'title' => t('Block'), 'title' => t('Block'),
...@@ -164,8 +166,10 @@ class views_display_plugin extends views_object { ...@@ -164,8 +166,10 @@ class views_display_plugin extends views_object {
'header' => array('header', 'header_format', 'header_empty'), 'header' => array('header', 'header_format', 'header_empty'),
'footer' => array('footer', 'footer_format', 'footer_empty'), 'footer' => array('footer', 'footer_format', 'footer_empty'),
'empty' => array('empty', 'empty_format'), 'empty' => array('empty', 'empty_format'),
// @todo
'items_per_page' => array('items_per_page', 'offset', 'use_pager', 'pager_element'), 'items_per_page' => array('items_per_page', 'offset', 'use_pager', 'pager_element'),
'use_pager' => array('items_per_page', 'offset', 'use_pager', 'pager_element'),
// @todo
'php_arg_code' => array('php_arg_code'), 'php_arg_code' => array('php_arg_code'),
'link_display' => array('link_display'), 'link_display' => array('link_display'),
'exposed_options' => array('exposed_options'), 'exposed_options' => array('exposed_options'),
...@@ -362,6 +366,19 @@ class views_display_plugin extends views_object { ...@@ -362,6 +366,19 @@ class views_display_plugin extends views_object {
break; break;
} }
if (!empty($this->definition['use pager'])) {
$options['use_pager'] = array(
'category' => 'basic',
'title' => t('Use pager'),
'value' => $this->get_option('use_pager') ? t('Yes') : t('No'),
);
}
$options['items_per_page'] = array(
'category' => 'basic',
'title' => $this->get_option('use_pager') ? t('Items per page') : t('Items to display'),
'value' => intval($this->get_option('items_per_page')),
);
$options['access'] = array( $options['access'] = array(
'category' => 'basic', 'category' => 'basic',
'title' => t('Access'), 'title' => t('Access'),
...@@ -428,6 +445,33 @@ class views_display_plugin extends views_object { ...@@ -428,6 +445,33 @@ class views_display_plugin extends views_object {
'#default_value' => $this->get_option('title'), '#default_value' => $this->get_option('title'),
); );
break; break;
case 'use_pager':
$form['#title'] .= t('Use a pager for this view');
$form['use_pager'] = array(
'#type' => 'radios',
'#options' => array(1 => t('Yes'), 0 => t('No')),
'#default_value' => $this->get_option('use_pager'),
);
$form['pager_element'] = array(
'#type' => 'textfield',
'#title' => t('Pager element'),
'#description' => t('Which element of the ?page= array to use, if the pager is enabled. 0 is recommended, but if there are other pagers on the page, you may use a higher number so as not to conflict. Large values are bad here as they will add a lot of commas to the URLs.'),
'#default_value' => intval($this->get_option('pager_element')),
);
break;
case 'items_per_page':
$form['items_per_page'] = array(
'#type' => 'textfield',
'#description' => t('The number of items to display per page. Enter 0 for no limit.'),
'#default_value' => intval($this->get_option('items_per_page')),
);
$form['offset'] = array(
'#type' => 'textfield',
'#title' => t('Offset'),
'#description' => t('The number of items to skip. For example, if this field is 3, the first 3 items will be skipped and not displayed.'),
'#default_value' => intval($this->get_option('offset')),
);
break;
case 'access': case 'access':
$form['#title'] .= t('Access restrictions'); $form['#title'] .= t('Access restrictions');
$form['access'] = array( $form['access'] = array(
...@@ -441,7 +485,7 @@ class views_display_plugin extends views_object { ...@@ -441,7 +485,7 @@ class views_display_plugin extends views_object {
$access = array('type' => 'none', 'role' => array(), 'perm' => ''); $access = array('type' => 'none', 'role' => array(), 'perm' => '');
} }
$form['access']['type'] = array( $form['access']['type'] = array(
'#prefix' => '<div class="views-left-20">', '#prefix' => '<div class="views-left-25">',
'#suffix' => '</div>', '#suffix' => '</div>',
'#title' => t('Type'), '#title' => t('Type'),
'#type' => 'radios', '#type' => 'radios',
...@@ -594,6 +638,14 @@ class views_display_plugin extends views_object { ...@@ -594,6 +638,14 @@ class views_display_plugin extends views_object {
case 'access': case 'access':
$this->set_option($section, $form_state['values'][$section]); $this->set_option($section, $form_state['values'][$section]);
break; break;
case 'use_pager':
$this->set_option($section, $form_state['values'][$section]);
$this->set_option('pager_element', intval($form_state['values']['pager_element']));
break;
case 'items_per_page':
$this->set_option($section, intval($form_state['values'][$section]));
$this->set_option('offset', intval($form_state['values']['offset']));
break;
case 'row_plugin': case 'row_plugin':
// This if prevents resetting options to default if they don't change // This if prevents resetting options to default if they don't change
// the plugin. // the plugin.
...@@ -723,8 +775,10 @@ class views_display_plugin extends views_object { ...@@ -723,8 +775,10 @@ class views_display_plugin extends views_object {
function render_title() { } function render_title() { }
function render_textarea($area) { function render_textarea($area) {
$format_string = $area . '_format'; $value = $this->get_option($area);
return check_markup($this->get_option($area), $this->get_option($format_string)); if ($value) {
return check_markup($value, $this->get_option($area . '_format'));
}
} }
/** /**
...@@ -800,6 +854,8 @@ class views_display_plugin_default extends views_display_plugin { ...@@ -800,6 +854,8 @@ class views_display_plugin_default extends views_display_plugin {
$display->display_options['sorts'] = array(); $display->display_options['sorts'] = array();
$display->display_options['arguments'] = array(); $display->display_options['arguments'] = array();
$display->display_options['filters'] = array(); $display->display_options['filters'] = array();
$display->display_options['items_per_page'] = 10;
} }
/** /**
......
...@@ -28,11 +28,14 @@ class view extends views_db_object { ...@@ -28,11 +28,14 @@ class view extends views_db_object {
var $build_info = array(); var $build_info = array();
// pager variables // pager variables
var $use_pager = FALSE; var $pager = array(
var $page_size = 25; 'use_pager' => FALSE,
var $pager_element = 0; 'items_per_page' => 10,
var $offset = 0; 'element' => 0,
var $current_page = 0; 'offset' => 0,
'current_page' => 0,
);
/** /**
* Constructor * Constructor
*/ */
...@@ -76,22 +79,22 @@ class view extends views_db_object { ...@@ -76,22 +79,22 @@ class view extends views_db_object {
* *
* @todo move all pager properties into a pager object * @todo move all pager properties into a pager object
*/ */
function set_page_size($page_size) { function set_items_per_page($items_per_page) {
$this->page_size = $page_size; $this->pager['items_per_page'] = $items_per_page;
} }
/** /**
* Whether or not the pager should be used. * Whether or not the pager should be used.
*/ */
function set_use_pager($use_pager) { function set_use_pager($use_pager) {
$this->use_pager = $use_pager; $this->pager['use_pager'] = $use_pager;
} }
/** /**
* The pager element id to use if use_apger is on * The pager element id to use if use_apger is on
*/ */
function set_pager_element($pager_element) { function set_pager_element($pager_element) {
$this->pager_element = $pager_element; $this->pager['element'] = $pager_element;
} }
/** /**
...@@ -99,7 +102,7 @@ class view extends views_db_object { ...@@ -99,7 +102,7 @@ class view extends views_db_object {
* set. * set.
*/ */
function set_offset($offset) { function set_offset($offset) {
$this->offset = $offset; $this->pager['offset'] = $offset;
} }
/** /**
...@@ -376,19 +379,24 @@ class view extends views_db_object { ...@@ -376,19 +379,24 @@ class view extends views_db_object {
$query = db_rewrite_sql($this->build_info['query'], $this->base_table, $this->base_field); $query = db_rewrite_sql($this->build_info['query'], $this->base_table, $this->base_field);
$args = $this->build_info['query_args']; $args = $this->build_info['query_args'];
// @todo: Fill in execution defaults if they are not set. vpr($query);
// use_pager, page_size, offset
$items = array(); $items = array();
if ($query) { if ($query) {
if (!empty($this->use_pager)) { if (!empty($this->pager['use_pager'])) {
$count_query = db_rewrite_sql($this->build_info['count_query'], $this->base_table, $this->base_field); $count_query = db_rewrite_sql($this->build_info['count_query'], $this->base_table, $this->base_field);
$this->result = pager_query($query, $view->page_size, $view->use_pager - 1, $count_query, $args);
$this->total_rows = $GLOBALS['pager_total_items'][$this->use_pager - 1]; $this->result = pager_query($query, $this->pager['items_per_page'], $this->pager['element'], $count_query, $args);
$this->total_rows = $GLOBALS['pager_total_items'][$this->pager['element']];
} }
else if (!empty($this->page_size)) { else if (!empty($this->pager['items_per_page'])) {
$offset = $this->current_page * $this->page_size + $this->offset; $offset = $this->pager['current_page'] * $this->pager['items_per_page'] + $this->pager['offset'];
$this->result = db_query_range($query, $args, $offset, $this->page_size); $this->result = db_query_range($query, $args, $offset, $this->pager['items_per_page']);
// Rewrite and run the count query so we can store how many items this would have
// matched.
$count_query = db_rewrite_sql($this->build_info['count_query'], $this->base_table, $this->base_field);
$this->total_rows = db_result(db_query($count_query));
} }
else { else {
$this->result = db_query($query, $args); $this->result = db_query($query, $args);
...@@ -423,12 +431,18 @@ class view extends views_db_object { ...@@ -423,12 +431,18 @@ class view extends views_db_object {
* the given arguments. * the given arguments.
*/ */
function execute_display($display_id = NULL, $args = array()) { function execute_display($display_id = NULL, $args = array()) {
// Prepare the view with the information we have.
$this->set_arguments($args);
if (!$this->set_display($display_id)) { if (!$this->set_display($display_id)) {
return FALSE; return FALSE;
} }
// Prepare the view with the information we have.
$this->set_arguments($args);
$this->set_use_pager($this->display_handler->get_option('use_pager'));
$this->set_items_per_page($this->display_handler->get_option('items_per_page'));
$this->set_pager_element($this->display_handler->get_option('pager_element'));
$this->set_offset($this->display_handler->get_option('offset'));
// Execute the view // Execute the view
return $this->display_handler->execute(); return $this->display_handler->execute();
} }
......
...@@ -24,8 +24,8 @@ function template_preprocess_views_view(&$vars) { ...@@ -24,8 +24,8 @@ function template_preprocess_views_view(&$vars) {
$vars['feed_icon'] = $view->display_handler->render_feed_icon(); $vars['feed_icon'] = $view->display_handler->render_feed_icon();
$vars['pager'] = ''; $vars['pager'] = '';
if (!empty($view->use_pager)) { if (!empty($view->pager['use_pager'])) {
$vars['pager'] = theme('pager', array(), $view->page_size, $view->page_element); $vars['pager'] = theme('pager', array(), $view->pager['items_per_page'], $view->pager['element']);
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment