Subversion Repositories computer_asset_manager_v1

Rev

Rev 63 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

<?php
   include_once( '../../header.php' );
   include_once( './functions.php' );
   include_once( '../../includes/functions.php' );


   $expectedValues = array( 'file_id','device_id','site_id','client_id','file_mime_type_id','added_date', 'removed_date', 'name', 'description', 'name_on_disk' );
   foreach ( $expectedValues as $field )
      $oldValues[$field] = '';
   $oldValues['added_date'] = date('Y-m-d', time() );
   // next three lines are useless code. Picks up referrer even if we choose the menu option
   // maybe add a parameter to the menu option to work around it, later.
   $returnTo = '';
   if ( isset( $_REQUEST['referer'] ) ) // we came from somewhere else, just to edit one record
      $returnTo = $_REQUEST['referer'];
   $mode = 'Edit';
   $result = array();
   $debug = array();
   logIt( "Beginning a run\n========================\n" );
   if ( isset( $_REQUEST['uploadfile'] ) ) {
      // clean up the input first
      foreach ( $expectedValues as $field )
         $fields[$field] = cleanInput( $_REQUEST[$field], $field == 'name' ? $_FILES['fileToUpload']['name'] : '' );
      
      $mode = $fields['file_id'] ? 'Replace' : 'Add';
      logIt( "Mode set to $mode" );
      if ( isset( $_FILES['fileToUpload']['error'] ) ) {
         $result = getFileUploadError( $_FILES['fileToUpload']['error'] );
         logIt( 'File Upload Status:<br>' . print_r( getFileUploadError( $_FILES['fileToUpload']['error'] ), true ) );
         if ( $_FILES['fileToUpload']['error'] == UPLOAD_ERR_NO_FILE ) {
            $result = array( 'valid' => true, 'message' => "Entering Edit Mode" );
            logIt( "Changing to Edit mode since they didn't upload a file" );
            $mode = 'Edit';
         }
      }

      if ( $result['valid'] && $mode == 'Replace' ) {
         logIt( "Replace mode, removing record $fields[file_id]" );
         // we are overwriting a record, so just mark the previous one removed
         doSQL( "update file set removed_date = now() where file_id = $fields[file_id]" );
         $fields['file_id'] = '';
         $mode = 'Add';
      }
      
      if ( $result['valid'] && $mode == 'Add' ) { // we are adding a file, so we upload it
         logIt( "Actually uploading file" );
         // file_mime_type_id can be passed in, calculated from the 'type' of $_FILES, or looked up by extension
         if ( $fields['file_mime_type_id'] == 0 ) { // we need to calculate the mime type (auto mode)
            // see if $_FILES has the info
            $fields['file_mime_type_id'] = check_mime_type( $_FILES['fileToUpload']['type'] );
            if ( $fields['file_mime_type_id'] === null ) { // no, so see if we can figure it out from the file name
               $fields['file_mime_type_id'] = check_mime_type( pathinfo($_FILES['fileToUpload']['name'], PATHINFO_EXTENSION) );
            }
         }
         logIt( 'Field List: ' . print_r( $fields, true ) );
         /*
          * this documentation will use the following example
          * A file, joe.csv, is uploaded and attached to the client
          * Walder IP Law. makeFileName discovers it to be file number
          * 123456 (based on an internal counter)
          * All comments below show what is returned
          */
         $result = makeFileName( 
                  $_FILES['fileToUpload']['name'], 
                  $fields['device_id'], 
                  $fields['client_id'], 
                  $fields['site_id']
                  );
         // at this point, we have 12/34/123456-c-Walder IP Law.csv
         if ( $result['valid'] ) {
            logIt( 'preparing to upload file, result = ' . print_r( $result, true ) );
            $fields['name_on_disk'] = $result['filename'];
            $result = uploadFile( $_FILES["fileToUpload"]["tmp_name"], $fields['name_on_disk'] );
            logIt( "After upload, result is " . print_r( $result, true ) );
         } else {
            $result = array( 'valid' => false, 'message' => "failed to make path<br />$saveTo" );
         }
      }
      logIt( "Just before adding file, result =\n" . print_r( $result, true ) );
      if ( $result['valid'] ) {
         $result = addfile( $fields );
      }
      /*
      if ( $returnTo )
         header( "Location: $returnTo" );
      */
   } elseif ( $_REQUEST['file_id'] ) { // if isset uploadfile
      $oldValues = queryDatabaseExtended( "select * from file where file_id = $_REQUEST[file_id]" );
      if ( $oldValues ) {
         $oldValues = $oldValues['data'][0];
         switch ( $oldValues['owner_type'] ) {
            case 'd' : $oldValues['device_id'] = $oldValues['owner_id'];
                       break;
            case 'c' : $oldValues['client_id'] = $oldValues['owner_id'];
                       break;
            case 's' : $oldValues['site_id'] = $oldValues['owner_id'];
                       break;
         }
      }
   }
