<?php
/**
 * @version   $Id$
 * @author    RocketTheme http://www.rockettheme.com
 * @copyright Copyright (C) 2007 - 2012 RocketTheme, LLC
 * @license   http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 only
 */

abstract class RokSprocket_Provider implements RokSprocket_IProvider
{
	/**
	 *
	 */
	const SORT_METHOD_AUTOMATIC = 'automatic';
	/**
	 *
	 */
	const SORT_METHOD_MANUAL = 'manual';

	/**
	 * @var RokCommon_Service_Container
	 */
	protected $container;

	/**
	 * @var string
	 */
	protected $fitler_file;

	/**
	 * @var RokCommon_Filter
	 */
	protected $filter;

	/**
	 * @var array
	 */
	protected $article_ids = array();

	/**
	 * @var array
	 */
	protected $filters = array();

	/**
	 * @var array
	 */
	protected $sort_filters = array();

	/**
	 * @var int
	 */
	protected $module_id;

	/**
	 * @var string
	 */
	protected $provider_name;

	/**
	 * @var string
	 */
	protected $sort_method = self::SORT_METHOD_AUTOMATIC;

	/**
	 * @var array
	 */
	protected $sort_options = array();

	/** @var RokCommon_Registry */
	protected $params;

	/**
	 * @var bool
	 */
	protected $showUnpublished = false;


	/**
	 * @param string $provider_name
	 */
	public function __construct($provider_name = 'unsupported')
	{
		$this->container     = RokCommon_Service::getContainer();
		$this->provider_name = $provider_name;
		$this->filter_file   = $this->container['roksprocket.providers.registered.' . $provider_name . '.path'] . '/' . $this->container['roksprocket.providers.registered.' . $provider_name . '.filter.file'];

		if (!file_exists($this->filter_file)) {
			throw new Exception(rc__('Unable to find filter file for %1s at path %2s.', $provider_name, $this->filter_file));
		}
		$xmlfile = simplexml_load_file($this->filter_file);
		$this->filter = new RokCommon_Filter($xmlfile);
	}


	/**
	 * @param array $filters
	 * @param       $sort_filters
	 */
	public function setFilterChoices($filters, $sort_filters)
	{
		$this->filters      = $this->format_filters($filters);
		$this->sort_filters = $this->format_filters($sort_filters);
	}

	/**
	 * @param $filters
	 *
	 * @return array
	 */
	protected function format_filters($filters)
	{
		$filter_lines = array();
		if (!empty($filters)) {
			$root_type = $this->filter->getRootType();
			if (!empty($filters)) {
				foreach ($filters as $row_number => $full_row) {
					foreach ($full_row[$root_type] as $filter_type => $filter_data) {
						if (!array_key_exists($filter_type, $filter_lines)) {
							$filter_lines[$filter_type] = array();
						}
						$filter_lines[$filter_type][] = $filter_data;
					}
				}
			}
		}
		return $filter_lines;
	}

	/**
	 * @return \RokCommon_Filter_IProcessor
	 */
	public function getFilterProcessor()
	{
		$processor_service = $this->container['roksprocket.providers.registered.' . $this->provider_name . '.filter.processor'];
		/** @var $processor RokCommon_Filter_IProcessor */
		$processor = $this->container->$processor_service;
		return $processor;
	}

	/**
	 * @param $id
	 */
	public function setModuleId($id)
	{
		$this->module_id = $id;
	}

	/**
	 * @param       $method
	 * @param array $options
	 */
	public function setSortInfo($method, array $options = array())
	{
		$this->sort_method  = $method;
		$this->sort_options = $options;
	}

	/**
	 * @param \RokCommon_Registry $params
	 */
	public function setParams(RokCommon_Registry $params)
	{
		$this->params = $params;
	}

	/**
	 * @param bool $show
	 */
	public function setShowUnpublished($show = false)
	{
		$this->showUnpublished = $show;
	}
}


