PageRenderTime 24ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/core/modules/views/src/Plugin/views/pager/SqlBase.php

https://gitlab.com/guillaumev/alkarama
PHP | 393 lines | 317 code | 40 blank | 36 comment | 29 complexity | d8b23ad018b34f726feaadb8828ab2ba MD5 | raw file
  1. <?php
  2. namespace Drupal\views\Plugin\views\pager;
  3. use Drupal\Core\Cache\Cache;
  4. use Drupal\Core\Cache\CacheableDependencyInterface;
  5. use Drupal\Core\Form\FormStateInterface;
  6. /**
  7. * A common base class for sql based pager.
  8. */
  9. abstract class SqlBase extends PagerPluginBase implements CacheableDependencyInterface {
  10. protected function defineOptions() {
  11. $options = parent::defineOptions();
  12. $options['items_per_page'] = array('default' => 10);
  13. $options['offset'] = array('default' => 0);
  14. $options['id'] = array('default' => 0);
  15. $options['total_pages'] = array('default' => '');
  16. $options['expose'] = array(
  17. 'contains' => array(
  18. 'items_per_page' => array('default' => FALSE),
  19. 'items_per_page_label' => array('default' => $this->t('Items per page')),
  20. 'items_per_page_options' => array('default' => '5, 10, 25, 50'),
  21. 'items_per_page_options_all' => array('default' => FALSE),
  22. 'items_per_page_options_all_label' => array('default' => $this->t('- All -')),
  23. 'offset' => array('default' => FALSE),
  24. 'offset_label' => array('default' => $this->t('Offset')),
  25. ),
  26. );
  27. $options['tags'] = array(
  28. 'contains' => array(
  29. 'previous' => array('default' => $this->t('‹ Previous')),
  30. 'next' => array('default' => $this->t('Next ›')),
  31. ),
  32. );
  33. return $options;
  34. }
  35. /**
  36. * Provide the default form for setting options.
  37. */
  38. public function buildOptionsForm(&$form, FormStateInterface $form_state) {
  39. parent::buildOptionsForm($form, $form_state);
  40. $pager_text = $this->displayHandler->getPagerText();
  41. $form['items_per_page'] = array(
  42. '#title' => $pager_text['items per page title'],
  43. '#type' => 'number',
  44. '#description' => $pager_text['items per page description'],
  45. '#default_value' => $this->options['items_per_page'],
  46. );
  47. $form['offset'] = array(
  48. '#type' => 'number',
  49. '#title' => $this->t('Offset (number of items to skip)'),
  50. '#description' => $this->t('For example, set this to 3 and the first 3 items will not be displayed.'),
  51. '#default_value' => $this->options['offset'],
  52. );
  53. $form['id'] = array(
  54. '#type' => 'number',
  55. '#title' => $this->t('Pager ID'),
  56. '#description' => $this->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."),
  57. '#default_value' => $this->options['id'],
  58. );
  59. $form['total_pages'] = array(
  60. '#type' => 'number',
  61. '#title' => $this->t('Number of pages'),
  62. '#description' => $this->t('Leave empty to show all pages.'),
  63. '#default_value' => $this->options['total_pages'],
  64. );
  65. $form['tags'] = array(
  66. '#type' => 'details',
  67. '#open' => TRUE,
  68. '#tree' => TRUE,
  69. '#title' => $this->t('Pager link labels'),
  70. '#input' => TRUE,
  71. );
  72. $form['tags']['previous'] = array(
  73. '#type' => 'textfield',
  74. '#title' => $this->t('Previous page link text'),
  75. '#default_value' => $this->options['tags']['previous'],
  76. );
  77. $form['tags']['next'] = array(
  78. '#type' => 'textfield',
  79. '#title' => $this->t('Next page link text'),
  80. '#default_value' => $this->options['tags']['next'],
  81. );
  82. $form['expose'] = array(
  83. '#type' => 'details',
  84. '#open' => TRUE,
  85. '#tree' => TRUE,
  86. '#title' => $this->t('Exposed options'),
  87. '#input' => TRUE,
  88. '#description' => $this->t('Allow user to control selected display options for this view.'),
  89. );
  90. $form['expose']['items_per_page'] = array(
  91. '#type' => 'checkbox',
  92. '#title' => $this->t('Allow user to control the number of items displayed in this view'),
  93. '#default_value' => $this->options['expose']['items_per_page'],
  94. );
  95. $form['expose']['items_per_page_label'] = array(
  96. '#type' => 'textfield',
  97. '#title' => $this->t('Items per page label'),
  98. '#required' => TRUE,
  99. '#default_value' => $this->options['expose']['items_per_page_label'],
  100. '#states' => array(
  101. 'invisible' => array(
  102. 'input[name="pager_options[expose][items_per_page]"]' => array('checked' => FALSE),
  103. ),
  104. ),
  105. );
  106. $form['expose']['items_per_page_options'] = array(
  107. '#type' => 'textfield',
  108. '#title' => $this->t('Exposed items per page options'),
  109. '#required' => TRUE,
  110. '#description' => $this->t('Set between which values the user can choose when determining the items per page. Separated by comma.'),
  111. '#default_value' => $this->options['expose']['items_per_page_options'],
  112. '#states' => array(
  113. 'invisible' => array(
  114. 'input[name="pager_options[expose][items_per_page]"]' => array('checked' => FALSE),
  115. ),
  116. ),
  117. );
  118. $form['expose']['items_per_page_options_all'] = array(
  119. '#type' => 'checkbox',
  120. '#title' => $this->t('Allow user to display all items'),
  121. '#default_value' => $this->options['expose']['items_per_page_options_all'],
  122. );
  123. $form['expose']['items_per_page_options_all_label'] = array(
  124. '#type' => 'textfield',
  125. '#title' => $this->t('All items label'),
  126. '#default_value' => $this->options['expose']['items_per_page_options_all_label'],
  127. '#states' => array(
  128. 'invisible' => array(
  129. 'input[name="pager_options[expose][items_per_page_options_all]"]' => array('checked' => FALSE),
  130. ),
  131. ),
  132. );
  133. $form['expose']['offset'] = array(
  134. '#type' => 'checkbox',
  135. '#title' => $this->t('Allow user to specify number of items skipped from beginning of this view.'),
  136. '#default_value' => $this->options['expose']['offset'],
  137. );
  138. $form['expose']['offset_label'] = array(
  139. '#type' => 'textfield',
  140. '#title' => $this->t('Offset label'),
  141. '#required' => TRUE,
  142. '#default_value' => $this->options['expose']['offset_label'],
  143. '#states' => array(
  144. 'invisible' => array(
  145. 'input[name="pager_options[expose][offset]"]' => array('checked' => FALSE),
  146. ),
  147. ),
  148. );
  149. }
  150. public function validateOptionsForm(&$form, FormStateInterface $form_state) {
  151. // Only accept integer values.
  152. $error = FALSE;
  153. $exposed_options = $form_state->getValue(array('pager_options', 'expose', 'items_per_page_options'));
  154. if (strpos($exposed_options, '.') !== FALSE) {
  155. $error = TRUE;
  156. }
  157. $options = explode(',', $exposed_options);
  158. if (!$error && is_array($options)) {
  159. foreach ($options as $option) {
  160. if (!is_numeric($option) || intval($option) == 0) {
  161. $error = TRUE;
  162. }
  163. }
  164. }
  165. else {
  166. $error = TRUE;
  167. }
  168. if ($error) {
  169. $form_state->setErrorByName('pager_options][expose][items_per_page_options', $this->t('Insert a list of integer numeric values separated by commas: e.g: 10, 20, 50, 100'));
  170. }
  171. // Make sure that the items_per_page is part of the expose settings.
  172. if (!$form_state->isValueEmpty(array('pager_options', 'expose', 'items_per_page')) && !$form_state->isValueEmpty(array('pager_options', 'items_per_page'))) {
  173. $items_per_page = $form_state->getValue(array('pager_options', 'items_per_page'));
  174. if (array_search($items_per_page, $options) === FALSE) {
  175. $form_state->setErrorByName('pager_options][expose][items_per_page_options', $this->t("The <em>Exposed items per page</em> field's options must include the value from the <em>Items per page</em> field (@items_per_page).",
  176. array('@items_per_page' => $items_per_page))
  177. );
  178. }
  179. }
  180. }
  181. public function query() {
  182. if ($this->itemsPerPageExposed()) {
  183. $query = $this->view->getRequest()->query;
  184. $items_per_page = $query->get('items_per_page');
  185. if ($items_per_page > 0) {
  186. $this->options['items_per_page'] = $items_per_page;
  187. }
  188. elseif ($items_per_page == 'All' && $this->options['expose']['items_per_page_options_all']) {
  189. $this->options['items_per_page'] = 0;
  190. }
  191. }
  192. if ($this->isOffsetExposed()) {
  193. $query = $this->view->getRequest()->query;
  194. $offset = $query->get('offset');
  195. if (isset($offset) && $offset >= 0) {
  196. $this->options['offset'] = $offset;
  197. }
  198. }
  199. $limit = $this->options['items_per_page'];
  200. $offset = $this->current_page * $this->options['items_per_page'] + $this->options['offset'];
  201. if (!empty($this->options['total_pages'])) {
  202. if ($this->current_page >= $this->options['total_pages']) {
  203. $limit = $this->options['items_per_page'];
  204. $offset = $this->options['total_pages'] * $this->options['items_per_page'];
  205. }
  206. }
  207. $this->view->query->setLimit($limit);
  208. $this->view->query->setOffset($offset);
  209. }
  210. /**
  211. * Set the current page.
  212. *
  213. * @param $number
  214. * If provided, the page number will be set to this. If NOT provided,
  215. * the page number will be set from the global page array.
  216. */
  217. public function setCurrentPage($number = NULL) {
  218. if (isset($number)) {
  219. $this->current_page = max(0, $number);
  220. return;
  221. }
  222. // If the current page number was not specified, extract it from the global
  223. // page array.
  224. global $pager_page_array;
  225. if (empty($pager_page_array)) {
  226. $pager_page_array = array();
  227. }
  228. // Fill in missing values in the global page array, in case the global page
  229. // array hasn't been initialized before.
  230. $page = $this->view->getRequest()->query->get('page');
  231. $page = isset($page) ? explode(',', $page) : array();
  232. for ($i = 0; $i <= $this->options['id'] || $i < count($pager_page_array); $i++) {
  233. $pager_page_array[$i] = empty($page[$i]) ? 0 : $page[$i];
  234. }
  235. // Don't allow the number to be less than zero.
  236. $this->current_page = max(0, intval($pager_page_array[$this->options['id']]));
  237. }
  238. public function getPagerTotal() {
  239. if ($items_per_page = intval($this->getItemsPerPage())) {
  240. return ceil($this->total_items / $items_per_page);
  241. }
  242. else {
  243. return 1;
  244. }
  245. }
  246. /**
  247. * Update global paging info.
  248. *
  249. * This is called after the count query has been run to set the total
  250. * items available and to update the current page if the requested
  251. * page is out of range.
  252. */
  253. public function updatePageInfo() {
  254. if (!empty($this->options['total_pages'])) {
  255. if (($this->options['total_pages'] * $this->options['items_per_page']) < $this->total_items) {
  256. $this->total_items = $this->options['total_pages'] * $this->options['items_per_page'];
  257. }
  258. }
  259. // Don't set pager settings for items per page = 0.
  260. $items_per_page = $this->getItemsPerPage();
  261. if (!empty($items_per_page)) {
  262. // Dump information about what we already know into the globals.
  263. global $pager_page_array, $pager_total, $pager_total_items, $pager_limits;
  264. // Set the limit.
  265. $pager_limits[$this->options['id']] = $this->options['items_per_page'];
  266. // Set the item count for the pager.
  267. $pager_total_items[$this->options['id']] = $this->total_items;
  268. // Calculate and set the count of available pages.
  269. $pager_total[$this->options['id']] = $this->getPagerTotal();
  270. // See if the requested page was within range:
  271. if ($this->current_page >= $pager_total[$this->options['id']]) {
  272. // Pages are numbered from 0 so if there are 10 pages, the last page is 9.
  273. $this->setCurrentPage($pager_total[$this->options['id']] - 1);
  274. }
  275. // Put this number in to guarantee that we do not generate notices when the pager
  276. // goes to look for it later.
  277. $pager_page_array[$this->options['id']] = $this->current_page;
  278. }
  279. }
  280. public function usesExposed() {
  281. return $this->itemsPerPageExposed() || $this->isOffsetExposed();
  282. }
  283. protected function itemsPerPageExposed() {
  284. return !empty($this->options['expose']['items_per_page']);
  285. }
  286. protected function isOffsetExposed() {
  287. return !empty($this->options['expose']['offset']);
  288. }
  289. public function exposedFormAlter(&$form, FormStateInterface $form_state) {
  290. if ($this->itemsPerPageExposed()) {
  291. $options = explode(',', $this->options['expose']['items_per_page_options']);
  292. $sanitized_options = array();
  293. if (is_array($options)) {
  294. foreach ($options as $option) {
  295. $sanitized_options[intval($option)] = intval($option);
  296. }
  297. if (!empty($this->options['expose']['items_per_page_options_all']) && !empty($this->options['expose']['items_per_page_options_all_label'])) {
  298. $sanitized_options['All'] = $this->options['expose']['items_per_page_options_all_label'];
  299. }
  300. $form['items_per_page'] = array(
  301. '#type' => 'select',
  302. '#title' => $this->options['expose']['items_per_page_label'],
  303. '#options' => $sanitized_options,
  304. '#default_value' => $this->getItemsPerPage(),
  305. );
  306. }
  307. }
  308. if ($this->isOffsetExposed()) {
  309. $form['offset'] = array(
  310. '#type' => 'textfield',
  311. '#size' => 10,
  312. '#maxlength' => 10,
  313. '#title' => $this->options['expose']['offset_label'],
  314. '#default_value' => $this->getOffset(),
  315. );
  316. }
  317. }
  318. public function exposedFormValidate(&$form, FormStateInterface $form_state) {
  319. if (!$form_state->isValueEmpty('offset') && trim($form_state->getValue('offset'))) {
  320. if (!is_numeric($form_state->getValue('offset')) || $form_state->getValue('offset') < 0) {
  321. $form_state->setErrorByName('offset', $this->t('Offset must be an number greater or equal than 0.'));
  322. }
  323. }
  324. }
  325. /**
  326. * {@inheritdoc}
  327. */
  328. public function getCacheMaxAge() {
  329. return Cache::PERMANENT;
  330. }
  331. /**
  332. * {@inheritdoc}
  333. */
  334. public function getCacheContexts() {
  335. // The rendered link needs to play well with any other query parameter used
  336. // on the page, like other pagers and exposed filter.
  337. return ['url.query_args'];
  338. }
  339. /**
  340. * {@inheritdoc}
  341. */
  342. public function getCacheTags() {
  343. return [];
  344. }
  345. }