/ Gists / ProductDeliveryDateInfo component
On gists

ProductDeliveryDateInfo component

Nette Nette-Controls AW

ProductDeliveryDateInfo.php Raw #

<?php

namespace FrontModule\Components;

use Nette,
	Andweb,
	Model;

class ProductDeliveryDateInfo extends Andweb\Application\UI\FrontControl 
{


	const HOUR = 13;



	protected $items = array(
				
			'Praha' => array(
			
				'Osobní odběr Praha'              => 0,
				'Expresní večerní doručení Praha' => 0,
			
			),
			
			'Česká republika' => array(
			
				'Česká pošta'    => 1,
				'Zásilkovna'     => 1,
				'Kurýrní služba' => 1,
			),			
			
			
			'Slovensko' => array(
			
				'Česká pošta'    => 2,
				'Zásilkovna'     => 2,
				'Kurýrní služba' => 2,
			),

	);


	public function __construct() 
	{

	}


	public function renderDefault() 
	{

		$template        = $this->template;
		$template->items = $this->items;

		// Fucking php5.3 on production
		$that = $this;

		$template->registerHelper('formatDate', function($s) use ($that) {
			return $that->formatDate($s);
		});
		
	}	



	public function renderNotAvailable() 
	{
		$template   = $this->template;
		$this->view = 'default';

		$template->notAvailable = TRUE;
		
		$template->items = $this->items;

		// Fucking php5.3 on production
		$that = $this;

		$template->registerHelper('formatDate', function($s) use ($that) {
			return $that->formatDate($s);
		});


		$this->render();
		
	}



	public function formatDate($s)
	{
		$today = new \DateTime();
		$tomorrow = clone $today;
		$tomorrow->modify('+ 1 day');

		if ($today->format('j.n.Y') == $s)
			return $this->presenter->translator->translate('dnes');			

		if ($tomorrow->format('j.n.Y') == $s)
			return $this->presenter->translator->translate( 'zítra');

		return $s;
	}


	// ukaze na velikonocni nedeli, napric vsemi casovymi pasmy, jinak funkce easter_day se chova obcas divne, viz php.net
	public function getEasterDateTime($year) 
	{
		$base = new \DateTime("$year-03-21");
		$days = easter_days($year);
	    return $base->add(new \DateInterval("P{$days}D"));
	}


	public function isNotHoliday(\Datetime $date)
	{
		// statni svatky
		$holidays = array('01-01', '05-01', '05-08', '07-05', '07-06', '09-28', '10-28', '11-17', '12-24', '12-25', '12-26');
		
		// velikonocni pondeli
		$holidays[] = $this->getEasterDateTime(date('Y'))->modify('+1day')->format('m-d');
		
		// velky patek, (pred velikonocnim pondelim)
		$holidays[] = $this->getEasterDateTime(date('Y'))->modify('-2day')->format('m-d');
		
		$day        = $date->format('w');

		if ($day == 0 || $day == 6) 
			return FALSE;

		if (in_array($date->format('m-d'), $holidays)) 
			return FALSE;

		return TRUE;
	}


	function getDeliveryDate($actualDate, $dayDelay = 0)
	{
		$actualDate = new \DateTime($actualDate);
		$actualDate->modify("+$dayDelay day");

		while (!$this->isNotHoliday($actualDate))
		{	
			$actualDate->modify('+1 day');
		}

		return $actualDate;
	}


	public function formatter($dayDelay)
	{
		// 13h a vic + 1 day
		if (date('H') >= self::HOUR)
			$dayDelay += 1;

		return $this->getDeliveryDate(date('Y-m-d'), $dayDelay);
	}





}