<?php
class plug_header{
    public $_css;
    public $_js;
    public $_plug;
    public $_plug_active;
    public $_controller;

    public function __construct($plug, $controller){
        $this->_css = array();
        $this->_js = array();
        $this->_plug_active = array();
        $this->_controller = $controller;
        $this->_plug = $plug;
    }
    
    public function add($plug_file){
        $tmp_css = array();
        $tmp_js = array();
        if (gettype ($plug_file ) == 'array'){
            
            foreach($plug_file as $file){
                
                foreach($this->_plug[$file]['css'] as $css_file){
                    $tmp_css[] = $css_file;
                }
                
                foreach($this->_plug[$file]['js'] as $js_file){
                    $tmp_js[] = $js_file;
                }
               
                $this->_plug_active[] = array("file"=>$file,"css"=>$tmp_css, "js"=>$tmp_js);
            }
        }
        else
        if (gettype ($plug_file ) == 'string'){
            foreach($this->_plug[$plug_file]['css'] as $css_file){
                $this->_css[] = $css_file;
            }
            foreach($this->_plug[$plug_file]['js'] as $js_file){
                $this->_js[] = $js_file;
            }
            $this->_plug_active[] = array("file"=>$plug_file,"css"=>$tmp_css, "js"=>$tmp_js);
        }
    }
    
    public function get_script(){
        $rtn_css  = "";
        $rtn_js  = "";

        foreach($this->_plug_active as $plugs){
            foreach($plugs['css'] as $css){
                
                $rtn_css .= file_get_contents('general/plugin/'.$plugs['file'].'/'.$css);
            }
            
            foreach($plugs['js'] as $js){
                
                $rtn_js .= file_get_contents('general/plugin/'.$plugs['file'].'/'.$js);
            }
        }
        
        if ($rtn_css) {
            $rtn_css = "<style>" . $rtn_css . "</style>";
        }
        
        if ($rtn_js) {
            
            $rtn_js = "<script>" . $rtn_js . "</script>";
        }
        
        return $rtn_css.$rtn_js;
    }
    
    public function render(){
        $rtn  = "";

        foreach($this->_plug_active as $plugs){

            foreach($plugs['css'] as $css){
                $rtn .= '
                    <link rel="stylesheet" href="general/plugin/'.$plugs['file'].'/'.$css.'" />';
            }
            
            foreach($plugs['js'] as $js){
                $rtn .= '<script src="general/plugin/'.$plugs['file'].'/'.$js.'" type="text/javascript"></script>';
            }
        }

        
        return $rtn;
    }
}


?>