Subversion Repositories php_library

Rev

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

Rev Author Line No. Line
36 rodolico 1
<?php
2
 
3
 
4
/* 
5
   This is the basic column type. It is a simple string, 
6
      left justified on HTML display
7
      <input type='text'> for form input
8
      varchar for db storage
9
   It is also used as the basis for all of the other column types
10
*/
11
 
12
 
13
/*
14
   CSS Classes used
15
   db_string      display and input a string
16
   db_field_name  display table column header or td
17
   db_textarea    display and input textarea (td or <textarea>)
18
   db_bool        display and input true/false (radio)
19
   db_date        display and input date (text)
20
   db_datetime    display and input date and time (text)
21
   db_int         display and input integers
22
   db_real        display and input floating point numbers
23
   db_password    display and input password (password, display is a series of 8 asterisks)
24
   db_file        display and input file (type="file")
25
*/
26
 
27
class DBColumn {
28
   /*
29
      A column has
30
         name
31
         type (used to define how it is processed)
32
         nullable (if it can be null or not)
33
         default value (used if creating a row)
34
         required (must have a non-null value entered)
35
         readOnly (will be displayed as text in an input form)
36
         width (size of the actual field)
37
   */
38
   public      $columnName;   // name in database
39
   public      $primaryKey;   // true if this is a member of the primary key
40
   public      $displayName;  // human readable name, uses $columnName if not defined
41
   public      $value;        // in most cases, can hold the actual value (not type file or manyToMany)
42
   public      $required;     // if false, may be set to null
43
   public      $default;      // for not null columns, what value to use if it is null. Also displayed on creation of an insert new row screen
37 rodolico 44
   public      $readOnly;     // if set, an input field will not be created for this column; it will be displayed instead.
36 rodolico 45
   public      $width;        // width of input field and/or html display
46
   public      $null;         // true/false if it can be null (as opposed to empty)
47
 
48
   /* 
49
      display and data input are based upon these three display templates
50
      These templates can be modified at runtime by the appropriate functions
51
      Templates built in assume data will be displayed in a table
52
      NOTE: these templates have class attributes which may be loaded via a css file to ease formatting
53
   */
54
   protected   $HTMLHeaderTemplate = '<td class="db_field_name">~~display_name~~</td>';
55
   protected   $HTMLValueTemplate =  '<td class="db_string">~~value~~</td>';
56
   protected   $HTMLInputTemplate =  '<td class="db_field_name">~~display_name~~</td><td ><input type="text" name="~~column_name~~" class="db_string"> value="~~value~~"</td>';
57
 
58
   // Accepts a column name (required), and array of definitions, and a value (both optional)
59
   public function __construct ( $columnName, $definition = '', $value='') {
60
      $this->columnName = $columnName;
61
      $this->value = $value;
62
      $this->displayName = $definition['display name'];
63
      $this->required = $definition['required'];
64
      $this->default = $definition['default'];
65
      $this->width = $definition['width'];
66
      $this->readOnly = $definition['readonly'];
67
      if ( $definition['default'] == 'null' or $definition['null_ok'] ) {
68
         $this->null = true;
69
         $this->default = '';
70
      }
71
      $this->primaryKey = ($definition['keyfield'] ? true : false);
72
   } // function __construct
73
 
74
   // following three functions simply allow user to set and get values for the different templates
75
   public function valueTemplate( $newValue = '' ) {
76
      $returnValue = $HTMLValueTemplate;
77
      if ($newValue) {
78
         $HTMLValueTemplate = $newValue;
79
      }
80
      return $returnValue;
81
   }
82
   public function headerTemplate( $newValue = '' ) {
83
      $returnValue = $HTMLHeaderTemplate;
84
      if ($newValue) {
85
         $HTMLHeaderTemplate = $newValue;
86
      }
87
      return $returnValue;
88
   }
37 rodolico 89
   public function inputTemplate( $newValue = '' ) {
36 rodolico 90
      $returnValue = $HTMLInputTemplate;
91
      if ($newValue) {
92
         $HTMLInputTemplate = $newValue;
93
      }
94
      return $returnValue;
95
   }
96
 
37 rodolico 97
   /* 
98
      function takes a string, and looks for the array names in $values, replacing occurrences of it with 
36 rodolico 99
   */
37 rodolico 100
   private function replaceTokens ( $string, $values ) {
36 rodolico 101
      foreach( $values as $token => $value ) {
102
         $string = preg_replace("/$token/", $value, $string);
103
      }
104
      return $string;
105
   }
106
 
107
   /* function will return a display field based on template, with the value inside it */
108
   public function displayHTML ($template = '') {
109
      return replaceTokens($HTMLValueTemplate, array('~~value~~'=>$this->value));
110
   } // function displayHTML
111
 
112
   /* function will return a formatted header for column names of tables, etc... */
113
   public function HTMLColumnName ($template = '') {
114
      return replaceTokens($HTMLHeaderTemplate, array('~~display_name~~'=>($this->displayName ? $this->displayName : $this->columnName), 
115
                                                      '~~column_name~~' => $this->columnName));
116
   } // function HTMLColumnName
117
 
118
   /* function will return an input field */
119
   public function HTMLInputField ($template = '') {
120
      return replaceTokens($HTMLInputTemplate, array( '~~display_name~~'=>($this->displayName ? $this->displayName : $this->columnName), 
37 rodolico 121
                                                      '~~column_name~~' => $this->columnName,
36 rodolico 122
                                                      '~~value~~'=>$this->value
123
                                                    )
124
                          );
125
   } // function HTMLInputField
126
 
127
}
128
 
