You are here

function fillpdf_form_edit in FillPDF 7.2

Same name and namespace in other branches
  1. 5 fillpdf.module \fillpdf_form_edit()
  2. 6 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
Implements hook_menu().

File

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

Code

function fillpdf_form_edit($form, &$form_state, $fid) {
  $pdf_form = db_query("SELECT * FROM {fillpdf_forms} WHERE fid = :fid", array(
    ':fid' => $fid,
  ))
    ->fetch();
  if ($pdf_form === FALSE) {
    drupal_set_message(t('Non-existent FillPDF Form ID.'), 'error');
    drupal_not_found();
    drupal_exit();
  }
  $form['#attributes'] = array(
    'enctype' => "multipart/form-data",
  );
  $form['admin_title'] = array(
    '#type' => 'textfield',
    '#title' => t('Administrative title (optional)'),
    '#maxlenth' => 512,
    '#default_value' => $pdf_form->admin_title,
    '#required' => FALSE,
    '#description' => t('Enter the name of the form here, and it will be shown on the <a href="!form_overview">form
      overview page</a>. It has no effect on functionality, but it can help you identify which form configuration
      you want to edit.', array(
      '!form_overview' => url('admin/structure/fillpdf'),
    )),
  );
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Title (filename pattern)'),
    '#maxlength' => 512,
    '#default_value' => $pdf_form->title,
    '#required' => TRUE,
    '#description' => t('Enter a title for this mapping configuration.
      This will 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();
  $form['default_nid'] = array(
    '#type' => 'textfield',
    '#title' => t('Default Node ID'),
    '#description' => t('When filling a PDF, use this node for the data source if no node is specified in the FillPDF URL.'),
    '#maxlength' => 10,
    '#default_value' => $pdf_form->default_nid,
  );

  // @@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'),
    '#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(
    '#type' => 'item',
    '#title' => 'Sample PDF',
    '#description' => l(t('See which fields are which in this PDF.'), fillpdf_pdf_link($fid, NULL, NULL, TRUE)) . '<br />' . t('If you have set a custom path on this PDF, the sample will be saved there silently.'),
  );
  if (!empty($pdf_form->default_nid)) {
    $form['pdf_info']['populate_default'] = array(
      '#type' => 'item',
      '#title' => 'FillPDF from default node',
      '#description' => l(t('Download this PDF filled with data from the default node (@node).', array(
        '@node' => $pdf_form->default_nid,
      )), fillpdf_pdf_link($fid)) . '<br />' . t('If you have set a custom path on this PDF, the sample will be saved there silently.'),
    );
  }
  $form['pdf_info']['form_id'] = array(
    '#type' => 'item',
    '#title' => 'Form Info',
    '#description' => "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 FillPDF 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' => 'Tokens',
    '#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('Update'),
  );
  $form['delete'] = array(
    '#type' => 'submit',
    '#value' => t('Delete'),
  );
  $form['cancel'] = array(
    '#type' => 'link',
    '#title' => t('Cancel'),
    '#href' => 'admin/structure/fillpdf',
  );
  $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 = :fid', array(
    ':fid' => $fid,
  ));
  $header = array(
    t('Label'),
    t('PDF-field key'),
    t('Prefix'),
    t('Value'),
    t('Suffix'),
    t('Transformed'),
    array(
      'data' => t('Operations'),
      'colspan' => 2,
    ),
  );
  $rows = array();
  foreach ($q as $field) {
    $row = array(
      check_plain($field->label),
      // editable
      check_plain($field->pdf_key),
      $field->prefix,
      $field->value,
      // editable, expandable
      $field->suffix,
      $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))),
    );
    $rows[] = $row;
  }
  $form['existing_fields'] = array(
    '#markup' => '<br/><br/>' . theme('table', array(
      'header' => $header,
      'rows' => $rows,
      'attributes' => array(
        'id' => 'fillpdf_fields',
      ),
    )),
  );
  $form['export_fields'] = array(
    '#prefix' => '<div>',
    '#markup' => l(t('Export these field mappings'), "admin/structure/fillpdf/{$pdf_form->fid}/export"),
    '#suffix' => '</div>',
  );
  $form['import_fields'] = array(
    '#prefix' => '<div>',
    '#markup' => l(t('Import a previous export into this PDF'), "admin/structure/fillpdf/{$pdf_form->fid}/import"),
    '#suffix' => '</div>',
  );
  return $form;
}