<?php
defined('_JEXEC') or die('Restricted access');
// jimport('joomla.application.component.view');

/*
$lang = JFactory::getLanguage();
$lang->load('com_cwreserveer', JPATH_SITE.'components/com_cwreserveer', $lang->getTag(), true);
*/

// print_r($lang);
class CwreserveerViewCalendar extends JViewLegacy {

    function selecteerAlleBoekingTypes() {
        $query = "SELECT * FROM #__cw_reserveer_boeking_type";
        return $query;
    }

    function getBoekingenVoorObject($object, $startDate, $endDate) {
        $query = "SELECT b.*, t.boekcode, t.boekdescr, t.boekclass
		    FROM #__cw_reserveer b, #__cw_reserveer_boeking_type t
			WHERE b.boeking_typeid = t.id AND b.objectid = $object AND b.published='1' AND b.tot >= '$startDate' AND b.van <= '$endDate' ORDER BY van ASC";
        return $query;
    }

    function getObjectNaam($object) {
        $database = & JFactory::getDBO();

        $database->setQuery("SELECT o.naam FROM #__cw_reserveer_object o WHERE o.id = $object");
        $records = $database->loadObjectList();
        return ($records[0]->naam);
    }

    function revDate($datein) {
        $y = substr($datein, 0, 4);
        $m = substr($datein, 5, 2);
        $d = substr($datein, 8, 2);

        return "$d-$m";
    }

    function createMonthCalendar($task, $month, $year, $locatie, $cw_object, $itemid) {
        $thismonth = gmdate('m');
        $thisyear = gmdate('Y');

        if ($year < $thisyear) {
            $year = $thisyear;
        }
        if ($year == $thisyear) {
            if ($month < $thismonth) {
                $month = $thismonth;
            }
        }

        $this->drawHeader($cw_object);
        // $this->drawSmallMonthNavigation1($task, $month, $year, $cw_object);
        $this->drawMonthCalendarvoorObject($task, $month, $year, $cw_object, $itemid);
        if ($month == '12') {
            $month = '01';
            $year = $year + 1;
        } else {
            $month = $month + 1;
            $year = $year;
        }
        $this->drawMonthCalendarvoorObject($task, $month, $year, $cw_object, $itemid);
        if ($month == '12') {
            $month = '01';
            $year = $year + 1;
        } else {
            $month = $month + 1;
            $year = $year;
        }
        $this->drawMonthCalendarvoorObject($task, $month, $year, $cw_object, $itemid);
        if ($month == '12') {
            $month = '01';
            $year = $year + 1;
        } else {
            $month = $month + 1;
            $year = $year;
        }
        $this->drawMonthCalendarvoorObject($task, $month, $year, $cw_object, $itemid);
        if ($month == '12') {
            $month = '01';
            $year = $year + 1;
        } else {
            $month = $month + 1;
            $year = $year;
        }
        $this->drawMonthCalendarvoorObject($task, $month, $year, $cw_object, $itemid);
        if ($month == '12') {
            $month = '01';
            $year = $year + 1;
        } else {
            $month = $month + 1;
            $year = $year;
        }
        $this->drawMonthCalendarvoorObject($task, $month, $year, $cw_object, $itemid);
        if ($month == '12') {
            $month = '01';
            $year = $year + 1;
        } else {
            $month = $month + 1;
            $year = $year;
        }
        $this->drawMonthCalendarvoorObject($task, $month, $year, $cw_object, $itemid);
        if ($month == '12') {
            $month = '01';
            $year = $year + 1;
        } else {
            $month = $month + 1;
            $year = $year;
        }
        $this->drawMonthCalendarvoorObject($task, $month, $year, $cw_object, $itemid);
        if ($month == '12') {
            $month = '01';
            $year = $year + 1;
        } else {
            $month = $month + 1;
            $year = $year;
        }
        $this->drawMonthCalendarvoorObject($task, $month, $year, $cw_object, $itemid);
        if ($month == '12') {
            $month = '01';
            $year = $year + 1;
        } else {
            $month = $month + 1;
            $year = $year;
        }
        $this->drawMonthCalendarvoorObject($task, $month, $year, $cw_object, $itemid);
        if ($month == '12') {
            $month = '01';
            $year = $year + 1;
        } else {
            $month = $month + 1;
            $year = $year;
        }
        $this->drawMonthCalendarvoorObject($task, $month, $year, $cw_object, $itemid);
        if ($month == '12') {
            $month = '01';
            $year = $year + 1;
        } else {
            $month = $month + 1;
            $year = $year;
        }
        $this->drawMonthCalendarvoorObject($task, $month, $year, $cw_object, $itemid);

        echo "<div class=\"clr\"></div></div>";
        $this->drawLegend();
        echo "<div class=\"clr\"></div></div>";
    }

