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