Subversion Repositories computer_asset_manager_v1

Rev

Rev 100 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 100 Rev 102
Line 134... Line 134...
134
      $attribute = makeSafeSQLConstant( $attribute, 's', '' );
134
      $attribute = makeSafeSQLConstant( $attribute, 's', '' );
135
      if ( ! $attribute ) return 0;
135
      if ( ! $attribute ) return 0;
136
      $return = getOneDBValue( "select attrib_id from attrib where name = $attribute" );
136
      $return = getOneDBValue( "select attrib_id from attrib where name = $attribute" );
137
      if ( ! $return && $createIfNotFound ) {
137
      if ( ! $return && $createIfNotFound ) {
138
         $return = queryDatabaseExtended( "insert into attrib( name, added_date, removed_date ) values ( $attribute, now(), null )" );
138
         $return = queryDatabaseExtended( "insert into attrib( name, added_date, removed_date ) values ( $attribute, now(), null )" );
139
         return $result['insert_id'] ? $result['insert_id'] : 0;
139
         return $return['insert_id'] ? $return['insert_id'] : 0;
140
      }
140
      }
141
      return $return;
141
      return $return;
142
   }
142
   }
143
 
143
 
144
   /*
144
   /*
Line 182... Line 182...
182
    */
182
    */
183
   function parseTabDelimFile ( $contents, $createAttributeIfNotFound = false, $createDeviceIfNotFound = false, $default_device_id='', $default_site_id='', $default_client_id='' ) {
183
   function parseTabDelimFile ( $contents, $createAttributeIfNotFound = false, $createDeviceIfNotFound = false, $default_device_id='', $default_site_id='', $default_client_id='' ) {
184
      $data = tabDelimToArray( $contents );
184
      $data = tabDelimToArray( $contents );
185
      // we'll put our SQL into an array, then dump it.
185
      // we'll put our SQL into an array, then dump it.
186
      $sql = array();
186
      $sql = array();
-
 
187
      // and track any errors here
-
 
188
      $errors = array();
187
      /*
189
      /*
188
      print "<pre>";
190
      print "<pre>";
189
      var_dump( $createAttributeIfNotFound, $createDeviceIfNotFound, $default_device_id, $default_site_id, $default_client_id );
191
      var_dump( $createAttributeIfNotFound, $createDeviceIfNotFound, $default_device_id, $default_site_id, $default_client_id );
190
      print "</pre>";
192
      print "</pre>";
191
      return $sql;
193
      return $sql;
Line 193... Line 195...
193
      // the following two arrays will store attributes and devices as we find them
195
      // the following two arrays will store attributes and devices as we find them
194
      // we can then FIRST look them up here, in memory, and go to database only when we don't know them
196
      // we can then FIRST look them up here, in memory, and go to database only when we don't know them
195
      $attributesFromDatabase = array(); // uniquely store our attributes here
197
      $attributesFromDatabase = array(); // uniquely store our attributes here
196
      $deviceFromDatabase = array();
198
      $deviceFromDatabase = array();
197
      for ( $i = 0; $i < count( $data ); $i++ ) {  // go through each line and grab fields we need
199
      for ( $i = 0; $i < count( $data ); $i++ ) {  // go through each line and grab fields we need
198
 
-
 
199
         // get device_id
200
         // get device_id
200
         if ( ! $data[$i]['device_id'] ) {
201
         if ( ! $data[$i]['device_id'] ) {
201
            if ( $data[$i]['device'] ) {
202
            if ( $data[$i]['device'] ) {
202
               if ( isset( $deviceFromDatabase[$data[$i]['device']] ) ) {
203
               if ( isset( $deviceFromDatabase[$data[$i]['device']] ) ) {
203
                  $data[$i]['device_id'] = $deviceFromDatabase[$data[$i]['device']];
204
                  $data[$i]['device_id'] = $deviceFromDatabase[$data[$i]['device']];
Line 209... Line 210...
209
            }
210
            }
210
            if ( ! $data[$i]['device_id'] ) {
211
            if ( ! $data[$i]['device_id'] ) {
211
               if ( $default_device_id ) {
212
               if ( $default_device_id ) {
212
                  $data[$i]['device_id'] = $default_device_id;
213
                  $data[$i]['device_id'] = $default_device_id;
213
               } else {
214
               } else {
214
                  $sql[] = "/* Can not locate device in line $i */";
215
                  $errors[] = "Can not locate device [" . $data[$i]['device'] . "] in line $i";
215
                  continue;
216
                  continue;
216
               }
217
               }
217
            }
218
            }
218
         }
219
         }
219
 
220
 
Line 227... Line 228...
227
               }
228
               }
228
               if ( $data[$i]['attrib_id'] )
229
               if ( $data[$i]['attrib_id'] )
229
                  $attributesFromDatabase[$data[$i]['attribute']] = $data[$i]['attrib_id'];
230
                  $attributesFromDatabase[$data[$i]['attribute']] = $data[$i]['attrib_id'];
