 <?php
defined('PUBLIC_ACCESS') or die('Restricted Access');

require_once($_SERVER['DOCUMENT_ROOT'] . '/sys/db.php');

class FieldInit {

  public $fieldInit = array();
  
  public function __construct($path) {
    
    $dal = new DAL();
    
    $sql   = "SELECT CO.control, CF.field, CA.attribute, CA.value " .
             "FROM menuitem MI, controlfield  CF, " .
             "controlattr  CA, controlobject CO " .
             "WHERE MI.path = '$path' AND CF.menuitem_id = MI.id " .
             "AND CA.controlfield_id = CF.id " .
             "AND CF.controlobject_id = CO.id " .
             "ORDER BY field ";
    $docpathFields = $dal->query($sql);

    $current = ""; 
    foreach ($docpathFields as $pathField) {

      if ($pathField->field != $current) {
        if ($current != "")
          $this->fieldInit[] = "\$field = (object)array( $attr ); $init";

        $init = "
          \$$pathField->field = new $pathField->control (\$field);
          \$this->".$pathField->field."Layout = $".$pathField->field."->outputLayout();
          \$this->".$pathField->field."Script = $".$pathField->field."->outputScript();";
        $comma = ",";
        $attr = "'field' => '$pathField->field'";
        $current = $pathField->field;
      }
      
      if ($pathField->value[0] == '$')
        $attr .= "$comma'$pathField->attribute' => $pathField->value";
      else
        $attr .= "$comma'$pathField->attribute' => '$pathField->value'";
         
      // Add comma separator after first element
      $comma = ',';
    }
    $this->fieldInit[] = "\$field = (object)array($attr); $init";
//print_r ("fields:");    
//$tmp = $this->fieldInit;
//print_r ($tmp);    

  }  

  public function get($prop) {
    return $this->$prop;
  }

}
?>  
  
  
