Subversion Repositories computer_asset_manager_v2

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 rodolico 1
<?php
56 rodolico 2
   //include_once( 'DatabaseDefinition.php' );
3
   include_once( 'camp.class.php' );
53 rodolico 4
   include_once( 'owner.class.php' );
5
   include_once( 'location.class.php' );
6
   include_once( 'device.class.php' );
7
 
1 rodolico 8
   global $LOGFILE;
9
   $LOGFILE='/tmp/camp.log';
10
   define( 'VERSION', '2.0b' );
11
   define( 'BUILD_DATE', '20130527');
12
 
45 rodolico 13
   function loadConfig() {
14
      // Search through directories looking for a config file
15
      $return = array(
16
         'error' => '',
17
         'path'  => '',
18
         'configuration' => array()
19
      );
20
      $configFileName = 'camp2_config.yaml';
21
      $searchDirectories = array( 
22
         // one level up from document root
23
         $_SERVER['DOCUMENT_ROOT'] . "/../$configFileName",
24
         // directory private one level up from document root
25
         $_SERVER['DOCUMENT_ROOT'] . "/../private/$configFileName",
26
         // the current directory
27
         "./$configFileName",
28
      );
29
      foreach ( $searchDirectories as $search ) {
30
         if ( file_exists( realpath( $search ) ) ) {
31
            $return['path'] = realpath( $search );
32
            $return['configuration'] = yaml_parse_file( $return['path'] );
33
            $return['error'] = '';
34
            return $return;
35
         } // if
36
      } // for
37
      $return['error'] = "No configuration found in<br />" . implode( '<br />', $searchDirectories );
38
      return $return;
39
   }
40
 
46 rodolico 41
   function saveConfig( $filename, $configuration ) {
42
      return yaml_emit_file( $filename, $configuration );
43
   }
44
 
1 rodolico 45
   function insertValuesIntoQuery( $query, $values ) {
46
      foreach ( $values as $name => $value ) {
47
         $query = search_replace_string($query, "<$name>", $value );
48
      }
49
      return $query;
50
   }
51
 
52
   function search_replace_string($string, $searchFor, $replaceWith ) {
53
      $string = str_replace ( $searchFor, $replaceWith, $string );
54
      return $string;
55
   }
45 rodolico 56
 
53 rodolico 57
   function processStats ( $className, $info ) {
58
      $result = "<div class='stats'>\n\t<h3>$className</h3>\n";
59
      foreach ( $info as $key => $value ) {
60
         $result .= "\t<p>$value $key</p>\n";
61
      }
56 rodolico 62
      $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>";
53 rodolico 63
      $result .= "</div>\n";
64
      return $result;
65
   }
55 rodolico 66
 
53 rodolico 67
 
68
 
45 rodolico 69
   /**
70
    * uses $_REQUEST to decide what to display
71
    * 
72
    * @param string[] $request The contents for $_REQUEST
73
    * 
74
    * @returns string HTML to be inserted into page
75
    */
