Subversion Repositories computer_asset_manager_v2

Rev

Rev 70 | 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' );
   include_once( 'admin.class.php' );
   include_once( 'devicetype.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 file $configFileName 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 ) {
      global $url;

      $result = "<div class='stats'>\n\t<h3><a href='$url?module=$className'>$className</a></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='action' value='search'>\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;
      
      /*
       * base classes are classes that we will run through the main processing
       * loop. Assumes they are all derived from camp.class.php (class Camp)
       * so they all work the same way. Everything else is processed differently
       */
      
      $baseClasses = array( 'Owner' => 1, 'Location' => 1, 'Device' => 1, 'DeviceType' => 1 );
      
      $return = '';
      
      if ( isset( $_REQUEST['doAdmin'] ) ) { // user administration
         $return = $_SESSION['user']->admin($connection) .  $_SESSION['user']->errors();
         $_SESSION['user']->clearErrors();
         return $return;
      } // user administration
         
      // standard flow
      $class = isset( $_REQUEST['module'] ) ? $_REQUEST['module'] : '';
      $thisKey = isset( $_REQUEST['id'] ) ? $_REQUEST['id'] : '' ;
      $selection = array();
      $action = 
         isset( $_REQUEST['action'] ) ?
            $_REQUEST['action'] : 'display';
            // removed the following, but may break things so leaving original
            //( $thisKey && $class ? 'display' : '' );

      // we have stale data in $_SESSION, so unset it
      if ( $class && $thisKey && isset( $_SESSION['data'][$class] ) && $_SESSION['data'][$class]->id != $thisKey ) {
         unset( $_SESSION['data'][$class] );
      }
      // we have something we need to work on, but it is not loaded, so load it
      if ( $thisKey && empty( $_SESSION['data'][$class] ) ) {
         $_SESSION['data'][$class] = new $class( $thisKey );
      }
      /*
      print "<pre>REQUEST\n" . 
         print_r( $_REQUEST, true ) . 
         "\nclass=$class\nthisKey=$thisKey\naction=$action\n" .
         print_r( $_SESSION['data'], true ) .
         "</pre>";
      die;
      */
      
      // 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 ) {
         if ( $action && isset( $baseClasses[$class] ) ) {
            switch ( $action ) {
               case 'add': // for an add, we need a new, empty instance
                  $_SESSION['data'][$class] = new $class();
               case 'edit':
               case 'post':
                  return $_SESSION['data'][$class]->run();
                  break;
               case 'search':
                  // display a list of all entries (filtered)
                  $selection = $class::getAll();
                  //print "<pre>selection\n" . print_r( $selection, true ) . "</pre>" ; die;
                  if ( count($selection) == 1 ) { // we have a single entry
                     $thisKey = key( $selection ); // so set key so we will display
                     $_SESSION['data'][$class] = new $class( $thisKey );
                  } elseif ( count( $selection ) == 0 ) {
                     $return = '<h3>No Records Found</h3>';
                  } else {
                     $return = $class::showSelectionList( array(), $selection );
                  }
               default:
                  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
                     if ( class_exists( $class ) ) {
                        $return = $class::showSelectionList( array(), $selection );
                     } else {
                        $return = "<p>We don't know how to do <b>" . $_REQUEST['module'] . "</b> yet</p>";
                     } // if class_exists
                  } // if a key exists
            } // switch
         } else { // no action, or not in baseClasses
            if ( empty ( $_SESSION['data'][$class] ) ) {
               $_SESSION['data'][$class] = new $class();
            }
            $return = $_SESSION['data'][$class]->run();
         } // if action and baseclass
      } else {
         // this is the opening screen, so clear out all the data and
         // working on
         unset( $_SESSION['data'] );
         unset( $_SESSION['workingon'] );
         $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
      } // if..else
      return $return;
   } // html
   
   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
      //print "<pre>temp in restrictions\n" . print_r( $temp, true ) . '</pre>'; die;
      if ( isset( $temp['owner'] ) ) {
         $values = implode( ',', $dbConnection->columnToArray( sprintf( "select owner_id id from owner where name in (%s)", implode( ',', $temp['owner'] ) ) ) );
         //$values = implode( ',', $values );
         //print "<pre>Restrictions on owner\n" . print_r( $values, true ) . '</pre>'; die;
         $_SESSION['restrictions']['Owner'][] = sprintf( 'owner_id in ( %s )', $values );
         //$_SESSION['restrictions']['Location'][] = sprintf( 'location_id in ( select location_id from owner_location where removed is null and owner_id in ( %s ) )', $values );
         $_SESSION['restrictions']['Location'][] = sprintf( '( location_id in ( select location_id from owner_location where removed is null and owner_id in ( %s ) ) or location_id in ( select location_id from location_device where device_id in ( select device_id from owner_device where owner_id in (%s) ) ) )', $values, $values );
         $_SESSION['restrictions']['Device'][] = sprintf( 'device_id in ( select device_id from owner_device where removed is null and owner_id in ( %s ) )', $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'] ) ) );
         $values = implode( ',', $values );
         $_SESSION['restrictions']['Location'][] = sprintf( 'location_id in ( %s )', $values );
         $_SESSION['restrictions']['Device'][] = sprintf( 'device_id in ( select device_id from location_device where removed is null and location_id in (%s) )', $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'] ) ) );
         $values = implode( ',', $values );
         $_SESSION['restrictions']['Device'][] = sprintf( 'device_id in ( %s )', $values );
      }
      //print "<pre>Restrictions\n" . print_r( $_SESSION['restrictions'], true ) . '</pre>'; die;
   } // buildRestrictions
         
   
   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;
   }
      
      
      
   
?>