    function display($tpl = null) {
        $view = JRequest::getWord('view');
        $task = JRequest::getWord('task');
        $itemid = JRequest::getInt('Itemid');

        $cw_prop = JRequest::getInt('cw_prop');
        $month = JRequest::getVar('month');
        $year = JRequest::getVar('year');
        $this->createMonthCalendar($task, $month, $year, $locatie, $cw_prop, $itemid);
    }

    function drawHeader($cw_prop) {
       $cw_propname = $this->getObjectNaam($cw_prop);
        echo "<div id=\"cwres\"><div class=\"calendars\">";
        echo "<h2 class=\"componentheading\">" . $cw_propname . "</h2>";
        if (strpos($cw_propname, 'Chambre') !== false) {
            echo JTEXT::sprintf('INTROTEXT_RESERVERING2', $cw_propname) . "<br /><br />";
        } else {
            echo JTEXT::sprintf('INTROTEXT_RESERVERING', $cw_propname) . "<br /><br />";
        }
    }

    function drawSmallMonthNavigation1($task, $month, $year, $cw_prop) {
        $model = $this->getModel();
        $thismonth = gmdate('m');
        $thisyear = gmdate('Y');
        $pm = '<img src="' . JURI::base() . 'components/com_cwreserveer/images/pm.png" border="0" alt="' . JText::_('COM_CWRESERVEER_PREVMONTH') . '" />';

        if (($thismonth >= $month) && ($thisyear >= $year)) {
            $lnprevmonth = '&nbsp;&nbsp;&nbsp;&nbsp;';
            $lnprevyear = '';
        } else {
            if ($month == 1) {
                $prevmonth = 12;
                $prevmyear = $year - 1;
            } else {
                $prevmonth = $month - 1;
                $prevmyear = $year;
            }
            $prevyear = $year - 1;
            $lnprevmonth = '<a href="' .
                    $model->buildUrl('calendar', 'display', JRequest::getWord('template'), $cw_prop, $prevmyear, $prevmonth) .
                    '" rel="nofollow">' . $pm . '</a>' . "\n";
        }
        if ($month == 12) {
            $nextmonth = 1;
            $nextmyear = $year + 1;
        } else {
            $nextmonth = $month + 1;
            $nextmyear = $year;
        }
        $nextyear = $year + 1;

        $nm = '<img src="' . JURI::base() . 'components/com_cwreserveer/images/nm.png" border="0" alt="' . JText::_('COM_CWRESERVEER_NEXTMONTH') . '" style="margin-right:10px;" />';
        $lnnextmonth = '<a href="' .
                $model->buildUrl('calendar', 'display', JRequest::getWord('template'), $cw_prop, $nextmyear, $nextmonth) .
                '" rel="nofollow">' . $nm . '</a>' . "\n";
        ?>
        <div class="monthnav"><div class="monthprev"><?php echo $lnprevmonth; ?></div><div class="monthnext"><?php echo $lnnextmonth; ?></div></div>
        <?php
    }

