<?php

abstract class View {
    
    protected 
$tplData = array(); // protected to be accessible by child classes and templates
    
    
public abstract function display();
    
    protected function 
loadTemplate($moduleName$template) {
        
        
Helper::addDebugDiv('debug_tplData_'.get_class($this), 'tplData_'.get_class($this), $this->tplData);
        
        
$output '';
        
        
# get filename
        
$templateFilename LIBRARY.'modules/'.$moduleName.'/'.$template.'.tpl.php';
        
        
        if (
file_exists($templateFilename)) {
            
# get output
            
ob_start();
            
#extract($this->tplData);
            
include $templateFilename;
            
#$output = ob_get_contents();
            #ob_end_clean();
            
$output ob_get_clean();
        } else {
            
$bt debug_backtrace(); $backtraceFlat '['.__CLASS__.'::'.__FUNCTION__.' / line '.__LINE__.' ] [caller: '.$bt[0]['file'].' / line '.$bt[0]['line'].']';
            
Errors::log('invalid template filename: '.$templateFilename.' '.$backtraceFlat);
        }
        
        
# return output
        
return $output;
    }
    
//    /**
//     * gets the data referenced by the given key from the given model and adds it to tplData
//     * @param Model $model
//     * @param string $key
//     */
//    protected function get($model, $key) {
//        #if (array_key_exists($key, $this->tplData)) {
//            $this->tplData[$key] = $model->get($key);
//        #} else {
//        #    $bt = debug_backtrace(); $backtraceFlat = '['.__CLASS__.'::'.__FUNCTION__.' / line '.__LINE__.' ] [caller: '.$bt[0]['file'].' / line '.$bt[0]['line'].']';
//        #    Errors::log('array key does not exist: "'.$key.'" '.$backtraceFlat);
//        #    var_dump($key);
//        #    var_dump($this->tplData);
//        #    var_dump($model);
//        #}
//    }
    
    /**
     * gets the data referenced by the given key from the given model and adds it to tplData
     * @param Model $model
     * @param string $key
     */
    
protected function getLabelData($model) {
        
$this->tplData array_merge($this->tplData$model->getLabelData());
    }
    
    
/**
     * prints the data referenced by the given key
     * @param string $key
     */
    
protected function pd($key) {
        if (
array_key_exists($key$this->tplData)) {
            print 
$this->tplData[$key];
        } else {
            
$bt debug_backtrace(); $backtraceFlat '['.__CLASS__.'::'.__FUNCTION__.' / line '.__LINE__.' ] [caller: '.$bt[0]['file'].' / line '.$bt[0]['line'].']';
            
Errors::log('array key does not exist: "'.$key.'" '.$backtraceFlat);
        }
    }
    
    
/**
     * gets the data referenced by the given key
     * @param string $key
     * @return mixed
     */
    
protected function gd($key) {
        if (
array_key_exists($key$this->tplData)) {
            return 
$this->tplData[$key];
        } else {
            
$bt debug_backtrace(); $backtraceFlat '['.__CLASS__.'::'.__FUNCTION__.' / line '.__LINE__.' ] [caller: '.$bt[0]['file'].' / line '.$bt[0]['line'].']';
            
Errors::log('array key does not exist: "'.$key.'" '.$backtraceFlat);
        }
    }
    
    protected function 
isInvalid($fieldName$tplDataName) {
        if (
in_array($fieldName$this->gd($tplDataName)->invalidFields)) {
            return 
true;
        }
        return 
false;
    }
    
    protected function 
getObjectData($model$tplDataName$byId 0) {
        
# reuse stored post/request data
        
if (isset($_SESSION['request'])) {
            
$this->tplData[$tplDataName] = $model->getNewFromData($_SESSION['request']);
            unset(
$_SESSION['request']);
        }
        else {
            if (((int)
$byId) > 0) {
                
# load data
                
$this->tplData[$tplDataName] = $model->getById($byId);
            } else {
                
# create new/empty data
                
$this->tplData[$tplDataName] = $model->getNew();
            }
        }
    }
    
    protected function 
getInvalidFields($tplDataName) {
        
# check invalid fields
        
if (isset($_SESSION['invalidFields'])) {
            
$this->tplData[$tplDataName]->invalidFields $_SESSION['invalidFields'];
            unset(
$_SESSION['invalidFields']);
        }
    }
    
    protected function 
printListColumnLabelHeader($text$sortField) {
        
$currentSortField Session::getInstance()->getListConfig('sortField');
        
$currentSortMode  Session::getInstance()->getListConfig('sortMode');
        
        
# get arrow
        
$sortIcon '';
        if (
$currentSortField==$sortField) {
            switch (
$currentSortMode) {
                case 
'ASC':    $sortIcon .= '&nbsp;&#9652;';    break;
                case 
'DESC':    $sortIcon .= '&nbsp;&#9662;';    break;
            }
        }
        
# print th
        
?>
        <th class="<?php if ($currentSortField==$sortField) { print 'currentSortFieldHeader'; };?>
            <?php # invoice list in customer view: disable sorting ?>
            <?php if (RootController::getModuleName() === 'customer') { ?>
                onclick="" 
            <?php } else { ?>
                onclick="change_listConfig('sortField', '<?php print $sortField?>', '<?php print Session::getInstance()->getCurrentModule()?>');" 
            <?php ?>
            >
            <?php
            
print $text.$sortIcon;
            
?>
        </th>
        <?php
    
}
    
    protected function 
printListColumnFilterHeader($filterField) {
        
$filterText Session::getInstance()->getListConfig('filterText'''null$filterField);
        
$listModule Session::getInstance()->getCurrentModule();
        
        
# print th
        
?>
        <th class="listfilterHeader">
            <input type="text" class="listfilterInput" name="listConfig_<?php print $listModule?>_filterText[<?php print $filterField;?>]" <?php print ($filterText != '') ? 'style="background-color: orangered;"' '';?> value="<?php print $filterText;?>">
        </th>
        <?php
    
}
    
    protected function 
debugTplData() {
        
#$_tmp_log = fopen($_SERVER['DOCUMENT_ROOT'].'/_tmp_log.txt', 'a');   fwrite($_tmp_log, '$this->tplData:' . print_r($this->tplData, true) . "\n");   fclose($_tmp_log);
    
}
}