<?php
/**
 * FIT Admin
 * 
 * $Id$
 * 
 * @author Daniel Jahnke <daniel.jahnke@web.de>
 * @package FIT
 * @license LGPL http://www.gnu.org/copyleft/lesser.html
 */
 
/**
 * load TableEditor
 */
include_once 'PHPFIT/TableEditor.php';

/**
 * FIT Admin
 * 
 * @version 0.2.1
 * @package FIT
 * @subpackage TableEditor
 */
class PHPFIT_Admin
{
   
/**
    * config
    * 
    * config settings
    * css-url       path to css-file
    * css-headline  alias for TableEditor headline from class in css-file
    * css-fileTable alias for file table form class in css-file
    * dataDir       path to testtable files
    * @var array
    */
    
private $_config = array( 
                
'css-url'       => 'default.css',
                
'css-headline'  => 'headline',
                
'css-fileTable' => 'fileTable',
                
'self'          => null,
                
'dataDir'       => 'data',
                
'testDir'       => 'test',
                );

   
/**
    * files 
    * contain all files which were found in dataDir
    * @var array
    */
    
private $_files = array();

   
/**
    * table editor
    * @var PHPFIT_TableEditor object
    */
    
private $_editor;

   
/**
    * tablesValue
    * contain the values from tables
    * @var array
    */
    
private $_tablesValue = array();

   
/**
    * check box
    * contain the selected rows
    * @var array
    */
    
private $_checkBox = array();


   
/**
    * constructor
    *
    * set the options to config-array
    * @param array $options  settings
    */
    
public function __construct$options null )
    {
        if( 
$options != null ) {
            foreach( 
$options as $key => $value ) {
                
$this->_config[$key] = $value;
            }
        }
        
        
// find myself
        
if( empty( $this->_config['self'] ) ) {
            
$this->_config['self']  =   $_SERVER['PHP_SELF'];
        }
        
        
// append question mark
        
if( !strstr$this->_config['self'], '?' ) ) {
            
$this->_config['self']  .=  '?';
        }
    }

   
/**
    * read data
    * read files from data-directory ( config-array => 'dataDir' ) recursive
    * an write the files into files-array.
    *  - name
    *  - size
    *  - time
    * @param string $dir    data-path
    */
    
private function _readData$dir )
    {
        
$directory = new DirectoryIterator$dir );
        while( 
$directory->valid() ) {
            if( !
$directory->isdot() ) {
                if( 
$directory->isdir() ) {
                    
$this->_readData$dir '/' $directory->current() );
                }

                if( 
ereg'\.html$|\.htm$'$directory->getfilename() ) ) {
                    
$tmp = array(
                            
'name' => str_replace$this->_config['dataDir'], ''$dir ) . '/' $directory->getfilename(),
                            
'size' => ( $directory->getsize() / 1000 ). ' kb',
                            
'time' => date'H:i:s d-m-Y'$directory->getmtime() ),
                                );
                    
$this->_files[] = $tmp;
                }
            }
            
$directory->next();
        }
    }

   
/**
    * display
    * decide which action will be called and display
    * the editor-content
    */
    
public function display()
    {
        
//echo '<pre><br> ' . print_r( $_REQUEST  , true ) ; echo '</pre>';
        
$actions $_REQUEST;

        
$action = array();
        
$table  = array();

        if( empty( 
$actions ) ){
            
$action[0] = 'list';
        }
        if( isset( 
$actions['action'] ) && $actions['action'] == 'tableEdit' ) {
            
$action[0] = 'startEdit';
        }
        else {
            foreach( 
$actions as $key => $value ) {
                
// rowAction
                
if( $key == 'action' ) {
                    foreach( 
$value as $k => $v ) {
                        
$action explode'_'$k );
                    }
                }
                
// table array
                
if( $key == 'table' ) {
                    
$this->_tablesValue $actions['table'];
                }
                
// table action includes checkBox
                
if( $key == 'tableAction' ) {
                    
$this->_checkBox = array();
                    foreach( 
$value as $k => $v ) {
                        
$box explode'_'$k );
                        
array_push$this->_checkBox$box );
                    }
                }
            }
        }

        if( isset( 
$actions['file'] ) ) {
            
$this->_editor = new PHPFIT_TableEditor$actions['file'], $this->_config );
        }

        if( !isset( 
$action[0] ) ) {
            
$action[0] = 'list';
        }

        switch( 
$action[0] ) {
            case 
'startEdit' :
                
$html $this->startEdit$actions['file'], $this->_config );
                break;

            case 
'newRow' :
                
$html $this->newRow$action[1] );
                break;

            case 
'save' :
                
$html $this->save$action[1], $this->_tablesValue[$action[1]], $action[2] );
                break;

            case 
'edit' :
                
$html $this->edit$action[1], $action[2] );
                break;

            case 
'delete' :
                
$html $this->delete$action[1], $action[2] );
                break;

            case 
'tableEdit' :
                
$html $this->tableEdit$action );
                break;

            case 
'saveFinal' :
                
$this->saveFinal();
                
$html $this->listFiles'dataDir' );
                break;

            case 