?>
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>Daily Data - Computer Asset Management Program</title>
  <link rel="stylesheet" type="text/css" href="../../camp.css">
</head>
<body>
<?php include_once('../../menu.php'); ?>
<div id="content">
   <?php 
      if ( $result ) print '<h3>' . $result['valid'] ? "Success" : "Error" . $result['message'] . '</h3>';
      //if ( isset( $debug ) ) print "<h3>" . implode( '<br />', $debug ) . "</h3>";
    ?>
    <h4>Upload a File</h4>
    <p>Max size: <?php print maxUploadFileSize(); ?></p>
   <p>Choose only <b>one</b> of device, site or client to add the file to</p>
  <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
    <input type='hidden' name='referer' value='<?php print $_SERVER['HTTP_REFERER']; ?>' />
    <input type='hidden' name='file_id' value='<?php print $oldValues['file_id']; ?>' />
    <table>
       <tr>
          <td>
             Attach To Device
          </td>
          <td>
             <select name='device_id' >
                <option value='0'>------------</option>
                <?php print queryToSelect( getAllDevices(), $oldValues['device_id'] ); ?>
             </select>
          </td>
       </tr>
       <tr>
          <td>
             Attach To Site
          </td>
          <td>
             <select name='site_id' >
                <option value='0'>------------</option>
                <?php print queryToSelect( getSites(), $oldValues['site_id'] ); ?>
             </select>
          </td>
       </tr>
       <tr>
          <td>
             Attach To Client
          </td>
          <td>
             <select name='client_id' >
                <option value='0'>------------</option>
                <?php print queryToSelect(getClients(), $oldValues['client_id'] ); ?>
             </select>
          </td>
       </tr>
       <tr>
          <td>
             Mime Type
          </td>
          <td>
             <select name='file_mime_type_id' >
                <option value='0'>Autodetect</option>
                <?php print queryToSelect("select file_mime_type_id, concat( extension, ' (', mime_type, ')') from file_mime_type order by extension", $oldValues['file_mime_type_id'] ); ?>
             </select>
          </td>
       </tr>
       <tr>
          <td>
             File Date
          </td>
          <td>
             <input type='text' name='added_date' width='20' value='<?php print $oldValues['added_date']; ?>' >
          </td>
       </tr>
       <tr>
          <td>
             Removed Date
          </td>
          <td>
             <input type='text' name='removed_date' width='20' value='<?php print $oldValues['removed_date']; ?>' >
          </td>
       </tr>
       <tr>
          <td>
             File Name
          </td>
          <td>
             <input type="text" name="name" size="30" maxlength="64" value='<?php print $oldValues['name']; ?>' />
          </td>
       </tr>
       <tr>
          <td valign='top'>
             Description
          </td>
          <td>
             <textarea name="description" cols='50' rows='5'><?php print $oldValues['description']; ?></textarea>
          </td>
       </tr>
       <tr>
          <td>
             Choose File
          </td>
          <td>
             <input name="fileToUpload" type="file" />
          </td>
       </tr>
       <tr>
          <td colspan='2' align="center">
             <input type="submit" name="uploadfile" value="Upload" />
          </td>
       </tr>
    </table>
  </form>
</div>

</body>
</html>