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;
18
 
19
         $return = array();
20
         $restrictions = array();
54 rodolico 21
         // get_called_class is the only function that will work with a static
22
         // call with descendants.
56 rodolico 23
         $trueClass = get_called_class();
54 rodolico 24
 
53 rodolico 25
         foreach ( $_SESSION['restrictions'] as $class => $restriction ) {
26
            $restrictions[] = $_SESSION['restrictions'][$class];
27
         }
28
         $whereClause = count($restrictions) ? ' and ' . implode( ' and ', $restrictions) : '';
29
 
54 rodolico 30
         $active = sprintf( 
31
            'select count(distinct %s) from %s where %s_removed is null %s', 
32
            static::$dbStructure['table']['primaryKey'],  
33
            self::$viewName, 
34
            $trueClass, 
35
            $whereClause );
36
         $inactive = sprintf( 
37
            'select count(distinct %s) from %s where %s_removed is not null %s', 
38
            static::$dbStructure['table']['primaryKey'],  
39
            self::$viewName, 
40
            $trueClass, 
41
            $whereClause );
42
         $return['active'] = $dbConnection->getOneDBValue( $active );
43
         $return['inactive'] = $dbConnection->getOneDBValue( $inactive );
53 rodolico 44
         return $return;
45
      }
46
 
56 rodolico 47
      public static function showSelectionList( $filter = array(), $list = array() ) {
53 rodolico 48
         global $url;
56 rodolico 49
         if ( empty( $list ) ) {
50
            $list = self::getAll( $filter );
51
         }
52
         $module = get_called_class();
53 rodolico 53
         foreach ( $list as $id => $name ) {
56 rodolico 54
            $return[] = "<a href='$url?module=$module&id=$id'>$name</a>";
53 rodolico 55
         }
56
         return '<ul><li>' . implode( '</li><li>', $return ) . '</li></ul>';
57
      }
58
 
56 rodolico 59
      public static function getAll( $filter = array() ) {
53 rodolico 60
         global $activeOnly;
61
         global $dbConnection;
62
 
56 rodolico 63
 
64
         if ( isset( $_REQUEST['to_find'] ) ) {
65
            $filter[] = sprintf( " %s like '%%%s%%'", static::$dbStructure['view']['selectionDisplay'],  $_REQUEST['to_find']) ;
66
         }
67
 
68
         //if ( $activeOnly )
69
            $filter[] = sprintf( '%s is null', static::$dbStructure['view']['fields']['removed']['fieldname'] );
70
 
53 rodolico 71
         $return = array();
56 rodolico 72
         $query = sprintf( 'select distinct %s id, %s name from %s', 
73
               static::$dbStructure['view']['primaryKey'],
74
               static::$dbStructure['view']['selectionDisplay'],
75
               static::$dbStructure['view']['viewName'],
53 rodolico 76
               );
56 rodolico 77
         if ( count($filter) )
78
            $query .= ' where ' . implode( ' and ', $filter );
79
         $query .= " order by " . static::$dbStructure['view']['selectionDisplay'];
80
         //print "<pre>$query</pre>\n"; die;
53 rodolico 81
         $result = $dbConnection->doSQL( $query );
82
         foreach ( $result['returnData'] as $row ) {
83
            $return[$row['id']] = $row['name'];
84
         }
85
         return $return;
86
      }
87
 
88
      public function __toString() {
89
         return $this->name;
90
      }
91
 
56 rodolico 92
      public function __construct( $id ) {
93
         global $dbConnection;
94
 
95
         $this->data = array();
96
         $fields = array();
97
         foreach ( static::$dbStructure['table']['fields'] as $key => $data ) {
98
            $fields[] = $data['fieldname'];
99
         }
100
         $query = sprintf( "select %s from %s where %s = %s",
101
            implode( ',', $fields ),
102
            static::$dbStructure['table']['tableName'],
103
            static::$dbStructure['table']['primaryKey'],
104
            $id
105
         );
106
         //print "<pre> Constructor using query\n$query</pre>";
107
         //print "<pre>Constructor finding dbstructure of\n" . print_r( static::$dbStructure, true) . '</pre>';
108
         $this->data = $dbConnection->getOneRow( $query );
109
      }
110
 
111
      public function display() {
112
         $return = array();
113
         foreach ( static::$dbStructure['table']['fields'] as $key => $record ) {
114
            $return[] = '<td>' . $record['displayName'] . '</td><td>' . $this->data[$record['fieldname']] . '</td>';
115
         }
116
         return "<table border='1'><tr>" . implode("</tr><tr>", $return ) . "</tr></table>";
117
      }
118
 
119
      public function run() {
120
         return $this->display();
121
      }
53 rodolico 122
   } // abstract class Camp
123
 
124
?>