<?php
/**
 * Open Source - Content Managment System
 * Next Generation v2
 * @Coder:  Mutzii
 * @copyright  Copyright (c) 2012-2013 by Mutzii
 * File: template.class.php
  * @Description: Template, Platzhalter klassen  
 */ 

class Content
{
   public $content = null;
   protected $_tempdir;
   protected $_error_temp = null;
   protected $_error_msg  = null;
   
   
    public function Content($tempdir,$error_temp)
	{
	  $this->_tempdir   = ROOT.'/'.$tempdir;
	  $this->_error_temp = $error_temp;	
	}
	
	public function isError()
	{
	  if($this->_error_msg == null){     
	    return false;
	  }else{
	    return true;
	  }
	  
	}
	
	public function setErrorMsg($String)
	{
        $this->_error_msg = $String;
	}
	
	public function getErrorMsg()
	{
	   echo $this->_error_msg;
	}
	
	public function isReadFile($file)
	{
	  $file = $this->_tempdir.$file;
	 
	   if(!file_exists($file)){ 
	       return false;
	   }else{
	    
		if($this->EmptyFile($file)){	
		   return false;
		}else{
		   return true;
		}
		
	   }
	  
	}
	
	function EmptyFile($file)
	{
	   if($this->ReadFile2($file) == null){
	      return true;
	   }else{
	      return false;
	   }
	}
	
	public function ReadFile2($file)
	{
	   $file_content = null;
	   
	   if(!file_exists($file)){ 
	     if($this->_error_temp == null){
		     $this->setErrorMsg("File not Found. 404 ");
		 }else{
		    $file = $this->_error_temp;
		 }
	  }
	  
	   if(!$this->isError())
	  {
	       $file = fopen($file,"r");
		   while(!feof($file))
			{  
				$temp = fgets($file);
				$this->content .= $temp;
				$file_content  .= $temp;
		    }
	  
	  }else{
	       $this->getErrorMsg();
	  }
       fclose($file);
	   return $file_content;
	}
	
	public function ReadFile($file)
	{
	  $file = $this->_tempdir.$file;
	  $file_content = null;
	  
	  if(!file_exists($file)){ 
	     if($this->_error_temp == null){
		     $this->setErrorMsg("File not Found. 404 ");
		 }else{
		    $file = $this->_error_temp;
		 }
	  }
	  
	  if(!$this->isError())
	  {
	       $file = fopen($file,"r");
		   while(!feof($file))
			{  
				$temp = fgets($file);
				$this->content .= $temp;
				$file_content  .= $temp;
		    }
	  
	  }else{
	       $this->getErrorMsg();
	  }
       fclose($file);
	   return $file_content;
    }	  
	  
	  public function setFile($file,$content)
	  {
	     if(file_exists($file)){
		 
		   $file = fopen($file,"w");
		   fwrite($file,$content);
		   
		 }else{
		 
		    $this->setErrorMsg("File not Found. 404");
		    $this->getErrorMsg();
		 }
		 
		 fclose($file);
	  }
	  
	   public function replace($temp,$key = false, $value = false)
	  {
	      if(is_array($temp) && isset($temp)){
		  
		           if(isset($key) && isset($value))
		            {
		              $this->content = str_replace($key,$value,$this->content);		   
		             }
		  
	      foreach($temp as $key => $wert){ 
             //$temp[$key] = $wert + 1; 
	         $this->content = str_replace($key,$wert,$this->content);
           }
          }	
          
		  
	  
	  }
	  
	  public function Show()
	  {
         echo $this->content;
      }
	  
	  public function ContentToString()
	  {
	    return $this->content;
	  }
	  
	  
}


class placeholder
{
   public $daten;
   public $arraylist;
   
      public function Loading($table,$db_connect,$tempDir)
	  {
	      // Sobald in der config.inc.php der wert auf true ist, wird diese methode initialisiert...
		 if($db_connect == true)
		 {
         //Datenbank Lesen..		 
		 $sql = new SQL();
		 $query  = $sql->select($table);
		 $result = $sql->query($query);
	
		 $this->daten = array();
		 $counter = 0;
				
		 while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
            
		    $keys[$counter] = $row["variable"];
			
			$value = str_replace("DEVIT_TEMP", $tempDir, $row["value"]);
			$value = str_replace("DEVIT_ROOT", ROOT, $value);
			
			$values[$counter] = $value;
		    $counter = $counter+1;
			
		}
		
		$this->daten = array_combine($keys, $values);
	    return $this->daten;
        mysql_free_result($result);         		 
	    }
	  }
	  
     public function load($array)
	 {
	    $this->arraylist = $array;
	 }
	 
	 public function fill($array)
	 {
	   if($this->arraylist == NULL && $array != NULL)
	   {
	      // Parameter Array wird in das arraylist hinzugefügt...
	      $this->load($array);
	   }else{
	       // Arrays werden kombiniert
		   if(is_array($array)){
	         $this->arraylist = array_merge($this->arraylist,$array);
		   }
	   }
	   
	 }
 
     public function output()
	 {
	    return $this->arraylist;
	 }


	  
	  

}








?>