<?PHP

class PostAdmin extends Buyers
{
    public function fetch()
    {
        $post = new stdClass;
        $post->categories = array();
        if($this->request->method('post'))
        {
            $status = "updated";
            $post->id = $this->request->post('id', 'integer');
            $post->name = $this->request->post('name');
            $post->author = $this->request->post('author', 'string');
            $post->visible = $this->request->post('visible', 'boolean');
            $post->categories = implode(",", $this->request->post('categories'));
            $post->url = trim($this->request->post('url', 'string'));
            $post->meta_title = $this->request->post('meta_title');
            $post->meta_description = $this->request->post('meta_description');

            $post->annotation = $this->request->post('annotation');
            $post->body = $this->request->post('body');

             // Не допустить одинаковые URL разделов.
            if(($a = $this->blog->get_post($post->url)) && $a->id != $post->id)
            {
                $this->request->redirect(array('module'=>'PostAdmin', 'id'=>$post->id, 'message_error'=>'url_exists'));
            }
            else
            {
                if(empty($post->id))
                {
                    $status = "added";
                    $post->id = $this->blog->add_post($post);
                }
                else
                    $post = $this->blog->update_post($post->id, $post);

                //добавим\обновим фотки
                $this->images->update_images($post->id, $this->image->blog_images_dir);
            }
            $this->request->redirect(array('module'=>'PostAdmin', 'id'=>$post->id, 'message_success'=>$status, 'return'=>$this->request->get('return')));
        }

        if($id = $this->request->get('id', 'integer'))
        {
            $post = $this->blog->get_post(intval($id));
            $post->categories = explode(",", $post->categories);
        }

        $this->design->assign('post', $post);
        $this->design->assign('categories', $this->blog->get_categories());
        return $this->design->fetch('post.tpl');
    }
}
