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
 
67 rodolico 88
      $baseClasses = array( 'owner' => 1, 'location' => 1, 'device' => 1 );
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
      }
56 rodolico 116
      //print "<pre>class=$class\nid=$id\n</pre>";
117
 
118
      // We have a class (module) calling and the class exists
119
      // if we don't have a single entry, we'll display a selection list
120
      // if we only have one entry, we'll display the screen for it
67 rodolico 121
      if ( $class ) {
122
         if ( $action && isset( $baseClasses[$class] ) ) {
123
            switch ( $action ) {
124
               case 'add': // for an add, we need a new, empty instance
125
                  $_SESSION['data'][$class] = new $class();
126
               case 'edit':
127
               case 'post':
128
                  return $_SESSION['data'][$class]->run();
129
                  break;
130
               case 'search':
131
                  // display a list of all entries (filtered)
132
                  $selection = $class::getAll();
133
                  if ( count($selection) == 1 ) { // we have a single entry
134
                     $thisKey = key( $selection ); // so set key so we will display
135
                     $_SESSION['data'][$class] = new $class( $thisKey );
136
                  } elseif ( count( $selection ) == 0 ) {
137
                     $return = '<h3>No Records Found</h3>';
138
                  } else {
139
                     $return = $class::showSelectionList( array(), $selection );
140
                  }
141
               default:
142
                  if ( $thisKey ) { // we are in the middle of doing something for the object
143
                     $return = $_SESSION['data'][$class]->run();
144
                  } else { // lets just show them a list of possible ojbects
145
                     if ( class_exists( $class ) ) {
146
                        $return = $class::showSelectionList( array(), $selection );
147
                     } else {
148
                        $return = "<p>We don't know how to do <b>" . $_REQUEST['module'] . "</b> yet</p>";
149
                     } // if class_exists
150
                  } // if a key exists
151
            } // switch
152
         } else { // no action, or not in baseClasses
153
            if ( empty ( $_SESSION['data'][$class] ) ) {
154
               $_SESSION['data'][$class] = new $class();
56 rodolico 155
            }
156
            $return = $_SESSION['data'][$class]->run();
67 rodolico 157
         } // if action and baseclass
56 rodolico 158
      } else {
66 rodolico 159
         // this is the opening screen, so clear out all the data and
160
         // working on
56 rodolico 161
         unset( $_SESSION['data'] );
66 rodolico 162
         unset( $_SESSION['workingon'] );
67 rodolico 163
         $motd = $dbConnection->getOneDBValue( "select key_value from _system where group_name = 'program' and key_name = 'motd'" );
164
         $return = "<div class='motd'>$motd</div>";
165
         foreach ( array( 'Owner','Location','Device' ) as $class ) {
166
            $return .= processStats( $class, $class::getStats() );
167
         } // foreach
168
      } // if..else
45 rodolico 169
      return $return;
67 rodolico 170
   } // html
46 rodolico 171
 
172
   function buildRestrictions() {
173
      global $dbConnection;
53 rodolico 174
      $rules = explode( "\n", $_SESSION['user']->restrictions );
175
      $_SESSION['restrictions'] = array();
176
      $temp = array();
177
      $workingOn = '';
178
      foreach ( $rules as $thisOne ) {
179
         //print "<pre>Working on $thisOne\n</pre>";
180
         if ( preg_match( '/\[([^\[\]]+)\]/', $thisOne, $match ) ) {
181
            //print "Adding as category\n<br />";
182
            $workingOn = $match[1];
183
         } else {
184
            //print "Adding as a value in $workingOn\n<br />";
185
            $temp[$workingOn][] = "'" . $dbConnection->real_escape_string(trim($thisOne)) . "'";
186
         }
187
      } // foreach
56 rodolico 188
      if ( isset( $temp['owner'] ) ) {
53 rodolico 189
         $values = $dbConnection->columnToArray( sprintf( "select distinct owner_id id from view_device_location_owner_type where owner in (%s)", implode( ',', $temp['owner'] ) ) );
57 rodolico 190
         $_SESSION['restrictions']['Owner'] = sprintf( 'owner_id in ( %s )', implode( ',', $values ) );
53 rodolico 191
      }
56 rodolico 192
      if ( isset( $temp['location'] ) ) {
53 rodolico 193
         $values = $dbConnection->columnToArray( sprintf( "select distinct location_id id from view_device_location_owner_type where location in (%s)", implode( ',', $temp['location'] ) ) );
57 rodolico 194
         $_SESSION['restrictions']['Location'] = sprintf( 'location_id in ( %s )', implode( ',', $values ) );
53 rodolico 195
      }
56 rodolico 196
      if ( isset( $temp['device'] ) ) {
53 rodolico 197
         $values = $dbConnection->columnToArray( sprintf( "select distinct device_id id from view_device_location_owner_type where device in (%s)", implode( ',', $temp['device'] ) ) );
57 rodolico 198
         $_SESSION['restrictions']['Device'] = sprintf( 'device_id in ( %s )', implode( ',', $values ) );
53 rodolico 199
      }
46 rodolico 200
   }
45 rodolico 201
 
53 rodolico 202
   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>" ) {
203
      $return = array();
204
      foreach ( $data as $key => $value ) {
205
         $return[] = sprintf( $template, $target, $key, $value );
206
      }
207
      return $before . implode( $inside , $return ) . $after;
208
   }
22 rodolico 209
 
45 rodolico 210
 
211
 
1 rodolico 212
 
213
?>