Пример #1
0
 protected function actionCreate()
 {
     try {
         $article = new \App\Models\News();
         $article->fill([]);
         $article->save();
     } catch (MultiException $e) {
         $this->view->errors = $e;
     }
     $this->view->display(__DIR__ . '/../templates/create.php');
 }
Пример #2
0
 protected function actionAdd()
 {
     $article = new \App\Models\News();
     $article->title = $_REQUEST['title'];
     $article->intro_text = $_REQUEST['intro_text'];
     $article->full_text = $_REQUEST['full_text'];
     $article->author_id = 1;
     $article->created_at = date('Y-m-d H:i:s');
     $article->save();
     $this->view->message = "Новость добавлена";
     $this->view->display(__DIR__ . '/../Views/admin_redirect.php');
 }
Пример #3
0
 protected function actionEdit()
 {
     $id = $_GET['id'] ?? false;
     if (false !== $id) {
         if (false === ($article = \App\Models\News::findByID($id))) {
             throw new \App\Exceptions\NotFound($_SERVER['REQUEST_URI']);
         }
     } else {
         $article = new \App\Models\News();
     }
     if (!empty($_POST)) {
         try {
             $article->fillByPost($_POST);
         } catch (MultiException $e) {
             $this->view->display('edit.php', ['article' => $article, 'authors' => \App\Models\Author::findAll(), 'errors' => $e]);
             exit(0);
         }
         $this->redirectIf('/admin/news/index', $article->save());
     }
     $this->view->display('edit.php', ['article' => $article, 'authors' => \App\Models\Author::findAll()]);
 }
Пример #4
0
 protected function actionSave()
 {
     try {
         if (isset($_POST['id'])) {
             $news = \App\Models\News::findById($_POST['id']);
             if (!$news) {
                 throw new \App\Exceptions\Err404('Новость не найдена ');
             }
         } else {
             $news = new \App\Models\News();
         }
         $news->fill($_POST);
         $news->save();
         header('Location: /admin/index');
         exit;
     } catch (\Lib\MultiException $e) {
         $this->view->errors = $e;
         $logger = new \App\LogUseLib();
         $logger->getArrMess($e);
         $this->view->news = $news;
         $this->view->display(__DIR__ . '/../templates/admin/edit.php');
     }
 }
Пример #5
0
<?php

require __DIR__ . '/autoload.php';
if (!empty($_POST['name'])) {
    if (!empty($_POST['id'])) {
        $article = \App\Models\News::findById((int) $_GET['id']);
    } else {
        $article = new \App\Models\News();
    }
    $article->name = htmlspecialchars($_POST['name']);
    $article->preview_content = htmlspecialchars($_POST['preview_content']);
    $article->detail_content = htmlspecialchars($_POST['detail_content']);
    $article->date_create = htmlspecialchars($_POST['date_create']);
    $article->save();
    if (!empty($_POST['save'])) {
        header('Location: /');
    }
}
if (!empty($_GET['id'])) {
    $article = \App\Models\News::findById((int) $_GET['id']);
    if (!empty($_GET['del'])) {
        $article->delete();
        header('Location: /');
    }
} else {
    $article = new \App\Models\News();
}
include __DIR__ . '/views/article_edit.tpl';