A bare bones front-end for knockout designed for maximum compatibility with "obsolete" browsers
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

111 Zeilen
2.8KB

  1. <?php
  2. namespace App\Knockout;
  3. use Illuminate\Http\Request;
  4. class Dataset
  5. {
  6. private $records = [];
  7. private $totalRecords = null;
  8. private $currentPage = null;
  9. private $recordsPerPage = null;
  10. public function __serialize(): array
  11. {
  12. return [
  13. 'records' => $this->records,
  14. 'totalRecords' => $this->totalRecords,
  15. 'currentPage' => $this->currentPage,
  16. 'recordsPerPage' => $this->recordsPerPage
  17. ];
  18. }
  19. public function __unserialize(array $data): void
  20. {
  21. $this->records = $data['records'] ?? [];
  22. $this->totalRecords = $data['totalRecords'] ?? null;
  23. $this->currentPage = $data['currentPage'] ?? null;
  24. $this->recordsPerPage = $data['recordsPerPage'] ?? null;
  25. }
  26. public function __construct($records)
  27. {
  28. if (!is_array($records)) $records = [$records];
  29. $this->records = $records;
  30. }
  31. public function setTotalRecords(int $total)
  32. {
  33. $this->totalRecords = $total;
  34. return $this;
  35. }
  36. public function setCurrentPage(int $page)
  37. {
  38. $this->currentPage = $page;
  39. return $this;
  40. }
  41. public function setRecordsPerPage(int $count)
  42. {
  43. $this->recordsPerPage = $count;
  44. return $this;
  45. }
  46. public function getRecords()
  47. {
  48. return $this->records;
  49. }
  50. public function getRecord()
  51. {
  52. return $this->records[0] ?? null;
  53. }
  54. private function getPageCount()
  55. {
  56. $exactPages = ($this->totalRecords / $this->recordsPerPage);
  57. $pages = ceil($exactPages);
  58. $pages = ($pages == $exactPages) ? floor($exactPages) : $pages;
  59. return ($pages < 1) ? 1 : $pages;
  60. }
  61. private function queryParams(array $query)
  62. {
  63. $output = [];
  64. foreach ($query as $key => $item) {
  65. $output[] = sprintf('%s=%s', $key, urlencode($item));
  66. }
  67. return '?' . implode('&', $output);
  68. }
  69. public function getPaginator(Request $request)
  70. {
  71. if (is_null($this->totalRecords)) return null;
  72. if (is_null($this->currentPage)) return null;
  73. if (is_null($this->recordsPerPage)) return null;
  74. $route = $request->route();
  75. $query = $request->query();
  76. $alias = $route[1]['as'] ?? 'index';
  77. $params = $route[2] ?? [];
  78. $pages = array_map(function($page) use($alias, $params, $query) {
  79. $currentPage = $params['page'] ?? 1;
  80. return (object) [
  81. 'number' => $page,
  82. 'active' => ($page == $currentPage),
  83. 'link' => route($alias, array_merge($params, ['page' => $page])) //. $this->queryParams($query)
  84. ];
  85. }, range(1, $this->getPageCount()));
  86. return view('partial/paginator', [
  87. 'pages' => $pages
  88. ]);
  89. }
  90. }