1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
<?php defined('BASEPATH') or exit('No direct script access allowed');
class Articles extends Auth_controller
{
public function __construct()
{
parent ::__construct();
$this->load->model('article_model');
}
public function index()
{
$this->data['articles'] = [];
$articles = $this->article_model->order_by('date', 'DESC')->get_all();
if($articles) {
$this->data['articles'] = $articles;
}
}
public function view($id = null)
{
$article = $this->article_model->get($id);
if (! $article) {
show_404();
}
$this->data = array_merge($this->data, $article);
}
public function create()
{
$this->load->helper('form');
$id = $this->article_model->from_form()->insert();
if ($id) {
$this->status('ok', true);
redirect_lang('admin/articles/view/'.$id);
}
elseif($this->input->method() === 'post') {
$this->status('error');
}
}
public function edit($id = null)
{
$this->load->helper('form');
$this->data['_'] = $this->article_model->get($id);
if (! $this->data['_'] || ! $id) {
show_404();
}
if($this->input->method() === 'post') {
if ($this->article_model->from_form(null, null, array('id' => $id))->update()) {
$this->status('ok', true);
redirect_lang('admin/articles/view/'.$id);
}
else {
$this->status('error');
}
}
}
public function delete($id = null)
{
if ($this->article_model->delete($id)) {
$this->status('ok', true);
} else {
$this->status('error', true);
}
redirect_lang('admin/articles');
}
}