129
/* ======================================================================================================================
130
   class DBColumnBool
131
   Used for multi row columns, ie HTML TextArea's and DB Text columns 
132
*/
133
class DBColumnText extends DBColumn {
134
   protected   $HTMLInputTemplate =  '<td class="db_field_name">~~display_name~~</td><td><textarea name="~~column_name~~" class="db_textarea">~~value~~</textarea>';
135
   protected   $HTMLValueTemplate =  '<td class="db_textarea">~~value~~</td>';
136
   protected   $HTML = false;       // if true, field contains HTML
137
} // class DBColumnText
138
 
139
/* ======================================================================================================================
140
   class DBColumnBool
141
   used for True/False, 1/0, etc...
142
      html display T or F (can be overridden)
143
      Input is a True/False drop down on form input
144
      Stored in a char
145
*/
146
class DBColumnBool extends DBColumn {
147
   protected   $HTMLInputTemplate =  '<td class="db_field_name">~~display_name~~</td><td><INPUT class="db_bool" type="radio" ~~checked~~ name="~~column_name~~"></td>';
148
   protected   $HTMLValueTemplate =  '<td class="db_bool">~~value~~</td>';
149
 
150
   /* function will return a display field based on template, with the value inside it */
151
   public function displayHTML ($template = '') {
152
      return replaceTokens($HTMLValueTemplate, array('~~value~~'=>($this->value ? 'true' : 'false')));
153
   } // function displayHTML
154
 
155
   public function HTMLInputField ($template = '') {
156
      return replaceTokens($HTMLInputTemplate, array( '~~display_name~~'=>($this->displayName ? $this->displayName : $this->columnName), 
37 rodolico 157
                                                      '~~column_name~~' => $this->columnName,
36 rodolico 158
                                                      '~~checked~~'=> ($this->value ? 'checked' : '' ) // puts a check mark in if the value is true
159
                                                    )
160
                          );
161
   } // function HTMLInputField
162
} // class DBColumnBool
163
 
164
/* ======================================================================================================================
165
   class DBColumnDate
166
   holds a date only (ie, no time)
167
      html display is yyyy-mm-dd (can be overridden)
168
      input uses advanced library
169
      stored in a date
170
*/
171
class DBColumnDate extends DBColumn {
172
   protected   $HTMLValueTemplate =  '<td class="db_date">~~value~~</td>';
173
   protected   $HTMLInputTemplate =  '<td class="db_field_name">~~display_name~~</td><td ><input type="text" name="~~column_name~~" class="db_date"> value="~~value~~"</td>';
174
} // class DBColumnDate
175
 
