You are here

function fillpdf_form_edit in FillPDF 7

Same name and namespace in other branches
  1. 5 fillpdf.module \fillpdf_form_edit()
  2. 6 fillpdf.admin.inc \fillpdf_form_edit()
  3. 7.2 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 311
Allows mappings of PDFs to site content.

Code

function fillpdf_form_edit($form, &$form_state, $fid) {
  $pdf_form = fillpdf_load($fid, FALSE, FALSE);
  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'] = array(
    '#type' => 'item',
    '#title' => 'Tokens',
  ) + _fillpdf_admin_token_form(TRUE);
  $entity_mode = module_exists('entity_token');
  $form['default_nid'] = array(
    '#type' => 'textfield',
    '#title' => $entity_mode ? t('Default Entity ID') : t('Default Node ID'),
    '#description' => $entity_mode ? t('When filling a PDF, use this entity for the data source if none is specified in the FillPDF URL. (When filling from Webform data, this is treated as the Webform Node ID.)') : 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,
  );
  if ($entity_mode) {

    // Compile a list of all entity types that we know provide tokens.
    $entity_type_options = array();
    $entity_types = entity_get_info();
    foreach ($entity_types as $entity_type => $entity_info) {
      if (!empty($entity_info['token type'])) {
        $entity_type_options[$entity_type] = $entity_info['label'];
      }
    }
    $form['default_entity_type'] = array(
      '#type' => 'select',
      '#title' => t('Default Entity Type'),
      '#description' => t('When filling a PDF, treat the entity ID as this type if none is specified in the FillPDF URL. If this is not set, <em>Node</em> is assumed.'),
      '#options' => array(
        '' => t('- None -'),
      ) + $entity_type_options,
      '#default_value' => $pdf_form->default_entity_type,
    );
  }
  else {

    // Don't break the submit method. Just supply it blank.
    $form['default_entity_type'] = array(
      '#type' => 'value',
      '#value' => '',
    );
  }

  // @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',
    '#attributes' => array(
      'accept' => 'application/pdf',
    ),
  );
  $url = fillpdf_pdf_link($fid, NULL, NULL, TRUE);

  // Unless we're redirecting to the PDF file, return to the form edit page.
  if (!empty($pdf_form->destination_path) && !$pdf_form->destination_redirect) {
    $url .= '&destination=admin/structure/fillpdf/' . $fid;
  }
  $form['pdf_info']['sample_populate'] = array(
    '#type' => 'item',
    '#title' => 'Sample PDF',
    '#description' => l(t('See which fields are which in this PDF.'), $url) . '<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' => $entity_mode ? t('Fill PDF from default entity') : t('Fill PDF from default node'),
      '#description' => ($entity_mode ? l(t('Download this PDF filled with data from the default entity (@entity).', array(
        '@entity' => $pdf_form->default_nid,
      )), fillpdf_pdf_link($fid)) : 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' => t('Form ID: [@fid]. Populate this form with node IDs, such as @example<br>', array(
      '@fid' => $fid,
      '@example' => $entity_mode ? "/fillpdf?fid={$fid}&nid=10" : "/fillpdf?fid={$fid}&entity_id=node:10",
    )),
  );
  $form['extra'] = array(
    '#type' => 'fieldset',
    '#title' => t('Additional PDF settings'),
    '#collapsible' => TRUE,
    '#collapsed' => !($pdf_form->destination_path || $pdf_form->replacements),
  );
  $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). If you are using <em>Private files</em> for storage, this path will be a subdirectory of <em>fillpdf/</em> in the private files directory.\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>\n      <p>The path 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>\n      <p>It is also recommended to use the <strong>[random:hash:?]</strong> token to append a random string somewhere\n      in the filename. This reduces the chance of a sequential number being appended to the end of the generated PDF.\n      This obscures whether PDFs with the same name have been generated before or not and may be particularly useful in\n      higher-security environments.</p>"),
    '#maxlength' => 255,
    '#default_value' => $pdf_form->destination_path,
  );
  $form['extra']['scheme'] = array(
    '#type' => 'radios',
    '#title' => t('Storage system for generated PDFs'),
    '#default_value' => !empty($pdf_form->scheme) ? $pdf_form->scheme : fillpdf_default_scheme(),
    '#options' => fillpdf_scheme_options(),
    '#description' => t('This setting is used as the storage/download method for generated PDFs. The use of public files is more efficient, but does not provide any access control. Changing this setting will require you to migrate associated files and data yourself and is not recommended after you have uploaded a template.'),
  );
  $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'] = array(
    '#type' => 'item',
    '#title' => 'Tokens',
  ) + _fillpdf_admin_token_form(TRUE);
  $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(
      // Editable.
      check_plain($field->label),
      check_plain($field->pdf_key),
      $field->prefix,
      // Editable, expandable.
      $field->value,
      $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;
}