<?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 ContactsController extends AppController



{



    var $name = "Contact";



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



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



	var $uses = array('Contact','Category','Cmspage','Cart');



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



    



    function beforeFilter()



    {



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



        {  



             $this->Setting->load();  



        } 



		global $sitefolder; 



		global $meta_description;



		global $meta_keywords;



		global $page_title;



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



		$sitefolder			=	$cnfg["sitebasefolder"];



		$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(){

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

		 if(empty($lst)===false)

		 {

		   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);	

		   

		 }  		

	}





   	function index(){


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



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



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



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



			exit();



		}



		$criteria=NULL;



		$cmbSearchType	=	"";



		$txtSearch		=	"";



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

					$cmbSearchType	=	$_GET["cmbSearchType"];

					$txtSearch		=	trim($_GET["txtSearch"]);

					if($cmbSearchType == "cname"){

						$data = $this->paginate('Contact', array('Contact.cname  LIKE ' => "%".addslashes($txtSearch)."%"));

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

						$data = $this->paginate('Contact', array('Contact.cdescr  LIKE ' => "%".addslashes($txtSearch)."%"));

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

						$data = $this->paginate('Contact', array('Contact.cemail  LIKE ' => "%".addslashes($txtSearch)."%"));

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

						$dt ='0000-00-00';

						if($txtSearch!='' && strpos($txtSearch,"-")>0)

						{

						list ($month, $day, $year) = split ("-", $txtSearch); //08-12-2010

						if(strlen($day)==1) $day = '0'.$day;

						if(strlen($month)==1) $month = '0'.$month;

						$dt = $year."-".$month."-".$day; //2010-08-13

						}

						$data = $this->paginate('Contact', array('Contact.cdate  LIKE ' => "%".addslashes($dt)."%"));

					}else{

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

					}

		}

		else if(isset($this->passedArgs["cmbSearchType"]) && isset($this->passedArgs["txtSearch"]) && 

		 $this->passedArgs["txtSearch"] <> '')

		{

		    $cmbSearchType	=	$this->passedArgs["cmbSearchType"];

			$txtSearch		=	$this->passedArgs["txtSearch"];			

					if($cmbSearchType == "cname"){

						$data = $this->paginate('Contact', array('Contact.cname  LIKE ' => "%".addslashes($txtSearch)."%"));

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

						$data = $this->paginate('Contact', array('Contact.cdescr  LIKE ' => "%".addslashes($txtSearch)."%"));

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

						$data = $this->paginate('Contact', array('Contact.cemail  LIKE ' => "%".addslashes($txtSearch)."%"));

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

						$dt ='0000-00-00';

						if($txtSearch!='' && strpos($txtSearch,"-")>0)

						{

						list ($month, $day, $year) = split ("-", $txtSearch); //08-12-2010

						if(strlen($day)==1) $day = '0'.$day;

						if(strlen($month)==1) $month = '0'.$month;

						$dt = $year."-".$month."-".$day; //2010-08-13

						}

						$data = $this->paginate('Contact', array('Contact.cdate  LIKE ' => "%".addslashes($dt)."%"));

					}else{

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

					}

		}else{

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

		}



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



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

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

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



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



		$this->layout = 'adm'; 



	}



    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.'));



				}



			}



    } 



	function add(){
            $cnfg 			= Configure::read('Neutrino');
            if(trim($_SERVER['HTTP_HOST']) == 'localhost'){ 
                include_once("../phpmailer/class.phpmailer.php");
            }else{
                include_once($_SERVER["DOCUMENT_ROOT"].$cnfg["sitebasefolder"]."/app/phpmailer/class.phpmailer.php");
            }


		//$lst	=	$this->Catmenu->getCatList(1);



		//$this->set('catmenu', $lst);



		//Code Added By shyam for Category Menu Listing starts here..



		 $lst	=	$this->Category->findAll();			 



		 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);		


		//Code Added By shyam for Category Menu Listing ends here..



		



				########### Section to store feedbacks............

                               

				if (!empty($this->data)) {



					$tm										=	date("Y-m-d" );



					$this->data["Contact"]["cdate"]			=	$tm;



					//print_r($this->data);exit;



					if ($this->Contact->save($this->data)) {



						$this->Session->setFlash(__('Your Feedback has been saved.', '/feedbacks'));
                                                
                                                $cnfg 			= Configure::read('Neutrino');
                                                $subject                = "A new contact us request with ".$cnfg["site_name"];
                                                $headers                = "From: ".$cnfg["site_name"]."<".$cnfg["admin_email"].">\r\n";
                                                $headers               .= "MIME-Version: 1.0\r\n";
                                                $headers               .= "Content-type: text/html; charset=iso-8859-1\r\n";
                                                $pdt 			= $this->Cmspage->findByTitle('Contact us mail for admin');
                                                $path			=	"SiteLogo_disp.gif";
                                                $mcont			=	"<table width='90%'  border='0' cellspacing='2' cellpadding='2' align='center'>
                                                                                 <tr><td><a href='".FULL_BASE_URL. $cnfg["sitebasefolder"]."' target='_blank'><img src='".FULL_BASE_URL. $cnfg["sitebasefolder"]."/app/webroot/".IMAGES_URL.$path."' border='0'></a></td></tr></table>";
                                                //$cnfg["site_name"]	=	"<a href='".FULL_BASE_URL. $cnfg["sitebasefolder"]."' target='_blank'>".$cnfg["site_name"]."</a>";
                                                $logurl			=	"<a href='".FULL_BASE_URL. $cnfg["sitebasefolder"]."/users/login' target='_blank'>Click here to login</a>";
                                                $mailcontent            = $mcont.$pdt["Cmspage"]["content"];
                                                $mailcontent            = str_replace("[name]", $this->data["Contact"]["cname"], $mailcontent);
                                                $mailcontent            = str_replace("[email]", $this->data["Contact"]["cemail"], $mailcontent);
                                                $mailcontent            = str_replace("[message]", $this->data["Contact"]["cdescr"], $mailcontent);
                                                $mailcontent            = str_replace("[SITE_NAME]", $cnfg["site_name"], $mailcontent);
                                                //echo  $mailcontent.'ssss'.$subject.'ssss'.$cnfg["admin_email"];exit;
                                                //$mailsent 		= mail($cnfg["admin_email"],$subject,$mailcontent,$headers);
                                                
                                                /******************************* MAIL TO THE USER ****************************/
                                                $site_name      = "";
                                                $admin_email    = "";
                                                if(trim($cnfg["site_name"]) <> ""){
                                                    $site_name  = trim($cnfg["site_name"]);                            
                                                }else{
                                                    $site_name = $cnfg["smtp_user"];                            
                                                }  
                                                if(trim($cnfg["admin_email"]) <> ""){
                                                    $admin_email = trim($cnfg["admin_email"]);
                                                }else{
                                                    $admin_email = $cnfg["smtp_user"];
                                                }
                    
                                                $mail             = new PHPMailer(true);                
                                                $body             = trim($mailcontent);
                                                //$body           = eregi_replace("[\]",'',$body);
                                                $mail->IsSMTP(); 
                                                $mail->SMTPSecure = 'tls';
                                                //$mail->SMTPDebug  = 2;                    
                                                $mail->SMTPAuth   = true;  
                    
                                                $mail->Host       = $cnfg["smtp_host"]; 
                    						$mail->Port       = $cnfg["smtp_port"];   
                    						$mail->Username   = $cnfg["smtp_user"];
                    						$mail->Password   = $cnfg["smtp_password"];      
                                                $mail->SetFrom($admin_email,$site_name);
                                                $mail->AddReplyTo($admin_email,$site_name);
                                                $mail->Subject    = $subject;
                                                $mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
                                                $mail->MsgHTML($body);                    
                                                $mail->AddAddress(trim($admin_email), ""); 
                                                $mailsent = $mail->Send();
                                                /******************************* MAIL TO THE USER ****************************/  
                                                
                                                $this->redirect(array('controller'=>'contacts','action'=>'add/'));



						exit;



					}



				}



				########### Section to store feedbacks............



	}



		function edit($id=0)
		 {

                    $cnfg 				=	Configure::read('Neutrino');
        $style				=	$cnfg["style"];
        $this->set('style', "admin/".$style);
		$this->__validateLoginStatus();
		$admsess	=	$this->Session->read('Admin');
		if($admsess["feedback"] <> 1){
		 	$this->redirect(array('controller' => 'admins', 'action' => 'index')); 
			exit();
		}
		if($id == 0){
			$this->redirect(array('controller' => 'contacts','action'=>'index'));
			exit();
		}

		$this->Contact->id	=	$id;
		if (!empty($this->data)) {						  
		    $fdate=explode('-', $this->data["Contact"]["cdate"]);    
            if(isset($fdate[0]))
			$this->data["Contact"]["cdate"]	=	$fdate[2].'-'.$fdate[0].'-'.$fdate[1];					
			$this->Contact->saveField('cname',$this->data["Contact"]["cname"]);
			$this->Contact->saveField('cemail',$this->data["Contact"]["cemail"]);
			$this->Contact->saveField('cdate',$this->data["Contact"]["cdate"]);
			$this->Contact->saveField('cdescr',$this->data["Contact"]["cdescr"]);
			$this->Session->setFlash(__('Feedback has been edited Successfully.', '/contacts'));
			$this->redirect(array('controller' => 'contacts', 'action' => 'index')); 	
		}
		$this->data 					= 	$this->Contact->read();
		$this->set('feedback', $this->data);		
		$this->layout = 'adm';
	} 
	
	

	function delete($id){



		$this->__validateLoginStatus();



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



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



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



			exit();



		}



		$this->Contact->del($id);



		$this->Session->setFlash(__(' Feedback Removed from Database.', '/feedbacks'));



		$this->redirect('index');



	}



    function contactus()

	{

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

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

		$dat["user_id"]		=	0;

		if(!empty($sus)===true)

		{

			$dat["user_id"]		=	$sus["id"];

		}

	}

	



}



?>