Rev 42 | 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;
}
$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 );
print "<tr>\n";
print "<td>Adding</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";
if ( $machine_id === '' ) # allow licenses which are not assigned a machine
$machine_id = 'null';
doSQL( "insert into license (client_id,device_id,license_product_id,license) values ( $client_id, $machine_id, $product_id, '$license' )" );
}
} // for each machine
} // for each client
print "</table>\n";
?>