<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * Admin / Blog — manage the Journal articles.
 */
#[\AllowDynamicProperties]
class Blog_admin extends Admin_Controller {

	public function __construct()
	{
		parent::__construct();
		$this->load->model('blog_model');
	}

	public function index()
	{
		$this->render('blog/index', array(
			'page_title' => 'Journal',
			'posts'      => $this->blog_model->get_all(),
		));
	}

	public function create()
	{
		$this->render('blog/form', array(
			'page_title' => 'New Article',
			'post'       => $this->_blank(),
			'mode'       => 'create',
		));
	}

	public function edit($id)
	{
		$post = $this->blog_model->get($id);
		if ( ! $post) show_404();
		$this->render('blog/form', array(
			'page_title' => 'Edit Article',
			'post'       => $post,
			'mode'       => 'edit',
		));
	}

	public function save()
	{
		$id = (int) $this->input->post('id');

		$this->load->library('form_validation');
		$this->form_validation->set_rules('title', 'Title', 'required|trim');
		$this->form_validation->set_rules('body', 'Body', 'required');

		if ($this->form_validation->run() === FALSE)
		{
			$this->session->set_flashdata('error', strip_tags(validation_errors()));
			redirect($id ? 'admin/blog/edit/'.$id : 'admin/blog/create');
		}

		$title  = $this->input->post('title', TRUE);
		$slug   = $this->input->post('slug', TRUE) ?: url_title($title, '-', TRUE);
		$status = $this->input->post('status', TRUE) ?: 'draft';

		// publish_at: explicit value, or now for an immediate publish
		$publish_at = $this->input->post('publish_at', TRUE);
		if (empty($publish_at))
		{
			$publish_at = ($status === 'published') ? date('Y-m-d H:i:s') : NULL;
		}

		$data = array(
			'title'      => $title,
			'slug'       => $slug,
			'category'   => $this->input->post('category', TRUE),
			'excerpt'    => $this->input->post('excerpt', TRUE),
			// body is rich text — allow HTML, do not html_escape
			'body'       => $this->input->post('body'),
			'status'     => $status,
			'publish_at' => $publish_at,
			'author_id'  => $this->user['id'],
			'meta_title' => $this->input->post('meta_title', TRUE),
			'meta_desc'  => $this->input->post('meta_desc', TRUE),
		);

		$cover = $this->upload('cover_image', 'blog');
		if ($cover) $data['cover_image'] = $cover;

		if ($id)
		{
			$this->blog_model->update($id, $data);
			$this->log('Updated article', $title);
			$msg = 'Article updated.';
		}
		else
		{
			$this->blog_model->insert($data);
			$this->log('Created article', $title);
			$msg = 'Article created.';
		}

		$this->session->set_flashdata('success', $msg);
		redirect('admin/blog');
	}

	public function delete($id)
	{
		$p = $this->blog_model->get($id);
		if ($p)
		{
			$this->blog_model->delete($id);
			$this->log('Deleted article', $p['title']);
			$this->session->set_flashdata('success', 'Article deleted.');
		}
		redirect('admin/blog');
	}

	private function _blank()
	{
		return array(
			'id' => 0, 'title' => '', 'slug' => '', 'category' => '', 'cover_image' => '',
			'excerpt' => '', 'body' => '', 'status' => 'draft', 'publish_at' => '',
			'meta_title' => '', 'meta_desc' => '',
		);
	}
}
