--- pdfview/pdfview.module	2005-10-28 16:36:56.000000000 -0400
+++ pdfview.module	2006-05-19 10:41:24.000000000 -0400
@@ -1,145 +1,192 @@
 <?php
-// $Id: pdfview.module,v 1.18 2005/10/28 20:36:56 urbanfalcon Exp $
+// $Id: pdfview.module for 4.7 2006/05/05 Exp $
 
 /**
- * Last tested:
- * By: Gabriel Sjoberg (aim:banditbuddhist email:xabbu-at-cox-dot-net drupal: TheLibrarian)
- * Running At: http://www.nosir.org
- * Date: 22 May 2004
- * Drupal Version: cvs (22 May 2004)
+ * @file
+ * Renders nodes as PDF files for download
  */
  
 /**
- * @todo Expand the help text.
- * @todo Comment the source code for this module.
- * @todo Improve support for images.
- * @todo Add support for printing entire books to pdf (this may have to be implemented in book.module).
- * @todo Add support for specifying exactly which types or which specific nodes users should be allowed to output as pdf.
- * @todo Move "" over to '' where applicable
- * @todo All _hook() functions should take in every parameter, even if they aren't used.
+ * Implementation of hook_help().
  */
+function pdfview_help($section) {
+	switch ($section) {
+		case 'admin/help#pdfview':
+			return t('View your nodes as pdf.');
+		case 'admin/modules#description':
+			return t('Enables users to view nodes as pdf.');
+		}
+	}
 
+/**
+ * Implementation of hook_menu().
+ */
+function pdfview_menu($may_cache) {
+	$items = array();
+	if (!$may_cache) {
+		$items[] = array('path' => 'node/'. arg(1) .'/pdf', 'title' => t('view as pdf'),
+			'callback' => 'pdfview_node_controller',
+			'access' => user_access('access content as pdf'),
+			'type' => MENU_CALLBACK
+			);
+		$items[] = array('path' => 'pdfview/'. arg(1), 'title' => t('view as pdf'),
+			'callback' => 'pdfview_node_controller',
+			'access' => user_access('access content as pdf'),
+			'type' => MENU_CALLBACK
+			);
+		}
+	return $items;
+	}
 
-function pdfview_help($section = 'admin/help#pdfview') {
-  $output = '';
-  switch ($section) {
-    case 'admin/help#pdfview':
-      $output = t('View your nodes as pdf.');
-      break;
-    case 'admin/modules#description':
-      $output = t('Enables users to view nodes as pdf.');
-      break;
-  }
-  return $output;
-}
-
-function pdfview_pdf ($node = 0, $depth = 1) {
-  if (user_access('access content as pdf')) {
-	//now using HTML2FPDF for more accuracy  
-    //require_once('pdf.class.php');
-	
-	require_once('html2fpdf/html2fpdf.php'); 
-
-    $pageheight = 770;
-    $pagewidth = 540;
-    
-	//now using HTML2FPDF for more accuracy
-    //$pdf = new PDF();
-	$pdf = new HTML2FPDF();
-    $pdf->Open();
-    $pdf->AddPage();
-	
-	echo $node->type;
-	
-	$node = node_load(array("nid" => $node));
-	//$text = $node->body;
-	//$p = explode("\n", $text);
-	$output = $node->body;
-	$output = explode("\n", $output);
-	
-	$title = $node->title;
-	$pdf->SetFont('helvetica','B',15);
-	$w = $pdf->GetStringWidth($title)+6;
-	$pdf->SetX((210-$w)/2);
-	$pdf->SetLineWidth(1);
-	$pdf->Cell($w,9,$title,0,1,'C');
-	$pdf->Ln(5);
-
-	$author = 'By '. strip_tags(format_name($node));
-	$pdf->SetFont('helvetica','BI',10);
-	$pdf->MultiCell(0, 5, $author, 0, 'L', 0);
-
-	$published = 'Published: '. format_date($node->created, 'small');
-	$pdf->SetFont('', '', 10);
-	$pdf->MultiCell(0, 5, $published, 0, 'L', 0);
-	$pdf->Ln(5);
-
-    // node type specific stuff.
-    if ($node->type == "image") {
-		$image = image_display($node);
-		$pdf->WriteHTML($image);
-		}
-
-    // Write the node's body to the PDF document.
-	$pdf->SetFont('helvetica', '', 11);
-	$param["height"] = 10;
-	foreach ($output as $one) {
-		$pdf->WriteHTML($one);
-		}
-
-    $filename = strtr(strip_tags($node->title), " .,?!&#", "_______");
-    header("Content-Disposition: attachment; filename=". $filename .".pdf");
-    header("Content-Type: application/pdf");
-
-    print $pdf->Output($filename.'.pdf', 'D');
-    die();
-  }
-}
+/**
+ * Implementation of hook_link().
+ */
+function pdfview_link($type, $node = 0, $main) {
+	$links = array();
+	if ($type == 'node' && $main == 0) {
+		$links[] = theme('pdfview_link', $node);
+		}
+	return $links;
+	}
 
+/**
+ * Implementation of hook_perm().
+ */
 function pdfview_perm() {
-  return array("access content as pdf");
-}
+	return array("access content as pdf");
+	}
 
