<?php

class Admin_BlogController extends Fiktiv_Controller_Action
{
    public function indexAction()
    {
        // Load models
        $posts = new ModelBlogPost();

        // Get form
        $form = $this->view->form = $this->_getCreateForm();

        // Populate form
        if ($this->_getParam('id', null)) {
            $post = $posts->find($this->_getParam('id'))->current();
            $form->populate($post->toArray());
        }

        // Save post
        if ($this->_request->isPost() && $form->isValid($this->_request->getPost())) {
            $posts->createPost($form->getValues(), $this->_getParam('id', null));
            #$this->_redirect('index');
        }
    }

    protected function _getCreateForm()
    {
        require_once APPLICATION_PATH . '/forms/Admin_BlogCreate.php';
        return new Admin_BlogCreate();
    }
}