<?PHP

class FeedbacksAdmin extends Buyers
{
  function fetch()
  {

    // Поиск
  	$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))
		switch($this->request->post('action'))
		{
			case 'disable':
			{
				$this->feedbacks->update_feedback($ids, array('visible'=>0));
				break;
			}
			case 'enable':
			{
				$this->feedbacks->update_feedback($ids, array('visible'=>1));
				break;
			}
		    case 'delete':
		    {
				foreach($ids as $id)
					$this->feedbacks->delete_feedback($id);
		        break;
		    }
		}

 	}

  	// Отображение
	$filter = array();
	$filter['page'] = max(1, $this->request->get('page', 'integer'));
	$filter['limit'] = 10;

	// Фильтр по типу
	$type = $this->request->get('type', 'string');
	if($type)
	{
		$filter['type'] = $type;
		$this->design->assign('filter', $type);
	}

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

    // Eсли человек первый раз на сайте, то сортировка в сессию пишется "по умолчанию"
	if(!$this->sessions->feedbacks_admin_sort)
		$this->sessions->feedbacks_admin_sort = 'added';

	if($sort = $this->request->get('sort', 'string'))
		$this->sessions->feedbacks_admin_sort = $sort;

	$filter['sort'] = $this->sessions->feedbacks_admin_sort;
	$this->design->assign('sort', $filter['sort']);

  	$feedbacks_count = $this->feedbacks->count_feedbacks($filter);

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

  	$feedbacks = $this->feedbacks->get_feedbacks($filter, true);

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

 	$this->design->assign('feedbacks', $feedbacks);
 	$this->design->assign('feedbacks_count', $feedbacks_count);

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


?>
