| 43 | rodolico | 1 | <?php 
 | 
        
           |  |  | 2 |    include_once( '../../header.php' ); 
 | 
        
           |  |  | 3 | ?>
 | 
        
           | 41 | rodolico | 4 | <?php
 | 
        
           |  |  | 5 |   | 
        
           |  |  | 6 | include_once( '../../header.php' ); 
 | 
        
           |  |  | 7 | include_once( 'csvImporter.php' );
 | 
        
           |  |  | 8 |   | 
        
           | 43 | rodolico | 9 | /* 
 | 
        
           |  |  | 10 |  * select $column from $table where $match = '$value'
 | 
        
           |  |  | 11 |  * if $add is true, will add row if it does not exist.
 | 
        
           |  |  | 12 |  * returns $column from the result (or '' if it does not exist)
 | 
        
           |  |  | 13 |  * Used mainly to get an index from a table with matching value
 | 
        
           |  |  | 14 |  */
 | 
        
           | 41 | rodolico | 15 |   | 
        
           |  |  | 16 | function getValue ( $table, $column, $match, $value, $add = false ) {
 | 
        
           |  |  | 17 |    $return = getOneDBValue( "select $column from $table where $match = '$value'" );
 | 
        
           |  |  | 18 |    if ( $return === null ) {
 | 
        
           |  |  | 19 |       if ( $add ) {
 | 
        
           |  |  | 20 |          $return = doSQL( "insert into $table ( $match ) values ( '$value' )" );
 | 
        
           |  |  | 21 |          return $return['insert_id'];
 | 
        
           |  |  | 22 |       } else {
 | 
        
           |  |  | 23 |          $return = '';
 | 
        
           |  |  | 24 |       }
 | 
        
           |  |  | 25 |    }
 | 
        
           |  |  | 26 |    return $return;
 | 
        
           |  |  | 27 | }
 | 
        
           |  |  | 28 |   | 
        
           | 43 | rodolico | 29 | /*
 | 
        
           |  |  | 30 |  * Adds/updates license in license table
 | 
        
           |  |  | 31 |  * if already licensed for this machine, returns 'Already Set'
 | 
        
           |  |  | 32 |  * if license exists and is for a different machine
 | 
        
           |  |  | 33 |  *    mark it as removed
 | 
        
           |  |  | 34 |  *    adds key to new machine
 | 
        
           |  |  | 35 |  *    returns "Assigned"
 | 
        
           |  |  | 36 |  * Othewise, Adds key and assigns to machine
 | 
        
           |  |  | 37 |  * returns "Added"
 | 
        
           |  |  | 38 |  * NOTE: $device_id may be null which indicates license owned by client but unassigned
 | 
        
           |  |  | 39 |  */
 | 
        
           | 42 | rodolico | 40 | function updateLicense ( $client_id, $device_id, $license_product_id, $license ) {
 | 
        
           |  |  | 41 |    // see if the entry already exists
 | 
        
           |  |  | 42 |    $results = queryDatabaseExtended( "select license_id,client_id,device_id from license where license_product_id = $license_product_id and license = '$license' and removed_date is null" );
 | 
        
           |  |  | 43 |    //print "<pre>"; print_r( $results ); print "</pre>"; die;
 | 
        
           |  |  | 44 |    $db_license_id = $results['data'][0]['license_id'];
 | 
        
           |  |  | 45 |    $db_client_id = $results['data'][0]['client_id'];
 | 
        
           |  |  | 46 |    $db_device_id = $results['data'][0]['device_id'];
 | 
        
           |  |  | 47 |    $db_license_product_id = $results['data'][0]['license_product_id'];
 | 
        
           |  |  | 48 |    if ( ! $results ) { # this was not found, so just add it
 | 
        
           |  |  | 49 |       if ( $device_id === '' ) $device_id = 'null';
 | 
        
           |  |  | 50 |       doSQL( "insert into license (client_id,device_id,license_product_id,license, added_date) values ( $client_id, $device_id, $license_product_id, '$license', now() )" );
 | 
        
           |  |  | 51 |       return "Added";
 | 
        
           |  |  | 52 |    }
 | 
        
           |  |  | 53 |    if ( $client_id == $db_client_id && $device_id == $db_device_id ) { // already done, so just leave alone
 | 
        
           |  |  | 54 |       return "Already Set";
 | 
        
           |  |  | 55 |    }
 | 
        
           |  |  | 56 |    if ( ! $db_device_id ) { # key was not assigned before, so just assign it
 | 
        
           |  |  | 57 |       doSQL( "update license set device_id = $db_device_id,added_date = now() where license_id = $db_license_id" );
 | 
        
           |  |  | 58 |       return "Assigned";
 | 
        
           |  |  | 59 |    }
 | 
        
           |  |  | 60 |    // at this point, there is already an entry, but it is for a different machine, so we need to update it, ie remove the old, add the new
 | 
        
           |  |  | 61 |    doSQL( "update license set removed_date = now() where license_id = $db_license_id" );
 | 
        
           |  |  | 62 |    doSQL( "insert into license (client_id,device_id,license_product_id,license, added_date) values ( $client_id, $device_id, $license_product_id, '$license', now() )" );
 | 
        
           |  |  | 63 |    return "Reassigned";
 | 
        
           |  |  | 64 | }
 | 
        
           | 41 | rodolico | 65 |   | 
        
           | 43 | rodolico | 66 | /* import CSV, processing one line at a time
 | 
        
           |  |  | 67 |  * CSV may be delimited by anything defined in CsvImporter with auto-detect
 | 
        
           |  |  | 68 |  * returns table with results, suitable for embedding in div
 | 
        
           |  |  | 69 |  */
 | 
        
           | 42 | rodolico | 70 |   | 
        
           | 43 | rodolico | 71 |    function processFile ( $filename ) {
 | 
        
           |  |  | 72 |       $return = '';
 | 
        
           |  |  | 73 |       $fileInfo = new CsvImporter( $filename, true );
 | 
        
           |  |  | 74 |       while ( $line = $fileInfo->get(1) ) {
 | 
        
           |  |  | 75 |          foreach ( $line as $index => $values ) {
 | 
        
           |  |  | 76 |             $import[$values['client']][$values['device']][$values['license_product']] = $values['license'];
 | 
        
           |  |  | 77 |          }
 | 
        
           |  |  | 78 |       };
 | 
        
           | 42 | rodolico | 79 |   | 
        
           | 43 | rodolico | 80 |       // process each line in turn, displaying results in table
 | 
        
           |  |  | 81 |       $return = "<table border='1'><tr></tr><th>Client</th><th>ID</th><th>Machine</th><th>ID</th><th>Product</th><th>ID</th><th>License</th></tr>\n";
 | 
        
           |  |  | 82 |       foreach ( $import as $client => $data ) {
 | 
        
           |  |  | 83 |          // find client_id
 | 
        
           |  |  | 84 |          $client_id = getValue ( 'client', 'client_id', 'name', $client );
 | 
        
           |  |  | 85 |          if ( $client_id === '' ) {
 | 
        
           |  |  | 86 |             $return .= "<tr><td><b>Error</b></td><td colspan='7'>Could not find client ID for $client</td></tr>";
 | 
        
           | 41 | rodolico | 87 |             continue;
 | 
        
           |  |  | 88 |          }
 | 
        
           | 43 | rodolico | 89 |          // find machine_id
 | 
        
           |  |  | 90 |          foreach ( $data as $machine => $info ) {
 | 
        
           |  |  | 91 |             if ( $machine === '' ) {
 | 
        
           |  |  | 92 |                $machine_id = '';
 | 
        
           |  |  | 93 |             } else {
 | 
        
           |  |  | 94 |                $machine_id = getValue( 'device', 'device_id', 'name', $machine );
 | 
        
           |  |  | 95 |                if ( $machine_id === '' ) {
 | 
        
           |  |  | 96 |                   $return .= "<td><b>Error</b></td><td>$client</td><td>$client_id</td>";
 | 
        
           |  |  | 97 |                   $return .= "<td colspan='5'>Could not find machine ID for $machine</td></tr>";
 | 
        
           |  |  | 98 |                   continue;
 | 
        
           |  |  | 99 |                }
 | 
        
           |  |  | 100 |             }
 | 
        
           |  |  | 101 |             // now, locate each license_product_id and update the license information
 | 
        
           |  |  | 102 |             // if product does not exist, add it
 | 
        
           |  |  | 103 |             // thus, spelling counts or you'll get dup entries in license_product table
 | 
        
           |  |  | 104 |             foreach ( $info as $product => $license ) {
 | 
        
           |  |  | 105 |                $product_id = getValue( 'license_product', 'license_product_id', 'name', $product, true );
 | 
        
           |  |  | 106 |                $action = updateLicense( $client_id, $machine_id, $product_id, $license );
 | 
        
           |  |  | 107 |                $return .= "<tr>\n";
 | 
        
           |  |  | 108 |                $return .= "<td>$action</td><td>$client</td><td>$client_id</td>";
 | 
        
           |  |  | 109 |                $return .= "<td>$machine</td><td>$machine_id</td>";
 | 
        
           |  |  | 110 |                $return .= "<td>$product</td><td>$product_id</td><td>$license</td>\n";
 | 
        
           |  |  | 111 |                $return .= "</tr>\n";
 | 
        
           |  |  | 112 |   | 
        
           |  |  | 113 |             }
 | 
        
           |  |  | 114 |          } // for each machine
 | 
        
           |  |  | 115 |       } // for each client
 | 
        
           |  |  | 116 |       $return .= "</table>\n";
 | 
        
           |  |  | 117 |       return $return;
 | 
        
           |  |  | 118 |    } // function processFile
 | 
        
           | 41 | rodolico | 119 |   | 
        
           |  |  | 120 | ?>
 | 
        
           | 43 | rodolico | 121 | <?xml version="1.0" encoding="utf-8"?>
 | 
        
           |  |  | 122 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
 | 
        
           |  |  | 123 |   | 
        
           |  |  | 124 | <html xmlns="http://www.w3.org/1999/xhtml">
 | 
        
           |  |  | 125 |    <head>
 | 
        
           |  |  | 126 |       <title>Computer Asset Management Program - MODULE NAME - PAGE NAME</title>
 | 
        
           |  |  | 127 |       <link rel="stylesheet" type="text/css" href="../../camp.css">
 | 
        
           |  |  | 128 |    </head>
 | 
        
           |  |  | 129 |    <body>
 | 
        
           |  |  | 130 |       <?php include_once('../../menu.php'); ?>
 | 
        
           |  |  | 131 |       <div id="content">
 | 
        
           |  |  | 132 |       <?php 
 | 
        
           |  |  | 133 |          if ( $_REQUEST['fileToUpload'] ) {
 | 
        
           |  |  | 134 |             print processFile( $_FILES["fileToUpload"]["tmp_name"] );
 | 
        
           |  |  | 135 |          } else {
 | 
        
           |  |  | 136 |       ?>
 | 
        
           |  |  | 137 |       <p>Upload a CSV file to be bulk imported into the license table</p>
 | 
        
           |  |  | 138 |       <p><b>Warning:</b> no error checking is done, ensure your file meets the proper specifications</p>
 | 
        
           |  |  | 139 |       <form action="upload.php" method="post" enctype="multipart/form-data">
 | 
        
           |  |  | 140 |           Select CSV to upload:
 | 
        
           |  |  | 141 |           <input type="file" name="fileToUpload" id="fileToUpload">
 | 
        
           |  |  | 142 |           <input type="submit" value="Upload CSV" name="submit">
 | 
        
           |  |  | 143 |       </form>
 | 
        
           |  |  | 144 |       <?php } ?>
 | 
        
           |  |  | 145 |       </div>
 | 
        
           |  |  | 146 |    </body>
 | 
        
           |  |  | 147 | </html>
 |