| 1 |
<?php |
<?php |
| 2 |
// $Id: docapi.module,v 1.2.2.4 2008/07/06 06:35:59 bradfordcp Exp $ |
// $Id: docapi.module,v 1.2.2.5 2008/07/08 04:26:39 bradfordcp Exp $ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* @file |
* @file |
| 13 |
$items = array(); |
$items = array(); |
| 14 |
|
|
| 15 |
$items['admin/settings/docapi'] = array( |
$items['admin/settings/docapi'] = array( |
| 16 |
'title' => t('DocAPI Configuration'), |
'title' => 'DocAPI Configuration', |
| 17 |
'description' => t('Configure various aspects of the DocAPI module.'), |
'description' => 'Configure various aspects of the DocAPI module.', |
| 18 |
'page callback' => 'drupal_get_form', |
'page callback' => 'drupal_get_form', |
| 19 |
'page arguments' => array('docapi_admin_form'), |
'page arguments' => array('docapi_admin_form'), |
| 20 |
'access arguments' => array('administer site configuration'), |
'access arguments' => array('administer site configuration'), |
| 21 |
); |
); |
| 22 |
|
|
| 23 |
$items['node/add/doc'] = array( |
$items['node/add/doc'] = array( |
| 24 |
'title' => t('Import Document into Library'), |
'title' => 'Import Document into Library', |
| 25 |
'description' => t('Upload and parse files into the document library.'), |
'description' => 'Upload and parse files into the document library.', |
| 26 |
'page callback' => 'drupal_get_form', |
'page callback' => 'drupal_get_form', |
| 27 |
'page arguments' => array('docapi_doc_add_form'), |
'page arguments' => array('docapi_doc_add_form'), |
| 28 |
'access arguments' => array('import documents to library'), |
'access arguments' => array('import documents to library'), |
| 59 |
} |
} |
| 60 |
|
|
| 61 |
/** |
/** |
| 62 |
* Probe other modules for docapi hooks |
* Implementation of hook_cron() |
|
* @TODO: Remove, this is being replaced with code in docapi_add_plugin() |
|
| 63 |
*/ |
*/ |
| 64 |
function docapi_find_plugins() { |
function docapi_cron() { |
| 65 |
$result = db_query("SELECT s.name FROM {system} s WHERE s.type = 'module' AND NOT (s.name IN (SELECT dp.module FROM {docapi_plugins} dp))"); |
docapi_update_mimes(); |
| 66 |
|
} |
| 67 |
while ($row = db_fetch_array($result)) { |
|
| 68 |
if ($info = module_invoke($row['name'], 'docapi_info')) { |
/** |
| 69 |
db_query("INSERT INTO {docapi_plugins} (name, module) VALUES ('%s', '%s')", $info['name'], $row['name']); |
* Verifies that a module still checks certain mimetypes |
| 70 |
$plugin_id = db_last_insert_id('docapi_info', 'plugin_id'); |
*/ |
| 71 |
|
function docapi_update_mimes() { |
|
foreach ($info['mimetypes'] as $mimetype) { |
|
|
// MyISAM does this automatically, but we cannot use it since we are within the schema API |
|
|
// Instead we pull out the largest value or 0 if the value is null |
|
|
$weight = db_result(db_query("SELECT dpm.weight FROM {docapi_plugin_mimetypes} dpm WHERE mimetype = '%s'", $mimetype)); |
|
|
|
|
|
if ($weight == null) |
|
|
$weight = 0; |
|
|
else |
|
|
$weight++; |
|
|
|
|
|
db_query("INSERT INTO {docapi_plugin_mimetypes} (plugin_id, mimetype, weight) VALUES (%d, '%s', %d)", $plugin_id, $mimetype, $weight); |
|
|
} |
|
|
} |
|
|
} |
|
| 72 |
} |
} |
| 73 |
|
|
| 74 |
/** |
/** |
| 202 |
$form = array(); |
$form = array(); |
| 203 |
$form['#attributes'] = array('enctype' => "multipart/form-data"); |
$form['#attributes'] = array('enctype' => "multipart/form-data"); |
| 204 |
|
|
| 205 |
if (!$form_state['submitted']) { |
if (!$form_state['submitted'] && !(array_key_exists('values', $form_state) && array_key_exists('docapi_file_path', $form_state['values']))) { |
| 206 |
$form['docapi_file'] = array( |
$form['docapi_file'] = array( |
| 207 |
'#type' => 'file', |
'#type' => 'file', |
| 208 |
'#title' => t('The document to import'), |
'#title' => t('The document to import'), |
| 223 |
); |
); |
| 224 |
} |
} |
| 225 |
else { |
else { |
| 226 |
/*$form['docapi_file'] = array( |
drupal_set_message(t('The uploaded file has been stored, please continue correcting any errors.')); |
|
'#type' => 'file', |
|
|
'#title' => t('The document to import'), |
|
|
'#description' => t(''), |
|
|
'#default_value' => '', |
|
|
); |
|
|
$form['docapi_file_path'] = array( |
|
|
'#type' => 'value', |
|
|
'#value' => '', |
|
|
);*/ |
|
|
drupal_set_message('FORM STATE: <pre>'. var_export($form_state, true) .'</pre>'); |
|
| 227 |
} |
} |
| 228 |
|
|
| 229 |
$form['docapi_advanced_settings'] = array( |
$form['docapi_advanced_settings'] = array( |
| 238 |
'#title' => t('Force Parser'), |
'#title' => t('Force Parser'), |
| 239 |
'#description' => t('Forces the document to be parsed through the specified parser. This does not disable checks to determine if the parser can validate the given file.'), |
'#description' => t('Forces the document to be parsed through the specified parser. This does not disable checks to determine if the parser can validate the given file.'), |
| 240 |
'#options' => docapi_get_parsers(true), |
'#options' => docapi_get_parsers(true), |
| 241 |
'#default_value' => 0, |
'#default_value' => 'AUTO', |
| 242 |
); |
); |
| 243 |
|
|
| 244 |
$form['docapi_doc_add_submit'] = array( |
$form['docapi_doc_add_submit'] = array( |
| 251 |
|
|
| 252 |
/** |
/** |
| 253 |
* DocAPI Upload document form validation |
* DocAPI Upload document form validation |
|
* |
|
|
* @TODO Implement a hidden field so that if the submission fails on something other than the file it stores the temporary path on the server and the user does not have to upload the file again. |
|
| 254 |
*/ |
*/ |
| 255 |
function docapi_doc_add_form_validate($form, &$form_state) { |
function docapi_doc_add_form_validate($form, &$form_state) { |
| 256 |
// Verify there is a file to be validated |
// Verify there is a file to be validated |
| 263 |
form_set_value($form['docapi_file_name'], $_FILES['files']['name']['docapi_file'], $form_state); |
form_set_value($form['docapi_file_name'], $_FILES['files']['name']['docapi_file'], $form_state); |
| 264 |
} |
} |
| 265 |
else { |
else { |
| 266 |
|
// agentrickard: I am not sure of what to put here... |
| 267 |
} |
} |
| 268 |
|
|
| 269 |
// Load a list of possible modules |
// Load a list of possible modules |
| 291 |
} |
} |
| 292 |
else { |
else { |
| 293 |
// Store the module name, as the select drop down response |
// Store the module name, as the select drop down response |
| 294 |
form_set_value($form['docapi_doc_parser'], $module, $form_state); |
form_set_value($form['docapi_advanced_settings']['docapi_doc_parser'], $module, $form_state); |
| 295 |
} |
} |
| 296 |
} |
} |
| 297 |
else { |
else { |
| 304 |
*/ |
*/ |
| 305 |
function docapi_doc_add_form_submit($form, &$form_state) { |
function docapi_doc_add_form_submit($form, &$form_state) { |
| 306 |
// @TODO: Remove, this is for debugging only. |
// @TODO: Remove, this is for debugging only. |
| 307 |
drupal_set_message('FORM STATE: <pre>'. var_export($form_state, true) .'</pre>'); |
//drupal_set_message('FORM STATE: <pre>'. var_export($form_state, true) .'</pre>'); |
| 308 |
|
|
| 309 |
// Begin file processing once it has cleared all of the validation |
// Begin file processing once it has cleared all of the validation |
| 310 |
|
|
| 311 |
// Move the file to the its location within the 'docapi_file_path' |
// Move the file to the its location within the 'docapi_file_path' |
| 312 |
if (!move_uploaded_file($form_state['values']['docapi_file_path'], variable_get('docapi_file_path', 'sites/defaultfiles/docapi_library') .'/'. $form_state['values']['docapi_file_name'])) { |
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']))) { |
| 313 |
form_set_error('docapi_file', t('Could not move file into the upload directory.')); |
form_set_error('docapi_file', t('Could not move file into the upload directory.')); |
| 314 |
} |
} |
| 315 |
else { |
else { |
| 316 |
drupal_set_message(t('Successfully moved file into working directy.')); |
drupal_set_message(t('Successfully moved file into working directory.')); |
| 317 |
|
|
| 318 |
|
// Add the file to the library |
| 319 |
|
global $user; |
| 320 |
|
|
| 321 |
|
// Set the variables |
| 322 |
|
$uid = $user->uid; |
| 323 |
|
$module = $form_state['values']['docapi_doc_parser']; |
| 324 |
|
$plugin_id = db_result(db_query("SELECT plugin_id FROM {docapi_plugins} WHERE module = '%s'", $module)); |
| 325 |
|
$file_mime = $form_state['values']['docapi_file_type']; |
| 326 |
|
$file_name = basename($form_state['values']['docapi_file_name']); |
| 327 |
|
$file_path = realpath(variable_get('docapi_file_path', 'sites/default/files/docapi_library')) .'/'. basename($file_name); |
| 328 |
|
$file_size = filesize($file_path); |
| 329 |
|
|
| 330 |
|
// Insert into docapi_library |
| 331 |
|
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()); |
| 332 |
|
$doc_id = db_last_insert_id('docapi_library', 'doc_id'); |
| 333 |
|
|
| 334 |
|
// Load the metadata and insert it into docapi_metadata |
| 335 |
|
$metadata = module_invoke($module, 'docapi_metadata', $file_path, $file_mime); |
| 336 |
|
foreach ($metadata as $meta => $data) { |
| 337 |
|
db_query("INSERT INTO {docapi_metadata} (doc_id, field, meta_value) VALUES (%d, '%s', %b)", $doc_id, $meta, $data); |
| 338 |
|
} |
| 339 |
|
|
| 340 |
|
drupal_set_message(t('Sucessfully imported %file', array('%file' => $file_name))); |
| 341 |
} |
} |
| 342 |
} |
} |