<?php
/***************************************************************************
 *                                                                          *
 * Copyright (c) 2005 ISPG Technologies (I) Pvt Ltd. All rights reserved.   *
 *                                                                          *
 * This  is  commercial  software,  only  users  who have purchased a valid *
 * license  and  accept  to the terms of the  License Agreement can install *
 * and use this program.                                                    *
 *                                                                          *
 ****************************************************************************
 * PLEASE READ THE FULL TEXT  OF THE SOFTWARE  LICENSE   AGREEMENT  IN  THE *
 * "copyright.txt" FILE PROVIDED WITH THIS DISTRIBUTION PACKAGE.            *
 ****************************************************************************/
//
//$Id: ThemeModel.php, v 1.0 2011/06/14 20:52:11
// * To change this template, choose Tools | Templates
//* and open the template in the editor.
//
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 * Description of TimezoneModel
 *
 * @author sariyamol
 */
//$Id: TimezoneModel.php, v 1.0 2013/03/18 16:10:00
//
class TimezoneModel extends Model {
    //put your code here
    //FUNCTION TO LIST TIME ZONES
    function listTimezones($DeafaultTimeZone = '2') {
        $sql         = "(
			  SELECT 
				timeZoneID, 
				CONCAT(
				  timeZoneName, 
				  '( ', 
				  timeZoneDiff, 
				  ')'
				) AS timeZoneName, 
				priority, 
				dstStartDate, 
				dstEndDate 
			  FROM 
				`" . DB_PREFIX . "timezones` 
			  WHERE 
				timeZoneID = '$DeafaultTimeZone'
			) 
			UNION 
			  (
				SELECT 
				  timeZoneID, 
				  CONCAT(
					timeZoneName, 
					'( ', 
					timeZoneDiff, 
					')'
				  ) AS timeZoneName, 
				  priority, 
				  dstStartDate, 
				  dstEndDate 
				FROM 
				  `" . DB_PREFIX . "timezones` 
				WHERE 
				  timeZoneID != '$DeafaultTimeZone'
			  ) 
			ORDER BY 
			  priority ASC";
        $rsTimezones = $this->ObjDatabase->readValues($sql);
        return $rsTimezones;
    }
    //END OF FUNCTION
    //FUNCTION TO GET TIME ZONE DETAILS
    function getTimezoneDetails($DeafaultTimeZone, $fields = '') {
        $sql = "
			SELECT
				";
        if ($fields == '') {
            $sql .= "*";
        } else {
            $sql .= $fields;
        }
        $sql .= "	
			FROM 
				" . DB_PREFIX . "timezones 
			WHERE 
				timeZoneID = '" . $DeafaultTimeZone . "' 
			LIMIT 1";
        $rsTimezones = $this->ObjDatabase->readValueSlave($sql);
        return $rsTimezones;
    }
    //END OF FUNCTION
    //FUNCTION TO CONVERT TIME ZONE
    public function convert_tz($date_str, $tz, $date_format = "m/d/Y H:i") {
        $time   = strtotime($date_str);
        $tz_bak = getenv("TZ");
        putenv("TZ=$tz");
        $ret = date($date_format, $time);
        putenv("TZ" . ($tz_bak ? "=$tz_bak" : ""));
        return $ret;
    }
    //END OF FUNCTION
    //FUNCTION TO SET SERVER SESSION TIME ZONE
    public function setServerSessionTimeZone($defaultTimezoneID, $timeZoneDiff = '') {
        if ($timeZoneDiff != '') {
            $sql = "
					SET 
					  time_zone = '" . $timeZoneDiff . "'";
        } else {
            $sql = "
					SET 
						time_zone = 
						(
							SELECT 
							  timeZoneDiff 
							FROM 
							   " . DB_PREFIX . "timezones 
							WHERE
								timeZoneID='" . $defaultTimezoneID . "'
						)";
        }
        $resUpdateSessionTimeZone = $this->ObjDatabase->setQuery($sql);
    }
    //END OF FUNCTION
    //FUNCTION TO GET CURRENT TIME
    public function getCurrentTime() {
        $sql = "
				SELECT 
				  NOW() AS currentTime";
        return $this->ObjDatabase->getField($sql);
    }
    //END OF FUNCTION
    //FUNCTION TO CHANGE TIME ZONE DIFFERENT WITH DST
    public function changeTimeZoneDiffWithDST() {
        $currentTime  = $this->getCurrentTime();
        $currentTime  = date("F d H:i:s", strtotime($currentTime));
        $currentTime  = strtotime($currentTime);
        //echo '---'.strtotime("June 08 09:39:28");
        //echo '--'.strtotime("9 hours 39 minutes 28 seconds June 08");
        $resTimezones = $this->listTimezones();
        // Gets the month names
        $months       = array(
            "January",
            "February",
            "March",
            "April",
            "May",
            "June",
            "July",
            "August",
            "September",
            "October",
            "November",
            "December"
        );
        foreach ($resTimezones as $timeZones) {
            // search daylight start and end time with months
            for ($k = 0; $k < 12; $k++) {
                if (stristr($timeZones['dstStartDate'], $months[$k]) == TRUE) {
                    $startMonth = $months[$k];
                }
                if (stristr($timeZones['dstEndDate'], $months[$k]) == TRUE) {
                    $endMonth = $months[$k];
                }
            }
            // if start date is greater then end date then end date rounds to next year
            if (isset($startMonth) && isset($endMonth) && strtotime($startMonth) > strtotime($endMonth)) {
                $timeZones['dstStartDate'] = $timeZones['dstStartDate'] . ' ' . date('Y');
                $nextYear                  = date('Y') + 1;
                $timeZones['dstEndDate']   = $timeZones['dstEndDate'] . ' ' . $nextYear;
            }
            $dstStartDate = strtotime($timeZones['dstStartDate']);
            $dstEndDate   = strtotime($timeZones['dstEndDate']);
            $timeZoneID   = $timeZones['timeZoneID'];
            if (($currentTime > $dstStartDate && $currentTime < $dstEndDate)) {
                $sql = "UPDATE 
						  " . DB_PREFIX . "timezones 
						SET 
						  timeZoneDiff = timeZoneDiffDST 
						WHERE 
						  timeZoneID = '" . $timeZoneID . "'";
            } else {
                $sql = "UPDATE 
						  " . DB_PREFIX . "timezones 
						SET 
						  timeZoneDiff = timeZoneDiffDefault 
						WHERE 
						  timeZoneID = '" . $timeZoneID . "'";
            }
            $resUpdateTimeZone = $this->ObjDatabase->setQuery($sql);
        }
        return true;
    }
    //END OF FUNCTION
}