58 rodolico 76
   function displayHTML(  ) {
45 rodolico 77
      global $connection;
55 rodolico 78
      global $dbConnection;
53 rodolico 79
      global $baseURL;
80
 
56 rodolico 81
      $undefinedModules = array( 'report' => 1 );
82
 
53 rodolico 83
      $return = '';
56 rodolico 84
      $class = isset( $_REQUEST['module'] ) ? $_REQUEST['module'] : '';
85
      $thisKey = isset( $_REQUEST['id'] ) ? $_REQUEST['id'] : 0 ;
86
      $selection = array();
87
 
88
      //print "<pre>class=$class\nid=$id\n</pre>";
89
 
90
      // We have a class (module) calling and the class exists
91
      // if we don't have a single entry, we'll display a selection list
92
      // if we only have one entry, we'll display the screen for it
93
      if ( $class && class_exists( $class ) ) {
66 rodolico 94
         if ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'add' ) {
95
            $_SESSION['data'][$class] = new $class();
96
            return $_SESSION['data'][$class]->run();
97
         } elseif ( ! isset( $_SESSION['data'][$class] ) && ! $thisKey ) { // nothing, so we're initializing
59 rodolico 98
            // display a list of all entries (filtered)
56 rodolico 99
            $selection = $class::getAll();
100
            if ( count($selection) == 1 ) { // we have a single entry
101
               $thisKey = key( $selection ); // so set key so we will display
57 rodolico 102
            } elseif ( count( $selection ) == 0 ) {
103
               return '<h3>No Records Found</h3>';
56 rodolico 104
            }
59 rodolico 105
         } elseif( ! $thisKey || (isset( $_SESSION['data'][$class] ) &&  $thisKey != $_SESSION['data'][$class]->id ) ) {
106
            // The key does not exist, or does not match the data we have
56 rodolico 107
            unset( $_SESSION['data'][$class] );
55 rodolico 108
         }
56 rodolico 109
         // we have a key, but we don't have a class instance yet
110
         if ( $thisKey && empty( $_SESSION['data'][$class] ) ) {
111
            $_SESSION['data'][$class] = new $class( $thisKey );
112
         }
113
         if ( $thisKey ) { // we are in the middle of doing something for the object
114
            $return = $_SESSION['data'][$class]->run();
115
         } else { // lets just show them a list of possible ojbects
116
            $return = $class::showSelectionList( array(), $selection );
117
         }
118
      } elseif ( $class ) { // Some module we don't have loaded
119
            switch ( $class ) {
59 rodolico 120
               case 'admin' : require_once( 'administration.php' );
121
                              $return = doAdmin();
58 rodolico 122
                              $return = print_r( $return, true );
56 rodolico 123
                              break;
124
               default      : $return = "<p>We don't know how to do <b>" . $_REQUEST['module'] . "</b> yet</p>";
125
            } // switch
126
      } else {
66 rodolico 127
         // this is the opening screen, so clear out all the data and
128
         // working on
56 rodolico 129
         unset( $_SESSION['data'] );
66 rodolico 130
         unset( $_SESSION['workingon'] );
57 rodolico 131
         if ( isset( $_REQUEST['doAdmin'] ) ) {
56 rodolico 132
            $return = $_SESSION['user']->admin($connection) .  $_SESSION['user']->errors();
133
            $_SESSION['user']->clearErrors();
134
         } else { // default to this if nothing else works
135
            $motd = $dbConnection->getOneDBValue( "select key_value from _system where group_name = 'program' and key_name = 'motd'" );
57 rodolico 136
            $return = "<div class='motd'>$motd</div>";
56 rodolico 137
            foreach ( array( 'Owner','Location','Device' ) as $class ) {
138
               $return .= processStats( $class, $class::getStats() );
139
            } // foreach
140
         } // else
45 rodolico 141
      } // else
142
      return $return;
143
   }
46 rodolico 144
 
145
   function buildRestrictions() {
146
      global $dbConnection;
53 rodolico 147
      $rules = explode( "\n", $_SESSION['user']->restrictions );
148
      $_SESSION['restrictions'] = array();
149
      $temp = array();
150
      $workingOn = '';
151
      foreach ( $rules as $thisOne ) {
152
         //print "<pre>Working on $thisOne\n</pre>";
153
         if ( preg_match( '/\[([^\[\]]+)\]/', $thisOne, $match ) ) {
154
            //print "Adding as category\n<br />";
155
            $workingOn = $match[1];
156
         } else {
157
            //print "Adding as a value in $workingOn\n<br />";
158
            $temp[$workingOn][] = "'" . $dbConnection->real_escape_string(trim($thisOne)) . "'";
159
         }
160
      } // foreach
56 rodolico 161
      if ( isset( $temp['owner'] ) ) {
53 rodolico 162
         $values = $dbConnection->columnToArray( sprintf( "select distinct owner_id id from view_device_location_owner_type where owner in (%s)", implode( ',', $temp['owner'] ) ) );
57 rodolico 163
         $_SESSION['restrictions']['Owner'] = sprintf( 'owner_id in ( %s )', implode( ',', $values ) );
53 rodolico 164
      }
56 rodolico 165
      if ( isset( $temp['location'] ) ) {
53 rodolico 166
         $values = $dbConnection->columnToArray( sprintf( "select distinct location_id id from view_device_location_owner_type where location in (%s)", implode( ',', $temp['location'] ) ) );
57 rodolico 167
         $_SESSION['restrictions']['Location'] = sprintf( 'location_id in ( %s )', implode( ',', $values ) );
53 rodolico 168
      }
56 rodolico 169
      if ( isset( $temp['device'] ) ) {
53 rodolico 170
         $values = $dbConnection->columnToArray( sprintf( "select distinct device_id id from view_device_location_owner_type where device in (%s)", implode( ',', $temp['device'] ) ) );
57 rodolico 171
         $_SESSION['restrictions']['Device'] = sprintf( 'device_id in ( %s )', implode( ',', $values ) );
53 rodolico 172
      }
46 rodolico 173
   }
45 rodolico 174
 
53 rodolico 175
   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>" ) {
176
      $return = array();
177
      foreach ( $data as $key => $value ) {
178
         $return[] = sprintf( $template, $target, $key, $value );
179
      }
180
      return $before . implode( $inside , $return ) . $after;
181
   }
22 rodolico 182
 
45 rodolico 183
 
184
 
1 rodolico 185
 
186
?>