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