    function drawMonthCalendarvoorObject($task, $currMonth, $currYear, $cw_object, $itemid) {
        $cw_tmpl = JRequest::getVar('template');

        /* 		if (!empty($cw_tmpl)){
          //			$cw_tmpl = '&amp;template='.$cw_tmpl;
          $cw_tmpl = 'target="_parent"';
          } */

        $model = $this->getModel();
        $database = & JFactory::getDBO();

        $canEdit = $model->canEdit($cw_object);
        $daysInMonth = date("t", mktime(0, 0, 0, $currMonth, 1, $currYear));
        $firstDay = date("w", mktime(0, 0, 0, $currMonth, 1, $currYear));
        $tempDays = (($firstDay) % 7) + $daysInMonth;
        $weeksInMonth = ceil($tempDays / 7);
        // echo $daysInMonth . " " . $firstDay . " " . $tempDays . " ". $weeksInMonth;
        $counter = 0;
        $i = 0;
        $j = 0;
        for ($j = 0; $j < $weeksInMonth; $j++) {
            for ($i = 0; $i < 7; $i++) {
                $counter++;
                $week[$j][$i] = $counter;
                $week[$j][$i] -= $firstDay;
                if (($week[$j][$i] < 1) || ($week[$j][$i] > $daysInMonth)) {
                    $week[$j][$i] = "";
                }
            }
        }
        $startMonth = mktime(0, 0, 0, $currMonth, 1, $currYear);
        $endMonth = mktime(0, 0, 0, $currMonth, $daysInMonth, $currYear);
        $startDate = date("Y-m-d", $startMonth);
        $endDate = date("Y-m-d", $endMonth);

        $database->setQuery($this->getBoekingenVoorObject($cw_object, $startDate, $endDate));
        $reserveringen = $database->loadObjectList();
        $database->setQuery("SELECT * FROM #__cw_reserveer_prijzen WHERE objectid=$cw_object");
        $tarieven = $database->loadObjectList();
        $dagToegestaan = 0;

        foreach ($tarieven as $tarief) {
            if ($tarief->prijstype == 'dag') {
                $dagToegestaan = 1;
            }
        }

        $dagToegestaan = 1;
        $nu = gmdate('Ymd');
        ?>
        <div class="maandblock">
            <div class="maandheader"><?php echo JText::_('COM_CWRESERVEER_S' . date('M', mktime(0, 0, 0, $currMonth, 1, $currYear))) . ' ' . $currYear; ?></div>
            <div class="clr"></div>
            <div class="dagheader"><?php echo substr(JText::_('COM_CWRESERVEER_SUN'), 0, 2); ?></div>
            <div class="dagheader"><?php echo substr(JText::_('COM_CWRESERVEER_MON'), 0, 2); ?></div>
            <div class="dagheader"><?php echo substr(JText::_('COM_CWRESERVEER_TUE'), 0, 2); ?></div>
            <div class="dagheader"><?php echo substr(JText::_('COM_CWRESERVEER_WED'), 0, 2); ?></div>
            <div class="dagheader"><?php echo substr(JText::_('COM_CWRESERVEER_THU'), 0, 2); ?></div>
            <div class="dagheader"><?php echo substr(JText::_('COM_CWRESERVEER_FRI'), 0, 2); ?></div>
            <div class="dagheader"><?php echo substr(JText::_('COM_CWRESERVEER_SAT'), 0, 2); ?></div>
            <div class="clr"></div>
            <?php
            $prevresid = -1;
            for ($j = 0; $j < $weeksInMonth; $j++) {
                for ($i = 0; $i < 7; $i++) {
                    if ($week[$j][$i] != '') {
	                    $boekclass = "v";
						$link = "";
						$endlink = "";


                        $curryDay = mktime(0, 0, 0, $currMonth, $week[$j][$i], $currYear);
                        $curryDate = date("Y-m-d 09:00:00", $curryDay);
                        $curryDate2 = date("Y-m-d 23:00:00", $curryDay);
                        $bezet = 0;
                        $bezet2 = 0;
                        $resid = '';
                        if (count($reserveringen) > 0) {
                            foreach ($reserveringen as $row) {
                                if ($row->vanuur == '--:--') {
                                    $row->vanuur = '14:00';
                                }
                                if ((strtotime($curryDate) >= strtotime($row->van . ' ' . $row->vanuur)) && (strtotime($curryDate) <= strtotime($row->tot . ' ' . $row->totuur))) {
                                    $boekclass = $row->boekclass;
                                    $bezet2 = 1;
                                    break;
                                }
                            }
                            foreach ($reserveringen as $row) {
                                if ((strtotime($curryDate2) >= strtotime($row->van . ' ' . $row->vanuur)) && (strtotime($curryDate2) <= strtotime($row->tot . ' ' . $row->totuur))) {
                                    $bezet = 1;
                                    $boekclass .= $row->boekclass;
                                    $boekcode = $row->boekcode;
                                    $resid = $row->id;
                                    break;
                                }
                            }
                        }
                        if ($bezet == 1) {
                            if ($canEdit == true) {
                                $nicevan = $this->revDate($row->van);
                                $nicetot = $this->revDate($row->tot);
                                $commentaar = ucfirst(strip_tags(str_replace('<br />', "\n", $row->commentaar)));
                                $beschrijving = ucfirst(strtolower($row->voornaam)) . " " . ucfirst(strtolower($row->achternaam)) . " \nvan: " . $nicevan . "/" . $nicetot . " \n" . $commentaar;
                                if ($prevresid != $row->id) {
                                    $boekclass .= 'n';
                                    $prevresid = $row->id;
                                }
                                $divcont = "<a class=\"edit\" href=\"" . $model->buildUrl('edit', 'display', '', $resid) . "\" title=\"$beschrijving\" rel=\"nofollow\">" . $week[$j][$i] . "</a>";
                            } else {
                                $bd = sprintf("%04d%02d%02d", $currYear, $currMonth, $week[$j][$i]);
  							    $divcont = $week[$j][$i];
                                if ($bd < $nu) { //in het verleden
	                                $boekclass = 'p';
                                } else {
/*
	                                if (($i == 0) || ($i == 6)) {
                                        $boekclass .= 'w';
                                    }
*/
                                }
                            }
                        } else {
                            $bd = sprintf("%04d%02d%02d", $currYear, $currMonth, $week[$j][$i]);
                            $bdnice = sprintf("%04d-%02d-%02d", $currYear, $currMonth, $week[$j][$i]);
                            if (($dagToegestaan == 0) && ($i == 2 || $i == 3 || $i == 4 )) {
  							    $divcont = $week[$j][$i];

                                if ($bd < $nu) { //in het verleden
	                                $boekclass = 'p';
                                } else {
	                                $boekclass .= 'v';
                                }
                            } else {
                                if ($bd < $nu) { //in het verleden
	                                $boekclass = 'p';
	                                $divcont = $week[$j][$i];
                                } else { // in de toekomst
/*
                                    if (($i == 0) || ($i == 6)) {
                                        $boekclass .= 'w';
                                    } else {
*/
                                        $boekclass .= 'v';
//                                     }
                                    $divcont = "<a href=\"" . $model->buildUrl('boeken', 'display', 'component', $cw_object, $bdnice) ."\" rel=\"nofollow\">" . $week[$j][$i] . "</a>";
                                }
                            }
                        }
                        echo "<div class=\"$boekclass\">$divcont</div>\n";
                    } else { // 'lege dagen
                    	echo "<div class=\"empty\">&nbsp;</div>\n";
                    }
                } // dagen van de week
                echo "<div class=\"clr\">&nbsp;</div>\n";
            } //weken
            ?>
        </div>
        <?php
    }

    function drawLegend() {
// 	    phpinfo();
	    return;
/*
        $database = & JFactory::getDBO();
        $database->setQuery($this->selecteerAlleBoekingTypes());

        echo '<div class="clr">&nbsp;</div><div class="legendawrap"><div class="legenda past"></div><div class="legendatekst">' . JText::_('CWRESERVEER_VERLEDEN') . '</div></div>';
        echo '<div class="legendawrap"><div class="legenda weekeinde"></div><div class="legendatekst">' . JText::_('CWRESERVEER_WEEKEND') . '</div></div>';
        echo '<div class="legendawrap"><div class="legenda vrij"></div><div class="legendatekst">' . JText::_('CWRESERVEER_VRIJ') . '</div></div>';

        $bt = $database->loadObjectList();
        if (count($bt) > 0) {
            foreach ($bt as $row) {
                echo '<div class="legendawrap"><div class="legenda ' . $row->boekclass . '"></div><div class="legendatekst">' . JText::_($row->boekclass) . '</div></div>';
            }
        }
*/
    }

}
?>
