<?php

/**
 * Simpla CMS
 *
 * @copyright	2011 Denis Pikusov
 * @link		http://simplacms.ru
 * @author		Denis Pikusov
 *
 */

require_once('api/Simpla.php');

class ArticlesAdmin extends Simpla
{	private $allowed_image_extentions = array('png', 'gif', 'jpg', 'jpeg', 'ico');

	public function fetch()
	{
        $filter = array();

        // Категории
        $categories = $this->articles->get_categories_tree();
        $this->design->assign('categories', $categories);

        // Текущая категория
        $category_id = $this->request->get('category_id', 'integer');
        if($category_id && $category = $this->articles->get_category($category_id)) {
            $filter['category_id'] = $category->children;
            $this->design->assign('category', $category);
        }

		// Обработка действий
		if($this->request->method('post'))
		{
			// Действия с выбранными
			$ids = $this->request->post('check');
			if(is_array($ids))
			    switch($this->request->post('action'))
			    {
			        case 'disable':
			        {
					    $this->articles->update_article($ids, array('visible'=>0));
					    break;
			        }
			        case 'enable':
			        {
					    $this->articles->update_article($ids, array('visible'=>1));
			            break;
			        }
			        case 'delete':
			        {
				        foreach($ids as $id)
						    $this->articles->delete_article($id);
			            break;
			        }
			    }
		}

		// Удаление изображения

                if($this->request->post('delete_image'))
                {
                    $this->articles->delete_image($post->id);
                }
                // Загрузка изображения
                $image = $this->request->files('image');
                if(!empty($image['name']) && in_array(strtolower(pathinfo($image['name'], PATHINFO_EXTENSION)), $this->allowed_image_extentions))
                {
                    if ($image_name = $this->image->upload_image($image['tmp_name'], $image['name']))
                    {
                            $this->articles->delete_image($post->id);
                            $this->articles->update_articles($post->id, array('image'=>$image_name));
                    }
                    else
                    {
                        $this->design->assign('error', 'error uploading image');
                    }
                }
                $post = $this->articles->get_articles(intval($post->id));

		$filter['page'] = max(1, $this->request->get('page', 'integer'));
		$filter['limit'] = 20;

		// Поиск
		$keyword = $this->request->get('keyword', 'string');
		if(!empty($keyword))
		{
			$filter['keyword'] = $keyword;
			$this->design->assign('keyword', $keyword);
		}

		$articles = $this->articles->get_articles($filter);
		$articles_count = $this->articles->count_articles($filter);

		$this->design->assign('articles_count', $articles_count);

		$this->design->assign('pages_count', ceil($articles_count/$filter['limit']));
		$this->design->assign('current_page', $filter['page']);

		$this->design->assign('articles', $articles);
		return $this->design->fetch('articles.tpl');
	}
}
