Paging.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Ui\Component;
  7. /**
  8. * @api
  9. * @since 100.0.2
  10. */
  11. class Paging extends AbstractComponent
  12. {
  13. const NAME = 'paging';
  14. /**
  15. * Default component data
  16. *
  17. * @var array
  18. */
  19. protected $_data = [
  20. 'config' => [
  21. 'options' => [
  22. '20' => [
  23. 'value' => 20,
  24. 'label' => 20
  25. ],
  26. '30' => [
  27. 'value' => 30,
  28. 'label' => 30
  29. ],
  30. '50' => [
  31. 'value' => 50,
  32. 'label' => 50
  33. ],
  34. '100' => [
  35. 'value' => 100,
  36. 'label' => 100
  37. ],
  38. '200' => [
  39. 'value' => 200,
  40. 'label' => 200
  41. ],
  42. ],
  43. 'pageSize' => 20,
  44. 'current' => 1
  45. ]
  46. ];
  47. /**
  48. * Get component name
  49. *
  50. * @return string
  51. */
  52. public function getComponentName()
  53. {
  54. return static::NAME;
  55. }
  56. /**
  57. * Register component and apply paging settings to Data Provider
  58. *
  59. * @return void
  60. */
  61. public function prepare()
  62. {
  63. $this->prepareOptions();
  64. $paging = $this->getContext()->getRequestParam('paging');
  65. if (!isset($paging['notLimits'])) {
  66. $this->getContext()
  67. ->getDataProvider()
  68. ->setLimit($this->getOffset($paging), $this->getSize($paging));
  69. }
  70. parent::prepare();
  71. }
  72. /**
  73. * Prepare paging options
  74. *
  75. * @return void
  76. */
  77. protected function prepareOptions()
  78. {
  79. $config = $this->getData('config');
  80. if (isset($config['options'])) {
  81. $config['options'] = array_values($config['options']);
  82. foreach ($config['options'] as &$item) {
  83. $item['value'] = (int) $item['value'];
  84. }
  85. unset($item);
  86. $this->setData('config', $config);
  87. }
  88. }
  89. /**
  90. * Get offset
  91. *
  92. * @param array|null $paging
  93. * @return int
  94. */
  95. protected function getOffset($paging)
  96. {
  97. $defaultPage = $this->getData('config/current') ?: 1;
  98. return (int) (isset($paging['current']) ? $paging['current'] : $defaultPage);
  99. }
  100. /**
  101. * Get size
  102. *
  103. * @param array|null $paging
  104. * @return int
  105. */
  106. protected function getSize($paging)
  107. {
  108. $defaultLimit = $this->getData('config/pageSize') ?: 20;
  109. return (int) (isset($paging['pageSize']) ? $paging['pageSize'] : $defaultLimit);
  110. }
  111. }