<?php
/**
*@desc This class adds new functions
* @author Md. Alauddin Husain
*/
class MY_Controller extends CI_Controller
{
    function MY_Controller()
    {
        parent::__construct();
        $this->white_label = $this->config->item('white_label');
        $this->white_label = $this->white_label[isset($_SESSION['white_label_id']) ? $_SESSION['white_label_id'] : 4];
    }

      /*
    * Application specific data
    * Created By: G J Jidni
    */
    function checkLogin($update_card = false)
    {
        if((isset($_SESSION['login_type']) && $_SESSION['login_type'] == 2) && !$update_card)
        {
            redirect();
        }

        if(!isset($_SESSION['logged_in']) || ($_SESSION['logged_in']!=1))
        {
            notification('You Must be Logged In to View This Page.');
            $_SESSION['merchant_request_uri'] = $this->uri->uri_string();
            redirect('login','location');
        }
    }

      /*
    * Application specific data
    * Created By: Ali Tareque Chowdhury
    * Date: 16 Feb 2010
    */
    function checkLoginShipUser()
    {
        if(!isset($_SESSION['ship_user_logged_in']) && ($_SESSION['ship_user_logged_in']!=1))
        {
            notification('You Must be Logged In to View This Page.');
            $url =  $this->uri->uri_string();
            $merchant_row_id = $this->uri->segment($this->uri->total_segments());
            non_secure_redirect('shipLogin/'.$merchant_row_id,'location');
        }
    }

    function checkMerchantOrUserLoggedIn()
    {
        if((!isset($_SESSION['logged_in']) || ($_SESSION['logged_in']!=1)) && (!isset($_SESSION['ship_user_logged_in']) && ($_SESSION['ship_user_logged_in']!=1)))
        {
            notification('You Must be Logged In to View This Page.');
            $_SESSION['merchant_request_uri'] = $this->uri->uri_string();
            redirect('login','location');
        }
    }

    /*
    * Application specific data
    * Created By: G J Jidni
    */
    function checkAdminLogin($url = '')
    {
        if(!isset($_SESSION['admin_logged_in']) && ($_SESSION['admin_logged_in']!=1))
        {
            $_SESSION['admin_request_uri'] = $url ? $url : $this->uri->uri_string();
            redirect('administration/admin_login','location');
        }
    }

    /**
     * This function checks the condition that if dashboard download page is accesed
     * from merchant panel or affiliate panel
     * added by : Himel Nag Rana
     */
    function checkMerchantAndAffiliateLogin()
    {
        if((!isset($_SESSION['logged_in']) && ($_SESSION['logged_in']!=1)) || (!isset($_SESSION['affiliate_logged_in']) && ($_SESSION['affiliate_logged_in']!=1)))
        {
            header("Location:".$this->config->item('expires_url'));
        }
    }

    function removeAdminSession()
    {
        unset($_SESSION['admin_logged_in']);
    }

    function removeAffiliateSession()
    {
        unset($_SESSION['affiliate_logged_in']);
        unset($_SESSION['affiliateId']);
    }
    function removeShipUserSession()
    {
        unset($_SESSION['ship_user_logged_in']);
        unset($_SESSION['shipUserId']);
    }
    function removeAdminAffiliateSession()
    {
        unset($_SESSION['admin_affiliate_logged_in']);
        unset($_SESSION['admin_affiliate_row_id']);
    }
    function removeMerchantSession()
    {
        unset($_SESSION['logged_in']);
        unset($_SESSION['userId']);
        unset($_SESSION['login_type']);
        unset($_SESSION['merchant_child_id']);
        unset($_SESSION['disable_video_popup']);
        unset($_SESSION['disable_merchant_video_popup']);
        unset($_SESSION['doNotShowCreditCardAlertInThisSession']);
    }

    /*
    * Application specific data
    * Created By: Shafiul Alam
    */
    function checkAffiliateLogin()
    {
        if(!isset($_SESSION['affiliate_logged_in']) && ($_SESSION['affiliate_logged_in']!=1))
        {
            $url =  $this->uri->uri_string();
            $merchant_row_id = $this->uri->segment($this->uri->total_segments());
            non_secure_redirect('affiliatePanel/affiliate_login/logout/'.$merchant_row_id,'location');
        }
    }

    function checkCustomerLogin()
    {
        if(!isset($_SESSION['loggedIn'])){
            redirect('customers/customer_setup/loginCheck','location');
        }
    }

    function _removeSession()
    {
        // Unset all of the session variables.
        $_SESSION = array();

        // If it's desired to kill the session, also delete the session cookie.
        // Note: This will destroy the session, and not just the session data!
        if (isset($_COOKIE[session_name()])) {
            setcookie(session_name(), '', time()-42000, '/');
        }

        // Finally, destroy the session.
        session_destroy();

        //Destroy the session
        unset($_SESSION);
    }

    /**
     *
     * added by G J Jidni
     * add http:// before the url
     *
    */

    function addHTTPtoURL($currentURL)
    {
        if(((substr($currentURL,0,7))=='http://')||((substr($currentURL,0,8))=='https://'))
        {
            return $currentURL;
        }
        else
        {
            $currentURL = 'http://'.$currentURL;
            return $currentURL;
        }
    }
    /*
    * Application specific data
    * Created By: Shafiul Alam
    */
    function checkAdminAffiliateLogin()
    {
        if(!isset($_SESSION['admin_affiliate_logged_in']) && ($_SESSION['admin_affiliate_logged_in']!=1))
        {
            non_secure_redirect('adminAffLogin/'.'1','location');
        }
    }

    function _isTaskRunning($taskID)
    {
        $date = date('dmyH');
        $sql = "SELECT task_id
                FROM tbl_cron_task_tracker
                WHERE task_id = $taskID
                AND date_mark = $date";

        $query = $this->db->query($sql);
        $row = $query->row();

        if($row && $row->task_id)
        {
            return true;
        }
        else
        {
            $this->db->insert('tbl_cron_task_tracker', array('task_id' => $taskID, 'date_mark' => $date));
            return false;
        }
    }

    function checkCoachLogin()
    {
        if (!isset($_SESSION['coach_row_id']) || (!$_SESSION['coach_row_id'])) {
            redirect('coach/login/logout', 'location');
        }
   }
   
   /**
    * @description setting coach timezone
    * @author sifat
    * @return void
    */
   function setTimeZone() {
       $timeZone = isset($_SESSION['coach_time_zone']) && $_SESSION['coach_time_zone'] ? $_SESSION['coach_time_zone'] : 'America/Los_Angeles';
       date_default_timezone_set($timeZone);
       
       //changing timezone in mysql
       //$this->db->query('SET time_zone=\'' . $timeZone . '\'');
   }
}
?>
