Subversion Repositories computer_asset_manager_v1

Rev

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

Rev Author Line No. Line
1 rodolico 1
<?php
54 rodolico 2
   include_once( '../../header.php' );
3
   include_once( './functions.php' );
86 rodolico 4
   include_once( '../../includes/functions.php' );
54 rodolico 5
 
63 rodolico 6
 
7
   $expectedValues = array( 'file_id','device_id','site_id','client_id','file_mime_type_id','added_date', 'removed_date', 'name', 'description', 'name_on_disk' );
8
   foreach ( $expectedValues as $field )
9
      $oldValues[$field] = '';
10
   $oldValues['added_date'] = date('Y-m-d', time() );
11
   // next three lines are useless code. Picks up referrer even if we choose the menu option
12
   // maybe add a parameter to the menu option to work around it, later.
13
   $returnTo = '';
14
   if ( isset( $_REQUEST['referer'] ) ) // we came from somewhere else, just to edit one record
15
      $returnTo = $_REQUEST['referer'];
16
   $mode = 'Edit';
17
   $result = array();
18
   $debug = array();
19
   logIt( "Beginning a run\n========================\n" );
59 rodolico 20
   if ( isset( $_REQUEST['uploadfile'] ) ) {
55 rodolico 21
      // clean up the input first
63 rodolico 22
      foreach ( $expectedValues as $field )
23
         $fields[$field] = cleanInput( $_REQUEST[$field], $field == 'name' ? $_FILES['fileToUpload']['name'] : '' );
24
 
25
      $mode = $fields['file_id'] ? 'Replace' : 'Add';
26
      logIt( "Mode set to $mode" );
27
      if ( isset( $_FILES['fileToUpload']['error'] ) ) {
28
         $result = getFileUploadError( $_FILES['fileToUpload']['error'] );
29
         logIt( 'File Upload Status:<br>' . print_r( getFileUploadError( $_FILES['fileToUpload']['error'] ), true ) );
30
         if ( $_FILES['fileToUpload']['error'] == UPLOAD_ERR_NO_FILE ) {
31
            $result = array( 'valid' => true, 'message' => "Entering Edit Mode" );
32
            logIt( "Changing to Edit mode since they didn't upload a file" );
33
            $mode = 'Edit';
55 rodolico 34
         }
35
      }
63 rodolico 36
 
37
      if ( $result['valid'] && $mode == 'Replace' ) {
38
         logIt( "Replace mode, removing record $fields[file_id]" );
39
         // we are overwriting a record, so just mark the previous one removed
40
         doSQL( "update file set removed_date = now() where file_id = $fields[file_id]" );
41
         $fields['file_id'] = '';
42
         $mode = 'Add';
43
      }
44
 
45
      if ( $result['valid'] && $mode == 'Add' ) { // we are adding a file, so we upload it
46
         logIt( "Actually uploading file" );
47
         // file_mime_type_id can be passed in, calculated from the 'type' of $_FILES, or looked up by extension
48
         if ( $fields['file_mime_type_id'] == 0 ) { // we need to calculate the mime type (auto mode)
49
            // see if $_FILES has the info
50
            $fields['file_mime_type_id'] = check_mime_type( $_FILES['fileToUpload']['type'] );
51
            if ( $fields['file_mime_type_id'] === null ) { // no, so see if we can figure it out from the file name
52
               $fields['file_mime_type_id'] = check_mime_type( pathinfo($_FILES['fileToUpload']['name'], PATHINFO_EXTENSION) );
53
            }
54
         }
55
         logIt( 'Field List: ' . print_r( $fields, true ) );
56
         /*
57
          * this documentation will use the following example
58
          * A file, joe.csv, is uploaded and attached to the client
59
          * Walder IP Law. makeFileName discovers it to be file number
60
          * 123456 (based on an internal counter)
61
          * All comments below show what is returned
62
          */
63
         $result = makeFileName( 
64
                  $_FILES['fileToUpload']['name'], 
65
                  $fields['device_id'], 
66
                  $fields['client_id'], 
67
                  $fields['site_id']
68
                  );
69
         // at this point, we have 12/34/123456-c-Walder IP Law.csv
70
         if ( $result['valid'] ) {
71
            logIt( 'preparing to upload file, result = ' . print_r( $result, true ) );
72
            $fields['name_on_disk'] = $result['filename'];
73
            $result = uploadFile( $_FILES["fileToUpload"]["tmp_name"], $fields['name_on_disk'] );
74
            logIt( "After upload, result is " . print_r( $result, true ) );
54 rodolico 75
         } else {
63 rodolico 76
            $result = array( 'valid' => false, 'message' => "failed to make path<br />$saveTo" );
54 rodolico 77
         }
63 rodolico 78
      }
79
      logIt( "Just before adding file, result =\n" . print_r( $result, true ) );
80
      if ( $result['valid'] ) {
81
         $result = addfile( $fields );
82
      }
83
      /*
84
      if ( $returnTo )
85
         header( "Location: $returnTo" );
86
      */
87
   } elseif ( $_REQUEST['file_id'] ) { // if isset uploadfile
88
      $oldValues = queryDatabaseExtended( "select * from file where file_id = $_REQUEST[file_id]" );
89
      if ( $oldValues ) {
90
         $oldValues = $oldValues['data'][0];
91
         switch ( $oldValues['owner_type'] ) {
92
            case 'd' : $oldValues['device_id'] = $oldValues['owner_id'];
93
                       break;
94
            case 'c' : $oldValues['client_id'] = $oldValues['owner_id'];
95
                       break;
96
            case 's' : $oldValues['site_id'] = $oldValues['owner_id'];
97
                       break;
98
         }
99
      }
100
   }
