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'];
|
45 |
rodolico |
47 |
// SQL does not understand an empty string, so we replace it with the keyword null for queries
|
|
|
48 |
$queryDeviceID = $device_id ? $device_id : 'null';
|
42 |
rodolico |
49 |
if ( ! $results ) { # this was not found, so just add it
|
45 |
rodolico |
50 |
doSQL( "insert into license (client_id,device_id,license_product_id,license, added_date) values ( $client_id, $queryDeviceID, $license_product_id, '$license', now() )" );
|
42 |
rodolico |
51 |
return "Added";
|
|
|
52 |
}
|
45 |
rodolico |
53 |
if ( $client_id == $db_client_id && $device_id == $db_device_id or $db_device_id ) { // already done, so just leave alone
|
42 |
rodolico |
54 |
return "Already Set";
|
|
|
55 |
}
|
|
|
56 |
if ( ! $db_device_id ) { # key was not assigned before, so just assign it
|
45 |
rodolico |
57 |
doSQL( "update license set device_id = $queryDeviceID,added_date = now() where license_id = $db_license_id" );
|
42 |
rodolico |
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" );
|
45 |
rodolico |
62 |
doSQL( "insert into license (client_id,device_id,license_product_id,license, added_date) values ( $client_id, $queryDeviceID, $license_product_id, '$license', now() )" );
|
42 |
rodolico |
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 |
|
45 |
rodolico |
71 |
function processFile ( $filename, $delimiter, $encapsulation = '"', $escape = '\\', $deviceNotFoundIsNull = false) {
|
43 |
rodolico |
72 |
$return = '';
|
45 |
rodolico |
73 |
$fileInfo = new CsvImporter( $filename, true, $delimiter, $enclosure, $escape );
|
|
|
74 |
|
|
|
75 |
/*
|
|
|
76 |
* load the information in a hash list, similar to
|
|
|
77 |
* client1
|
|
|
78 |
* machine1
|
|
|
79 |
* product1
|
|
|
80 |
* product2
|
|
|
81 |
* machine2
|
|
|
82 |
* productx
|
|
|
83 |
* client2
|
|
|
84 |
* ...
|
|
|
85 |
*/
|
43 |
rodolico |
86 |
while ( $line = $fileInfo->get(1) ) {
|
|
|
87 |
foreach ( $line as $index => $values ) {
|
|
|
88 |
$import[$values['client']][$values['device']][$values['license_product']] = $values['license'];
|
|
|
89 |
}
|
|
|
90 |
};
|
42 |
rodolico |
91 |
|
45 |
rodolico |
92 |
// process each client/machine/license in turn, displaying results in table
|
|
|
93 |
$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 |
94 |
foreach ( $import as $client => $data ) {
|
|
|
95 |
// find client_id
|
|
|
96 |
$client_id = getValue ( 'client', 'client_id', 'name', $client );
|
45 |
rodolico |
97 |
if ( $client_id === '' ) { // can't find client, so big error
|
43 |
rodolico |
98 |
$return .= "<tr><td><b>Error</b></td><td colspan='7'>Could not find client ID for $client</td></tr>";
|
41 |
rodolico |
99 |
continue;
|
|
|
100 |
}
|
43 |
rodolico |
101 |
// find machine_id
|
|
|
102 |
foreach ( $data as $machine => $info ) {
|
45 |
rodolico |
103 |
if ( $machine === '' ) { // can't find machine
|
43 |
rodolico |
104 |
$machine_id = '';
|
|
|
105 |
} else {
|
|
|
106 |
$machine_id = getValue( 'device', 'device_id', 'name', $machine );
|
45 |
rodolico |
107 |
// if we can't find it and they don't want to continue
|
|
|
108 |
if ( $machine_id === '' and ! $deviceNotFoundIsNull ) {
|
43 |
rodolico |
109 |
$return .= "<td><b>Error</b></td><td>$client</td><td>$client_id</td>";
|
|
|
110 |
$return .= "<td colspan='5'>Could not find machine ID for $machine</td></tr>";
|
45 |
rodolico |
111 |
continue; //ignor
|
43 |
rodolico |
112 |
}
|
|
|
113 |
}
|
|
|
114 |
// now, locate each license_product_id and update the license information
|
|
|
115 |
// if product does not exist, add it
|
|
|
116 |
// thus, spelling counts or you'll get dup entries in license_product table
|
|
|
117 |
foreach ( $info as $product => $license ) {
|
|
|
118 |
$product_id = getValue( 'license_product', 'license_product_id', 'name', $product, true );
|
|
|
119 |
$action = updateLicense( $client_id, $machine_id, $product_id, $license );
|
|
|
120 |
$return .= "<tr>\n";
|
|
|
121 |
$return .= "<td>$action</td><td>$client</td><td>$client_id</td>";
|
|
|
122 |
$return .= "<td>$machine</td><td>$machine_id</td>";
|
|
|
123 |
$return .= "<td>$product</td><td>$product_id</td><td>$license</td>\n";
|
|
|
124 |
$return .= "</tr>\n";
|
|
|
125 |
|
|
|
126 |
}
|
|
|
127 |
} // for each machine
|
|
|
128 |
} // for each client
|
|
|
129 |
$return .= "</table>\n";
|
|
|
130 |
return $return;
|
|
|
131 |
} // function processFile
|
41 |
rodolico |
132 |
|
|
|
133 |
?>
|
43 |
rodolico |
134 |
<?xml version="1.0" encoding="utf-8"?>
|
|
|
135 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
|
|
|
136 |
|
|
|
137 |
<html xmlns="http://www.w3.org/1999/xhtml">
|
|
|
138 |
<head>
|
|
|
139 |
<title>Computer Asset Management Program - MODULE NAME - PAGE NAME</title>
|
|
|
140 |
<link rel="stylesheet" type="text/css" href="../../camp.css">
|
|
|
141 |
</head>
|
|
|
142 |
<body>
|
|
|
143 |
<?php include_once('../../menu.php'); ?>
|
|
|
144 |
<div id="content">
|
|
|
145 |
<?php
|
45 |
rodolico |
146 |
if ( $_POST["submit"] ) {
|
|
|
147 |
$filename = tempnam( '/tmp', 'blk' );
|
|
|
148 |
if ( move_uploaded_file( $_FILES["fileToUpload"]["tmp_name"], $filename ) ) {
|
|
|
149 |
print "<p>Loading Data</p>\n";
|
|
|
150 |
print processFile( $filename , $_REQUEST['delimiter'], $_REQUEST['encapsulation'], $_REQUEST['escape'], $_REQUEST['notfoundisnull'] );
|
|
|
151 |
} else {
|
|
|
152 |
print "<p>Error: move_uploaded_file failed to move to $filename</p>";
|
|
|
153 |
}
|
|
|
154 |
//unlink( $filename );
|
43 |
rodolico |
155 |
} else {
|
|
|
156 |
?>
|
45 |
rodolico |
157 |
<p align\'center'>Upload a CSV file to be bulk imported into the license table</p>
|
|
|
158 |
<p><b>Warning:</b> no error checking is done, ensure your file meets the proper specifications. The fields may be separated by</p>
|
|
|
159 |
<ul>
|
|
|
160 |
<li>comma</li>
|
|
|
161 |
<li>semicolon</li>
|
|
|
162 |
<li>tab</li>
|
|
|
163 |
<li>pipe</li>
|
|
|
164 |
<li>colon</li>
|
|
|
165 |
</ul>
|
|
|
166 |
<p>The first line should be a header, containing the following in any order. All other fields will be ignored</p>
|
|
|
167 |
<ul>
|
|
|
168 |
<li>client</li>
|
|
|
169 |
<li>device</li>
|
|
|
170 |
<li>license_product</li>
|
|
|
171 |
<li>license</li>
|
|
|
172 |
</ul>
|
|
|
173 |
<p><b>Warning!</b> Client, Machine and Product name must be exactly as already stored.</p>
|
|
|
174 |
<p>License is case sensitive</p>
|
|
|
175 |
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" enctype="multipart/form-data">
|
|
|
176 |
<table border='1'>
|
|
|
177 |
<tr>
|
|
|
178 |
<td>Set Machine to Null if not found</td>
|
|
|
179 |
<td> <input type="checkbox" name="notfoundisnull" value="notfoundisnull"></td>
|
|
|
180 |
</tr>
|
|
|
181 |
<tr>
|
|
|
182 |
<td>Select CSV to upload</td>
|
|
|
183 |
<td><input type="file" name="fileToUpload" id="fileToUpload"></td>
|
|
|
184 |
</tr>
|
|
|
185 |
<tr>
|
|
|
186 |
<td>Field Delimiters</td>
|
|
|
187 |
<td>
|
|
|
188 |
<select name='delimiter' id='delimiter'>
|
|
|
189 |
<option value='auto' selected>Auto Detect</option>
|
|
|
190 |
<option value='tab'>Tab</option>
|
|
|
191 |
<option value=','>Comma (,)</option>
|
|
|
192 |
<option value=';'>Semicolon (;)</option>
|
|
|
193 |
<option value='|'>Pipe (|)</option>
|
|
|
194 |
<option value=':'>Colon (:)</option>
|
|
|
195 |
</select>
|
|
|
196 |
|
|
|
197 |
</td>
|
|
|
198 |
</tr>
|
|
|
199 |
<tr>
|
|
|
200 |
<td>Encapsulation</td>
|
|
|
201 |
<td>
|
|
|
202 |
<select name='encapsulation' id='encapsulation'>
|
|
|
203 |
<option value='auto' selected>Auto Detect</option>
|
|
|
204 |
<option value=''>None</option>
|
|
|
205 |
<option value='"'>Double Quotes (")</option>
|
|
|
206 |
<option value="'">Single Quotes (')</option>
|
|
|
207 |
<option value='~'>Tilde (~)</option>
|
|
|
208 |
</select>
|
|
|
209 |
</td>
|
|
|
210 |
</tr>
|
|
|
211 |
<tr>
|
|
|
212 |
<td>Escape Char</td>
|
|
|
213 |
<td>
|
|
|
214 |
<select name='escape' id='escape'>
|
|
|
215 |
<option value='' selected>None</option>
|
|
|
216 |
<option value='\'>Backslash (\)</option>
|
|
|
217 |
</select>
|
|
|
218 |
</td>
|
|
|
219 |
</tr>
|
|
|
220 |
<tr>
|
|
|
221 |
<td colspan='2' align='center'><input type="submit" value="Upload CSV" name="submit"></td>
|
|
|
222 |
</tr>
|
|
|
223 |
</table>
|
43 |
rodolico |
224 |
</form>
|
45 |
rodolico |
225 |
<p>It is generally safe to leave Encapsulation and Escape at their defaults</p>
|
43 |
rodolico |
226 |
<?php } ?>
|
|
|
227 |
</div>
|
|
|
228 |
</body>
|
|
|
229 |
</html>
|