<?php
require_once 'rollout.interface.php';

/**
 * rollout_base class that will beextended in a rollout plugin
 *
 * 
 * @author axel
 */
class rollout_base implements iRolloutplugin{
    
    // ---------------------------------------------------------------
    // VARIABLES
    // ---------------------------------------------------------------
    /**
     * identifier for current plugin; it us used to find the current plugin
     * settings in the config structore for global and project based config
     * @var string
     */
    protected $_sPluginId='UNSET';
    /**
     * data with plugin infos (read from info.json)
     * @var array
     */
    protected $_aPlugininfos=false;
    
    /**
     * array with translation texts
     * @var type
     */
    protected $_aLang=false;
    
    /**
     * set language; 2 letter code, i.e. "de"; default language is "en" ; a 
     * file "lang_en.json" is required in the plugin dir
     * @var string
     */
    protected $_sLang = 'en';
    
    /**
     * string with phase of project; one of preview|stage|live
     * @var type
     */
    protected $_sPhase = false;
    
    /**
     * global configuration of the rollout plugin
     * @var array
     */
    protected $_aCfgGlobal = false;
    /**
     * configuration of the project
     * @var array
     */
    protected $_aCfgProject = false;
    

    // ---------------------------------------------------------------
    // METHODS
    // ---------------------------------------------------------------

    /**
     * initialize rollout plugin
     * @param array $aParams  hash with those possible keys
     *                  lang         string   language, i.e. 'de'
     *                  phase        string   name of phase in a project
     *                  globalcfg    array    given global config $aConfig
     *                  projectcfg   array    project config to generate config 
     *                                        for project and all phases
     * @return boolean
     */
    public function __construct($aParams) {
        
        // set current plugin id - taken from plugin directory name above
        $oReflection=new ReflectionClass($this);
        $this->_sPluginId=basename(dirname($oReflection->getFileName()));
       
        // ----- init language
        if (isset($aParams['lang'])){
            $this->setLang($aParams['lang']);
        } else {
            $this->setLang();
        }
        
        // ----- init phase
        if (isset($aParams['phase'])){
            $this->setPhase($aParams['phase']);
        }

        // ----- init global config
        if (isset($aParams['globalcfg'])){
            $this->setGlobalConfig($aParams['globalcfg']);
        }
        // ----- init project config
        if (isset($aParams['projectcfg'])){
            $this->setProjectConfig($aParams['projectcfg']);
        }
        return true;
    }
    

    protected function _renderForm($aFormdata, $sKey){
        $sReturn='';
        $sKeyPrefix=$this->getId().'_'.$sKey;
        $i=0;
        
        $oForm = new formgen($aForms);
        foreach ($aFormdata as $elementData) {
            $elementKey=$sKeyPrefix.'_'.$i++;
            $sReturn.=$oForm->renderHtmlElement($elementKey, $elementData);
        }
        return $sReturn;
    }
    
    /**
     * get a translated text from lang_XX.json in plugin dir;
     * If the key is missed it returns "[KEY :: LANG]"
     * 
     * @see setLang()
     * @param string $sKey  key to find in lang file
     * @return string
     */
    protected function _t($sKey){
        return (isset($this->_aLang[$sKey]) && $this->_aLang[$sKey])
                ? $this->_aLang[$sKey]
                : "[ $sKey :: $this->_sLang ]"
        ;
    }


    /**
     * set language for output of formdata and other texts.
     * This method loads the language file into a hash. The output of 
     * translated texts can be done with $this->_t("your_key")
     * 
     * @see _t()
     * @param string   $sLang  language code, i.e. "de"
     * @return boolean
     */
    public function setLang($sLang=false){
        $this->_sLang=$sLang ? $sLang : $this->_sLang;
        
        $oReflection=new ReflectionClass($this);
        $sFile=dirname($oReflection->getFileName()) . '/lang_'.$this->_sLang.'.json';
        $this->_aLang=(file_exists($sFile)) ? json_decode(file_get_contents($sFile), 1) : $this->_aLang;
        return true;
    }
    
