Subversion Repositories computer_asset_manager_v1

Rev

Rev 69 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

<?php 
   include_once( '../../header.php' );
   include_once( './functions.php' );
?>
<?php

include_once( '../../header.php' ); 
include_once( 'csvImporter.php' );

/* import CSV, processing one line at a time
 * CSV may be delimited by anything defined in CsvImporter with auto-detect
 * returns table with results, suitable for embedding in div
 */

   function processFile ( $filename, $delimiter, $encapsulation = '"', $escape = '\\', $deviceNotFoundIsNull = false) {
      $return = '';
      $fileInfo = new CsvImporter( $filename, true, $delimiter, $enclosure, $escape );
      $numLines = 0;
      
      /*
       * load the information in a hash list, similar to
       * client1
       *    machine1
       *       product1
       *       product2
       *    machine2
       *       productx
       * client2
       *    ...
       */
      while ( $line = $fileInfo->get(1) ) {
         foreach ( $line as $index => $values ) {
            $import[$values['client']][$values['license_product']][$values['license']] = $values['device'];
            $numLines++;
         }
      };
      $return .= "<p>$numLines lines read</p>";
      $numLines = 0; // reset so we can count the number of lines processed
      // process each client/machine/license in turn, displaying results in table
      $return .= "<table border='1'><tr></tr><th>Status</th><th>Client</th><th>ID</th><th>Machine</th><th>ID</th><th>Product</th><th>ID</th><th>License</th></tr>\n";
      foreach ( $import as $client => $data ) {
         // find client_id
         $client_id = getValue ( 'client', 'client_id', 'name', $client );
         if ( $client_id === '' ) { // can't find client, so big error
            $return .= "<tr><td><b>Error</b></td><td colspan='7'>Could not find client ID for $client</td></tr>";
            continue;
         }
         foreach ( $data as $product => $info ) {
            foreach ( $info as $license => $machine ) {
               $numLines++;
               if ( $machine === '' ) { // can't find machine
                  $machine_id = '';
               } else {
                  $machine_id = getValue( 'device', 'device_id', 'name', $machine );
                  // if we can't find it and they don't want to continue
                  if ( $machine_id === '' and ! $deviceNotFoundIsNull ) { 
                     $return .= "<td><b>Error</b></td><td>$client</td><td>$client_id</td>";
                     $return .= "<td colspan='5'>Could not find machine ID for $machine</td></tr>";
                     continue; //ignor
                  }
               }
               // now, locate each license_product_id and update the license information
               // if product does not exist, add it
               // thus, spelling counts or you'll get dup entries in license_product table
               # look for product name, but don't update it if it doesn't exist
               $product_id = getValue( 'license_product', 'license_product_id', 'name', $product, false );
               /*
                * Some software (like Belarc) show product names as publiser - product name, ie
                * Microsoft - Office 2017
                * So, we'll look to see if this is the case here.
                * NOTE: we are only checking if this key is publisher - product and, if so checking for product
                *       however, if it is listed in database as publisher - product and we are just product, it 
                *       will still result in dups.
                */
               if ( ! $product_id ) { 
                  if ( strpos( $product, ' - ' ) !== false ) {
                     $product_id = getValue( 'license_product', 'license_product_id', 'name', substr( $product, strpos( $product, ' - ' ) + 3 ), false );
                  }
               }
               // if we still haven't found it, just add the bloody thing.
               if ( ! $product_id )
                  $product_id = getValue( 'license_product', 'license_product_id', 'name', $product, true );
      
               $action = updateLicense( $client_id, $machine_id, $product_id, $license );
               $return .= "<tr>\n";
               $return .= "<td>$action</td><td>$client</td><td>$client_id</td>";
               $return .= "<td>$machine</td><td>$machine_id</td>";
               $return .= "<td>$product</td><td>$product_id</td><td>$license</td>\n";
               $return .= "</tr>\n";
               
            }
         } // for each machine
      } // for each client
      $return .= "</table>\n";
      $return .= "<p>$numLines processed</p>";
      return $return;
   } // function processFile

?>
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >

<html xmlns="http://www.w3.org/1999/xhtml">
   <head>
      <title>Computer Asset Management Program - MODULE NAME - PAGE NAME</title>
      <link rel="stylesheet" type="text/css" href="../../camp.css">
   </head>
   <body>
      <?php include_once('../../menu.php'); ?>
      <div id="content">
      <?php 
         if ( isset( $_POST["submit"] ) ) {
            $filename = tempnam( '/tmp', 'blk' );
            if ( move_uploaded_file( $_FILES["fileToUpload"]["tmp_name"], $filename ) ) {
               print "<p>Loading Data</p>\n";
               print processFile( $filename , $_REQUEST['delimiter'], $_REQUEST['encapsulation'], $_REQUEST['escape'], $_REQUEST['notfoundisnull']  );
            } else {
               print "<p>Error: move_uploaded_file failed to move to $filename</p>";
            }
            //unlink( $filename );
         } else {
      ?>
      <p align\'center'>Upload a CSV file to be bulk imported into the license table</p>
      <p><b>Warning:</b> no error checking is done, ensure your file meets the proper specifications. The fields may be separated by</p>
      <ul>
         <li>comma</li>
         <li>semicolon</li>
         <li>tab</li>
         <li>pipe</li>
         <li>colon</li>
      </ul>
      <p>The first line should be a header, containing the following in any order. All other fields will be ignored</p>
      <ul>
         <li>client</li>
         <li>device</li>
         <li>license_product</li>
         <li>license</li>
      </ul>
      <p><b>Warning!</b> Client, Machine and Product name must be exactly as already stored.</p>
      <a href="<?php print dirname($_SERVER['SCRIPT_NAME']) . '/license_master.csv'; ?>">Get master csv file</a>.
      This contains all license names as of 2020-01-15. Just delete any lines/columns you do not use.
      <p>License is case sensitive</p>
      <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" enctype="multipart/form-data">
         <table border='1'>
            <tr>
               <td>Set Machine to Null if not found</td>
               <td> <input type="checkbox" name="notfoundisnull" value="notfoundisnull"></td>
            </tr>
            <tr>
               <td>Select CSV to upload</td>
               <td><input type="file" name="fileToUpload" id="fileToUpload"></td>
            </tr>
            <tr>
               <td>Field Delimiters</td>
               <td>
                  <select name='delimiter' id='delimiter'>
                     <option value='auto' selected>Auto Detect</option>
                     <option value='tab'>Tab</option>
                     <option value=','>Comma (,)</option>
                     <option value=';'>Semicolon (;)</option>
                     <option value='|'>Pipe (|)</option>
                     <option value=':'>Colon (:)</option>
                  </select>

               </td>
            </tr>
            <tr>
               <td>Encapsulation</td>
               <td>
                  <select name='encapsulation' id='encapsulation'>
                     <option value='auto' selected>Auto Detect</option>
                     <option value=''>None</option>
                     <option value='"'>Double Quotes (")</option>
                     <option value="'">Single Quotes (')</option>
                     <option value='~'>Tilde (~)</option>
                  </select>
               </td>
            </tr>
            <tr>
               <td>Escape Char</td>
               <td>
                  <select name='escape' id='escape'>
                     <option value='' selected>None</option>
                     <option value='\'>Backslash (\)</option>
                  </select>
               </td>
            </tr>
            <tr>
               <td colspan='2' align='center'><input type="submit" value="Upload CSV" name="submit"></td>
            </tr>
          </table>
      </form>
      <p>It is generally safe to leave Encapsulation and Escape at their defaults</p>
      <?php } ?>
      </div>
   </body>
</html>