diff --git a/fillpdf.admin.inc b/fillpdf.admin.inc index e60dd6f94ce21607a5711884907cfdacc9b0c157..447f8c6e57f860af36d761761be1a34507541c78 100644 --- a/fillpdf.admin.inc +++ b/fillpdf.admin.inc @@ -1,4 +1,4 @@ -e?php +<?php /** * @file @@ -194,6 +194,14 @@ function fillpdf_form_edit($form, &$form_state, $fid) { '#required' => TRUE, ); + $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 Fill PDF 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 @@ -321,6 +329,7 @@ function fillpdf_form_edit_submit($form, &$form_state) { db_update('fillpdf_forms') ->fields(array( 'title' => $form_state['values']['title'], + 'default_nid' => (int) $form_state['values']['default_nid'] > 0 ? (int) $form_state['values']['default_nid'] : NULL, 'destination_path' => $form_state['values']['destination_path'], 'replacements' => $form_state['values']['replacements'], )) @@ -695,3 +704,4 @@ function fillpdf_update_field(&$pdf_form, &$field, $old_key) { ->condition('pdf_key', $old_key) ->execute(); } + diff --git a/fillpdf.install b/fillpdf.install index 5830ff0fbc2178d186ec803cbd0e96b93262460c..7c33ef9ea1073476230a188debec54c2bf708554 100644 --- a/fillpdf.install +++ b/fillpdf.install @@ -24,6 +24,11 @@ function fillpdf_schema() { 'length' => 255, 'not null' => TRUE, ), + 'default_nid' => array( + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => FALSE, + ), 'url' => array( 'type' => 'varchar', 'length' => 255, @@ -134,3 +139,11 @@ function fillpdf_update_7003() { variable_set('fillpdf_service', $variable_name_map[$default]); } } + +/** + * Add field to store default NID. + */ +function fillpdf_update_7004() { + db_add_field('fillpdf_forms', 'default_nid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => FALSE)); +} + diff --git a/fillpdf.module b/fillpdf.module index d84f3f0e0900194657e6b2320eb5480e916737a4..e0920e443bac7615d9533b44164b900d4e9dab65 100644 --- a/fillpdf.module +++ b/fillpdf.module @@ -227,7 +227,7 @@ function fillpdf_merge_pdf($fid, $nids = NULL, $webform_arr = NULL, $sample = NU drupal_goto(); } - $fillpdf_info = db_query("SELECT title, url, destination_path, replacements FROM {fillpdf_forms} WHERE fid = :fid", array(':fid' => $fid))->fetch(); + $fillpdf_info = db_query("SELECT title, default_nid, url, destination_path, replacements FROM {fillpdf_forms} WHERE fid = :fid", array(':fid' => $fid))->fetch(); $fillpdf_info->replacements = _fillpdf_replacements_to_array($fillpdf_info->replacements); // Case 2: Only $fid -- just give them empty pdf @@ -240,6 +240,29 @@ function fillpdf_merge_pdf($fid, $nids = NULL, $webform_arr = NULL, $sample = NU global $user; $nodes = $webforms = array(); + + // If $webform_arr contains entries with an sid, but not an nid, set the nid to the default. + if (!empty($fillpdf_info->default_nid) && is_array($webform_arr)) { + foreach (array_keys($webform_arr) as $key) { + if (empty($webform_arr[$key]['nid'])) { + $webform_arr[$key]['nid'] = $fillpdf_info->default_nid; + } + } + } + + // If no nid is given, use the default. + if (!empty($fillpdf_info->default_nid) && empty($nids) && empty($webform_arr)) { + $default_node = node_load($fillpdf_info->default_nid); + if ($default_node) { + if (empty($default_node->webform)) { // Default node is a non-webform node. + $nodes[] = $default_node; + } + else { // Default node is a webform. + $webform_arr = array(array('nid' => $fillpdf_info->default_nid, 'node' => $default_node)); + } + } + } + // Nodes if (is_array($nids)) { foreach ($nids as $nid) { @@ -256,14 +279,16 @@ function fillpdf_merge_pdf($fid, $nids = NULL, $webform_arr = NULL, $sample = NU } foreach ($webform_arr as $webform) { - if (empty($webform['sid'])) { // User did not specify submission ID, meaning they want most recent. - $webform['sid'] = db_query('SELECT sid FROM {webform_submissions} - WHERE nid = :nid AND uid = :uid ORDER BY submitted DESC', array(':nid' => (int) $webform['nid'], ':uid' => (int) $user->uid))->fetchField(); + if (!empty($webform['nid'])) { + if (empty($webform['sid'])) { // User did not specify submission ID, meaning they want most recent. + $webform['sid'] = db_query('SELECT sid FROM {webform_submissions} + WHERE nid = :nid AND uid = :uid ORDER BY submitted DESC', array(':nid' => $webform['nid'], ':uid' => $user->uid))->fetchField(); + } + $webforms[] = array( + 'webform' => empty($webform['node']) ? node_load($webform['nid']) : $webform['node'], + 'submission' => webform_get_submission($webform['nid'], $webform['sid']), + ); } - $webforms[] = array( - 'webform' => node_load($webform['nid']), - 'submission' => webform_get_submission($webform['nid'], $webform['sid']), - ); } }