<?php
defined('PRODUCT_NAME') or die;
class HoursModule extends ModuleInstance
{
	function render()
	{
		$data = array("module_instance_id" 	=> $this->module_instance_id,
					  "title" 				=> $this->setting("title"),
					  "title_heading_level" => $this->setting("title_heading_level"),
		              "module_class" 		=> $this->setting("module_class"),
					  "sunday" 				=> $this->setting("sunday"),
					  "sunday_open" 		=> $this->setting("sunday_open"),
					  "sunday_close" 		=> $this->setting("sunday_close"),
					  "monday" 				=> $this->setting("monday"),
					  "monday_open" 		=> $this->setting("monday_open"),
					  "monday_close" 		=> $this->setting("monday_close"),
					  "tuesday" 			=> $this->setting("tuesday"),
					  "tuesday_open" 		=> $this->setting("tuesday_open"),
					  "tuesday_close" 		=> $this->setting("tuesday_close"),
					  "wednesday" 			=> $this->setting("wednesday"),
					  "wednesday_open" 		=> $this->setting("wednesday_open"),
					  "wednesday_close" 	=> $this->setting("wednesday_close"),
					  "thursday" 			=> $this->setting("thursday"),
					  "thursday_open" 		=> $this->setting("thursday_open"),
					  "thursday_close" 		=> $this->setting("thursday_close"),
					  "friday" 				=> $this->setting("friday"),
					  "friday_open" 		=> $this->setting("friday_open"),
					  "friday_close" 		=> $this->setting("friday_close"),
					  "saturday" 			=> $this->setting("saturday"),
					  "saturday_open" 		=> $this->setting("saturday_open"),
					  "saturday_close" 		=> $this->setting("saturday_close"),
					  "newyears" 			=> $this->setting("newyears"),
					  "newyears_open" 		=> $this->setting("newyears_open"),
					  "newyears_close" 		=> $this->setting("newyears_close"),
					  "familyday" 			=> $this->setting("familyday"),
					  "familyday_open" 		=> $this->setting("familyday_open"),
					  "familyday_close" 	=> $this->setting("familyday_close"),
					  "goodfriday" 			=> $this->setting("goodfriday"),
					  "goodfriday_open" 	=> $this->setting("goodfriday_open"),
					  "goodfriday_close" 	=> $this->setting("goodfriday_close"),
					  "eastersunday" 		=> $this->setting("eastersunday"),
					  "eastersunday_open" 	=> $this->setting("eastersunday_open"),
					  "eastersunday_close" 	=> $this->setting("eastersunday_close"),
					  "eastermonday" 		=> $this->setting("eastermonday"),
					  "eastermonday_open" 	=> $this->setting("eastermonday_open"),
					  "eastermonday_close" 	=> $this->setting("eastermonday_close"),
					  "victoriaday" 		=> $this->setting("victoriaday"),
					  "victoria_open" 		=> $this->setting("victoria_open"),
					  "victoria_close" 		=> $this->setting("victoria_close"),
					  "canadaday" 			=> $this->setting("canadaday"),
					  "canadaday_open" 		=> $this->setting("canadaday_open"),
					  "canadaday_close" 	=> $this->setting("canadaday_close"),
					  "augustcivic" 		=> $this->setting("augustcivic"),
					  "augustcivic_open" 	=> $this->setting("augustcivic_open"),
					  "augustcivic_close" 	=> $this->setting("augustcivic_close"),
					  "labourday" 			=> $this->setting("labourday"),
					  "labourday_open" 		=> $this->setting("labourday_open"),
					  "labourday_close" 	=> $this->setting("labourday_close"),
					  "thanksgiving" 		=> $this->setting("thanksgiving"),
					  "thanksgiving_open" 	=> $this->setting("thanksgiving_open"),
					  "thanksgiving_close" 	=> $this->setting("thanksgiving_close"),
					  "christmas" 			=> $this->setting("christmas"),
					  "christmas_open" 		=> $this->setting("christmas_open"),
					  "christmas_close" 	=> $this->setting("christmas_close"),
					  "boxingday" 			=> $this->setting("boxingday"),
					  "boxingday_open" 		=> $this->setting("boxingday_open"),
					  "boxingday_close" 	=> $this->setting("boxingday_close"),
					  "exceptions" 			=> $this->setting("exceptions"));

		$data["open"] 		= true;
		$data["status"] 	= "";
		$day 				= date("w"); // 0 = sunday
		$date 				= date("Y-m-d");
		$monthday 			= date("m-d");
		$time 				= time();
		$year 				= date("Y");
		$easter 			= date("Y-m-d", easter_date());
		$data["more_info"] 	= null;

		if($this->setting("page_id"))
		{
			$p = new Page();
			$p->load($this->setting("page_id"));
			$data["more_info"] = "/" . $p->url;
		}

		// Check if the company is open today.
		if($day == 0 && $this->setting("sunday") != 1)
		{
			$data["open"] = false;
			$data["status"] = "Closed Today";
		}
		else if($day == 0 && $time < strtotime($this->setting("sunday_open")))
		{
			$data["status"] = "Open " . $this->setting("sunday_open") . " to " . $this->setting("sunday_close") . " Today";
		}
		else if($day == 0 && $time > strtotime($this->setting("sunday_open")) && $time < strtotime($this->setting("sunday_open")))
		{
			$data["status"] = "Open Until " . $this->setting("sunday_close") . " Today";
		}
		else if($day == 0 && $time >= strtotime($this->setting("sunday_close")))
		{
			$data["open"] = false;
			$data["status"] = "Closed at " . $this->setting("sunday_close") . " Today";
		}
		else if($day == 1 && $this->setting("monday") != 1)
		{
			$data["open"] = false;
			$data["status"] = "Closed Today";
		}
		else if($day == 1 && $time < strtotime($this->setting("monday_open")))
		{
			$data["status"] = "Open " . $this->setting("monday_open") . " to " . $this->setting("monday_close") . " Today";
		}
		else if($day == 1 && $time >= strtotime($this->setting("monday_open")) && $time < strtotime($this->setting("monday_close")))
		{
			$data["status"] = "Open Until " . $this->setting("monday_close") . " Today";
		}
		else if($day == 1 && $time >= strtotime($this->setting("monday_close")))
		{
			$data["open"] = false;
			$data["status"] = "Closed at " . $this->setting("monday_close") . " Today";
		}
		else if($day == 2 && $this->setting("tuesday") != 1)
		{
			$data["open"] = false;
			$data["status"] = "Closed Today";
		}
		else if($day == 2 && $time < strtotime($this->setting("tuesday_open")))
		{
			$data["status"] = "Open " . $this->setting("tuesday_open") . " to " . $this->setting("tuesday_close") . " Today";
		}
		else if($day == 2 && $time >= strtotime($this->setting("tuesday_open")) && $time < strtotime($this->setting("tuesday_close")))
		{
			$data["status"] = "Open Until " . $this->setting("tuesday_close") . " Today";
		}
		else if($day == 2 && $time >= strtotime($this->setting("tuesday_close")))
		{
			$data["open"] = false;
			$data["status"] = "Closed at " . $this->setting("tuesday_close") . " Today";
		}
		else if($day == 3 && $this->setting("wednesday") != 1)
		{
			$data["open"] = false;
			$data["status"] = "Closed Today";
		}
		else if($day == 3 && $time < strtotime($this->setting("wednesday_open")))
		{
			$data["status"] = "Open " . $this->setting("wednesday_open") . " to " . $this->setting("wednesday_close") . " Today";
		}
		else if($day == 3 && $time >= strtotime($this->setting("wednesday_open")) && $time < strtotime($this->setting("wednesday_close")))
		{
			$data["status"] = "Open Until " . $this->setting("wednesday_close") . " Today";
		}
		else if($day == 3 && $time >= strtotime($this->setting("wednesday_close")))
		{
			$data["open"] = false;
			$data["status"] = "Closed at " . $this->setting("wednesday_close") . " Today";
		}
		else if($day == 4 && $this->setting("thursday") != 1)
		{
			$data["open"] = false;
			$data["status"] = "Closed Today";
		}
		else if($day == 4 && $time < strtotime($this->setting("thursday_open")))
		{
			$data["status"] = "Open " . $this->setting("thursday_open") . " to " . $this->setting("thursday_close") . " Today";
		}
		else if($day == 4 && $time >= strtotime($this->setting("thursday_open")) && $time < strtotime($this->setting("thursday_close")))
		{
			$data["status"] = "Open Until " . $this->setting("thursday_close") . " Today";
		}
		else if($day == 4 && $time >= strtotime($this->setting("thursday_close")))
		{
			$data["open"] = false;
			$data["status"] = "Closed at " . $this->setting("thursday_close") . " Today";
		}
		else if($day == 5 && $this->setting("friday") != 1)
		{
			$data["open"] = false;
			$data["status"] = "Closed Today";
		}
		else if($day == 5 && $time < strtotime($this->setting("friday_open")))
		{
			$data["status"] = "Open " . $this->setting("friday_open") . " to " . $this->setting("friday_close") . " Today";
		}
		else if($day == 5 && $time >= strtotime($this->setting("friday_open")) && $time < strtotime($this->setting("friday_close")))
		{
			$data["status"] = "Open Until " . $this->setting("friday_close") . " Today";
		}
		else if($day == 5 && $time >= strtotime($this->setting("friday_close")))
		{
			$data["open"] = false;
			$data["status"] = "Closed at " . $this->setting("friday_close") . " Today";
		}
		else if($day == 6 && $this->setting("saturday") != 1)
		{
			$data["open"] = false;
			$data["status"] = "Closed Today";
		}
		else if($day == 6 && $time < strtotime($this->setting("saturday_open")))
		{
			$data["status"] = "Open " . $this->setting("saturday_open") . " to " . $this->setting("saturday_close") . " Today";
		}
		else if($day == 6 && $time >= strtotime($this->setting("saturday_open")) && $time < strtotime($this->setting("saturday_close")))
		{
			$data["status"] = "Open Until " . $this->setting("saturday_close") . " Today";
		}
		else if($day == 6 && $time >= strtotime($this->setting("saturday_close")))
		{
			$data["open"] = false;
			$data["status"] = "Closed at " . $this->setting("saturday_close") . " Today";
		}

		// Check if this is a holiday, and whether the company is open on this holiday.
		$holidays = array("newyears" 		=> date("Y-m-d", strtotime("January 1 $year")),
		                  "familyday" 		=> date("Y-m-d", strtotime("February $year Third Monday")),
		                  "eastersunday" 	=> date("Y-m-d", strtotime("$easter")),
		                  "goodfriday" 		=> date("Y-m-d", strtotime("$easter last Friday")),
		                  "eastermonday" 	=> date("Y-m-d", strtotime("$easter next Monday")),
		                  "victoriaday" 	=> date("Y-m-d", strtotime("May 25 $year last Monday")),
		                  "canadaday" 		=> date("Y-m-d", strtotime("July 1 $year")),
		                  "augustcivic" 	=> date("Y-m-d", strtotime("August $year First Monday")),
		                  "labourday" 		=> date("Y-m-d", strtotime("September $year First Monday")),
		                  "thanksgiving" 	=> date("Y-m-d", strtotime("October $year Second Monday")),
		                  "christmas" 		=> date("Y-m-d", strtotime("December 25 $year")),
		                  "december" 		=> date("Y-m-d", strtotime("December 26 $year")),
		                  );

		foreach($holidays as $key=>$val)
		{
			if($date == $val && $this->setting($key) != 1)
			{
				$data["open"] = false;
				$data["status"] = "Closed Today";
			}
			else if($date == $val && $time < strtotime($this->setting($key . "_open")))
			{
				$data["open"] = true;
				$data["status"] = "Open " . $this->setting($key . "_open") . " to " . $this->setting($key . "_close") . " Today";
			}
			else if($date == $val && $time >= strtotime($this->setting($key . "_open")) && $time < strtotime($this->setting($key . "_close")))
			{
				$data["open"] = true;
				$data["status"] = "Open Until " . $this->setting($key . "_close") . " Today";
			}
			else if($date == $val && $time >= strtotime($this->setting($key . "_close")))
			{
				$data["open"] = false;
				$data["status"] = "Closed at " . $this->setting($key . "_close") . " Today";
			}
		}

		// Check if today has any exceptions.
		for($i=1; $i<=20; $i++)
		{
			if($monthday == $this->setting("exception_date" . $i) && $this->setting("exception_status" . $i) == 'c')
			{
				$data["open"] = false;
				$data["status"] = "Closed Today";
			}
			else if($monthday == $this->setting("exception_date" . $i) && $time < strtotime($this->setting("exception_open" . $i)))
			{
				$data["open"] = true;
				$data["status"] = "Open " . $this->setting("exception_open" . $i) . " to " . $this->setting("exception_close" . $i) . " Today";
			}
			else if($monthday == $this->setting("exception_date" . $i) && $time >= strtotime($this->setting("exception_open" . $i)) && $time < strtotime($this->setting("exception_close" . $i)))
			{
				$data["open"] = true;
				$data["status"] = "Open Until " . $this->setting("exception_close" . $i) . " Today";
			}
			else if($monthday == $this->setting("exception_date" . $i) && $time >= strtotime($this->setting("exception_close" . $i)))
			{
				$data["open"] = false;
				$data["status"] = "Closed at " . $this->setting("exception_close" . $i) . " Today";
			}
		}

		$this->load->view("modules/hours/module", $data);
	}
}