-function pdfview_page($nid) {
-  if (user_access('access content as pdf')) {
-    if ($node != null)
-      pdfview_pdf($nid);
-    else
-      switch (arg(1)) {
-        case 'view':
-          if (arg(2)) {
-            pdfview_pdf(arg(2));
-          }
-          break;
-        default:
-          $output = t('You must specify which node you wish to view as pdf.');
-          print theme('page', $output);
-          break;
-      }
-  }
-  else {
-    print theme('page', t("Access Denied"), message_access());
-  }
-}
-
-function pdfview_link($type, $node = null, $teaser = false) {
-  $links = array();
-  if (($type == "node") && (user_access("access content as pdf"))) {
-    $links[] = l(t("view as pdf"), "pdfview/view/$node->nid", array("title" => t("View and print node as pdf.")));
-  }
-  
-  return $links;
-}
+/**
+ * Module functions.
+ */
+function pdfview_node_controller() {
+	$nid = arg(1);
+	if (is_numeric($nid)) {
+		pdfview_generate_file($nid);
+		}
+	}
+	
+/**
+ * Outputs a pdf file.
+ */
+function pdfview_generate_page($pdf, $nid) {
+	if (user_access('access content as pdf')) {	
+		$node = node_load($nid);
+		$body = $node->body;
+		$body = explode("\n", $body);
+		$title = $node->title;
+		$author = 'By '. strip_tags(theme('username', $node));
+		$published = 'Published: '. format_date($node->created, 'small');
+		
+		$pdf->AddPage();
+		
+		//title
+		$pdf->SetFont('helvetica','B',15);
+		$w = $pdf->GetStringWidth($title)+6;
+		$pdf->SetX((210-$w)/2);
+		$pdf->SetLineWidth(1);
+		$pdf->Cell($w,9,$title,0,1,'C');
+		$pdf->Ln(5);
+		
+		//author
+		$pdf->SetFont('helvetica','BI',10);
+		$pdf->MultiCell(0, 5, $author, 0, 'L', 0);
+		
+		//published
+		$pdf->SetFont('', '', 10);
+		$pdf->MultiCell(0, 5, $published, 0, 'L', 0);
+		$pdf->Ln(5);
+		
+		//node type specific stuff.
+		if ($node->type == "image") {
+			$image = image_display($node);
+			$pdf->WriteHTML($image);
+			}
+		
+		//body
+		$pdf->SetFont('helvetica', '', 11);
+		$param["height"] = 10;
+		foreach ($body as $one) {
+			$pdf->WriteHTML($one);
+			}
 
-function pdfview_menu($may_cache) {
-  $items = array();
-  
-  $items[] = array('path' => 'pdfview', 'title' => t('view as pdf'),
-    'callback' => 'pdfview_page',
-    'callback arguments' => $node->nid,
-    'type' => MENU_CALLBACK,
-    'access' => user_access('access content as pdf'));
-  return $items;
-}
+		return $pdf;
+		}
+	}	
 
+/**
+ * Outputs an array of book nodes (children)
+ */
+function pdfview_book_recurse($pdf, $nid, $depth = 10) {
+	$result = db_query(db_rewrite_sql('SELECT n.nid, n.title, b.weight FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE n.status = 1 AND n.nid = %d AND n.moderate = 0 ORDER BY b.weight, n.title'), $nid);
+	while ($page = db_fetch_object($result)) {
+		$node = node_load($page->nid);
+		if ($node) {
+			$pdf = pdfview_generate_page($pdf, $node->nid);
+			$children = db_query(db_rewrite_sql('SELECT n.nid, n.title, b.weight FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE n.status = 1 AND b.parent = %d AND n.moderate = 0 ORDER BY b.weight, n.title'), $node->nid);
+			while ($childpage = db_fetch_object($children)) {
+				$childnode = node_load($childpage->nid);				
+				if ($childnode->nid != $node->nid) {
+					$pdf = pdfview_book_recurse($pdf, $childnode->nid, $depth + 1);
+					}
+				}
+			}
+		}
+	return $pdf;
+	}	
+	
+/**
+ * Implementation of hook_form().
+ */
+function pdfview_form_alter($form_id, &$form) {
+	//alter content type settings
+	if (isset($form['type']) && $form['type']['#value'].'_node_settings' == $form_id) {
+		$form['workflow']['pdfview_'. $form['type']['#value']] = array(
+			'#type' => 'radios', 
+			'#title' => t('Download PDF'), 
+			'#default_value' => variable_get('pdfview_'.$form['type']['#value'], 1),
+			'#options' => array(t('Disabled'), t('Enabled')),
+			'#description' => t('Enable download footer link for those with access control permission.'),
+			 );
+		}
+	}
+	
+/**
+ * Outputs a pdf file.
+ */
+function pdfview_generate_file($nid) {
+	global $base_url;
+	
+	if (user_access('access content as pdf')) {	
+		require_once(drupal_get_path('module', 'pdfview') . '/html2fpdf/html2fpdf.php'); 
+		
+		$pageheight = 770;
+		$pagewidth = 540;
+		$pdf = new HTML2FPDF();
+		$pdf->Open();
+
+		$initial_node = node_load($nid);
+		if (!isset($initial_node->parent)){
+			$pdf = pdfview_generate_page($pdf, $nid);
+			} else { //print book
+			$depth = count(book_location($initial_node)) + 1;
+			$pdf = pdfview_book_recurse($pdf, $nid, $depth);
+			}		
+		
+		$filename = strtr(strip_tags($node->title), " .,?!&#", "_______");
+		drupal_set_header("Content-Disposition: attachment; filename=". $filename .".pdf");
+		drupal_set_header("Content-Type: application/pdf");
+		
+		print $pdf->Output($filename.'.pdf', 'D');
+		die();
+		}
+	}
+	
+function theme_pdfview_link($node) {
+	if ((user_access('access content as pdf') || user_access('administer nodes')) && variable_get('pdfview_'.$node->type, 1) == 1) {
+		$attributes = array("title" => "Display a PDF version of this page.");
+		$links = l(t('download pdf'), "node/$node->nid/pdf", $attributes);
+		}
+	return $links;
+	}	
 ?>
