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

Issue #2359213: Move field processing to handler.

parent 25e768df
No related branches found
No related tags found
No related merge requests found
...@@ -203,14 +203,30 @@ class FillPdfOverviewForm extends FillPdfAdminFormBase { ...@@ -203,14 +203,30 @@ class FillPdfOverviewForm extends FillPdfAdminFormBase {
// Attempt to parse the fields in the PDF. // Attempt to parse the fields in the PDF.
$fields = $backend->parse($fillpdf_form); $fields = $backend->parse($fillpdf_form);
$form_fields = [];
foreach ((array) $fields as $key => $arr) {
if ($arr['type']) { // Don't store "container" fields
$arr['name'] = str_replace('�', '', $arr['name']); // pdftk sometimes inserts random � markers - strip these out. NOTE: This may break forms that actually DO contain this pattern, but 99%-of-the-time functionality is better than merge failing due to improper parsing.
$field = FillPdfFormField::create(
[
'fillpdf_form' => $fillpdf_form,
'pdf_key' => $arr['name'],
'value' => '',
]
);
$form_fields[] = $field;
}
}
// Save the fields that were parsed out (if any). If none were, set a // Save the fields that were parsed out (if any). If none were, set a
// warning message telling the user that. // warning message telling the user that.
foreach ($fields as $fillpdf_form_field) { foreach ($form_fields as $fillpdf_form_field) {
/** @var FillPdfFormField $fillpdf_form_field */ /** @var FillPdfFormField $fillpdf_form_field */
$fillpdf_form_field->save(); $fillpdf_form_field->save();
} }
if (count($fields) === 0) { if (count($form_fields) === 0) {
drupal_set_message($this->t('No fields detected in PDF. Are you sure it contains editable fields?'), 'warning'); drupal_set_message($this->t('No fields detected in PDF. Are you sure it contains editable fields?'), 'warning');
} }
......
...@@ -10,10 +10,8 @@ use Drupal\Component\Annotation\Plugin; ...@@ -10,10 +10,8 @@ use Drupal\Component\Annotation\Plugin;
use Drupal\Core\Annotation\Translation; use Drupal\Core\Annotation\Translation;
use Drupal\file\Entity\File; use Drupal\file\Entity\File;
use Drupal\file\FileInterface; use Drupal\file\FileInterface;
use Drupal\fillpdf\Entity\FillPdfFormField;
use Drupal\fillpdf\FillPdfBackendPluginInterface; use Drupal\fillpdf\FillPdfBackendPluginInterface;
use Drupal\fillpdf\FillPdfFormInterface; use Drupal\fillpdf\FillPdfFormInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/** /**
* @Plugin( * @Plugin(
...@@ -39,60 +37,22 @@ class FillPdfServiceFillPdfBackend implements FillPdfBackendPluginInterface { ...@@ -39,60 +37,22 @@ class FillPdfServiceFillPdfBackend implements FillPdfBackendPluginInterface {
* @inheritdoc * @inheritdoc
*/ */
public function parse(FillPdfFormInterface $fillpdf_form) { public function parse(FillPdfFormInterface $fillpdf_form) {
// @todo: Is there a better way to do this? /** @var FileInterface $file */
$file = File::load($fillpdf_form->file->target_id); $file = File::load($fillpdf_form->file->target_id);
$content = file_get_contents($file->getFileUri()); $content = file_get_contents($file->getFileUri());
$result = $this->xmlRpcRequest('parse_pdf_fields', base64_encode($content)); $result = $this->xmlRpcRequest('parse_pdf_fields', base64_encode($content));
// TODO: Don't do magic error handling. Throw an exception and let the caller decide what to do. Make my own exception class (see PluginNotFoundException for inspiration). if ($result->error == TRUE) {
// if ($result->error == TRUE) { // @todo: Throw an exception, log a message etc.
// drupal_goto('admin/structure/fillpdf'); return [];
// } // after setting error message } // after setting error message
$fields = $result->data; $fields = $result->data;
$form_fields = array(); return $fields;
foreach ((array) $fields as $key => $arr) {
if ($arr['type']) { // Don't store "container" fields
$arr['name'] = str_replace('�', '', $arr['name']); // pdftk sometimes inserts random � markers - strip these out. NOTE: This may break forms that actually DO contain this pattern, but 99%-of-the-time functionality is better than merge failing due to improper parsing.
$field = FillPdfFormField::create(
array(
'fillpdf_form' => $fillpdf_form,
'pdf_key' => $arr['name'],
'value' => '',
)
);
$form_fields[] = $field;
}
}
return $form_fields;
}
/**
* @inheritdoc
*/
public function populateWithFieldData(FillPdfFormInterface $pdf_form, array $field_mapping, array $context) {
/** @var FileInterface $original_file */
$original_file = File::load($pdf_form->file->target_id);
$original_pdf = file_get_contents($original_file->getFileUri());
$api_key = $this->config['fillpdf_service_api_key'];
$result = $this->xmlRpcRequest('merge_pdf_v3', base64_encode($original_pdf), $field_mapping['fields'], $api_key, $context['flatten'], $field_mapping['images']);
// @todo: Error handling/exceptions
// if ($result->error == TRUE) {
// drupal_goto();
// }
$populated_pdf = base64_decode($result->data);
return $populated_pdf;
} }
/** /**
* Make an XML_RPC request. * Make an XML_RPC request.
* *
...@@ -109,17 +69,16 @@ class FillPdfServiceFillPdfBackend implements FillPdfBackendPluginInterface { ...@@ -109,17 +69,16 @@ class FillPdfServiceFillPdfBackend implements FillPdfBackendPluginInterface {
$ret = new \stdClass; $ret = new \stdClass;
// TODO: Exceptions, not error messages
if (isset($result['error'])) { if (isset($result['error'])) {
// drupal_set_message($result['error'], 'error'); drupal_set_message($result['error'], 'error');
$ret->error = TRUE; $ret->error = TRUE;
} }
elseif ($result == FALSE || xmlrpc_error()) { elseif ($result == FALSE || xmlrpc_error()) {
$error = xmlrpc_error(); $error = xmlrpc_error();
$ret->error = TRUE; $ret->error = TRUE;
// drupal_set_message(t('There was a problem contacting the FillPDF service. drupal_set_message(t('There was a problem contacting the FillPDF service.
// It may be down, or you may not have internet access. [ERROR @code: @message]', It may be down, or you may not have internet access. [ERROR @code: @message]',
// array('@code' => $error->code, '@message' => $error->message)), 'error'); ['@code' => $error->code, '@message' => $error->message]), 'error');
} }
else { else {
$ret->data = $result['data']; $ret->data = $result['data'];
...@@ -128,4 +87,19 @@ class FillPdfServiceFillPdfBackend implements FillPdfBackendPluginInterface { ...@@ -128,4 +87,19 @@ class FillPdfServiceFillPdfBackend implements FillPdfBackendPluginInterface {
return $ret; return $ret;
} }
/**
* @inheritdoc
*/
public function populateWithFieldData(FillPdfFormInterface $pdf_form, array $field_mapping, array $context) {
/** @var FileInterface $original_file */
$original_file = File::load($pdf_form->file->target_id);
$original_pdf = file_get_contents($original_file->getFileUri());
$api_key = $this->config['fillpdf_service_api_key'];
$result = $this->xmlRpcRequest('merge_pdf_v3', base64_encode($original_pdf), $field_mapping['fields'], $api_key, $context['flatten'], $field_mapping['images']);
$populated_pdf = base64_decode($result->data);
return $populated_pdf;
}
} }
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