You are here

function _fillpdf_save_upload in FillPDF 7

Same name and namespace in other branches
  1. 7.2 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 255
Allows mappings of PDFs to site content.

Code

function _fillpdf_save_upload($form_key, $fid = NULL) {
  if (isset($fid)) {
    $action = FILE_EXISTS_REPLACE;
    $destination = db_select('fillpdf_forms', 'ff')
      ->fields('ff', array(
      'url',
    ))
      ->condition('ff.fid', $fid, '=')
      ->execute()
      ->fetchField();
  }
  else {
    $action = FILE_EXISTS_RENAME;
    $destination = fillpdf_build_uri('fillpdf');
  }

  // $validators not working, so I just checked manually
  // in fillpdf_forms_validate()
  $validators = array(
    'file_validate_extensions' => array(
      'pdf',
    ),
  );

  // $destination in file_save_upload() must be a directory. With file_move(),
  // We can put the file to the exact filename that we want, overwriting the
  // older file when replacing the PDF for an existing form.
  $file = file_save_upload($form_key, $validators);
  $file = file_move($file, $destination, $action);
  if ($file) {
    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)) {
      db_insert('fillpdf_forms')
        ->fields(array(
        'fid' => $file->fid,
        'title' => $file->filename,
        'url' => $file->uri,
        'scheme' => fillpdf_template_scheme(),
      ))
        ->execute();
      $fid = $file->fid;
      file_usage_add($file, 'fillpdf', 'fillpdf_form', $fid);
    }
    fillpdf_parse_pdf($fid);
    return $fid;
  }
  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).
    drupal_set_message(t('Error saving file to @destination', array(
      '@destination' => $destination,
    )), 'error');
  }
}