<?php if (!defined('TL_ROOT')) die('You cannot access this file directly!');




/**
 * Show information about selected shipping methods.
 *
 * @copyright  2013, Falko Schumann <http://www.muspellheim.de>
 * @author     Falko Schumann <falko.schumann@muspellheim.de>
 * @package    Controller
 */
class ModuleIsotopeShippingInfo extends ModuleIsotope
{

	/**
	 * @var string
	 */
	protected $strTemplate = 'mod_iso_shippinginfo';


	/**
	 * @return string
	 */
	public function generate()
	{
		if (TL_MODE == 'BE')
		{
			return $this->generateBackendWildcard();
		}
		return parent::generate();
	}


	/**
	 * @return string
	 */
	private function generateBackendWildcard()
	{
		$objTemplate = new BackendTemplate('be_wildcard');
		$objTemplate->wildcard = '### ISOTOPE SHIPPING INFO ###';
		$objTemplate->title = $this->headline;
		$objTemplate->id = $this->id;
		$objTemplate->link = $this->name;
		$objTemplate->href = 'contao/main.php?do=themes&amp;table=tl_module&amp;act=edit&amp;id=' . $this->id;
		return $objTemplate->parse();
	}


	protected function compile()
	{
		$arrModules = $this->getModules();
		if (empty($arrModules))
			$this->compileMessageNoModules();
		else
			$this->compileModules($arrModules);
	}


	/**
	 * Return an array of associative arrays with keys id, label, price and note.
	 *
	 * @return array
	 */
	private function getModules()
	{
		$arrModules = array();
		$arrModuleIds = deserialize($this->iso_shipping_modules);
		if (is_array($arrModuleIds) && !empty($arrModuleIds))
		{
			$arrModuleIds = array_map('intval', $arrModuleIds);
			$objModules = $this->Database->execute(
					'SELECT * ' .
					'FROM tl_iso_shipping_modules ' .
					'WHERE id IN (' . implode(',', $arrModuleIds) . ')' . (BE_USER_LOGGED_IN === true ? ' ' : " AND enabled='1' ") .
					'ORDER BY ' . $this->Database->findInSet('id', $arrModuleIds));
			while ($objModules->next())
			{
				$objModule = $this->createModule($objModules);
				if (!$objModule)
					continue;

				$arrModules[] = array
				(
						'id'		=> $objModule->id,
						'label'		=> $objModule->label,
						'price'		=> $this->calculatePrice($objModule),
						'note'		=> $objModule->note,
				);
			}
		}
		return $arrModules;
	}
	
	/**
	 * @param Database_Result $result
	 * @return IsotopeShipping|void
	 */
	private function createModule(&$result)
	{
		$strClass = $GLOBALS['ISO_SHIP'][$result->type];
		if (strlen($strClass) && $this->classFileExists($strClass))
			return new $strClass($result->row());

		return;
	}

	/**
	 * @param IsotopeShipping $objModule
	 * @return string
	 */
	private function calculatePrice(&$objModule)
	{
		$fltPrice = $objModule->price;
		$strSurcharge = $objModule->surcharge;
		if (!empty($strSurcharge))
			return ': ' . $strSurcharge;
		else if ($fltPrice != 0) 
			return ': '.$this->Isotope->formatPriceWithCurrency($fltPrice);

		return '';
	}


	private function compileMessageNoModules()
	{
		$this->Template = new FrontendTemplate('mod_message');
		$this->Template->class = 'shipping_method';
		$this->Template->hl = 'h2';
		$this->Template->headline = $GLOBALS['TL_LANG']['ISO']['shipping_method'];
		$this->Template->type = 'error';
		$this->Template->message = $GLOBALS['TL_LANG']['MSC']['noShippingModules'];
	}


	/**
	 * @param array $arrModules
	 */
	private function compileModules(&$arrModules)
	{
		$this->Template->shippingMethods = $arrModules;
	}

}

?>