<?php

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: 			*/

// +----------------------------------------------------------------------+

// | PHP version 4/5                                                      |

// +----------------------------------------------------------------------+

// | Copyright (c) 2004-2010 ARMIA INC                                    |

// +----------------------------------------------------------------------+

// | This source file is a part of iScripts VistaCart                     |

// +----------------------------------------------------------------------+

// | Authors: Jipson<jipson.thomas@armiasystems.com>             		  |

// +----------------------------------------------------------------------+

App::import('Sanitize');

class GiftcardusersController extends AppController

{

    var $name = "Giftcarduser";

    var $helpers = array('Html', 'Form','Pagination','Javascript');   

	var $components = array ('Pagination','Catmenu'); // Added

	var $uses = array('Giftcarduser','Giftcard','Setting','Currency','User','Category','Cmspage','Cart');

    var $paginate = array('limit' => 25,'order' => array('Giftcarduser.date' => 'desc'));

	function index()

    {

       $this->__validateLoginStatus();
       $cnfg 				=	Configure::read('Neutrino');
        $style				=	$cnfg["style"];
        $this->set('style', "admin/".$style);

		$admsess	=	$this->Session->read('Admin');

		if($admsess["orders"] <> 1){

		 	$this->redirect(array('controller' => 'admins', 'action' => 'index')); 

			exit();

		}

		

		$usrtbl=$this->User->getTableName("users");	

		$usraddrtbl=$this->User->getTableName("user_addresses");	

		$gftcrdtbl=$this->User->getTableName("giftcards");		

		$gftdet=$this->User->getTableName("giftcard_details");		

		$cmbSearchType	=	"";

		$txtSearch		=	"";

		if(isset($_GET["cmbSearchType"]) && isset($_GET["txtSearch"])){

		/*

			$cmbSearchType	=	$_GET["cmbSearchType"];

			$txtSearch		=	$_GET["txtSearch"];

			if($cmbSearchType == "oid"){

				$data = $this->paginate('Order', array('Order.user_id =' =>$user_id,'Order.id =' => addslashes($txtSearch)));

			}elseif($cmbSearchType == "orderstatus"){

				$data = $this->paginate('Order', array('Order.user_id =' =>$user_id,'OrderStatus.name  LIKE' =>  addslashes($txtSearch)."%"));

			}elseif($cmbSearchType == "username"){

				$data = $this->paginate('Order', array('Order.user_id =' =>$user_id,'User.uname  LIKE' =>  addslashes($txtSearch)."%"));

			}elseif($cmbSearchType == "dateoforder"){

				$data = $this->paginate('Order', array('Order.user_id =' =>$user_id,'Order.date  LIKE' =>  addslashes($txtSearch)."%"));

			}else{

				$data = $this->paginate('Order', array('Order.user_id =' =>$user_id));

			}

			*/

		}else{

			//$data = $this->paginate('Giftcarduser', array('Giftcarduser.user_id =' =>$user_id));

			 	 

			 $this->paginate = array(

                                       'conditions' => array(''),					

							           'fields' => array('id','user_id','date','amnt_used','USR.uname','ADDR.fname','ADDR.lname','GCRD.gift_code','GDET.card_code','GDET.balance'), //array of field names

							          

									   'limit' => $this->paginate["limit"],

							           'joins' => array(

														array( 

														'table' => $usrtbl, 

														'alias' => 'USR', 

														'type' => 'INNER',  

														'conditions'=> array('Giftcarduser.user_id = USR.id') 

														), 

														array( 

															'table' => $gftdet, 

															'alias' => 'GDET', 

															'type' => 'LEFT',  

															'conditions'=> array('Giftcarduser.usercard_id = GDET.id') 

														), 

														array( 

															'table' => $gftcrdtbl, 

															'alias' => 'GCRD', 

															'type' => 'LEFT',  

															'conditions'=> array('Giftcarduser.card_id = GCRD.id') 

														), 

														array( 

															'table' => $usraddrtbl, 

															'alias' => 'ADDR', 

															'type' => 'LEFT',  

															'conditions'=> array('Giftcarduser.user_id = ADDR.user_id') 

														)

								                       ), // array of arrays defining join operations

							           'group' => 'id',

									   'passit' => $this->passedArgs

                                     );



              $data = $this->paginate('Giftcarduser');

             $this->set(compact('data'));

		}

		$this->set('cmbSearchType', $cmbSearchType);

		$this->set('txtSearch', $txtSearch);		

		$this->set('pagelimit', $this->paginate["limit"]);			

		$this->passedArgs['limit']=$this->paginate["limit"];

		$this->set('passedarg',$this->passedArgs);

		$this->set('giftdet', $data);

		$this->layout = 'adm';

    } 

	

	

