<?php
/**
 * Open Source - Content Managment System
 * Next Generation v2
 * @Coder:  Mutzii
 * @copyright  Copyright (c) 2012-2013 by Mutzii
 * File: ajax.api.php
 */

class ajax extends API
{
  protected $mode     = null;
  protected $activ    = null;
  protected $error    = null;
 
    public function Init()
    {
      $this->getAjaxEnable();
	  $this->setAjaxMode("default");
   }
   
	public function getAjaxDisable()
	{
	 $this->activ = false;
	 return $this->activ;
	}
	
	public function getAjaxEnable()
	{
	  $this->activ = true;
	  return $this->activ;
    }
	
	public function isAjaxActiv()
	{
       return $this->activ;
	}
  
    public function AjaxInstance($loginstatus)
    {
                 $this->getInstance()->setConfig();
			     $this->getInstance()->DataBase_Connect();
				 $this->getInstance()->setIndexContent('index');
				 $this->getInstance()->setIndexDefault();
				 $this->getInstance()->setTemplateDir('templates/ajax/');
				 $this->getInstance()->setLogin();
				 $this->getInstance()->getLogin()->start();
				 $this->getInstance()->setSQL();
				 $this->getInstance()->setPlaceHolder();
				 
				 $this->setAjaxMode("aktiv");
				 $this->error = $this->AjaxMessages();
				 
				 $lang      = $this->getInstance()->getAPI("language");
				 $lang_ID   = $lang->getUserIdLanguage($this->getInstance()->getAPI("auktion")->getLoginUser());
	             $user_lang = $lang->getUserLanguage($lang_ID);
	             $lang->Fill_Message_List($user_lang);
	             $lang->setPlaceHolders();
  
        if($this->getInstance()->getLogin()->Login()){
              if($this->getInstance()->LoginStatus() == $loginstatus){
			       return true;
			  }
	    }
  
  
    return false;
  }
  
  
  public function AjaxMessages()
  {
     $error = $this->getInstance()->getErrorList();
	 $error->addError(1,"Cart List is Empty.");
	 $error->addError(2,"No Select Items.");
	 $error->addError(3,"This action is not allowed here.");
	 $error->addError(4,"Remove Items Successfully.");
	 $error->addError(5,"poke into Post Box Successfully.");
	 $error->addError(6,"poke into StoreHouse Successfully.");
	 
     return $error;
  }
  
  
   
    public function isAjax($mode)
    {
     if($this->isAjaxActiv())
	 {
         if(isset($_GET['ajax'])){
	        if(!empty($_GET['ajax'])){
		        if(strcmp($_GET['ajax'],$mode) == false){
		           return true;
			    }
		    }
	    }
	 }
	 
    return false;
    }
  
  public function isAjaxPara(){
  
   if(isset($_GET['parameter'])){
	    if(!empty($_GET['parameter'])){
		  return true;
		}
	 }
	 
    return false;
  }
  
  public function getAjaxMode($mode)
  {
    if(strcmp($mode,$this->mode) == false){
	   return true;
	}
	
    return false;
  }
  
  public function setAjaxMode($mode)
  {
     $this->mode = $mode;
  }
  
  /* CAPTA: ConvertAjaxParameterToArray */
  public function CAPTA($parameter)
  {
     $item_liste = array();
	 $string  = $_GET['parameter'];
     $counter = substr_count($string, ',');
	 $teile = explode(",", $string);
					   
					   for($y=0; $y <= $counter; $y++){
					        array_push($item_liste,$teile[$y]);
					   }
					   
     return $item_liste;
  }
  
  public function Warenkorb()
  {
                       $counter = 0;
					   $warenkorb_liste = "";
					   $money_counter = 0;
					   $item_liste = $this->CAPTA($_GET['parameter']);
			           
					   			  
					   $size = count($item_liste);		   
					   $SQL = $this->getInstance()->getSQL();
					   $API = $this->getInstance()->getAPI("auktion");
					   
					   $user_money = $API->getPlayerMoney($API->getLoginUser());
					   
					   for($x = 0; $x < $size; $x++){
					   
					       $query  = $SQL->search("auktion","AuktionsID",$item_liste[$x]);
						   $result = $SQL->query($query);
						   $money  = "";
					       $bytes  = "";
						   $itemid = "";
						   
						   while($daten = mysql_fetch_object($result)){	     
							    $money_counter  = $money_counter + $daten->ItemPrice;
                                $money  = $daten->ItemPrice;	
                                $bytes  = $daten->ItemBytes;	
                                $itemid = $daten->ItemID;	 							
						     break;
						   }
      
							$item_image = $API->getImageItem($itemid,$bytes);   
							$warenkorb_liste .='
							
							<div id="WareHouseMidLeft"><img src="'.$item_image.'"></img></div>
					        <div id="WareHouseMidMid">'.$money.'</div>
							<div id="WareHouseMidMid"><a href="javascript:uncheck('.$item_liste[$x].');"><img src="'.$this->getInstance()->getTempDir().'images/rotes_x.gif"></img></a></div>
							
							';			
					   }
					   
					    $array = array(
					                "{warenkorb_liste}" => $warenkorb_liste,
					                "{warenkorb_money}" => $money_counter,
									"{user_money}"      => $user_money
									);
									
					   $this->getInstance()->getPlaceHolder()->fill($array);
					   
					   if($size != 0){
							$_content = new Content( $this->getInstance()->getTempDir(), $this->getInstance()->getTempError());		
							$_content->ReadFile( $this->getInstance()->getIndexContent(). $this->getInstance()->getContentFormat());
							$_content->replace( $this->getInstance()->getPlaceHolder()->output());
							$_content->Show();
					   }else{
					        echo "Cart List is Empty.";
					   }
  
  
  }
  
  
  public function AjaxListe($table)
  {
  
               $item_liste = $this->CAPTA($_GET['parameter']);
			   $size = count($item_liste);
			   
			   $SQL  = $this->getInstance()->getSQL();
			   $API  = $this->getInstance()->getAPI("auktion");
               
			   $liste  = "";
			   
				for($x = 0; $x < $size; $x++){
					   
					       $query  = $SQL->search($table,"AuktionsID",$item_liste[$x]);
						   $result = $SQL->query($query);
					       $bytes  = "";
						   $itemid = "";
						   
						   
						   while($daten = mysql_fetch_object($result)){	

                             if(strcmp($API->getLoginUser(),$daten->User) == false){						   	
                                $bytes  = $daten->ItemBytes;	
                                $itemid = $daten->ItemID;
								$item_image = $API->getImageItem($itemid,$bytes); 

								$liste .='
							
									<div id="WareHouseMidLeft"><img src="'.$item_image.'"></img></div>
									<div id="WareHouseMidMid"><a href="javascript:uncheck('.$item_liste[$x].');"><img src="'.$this->getInstance()->getTempDir().'images/rotes_x.gif"></img></a></div>	
							     ';	
							
                             }	 							
						     break;
						   }	
				}
	
				                    $array = array(
					                "{item_liste}" => $liste
									);$this->getInstance()->getPlaceHolder()->fill($array);
											   
					   if($size != 0){
							$_content = new Content($this->getInstance()->getTempDir(),$this->getInstance()->getTempError());		
							$_content->ReadFile("profil".$this->getInstance()->getContentFormat());
							$_content->replace($this->getInstance()->getPlaceHolder()->output());
							$_content->Show();
					   }else{
					        echo "Keine Items selektiert.";
					   }
			 
  }



}