Новостная лента
Создаем файл модели models/news.model.php
class News extends Model { protected $name = "Новости"; protected $model_elements = [ ["Активация", "bool", "active", ["on_create" => true]), ["Дата", "date", "date", ["required" => true]), ["Название", "char", "name", ["required" => true]), ["Ссылка", "url", "url", ["unique" => true, "translit_from" => "name"]), ["Изображение", "image", "image"], ["Текст новости", "text", "content", ["rich_text" => true]) );
Вносим название модели в файл config/models.php
$mvActiveModels = ['pages', 'blocks', ... , 'news'];
Создаем SQL таблицу в базе данных, после чего модель может работать в административной панели
CREATE TABLE `news` ( `id` int(11) NOT NULL, `active` tinyint(1) NOT NULL, `name` varchar(250) NOT NULL, `date` date NOT NULL, `image` varchar(250) NOT NULL, `url` varchar(200) NOT NULL, `content` text NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; ALTER TABLE `news` ADD PRIMARY KEY (`id`), ADD KEY `url` (`url`); ALTER TABLE `news` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
Создаем файлы шаблонов views/view-news.php и views/view-news-details.php, а также вносим маршруты к шаблонам в файл config/routes.php
$mvFrontendRoutes = [ ... , "news/" => "view-news.php", "news/*/" => "view-news-details.php" ];
Добавляем методы в файл модели
public function defineNewsPage(Router $router) { $url_parts = $router -> getUrlParts(); $record = false; if(count($url_parts) == 2) if(is_numeric($url_parts[1])) $record = $this -> find(["id" => $url_parts[1], "active" => 1]); else $record = $this -> find(["url" => $url_parts[1], "active" => 1]); return $record; } public function display() { $params = ["active" => 1, "order->desc" => "date", "limit->" => $this -> pager -> getParamsForSelect(]); $rows = $this -> select($params); $html = ""; foreach($rows as $row) { $url = $this -> root_path."news/".($row['url'] ? $row['url'] : $row['id'])."/"; $html .= "\n"; $html .= "\n"; } return $html; } public function displayPreviousAndNext($current_id, $current_date) { $html = ""; $params = ["active" => 1, "id!=" => $current_id, "date>=" => $current_date, "order->asc" => "date"]; if($record = $this -> find($params)) { $url = $this -> root_path."news/".($record -> url ? $record -> url : $record -> id)."/"; $html .= "Предыдущая новость\n"; } $params = ["active" => 1, "id!=" => $current_id, "date<=" => $current_date, "order->desc" => "date"]; if($record = $this -> find($params)) { $url = $this -> root_path."news/".($record -> url ? $record -> url : $record -> id)."/"; $html .= "Следующая новость\n"; } return $html; }".$row['name']."
\n"; $html .= "".I18n :: formatDate($row['date'])."
\n"; if($row['image']) { $html .= "\n"; $html .= $this -> cropImage($row['image'], 187, 127)."\n"; } $html .= "".Service :: cutText($row['content'], 350, " ...")."
\n"; $html .= "
Содержимое файла шаблона view-news.php
$count_records = $mv -> news -> countRecords(["active" => 1]); $current_page = $mv -> router -> defineCurrentPage("page"); $mv -> news -> runPager($count_records, 7, $current_page); $path = $mv -> root_path."news/"; $mv -> seo -> mergeParams("Новости"); include $mv -> views_path."main-header.php";include $mv -> views_path."main-footer.php";Новости
echo $mv -> news -> display(); if($mv -> news -> pager -> hasPages()) { echo "\nСтраница\n"; echo $mv -> news -> pager -> displayPrevLink("предыдущая", $path); echo $mv -> news -> pager -> display($path, false); echo $mv -> news -> pager -> displayNextLink("следующая", $path); echo "\n"; }
Содержимое файла шаблона view-news-details.php
$record = $mv -> news -> defineNewsPage($mv -> router); $mv -> display404($record); $mv -> seo -> mergeParams($record, "name"); include $mv -> views_path."main-header.php";include $mv -> views_path."main-footer.php";echo $record -> name;
echo I18n :: formatDate($record -> date);echo $mv -> news -> cropImage($record -> image, 383, 258); echo $record -> content;echo $mv -> news -> displayPreviousAndNext($record -> id, $record -> date);
Предыдущий раздел
Template