Subversion Repositories computer_asset_manager_v2

Rev

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

Rev Author Line No. Line
53 rodolico 1
<?php
2
 
3
   class Camp {
4
      protected static $dbStructure;
61 rodolico 5
      //public static $myName;
6
      //protected static $viewName = 'view_device_location_owner_type';
7
 
8
      /**
9
       * Builds an instance and loads data from database
10
       * 
11
       * #parameter int $id The id of the record to load
12
       */
63 rodolico 13
      public function __construct( $id = 0 ) {
14
         if ( $id )
15
            $this->loadFromDB( $id );
16
      } // __construct
17
 
18
      public function loadFromDB( $id ) {
59 rodolico 19
         global $dbConnection;
20
 
21
         $this->data = array();
22
         $fields = array();
23
         foreach ( static::$dbStructure['table']['fields'] as $key => $data ) {
24
            if ( $data['type'] != 'calculated' ) { // we have some which are calculated
25
               $fields[] = $data['fieldname'];
26
            }
27
         }
28
         $query = sprintf( "select %s from %s where %s = %s",
29
            implode( ',', $fields ),
30
            static::$dbStructure['table']['tableName'],
31
            static::$dbStructure['table']['primaryKey'],
32
            $id
33
         );
34
         //print "<pre> Constructor using query\n$query</pre>";
35
         //print "<pre>Constructor finding dbstructure of\n" . print_r( static::$dbStructure, true) . '</pre>';
61 rodolico 36
         if ( ( $this->data = $dbConnection->getOneRow( $query ) ) === false )
37
            print DBQuery::error2String();
59 rodolico 38
 
39
         // figure out which fields have calculated set so we can
40
         // do them next
41
         $calculatedFields = array();
42
         foreach ( static::$dbStructure['table']['fields'] as $field => $record ) {
43
            if ( $record['type'] == 'calculated' || $record['type'] == 'link') {
44
               $calculatedFields[] = $field;
45
            }
46
         }
47
 
48
         //print "<pre>" . print_r( $calculatedFields, true ) . "</pre>"; die;
49
         //print "<pre>" . print_r( $this->data, true ) . "</pre>"; die;
61 rodolico 50
         if ( count( $calculatedFields ) ) {
51
            $query = sprintf( 'select distinct %s from %s %s',
52
                     implode( ',', $calculatedFields ),
53
                     static::$dbStructure['view']['viewName'],
54
                     static::makeWhereClause( array( 
55
                        static::$dbStructure['table']['fields']['id']['fieldname'] . 
56
                        ' = ' . 
57
                        $this->data[static::$dbStructure['table']['primaryKey']] ) 
58
                        )
59
                     );
60
            //print "<pre>$query</pre>"; die;
61
            $result = $dbConnection->getOneRow( $query );
62
            foreach ( $calculatedFields as $field ) {
63
               $this->data[$field] = empty( $result[$field] ) ? '' : $result[$field];
64
            } // foreach
65
         }
63 rodolico 66
      } // loadFromDB
59 rodolico 67
 
61 rodolico 68
      /**
69
       * gets the contents of $fieldname
70
       * 
71
       * @parameter string $fieldname name of the database field to retrieve
72
       * 
73
       * @return string The value of $fieldname stored in structure
74
       */
59 rodolico 75
      public function __get( $fieldname ) {
76
         return 
77
            isset( $this->data[static::$dbStructure['table']['fields'][$fieldname]['fieldname']] ) ?
78
               $this->data[static::$dbStructure['table']['fields'][$fieldname]['fieldname']] :
79
               null;
80
      } // __get
81
 
82
 
55 rodolico 83
      /**
84
       * Gets stats for an extended class.
85
       * 
86
       * Gets number of active and inactive rows for a class, using the
87
       * view ($viewName)
88
       * 
89
       * @return int[] array of integers which is a count
90
       */
53 rodolico 91
      public static function getStats () {
92
         global $dbConnection;
57 rodolico 93
         global $activeOnly;
53 rodolico 94
 
95
         $return = array();
96
         $restrictions = array();
56 rodolico 97
         $trueClass = get_called_class();
57 rodolico 98
         $saveActive = $activeOnly;
99
         $activeOnly = true;
54 rodolico 100
 
101
         $active = sprintf( 
57 rodolico 102
            'select count(distinct %s) from %s %s', 
54 rodolico 103
            static::$dbStructure['table']['primaryKey'],  
61 rodolico 104
            static::$dbStructure['view']['viewName'], 
57 rodolico 105
            self::makeWhereClause() 
106
         );
107
 
108
         $activeOnly = false;
54 rodolico 109
         $inactive = sprintf( 
57 rodolico 110
            'select count(distinct %s) from %s  %s', 
54 rodolico 111
            static::$dbStructure['table']['primaryKey'],  
61 rodolico 112
            static::$dbStructure['view']['viewName'], 
57 rodolico 113
            self::makeWhereClause( array( strtolower( $trueClass ) . "_removed is not null" ) )
114
         );
115
         $activeOnly = $saveActive;
116
 
117
         //print "<pre>Active\n$active</pre><pre>Inactive\n$inactive</pre>"; die;
118
 
54 rodolico 119
         $return['active'] = $dbConnection->getOneDBValue( $active );
120
         $return['inactive'] = $dbConnection->getOneDBValue( $inactive );
53 rodolico 121
         return $return;
122
      }
123
 
61 rodolico 124
      /**
125
       * Creates a where clause
126
       * 
127
       * Merges $restrictions with any restrictions which may be in place
128
       * for the current user. Also sets 'class_removed is null' if
129
       * $activeOnly (global) is set
130
       * 
131
       * @parameter string[] $restrictions array of conditionals
132
       * 
133
       * @return string all restricions ANDED together
134
       */
57 rodolico 135
      protected static function makeWhereClause( $restrictions = array() ) {
136
         global $activeOnly;
137
 
138
         //print "<pre>" . print_r( $restrictions, true ) . "</pre>";
139
 
140
         $trueClass = get_called_class();
141
         foreach ( $_SESSION['restrictions'] as $class => $restriction ) {
142
            $restrictions[] = $_SESSION['restrictions'][$class];
143
         }
144
         if ( $activeOnly ) {
145
            $restrictions[] = strtolower( $trueClass ) . '_removed is null';
146
         }
147
         return count( $restrictions ) ? 'where ' . implode( ' and ', $restrictions ) : '';
148
      } //
149
 
61 rodolico 150
      /**
151
       * Creates a list of all records for a particular class and returns
152
       * a UL with all LI's set as links (A's) to displaying a record.
153
       * 
154
       * @parameter string[] $filter array of conditionals to limit if $list not set
155
       * @parameter string[] $list An optional list of data to be displayed
156
       * 
157
       * @return string A UL with each LI containing a link to an entry
158
       */
56 rodolico 159
      public static function showSelectionList( $filter = array(), $list = array() ) {
53 rodolico 160
         global $url;
59 rodolico 161
         $return = array();
56 rodolico 162
         if ( empty( $list ) ) {
163
            $list = self::getAll( $filter );
164
         }
165
         $module = get_called_class();
53 rodolico 166
         foreach ( $list as $id => $name ) {
56 rodolico 167
            $return[] = "<a href='$url?module=$module&id=$id'>$name</a>";
53 rodolico 168
         }
59 rodolico 169
         return "<ul>\n<li>" . implode( "</li>\n<li>", $return ) . "</li>\n</ul>";
53 rodolico 170
      }
171
 
61 rodolico 172
      /**
173
       * Gets all matching rows
174
       * 
175
       * Does an SQL call to retrieve ID and Name for all rows matching
176
       * $filter and $_REQUEST['to_find'].
177
       * 
178
       * @parameter string[] $filter Optional list of conditionals for where clause
179
       * 
180
       * @return array[] An array of indexes and names matching query
181
       */
56 rodolico 182
      public static function getAll( $filter = array() ) {
53 rodolico 183
         global $activeOnly;
184
         global $dbConnection;
185
 
59 rodolico 186
         //print "<pre>" . print_r( $filter, true ) . "</pre>";
56 rodolico 187
         if ( isset( $_REQUEST['to_find'] ) ) {
188
            $filter[] = sprintf( " %s like '%%%s%%'", static::$dbStructure['view']['selectionDisplay'],  $_REQUEST['to_find']) ;
189
         }
190
 
57 rodolico 191
 
53 rodolico 192
         $return = array();
57 rodolico 193
         $query = sprintf( 'select distinct %s id, %s name from %s %s', 
56 rodolico 194
               static::$dbStructure['view']['primaryKey'],
195
               static::$dbStructure['view']['selectionDisplay'],
196
               static::$dbStructure['view']['viewName'],
57 rodolico 197
               self::makeWhereClause( $filter )
53 rodolico 198
               );
56 rodolico 199
         $query .= " order by " . static::$dbStructure['view']['selectionDisplay'];
59 rodolico 200
         //print "<pre>$query</pre>\n";
53 rodolico 201
         $result = $dbConnection->doSQL( $query );
202
         foreach ( $result['returnData'] as $row ) {
203
            $return[$row['id']] = $row['name'];
204
         }
205
         return $return;
206
      }
61 rodolico 207
 
208
      /**
209
       * Returns the "name" of an instance
210
       * 
211
       * Normally, this is $this->name, but some classes may want to put
212
       * in additional data
213
       * 
214
       * @return string Display name of this instance
215
       */
53 rodolico 216
      public function __toString() {
217
         return $this->name;
218
      }
219
 
61 rodolico 220
      /**
221
       * Returns an HTML structure which can display a record
222
       * 
223
       * Note that if the instance has children, they will be displayed 
224
       * also. Additionally, if it has fields marked as type 'calculated'
225
       * it will perform the limited calculations.
226
       * 
227
       * @return string HTML to display record
228
       */
56 rodolico 229
      public function display() {
59 rodolico 230
         global $url;
56 rodolico 231
         $return = array();
59 rodolico 232
         $save = '';
233
 
234
         /* get the records for this, including from other tables */
56 rodolico 235
         foreach ( static::$dbStructure['table']['fields'] as $key => $record ) {
59 rodolico 236
            if ( isset( $record['display'] ) && $record['display'] === false ) // we don't try to show links
237
               continue;
238
            if ( isset( $record['link'] ) ) { // this one is supposed to be set up as a link
239
                  $return[] = sprintf( 
240
                     "<td>%s</td><td><a href='$url?module=%s&id=%s'>%s</a></td>",
241
                     $record['displayName'],
242
                     static::$dbStructure['table']['fields'][$record['link']]['class'],
243
                     $this->data[static::$dbStructure['table']['fields'][$record['link']]['fieldname']],
244
                     $this->data[$record['fieldname']]
245
                  );
246
            } else { // just pulling data from the table itself
247
               $return[] = '<td>' . $record['displayName'] . "</td>\n<td>" . $this->data[$record['fieldname']] . '</td>';
248
            }
56 rodolico 249
         }
61 rodolico 250
         $return[] = sprintf( "<td colspan='2'><a href='$url?module=%s&id=%s&action=edit'>Edit</a></td>",
251
                     get_called_class(),
252
                     $this->data[static::$dbStructure['table']['primaryKey']]
253
                  );
254
 
59 rodolico 255
         $return = "<div class='show_record'>\n<table class='show_record'>\n<tr>" . implode("</tr>\n<tr>", $return ) . "</tr>\n</table>\n</div>";
256
 
257
         /* Now, get the children records */
258
         if ( isset( $_REQUEST['to_find'] ) ) {
259
            $save = $_REQUEST['to_find'];
260
            unset( $_REQUEST['to_find'] );
261
         }
262
         foreach ( static::$dbStructure['children'] as $class ) {
263
            $return .= "<div class='show_record'>\n<h3>$class</h3>\n" . $class::showSelectionList( array( static::$dbStructure['table']['primaryKey'] . ' = ' . $this->data[static::$dbStructure['table']['primaryKey']] )  ) . '</div>';
264
         }
265
         if ( $save !== null ) {
266
            $_REQUEST['to_find'] = $save;
267
         }
268
         return $return;
56 rodolico 269
      }
270
 
61 rodolico 271
      /**
63 rodolico 272
       * Function is basically a placeholder in case a extended class
273
       * needs to do some additional processing when editing
274
       * 
275
       * In some cases, the edit function can not handle additional fields.
276
       * See the Device class, where device_type is a table with many-to-many
277
       * linkages to device. Since this type of thing is very rare, I decided
278
       * to just allow edit and post to call an additional function in
279
       * those cases.
280
       * 
281
       * Based on the action requested, will either return an HTML string
282
       * which will be placed in the edit screen (for edit), or an array
283
       * of SQL to be executed to update/add rows (for post).
284
       * 
285
       * If the action is edit, there should be a <td></td> around the 
286
       * whole thing
287
       */
288
 
289
      protected function editAdditionalFields() {
290
         if ( $_REQUEST['action'] == 'edit' ) {
291
            return '';
292
         } elseif ($_REQUEST['action'] == 'post' ) {
293
            return array();
294
         }
295
      }
296
 
297
      protected function edit() {
298
         global $dbConnection;
299
 
300
 
301
         // save everything we have right now
302
         $this->beforeEdit = $this->data;
303
         foreach ( static::$dbStructure['table']['fields'] as $key => $record ) {
304
            if ( isset( $record['display'] ) && $record['display'] === false ) // we don't things which aren't supposed to be shown
305
               continue;
306
            if ( isset( $record['link'] ) ) { // this one is supposed to be set up as a link
307
 
308
                  // get a list of all options
309
                  $list = static::$dbStructure['table']['fields'][$record['link']]['class']::getAll();
310
 
311
                  //print '<pre>' . print_r( $record, true ) . "\n----\n" . print_r( $this->data, true ) . '</pre>'; die;
312
                  $selectedRow = $this->data[static::$dbStructure['table']['fields'][$record['link']]['fieldname']];
313
                  $select = $dbConnection->htmlSelect( array (
314
                              'data'      => $list,
315
                              'name'      => $record['fieldname'],
316
                              'nullok'    => true,
317
                              'selected'  => isset( $selectedRow ) ? $selectedRow : -1,
318
                              //'class'     => 
319
                           )
320
                  );
321
                  $return[] = sprintf( 
322
                     "<td>%s</td><td>%s</td>",
323
                     $record['displayName'],
324
                     $select
325
                  );
326
            } else { // just pulling data from the table itself
327
               if ( isset( $record['canEdit'] ) && $record['canEdit'] == false ) {
328
                  $return[] = sprintf( 
329
                           "<td>%s</td><td>%s</td>",
330
                           $record['displayName'],
331
                           $this->data[$record['fieldname']]
332
                        );
333
               } else {
334
                  $return[] = sprintf( 
335
                           "<td>%s</td><td><input type='text' name='%s' value='%s'></td>",
336
                           $record['displayName'],
337
                           $record['fieldname'],
338
                           $this->data[$record['fieldname']]
339
                           );
340
               }
341
            }
342
         }
343
         $return[] = $this->editAdditionalFields();
344
         $return[] = "<td colspan='2'><input type='submit' name='Save' value='Save'></td>";
345
         $hiddens = sprintf( "<input type='hidden' name='module' value='%s'>\n", get_called_class() ) .
346
                     sprintf( "<input type='hidden' name='id' value='%s'>\n", $this->data[static::$dbStructure['table']['primaryKey']] ) .
347
                     "<input type='hidden' name='action' value='post'>\n";
348
         return "<div class='show_record'>\n<form><table class='show_record'>\n<tr>" . implode("</tr>\n<tr>", $return ) . "</tr>\n</table>\n$hiddens</form></div>";
349
      } // edit
350
 
351
      protected function post() {
352
         global $dbConnection;
353
 
354
         $queries = array();
355
         $fields = array();
356
         foreach ( static::$dbStructure['table']['fields'] as $key => $record ) {
357
            if ( isset( $record['display'] ) && $record['display'] === false ) // we don't try to show links
358
               continue;
359
            if ( isset( $record['canEdit'] ) && ! $record['canEdit'] )
360
               continue;
361
            if ( isset( $record['link'] ) ) { // this one is supposed to be set up as a link
362
            } elseif ( $_REQUEST[$record['fieldname']] != $this->data[$record['fieldname']] ) { 
363
               // just a regular field, and it has changed, so add it to fields
364
               $fields[$record['fieldname']] = $_REQUEST[$record['fieldname']];
365
            }
366
         } // foreach
367
         //print "<pre>Fields\n" . print_r( $this->data, true ) . "</pre>"; die;
368
         if ( count( $fields ) ) {
369
            if ( isset( $this->data[static::$dbStructure['table']['primaryKey']] ) ) { // this is an update
370
               $queries[] = $dbConnection->updateQuery( 
371
                     static::$dbStructure['table']['tableName'],
372
                     array( static::$dbStructure['table']['primaryKey'] => $this->data[static::$dbStructure['table']['primaryKey']] ),
373
                     $fields );
374
            } else { // this is an insert
375
               $queries[] = $dbConnection->insertQuery(
376
                     static::$dbStructure['table']['tableName'],
377
                     $fields );
378
            }
379
         } // if there are fields
380
         //print "<pre>Queries" . print_r( $queries, true ) . "</pre>"; die;
381
         $dbConnection->runSQLScript( $queries );
382
         $this->loadFromDB( $this->data[static::$dbStructure['table']['primaryKey']] );
383
      } // post
384
 
385
      /**
61 rodolico 386
       * Simple controller. Will determine what action is going on, then
387
       * call the correct method, returning the HTML required
388
       */
56 rodolico 389
      public function run() {
63 rodolico 390
         if ( isset( $_REQUEST['action'] ) ) {
391
            switch ( $_REQUEST['action'] ) {
392
               case 'edit' :  $return = $this->edit();
393
                              break;
394
               case 'post' :  $this->post();
395
               default:       $return =  $this->display();
396
            } // switch
397
         } else {
398
            $return =  $this->display();
399
         }
400
         return $return;
56 rodolico 401
      }
53 rodolico 402
   } // abstract class Camp
403
 
404
?>