You are here

function _fillpdf_save_upload in FillPDF 7.2

Same name and namespace in other branches
  1. 7 fillpdf.admin.inc \_fillpdf_save_upload()
2 calls to _fillpdf_save_upload()
fillpdf_forms_admin_submit in ./fillpdf.admin.inc
Creates a new Form from the uploaded PDF, including parsed fields
fillpdf_form_edit_submit in ./fillpdf.admin.inc
Submit Edit or Delete for existing PDF form

File

./fillpdf.admin.inc, line 122
Allows mappings of PDFs to site content

Code

function _fillpdf_save_upload($form_key, $fid = NULL) {
  $dir = file_build_uri('fillpdf');
  $validators = array(
    'file_validate_extensions' => array(
      'pdf',
    ),
  );
  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,
    )));
    $file->status = FILE_STATUS_PERMANENT;
    $file = file_save($file);

    // Does this file already exist in {fillpdf_forms}?
    // If so, don't re-insert it.
    if (isset($fid) === FALSE) {
      db_insert('fillpdf_forms')
        ->fields(array(
        'fid' => $file->fid,
        'title' => $file->filename,
        'url' => $file->uri,
      ))
        ->execute();
      $fid = $file->fid;
    }
    else {
      db_update('fillpdf_forms')
        ->fields(array(
        'url' => $file->uri,
      ))
        ->condition('fid', $fid)
        ->execute();
    }
    fillpdf_parse_pdf($fid);
    return $fid;
  }
  else {
    drupal_set_message(t('Error saving file to @dir', array(
      '@dir' => $dir,
    )), 'error');
  }
}