176
/* ======================================================================================================================
177
   class DBColumnDateTime
178
   holds a date time stamp
179
      html display is yyyy-mm-dd hh:mm:ss (can be overridden)
180
      input uses advanced library
181
      stored in datetime
182
*/
183
class DBColumnDateTime extends DBColumn {
184
   protected   $HTMLValueTemplate =  '<td class="db_datetime">~~value~~</td>';
185
   protected   $HTMLInputTemplate =  '<td class="db_field_name">~~display_name~~</td><td ><input type="text" name="~~column_name~~" class="db_datetime"> value="~~value~~"</td>';
186
} // class DBColumnDateTime
187
 
188
/* ======================================================================================================================
189
   class DBColumnInt
190
   holds an integer
191
      html display is right justified
192
      input verifies numerics only
193
      stored in int (may be stored in int unsigned)
194
*/
195
class DBColumnInt extends DBColumn {
196
   protected   $HTMLValueTemplate =  '<td class="db_int">~~value~~</td>';
197
   protected   $HTMLInputTemplate =  '<td class="db_field_name">~~display_name~~</td><td ><input type="text" name="~~column_name~~" class="db_int"> value="~~value~~"</td>';
198
   public      $range;                 // if defined, number must be within this range
199
   public      $signed = true;        // if false, uses int unsigned
200
 
201
   public function __constructor( $columnName, $definitions = '', $value = '') {
202
      parent::__construct( $columnName, $definitions = '', $value = '' );     // first call base class
203
      if ($definition['min']) {
204
         $this->range['min'] = $definition['min'];
205
      }
206
      if ($definition['max']) {
207
         $this->range['max'] = $definition['max'];
208
      }
209
      if ($definition['signed']) {
210
         $this->signed = $definition['signed'];
211
      }
212
   } // function __constructor
213
 
214
} // class DBColumnInt 
215
 
216
/* ======================================================================================================================
217
   class DBColumnReal
218
   holds a floating point number
219
      html display is right justified
220
      may be padded
221
      input verfies floating point number
222
      stored in float
223
*/
224
class DBColumnReal extends DBColumnInt {
225
   public      $decimalPlaces;
226
   protected   $HTMLValueTemplate =  '<td class="db_real">~~value~~</td>';
227
   protected   $HTMLInputTemplate =  '<td class="db_field_name">~~display_name~~</td><td ><input type="text" name="~~column_name~~" class="db_real"> value="~~value~~"</td>';
228
 
229
   public function __constructor( $columnName, $definitions = '', $value = '') {
230
      parent::__construct( $columnName, $definitions = '', $value = '' );     // first call base class
231
      $this->decimalPlaces = $definition['decimal places'];
232
   } // function __constructor
233
 
234
} // class DBColumnReal
235
 
236
/* ======================================================================================================================
237
   class DBColumnPassword
238
   holds a hash of a password
239
      HTML display is a series of 8 stars
240
      html input is type='password'
241
      stored in a char(32). This is the MD5 sum of the password
242
*/
243
class DBColumnPassword extends DBColumn {
244
   protected   $HTMLValueTemplate =  '<td class="db_password">********</td>';
245
   protected   $HTMLInputTemplate =  '<td class="db_field_name">~~display_name~~</td><td ><input type="password" name="~~column_name~~" class="db_password"> value="~~value~~"</td>';
246
} // class DBColumnPassword
247
 