	function mygiftcard(){

	    $sus		=	$this->Session->read('Userlog');

		global $currency;

		$curr		=	$this->Currency->findByCode($currency);		

		$this->set('curlogo',$curr["Currency"]["logo"]);

		$this->set('sus', $sus);

		$user_id	=	$sus["id"];

		

		$usrtbl=$this->User->getTableName("users");		

		$gftdet=$this->User->getTableName("giftcard_details");		

		$cmbSearchType	=	"";

		$txtSearch		=	"";

		if(isset($_GET["cmbSearchType"]) && isset($_GET["txtSearch"])){
                    


		/*

			$cmbSearchType	=	$_GET["cmbSearchType"];

			$txtSearch		=	$_GET["txtSearch"];

			if($cmbSearchType == "oid"){

				$data = $this->paginate('Order', array('Order.user_id =' =>$user_id,'Order.id =' => addslashes($txtSearch)));

			}elseif($cmbSearchType == "orderstatus"){

				$data = $this->paginate('Order', array('Order.user_id =' =>$user_id,'OrderStatus.name  LIKE' =>  addslashes($txtSearch)."%"));

			}elseif($cmbSearchType == "username"){

				$data = $this->paginate('Order', array('Order.user_id =' =>$user_id,'User.uname  LIKE' =>  addslashes($txtSearch)."%"));

			}elseif($cmbSearchType == "dateoforder"){

				$data = $this->paginate('Order', array('Order.user_id =' =>$user_id,'Order.date  LIKE' =>  addslashes($txtSearch)."%"));

			}else{

				$data = $this->paginate('Order', array('Order.user_id =' =>$user_id));

			}

			*/

		}else{
                        
			//$data = $this->paginate('Giftcarduser', array('Giftcarduser.user_id =' =>$user_id));

			 	 

			 $this->paginate = array(

                                       'conditions' => array('Giftcarduser.user_id =' => $user_id),					

							           'fields' => array('id','user_id','date','amnt_used','USR.uname','GDET.card_code','GDET.balance'), //array of field names

							           'order' => '', //string or array defining order

									   'limit' => $this->paginate["limit"],

							           'joins' => array(

														array( 

														'table' => $usrtbl, 

														'alias' => 'USR', 

														'type' => 'INNER',  

														'conditions'=> array('Giftcarduser.user_id = USR.id') 

														), 

														array( 

															'table' => $gftdet, 

															'alias' => 'GDET', 

															'type' => 'LEFT',  

															'conditions'=> array('Giftcarduser.usercard_id = GDET.id') 

														)

								                       ), // array of arrays defining join operations

							           'group' => '',

									   'passit' => $this->passedArgs

                                     );



             $data = $this->paginate('Giftcarduser');

             $this->set(compact('data'));

		}
                

		$this->set('cmbSearchType', $cmbSearchType);

		$this->set('txtSearch', $txtSearch);		

		$this->set('pagelimit', $this->paginate["limit"]);			

		$this->passedArgs['limit']=$this->paginate["limit"];

		$this->set('passedarg',$this->passedArgs);

		$this->set('giftdet', $data);

	}

	

	

	 function beforeFilter()

    {

		if (isset($this->Setting) && !empty($this->Setting->table))  

        {  

             $this->Setting->load();  

        } 

		global $sitefolder; 

		global $meta_description;

		global $meta_keywords;

		global $page_title;

		global $currency;

		$cnfg 				=	Configure::read('Neutrino');

		$sitefolder			=	$cnfg["sitebasefolder"];

		$currency			=	$cnfg["currency"];

		$meta_description	=	$cnfg["meta_description"];

		$meta_keywords		=	$cnfg["meta_keywords"];

		$page_title			=	$cnfg["page_title"];

		global $style; 

		$style				=	$cnfg["style"];

		$this->set('style', $style);

		$this->set('meta_description', $meta_description);

		$this->set('meta_keywords', $meta_keywords);

		$this->pageTitle = $page_title	; 

		$this->set('cnfg',$cnfg);

		$this->paginate["limit"]=$cnfg["items_perpage"]; //Setting pagination limit per page.
		
				$this->set('sitefolder', $sitefolder);

		$httpHost	=	$_SERVER['HTTP_HOST'];

		$this->set('sitebase','http://'.$httpHost);
                //get cart count
                 $sus = $this->Session->read();
                if ($this->Session->check('Userlog') === true) {
                    $userid = $sus["Userlog"]["id"];
                    $cartCount = $this->Cart->getCartCount($userid,'user');
                }else {
                    $sessid = $this->Session->id();
                    $cartCount = $this->Cart->getCartCount($sessid,'G');
                }
                $this->set('cartcount',$cartCount);


    }

	function beforeRender()

    {

       	//Code for Category Menu Listing starts here..

		// $lst	=	$this->Category->findAll();
                $lst	=	$this->Category->findAll(null, NULL, 'Category.cname ASC', null, null);
		 foreach($lst as $det)

		 $menu_array[$det["Category"]["id"]] = array('name' => $det["Category"]["cname"],'parent' => $det["Category"]["parent_id"]);

		 $this->set('menu_array', $menu_array);				
		 $footer_links = $this->Cmspage->find('list',array('conditions' => array('Cmspage.type' => 'content','Cmspage.status' => 'Y')));	
		$this->set('footerlinks', $footer_links);			

    }

	

	function __validateLoginStatus($typ='A')

    {

           if($typ == 'A'){

				if($this->Session->check('Admin') == false)

				{

					$this->redirect(array('controller' => 'admins', 'action' => 'login')); 

					$this->Session->setFlash('The URL you\'ve followed requires you login.');

				}

			}else{

				if($this->Session->check('Userlog') == false)

				{

					$this->redirect(array('controller' => 'users', 'action' => 'login')); 

					$this->Session->setFlash('The URL you\'ve followed requires you login.');

				}

			}

    } 

	

}

?>