    /**
     * set a phase for automatic use GETTER methods
     */
    public function setPhase($sPhase){
        $this->_sPhase=$sPhase;
        return true;
    }
    

    // ----------------------------------------------------------------------
    // INTERFACE :: CHECKS
    // ----------------------------------------------------------------------

    /**
     * check requirements if the plugin could work
     */
    public function checkRequirements(){
        // no specific checks needed ... always true
        return true;
    }

    /**
     * check access to a deploy target
     */
    public function checkConnectionToTarget(){
        // do nothing ... always true
        return true;
    }

    // ----------------------------------------------------------------------
    // INTERFACE :: SETTER
    // ----------------------------------------------------------------------


    /**
     * set Config ... by given global config of the current plugin
     * @param array $aConfigArray 
     */
    public function setGlobalConfig($aConfigArray){
        return $this->_aCfgGlobal=$aConfigArray;
    }



    /**
     * set Config ... by given project config
     */
    public function setProjectConfig($aProjectConfigArray){
        $this->_aCfgProject=$aProjectConfigArray;
        // echo '<pre>'.print_r($aProjectConfigArray, 1).'</pre>';
        // ----- ensure that the config structure exists 
        // (it is easier fo handling in getConfig())
        if (!isset($this->_aCfgProject['plugins']['rollout'][$this->_sPluginId])){
            /*
            if (!isset($this->_aCfgProject['plugins']['rollout'])){
                if (!isset($this->_aCfgProject['plugins'])){
                    $this->_aCfgProject['plugins']=array();
                }
                $this->_aCfgProject['plugins']['rollout']=array();
            }
             * 
             */
            $this->_aCfgProject['plugins']['rollout'][$this->_sPluginId]=array('INFO'=>'created');
        }
        return $this->_aCfgProject;
    }
    
    // ----------------------------------------------------------------------
    // INTERFACE :: GETTER
    // ----------------------------------------------------------------------

    /**
     * get a hash with the merged config for project or mo specific: of a given 
     * phase
     * @param  string  $sPhase
     * @return array
     */
    public function getConfig($sPhase=false){

        return ($sPhase && isset($this->_aCfgProject['phases']['plugins']['rollout'][$this->_sPluginId]))
            ? array_merge($this->_aCfgGlobal, $this->_aCfgProject['plugins']['rollout'][$this->_sPluginId], $this->_aCfgProject['phases']['plugins']['rollout'][$this->_sPluginId])
            : array_merge($this->_aCfgGlobal, $this->_aCfgProject['plugins']['rollout'][$this->_sPluginId])
        ;
    }
    
    /**
     * get string with current ID
     * @return string
     */
    public function getId(){
        return $this->_sPluginId;
    }
    
    /**
     * get string with plugin name (taken from plugin language file)
     * @return string
     */
    public function getName(){
        return $this->_t('plugin_name');
    }
    
    /**
     * get string with plugin description (taken from plugin language file)
     * @return string
     */
    public function getDescription(){
        return $this->_t('description');
    }
    /**
     * get array read from info.json
     * @return type
     */
    public function getPluginInfos(){

        if ($this->_aPlugininfos){
            return $this->_aPlugininfos;
        }
        
        $oReflection=new ReflectionClass($this);
        $sFile=dirname($oReflection->getFileName()) . '/info.json';
        $this->_aPlugininfos= (file_exists($sFile))
            ? json_decode(file_get_contents($sFile), 1)
            : array('error'=> 'unable to read info file ['.$sFile.'].')
        ;
        return $this->_aPlugininfos;
    }

    // ----------------------------------------------------------------------
    // INTERFACE :: RENDERER
    // ----------------------------------------------------------------------
    public function renderFormdata4Project() {
        return '';
    }

}