<?php

/**
 * Main Class
 * @author franzz
 * @version 0.1
 */
class Pedidor extends Main
{
	//SQL tables
	const USER_TABLE = 'users';
	const MATL_TABLE = 'materials';
	const MA_TY_TABLE = 'mat_types';
	const ORDER_TABLE = 'orders';
	
	private $oAuth;	
	
	public function __construct($oClassManagement, $sProcessPage)
	{
		//Load classes
		$oClassManagement->incClass('auth', true);
		
		parent::__construct($oClassManagement, $sProcessPage);
		
		//Init objects
		$this->oAuth = new Auth($this->oMySql, Settings::API_KEY);
	}
	
	protected function install()
	{
		$this->oAuth = new Auth($this->oMySql, Settings::API_KEY, false);
		
		//Install DB
		$this->oMySql->install();
		$this->addUser('clara');
		$this->addUser('franzz');
		$this->addUser('micro');
	}
	
	/* Authorizations handling */
	
	public function isLoggedIn()
	{
		return $this->oAuth->isLoggedIn();
	}
	
	public function logMeIn($sToken)
	{
		return $this->oAuth->logMeIn($sToken);
	}
	
	public function checkApiKey($sApiKey)
	{
		return $this->oAuth->checkApiKey($sApiKey);
	}
	
	/* Building main pages */
	
	public function getPage($bLoggedIn)
	{
		//Constants
		$asPages = array_values($this->asMasks);
		unset($asPages['index']);
		foreach($asPages as $sPage) $asGlobalVars['consts']['pages'][$sPage] = $this->getPageContent($sPage);
		$asGlobalVars['consts']['page_to_hash'] = array('logon'=>'connectarse', 'add_order'=>'crear_pedido', 'orders'=>'pedidos', 'products'=>'productos');
		$asGlobalVars['consts']['hash_to_page'] = array_flip($asGlobalVars['consts']['page_to_hash']);
		$asGlobalVars['consts']['token_sep'] = Auth::TOKEN_SEP;
		$asGlobalVars['consts']['error'] = self::ERROR;
		$asGlobalVars['consts']['success'] = self::SUCCESS;
		$asGlobalVars['consts']['context'] = $this->asContext;
		$asGlobalVars['consts']['process_page'] = $this->asContext['process_page'];
		$asGlobalVars['vars']['id'] = $this->oAuth->getUserId();
		$asGlobalVars['vars']['log_in'] = $bLoggedIn;
		
		//Main Page
		$sPage = $this->getPageContent('index');
		$sPage = str_replace('asGlobalVars', json_encode($asGlobalVars), $sPage);
		return $sPage;
	}
	
	/* DB structure. See MySqlManager::__construct */
	
	protected function getSqlOptions()
	{
		return array
		(
			'tables' 			=>	array
									(
										self::USER_TABLE	=> array(MySqlManager::getText(self::USER_TABLE), 'first_name', 'last_name', 'email', 'pass', 'cookie', 'active', 'clearance'),
										self::MATL_TABLE	=> array('z_code', MySqlManager::getText(self::MATL_TABLE), MySqlManager::getId(self::MA_TY_TABLE), 'safe'),
										self::MA_TY_TABLE	=> array(MySqlManager::getText(self::MA_TY_TABLE))
									),
			'types' 			=>	array
									(
										MySqlManager::getText(self::USER_TABLE)		=> "varchar(30) NOT NULL",
										'first_name'								=> "varchar(20) NOT NULL",
										'last_name'									=> "varchar(20) NOT NULL",
										'email'										=> "varchar(100) NOT NULL",
										'pass'										=> "varchar(255) NOT NULL",
										'cookie'								=> "varchar(255) NOT NULL",
										'active'									=> "tinyint(1) DEFAULT ".Auth::MEMBER_ACTIVE,
										'clearance'									=> "int(1) DEFAULT ".Auth::CLEARANCE_MEMBER,
										'z_code'									=> "varchar(20)",
										MySqlManager::getText(self::MATL_TABLE)		=> "varchar(200) NOT NULL",
										'safe'										=> "varchar(500) NOT NULL",
										MySqlManager::getText(self::MA_TY_TABLE)	=> "varchar(200) NOT NULL"
									),
			'constraints'		=>	array
									(
										self::USER_TABLE => "UNIQUE KEY `user_user` (`".MySqlManager::getText(self::USER_TABLE)."`, `last_name`)",
										self::MATL_TABLE => "UNIQUE KEY `uni_zeta` (`z_code`)",
									)/*,
			'cascading_delete'	=>	array
									(
										self::USER_TABLE=>array(self::SETTINGS_TABLE)
									)*/
		);
	}
	
	/* Pedidor public functions */
	
	public function uploadProducts()
	{
		$this->oClassManagement->incClass('phpexcel');
		$objPHPExcel = PHPExcel_IOFactory::load("05featuredemo.xlsx");
	}
	
	/* My Thoughts private functions */
	
	public function addUser($sNickName, $bLogMeIn=false)
	{
		return $this->oAuth->addUser($sNickName, $sNickName);
	}
	
	/* Static toolbox functions */

}

?>