248
/* ======================================================================================================================
249
   class DBColumnFile
250
   holds file
251
      html display is file name, click to download
252
      input is type='file'
253
      stored either in a blob, or in a varchar as the filename on disk
254
*/
255
class DBColumnFile extends DBColumn {
256
   public      $viewable; // if true, can be displayed on screen, ie pictures
257
   public      $onDisk = true;   // if false, this is a blob column which contains the file. If true, it is a varchar which contains the path
258
   protected   $HTMLValueTemplate =  '<td class="db_file">~~value~~</td>';
259
   protected   $HTMLInputTemplate =  '<td class="db_field_name">~~display_name~~</td><td ><input type="file" name="~~column_name~~" class="db_file"> value="~~value~~"</td>';
260
 
261
   public function __constructor( $columnName, $definitions = '', $value = '') {
262
      parent::__construct( $columnName, $definitions = '', $value = '' );     // first call base class
263
      $this->viewable = $definition['viewable'];
264
   } // function __constructor
265
 
266
} // class DBColumnFile
267
 
268
/* ======================================================================================================================
269
   class DBColumnLookup
270
   Holds a foreign key value
271
      html display is target entry
272
      input is a dropdown
273
      stored as an int unsigned foreign key (tablename.primary key)
274
  The display will be a drop down box which will be filled with display values from the foreign table
275
  Basically, will generate the query
276
   select $this->$remotePrimaryKey,$this->remoteDisplayField 
277
   from $this->remoteTableName 
278
   where $this->filter
279
   sort by $this->sortOrder
280
 
281
  where
282
   '$this->filter' will only be used if it is non-null (otherwise, there will be no where clause)
283
   'sort by $this->sortOrder' will only be used if $sortOrder is non-null (otherwise, there will be no sort by)
284
   $remoteDisplayField must resolve to a single column in the query result, ie concat(field1, field2), not field1,field2
285
 
286
  The <select> box will have the remote field highlighted that corresponds to $this->value
287
  If $this->value is null and this is a nullable field, the special text "No Value" is added to the <select> box
288
  If $this->value is null and this is not a nullable field, the first item in the drop down will be selected.
289
*/
290
class DBColumnLookup extends DBColumn {
291
   protected   $remoteTableName;    // the actual table name in the database that is used for lookups
292
   protected   $remotePrimaryKey;   // column name in $remoteTableName that matches this columns value
293
   protected   $remoteDisplayField; // a string used in select statement to build a display
294
   protected   $filter;             // if set, will generate an additional where clause (anded) to limit the display of fields
295
   protected   $sortOrder;          // if set, will generate a sort by clause to determine display order
296
   protected   $HTMLValueTemplate =  '<td class="db_file">********</td>';
297
   protected   $HTMLInputTemplate =  '<td class="db_field_name">~~display_name~~</td><td ><select name="~~column_name~~" class="db_file">~~value~~</select></td>';
298
 
299
   public function __constructor( $columnName, $definitions = '', $value = '') {
300
      parent::__construct( $columnName, $definitions = '', $value = '' );     // first call base class
301
      $this->remoteTableName = $definition['table'];
302
      $this->remotePrimaryKey = array($definition['keyfiled']);
303
      $this->remoteDisplayField = $definition['display_field'];
304
      $this->filter = $definition['filter'];
305
      $this->sortOrder = $definition['sort by'];
306
   } // function __constructor
307
 
308
 
309
   /* function will return an input field */
310
   public function HTMLInputField ($template = '') {
311
      $dropDown = makeDropDown ( $this->remoteTableName, 
312
                                 $this->remotePrimaryKey,
313
                                 $this->remoteDisplayField,
314
                                 $this->value
315
                               );
316
 
317
      return replaceTokens($HTMLInputTemplate, array( '~~display_name~~'=>($this->displayName ? $this->displayName : $this->columnName), 
37 rodolico 318
                                                      '~~column_name~~' => $this->columnName,
36 rodolico 319
                                                      '~~value~~'=>$this->$dropDown
320
                                                    )
321
                          );
322
   } // function HTMLInputField
323
 
324
   // finds value in child table and simply displays it
325
   public function displayHTML ($template = '') {
326
      $display = getOneDBValue("select $this->remoteDisplayField from $this->remoteTableName where $this->remotePrimaryKey = $value");
327
      return replaceTokens($HTMLValueTemplate, array('~~value~~'=>$display));
328
   } // function displayHTML
329
 
330
} // class DBColumnLookup
331
 
