| 43 | rodolico | 1 | <?php 
 | 
        
           | 46 | rodolico | 2 |    include_once( '../../header.php' );
 | 
        
           |  |  | 3 |    include_once( './functions.php' );
 | 
        
           | 43 | rodolico | 4 | ?>
 | 
        
           | 41 | rodolico | 5 | <?php
 | 
        
           |  |  | 6 |   | 
        
           |  |  | 7 | include_once( '../../header.php' ); 
 | 
        
           |  |  | 8 | include_once( 'csvImporter.php' );
 | 
        
           |  |  | 9 |   | 
        
           | 43 | rodolico | 10 | /* import CSV, processing one line at a time
 | 
        
           |  |  | 11 |  * CSV may be delimited by anything defined in CsvImporter with auto-detect
 | 
        
           |  |  | 12 |  * returns table with results, suitable for embedding in div
 | 
        
           |  |  | 13 |  */
 | 
        
           | 42 | rodolico | 14 |   | 
        
           | 45 | rodolico | 15 |    function processFile ( $filename, $delimiter, $encapsulation = '"', $escape = '\\', $deviceNotFoundIsNull = false) {
 | 
        
           | 43 | rodolico | 16 |       $return = '';
 | 
        
           | 45 | rodolico | 17 |       $fileInfo = new CsvImporter( $filename, true, $delimiter, $enclosure, $escape );
 | 
        
           |  |  | 18 |   | 
        
           |  |  | 19 |       /*
 | 
        
           |  |  | 20 |        * load the information in a hash list, similar to
 | 
        
           |  |  | 21 |        * client1
 | 
        
           |  |  | 22 |        *    machine1
 | 
        
           |  |  | 23 |        *       product1
 | 
        
           |  |  | 24 |        *       product2
 | 
        
           |  |  | 25 |        *    machine2
 | 
        
           |  |  | 26 |        *       productx
 | 
        
           |  |  | 27 |        * client2
 | 
        
           |  |  | 28 |        *    ...
 | 
        
           |  |  | 29 |        */
 | 
        
           | 43 | rodolico | 30 |       while ( $line = $fileInfo->get(1) ) {
 | 
        
           |  |  | 31 |          foreach ( $line as $index => $values ) {
 | 
        
           |  |  | 32 |             $import[$values['client']][$values['device']][$values['license_product']] = $values['license'];
 | 
        
           |  |  | 33 |          }
 | 
        
           |  |  | 34 |       };
 | 
        
           | 42 | rodolico | 35 |   | 
        
           | 45 | rodolico | 36 |       // process each client/machine/license in turn, displaying results in table
 | 
        
           |  |  | 37 |       $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";
 | 
        
           | 43 | rodolico | 38 |       foreach ( $import as $client => $data ) {
 | 
        
           |  |  | 39 |          // find client_id
 | 
        
           |  |  | 40 |          $client_id = getValue ( 'client', 'client_id', 'name', $client );
 | 
        
           | 45 | rodolico | 41 |          if ( $client_id === '' ) { // can't find client, so big error
 | 
        
           | 43 | rodolico | 42 |             $return .= "<tr><td><b>Error</b></td><td colspan='7'>Could not find client ID for $client</td></tr>";
 | 
        
           | 41 | rodolico | 43 |             continue;
 | 
        
           |  |  | 44 |          }
 | 
        
           | 43 | rodolico | 45 |          // find machine_id
 | 
        
           |  |  | 46 |          foreach ( $data as $machine => $info ) {
 | 
        
           | 45 | rodolico | 47 |             if ( $machine === '' ) { // can't find machine
 | 
        
           | 43 | rodolico | 48 |                $machine_id = '';
 | 
        
           |  |  | 49 |             } else {
 | 
        
           |  |  | 50 |                $machine_id = getValue( 'device', 'device_id', 'name', $machine );
 | 
        
           | 45 | rodolico | 51 |                // if we can't find it and they don't want to continue
 | 
        
           |  |  | 52 |                if ( $machine_id === '' and ! $deviceNotFoundIsNull ) { 
 | 
        
           | 43 | rodolico | 53 |                   $return .= "<td><b>Error</b></td><td>$client</td><td>$client_id</td>";
 | 
        
           |  |  | 54 |                   $return .= "<td colspan='5'>Could not find machine ID for $machine</td></tr>";
 | 
        
           | 45 | rodolico | 55 |                   continue; //ignor
 | 
        
           | 43 | rodolico | 56 |                }
 | 
        
           |  |  | 57 |             }
 | 
        
           |  |  | 58 |             // now, locate each license_product_id and update the license information
 | 
        
           |  |  | 59 |             // if product does not exist, add it
 | 
        
           |  |  | 60 |             // thus, spelling counts or you'll get dup entries in license_product table
 | 
        
           |  |  | 61 |             foreach ( $info as $product => $license ) {
 | 
        
           |  |  | 62 |                $product_id = getValue( 'license_product', 'license_product_id', 'name', $product, true );
 | 
        
           |  |  | 63 |                $action = updateLicense( $client_id, $machine_id, $product_id, $license );
 | 
        
           |  |  | 64 |                $return .= "<tr>\n";
 | 
        
           |  |  | 65 |                $return .= "<td>$action</td><td>$client</td><td>$client_id</td>";
 | 
        
           |  |  | 66 |                $return .= "<td>$machine</td><td>$machine_id</td>";
 | 
        
           |  |  | 67 |                $return .= "<td>$product</td><td>$product_id</td><td>$license</td>\n";
 | 
        
           |  |  | 68 |                $return .= "</tr>\n";
 | 
        
           |  |  | 69 |   | 
        
           |  |  | 70 |             }
 | 
        
           |  |  | 71 |          } // for each machine
 | 
        
           |  |  | 72 |       } // for each client
 | 
        
           |  |  | 73 |       $return .= "</table>\n";
 | 
        
           |  |  | 74 |       return $return;
 | 
        
           |  |  | 75 |    } // function processFile
 | 
        
           | 41 | rodolico | 76 |   | 
        
           |  |  | 77 | ?>
 | 
        
           | 43 | rodolico | 78 | <?xml version="1.0" encoding="utf-8"?>
 | 
        
           |  |  | 79 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
 | 
        
           |  |  | 80 |   | 
        
           |  |  | 81 | <html xmlns="http://www.w3.org/1999/xhtml">
 | 
        
           |  |  | 82 |    <head>
 | 
        
           |  |  | 83 |       <title>Computer Asset Management Program - MODULE NAME - PAGE NAME</title>
 | 
        
           |  |  | 84 |       <link rel="stylesheet" type="text/css" href="../../camp.css">
 | 
        
           |  |  | 85 |    </head>
 | 
        
           |  |  | 86 |    <body>
 | 
        
           |  |  | 87 |       <?php include_once('../../menu.php'); ?>
 | 
        
           |  |  | 88 |       <div id="content">
 | 
        
           |  |  | 89 |       <?php 
 | 
        
           | 45 | rodolico | 90 |          if ( $_POST["submit"] ) {
 | 
        
           |  |  | 91 |             $filename = tempnam( '/tmp', 'blk' );
 | 
        
           |  |  | 92 |             if ( move_uploaded_file( $_FILES["fileToUpload"]["tmp_name"], $filename ) ) {
 | 
        
           |  |  | 93 |                print "<p>Loading Data</p>\n";
 | 
        
           |  |  | 94 |                print processFile( $filename , $_REQUEST['delimiter'], $_REQUEST['encapsulation'], $_REQUEST['escape'], $_REQUEST['notfoundisnull']  );
 | 
        
           |  |  | 95 |             } else {
 | 
        
           |  |  | 96 |                print "<p>Error: move_uploaded_file failed to move to $filename</p>";
 | 
        
           |  |  | 97 |             }
 | 
        
           |  |  | 98 |             //unlink( $filename );
 | 
        
           | 43 | rodolico | 99 |          } else {
 | 
        
           |  |  | 100 |       ?>
 | 
        
           | 45 | rodolico | 101 |       <p align\'center'>Upload a CSV file to be bulk imported into the license table</p>
 | 
        
           |  |  | 102 |       <p><b>Warning:</b> no error checking is done, ensure your file meets the proper specifications. The fields may be separated by</p>
 | 
        
           |  |  | 103 |       <ul>
 | 
        
           |  |  | 104 |          <li>comma</li>
 | 
        
           |  |  | 105 |          <li>semicolon</li>
 | 
        
           |  |  | 106 |          <li>tab</li>
 | 
        
           |  |  | 107 |          <li>pipe</li>
 | 
        
           |  |  | 108 |          <li>colon</li>
 | 
        
           |  |  | 109 |       </ul>
 | 
        
           |  |  | 110 |       <p>The first line should be a header, containing the following in any order. All other fields will be ignored</p>
 | 
        
           |  |  | 111 |       <ul>
 | 
        
           |  |  | 112 |          <li>client</li>
 | 
        
           |  |  | 113 |          <li>device</li>
 | 
        
           |  |  | 114 |          <li>license_product</li>
 | 
        
           |  |  | 115 |          <li>license</li>
 | 
        
           |  |  | 116 |       </ul>
 | 
        
           |  |  | 117 |       <p><b>Warning!</b> Client, Machine and Product name must be exactly as already stored.</p>
 | 
        
           |  |  | 118 |       <p>License is case sensitive</p>
 | 
        
           |  |  | 119 |       <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" enctype="multipart/form-data">
 | 
        
           |  |  | 120 |          <table border='1'>
 | 
        
           |  |  | 121 |             <tr>
 | 
        
           |  |  | 122 |                <td>Set Machine to Null if not found</td>
 | 
        
           |  |  | 123 |                <td> <input type="checkbox" name="notfoundisnull" value="notfoundisnull"></td>
 | 
        
           |  |  | 124 |             </tr>
 | 
        
           |  |  | 125 |             <tr>
 | 
        
           |  |  | 126 |                <td>Select CSV to upload</td>
 | 
        
           |  |  | 127 |                <td><input type="file" name="fileToUpload" id="fileToUpload"></td>
 | 
        
           |  |  | 128 |             </tr>
 | 
        
           |  |  | 129 |             <tr>
 | 
        
           |  |  | 130 |                <td>Field Delimiters</td>
 | 
        
           |  |  | 131 |                <td>
 | 
        
           |  |  | 132 |                   <select name='delimiter' id='delimiter'>
 | 
        
           |  |  | 133 |                      <option value='auto' selected>Auto Detect</option>
 | 
        
           |  |  | 134 |                      <option value='tab'>Tab</option>
 | 
        
           |  |  | 135 |                      <option value=','>Comma (,)</option>
 | 
        
           |  |  | 136 |                      <option value=';'>Semicolon (;)</option>
 | 
        
           |  |  | 137 |                      <option value='|'>Pipe (|)</option>
 | 
        
           |  |  | 138 |                      <option value=':'>Colon (:)</option>
 | 
        
           |  |  | 139 |                   </select>
 | 
        
           |  |  | 140 |   | 
        
           |  |  | 141 |                </td>
 | 
        
           |  |  | 142 |             </tr>
 | 
        
           |  |  | 143 |             <tr>
 | 
        
           |  |  | 144 |                <td>Encapsulation</td>
 | 
        
           |  |  | 145 |                <td>
 | 
        
           |  |  | 146 |                   <select name='encapsulation' id='encapsulation'>
 | 
        
           |  |  | 147 |                      <option value='auto' selected>Auto Detect</option>
 | 
        
           |  |  | 148 |                      <option value=''>None</option>
 | 
        
           |  |  | 149 |                      <option value='"'>Double Quotes (")</option>
 | 
        
           |  |  | 150 |                      <option value="'">Single Quotes (')</option>
 | 
        
           |  |  | 151 |                      <option value='~'>Tilde (~)</option>
 | 
        
           |  |  | 152 |                   </select>
 | 
        
           |  |  | 153 |                </td>
 | 
        
           |  |  | 154 |             </tr>
 | 
        
           |  |  | 155 |             <tr>
 | 
        
           |  |  | 156 |                <td>Escape Char</td>
 | 
        
           |  |  | 157 |                <td>
 | 
        
           |  |  | 158 |                   <select name='escape' id='escape'>
 | 
        
           |  |  | 159 |                      <option value='' selected>None</option>
 | 
        
           |  |  | 160 |                      <option value='\'>Backslash (\)</option>
 | 
        
           |  |  | 161 |                   </select>
 | 
        
           |  |  | 162 |                </td>
 | 
        
           |  |  | 163 |             </tr>
 | 
        
           |  |  | 164 |             <tr>
 | 
        
           |  |  | 165 |                <td colspan='2' align='center'><input type="submit" value="Upload CSV" name="submit"></td>
 | 
        
           |  |  | 166 |             </tr>
 | 
        
           |  |  | 167 |           </table>
 | 
        
           | 43 | rodolico | 168 |       </form>
 | 
        
           | 45 | rodolico | 169 |       <p>It is generally safe to leave Encapsulation and Escape at their defaults</p>
 | 
        
           | 43 | rodolico | 170 |       <?php } ?>
 | 
        
           |  |  | 171 |       </div>
 | 
        
           |  |  | 172 |    </body>
 | 
        
           |  |  | 173 | </html>
 |