230
            }
231
            }
231
            if ( ! $data[$i]['attrib_id'] ) {
232
            if ( ! $data[$i]['attrib_id'] ) {
232
               $sql[] = "/* Can not locate attribute in line $i */";
233
               $errors[] = 'Can not locate attribute [' . $data[$i]['attribute'] . "] in line $i";
233
               continue;
234
               continue;
234
            }
235
            }
235
         }
236
         }
236
         
237
         
237
         if ( ! $data[$i]['value'] ) {
238
         if ( ! $data[$i]['value'] ) {
238
            $sql[] = "/* No Value given for line $i, skipped */";
239
            $errors[] = "No Value given for line $i, skipped";
239
            continue;
240
            continue;
240
         }
241
         }
241
 
242
 
242
         $value = makeSafeSQLConstant( $data[$i]['value'] );
243
         $sql = array_merge( $sql, makeSQL( $data[$i] ) );
243
         $attrib_id = makeSafeSQLConstant( $data[$i]['attrib_id'], 'i' );
-
 
244
         $device_id = makeSafeSQLConstant( $data[$i]['device_id'],'i' );
-
 
245
         // standard SQL that does an insert if the value doesn't already exist.
-
 
246
         $sql[] =
-
 
247
            "insert into attrib_device ( device_id, attrib_id,value, added_date )
-
 
248
               select $device_id,$attrib_id, $value, now()
-
 
249
               from dual
-
 
250
               where
-
 
251
                  not exists (
-
 
252
                     select * from attrib_device join device using (device_id) join attrib using (attrib_id) where device_id = $device_id and attrib_id = $attrib_id
-
 
253
                     );";
-
 
254
      }
244
      }
-
 
245
      return array( 'errors' => $errors, 'sql' => $sql );
-
 
246
   }
-
 
247
 
-
 
248
   /*
-
 
249
    * makeSQL
-
 
250
    * creates one or more SQL statements to upload attribute/value into the database.
-
 
251
    * if unique is true, will not do an insert. Instead, it will only update an existing
-
 
252
    * row if device_id and attrib_id pair already match.
-
 
253
    * if unique is false, will only do an insert of a new (possibly duplicated) value
-
 
254
    * WARNING: If you have multiple rows with device_id/attrib_id pair, this could delete all of them.
-
 
255
    */
-
 
256
 
-
 
257
   function makeSQL ( $data, $unique = false ) {
-
 
258
      $value = makeSafeSQLConstant( $data['value'] );
-
 
259
      $attrib_id = makeSafeSQLConstant( $data['attrib_id'], 'i' );
-
 
260
      $device_id = makeSafeSQLConstant( $data['device_id'],'i' );
-
 
261
      $sql = array();
-
 
262
      // standard SQL that does an insert if the value doesn't already exist.
-
 
263
      // remove any existing records
-
 
264
      $sql[] =
-
 
265
         "update attrib_device
-
 
266
            set removed_date = now()
-
 
267
            where
-
 
268
               device_id = $device_id
-
 
269
               and attrib_id = $attrib_id
-
 
270
               and removed_date is null
-
 
271
               and attrib_id in (select attrib_id from attrib where multiples is null);";
-
 
272
      $sql[] =
-
 
273
         "update attrib_device
-
 
274
            set value = $value
-
 
275
            where
-
 
276
               device_id = $device_id
-
 
277
               and attrib_id = $attrib_id
-
 
278
               and attrib_id in (select attrib_id from attrib where multiples is null)
-
 
279
               and not exists (
-
 
280
                     select * from (select * from attrib_device ) as m2 where device_id = $device_id and attrib_id = $attrib_id and value = $value
-
 
281
                  );";
-
 
282
      $sql[] = 
-
 
283
         "insert into attrib_device ( device_id, attrib_id,value, added_date )
-
 
284
            select $device_id,$attrib_id, $value, now()
-
 
285
            from dual
-
 
286
            where
-
 
287
               not exists (
-
 
288
                  select *
-
 
289
                  from
-
 
290
                     attrib_device
-
 
291
                  where
-
 
292
                     device_id = $device_id
-
 
293
                     and attrib_id = $attrib_id
-
 
294
                     and value = $value
-
 
295
                     and removed_date is null
-
 
296
                  );";
255
      return $sql;
297
      return $sql;
-
 
298
   } // getSQL
-
 
299
 
-
 
300
   function updateDatabase ( $queries ) {
-
 
301
      $count = 0;
-
 
302
      for( $i = 0; $i < count( $queries ); $i++ ) {
-
 
303
         $result = doSQL( $queries[$i] );
-
 
304
         $count += $result['affected_rows'];
-
 
305
      }
-
 
306
      return "$count entries added or updated";
256
   }
307
   }
257
 
308
 
258
 
309
 
259
   /*
310
   /*
260
    * following block of code is duplicated from the files module. It should instead be placed in root/include/library.php or something
311
    * following block of code is duplicated from the files module. It should instead be placed in root/include/library.php or something