<?PHP

class CommentsAdmin extends Buyers
{

    function fetch()
    {
        $filter = array();
        $filter['page'] = max(1, $this->request->get('page', 'integer'));
        $filter['limit'] = 40;

        // Тип
        $type = $this->request->get('type', 'string');
        if(!empty($type))
        {
        	$filter['type'] = $type;
     		$this->design->assign('type', $type);
     	}

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

      	// Обработка действий
      	if($this->request->method('post'))
      	{

    		// Действия с выбранными
    		$ids = $this->request->post('check');
    		if(!empty($ids) && is_array($ids))
    		switch($this->request->post('action'))
    		{
    		    case 'approve':
    		    {
    				foreach($ids as $id)
    					$this->comments->update_comment($id, array('approved'=>1));
    		        break;
    		    }
                case 'unapprove':
                {
                    foreach($ids as $id)
                        $this->comments->update_comment($id, array('approved'=>0));
                    break;
                }
    		    case 'delete':
    		    {
    				foreach($ids as $id)
    					$this->comments->delete_comment($id);
    		        break;
    		    }
    		}

     	}

    	// Отображение
      	$comments_count = $this->comments->count_comments($filter);
    	// Показать все страницы сразу
    	if($this->request->get('page') == 'all')
    		$filter['limit'] = $comments_count;
      	$comments = $this->comments->get_comments($filter);


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

     	$this->design->assign('comments', $comments);
     	$this->design->assign('comments_count', $comments_count);

    	return $this->design->fetch('comments.tpl');
    }
}
?>
