From 38e1d21b69fbeb67b2431eb796986489ae6dda36 Mon Sep 17 00:00:00 2001 From: Aleksandr Shelkovin Date: Fri, 31 Jan 2025 21:37:24 +0300 Subject: [PATCH] End of jan --- engine/app/controller/MainController.php | 26 +++- engine/app/template/index_tpl.php | 152 +++++++++++++---------- engine/app/template/portial_music.php | 15 +++ engine/app/template/tpl_layout.php | 54 +++++--- engine/core/Autoloader.php | 2 +- engine/core/Paginator.php | 80 ++++++++++++ engine/routes.php | 1 + 7 files changed, 245 insertions(+), 85 deletions(-) create mode 100644 engine/app/template/portial_music.php create mode 100644 engine/core/Paginator.php diff --git a/engine/app/controller/MainController.php b/engine/app/controller/MainController.php index 3d697a1..f2e5d1a 100644 --- a/engine/app/controller/MainController.php +++ b/engine/app/controller/MainController.php @@ -2,17 +2,35 @@ require_once __DIR__ . "/../model/MusicModel.php"; require_once __DIR__ . "/../view/View.php"; +require_once __DIR__ . "/../../core/Paginator.php"; class MainController { -public function actionIndex() { - $result = MusicModel::getData(); - echo(View::render(['box' => $result], 'index_tpl.php')); -} + private $limit = Null; + private $offset = Null; + + public function actionIndex() + { + $result = MusicModel::getData(); + $builder = new MusicPageBuilder($result); + $portial = $builder->setFeed(); + echo (View::render(['portial' => $portial], 'index_tpl.php')); + } public function actionNotFound() { echo (View::render([], '404.php')); } + + public function actionPortial() + { + // $this->limit = isset($_GET['limit']) ? $_GET['limit'] : Null; + // $this->offset = isset($_GET['offset']) ? $_GET['offset'] : Null; + $result = MusicModel::getData(); + // var_dump($result); + $builder = new MusicPageBuilder($result); + $page = $builder->setFeed(); + echo $page; + } } diff --git a/engine/app/template/index_tpl.php b/engine/app/template/index_tpl.php index 95497b6..34f0e8c 100644 --- a/engine/app/template/index_tpl.php +++ b/engine/app/template/index_tpl.php @@ -2,82 +2,106 @@ - - - - Resposive Layout + + + + Resposive Layout - +
+ + + +
-
- - - - - +
+
+ + + - + document.getElementById(tabId).classList.add('active'); + document.getElementById(tabId).classList.add('border-blue-500'); + document.getElementById(tabId).classList.add('text-blue-500'); + document.getElementById(tabId).classList.remove('border-transparent'); + document.getElementById(tabId).classList.remove('text-gray-500'); + document.getElementById(`content${tabId.slice(-1)}`).classList.remove('hidden'); + } + + async function getFetch(url) { + const response = await fetch(url, { + method: 'get' + }) + return await response.text(); + + } + async function getPage(event) { + event.stopPropagation() + event.preventDefault() + let num_page = parseInt(event.target.getAttribute('data-id')) + console.log(num_page) + let limit = 5 + url = 'portial?offset=' + limit * num_page + '&limit=' + limit + // console.log(url) + let data = await getFetch(url) + if (!data) event.target.remove() + // event.target.remove() + feed = document.getElementById('feed') + feed.innerHTML = feed.innerHTML + data + event.target.setAttribute('data-id', parseInt(num_page) + 1) + + } + + const parent = document.getElementById('paginator') + parent.addEventListener('click', getPage) + diff --git a/engine/app/template/portial_music.php b/engine/app/template/portial_music.php new file mode 100644 index 0000000..81d6a14 --- /dev/null +++ b/engine/app/template/portial_music.php @@ -0,0 +1,15 @@ + +
+ +
+ +

Файл "" не найден.

+ diff --git a/engine/app/template/tpl_layout.php b/engine/app/template/tpl_layout.php index dc82b3d..387b7b5 100644 --- a/engine/app/template/tpl_layout.php +++ b/engine/app/template/tpl_layout.php @@ -28,23 +28,45 @@ +
+ - -
- -
- -

Файл "" не найден.

- - +
+ + \ No newline at end of file diff --git a/engine/core/Autoloader.php b/engine/core/Autoloader.php index 638f862..8652679 100644 --- a/engine/core/Autoloader.php +++ b/engine/core/Autoloader.php @@ -5,7 +5,7 @@ class Autoloader { spl_autoload_register(function ($class_name) { // print('auto: ' . $class_name); - $dir = ['/../app/controller/']; + $dir = ['/../app/controller/', '../app/view']; // print($dir); foreach ($dir as $path) { $path = __DIR__ . $path . $class_name . '.php'; diff --git a/engine/core/Paginator.php b/engine/core/Paginator.php new file mode 100644 index 0000000..5fb2594 --- /dev/null +++ b/engine/core/Paginator.php @@ -0,0 +1,80 @@ +html; + } +} + + +interface PageBuilder +{ + public function setFeed(); + public function setPaginator(); + // ... другие методы для установки свойств +} + +//implements PageBuilder { +class MusicPageBuilder implements PageBuilder +{ + + private Page $page; + + const LIMIT = 5; + const OFFSET = 0; + private $data = []; + private $limit = Null; + private $offset = Null; + private $flag = 'ajax'; + + + public function __construct($data, $flag = 'ajax') + { + + $this->page = new Page(); + $this->flag = $flag; + $this->data = $data; + $this->limit = isset($_GET['limit']) ? $_GET['limit'] : self::LIMIT; + $this->offset = isset($_GET['offset']) ? $_GET['offset'] : self::OFFSET; + } + + public function setFeed() + { + $portial = array_slice($this->data, $this->offset, $this->limit); + $data = View::render(['box' => $portial], 'portial_music.php'); + // var_dump ($data); + return $data; + } + + public function setPaginator() + { + $len = sizeof($this->data); + $contPage = intdiv($len, $this->limit); + for ($i = 0; $i < $contPage; $i++) { + $this->page->paginator .= '' . $i . '  '; + } + return $this; + } +} + +// Клиентский код +// $builder = new MusicPageBuilder($arrMusic); +// $page = $builder->setFeed()->setPaginator()->build(); +// echo $page . PHP_EOL; + +// $p = new Paginator($arrMusic); +// $p->renderFeed(); +// $p->renderPaginator(); +// $p->renderPage(); diff --git a/engine/routes.php b/engine/routes.php index 0f92408..8cdc4f7 100644 --- a/engine/routes.php +++ b/engine/routes.php @@ -10,6 +10,7 @@ return array( 'logout' => 'auth/logout', 'upload' => 'upload/index', 'file_upload' => 'upload/addfile', + 'portial' => 'main/portial' ); // - где 'news' - строка запроса // 'news/index' - имя контроллера и экшена для обработки этого запроса (путь обработчика)