Subversion Repositories phpLibraryV2

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 rodolico 1
<?php
2
 
3
require_once "Template.class.php";
4
require_once "DBDatabase.class.php";
5
 
6
class DBTemplate extends Template {
7
 
8
   public static $autoSave = true;
9
 
10
   private $tableDef;
11
   private $type;       // the type of the template (view, edit, list) created
12
 
13
   public function __construct( $tableDef, $type='view', $templatePath = null, $filename = null ) {
14
      parent::__construct( $filename, $templatePath );
15
      $this->tableDef = $tableDef;
16
      $this->type = $type;
17
      if ( ! isset( $this->filename ) ) $this->FileName( );
18
   }
19
 
20
   public function process( $dataset = null ) {
21
      $this->dataset = array(
22
                              $this->tableDef->tableName => $this->tableDef->currentRecords
23
                              );
24
      return parent::process( 
25
                              $this->dataset
26
                           );
27
   }
28
 
29
   public function makeTemplate() {
30
      switch ( $this->type ) {
31
         case 'edit' : break;
32
         case 'list' : $this->makeListTemplate();
33
                       break;
34
         case 'view' : $this->makeViewTemplate();
35
                       break;
36
      } //switch
37
      if ( self::$autoSave ) {
38
         $this->saveTemplate();
39
      }
40
   } // function makeTemplate
41
 
42
   private function makeListTemplate() {
43
      $columnHeaders = array();
44
      $data = array();
45
      foreach ( $this->tableDef->columns as $name => $definition ) {
46
         $columnHeaders[] = $definition->displayName;
47
         $data[] = "<datafield $definition->columnName>";
48
      }
49
      $this->template = "<table border='1'>\n";
50
      $this->template .= "<tr><td>" . implode( '</td><td>', $columnHeaders ) . "</td></tr>\n";
51
      $this->template .= "<loop " . $this->tableDef->tableName . ">\n";
52
      $this->template .= "<tr><td>" . implode( '</td><td>', $data ) . "</td></tr>\n";
53
      $this->template .= "</loop>\n</table>\n";
54
   } // makeListTemplate
55
 
56
 
57
 
58
   private function makeViewTemplate () {
59
      $this->template = '';
60
      foreach ( $this->tableDef->columns as $name => $definition ) {
61
         $this->template .= "<tr><td>$definition->displayName</td><td>" . 
62
                             "<datafield $definition->columnName></td></tr>\n";
63
      }
64
      $this->template = "\n$this->template</table>\n";
65
   } // makeViewTemplate
66
 
67
  /*
68
    * name:        loadTemplate
69
    * parameters:  $filename -- optional override of filename stored in instance
70
    * returns:     nothing
71
    * description: Loads the template file from disk. This is an OVERLOAD of Templates::loadTemplate;
72
    */
73
 
74
   public function loadTemplate ( $type = null, $filename = null ) {
75
      if ( isset( $filename ) )
76
         $this->FileName( $filename );
77
      if ( isset( $type ) ) 
78
         $this->type = $type;
79
 
80
      try {
81
         parent::loadTemplate( );
82
      } catch ( Exception $e ) {
83
          $this->makeTemplate();
84
      }
85
   } // loadTemplate
86
 
87
 
88
   public function saveTemplate() {
89
      $results = file_put_contents( $this->filename, $this->template );
90
      if ( $results === false )
91
         throw new Exception( 'Can not write to [' . $this->filename . '], template not saved', 16 );
92
      return $results;
93
   } // loadTemplate
94
 
95
 
96
   public function FileName ( $filename = null ) {
97
      return parent::FileName( isset( $filename ) ? 
98
                                  $filename : 
99
                                  $this->tableDef->tableName . '-' . $this->type . '.template' 
100
                      );
101
   }
102
 
103
}
104
 
105
 
106
?>