/[drupal]/contributions/modules/docapi/docapi.module
ViewVC logotype

Diff of /contributions/modules/docapi/docapi.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.2.2.6, Tue Jul 8 06:01:48 2008 UTC revision 1.2.2.7, Sat Jul 12 19:24:49 2008 UTC
# Line 1  Line 1 
1  <?php  <?php
2    // $Id: docapi.module,v 1.2.2.5 2008/07/08 04:26:39 bradfordcp Exp $    // $Id: docapi.module,v 1.2.2.6 2008/07/08 06:01:48 bradfordcp Exp $
3    
4    /**    /**
5     * @file     * @file
# Line 28  Line 28 
28        'access arguments' => array('import documents to library'),        'access arguments' => array('import documents to library'),
29      );      );
30    
31        $items['doc/library'] = array(
32          'title' => 'Document Library',
33          'description' => 'The collection of uploaded documents',
34          'page callback' => 'docapi_library',
35          'page_arguments' => array(),
36          'access arguments' => array(),
37        );
38    
39      // The item to load a document from the library      // The item to load a document from the library
40      /*$items['node/add/lib'] = array(      /*$items['node/add/lib'] = array(
41        'title' => t('Load Library Item'),        'title' => t('Load Library Item'),
# Line 159  Line 167 
167        '#type' => 'textfield',        '#type' => 'textfield',
168        '#title' => t('DocAPI file save path'),        '#title' => t('DocAPI file save path'),
169        '#description' => t('The path the location where the imported files will be placed.'),        '#description' => t('The path the location where the imported files will be placed.'),
170        '#default_value' => variable_get('docapi_file_path', 'sites/default/files/docapi_library'),        '#default_value' => variable_get('docapi_file_path', file_directory_path() .'/docapi'),
171      );      );
172    
173      $form['docapi_plugin_settings'] = array(      $form['docapi_plugin_settings'] = array(
# Line 199  Line 207 
207     * DocAPI Upload document form     * DocAPI Upload document form
208     */     */
209    function docapi_doc_add_form($form_state) {    function docapi_doc_add_form($form_state) {
210        drupal_set_message('FORM STATE: <pre>'. var_export($form_state, true) .'</pre>');
211    
212      $form = array();      $form = array();
213      $form['#attributes'] = array('enctype' => "multipart/form-data");      $form['#attributes'] = array('enctype' => "multipart/form-data");
214    
215      if (!$form_state['submitted']  && !(array_key_exists('values', $form_state) && array_key_exists('docapi_file_path', $form_state['values']))) {      if (!$form_state['submitted']  && !(array_key_exists('values', $form_state) && array_key_exists('docapi_obj_file', $form_state['values']))) {
216        $form['docapi_file'] = array(        $form['docapi_file'] = array(
217          '#type' => 'file',          '#type' => 'file',
218          '#title' => t('The document to import'),          '#title' => t('The document to import'),
219          '#description' => t(''),          '#description' => t(''),
220          '#default_value' => '',          '#default_value' => '',
221        );        );
222        $form['docapi_file_path'] = array(        $form['docapi_obj_file'] = array(
         '#type' => 'value',  
         '#value' => '',  
       );  
       $form['docapi_file_type'] = array(  
         '#type' => 'value',  
         '#value' => '',  
       );  
       $form['docapi_file_name'] = array(  
223          '#type' => 'value',          '#type' => 'value',
224          '#value' => '',          '#value' => '',
225        );        );
226      }      }
227      else {      else {
228        drupal_set_message(t('The uploaded file has been stored, please continue correcting any errors.'));        if ($form_state['values']['docapi_obj_file'] == 0) {
229            $form['docapi_file'] = array(
230              '#type' => 'file',
231              '#title' => t('The document to import'),
232              '#description' => t(''),
233              '#default_value' => '',
234            );
235          }
236          else {
237            drupal_set_message(t('The uploaded file has been stored, please continue correcting any errors.'));
238          }
239      }      }
240    
241      $form['docapi_advanced_settings'] = array(      $form['docapi_advanced_settings'] = array(
# Line 254  Line 266 
266     */     */
267    function docapi_doc_add_form_validate($form, &$form_state) {    function docapi_doc_add_form_validate($form, &$form_state) {
268      // Verify there is a file to be validated      // Verify there is a file to be validated
269      if (!empty($_FILES['files']['name']['docapi_file'])) {      if (!empty($_FILES['files']['name']['docapi_file']) || !empty($form_state['values']['docapi_obj_file'])) {
270          $file = 0;
271    
272        // Set the path to the tmp file, if a validation error occurs and the user corrects it, they do not need to upload the file again.        // Set the path to the tmp file, if a validation error occurs and the user corrects it, they do not need to upload the file again.
273        if (empty($form_state['values']['docapi_file_path'])) {        if (empty($form_state['values']['docapi_obj_file'])) {
274          form_set_value($form['docapi_file_path'], $_FILES['files']['tmp_name']['docapi_file'], $form_state);          $file = file_save_upload('docapi_file');
275          form_set_value($form['docapi_file_type'], $_FILES['files']['type']['docapi_file'], $form_state);          form_set_value($form['docapi_obj_file'], $file, $form_state);
276          form_set_value($form['docapi_file_name'], $_FILES['files']['name']['docapi_file'], $form_state);  
277        }        }
278        else {        else {
279          // agentrickard: I am not sure of what to put here...          $file = $form_state['values']['docapi_obj_file'];
280        }        }
281    
282        // Load a list of possible modules        if ($file == 0) {
283        $result = db_query("SELECT dp.module FROM {docapi_plugins} dp INNER JOIN {docapi_plugin_mimetypes} dpm ON dpm.plugin_id = dp.plugin_id WHERE dpm.mimetype = '%s' ORDER BY dpm.weight ASC", $form_state['values']['docapi_file_type']);          // Load a list of possible modules
284            $result = db_query("SELECT dp.module FROM {docapi_plugins} dp INNER JOIN {docapi_plugin_mimetypes} dpm ON dpm.plugin_id = dp.plugin_id WHERE dpm.mimetype = '%s' ORDER BY dpm.weight ASC", $file->filemime);
285        // Load the initial parser (Note: This may be set to AUTO by default)  
286        $module = $form_state['values']['docapi_doc_parser'];          // Load the initial parser (Note: This may be set to AUTO by default)
287            $module = $form_state['values']['docapi_doc_parser'];
288        // If no module was specified load the first result  
289        if ($module == 'AUTO') {          // If no module was specified load the first result
290          $module = db_result($result);          if ($module == 'AUTO') {
291              $module = db_result($result);
292            }
293    
294            // Validate the file against all available parsers, (Note: Should the selected module fail validation a check will be made by mimetype as well.)
295            do {
296              // Call the module's validate function and break if the function returns true
297              if (module_invoke($module, 'docapi_validate_file', $file->filepath, $file->filemime))
298                break;
299            } while ($module = db_result($result));
300    
301            if ($module == null) {
302              // A module was not found, suggest the user select the parser manually
303              form_set_error('docapi_doc_parser', t('A valid parser was not found, please manually select from the list of available parsers.'));
304              $form_state['rebuild'] = true;
305            }
306            else {
307              // Store the module name, as the select drop down response
308              form_set_value($form['docapi_advanced_settings']['docapi_doc_parser'], $module, $form_state);
309            }
310        }        }
   
       // Validate the file against all available parsers, (Note: Should the selected module fail validation a check will be made by mimetype as well.)  
       do {  
         // Call the module's validate function and break if the function returns true  
         if (module_invoke($module, 'docapi_validate_file', $form_state['values']['docapi_file_path'], $form_state['values']['docapi_file_type']))  
           break;  
       } while ($module = db_result($result));  
   
       if ($module == null) {  
         // A module was not found, suggest the user select the parser manually  
         form_set_error('docapi_doc_parser', t('A valid parser was not found, please manually select from the list of available parsers.'));  
         $form_state['rebuild'] = true;  
       }  
311        else {        else {
312          // Store the module name, as the select drop down response          // The file was not moved, re-build the form
313          form_set_value($form['docapi_advanced_settings']['docapi_doc_parser'], $module, $form_state);          form_set_error('docapi_file', t('Error parsing file, please try again.'));
314            $form_state['rebuild'] = true;
315        }        }
316      }      }
317      else {      else {
# Line 309  Line 329 
329      // Begin file processing once it has cleared all of the validation      // Begin file processing once it has cleared all of the validation
330    
331      // Move the file to the its location within the 'docapi_file_path'      // Move the file to the its location within the 'docapi_file_path'
332      if (!move_uploaded_file($form_state['values']['docapi_file_path'], realpath(variable_get('docapi_file_path', 'sites/default/files/docapi_library')) .'/'. basename($form_state['values']['docapi_file_name']))) {      drupal_set_message(t('Successfully moved file into working directory.'));
333        form_set_error('docapi_file', t('Could not move file into the upload directory.'));  
334      }      // Add the file to the library
335      else {      global $user;
336        drupal_set_message(t('Successfully moved file into working directory.'));  
337        // Set the variables
338        // Add the file to the library      $file = $form_state['values']['docapi_obj_file'];
339        global $user;      $module = $form_state['values']['docapi_doc_parser'];
340        $plugin_id = db_result(db_query("SELECT plugin_id FROM {docapi_plugins} WHERE module = '%s'", $module));
341        // Set the variables  
342        $uid = $user->uid;      // Insert into docapi_library
343        $module = $form_state['values']['docapi_doc_parser'];      db_query("INSERT INTO {docapi_library} (plugin_id, fid) VALUES (%d, %d)", $plugin_id, $file->fid);
344        $plugin_id = db_result(db_query("SELECT plugin_id FROM {docapi_plugins} WHERE module = '%s'", $module));      $doc_id = db_last_insert_id('docapi_library', 'doc_id');
345        $file_mime = $form_state['values']['docapi_file_type'];  
346        $file_name = basename($form_state['values']['docapi_file_name']);      // Load the metadata and insert it into docapi_metadata
347        $file_path = realpath(variable_get('docapi_file_path', 'sites/default/files/docapi_library')) .'/'. basename($file_name);      $metadata = module_invoke($module, 'docapi_metadata', $file->filepath, $file->filemime);
348        $file_size = filesize($file_path);      foreach ($metadata as $meta => $data) {
349          db_query("INSERT INTO {docapi_metadata} (doc_id, field, meta_value) VALUES (%d, '%s', %b)", $doc_id, $meta, $data);
       // Insert into docapi_library  
       db_query("INSERT INTO {docapi_library} (plugin_id, uid, filemime, filename, filepath, filesize, created, changed) VALUES (%d, %d, '%s', '%s', '%s', %d, %d, %d)", $plugin_id, $uid, $file_mime, $file_name, $file_path, $file_size, time(), time());  
       $doc_id = db_last_insert_id('docapi_library', 'doc_id');  
   
       // Load the metadata and insert it into docapi_metadata  
       $metadata = module_invoke($module, 'docapi_metadata', $file_path, $file_mime);  
       foreach ($metadata as $meta => $data) {  
         db_query("INSERT INTO {docapi_metadata} (doc_id, field, meta_value) VALUES (%d, '%s', %b)", $doc_id, $meta, $data);  
       }  
   
       drupal_set_message(t('Sucessfully imported %file', array('%file' => $file_name)));  
350      }      }
351    
352        drupal_set_message(t('Sucessfully imported %file', array('%file' => $file->filename)));
353      }
354    
355      /**
356       * Displays a list of all documents that have been imported
357       */
358      function docapi_library() {
359        $ret = '';
360    
361        return $ret;
362    }    }

Legend:
Removed from v.1.2.2.6  
changed lines
  Added in v.1.2.2.7

  ViewVC Help
Powered by ViewVC 1.1.2