• Main Page
  • Related Pages
  • Modules
  • Classes
  • Files
  • File List
  • File Members

plugins/views_plugin_pager_full.inc

00001 <?php
00002 
00008 class views_plugin_pager_full extends views_plugin_pager {
00009   function summary_title() {
00010     if (!empty($this->options['offset'])) {
00011       return format_plural($this->options['items_per_page'], '@count item, skip @skip', 'Paged, @count items, skip @skip', array('@count' => $this->options['items_per_page'], '@skip' => $this->options['offset']));
00012     }
00013       return format_plural($this->options['items_per_page'], '@count item', 'Paged, @count items', array('@count' => $this->options['items_per_page']));
00014   }
00015 
00016   function option_definition() {
00017     $options = parent::option_definition();
00018     $options['items_per_page'] = array('default' => 10);
00019     $options['offset'] = array('default' => 0);
00020     $options['id'] = array('default' => 0);
00021     $options['total_pages'] = array('default' => '');
00022     $options['expose'] = array(
00023       'contains' => array(
00024         'items_per_page' => array('default' => FALSE, 'bool' => TRUE),
00025         'items_per_page_label' => array('default' => 'Items per page', 'translatable' => TRUE),
00026         'items_per_page_options' => array('default' => '5, 10, 20, 40, 60'),
00027         'items_per_page_options_all' => array('default' => FALSE),
00028         'items_per_page_options_all_label' => array('default' => '- All -', 'translatable' => TRUE),
00029 
00030         'offset' => array('default' => FALSE, 'bool' => TRUE),
00031         'offset_label' => array('default' => 'Offset', 'translatable' => TRUE),
00032       ),
00033     );
00034     return $options;
00035   }
00036 
00040   function options_form(&$form, &$form_state) {
00041     parent::options_form($form, $form_state);
00042     $pager_text = $this->display->handler->get_pager_text();
00043     $form['items_per_page'] = array(
00044       '#title' => $pager_text['items per page title'],
00045       '#type' => 'textfield',
00046       '#description' => $pager_text['items per page description'],
00047       '#default_value' => $this->options['items_per_page'],
00048     );
00049 
00050     $form['offset'] = array(
00051       '#type' => 'textfield',
00052       '#title' => t('Offset'),
00053       '#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.'),
00054       '#default_value' => $this->options['offset'],
00055     );
00056 
00057     $form['id'] = array(
00058       '#type' => 'textfield',
00059       '#title' => t('Pager ID'),
00060       '#description' => t("Unless you're experiencing problems with pagers related to this view, you should leave this at 0. If using multiple pagers on one page you may need to set this number to a higher value so as not to conflict within the ?page= array. Large values will add a lot of commas to your URLs, so avoid if possible."),
00061       '#default_value' => $this->options['id'],
00062     );
00063 
00064     $form['total_pages'] = array(
00065       '#type' => 'textfield',
00066       '#title' => t('Number of pages'),
00067       '#description' => t('The total number of pages. Leave empty to show all pages.'),
00068       '#default_value' => $this->options['total_pages'],
00069     );
00070 
00071     $form['expose'] = array (
00072       '#type' => 'fieldset',
00073       '#collapsible' => FALSE,
00074       '#collapsed' => FALSE,
00075       '#tree' => TRUE,
00076       '#title' => t('Exposed options'),
00077       '#input' => TRUE,
00078       '#description' => t('Exposing this options allows users to define their values in a exposed form when view is displayed'),
00079     );
00080 
00081     $form['expose']['items_per_page'] = array(
00082       '#type' => 'checkbox',
00083       '#title' => t('Expose items per page'),
00084       '#description' => t('When checked, users can determine how many items per page show in a view'),
00085       '#default_value' => $this->options['expose']['items_per_page'],
00086     );
00087 
00088     $form['expose']['items_per_page_label'] = array(
00089       '#type' => 'textfield',
00090       '#title' => t('Items per page label'),
00091       '#required' => TRUE,
00092       '#description' => t('Label to use in the exposed items per page form element.'),
00093       '#default_value' => $this->options['expose']['items_per_page_label'],
00094       '#dependency' => array(
00095         'edit-pager-options-expose-items-per-page' => array(1)
00096       ),
00097     );
00098 
00099     $form['expose']['items_per_page_options'] = array(
00100       '#type' => 'textfield',
00101       '#title' => t('Exposed items per page options'),
00102       '#required' => TRUE,
00103       '#description' => t('Set between which values the user can choose when determining the items per page. Separated by comma.'),
00104       '#default_value' => $this->options['expose']['items_per_page_options'],
00105       '#dependency' => array(
00106         'edit-pager-options-expose-items-per-page' => array(1)
00107       ),
00108     );
00109 
00110 
00111     $form['expose']['items_per_page_options_all'] = array(
00112       '#type' => 'checkbox',
00113       '#title' => t('Include all items option'),
00114       '#description' => t('If checked, an extra item will be included to items per page to display all items'),
00115       '#default_value' => $this->options['expose']['items_per_page_options_all'],
00116     );
00117 
00118     $form['expose']['items_per_page_options_all_label'] = array(
00119       '#type' => 'textfield',
00120       '#title' => t('All items label'),
00121       '#description' => t('Which label will be used to display all items'),
00122       '#default_value' => $this->options['expose']['items_per_page_options_all_label'],
00123       '#dependency' => array(
00124         'edit-items-per-page-options-all' => array(1),
00125       ),
00126     );
00127 
00128     $form['expose']['offset'] = array(
00129       '#type' => 'checkbox',
00130       '#title' => t('Expose Offset'),
00131       '#description' => t('When checked, users can determine how many items should be skipped at the beginning.'),
00132       '#default_value' => $this->options['expose']['offset'],
00133     );
00134 
00135     $form['expose']['offset_label'] = array(
00136       '#type' => 'textfield',
00137       '#title' => t('Offset label'),
00138       '#required' => TRUE,
00139       '#description' => t('Label to use in the exposed offset form element.'),
00140       '#default_value' => $this->options['expose']['offset_label'],
00141       '#dependency' => array(
00142         'edit-pager-options-expose-offset' => array(1)
00143       ),
00144     );
00145   }
00146 
00147   function options_validate(&$form, &$form_state) {
00148     // Only accept integer values.
00149     $error = FALSE;
00150     $exposed_options = $form_state['values']['pager_options']['expose']['items_per_page_options'];
00151     if (strpos($exposed_options, '.') !== FALSE) {
00152       $error = TRUE;
00153     }
00154     $options = explode(',',$exposed_options);
00155     if (!$error && is_array($options)) {
00156       foreach ($options as $option) {
00157         if (!is_numeric($option) || intval($option) == 0) {
00158           $error = TRUE;
00159         }
00160       }
00161     }
00162     else {
00163       $error = TRUE;
00164     }
00165     if ($error) {
00166       form_set_error('pager_options][expose][items_per_page_options', t('Please insert a list of integer numeric values separated by commas: e.g: 10, 20, 50, 100'));
00167     }
00168 
00169     // Take sure that the items_per_page is part of the expose settings.
00170     if (!empty($form_state['values']['pager_options']['expose']['items_per_page']) && !empty($form_state['values']['pager_options']['items_per_page'])) {
00171       $items_per_page = $form_state['values']['pager_options']['items_per_page'];
00172       if (array_search($items_per_page, $options) === FALSE) {
00173         form_set_error('pager_options][expose][items_per_page_options', t('Please insert the items per page (@items_per_page) from above.',
00174           array('@items_per_page' => $items_per_page))
00175         );
00176       }
00177     }
00178   }
00179 
00180   function query() {
00181     if ($this->items_per_page_exposed()) {
00182       if (!empty($_GET['items_per_page']) && $_GET['items_per_page'] > 0) {
00183         $this->options['items_per_page'] = $_GET['items_per_page'];
00184       }
00185       elseif (!empty($_GET['items_per_page']) && $_GET['items_per_page'] == 'All' && $this->options['expose']['items_per_page_options_all']) {
00186         $this->options['items_per_page'] = 0;
00187       }
00188     }
00189     if ($this->offset_exposed()) {
00190       if (isset($_GET['offset']) && $_GET['offset'] >= 0) {
00191         $this->options['offset'] = $_GET['offset'];
00192       }
00193     }
00194 
00195     $limit = $this->options['items_per_page'];
00196     $offset = $this->current_page * $this->options['items_per_page'] + $this->options['offset'];
00197     if (!empty($this->options['total_pages'])) {
00198       if ($this->current_page >= $this->options['total_pages']) {
00199         $limit = $this->options['items_per_page'];
00200         $offset = $this->options['total_pages'] * $this->options['items_per_page'];
00201       }
00202     }
00203 
00204     $this->view->query->set_limit($limit);
00205     $this->view->query->set_offset($offset);
00206   }
00207 
00208   function render($input) {
00209     $pager_theme = views_theme_functions('pager', $this->view, $this->display);
00210     $output = theme($pager_theme, array(
00211       'parameters' => $input, 'element' => $this->options['id']));
00212     return $output;
00213   }
00214 
00222   function set_current_page($number = NULL) {
00223     if (isset($number)) {
00224       $this->current_page = $number;
00225       return;
00226     }
00227 
00228     // If the current page number was not specified, extract it from the global
00229     // page array.
00230     global $pager_page_array;
00231 
00232     if (empty($pager_page_array)) {
00233       $pager_page_array = array();
00234     }
00235 
00236     // Fill in missing values in the global page array, in case the global page
00237     // array hasn't been initialized before.
00238     $page = isset($_GET['page']) ? explode(',', $_GET['page']) : array();
00239 
00240     for ($i = 0; $i <= $this->options['id'] || $i < count($pager_page_array); $i++) {
00241       $pager_page_array[$i] = empty($page[$i]) ? 0 : $page[$i];
00242     }
00243 
00244     $this->current_page = intval($pager_page_array[$this->options['id']]);
00245 
00246     if ($this->current_page < 0) {
00247       $this->current_page = 0;
00248     }
00249   }
00250 
00251   function get_pager_total() {
00252     if ($items_per_page = intval($this->get_items_per_page())) {
00253       return ceil($this->total_items / $items_per_page);
00254     }
00255     else {
00256       return 1;
00257     }
00258   }
00259 
00267   function update_page_info() {
00268     if (!empty($this->options['total_pages'])) {
00269       if (($this->options['total_pages'] * $this->options['items_per_page']) < $this->total_items) {
00270         $this->total_items = $this->options['total_pages'] * $this->options['items_per_page'];
00271       }
00272     }
00273 
00274     // Don't set pager settings for items per page = 0.
00275     $items_per_page = $this->get_items_per_page();
00276     if (!empty($items_per_page)) {
00277       // Dump information about what we already know into the globals.
00278       global $pager_page_array, $pager_total, $pager_total_items, $pager_limits;
00279       // Set the limit.
00280       $pager_limits[$this->options['id']] = $this->options['items_per_page'];
00281       // Set the item count for the pager.
00282       $pager_total_items[$this->options['id']] = $this->total_items;
00283       // Calculate and set the count of available pages.
00284       $pager_total[$this->options['id']] = $this->get_pager_total();
00285 
00286       // See if the requested page was within range:
00287       if ($this->current_page < 0) {
00288         $this->current_page = 0;
00289       }
00290       else if ($this->current_page >= $pager_total[$this->options['id']]) {
00291         // Pages are numbered from 0 so if there are 10 pages, the last page is 9.
00292         $this->current_page = $pager_total[$this->options['id']] - 1;
00293       }
00294 
00295       // Put this number in to guarantee that we do not generate notices when the pager
00296       // goes to look for it later.
00297       $pager_page_array[$this->options['id']] = $this->current_page;
00298     }
00299   }
00300 
00301   function uses_exposed() {
00302     return $this->items_per_page_exposed() || $this->offset_exposed();
00303   }
00304 
00305   function items_per_page_exposed() {
00306     return !empty($this->options['expose']['items_per_page']);
00307   }
00308 
00309   function offset_exposed() {
00310     return !empty($this->options['expose']['offset']);
00311   }
00312 
00313   function exposed_form_alter(&$form, &$form_state) {
00314     if ($this->items_per_page_exposed()) {
00315       $options = explode(',', $this->options['expose']['items_per_page_options']);
00316       $sanitized_options = array();
00317       if (is_array($options)) {
00318         foreach ($options as $option) {
00319           $sanitized_options[intval($option)] = intval($option);
00320         }
00321         if (!empty($this->options['expose']['items_per_page_options_all']) && !empty($this->options['expose']['items_per_page_options_all_label'])) {
00322           $sanitized_options['All'] = $this->options['expose']['items_per_page_options_all_label'];
00323         }
00324         $form['items_per_page'] = array(
00325           '#type' => 'select',
00326           '#title' => $this->options['expose']['items_per_page_label'],
00327           '#options' => $sanitized_options,
00328           '#default_value' => $this->get_items_per_page(),
00329         );
00330       }
00331     }
00332 
00333     if ($this->offset_exposed()) {
00334       $form['offset'] = array(
00335         '#type' => 'textfield',
00336         '#size' => 10,
00337         '#maxlength' => 10,
00338         '#title' => $this->options['expose']['offset_label'],
00339         '#default_value' => $this->get_offset(),
00340       );
00341     }
00342   }
00343 
00344   function exposed_form_validate(&$form, &$form_state) {
00345     if (!empty($form_state['values']['offset']) && trim($form_state['values']['offset'])) {
00346       if (!is_numeric($form_state['values']['offset']) || $form_state['values']['offset'] < 0) {
00347         form_set_error('offset', t('Offset must be an number greather or equal than 0.'));
00348       }
00349     }
00350   }
00351 }

Generated on Sun Feb 26 2012 12:52:52 for Views by  doxygen 1.7.1