<?php

/**
 * Contao Open Source CMS
 * Copyright (C) 2005-2012 Leo Feyer
 *
 * Formerly known as TYPOlight Open Source CMS.
 *
 * This program is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation, either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program. If not, please visit the Free
 * Software Foundation website at <http://www.gnu.org/licenses/>.
 *
 * PHP version 5
 * @copyright  terminal42 gmbh 2013
 * @author     Andreas Schempp <andreas.schempp@terminal42.ch>
 * @license    http://opensource.org/licenses/lgpl-3.0.html
 */


/**
 * Frontend module for Linux Magzin demo
 **/
class ModuleLinuxMagazin extends Module
{

	/**
	 * Template
	 * @var string
	 */
	protected $strTemplate = 'mod_linuxmagazin';


	public function generate()
	{
		if (TL_MODE == 'BE')
		{
			$objTemplate = new BackendTemplate('be_wildcard');

			$objTemplate->wildcard = '### LINUX MAGAZIN DEMO ###';
			$objTemplate->title = $this->headline;
			$objTemplate->id = $this->id;
			$objTemplate->link = $this->name;
			$objTemplate->href = $this->Environment->script.'?do=themes&amp;table=tl_module&amp;act=edit&amp;id=' . $this->id;

			return $objTemplate->parse();
		}

        // Demo styles from berlin.de website
		$GLOBALS['TL_CSS'][] = 'system/modules/linuxmagazin/assets/style.css';
		$GLOBALS['TL_CSS'][] = 'system/modules/linuxmagazin/assets/bde_2010_land.css';
		$GLOBALS['TL_CSS'][] = 'system/modules/linuxmagazin/assets/bde_2010.css';
		$GLOBALS['TL_CSS'][] = 'system/modules/linuxmagazin/assets/land_form.css';

		$GLOBALS['TL_JAVASCRIPT'][] = 'assets/mootools/datepicker/' . DATEPICKER . '/datepicker.js';
		$GLOBALS['TL_CSS'][] = 'assets/mootools/datepicker/' . DATEPICKER . '/dashboard.css';

		return parent::generate();
	}


	protected function compile()
	{
	    $this->Template->query = $this->Input->get('q');
	    $this->Template->bezirk = $this->Input->get('bezirk');
	    $this->Template->von_from = (null === $this->Input->get('von_from') ? date('d.m.Y') : $this->Input->get('von_from'));
	    $this->Template->von_to = (null === $this->Input->get('von_to') ? date('t.m.Y') : $this->Input->get('von_to'));
	    $this->Template->bis = $this->Input->get('bis');

	    $strUrl = 'http://www.berlin.de/sen/wirtschaft/service/maerkte/strassenfeste/index.php/index/all.json';

        $strUrl .= '?q=' . $this->Template->query;
        $strUrl .= '&bezirk=' . $this->Template->bezirk;
        $strUrl .= '&von_from=' . $this->Template->von_from;
        $strUrl .= '&von_to=' . $this->Template->von_to;
        $strUrl .= '&bis=' . $this->Template->bis;

	    $objRequest = new Request();
	    $objRequest->send($strUrl);

	    if ($objRequest->error) {
    	    throw new Exception($objRequest->error);
	    }

        $i = 0;
        $arrData = array();
	    $objData = json_decode($objRequest->response);

        $perPage = (int) $this->Input->get('ipp') ?: 20;
	    $page = (int) $this->Input->get('page') ?: 1;
	    $skip = ($page - 1) * $perPage;

	    foreach (array_slice($objData->index, $skip, $perPage) as $objResult) {

    	    $arrData[] = array_merge((array) $objResult, array(
    	        'von'   => date('d.m.Y', strtotime($objResult->von)),
    	        'class' => ($i%2 ? 'odd' : 'even'),
    	    ));

    	    $i += 1;
	    }

	    $this->Template->results = $arrData;
	    $this->Template->caption = $objData->results->count . ' Ergebnisse gefunden';
	    $this->Template->summary = $objData->results->count . ' Einträge insgesamt';
	    $this->Template->action = ampersand($this->Environment->request);

	    $objPagination = new Pagination($objData->results->count, $perPage);
	    $this->Template->pagination = $objPagination->generate();
        $this->Template->ipp = $perPage;
	}
}
