Rev 41 | Rev 43 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
<?php
include_once( '../../header.php' );
include_once( 'csvImporter.php' );
$filename = 'machine_licenses.csv';
function getValue ( $table, $column, $match, $value, $add = false ) {
$return = getOneDBValue( "select $column from $table where $match = '$value'" );
if ( $return === null ) {
if ( $add ) {
$return = doSQL( "insert into $table ( $match ) values ( '$value' )" );
return $return['insert_id'];
} else {
$return = '';
}
}
return $return;
}
function updateLicense ( $client_id, $device_id, $license_product_id, $license ) {
// see if the entry already exists
$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" );
//print "<pre>"; print_r( $results ); print "</pre>"; die;
$db_license_id = $results['data'][0]['license_id'];
$db_client_id = $results['data'][0]['client_id'];
$db_device_id = $results['data'][0]['device_id'];
$db_license_product_id = $results['data'][0]['license_product_id'];
if ( ! $results ) { # this was not found, so just add it
if ( $device_id === '' ) $device_id = 'null';
doSQL( "insert into license (client_id,device_id,license_product_id,license, added_date) values ( $client_id, $device_id, $license_product_id, '$license', now() )" );
return "Added";
}
if ( $client_id == $db_client_id && $device_id == $db_device_id ) { // already done, so just leave alone
return "Already Set";
}
if ( ! $db_device_id ) { # key was not assigned before, so just assign it
doSQL( "update license set device_id = $db_device_id,added_date = now() where license_id = $db_license_id" );
return "Assigned";
}
// 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
doSQL( "update license set removed_date = now() where license_id = $db_license_id" );
doSQL( "insert into license (client_id,device_id,license_product_id,license, added_date) values ( $client_id, $device_id, $license_product_id, '$license', now() )" );
return "Reassigned";
}
$fileInfo = new CsvImporter( $filename, true );
while ( $line = $fileInfo->get(1) ) {
foreach ( $line as $index => $values ) {
$import[$values['client']][$values['device']][$values['license_product']] = $values['license'];
}
};
//print '<pre>'; print_r( $import ); print '</pre>';
print "<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";
foreach ( $import as $client => $data ) {
$client_id = getValue ( 'client', 'client_id', 'name', $client );
if ( $client_id === '' ) {
print "<tr><td><b>Error</b></td><td colspan='7'>Could not find client ID for $client</td></tr>";
continue;
}
foreach ( $data as $machine => $info ) {
if ( $machine === '' ) {
$machine_id = '';
} else {
$machine_id = getValue( 'device', 'device_id', 'name', $machine );
if ( $machine_id === '' ) {
print "<td><b>Error</b></td><td>$client</td><td>$client_id</td>";
print "<td colspan='5'>Could not find machine ID for $machine</td></tr>";
continue;
}
}
foreach ( $info as $product => $license ) {
$product_id = getValue( 'license_product', 'license_product_id', 'name', $product, true );
$action = updateLicense( $client_id, $machine_id, $product_id, $license );
print "<tr>\n";
print "<td>$action</td><td>$client</td><td>$client_id</td>";
print "<td>$machine</td><td>$machine_id</td>";
print "<td>$product</td><td>$product_id</td><td>$license</td>\n";
print "</tr>\n";
}
} // for each machine
} // for each client
print "</table>\n";
?>