332
/* ======================================================================================================================
333
   class DBColumnManyToMany
334
   simply indicates a one to many relationship
335
      HTML display is a list of remote table values
336
      input is a multi-select
337
      stored as a secondary table with 
338
         one set of columns containing the primary key of this table
339
         second set of column containing the primary key of a second table.
340
*/
341
class DBColumnManyToMany extends DBColumn {
342
} // class DBColumnManyToMany
343
 
344
 
345
 
346
class DBRow {
347
}
348
 
349
/*
350
   A table is made up of columns
351
   A table has relationships with other tables
352
   A table has an array (possibly null) of columns that make up the primary key
353
   A table has an array of columns displayed for choosing a row from a list
354
   A table has a (possibly null) query to display the list. If null, will default to select *
355
*/
356
class DBTable {
357
   protected $tableName;
358
   protected $columns;
359
   public    $displayName;
360
   public    $displayColumns;
361
   public    $displayQuery;
362
 
363
   public function __construct( $tableName, $displayName = '', $displayColumns = '', $displayQuery = '', $columnDefinitionArray = '' ) {
364
      $this->tableName = $tableName;
365
      // if a display name is not passed in, use the table name
366
      $this->displayName = ($displayName ? $displayName : $tableName );
367
      // if a list of display columns is not passed in, use the column names from the column definition
368
      $this->displayColumns = ($displayColumns ? $displayColumns : array_keys($columnDefinitionArray) );
369
      // if a display query is not passwd in, just do a select *
370
      $this->displayQuery = ($displayQuery ? $displayQuery : "select * from $this->tableName" );
371
      // get the column definitions
37 rodolico 372
      $this->columnDefinitionsArrayToObject( $columnDefinitionArray );
36 rodolico 373
   } // function __construct
374
 
375
 
376
   // Will create an array of DBColumns based upon the definition in $arr
377
   // see information in separate file for definition of array
37 rodolico 378
   public function columnDefinitionsArrayToObject ( $arr ) {
36 rodolico 379
      $thisColumn;
380
      foreach ( $arr as $columnName => $definitions ) {
381
         switch ( $definitions['type'] ) {
382
            case 'text'    : $thisColumn = new DBColumnText($columnName, $definitions );
383
                              break;
384
            case 'date'    : $thisColumn = new DBColumnDate($columnName, $definitions );
385
                              break;
386
            case 'datetime': $thisColumn = new DBColumnDateTime($columnName, $definitions );
387
                              break;
388
            case 'int'     : $thisColumn = new DBColumnInt($columnName, $definitions );
389
                              break;
390
            case 'real'    : $thisColumn = new DBColumnReal($columnName, $definitions );
391
                              break;
392
            case 'password': $thisColumn = new DBColumnPassword($columnName, $definitions );
393
                              break;
394
            case 'file'    : $thisColumn = new DBColumnFile($columnName, $definitions );
395
                              break;
396
            case 'lookup'  : $thisColumn = new DBColumnLookup($columnName, $definitions );
397
                              break;
398
            case 'multi'   : $thisColumn = new DBColumnManyToMany($columnName, $definitions );
399
                              break;
400
            default        : $thisColumn = new DBColumn( $columnName, $definitions );
401
         } // switch
402
         $this->columns[] = $thisColumn;
403
      } // foreach
404
   } // function arrayToObject
405
 
406
}
407
 
408
 
409
class DBDatabase {
410
   protected $databaseName;
411
   protected $tables;
412
   protected $displayOptions;
413
 
414
   public function __construct( $databaseName, $tableDefinitionArray = '',  $displayOptions = '' ) {
415
      $this->databaseName = $databaseName;
416
      $this->displayOptions = $displayOptions;
417
      foreach ( $tableDefinitionArray as $tableName => $definition ) {
418
         $this->tables[] = new DBTable($tableName, $definition['display name'], $definition['display columns'], $definition['display query'], $definition['field info'] );
419
      }
420
   } // function __construct
421
} // class DBDatabase
422
 
423
?>
424
 
425