You are here

function fillpdf_form_edit in FillPDF 6

Same name and namespace in other branches
  1. 5 fillpdf.module \fillpdf_form_edit()
  2. 7.2 fillpdf.admin.inc \fillpdf_form_edit()
  3. 7 fillpdf.admin.inc \fillpdf_form_edit()

Edit existing PDF form

1 string reference to 'fillpdf_form_edit'
fillpdf_menu in ./fillpdf.module
Implementation of hook_menu().

File

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

Code

function fillpdf_form_edit(&$form_state, $fid) {
  $pdf_form = db_fetch_object(db_query("SELECT * FROM {fillpdf_forms} WHERE fid = %d", $fid));
  if ($pdf_form === FALSE) {
    drupal_set_message(t('Non-existent Fill PDF Form ID.'), 'error');
    drupal_not_found();
    exit;
  }
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Title'),
    '#maxlength' => 127,
    '#default_value' => $pdf_form->title,
    '#required' => TRUE,
    '#description' => t('Enter a title for this mapping configuration.
       This will also be used for deciding the filename of your PDF. <strong>This
       field supports tokens.</strong>'),
  );
  $form['title_tokens_fieldset'] = array(
    '#type' => 'fieldset',
    '#title' => 'Tokens',
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['title_tokens_fieldset']['tokens'] = _fillpdf_admin_token_form();

  // @@TODO:
  // They can upload a PDF any time, but fields will only be generated on add.  Don't want to purge existing fields,
  // however a user might have accidently uploaded an old template and discover much later (if it's substantially different, just
  // create a new Form
  $form['pdf_info'] = array(
    '#type' => 'fieldset',
    '#title' => 'PDF Form information',
    '#collapsed' => true,
  );
  $form['pdf_info']['submitted_pdf'] = array(
    '#type' => 'item',
    '#title' => t('Uploaded PDF'),
    '#value' => $pdf_form->url,
  );
  $form['pdf_info']['sample_populate'] = array(
    '#type' => 'item',
    '#title' => 'Sample PDF',
    '#value' => l("See which fields are which in this PDF", fillpdf_pdf_link($fid, null, null, true)),
    '#description' => t("If you have set a custom path on this PDF, the sample will be saved there silently.\n      Also, path token replacement requires a real node or webform. It will not work in sample mode."),
  );
  $form['pdf_info']['form_id'] = array(
    '#type' => 'item',
    '#title' => 'Form Info',
    '#value' => "Form ID: [{$fid}].  Populate this form with node IDs, such as /fillpdf?fid={$fid}&nid=10<br/>",
  );
  $form['extra'] = array(
    '#type' => 'fieldset',
    '#title' => t('Additional PDF settings'),
    '#collapsible' => TRUE,
    '#collapsed' => $pdf_form->destination_path || $pdf_form->replacements ? FALSE : TRUE,
  );
  $form['extra']['destination_path'] = array(
    '#type' => 'textfield',
    '#title' => t('Custom path for generated PDFs'),
    '#description' => t("<p>By default, filled PDFs are not saved to disk; they are simply sent\n      directly to the browser for download. Enter a path here to change this behavior (tokens allowed).\n      <strong>Warning! Unless you include the &download=1 flag in the Fill PDF URL, PDFs will only\n      be saved to disk <em>and won't</em> be sent to the browser as well.</strong></p><p>The path\n      you specify must be in one of the following two formats:<br />\n        <ul>\n          <li><em>path/to/directory</em> (path will be treated as relative to\n          your <em>files</em> directory)</li>\n          <li><em>/absolute/path/to/directory</em> (path will be treated as relative to your entire\n          filesystem)</li>\n        </ul>\n      Note that, in both cases, you are responsible for ensuring that the user under which PHP is running can write to this path. Do not include a trailing slash.</p>"),
    '#maxlength' => 255,
    '#default_value' => $pdf_form->destination_path,
  );
  $form['extra']['destination_redirect'] = array(
    '#type' => 'checkbox',
    '#title' => t('Redirect directly to PDF'),
    '#description' => t("<strong>This setting is applicable only if a <em>Custom path for generated PDFs</em> is set.</strong> Instead of redirecting your visitors to the front page, it will redirect them directly to the PDF. However, if you pass Drupal's <em>destination</em> query string parameter, that will override this setting."),
    '#default_value' => $pdf_form->destination_redirect,
  );
  $form['extra']['tokens_fieldset'] = array(
    '#type' => 'fieldset',
    '#title' => 'Replacement patterns',
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['extra']['tokens_fieldset']['tokens'] = _fillpdf_admin_token_form();
  $form['extra']['replacements'] = array(
    '#type' => 'textarea',
    '#title' => t('Transform filled PDF field values'),
    '#wysiwyg' => FALSE,
    '#description' => FILLPDF_REPLACEMENTS_DESCRIPTION,
    '#default_value' => $pdf_form->replacements,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
  );
  $form['delete'] = array(
    '#type' => 'submit',
    '#value' => t('Delete'),
  );
  $form['delete'] = array(
    '#type' => 'submit',
    '#value' => t('Delete'),
  );
  $form['#pdf_form'] = $pdf_form;

  // @@TODO: order by weight, and add dragable ala http://www.computerminds.co.uk/quick-guide-using-drupal-add-tabledrag-and-enjoying-jquery-drag-and-drop-loveliness
  $q = db_query('SELECT * FROM {fillpdf_fields} WHERE fid = %d', $fid);
  $header = array(
    t('Label'),
    t('PDF-field key'),
    t('Value'),
    t('Transformed'),
    array(
      'data' => t('Operations'),
      'colspan' => 2,
    ),
  );
  while ($field = db_fetch_object($q)) {
    $row = array(
      check_plain($field->label),
      //editable
      check_plain($field->pdf_key),
      $field->value,
      //editable, expandable
      $field->replacements ? 'Yes' : 'No',
      // rawurlencode() is needed twice to fully protect "/". Otherwise, "/" is
      // taken as a separator when looking for a match in hook_menu().
      l(t('Edit'), "admin/structure/fillpdf/{$fid}/edit/" . rawurlencode(rawurlencode($field->pdf_key))),
      l(t('Delete'), "admin/content/fillpdf/{$fid}/delete/" . rawurlencode(rawurlencode($field->pdf_key))),
    );
    $rows[] = $row;
  }
  $form['existing_fields'] = array(
    '#type' => 'markup',
    '#value' => '<br/><br/>' . theme('table', $header, $rows, array(
      'id' => 'fillpdf_fields',
    )),
  );
  $form['export_fields'] = array(
    '#prefix' => '<div>',
    '#value' => l(t('Export these field mappings'), "admin/content/fillpdf/{$pdf_form->fid}/export"),
    '#suffix' => '</div>',
  );
  $form['import_fields'] = array(
    '#prefix' => '<div>',
    '#value' => l(t('Import a previous export into this PDF'), "admin/content/fillpdf/{$pdf_form->fid}/import"),
    '#suffix' => '</div>',
  );
  return $form;
}