'cancel' :
                
$this->cancel();
                
$html $this->listFiles'dataDir' );
                break;

            default:
            case 
'list':
                
$html $this->listFiles'dataDir' );
                break;
        }

        echo 
'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html>
        <head>
        <script type="text/javascript">' 
file_get_contentsdirname__FILE__ ) . '/data/js/phpfit.js' ) . '</script>
        <style type="text/css">
        @import url( "' 
$this->_config['css-url'] . '" );
        </style>
        <title>Admin-TableEditor</title>
        </head>
        <body>'
;
        echo 
'<h2 class="' $this->_config['css-headline'] . '" >TableEditor</h2>';
        for( 
$i 0$i count$html ); $i++ ) {
            echo 
$html[$i];
        }
        echo 
'</body></html>';

    }

   
/**
    * listFiles
    * display the files from files-array
    * @return array $fileList   array with table-html-tags and files
    */
    
public function listFiles$type )
    {
        
$this->_readData$this->_config[$type] );

        
$self   =   $this->_config['self'];

        
$style  =   'class="' $this->_config['css-fileTable'] . '"';

        
$fileList[] = '<table align="center">';
        
$fileList[] = '<th> name </th><th> size </th><th> last modification </th>';
        for( 
$i 0$i count$this->_files ); $i++ ) {
            
$fileList[] = '<tr>';
            
$fileList[] = '<td ' $style '><a href="' $self '&amp;action=tableEdit&amp;file=/' $this->_files[$i]['name'] . '">' $this->_files[$i]['name'] . '</a></td>';
            
$fileList[] = '<td ' $style '>' $this->_files[$i]['size'] . '</td>';
            
$fileList[] = '<td ' $style '>' $this->_files[$i]['time'] . '</td>';
            
$fileList[] = '<tr>';
        }
        
$fileList[] = '</table>';
        return 
$fileList;
    }

   
/**
    * startEdit
    * called from function display
    * creates a PHPFIT_TableEditor-object
    * @param string $file   file to edit
    * @return array $html   array that contain the html-body of the PHPFIT_TableEditor
    */
    
public function startEdit$file )
    {
        
$this->_editor = new PHPFIT_TableEditor$file$this->_config );
        return 
$this->_editor->getHtml();
    }

   
/**
    * new row
    * called from function display
    * append a new row at table
    * @param int    $table
    * @return array $html   array that contain the html-body of the PHPFIT_TableEditor
    */
    
public function newRow$table )
    {
        
$this->_editor->newRow$table );
        return 
$this->_editor->getHtml();
    }

   
/**
    * save
    * called from function display
    * save the edited or new created row
    * @param int    $table
    * @param array  $cells  all values from row
    * @param int    $row    the row which will be saved
    * @return array $html   array that contain the html-body of the PHPFIT_TableEditor
    */
    
public function save$table$cells$row )
    {
        
$this->_editor->save$table$cells$row );
        return 
$this->_editor->getHtml();
    }

   
/**
    * save final
    * called from function display
    * overwrite the original file
    */
    
public function saveFinal()
    {
        
$this->_editor->saveFinal();
        return;
    }

   
/**
    * edit
    * called form function display
    * make the $row form $table editable
    * @param int $table
    * @param int $row
    * @return array $html   array that contain the html-body of the PHPFIT_TableEditor
    */
    
public function edit$table$row )
    {
        
$this->_editor->edit$table$row );
        return 
$this->_editor->getHtml();
    }

   
/**
    * delete
    * called form function display
    * delete the $row from $table
    * @param int $table
    * @param int $row
    * @return array $html   array that contain the html-body of the PHPFIT_TableEditor
    */
    
public function delete$table$row )
    {
        
$this->_editor->delete$table$row );
        return 
$this->_editor->getHtml();
    }

   
/**
    * cancel
    * called from function display
    * cancel the editor and donĀ“t write to original file
    */
    
public function cancel()
    {
        
$this->_editor->cancel();
        return;
    }

   
/**
    * table edit
    * called from fuction display
    * decide which action will be called to modify
    * the rows which are selected
    * @param array $action  contain on index 2 the method ( save, delete, edit )
    * @return array $html   array that contain the html-body of the PHPFIT_TableEditor
    */
    
public function tableEdit$action 
    {
        switch ( 
$action[2] ) {
            case 
'save' :
                
$this->_editor->tableSave$action[1], $this->_checkBox$this->_tablesValue );
                break;
            case 
'delete' :
                
$this->_editor->tableDelete$action[1], $this->_checkBox );
                break;
            case 
'edit' :
                
$this->_editor->tableEdit$action[1], $this->_checkBox );
                break;
            case 
'newRow' :
                
$this->_editor->tableNewRow$action[1], $this->_checkBox );
                break;
/*
            case 'addSummary' :
                $this->_editor->addSummaryTable( $action[1] );
                break;
            case 'removeSummary' :
                $this->_editor->removeSummaryTable( $action[1] );
                break;
*/
            
default :
                break;
        }

        return 
$this->_editor->getHtml();
    }
}
?>