Subversion Repositories computer_asset_manager_v2

Rev

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

<?php
   //include_once( 'DatabaseDefinition.php' );
   include_once( 'camp.class.php' );
   include_once( 'owner.class.php' );
   include_once( 'location.class.php' );
   include_once( 'device.class.php' );
   
   global $LOGFILE;
   $LOGFILE='/tmp/camp.log';
   define( 'VERSION', '2.0b' );
   define( 'BUILD_DATE', '20130527');
   
   function loadConfig() {
      // Search through directories looking for a config file
      $return = array(
         'error' => '',
         'path'  => '',
         'configuration' => array()
      );
      $configFileName = 'camp2_config.yaml';
      $searchDirectories = array( 
         // one level up from document root
         $_SERVER['DOCUMENT_ROOT'] . "/../$configFileName",
         // directory private one level up from document root
         $_SERVER['DOCUMENT_ROOT'] . "/../private/$configFileName",
         // the current directory
         "./$configFileName",
      );
      foreach ( $searchDirectories as $search ) {
         if ( file_exists( realpath( $search ) ) ) {
            $return['path'] = realpath( $search );
            $return['configuration'] = yaml_parse_file( $return['path'] );
            $return['error'] = '';
            return $return;
         } // if
      } // for
      $return['error'] = "No configuration found in<br />" . implode( '<br />', $searchDirectories );
      return $return;
   }
   
   function saveConfig( $filename, $configuration ) {
      return yaml_emit_file( $filename, $configuration );
   }
   
   function insertValuesIntoQuery( $query, $values ) {
      foreach ( $values as $name => $value ) {
         $query = search_replace_string($query, "<$name>", $value );
      }
      return $query;
   }

   function search_replace_string($string, $searchFor, $replaceWith ) {
      $string = str_replace ( $searchFor, $replaceWith, $string );
      return $string;
   }
   
   function processStats ( $className, $info ) {
      $result = "<div class='stats'>\n\t<h3>$className</h3>\n";
      foreach ( $info as $key => $value ) {
         $result .= "\t<p>$value $key</p>\n";
      }
      $result .= "<form><input type='text' size='10' name='to_find'>\n<input type='hidden' name='module' value='$className'>\n<input type='submit' name='search' value='Search'>\n</form>";
      $result .= "</div>\n";
      return $result;
   }
   
      
   
   /**
    * uses $_REQUEST to decide what to display
    * 
    * @param string[] $request The contents for $_REQUEST
    * 
    * @returns string HTML to be inserted into page
    */
   function displayHTML(  ) {
      global $connection;
      global $dbConnection;
      global $baseURL;
      
      $undefinedModules = array( 'report' => 1 );
      
      $return = '';
      $class = isset( $_REQUEST['module'] ) ? $_REQUEST['module'] : '';
      $thisKey = isset( $_REQUEST['id'] ) ? $_REQUEST['id'] : 0 ;
      $selection = array();
      
      //print "<pre>class=$class\nid=$id\n</pre>";
      
      // We have a class (module) calling and the class exists
      // if we don't have a single entry, we'll display a selection list
      // if we only have one entry, we'll display the screen for it
      if ( $class && class_exists( $class ) ) {
         if ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'add' ) {
            $_SESSION['data'][$class] = new $class();
            return $_SESSION['data'][$class]->run();
         } elseif ( ! isset( $_SESSION['data'][$class] ) && ! $thisKey ) { // nothing, so we're initializing
            // display a list of all entries (filtered)
            $selection = $class::getAll();
            if ( count($selection) == 1 ) { // we have a single entry
               $thisKey = key( $selection ); // so set key so we will display
            } elseif ( count( $selection ) == 0 ) {
               return '<h3>No Records Found</h3>';
            }
         } elseif( ! $thisKey || (isset( $_SESSION['data'][$class] ) &&  $thisKey != $_SESSION['data'][$class]->id ) ) {
            // The key does not exist, or does not match the data we have
            unset( $_SESSION['data'][$class] );
         }
         // we have a key, but we don't have a class instance yet
         if ( $thisKey && empty( $_SESSION['data'][$class] ) ) {
            $_SESSION['data'][$class] = new $class( $thisKey );
         }
         if ( $thisKey ) { // we are in the middle of doing something for the object
            $return = $_SESSION['data'][$class]->run();
         } else { // lets just show them a list of possible ojbects
            $return = $class::showSelectionList( array(), $selection );
         }
      } elseif ( $class ) { // Some module we don't have loaded
            switch ( $class ) {
               case 'admin' : require_once( 'administration.php' );
                              $return = doAdmin();
                              $return = print_r( $return, true );
                              break;
               default      : $return = "<p>We don't know how to do <b>" . $_REQUEST['module'] . "</b> yet</p>";
            } // switch
      } else {
         // this is the opening screen, so clear out all the data and
         // working on
         unset( $_SESSION['data'] );
         unset( $_SESSION['workingon'] );
         if ( isset( $_REQUEST['doAdmin'] ) ) {
            $return = $_SESSION['user']->admin($connection) .  $_SESSION['user']->errors();
            $_SESSION['user']->clearErrors();
         } else { // default to this if nothing else works
            $motd = $dbConnection->getOneDBValue( "select key_value from _system where group_name = 'program' and key_name = 'motd'" );
            $return = "<div class='motd'>$motd</div>";
            foreach ( array( 'Owner','Location','Device' ) as $class ) {
               $return .= processStats( $class, $class::getStats() );
            } // foreach
         } // else
      } // else
      return $return;
   }
   
   function buildRestrictions() {
      global $dbConnection;
      $rules = explode( "\n", $_SESSION['user']->restrictions );
      $_SESSION['restrictions'] = array();
      $temp = array();
      $workingOn = '';
      foreach ( $rules as $thisOne ) {
         //print "<pre>Working on $thisOne\n</pre>";
         if ( preg_match( '/\[([^\[\]]+)\]/', $thisOne, $match ) ) {
            //print "Adding as category\n<br />";
            $workingOn = $match[1];
         } else {
            //print "Adding as a value in $workingOn\n<br />";
            $temp[$workingOn][] = "'" . $dbConnection->real_escape_string(trim($thisOne)) . "'";
         }
      } // foreach
      if ( isset( $temp['owner'] ) ) {
         $values = $dbConnection->columnToArray( sprintf( "select distinct owner_id id from view_device_location_owner_type where owner in (%s)", implode( ',', $temp['owner'] ) ) );
         $_SESSION['restrictions']['Owner'] = sprintf( 'owner_id in ( %s )', implode( ',', $values ) );
      }
      if ( isset( $temp['location'] ) ) {
         $values = $dbConnection->columnToArray( sprintf( "select distinct location_id id from view_device_location_owner_type where location in (%s)", implode( ',', $temp['location'] ) ) );
         $_SESSION['restrictions']['Location'] = sprintf( 'location_id in ( %s )', implode( ',', $values ) );
      }
      if ( isset( $temp['device'] ) ) {
         $values = $dbConnection->columnToArray( sprintf( "select distinct device_id id from view_device_location_owner_type where device in (%s)", implode( ',', $temp['device'] ) ) );
         $_SESSION['restrictions']['Device'] = sprintf( 'device_id in ( %s )', implode( ',', $values ) );
      }
   }
         
   function makeHrefList( $data, $target, $template = "<a href='%s%s'>%s</a>", $before = '<table><tr><td>', $after = "</td></tr></table>", $inside = "\n</td></tr><tr><td>" ) {
      $return = array();
      foreach ( $data as $key => $value ) {
         $return[] = sprintf( $template, $target, $key, $value );
      }
      return $before . implode( $inside , $return ) . $after;
   }
      
      
      
   
?>