Subversion Repositories computer_asset_manager_v1

Rev

Rev 44 | Rev 46 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 44 Rev 45
Line 42... Line 42...
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" );
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;
43
   //print "<pre>"; print_r( $results ); print "</pre>"; die;
44
   $db_license_id = $results['data'][0]['license_id'];
44
   $db_license_id = $results['data'][0]['license_id'];
45
   $db_client_id = $results['data'][0]['client_id'];
45
   $db_client_id = $results['data'][0]['client_id'];
46
   $db_device_id = $results['data'][0]['device_id'];
46
   $db_device_id = $results['data'][0]['device_id'];
-
 
47
   // SQL does not understand an empty string, so we replace it with the keyword null for queries
47
   $db_license_product_id = $results['data'][0]['license_product_id'];
48
   $queryDeviceID = $device_id ? $device_id : 'null';
48
   if ( ! $results ) { # this was not found, so just add it
49
   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() )" );
50
      doSQL( "insert into license (client_id,device_id,license_product_id,license, added_date) values ( $client_id, $queryDeviceID, $license_product_id, '$license', now() )" );
51
      return "Added";
51
      return "Added";
52
   }
52
   }
53
   if ( $client_id == $db_client_id && $device_id == $db_device_id ) { // already done, so just leave alone
53
   if ( $client_id == $db_client_id && $device_id == $db_device_id or $db_device_id  ) { // already done, so just leave alone
54
      return "Already Set";
54
      return "Already Set";
55
   }
55
   }
56
   if ( ! $db_device_id ) { # key was not assigned before, so just assign it
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" );
57
      doSQL( "update license set device_id = $queryDeviceID,added_date = now() where license_id = $db_license_id" );
58
      return "Assigned";
58
      return "Assigned";
59
   }
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
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" );
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() )" );
62
   doSQL( "insert into license (client_id,device_id,license_product_id,license, added_date) values ( $client_id, $queryDeviceID, $license_product_id, '$license', now() )" );
63
   return "Reassigned";
63
   return "Reassigned";
64
}
64
}
65
 
65
 
66
/* import CSV, processing one line at a time
66
/* import CSV, processing one line at a time
67
 * CSV may be delimited by anything defined in CsvImporter with auto-detect
67
 * CSV may be delimited by anything defined in CsvImporter with auto-detect
68
 * returns table with results, suitable for embedding in div
68
 * returns table with results, suitable for embedding in div
69
 */
69
 */
70
 
70
 
71
   function processFile ( $filename ) {
71
   function processFile ( $filename, $delimiter, $encapsulation = '"', $escape = '\\', $deviceNotFoundIsNull = false) {
72
      $return = '';
72
      $return = '';
73
      $fileInfo = new CsvImporter( $filename, true );
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
       */
74
      while ( $line = $fileInfo->get(1) ) {
86
      while ( $line = $fileInfo->get(1) ) {
75
         foreach ( $line as $index => $values ) {
87
         foreach ( $line as $index => $values ) {
76
            $import[$values['client']][$values['device']][$values['license_product']] = $values['license'];
88
            $import[$values['client']][$values['device']][$values['license_product']] = $values['license'];
77
         }
89
         }
78
      };
90
      };
79
 
91
 
80
      // process each line in turn, displaying results in table
92
      // process each client/machine/license 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";
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";
82
      foreach ( $import as $client => $data ) {
94
      foreach ( $import as $client => $data ) {
83
         // find client_id
95
         // find client_id
84
         $client_id = getValue ( 'client', 'client_id', 'name', $client );
96
         $client_id = getValue ( 'client', 'client_id', 'name', $client );
85
         if ( $client_id === '' ) {
97
         if ( $client_id === '' ) { // can't find client, so big error
86
            $return .= "<tr><td><b>Error</b></td><td colspan='7'>Could not find client ID for $client</td></tr>";
98
            $return .= "<tr><td><b>Error</b></td><td colspan='7'>Could not find client ID for $client</td></tr>";
87
            continue;
99
            continue;
88
         }
100
         }
89
         // find machine_id
101
         // find machine_id
90
         foreach ( $data as $machine => $info ) {
102
         foreach ( $data as $machine => $info ) {
91
            if ( $machine === '' ) {
103
            if ( $machine === '' ) { // can't find machine
92
               $machine_id = '';
104
               $machine_id = '';
93
            } else {
105
            } else {
94
               $machine_id = getValue( 'device', 'device_id', 'name', $machine );
106
               $machine_id = getValue( 'device', 'device_id', 'name', $machine );
-
 
107
               // if we can't find it and they don't want to continue
95
               if ( $machine_id === '' ) {
108
               if ( $machine_id === '' and ! $deviceNotFoundIsNull ) { 
96
                  $return .= "<td><b>Error</b></td><td>$client</td><td>$client_id</td>";
109
                  $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>";
110
                  $return .= "<td colspan='5'>Could not find machine ID for $machine</td></tr>";
98
                  continue;
111
                  continue; //ignor
99
               }
112
               }
100
            }
113
            }
101
            // now, locate each license_product_id and update the license information
114
            // now, locate each license_product_id and update the license information
102
            // if product does not exist, add it
115
            // if product does not exist, add it
103
            // thus, spelling counts or you'll get dup entries in license_product table
116
            // thus, spelling counts or you'll get dup entries in license_product table
Line 128... Line 141...
128
   </head>
141
   </head>
129
   <body>
142
   <body>
130
      <?php include_once('../../menu.php'); ?>
143
      <?php include_once('../../menu.php'); ?>
131
      <div id="content">
144
      <div id="content">
132
      <?php 
145
      <?php 
133
         if ( $_REQUEST['fileToUpload'] ) {
146
         if ( $_POST["submit"] ) {
-
 
147
            $filename = tempnam( '/tmp', 'blk' );
134
            print processFile( $_FILES["fileToUpload"]["tmp_name"] );
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 );
135
         } else {
155
         } else {
136
      ?>
156
      ?>
137
      <p>Upload a CSV file to be bulk imported into the license table</p>
157
      <p align\'center'>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>
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>
139
      <form action="upload.php" method="post" enctype="multipart/form-data">
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>
140
          Select CSV to upload:
182
               <td>Select CSV to upload</td>
141
          <input type="file" name="fileToUpload" id="fileToUpload">
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>
142
          <input type="submit" value="Upload CSV" name="submit">
221
               <td colspan='2' align='center'><input type="submit" value="Upload CSV" name="submit"></td>
-
 
222
            </tr>
-
 
223
          </table>
143
      </form>
224
      </form>
-
 
225
      <p>It is generally safe to leave Encapsulation and Escape at their defaults</p>
144
      <?php } ?>
226
      <?php } ?>
145
      </div>
227
      </div>
146
   </body>
228
   </body>
147
</html>
229
</html>