<?php
  
  // load viewer library
  $libraryPath = 'cmsAdmin/lib/viewer_functions.php';
  $dirsToCheck = array('/hsphere/local/home/gkornblu/thecmsbcookbook.com/','','../','../../','../../../');
  foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
  if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }

  // error checking
  if (!@$GLOBALS['NEWSLETTER_BUILDER_PLUGIN']) { die("You must activate the newsletter plugin to see this page."); }

  // load newsletters settings and archives
  global $newsletterSettings;
  $newsletterSettings  = mysql_get('_nlb_settings', 1);
  $archivedNewsletters = mysql_select('_nlb_messages', "send IN ('all','archive') ORDER BY sent_date DESC");

  // load latest newsletter
  list($_nlb_messages, $_nlb_messagesMetaData) = getRecords(array(
    'tableName'   => '_nlb_messages',
    'where'       => "send IN ('all','archive') AND " . whereRecordNumberInUrl(1), // If no record # is specified then latest record is shown
    'loadUploads' => true,
    'allowSearch' => false,
    'limit'       => '1',
  ));
  $latestNewsletter = @$_nlb_messages[0]; // get first record
  if (!$latestNewsletter) { dieWith404("Couldn't find newsletter!"); } // show error message if no record found

  // replace placeholders
  $archivedNewsletters = nlb_viewers_replacePlaceholders($archivedNewsletters);
  $latestNewsletter    = nlb_viewers_replacePlaceholders($latestNewsletter);

?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <title></title>
  <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
  <style type="text/css">
    body          { font-family: arial; }
    .instructions { border: 3px solid #000; background-color: #EEE; padding: 10px; text-align: left; margin: 25px}
  </style>
 </head>
<body>


  <!-- INSTRUCTIONS -->
    <div class="instructions">
      <b>Sample Newsletter Archive - Instructions:</b>
      <ol>
        <?php /*><li style="color: red; font-weight: bold">Rename this file to have a .php extension!</li><x */ ?>
        <li>Newsletter subscribers and website visitors will use this page to view current and past newsletters</li>
        <li>Modify the HTML and design of this page as needed, then update <a href="http://thecmsbcookbook.com/cmsAdmin/admin.php?menu=_nlb_settings">Archive Url</a> under Newsletter Settings.</li>
      </ol>
    </div>
  <!-- /INSTRUCTIONS -->


<h1>Newsletter Archive</h1>

<table border="1" cellspacing="2" cellpadding="4">
 <tr>
  <td valign="top">

    <b><?php echo htmlencode($latestNewsletter['subject']) ?></b><br/>
    <?php echo $latestNewsletter['html']; ?><br/>

  </td>
  <td valign="top">

  <!-- STEP2: Display Record List (Paste this where you want your record list) -->
    <b>Latest Newsletters</b><br/><br/>

    <?php foreach ($archivedNewsletters as $record): ?>
      <?php $isSelected = ($record['num'] == $latestNewsletter['num']); ?>
      <?php if ($isSelected) { print "<b>"; } ?>
      <a href=""><?php echo htmlencode($record['subject']) ?></a><br/>
      <?php if ($isSelected) { print "</b>"; } ?>
    <?php endforeach ?>

    <?php if (!$_nlb_messages): ?>
      No newsletters were found!<br/><br/>
    <?php endif ?>
  <!-- /STEP2: Display Record List -->

  </td>
 </tr>
</table>

</body>
</html>
