views_plugin_pager_full.inc

Definition of views_plugin_pager_full.

File

plugins/views_plugin_pager_full.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Definition of views_plugin_pager_full.
  5. */
  6. /**
  7. * The plugin to handle full pager.
  8. *
  9. * @ingroup views_pager_plugins
  10. */
  11. class views_plugin_pager_full extends views_plugin_pager {
  12. function summary_title() {
  13. if (!empty($this->options['offset'])) {
  14. 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']));
  15. }
  16. return format_plural($this->options['items_per_page'], '@count item', 'Paged, @count items', array('@count' => $this->options['items_per_page']));
  17. }
  18. function option_definition() {
  19. $options = parent::option_definition();
  20. $options['items_per_page'] = array('default' => 10);
  21. $options['offset'] = array('default' => 0);
  22. $options['id'] = array('default' => 0);
  23. $options['total_pages'] = array('default' => '');
  24. // Use the same default quantity that core uses by default.
  25. $options['quantity'] = array('default' => 9);
  26. $options['expose'] = array(
  27. 'contains' => array(
  28. 'items_per_page' => array('default' => FALSE, 'bool' => TRUE),
  29. 'items_per_page_label' => array('default' => 'Items per page', 'translatable' => TRUE),
  30. 'items_per_page_options' => array('default' => '5, 10, 20, 40, 60'),
  31. 'items_per_page_options_all' => array('default' => FALSE, 'bool' => TRUE),
  32. 'items_per_page_options_all_label' => array('default' => '- All -', 'translatable' => TRUE),
  33. 'offset' => array('default' => FALSE, 'bool' => TRUE),
  34. 'offset_label' => array('default' => 'Offset', 'translatable' => TRUE),
  35. ),
  36. );
  37. $options['tags'] = array(
  38. 'contains' => array(
  39. 'first' => array('default' => '« first', 'translatable' => TRUE),
  40. 'previous' => array('default' => '‹ previous', 'translatable' => TRUE),
  41. 'next' => array('default' => 'next ›', 'translatable' => TRUE),
  42. 'last' => array('default' => 'last »', 'translatable' => TRUE),
  43. ),
  44. );
  45. return $options;
  46. }
  47. /**
  48. * Provide the default form for setting options.
  49. */
  50. function options_form(&$form, &$form_state) {
  51. parent::options_form($form, $form_state);
  52. $pager_text = $this->display->handler->get_pager_text();
  53. $form['items_per_page'] = array(
  54. '#title' => $pager_text['items per page title'],
  55. '#type' => 'textfield',
  56. '#description' => $pager_text['items per page description'],
  57. '#default_value' => $this->options['items_per_page'],
  58. );
  59. $form['offset'] = array(
  60. '#type' => 'textfield',
  61. '#title' => t('Offset'),
  62. '#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.'),
  63. '#default_value' => $this->options['offset'],
  64. );
  65. $form['id'] = array(
  66. '#type' => 'textfield',
  67. '#title' => t('Pager ID'),
  68. '#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."),
  69. '#default_value' => $this->options['id'],
  70. );
  71. $form['total_pages'] = array(
  72. '#type' => 'textfield',
  73. '#title' => t('Number of pages'),
  74. '#description' => t('The total number of pages. Leave empty to show all pages.'),
  75. '#default_value' => $this->options['total_pages'],
  76. );
  77. $form['quantity'] = array(
  78. '#type' => 'textfield',
  79. '#title' => t('Number of pager links visible'),
  80. '#description' => t('Specify the number of links to pages to display in the pager.'),
  81. '#default_value' => $this->options['quantity'],
  82. );
  83. $form['tags'] = array (
  84. '#type' => 'fieldset',
  85. '#collapsible' => FALSE,
  86. '#collapsed' => FALSE,
  87. '#tree' => TRUE,
  88. '#title' => t('Tags'),
  89. '#input' => TRUE,
  90. '#description' => t('A lists of labels for the controls in the pager'),
  91. );
  92. $form['tags']['first'] = array(
  93. '#type' => 'textfield',
  94. '#title' => t('Text for "first"-link'),
  95. '#description' => t('Text for "first"-link'),
  96. '#default_value' => $this->options['tags']['first'],
  97. );
  98. $form['tags']['previous'] = array(
  99. '#type' => 'textfield',
  100. '#title' => t('Text for "previous"-link'),
  101. '#description' => t('Text for "previous"-link'),
  102. '#default_value' => $this->options['tags']['previous'],
  103. );
  104. $form['tags']['next'] = array(
  105. '#type' => 'textfield',
  106. '#title' => t('Text for "next"-link'),
  107. '#description' => t('Text for "next"-link'),
  108. '#default_value' => $this->options['tags']['next'],
  109. );
  110. $form['tags']['last'] = array(
  111. '#type' => 'textfield',
  112. '#title' => t('Text for "last"-link'),
  113. '#description' => t('Text for "last"-link'),
  114. '#default_value' => $this->options['tags']['last'],
  115. );
  116. $form['expose'] = array (
  117. '#type' => 'fieldset',
  118. '#collapsible' => FALSE,
  119. '#collapsed' => FALSE,
  120. '#tree' => TRUE,
  121. '#title' => t('Exposed options'),
  122. '#input' => TRUE,
  123. '#description' => t('Exposing this options allows users to define their values in a exposed form when view is displayed'),
  124. );
  125. $form['expose']['items_per_page'] = array(
  126. '#type' => 'checkbox',
  127. '#title' => t('Expose items per page'),
  128. '#description' => t('When checked, users can determine how many items per page show in a view'),
  129. '#default_value' => $this->options['expose']['items_per_page'],
  130. );
  131. $form['expose']['items_per_page_label'] = array(
  132. '#type' => 'textfield',
  133. '#title' => t('Items per page label'),
  134. '#required' => TRUE,
  135. '#description' => t('Label to use in the exposed items per page form element.'),
  136. '#default_value' => $this->options['expose']['items_per_page_label'],
  137. '#dependency' => array(
  138. 'edit-pager-options-expose-items-per-page' => array(1)
  139. ),
  140. );
  141. $form['expose']['items_per_page_options'] = array(
  142. '#type' => 'textfield',
  143. '#title' => t('Exposed items per page options'),
  144. '#required' => TRUE,
  145. '#description' => t('Set between which values the user can choose when determining the items per page. Separated by comma.'),
  146. '#default_value' => $this->options['expose']['items_per_page_options'],
  147. '#dependency' => array(
  148. 'edit-pager-options-expose-items-per-page' => array(1)
  149. ),
  150. );
  151. $form['expose']['items_per_page_options_all'] = array(
  152. '#type' => 'checkbox',
  153. '#title' => t('Include all items option'),
  154. '#description' => t('If checked, an extra item will be included to items per page to display all items'),
  155. '#default_value' => $this->options['expose']['items_per_page_options_all'],
  156. );
  157. $form['expose']['items_per_page_options_all_label'] = array(
  158. '#type' => 'textfield',
  159. '#title' => t('All items label'),
  160. '#description' => t('Which label will be used to display all items'),
  161. '#default_value' => $this->options['expose']['items_per_page_options_all_label'],
  162. '#dependency' => array(
  163. 'edit-items-per-page-options-all' => array(1),
  164. ),
  165. );
  166. $form['expose']['offset'] = array(
  167. '#type' => 'checkbox',
  168. '#title' => t('Expose Offset'),
  169. '#description' => t('When checked, users can determine how many items should be skipped at the beginning.'),
  170. '#default_value' => $this->options['expose']['offset'],
  171. );
  172. $form['expose']['offset_label'] = array(
  173. '#type' => 'textfield',
  174. '#title' => t('Offset label'),
  175. '#required' => TRUE,
  176. '#description' => t('Label to use in the exposed offset form element.'),
  177. '#default_value' => $this->options['expose']['offset_label'],
  178. '#dependency' => array(
  179. 'edit-pager-options-expose-offset' => array(1)
  180. ),
  181. );
  182. }
  183. function options_validate(&$form, &$form_state) {
  184. // Only accept integer values.
  185. $error = FALSE;
  186. $exposed_options = $form_state['values']['pager_options']['expose']['items_per_page_options'];
  187. if (strpos($exposed_options, '.') !== FALSE) {
  188. $error = TRUE;
  189. }
  190. $options = explode(',',$exposed_options);
  191. if (!$error && is_array($options)) {
  192. foreach ($options as $option) {
  193. if (!is_numeric($option) || intval($option) == 0) {
  194. $error = TRUE;
  195. }
  196. }
  197. }
  198. else {
  199. $error = TRUE;
  200. }
  201. if ($error) {
  202. 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'));
  203. }
  204. // Take sure that the items_per_page is part of the expose settings.
  205. if (!empty($form_state['values']['pager_options']['expose']['items_per_page']) && !empty($form_state['values']['pager_options']['items_per_page'])) {
  206. $items_per_page = $form_state['values']['pager_options']['items_per_page'];
  207. if (array_search($items_per_page, $options) === FALSE) {
  208. form_set_error('pager_options][expose][items_per_page_options', t('Please insert the items per page (@items_per_page) from above.',
  209. array('@items_per_page' => $items_per_page))
  210. );
  211. }
  212. }
  213. }
  214. function query() {
  215. if ($this->items_per_page_exposed()) {
  216. if (!empty($_GET['items_per_page']) && $_GET['items_per_page'] > 0) {
  217. $this->options['items_per_page'] = $_GET['items_per_page'];
  218. }
  219. elseif (!empty($_GET['items_per_page']) && $_GET['items_per_page'] == 'All' && $this->options['expose']['items_per_page_options_all']) {
  220. $this->options['items_per_page'] = 0;
  221. }
  222. }
  223. if ($this->offset_exposed()) {
  224. if (isset($_GET['offset']) && $_GET['offset'] >= 0) {
  225. $this->options['offset'] = $_GET['offset'];
  226. }
  227. }
  228. $limit = $this->options['items_per_page'];
  229. $offset = $this->current_page * $this->options['items_per_page'] + $this->options['offset'];
  230. if (!empty($this->options['total_pages'])) {
  231. if ($this->current_page >= $this->options['total_pages']) {
  232. $limit = $this->options['items_per_page'];
  233. $offset = $this->options['total_pages'] * $this->options['items_per_page'];
  234. }
  235. }
  236. $this->view->query->set_limit($limit);
  237. $this->view->query->set_offset($offset);
  238. }
  239. function render($input) {
  240. $pager_theme = views_theme_functions('pager', $this->view, $this->display);
  241. // The 0, 1, 3, 4 index are correct. See theme_pager documentation.
  242. $tags = array(
  243. 0 => $this->options['tags']['first'],
  244. 1 => $this->options['tags']['previous'],
  245. 3 => $this->options['tags']['next'],
  246. 4 => $this->options['tags']['last'],
  247. );
  248. $output = theme($pager_theme, array(
  249. 'tags' => $tags,
  250. 'element' => $this->get_pager_id(),
  251. 'parameters' => $input,
  252. 'quantity' => $this->options['quantity'],
  253. ));
  254. return $output;
  255. }
  256. /**
  257. * Set the current page.
  258. *
  259. * @param $number
  260. * If provided, the page number will be set to this. If NOT provided,
  261. * the page number will be set from the global page array.
  262. */
  263. function set_current_page($number = NULL) {
  264. if (isset($number)) {
  265. $this->current_page = $number;
  266. return;
  267. }
  268. // If the current page number was not specified, extract it from the global
  269. // page array.
  270. global $pager_page_array;
  271. if (empty($pager_page_array)) {
  272. $pager_page_array = array();
  273. }
  274. // Fill in missing values in the global page array, in case the global page
  275. // array hasn't been initialized before.
  276. $page = isset($_GET['page']) ? explode(',', $_GET['page']) : array();
  277. $pager_id = $this->get_pager_id();
  278. for ($i = 0; $i <= $pager_id || $i < count($pager_page_array); $i++) {
  279. $pager_page_array[$i] = empty($page[$i]) ? 0 : $page[$i];
  280. }
  281. $this->current_page = intval($pager_page_array[$pager_id]);
  282. if ($this->current_page < 0) {
  283. $this->current_page = 0;
  284. }
  285. }
  286. function get_pager_total() {
  287. if ($items_per_page = intval($this->get_items_per_page())) {
  288. return ceil($this->total_items / $items_per_page);
  289. }
  290. else {
  291. return 1;
  292. }
  293. }
  294. /**
  295. * Update global paging info.
  296. *
  297. * This is called after the count query has been run to set the total
  298. * items available and to update the current page if the requested
  299. * page is out of range.
  300. */
  301. function update_page_info() {
  302. if (!empty($this->options['total_pages'])) {
  303. if (($this->options['total_pages'] * $this->options['items_per_page']) < $this->total_items) {
  304. $this->total_items = $this->options['total_pages'] * $this->options['items_per_page'];
  305. }
  306. }
  307. // Don't set pager settings for items per page = 0.
  308. $items_per_page = $this->get_items_per_page();
  309. if (!empty($items_per_page)) {
  310. // Dump information about what we already know into the globals.
  311. global $pager_page_array, $pager_total, $pager_total_items, $pager_limits;
  312. // Set the limit.
  313. $pager_id = $this->get_pager_id();
  314. $pager_limits[$pager_id] = $this->options['items_per_page'];
  315. // Set the item count for the pager.
  316. $pager_total_items[$pager_id] = $this->total_items;
  317. // Calculate and set the count of available pages.
  318. $pager_total[$pager_id] = $this->get_pager_total();
  319. // See if the requested page was within range:
  320. if ($this->current_page < 0) {
  321. $this->current_page = 0;
  322. }
  323. else if ($this->current_page >= $pager_total[$pager_id]) {
  324. // Pages are numbered from 0 so if there are 10 pages, the last page is 9.
  325. $this->current_page = $pager_total[$pager_id] - 1;
  326. }
  327. // Put this number in to guarantee that we do not generate notices when the pager
  328. // goes to look for it later.
  329. $pager_page_array[$pager_id] = $this->current_page;
  330. }
  331. }
  332. function uses_exposed() {
  333. return $this->items_per_page_exposed() || $this->offset_exposed();
  334. }
  335. function items_per_page_exposed() {
  336. return !empty($this->options['expose']['items_per_page']);
  337. }
  338. function offset_exposed() {
  339. return !empty($this->options['expose']['offset']);
  340. }
  341. function exposed_form_alter(&$form, &$form_state) {
  342. if ($this->items_per_page_exposed()) {
  343. $options = explode(',', $this->options['expose']['items_per_page_options']);
  344. $sanitized_options = array();
  345. if (is_array($options)) {
  346. foreach ($options as $option) {
  347. $sanitized_options[intval($option)] = intval($option);
  348. }
  349. if (!empty($this->options['expose']['items_per_page_options_all']) && !empty($this->options['expose']['items_per_page_options_all_label'])) {
  350. $sanitized_options['All'] = $this->options['expose']['items_per_page_options_all_label'];
  351. }
  352. $form['items_per_page'] = array(
  353. '#type' => 'select',
  354. '#title' => $this->options['expose']['items_per_page_label'],
  355. '#options' => $sanitized_options,
  356. '#default_value' => $this->get_items_per_page(),
  357. );
  358. }
  359. }
  360. if ($this->offset_exposed()) {
  361. $form['offset'] = array(
  362. '#type' => 'textfield',
  363. '#size' => 10,
  364. '#maxlength' => 10,
  365. '#title' => $this->options['expose']['offset_label'],
  366. '#default_value' => $this->get_offset(),
  367. );
  368. }
  369. }
  370. function exposed_form_validate(&$form, &$form_state) {
  371. if (!empty($form_state['values']['offset']) && trim($form_state['values']['offset'])) {
  372. if (!is_numeric($form_state['values']['offset']) || $form_state['values']['offset'] < 0) {
  373. form_set_error('offset', t('Offset must be an number greather or equal than 0.'));
  374. }
  375. }
  376. }
  377. }