Subversion Repositories computer_asset_manager_v1

Rev

Rev 46 | Rev 69 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
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 ) {
47 rodolico 62
               # look for product name, but don't update it if it doesn't exist
63
               $product_id = getValue( 'license_product', 'license_product_id', 'name', $product, false );
64
               /*
65
                * Some software (like Belarc) show product names as publiser - product name, ie
66
                * Microsoft - Office 2017
67
                * So, we'll look to see if this is the case here.
68
                * NOTE: we are only checking if this key is publisher - product and, if so checking for product
69
                *       however, if it is listed in database as publisher - product and we are just product, it 
70
                *       will still result in dups.
71
                */
72
               if ( ! $product_id ) { 
73
                  if ( strpos( $product, ' - ' ) !== false ) {
74
                     $product_id = getValue( 'license_product', 'license_product_id', 'name', substr( $product, strpos( $product, ' - ' ) + 3 ), false );
75
                  }
76
               }
77
               // if we still haven't found it, just add the bloody thing.
78
               if ( ! $product_id )
79
                  $product_id = getValue( 'license_product', 'license_product_id', 'name', $product, true );
80
 
43 rodolico 81
               $action = updateLicense( $client_id, $machine_id, $product_id, $license );
82
               $return .= "<tr>\n";
83
               $return .= "<td>$action</td><td>$client</td><td>$client_id</td>";
84
               $return .= "<td>$machine</td><td>$machine_id</td>";
85
               $return .= "<td>$product</td><td>$product_id</td><td>$license</td>\n";
86
               $return .= "</tr>\n";
87
 
88
            }
89
         } // for each machine
90
      } // for each client
91
      $return .= "</table>\n";
92
      return $return;
93
   } // function processFile
41 rodolico 94
 
95
?>
43 rodolico 96
<?xml version="1.0" encoding="utf-8"?>
97
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
98
 
99
<html xmlns="http://www.w3.org/1999/xhtml">
100
   <head>
101
      <title>Computer Asset Management Program - MODULE NAME - PAGE NAME</title>
102
      <link rel="stylesheet" type="text/css" href="../../camp.css">
103
   </head>
104
   <body>
105
      <?php include_once('../../menu.php'); ?>
106
      <div id="content">
107
      <?php 
47 rodolico 108
         if ( isset( $_POST["submit"] ) ) {
45 rodolico 109
            $filename = tempnam( '/tmp', 'blk' );
110
            if ( move_uploaded_file( $_FILES["fileToUpload"]["tmp_name"], $filename ) ) {
111
               print "<p>Loading Data</p>\n";
112
               print processFile( $filename , $_REQUEST['delimiter'], $_REQUEST['encapsulation'], $_REQUEST['escape'], $_REQUEST['notfoundisnull']  );
113
            } else {
114
               print "<p>Error: move_uploaded_file failed to move to $filename</p>";
115
            }
116
            //unlink( $filename );
43 rodolico 117
         } else {
118
      ?>
45 rodolico 119
      <p align\'center'>Upload a CSV file to be bulk imported into the license table</p>
120
      <p><b>Warning:</b> no error checking is done, ensure your file meets the proper specifications. The fields may be separated by</p>
121
      <ul>
122
         <li>comma</li>
123
         <li>semicolon</li>
124
         <li>tab</li>
125
         <li>pipe</li>
126
         <li>colon</li>
127
      </ul>
128
      <p>The first line should be a header, containing the following in any order. All other fields will be ignored</p>
129
      <ul>
130
         <li>client</li>
131
         <li>device</li>
132
         <li>license_product</li>
133
         <li>license</li>
134
      </ul>
135
      <p><b>Warning!</b> Client, Machine and Product name must be exactly as already stored.</p>
136
      <p>License is case sensitive</p>
137
      <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" enctype="multipart/form-data">
138
         <table border='1'>
139
            <tr>
140
               <td>Set Machine to Null if not found</td>
141
               <td> <input type="checkbox" name="notfoundisnull" value="notfoundisnull"></td>
142
            </tr>
143
            <tr>
144
               <td>Select CSV to upload</td>
145
               <td><input type="file" name="fileToUpload" id="fileToUpload"></td>
146
            </tr>
147
            <tr>
148
               <td>Field Delimiters</td>
149
               <td>
150
                  <select name='delimiter' id='delimiter'>
151
                     <option value='auto' selected>Auto Detect</option>
152
                     <option value='tab'>Tab</option>
153
                     <option value=','>Comma (,)</option>
154
                     <option value=';'>Semicolon (;)</option>
155
                     <option value='|'>Pipe (|)</option>
156
                     <option value=':'>Colon (:)</option>
157
                  </select>
158
 
159
               </td>
160
            </tr>
161
            <tr>
162
               <td>Encapsulation</td>
163
               <td>
164
                  <select name='encapsulation' id='encapsulation'>
165
                     <option value='auto' selected>Auto Detect</option>
166
                     <option value=''>None</option>
167
                     <option value='"'>Double Quotes (")</option>
168
                     <option value="'">Single Quotes (')</option>
169
                     <option value='~'>Tilde (~)</option>
170
                  </select>
171
               </td>
172
            </tr>
173
            <tr>
174
               <td>Escape Char</td>
175
               <td>
176
                  <select name='escape' id='escape'>
177
                     <option value='' selected>None</option>
178
                     <option value='\'>Backslash (\)</option>
179
                  </select>
180
               </td>
181
            </tr>
182
            <tr>
183
               <td colspan='2' align='center'><input type="submit" value="Upload CSV" name="submit"></td>
184
            </tr>
185
          </table>
43 rodolico 186
      </form>
45 rodolico 187
      <p>It is generally safe to leave Encapsulation and Escape at their defaults</p>
43 rodolico 188
      <?php } ?>
189
      </div>
190
   </body>
191
</html>