1 rodolico 101
?>
102
<?xml version="1.0" encoding="utf-8"?>
103
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
104
<html xmlns="http://www.w3.org/1999/xhtml">
105
<head>
106
  <title>Daily Data - Computer Asset Management Program</title>
107
  <link rel="stylesheet" type="text/css" href="../../camp.css">
108
</head>
109
<body>
110
<?php include_once('../../menu.php'); ?>
111
<div id="content">
55 rodolico 112
   <?php 
63 rodolico 113
      if ( $result ) print '<h3>' . $result['valid'] ? "Success" : "Error" . $result['message'] . '</h3>';
114
      //if ( isset( $debug ) ) print "<h3>" . implode( '<br />', $debug ) . "</h3>";
55 rodolico 115
    ?>
63 rodolico 116
    <h4>Upload a File</h4>
117
    <p>Max size: <?php print maxUploadFileSize(); ?></p>
59 rodolico 118
   <p>Choose only <b>one</b> of device, site or client to add the file to</p>
1 rodolico 119
  <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
63 rodolico 120
    <input type='hidden' name='referer' value='<?php print $_SERVER['HTTP_REFERER']; ?>' />
121
    <input type='hidden' name='file_id' value='<?php print $oldValues['file_id']; ?>' />
1 rodolico 122
    <table>
123
       <tr>
124
          <td>
125
             Attach To Device
126
          </td>
127
          <td>
54 rodolico 128
             <select name='device_id' >
1 rodolico 129
                <option value='0'>------------</option>
86 rodolico 130
                <?php print queryToSelect( getAllDevices(), $oldValues['device_id'] ); ?>
1 rodolico 131
             </select>
132
          </td>
133
       </tr>
134
       <tr>
135
          <td>
136
             Attach To Site
137
          </td>
138
          <td>
54 rodolico 139
             <select name='site_id' >
1 rodolico 140
                <option value='0'>------------</option>
86 rodolico 141
                <?php print queryToSelect( getSites(), $oldValues['site_id'] ); ?>
1 rodolico 142
             </select>
143
          </td>
144
       </tr>
145
       <tr>
146
          <td>
147
             Attach To Client
148
          </td>
149
          <td>
54 rodolico 150
             <select name='client_id' >
1 rodolico 151
                <option value='0'>------------</option>
86 rodolico 152
                <?php print queryToSelect(getClients(), $oldValues['client_id'] ); ?>
1 rodolico 153
             </select>
154
          </td>
155
       </tr>
156
       <tr>
157
          <td>
158
             Mime Type
159
          </td>
160
          <td>
63 rodolico 161
             <select name='file_mime_type_id' >
54 rodolico 162
                <option value='0'>Autodetect</option>
63 rodolico 163
                <?php print queryToSelect("select file_mime_type_id, concat( extension, ' (', mime_type, ')') from file_mime_type order by extension", $oldValues['file_mime_type_id'] ); ?>
1 rodolico 164
             </select>
165
          </td>
166
       </tr>
167
       <tr>
168
          <td>
63 rodolico 169
             File Date
1 rodolico 170
          </td>
171
          <td>
63 rodolico 172
             <input type='text' name='added_date' width='20' value='<?php print $oldValues['added_date']; ?>' >
1 rodolico 173
          </td>
174
       </tr>
175
       <tr>
63 rodolico 176
          <td>
177
             Removed Date
178
          </td>
179
          <td>
180
             <input type='text' name='removed_date' width='20' value='<?php print $oldValues['removed_date']; ?>' >
181
          </td>
182
       </tr>
183
       <tr>
184
          <td>
185
             File Name
186
          </td>
187
          <td>
188
             <input type="text" name="name" size="30" maxlength="64" value='<?php print $oldValues['name']; ?>' />
189
          </td>
190
       </tr>
191
       <tr>
55 rodolico 192
          <td valign='top'>
1 rodolico 193
             Description
194
          </td>
195
          <td>
63 rodolico 196
             <textarea name="description" cols='50' rows='5'><?php print $oldValues['description']; ?></textarea>
1 rodolico 197
          </td>
198
       </tr>
199
       <tr>
200
          <td>
201
             Choose File
202
          </td>
203
          <td>
54 rodolico 204
             <input name="fileToUpload" type="file" />
1 rodolico 205
          </td>
206
       </tr>
207
       <tr>
208
          <td colspan='2' align="center">
59 rodolico 209
             <input type="submit" name="uploadfile" value="Upload" />
1 rodolico 210
          </td>
211
       </tr>
212
    </table>
213
  </form>
214
</div>
215
 
216
</body>
217
</html>
218