Skip to content
Snippets Groups Projects
Commit ab60e662 authored by Kevin Kaland's avatar Kevin Kaland
Browse files

Issue #1505030: Allow updating the PDF.

This patch also shows additional information on the Fill PDF overview
  page, namely the location of each PDF. This is to distinguish them
  since this patch also fixes the issue where identical filenames
  are not renamed.
parent 8c313f96
No related branches found
No related tags found
No related merge requests found
...@@ -88,15 +88,20 @@ function fillpdf_settings($form, &$form_state) { ...@@ -88,15 +88,20 @@ function fillpdf_settings($form, &$form_state) {
* Manage your existing forms, or upload a new one * Manage your existing forms, or upload a new one
*/ */
function fillpdf_forms_admin($form, &$form_state) { function fillpdf_forms_admin($form, &$form_state) {
$result = db_query("SELECT title, fid FROM {fillpdf_forms} ORDER BY title"); $result = db_query("SELECT title, url, fid FROM {fillpdf_forms} ORDER BY title");
$header = array(t('Title'), array( $header = array(
t('Title'),
t('Location'),
array(
'data' => t('Operations'), 'data' => t('Operations'),
'colspan' => '4', 'colspan' => '4',
)); ),
);
$rows = array(); $rows = array();
foreach ($result as $pdf_form) { foreach ($result as $pdf_form) {
$row = array( $row = array(
check_plain($pdf_form->title), check_plain($pdf_form->title),
check_plain($pdf_form->url),
l(t('Edit'), "admin/structure/fillpdf/$pdf_form->fid"), l(t('Edit'), "admin/structure/fillpdf/$pdf_form->fid"),
l(t('Delete'), "admin/structure/fillpdf/$pdf_form->fid/delete"), l(t('Delete'), "admin/structure/fillpdf/$pdf_form->fid/delete"),
l(t('Export field mappings'), "admin/structure/fillpdf/$pdf_form->fid/export"), l(t('Export field mappings'), "admin/structure/fillpdf/$pdf_form->fid/export"),
...@@ -128,42 +133,56 @@ function fillpdf_forms_admin($form, &$form_state) { ...@@ -128,42 +133,56 @@ function fillpdf_forms_admin($form, &$form_state) {
* Makes sure the Upload was provided (want to validate .pdf here too) * Makes sure the Upload was provided (want to validate .pdf here too)
*/ */
function fillpdf_forms_admin_validate($form, &$form_state) { function fillpdf_forms_admin_validate($form, &$form_state) {
// uploading anything? // uploading anything?
$file = $_FILES['files']['name']['upload_pdf']; $file = $_FILES['files']['name']['upload_pdf'];
if (!$file) { if (!$file) {
form_set_error('url', t('A PDF must be provided.')); form_set_error('url', t('A PDF must be provided.'));
} }
$validate_file = _fillpdf_validate_upload($file);
if (isset($validate_file['#error'])) {
form_set_error('url', $validate_file['#message']);
}
}
function _fillpdf_validate_upload($file) {
// from includes/file.inc, line 634, but can't use that function because file not an object yet // from includes/file.inc, line 634, but can't use that function because file not an object yet
if (!preg_match('/\.pdf$/i', $file)) { if (!preg_match('/\.pdf$/i', $file)) {
form_set_error('url', t('Only PDF files are allowed')); return array(
'#error' => TRUE,
'#message' => t('Only PDF files are allowed'),
);
} }
// directory exist or writeable? // directory exist or writeable?
$dir = file_build_uri('fillpdf'); $dir = file_build_uri('fillpdf');
file_prepare_directory($dir, FILE_CREATE_DIRECTORY); file_prepare_directory($dir, FILE_CREATE_DIRECTORY);
return TRUE;
} }
/** /**
* Creates a new Form from the uploaded PDF, including parsed fields * Creates a new Form from the uploaded PDF, including parsed fields
*/ */
function fillpdf_forms_admin_submit($form, &$form_state) { function fillpdf_forms_admin_submit($form, &$form_state) {
$fid = _fillpdf_save_upload('upload_pdf');
if (is_numeric($fid)) {
$form_state['redirect'] = "admin/structure/fillpdf/$fid";
}
}
function _fillpdf_save_upload($form_key, $fid = NULL) {
$dir = file_build_uri('fillpdf'); $dir = file_build_uri('fillpdf');
// $validators not working, so I just checked manually in fillpdf_forms_validate() // $validators not working, so I just checked manually
// in fillpdf_forms_validate()
$validators = array('file_validate_extensions' => array('pdf')); $validators = array('file_validate_extensions' => array('pdf'));
if ($file = file_save_upload('upload_pdf', $validators, $dir, FILE_EXISTS_REPLACE)) { if ($file = file_save_upload($form_key, $validators, $dir, FILE_EXISTS_RENAME)) {
drupal_set_message(t('<strong>@filename</strong> was successfully uploaded.', array('@filename' => $file->filename))); drupal_set_message(t('<strong>@filename</strong> was successfully uploaded.', array('@filename' => $file->filename)));
$file->status = FILE_STATUS_PERMANENT; $file->status = FILE_STATUS_PERMANENT;
$file = file_save($file); $file = file_save($file);
// Does this file already exist in {fillpdf_forms}? If so, don't re-insert it. // Does this file already exist in {fillpdf_forms}?
$exists = (bool) db_select('fillpdf_forms', 'ff') // If so, don't re-insert it.
->fields('ff', array('fid')) if (isset($fid) === FALSE) {
->condition('fid', $file->fid)
->execute()
->fetchField();
if ($exists === FALSE) {
db_insert('fillpdf_forms') db_insert('fillpdf_forms')
->fields(array( ->fields(array(
'fid' => $file->fid, 'fid' => $file->fid,
...@@ -171,19 +190,25 @@ function fillpdf_forms_admin_submit($form, &$form_state) { ...@@ -171,19 +190,25 @@ function fillpdf_forms_admin_submit($form, &$form_state) {
'url' => $file->uri, 'url' => $file->uri,
)) ))
->execute(); ->execute();
$fid = $file->fid;
}
else {
db_update('fillpdf_forms')
->fields(array(
'url' => $file->uri,
))
->condition('fid', $fid)
->execute();
} }
$fid = $file->fid;
fillpdf_parse_pdf($fid); fillpdf_parse_pdf($fid);
return $fid;
} }
else { else {
// commented out because even though error if file doesn't upload right, not error if they dont' upload a file (& this is still triggered) // commented out because even though error if file doesn't upload right, not error if they dont' upload a file (& this is still triggered)
drupal_set_message(t('Error saving file to @dir', array('@dir' => $dir)), 'error'); drupal_set_message(t('Error saving file to @dir', array('@dir' => $dir)), 'error');
} }
$form_state['redirect'] = "admin/structure/fillpdf/$fid";
} }
/* ---------------- Form Edit --------------------*/ /* ---------------- Form Edit --------------------*/
/** /**
...@@ -198,6 +223,8 @@ function fillpdf_form_edit($form, &$form_state, $fid) { ...@@ -198,6 +223,8 @@ function fillpdf_form_edit($form, &$form_state, $fid) {
drupal_exit(); drupal_exit();
} }
$form['#attributes'] = array('enctype' => "multipart/form-data");
$form['title'] = array( $form['title'] = array(
'#type' => 'textfield', '#type' => 'textfield',
'#title' => t('Title'), '#title' => t('Title'),
...@@ -228,6 +255,11 @@ function fillpdf_form_edit($form, &$form_state, $fid) { ...@@ -228,6 +255,11 @@ function fillpdf_form_edit($form, &$form_state, $fid) {
'#title' => t('Uploaded PDF'), '#title' => t('Uploaded PDF'),
'#description' => $pdf_form->url, '#description' => $pdf_form->url,
); );
$form['pdf_info']['upload_pdf'] = array(
'#type' => 'file',
'#title' => 'Update PDF template',
'#description' => 'Update the PDF template used by this form',
);
$form['pdf_info']['sample_populate'] = array( $form['pdf_info']['sample_populate'] = array(
'#type' => 'item', '#type' => 'item',
'#title' => 'Sample PDF', '#title' => 'Sample PDF',
...@@ -337,6 +369,15 @@ function fillpdf_form_edit($form, &$form_state, $fid) { ...@@ -337,6 +369,15 @@ function fillpdf_form_edit($form, &$form_state, $fid) {
return $form; return $form;
} }
function fillpdf_form_edit_validate($form, &$form_state) {
if ($file = $_FILES['files']['name']['upload_pdf']) {
$validate_file = _fillpdf_validate_upload($file);
if (isset($validate_file['#error'])) {
form_set_error('url', $validate_file['#message']);
}
}
}
/** /**
* Submit Edit or Delete for existing PDF form * Submit Edit or Delete for existing PDF form
*/ */
...@@ -355,9 +396,21 @@ function fillpdf_form_edit_submit($form, &$form_state) { ...@@ -355,9 +396,21 @@ function fillpdf_form_edit_submit($form, &$form_state) {
)) ))
->condition('fid', $form['#pdf_form']->fid) ->condition('fid', $form['#pdf_form']->fid)
->execute(); ->execute();
$form_state['redirect'] = "admin/structure/fillpdf/{$form['#pdf_form']->fid}"; if ($file = $_FILES['files']['name']['upload_pdf']) {
drupal_set_message(t('Successfully updated form.')); // Export the current field mappings to a variable
// $form_state['nid'] = $node->nid; $mappings = fillpdf_generate_mappings($form['#pdf_form']);
// Save the uploaded file; this also re-parses it
_fillpdf_save_upload('upload_pdf', $form['#pdf_form']->fid);
// Import the ones we just saved. This ensures there are
// orphaned mappings.
drupal_set_message(t('Your previous field mappings have been transferred to the new PDF template you uploaded. Review the messages below to make sure the results are what you expected.'));
fillpdf_import_mappings($form['#pdf_form'], $mappings);
}
$form_state['redirect'] = "admin/structure/fillpdf/{$form['#pdf_form']->fid}";
drupal_set_message(t('Successfully updated form.'));
} }
} }
...@@ -404,9 +457,17 @@ function fillpdf_form_delete_confirm_submit($form, &$form_state) { ...@@ -404,9 +457,17 @@ function fillpdf_form_delete_confirm_submit($form, &$form_state) {
* @param mixed $pdf_form The FillPDF form ID. * @param mixed $pdf_form The FillPDF form ID.
*/ */
function fillpdf_form_export($pdf_form) { function fillpdf_form_export($pdf_form) {
$fillpdf_code = fillpdf_generate_mappings($pdf_form);
return drupal_get_form('fillpdf_export_form', $fillpdf_code);
}
function fillpdf_generate_mappings($pdf_form, $skip_encoding = FALSE) {
if (is_numeric($pdf_form)) { if (is_numeric($pdf_form)) {
$fid = db_query("SELECT * FROM {fillpdf_forms} WHERE fid = :fid", array(':fid' => $pdf_form))->fetch(); $fid = db_query("SELECT * FROM {fillpdf_forms} WHERE fid = :fid", array(':fid' => $pdf_form))->fetch();
} }
else {
$fid = $pdf_form;
}
if (!$fid) { if (!$fid) {
drupal_not_found(); drupal_not_found();
drupal_exit(); drupal_exit();
...@@ -420,8 +481,7 @@ function fillpdf_form_export($pdf_form) { ...@@ -420,8 +481,7 @@ function fillpdf_form_export($pdf_form) {
'replacements' => $field->replacements, 'replacements' => $field->replacements,
); );
} }
$fillpdf_code = json_encode($export_array); return ($skip_encoding === FALSE ? $export_array : json_encode($export_array));
return drupal_get_form('fillpdf_export_form', $fillpdf_code);
} }
/** /**
...@@ -500,6 +560,17 @@ function fillpdf_form_import_form_submit($form, &$form_state) { ...@@ -500,6 +560,17 @@ function fillpdf_form_import_form_submit($form, &$form_state) {
$pdf_form = new stdClass(); $pdf_form = new stdClass();
$pdf_form->fid = $form_state['values']['fid']; $pdf_form->fid = $form_state['values']['fid'];
$mappings = $form_state['values']['mappings']; $mappings = $form_state['values']['mappings'];
fillpdf_import_mappings($mappings);
$form_state['redirect'] = "admin/structure/fillpdf/{$pdf_form->fid}";
}
/**
* Import an array of decoded Fill PDF mappings.
* For the format,
* @see fillpdf_generate_mappings()
*/
function fillpdf_import_mappings($pdf_form, $mappings) {
$fields = fillpdf_get_fields($pdf_form->fid); $fields = fillpdf_get_fields($pdf_form->fid);
$field_keys = array_keys($fields); $field_keys = array_keys($fields);
// Process the mappings // Process the mappings
...@@ -514,7 +585,6 @@ function fillpdf_form_import_form_submit($form, &$form_state) { ...@@ -514,7 +585,6 @@ function fillpdf_form_import_form_submit($form, &$form_state) {
} }
} }
drupal_set_message(t('Successfully imported matching PDF field keys. If any field mappings failed to import, they are listed above.')); drupal_set_message(t('Successfully imported matching PDF field keys. If any field mappings failed to import, they are listed above.'));
$form_state['redirect'] = "admin/structure/fillpdf/{$pdf_form->fid}";
} }
/* ---------------- Fields Edit --------------------*/ /* ---------------- Fields Edit --------------------*/
......
...@@ -643,6 +643,11 @@ function fillpdf_parse_pdf($fid) { ...@@ -643,6 +643,11 @@ function fillpdf_parse_pdf($fid) {
drupal_goto('admin/structure/fillpdf'); drupal_goto('admin/structure/fillpdf');
} }
// Delete any existing fields (in case the PDF has been parsed before)
db_delete('fillpdf_fields')
->condition('fid', $fid)
->execute();
//create fields //create fields
foreach ((array) $fields as $key => $arr) { foreach ((array) $fields as $key => $arr) {
if ($arr['type']) { // Don't store "container" fields if ($arr['type']) { // Don't store "container" fields
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment