Subversion Repositories computer_asset_manager_v1

Rev

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