<?php

  

  class ModuleFieldsParser {

    

    var $modules;

    var $name;

    

    function ModuleFieldsParser($name = 'mfp') {

      $this->modules = Array();

      if(file_exists('modules/EcmStockDocIns/ModuleFieldsParser/config.php')) { 

        require_once('modules/EcmStockDocIns/ModuleFieldsParser/config.php'); 

        $this->modules = $mfp_modules;

      }

      $this->name = $name;

    }

    

    function getModulesSelectOptions($fieldRealNames = false) {

      global $app_list_strings;

      $arr = array();

      if(count($this->modules)>0) {

        foreach($this->modules as $key => $value) {

          if(isset($value['name']) && $value['name'] != '')

            if(isset($app_list_strings['moduleList'][$value['name']]) && $app_list_strings['moduleList'][$value['name']] != '') 

              $value['name'] = $app_list_strings['moduleList'][$value['name']];

          $arr[$key]['name'] = $value['name'];

          $arr[$key]['fields'] = $this->getModuleFieldsSelectOptions($key,$fieldRealNames);

        }

      }

      return $arr;

    }

    

    function getModuleFieldsSelectOptions($module,$fieldRealNames = false) {

      global $beanList, $current_language, $GLOBALS;

      $dict = null;

      $file = 'cache/modules/'.$module.'/'.$beanList[$module].'vardefs.php';

      if(file_exists($file)) { 

        include($file); 

        $dict = $GLOBALS['dictionary'][$beanList[$module]]['fields']; 

      }

      else {

        $file = 'modules/'.$module.'/vardefs.php';

        if(file_exists($file)) { include($file); $dict = $dictionary[$beanList[$module]]['fields']; }

      }

      if($dict) {

        $mod = return_module_language($current_language, $module);

        $arr = array();

        foreach($dict as $key => $value) {

          $tmp = '$'.$this->modules[$module]['prefix'].$value['name'];

          if(!$fieldRealNames) 

            $arr[$tmp] = ((isset($mod[$value['vname']]) && $mod[$value['vname']] != '') ? $mod[$value['vname']] : $value['name']);

          else

            $arr[$tmp] = $value['name'];

        }

        return $arr;

      }

      return array();

    }

    

    function getJS() {

      $json = getJSONobj();

      $js = '<script language="javascript">';

      $js .= 'var '.$this->name.'_data = '.$json->encode($this->getModulesSelectOptions()).';';

      $js .= 'function '.$this->name.'_loadModules() { var ms = document.getElementById("'.$this->name.'_module"); for(x in '.$this->name.'_data) { ms.options[ms.options.length] = new Option('.$this->name.'_data[x]["name"],x,false); }; }';

      $js .= 'function '.$this->name.'_loadFields() { var ms = document.getElementById("'.$this->name.'_module"); var fs = document.getElementById("'.$this->name.'_fields"); while(fs.options.length>0) fs.remove(0); var fields = '.$this->name.'_data[ms.value]["fields"]; for(x in fields) fs.options[fs.options.length] = new Option(fields[x],x,false); fs.onchange(); };';

      $js .= 'function '.$this->name.'_loadField() { var fs = document.getElementById("'.$this->name.'_fields");  var fe = document.getElementById("'.$this->name.'_field"); fe.value = fs.value; };';

      $js .= $this->name.'_loadModules(); '.$this->name.'_loadFields(); '.$this->name.'_loadField();';

      $js .= '</script>';

      return $js;

    }

    

    function getFormHTML($smarty = false, $name = '') {

      if($name != '') $this->name = $name;

      $html = '

        <span>

        <select id="'.$this->name.'_module" name="'.$this->name.'_module" onChange="'.$this->name.'_loadFields();"></select>&nbsp;

        <select id="'.$this->name.'_fields" name="'.$this->name.'_fields" onChange="'.$this->name.'_loadField()"></select>&nbsp;

        <input id="'.$this->name.'_field" name="'.$this->name.'_field" value="" size="40">'

        .($smarty ? '{literal}' : '').$this->getJS().($smarty ? '{/literal}' : '').

        '</span>';

      return $html;

    }

  

    function fillFocusToModules() {

      foreach($this->modules as $key => $value) {

        if((!isset($value['focus']) || $value['focus'] == '') && (isset($value['id']) && $value['id'] != '')) {

          global $beanFiles, $beanList;

          $path = $beanFiles[$beanList[$key]];

          if(file_exists($path)) {

            require_once($path);

            $this->modules[$key]['focus'] = new $beanList[$key]();

            $this->modules[$key]['focus']->format_all_fields();

          }

        }

      }

    }



    function parseText($text) {

      $this->fillFocusToModules();

      $temp = $this->getModulesSelectOptions(true);

      foreach($this->modules as $module => $arr) {

        if(isset($arr['focus']) && $arr['focus']!='') {

          foreach($temp[$module]['fields'] as $field => $value) {

            //$text = str_replace($field,((isset($this->modules[$module]['focus']->$value) && $this->modules[$module]['focus']->$value != '')?$this->modules[$module]['focus']->$value:''),$text);

          }

        }

      }

      return $text;

    }

  

  };



?>