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;
5
      public static $myName;
6
      protected static $viewName = 'view_device_location_owner_type';
7
 
55 rodolico 8
      /**
9
       * Gets stats for an extended class.
10
       * 
11
       * Gets number of active and inactive rows for a class, using the
12
       * view ($viewName)
13
       * 
14
       * @return int[] array of integers which is a count
15
       */
53 rodolico 16
      public static function getStats () {
17
         global $dbConnection;
57 rodolico 18
         global $activeOnly;
53 rodolico 19
 
20
         $return = array();
21
         $restrictions = array();
56 rodolico 22
         $trueClass = get_called_class();
57 rodolico 23
         $saveActive = $activeOnly;
24
         $activeOnly = true;
54 rodolico 25
 
26
         $active = sprintf( 
57 rodolico 27
            'select count(distinct %s) from %s %s', 
54 rodolico 28
            static::$dbStructure['table']['primaryKey'],  
29
            self::$viewName, 
57 rodolico 30
            self::makeWhereClause() 
31
         );
32
 
33
         $activeOnly = false;
54 rodolico 34
         $inactive = sprintf( 
57 rodolico 35
            'select count(distinct %s) from %s  %s', 
54 rodolico 36
            static::$dbStructure['table']['primaryKey'],  
37
            self::$viewName, 
57 rodolico 38
            self::makeWhereClause( array( strtolower( $trueClass ) . "_removed is not null" ) )
39
         );
40
         $activeOnly = $saveActive;
41
 
42
         //print "<pre>Active\n$active</pre><pre>Inactive\n$inactive</pre>"; die;
43
 
54 rodolico 44
         $return['active'] = $dbConnection->getOneDBValue( $active );
45
         $return['inactive'] = $dbConnection->getOneDBValue( $inactive );
53 rodolico 46
         return $return;
47
      }
48
 
57 rodolico 49
      protected static function makeWhereClause( $restrictions = array() ) {
50
         global $activeOnly;
51
 
52
         //print "<pre>" . print_r( $restrictions, true ) . "</pre>";
53
 
54
         $trueClass = get_called_class();
55
         foreach ( $_SESSION['restrictions'] as $class => $restriction ) {
56
            $restrictions[] = $_SESSION['restrictions'][$class];
57
         }
58
         if ( $activeOnly ) {
59
            $restrictions[] = strtolower( $trueClass ) . '_removed is null';
60
         }
61
         return count( $restrictions ) ? 'where ' . implode( ' and ', $restrictions ) : '';
62
      } //
63
 
56 rodolico 64
      public static function showSelectionList( $filter = array(), $list = array() ) {
53 rodolico 65
         global $url;
56 rodolico 66
         if ( empty( $list ) ) {
67
            $list = self::getAll( $filter );
68
         }
69
         $module = get_called_class();
53 rodolico 70
         foreach ( $list as $id => $name ) {
56 rodolico 71
            $return[] = "<a href='$url?module=$module&id=$id'>$name</a>";
53 rodolico 72
         }
73
         return '<ul><li>' . implode( '</li><li>', $return ) . '</li></ul>';
74
      }
75
 
56 rodolico 76
      public static function getAll( $filter = array() ) {
53 rodolico 77
         global $activeOnly;
78
         global $dbConnection;
79
 
56 rodolico 80
 
81
         if ( isset( $_REQUEST['to_find'] ) ) {
82
            $filter[] = sprintf( " %s like '%%%s%%'", static::$dbStructure['view']['selectionDisplay'],  $_REQUEST['to_find']) ;
83
         }
84
 
57 rodolico 85
 
53 rodolico 86
         $return = array();
57 rodolico 87
         $query = sprintf( 'select distinct %s id, %s name from %s %s', 
56 rodolico 88
               static::$dbStructure['view']['primaryKey'],
89
               static::$dbStructure['view']['selectionDisplay'],
90
               static::$dbStructure['view']['viewName'],
57 rodolico 91
               self::makeWhereClause( $filter )
53 rodolico 92
               );
56 rodolico 93
         $query .= " order by " . static::$dbStructure['view']['selectionDisplay'];
94
         //print "<pre>$query</pre>\n"; die;
53 rodolico 95
         $result = $dbConnection->doSQL( $query );
96
         foreach ( $result['returnData'] as $row ) {
97
            $return[$row['id']] = $row['name'];
98
         }
99
         return $return;
100
      }
101
 
102
      public function __toString() {
103
         return $this->name;
104
      }
105
 
56 rodolico 106
      public function __construct( $id ) {
107
         global $dbConnection;
108
 
109
         $this->data = array();
110
         $fields = array();
111
         foreach ( static::$dbStructure['table']['fields'] as $key => $data ) {
112
            $fields[] = $data['fieldname'];
113
         }
114
         $query = sprintf( "select %s from %s where %s = %s",
115
            implode( ',', $fields ),
116
            static::$dbStructure['table']['tableName'],
117
            static::$dbStructure['table']['primaryKey'],
118
            $id
119
         );
120
         //print "<pre> Constructor using query\n$query</pre>";
121
         //print "<pre>Constructor finding dbstructure of\n" . print_r( static::$dbStructure, true) . '</pre>';
122
         $this->data = $dbConnection->getOneRow( $query );
123
      }
124
 
125
      public function display() {
126
         $return = array();
127
         foreach ( static::$dbStructure['table']['fields'] as $key => $record ) {
128
            $return[] = '<td>' . $record['displayName'] . '</td><td>' . $this->data[$record['fieldname']] . '</td>';
129
         }
130
         return "<table border='1'><tr>" . implode("</tr><tr>", $return ) . "</tr></table>";
131
      }
132
 
133
      public function run() {
134
         return $this->display();
135
      }
53 rodolico 136
   } // abstract class Camp
137
 
138
?>