From e9a36549fd7dc0d859808665dca1d38f539d262f Mon Sep 17 00:00:00 2001
From: Liam Morland <lkmorlan@493050.no-reply.drupal.org>
Date: Wed, 8 Feb 2012 18:44:33 -0800
Subject: [PATCH] Issue #1393012: Allow specifying default node.

This works with Webform nodes as the node as well.
---
 fillpdf.admin.inc | 12 +++++++++++-
 fillpdf.install   | 13 +++++++++++++
 fillpdf.module    | 41 +++++++++++++++++++++++++++++++++--------
 3 files changed, 57 insertions(+), 9 deletions(-)

diff --git a/fillpdf.admin.inc b/fillpdf.admin.inc
index e60dd6f..447f8c6 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 5830ff0..7c33ef9 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 d84f3f0..e0920e4 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']),
-      );
     }
   }
 
-- 
GitLab