<?php

/**
 * eSales Media oxVoucher for Contao Open Source CMS
 *
 * Copyright (c) 2015 eSales Media
 *
 * @author  Benjamin Roth [benjamin@esales-media.de]
 * @license proprietary
 */

namespace eSM_oxVoucher;

abstract class ModuleVoucher extends \Module
{

	/**
	 * Oxid Database Object
	 * @var \Database|null
	 */
	protected $oxDB = null;

	/**
	 * Import Oxid database instance
	 */
	protected function importOxDbInstance()
	{
		$this->oxDB = Voucher::getOxDbInstance();
	}

	protected function setMessage($strMessage, $strType = 'info')
	{

		$strType = strtolower($strType);

		if ($strMessage == '')
		{
			return;
		}

		if (!in_array(strtolower($strType), array('confirm','info','error')))
		{
			throw new \Exception("Invalid message type $strType");
		}

		if (!is_array($_SESSION['eSM_oxVoucher'][$strType]))
		{
			$_SESSION['eSM_oxVoucher'][$strType] = array();
		}

		$_SESSION['eSM_oxVoucher'][$strType][] = $strMessage;
	}

	protected function getMessages()
	{
		$strMessages = '';

		foreach (array('confirm','info','error') as $strType)
		{
			if ($_SESSION['eSM_oxVoucher'][$strType])
			{
				foreach ($_SESSION['eSM_oxVoucher'][$strType] as $message)
				{
					$strMessages .= '<p class="'.$strType.'">'.$message.'</p>'."\n";
				}
			}
		}

		unset($_SESSION['eSM_oxVoucher']);

		return $strMessages;
	}
}