Subversion Repositories computer_asset_manager_v2

Rev

Rev 56 | Rev 59 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

<?php

   class Camp {
      protected static $dbStructure;
      public static $myName;
      protected static $viewName = 'view_device_location_owner_type';
            
      /**
       * Gets stats for an extended class.
       * 
       * Gets number of active and inactive rows for a class, using the
       * view ($viewName)
       * 
       * @return int[] array of integers which is a count
       */
      public static function getStats () {
         global $dbConnection;
         global $activeOnly;
         
         $return = array();
         $restrictions = array();
         $trueClass = get_called_class();
         $saveActive = $activeOnly;
         $activeOnly = true;
         
         $active = sprintf( 
            'select count(distinct %s) from %s %s', 
            static::$dbStructure['table']['primaryKey'],  
            self::$viewName, 
            self::makeWhereClause() 
         );
         
         $activeOnly = false;
         $inactive = sprintf( 
            'select count(distinct %s) from %s  %s', 
            static::$dbStructure['table']['primaryKey'],  
            self::$viewName, 
            self::makeWhereClause( array( strtolower( $trueClass ) . "_removed is not null" ) )
         );
         $activeOnly = $saveActive;
         
         //print "<pre>Active\n$active</pre><pre>Inactive\n$inactive</pre>"; die;
         
         $return['active'] = $dbConnection->getOneDBValue( $active );
         $return['inactive'] = $dbConnection->getOneDBValue( $inactive );
         return $return;
      }
      
      protected static function makeWhereClause( $restrictions = array() ) {
         global $activeOnly;
         
         //print "<pre>" . print_r( $restrictions, true ) . "</pre>";

         $trueClass = get_called_class();
         foreach ( $_SESSION['restrictions'] as $class => $restriction ) {
            $restrictions[] = $_SESSION['restrictions'][$class];
         }
         if ( $activeOnly ) {
            $restrictions[] = strtolower( $trueClass ) . '_removed is null';
         }
         return count( $restrictions ) ? 'where ' . implode( ' and ', $restrictions ) : '';
      } //
      
      public static function showSelectionList( $filter = array(), $list = array() ) {
         global $url;
         if ( empty( $list ) ) {
            $list = self::getAll( $filter );
         }
         $module = get_called_class();
         foreach ( $list as $id => $name ) {
            $return[] = "<a href='$url?module=$module&id=$id'>$name</a>";
         }
         return '<ul><li>' . implode( '</li><li>', $return ) . '</li></ul>';
      }
      
      public static function getAll( $filter = array() ) {
         global $activeOnly;
         global $dbConnection;
         
         
         if ( isset( $_REQUEST['to_find'] ) ) {
            $filter[] = sprintf( " %s like '%%%s%%'", static::$dbStructure['view']['selectionDisplay'],  $_REQUEST['to_find']) ;
         }
         
         
         $return = array();
         $query = sprintf( 'select distinct %s id, %s name from %s %s', 
               static::$dbStructure['view']['primaryKey'],
               static::$dbStructure['view']['selectionDisplay'],
               static::$dbStructure['view']['viewName'],
               self::makeWhereClause( $filter )
               );
         $query .= " order by " . static::$dbStructure['view']['selectionDisplay'];
         //print "<pre>$query</pre>\n"; die;
         $result = $dbConnection->doSQL( $query );
         foreach ( $result['returnData'] as $row ) {
            $return[$row['id']] = $row['name'];
         }
         return $return;
      }
         
      public function __toString() {
         return $this->name;
      }
      
      public function __construct( $id ) {
         global $dbConnection;

         $this->data = array();
         $fields = array();
         foreach ( static::$dbStructure['table']['fields'] as $key => $data ) {
            $fields[] = $data['fieldname'];
         }
         $query = sprintf( "select %s from %s where %s = %s",
            implode( ',', $fields ),
            static::$dbStructure['table']['tableName'],
            static::$dbStructure['table']['primaryKey'],
            $id
         );
         //print "<pre> Constructor using query\n$query</pre>";
         //print "<pre>Constructor finding dbstructure of\n" . print_r( static::$dbStructure, true) . '</pre>';
         $this->data = $dbConnection->getOneRow( $query );
      }
      
      public function display() {
         $return = array();
         foreach ( static::$dbStructure['table']['fields'] as $key => $record ) {
            $return[] = '<td>' . $record['displayName'] . '</td><td>' . $this->data[$record['fieldname']] . '</td>';
         }
         return "<table border='1'><tr>" . implode("</tr><tr>", $return ) . "</tr></table>";
      }
      
      public function run() {
         return $this->display();
      }
   } // abstract class Camp

?>