<?php // newsletter Builder Code Generator Functions

//
function nlb_codeGenerator_showPage() {

  // Support default "Go Back >>" button
  if (array_key_exists('_showCode', $_REQUEST)) { redirectBrowserToURL('?menu=_codeGenerator'); }

  //
  $GENERATOR_FILENAMES_TO_DESCRIPTIONS = array(
#    'nl_signup.php'  => "Simple Signup Form - Allow users to enter their email to signup.",
    'nl_manage.php'  => "Manage Subscriptions - Allow users to subscribe, confirm and unsubscribe.",
    'nl_archive.php' => "Newsletter Archive - For viewing current and previous newsletters.",
  );

  
  // show header
  $function = "nlb_codeGenerator";
  echo cg2_header($function, $GLOBALS['NEWSLETTER_BUILDER_PLUGIN']);

  // show page menu
  print "<ul style='padding-top: 0; padding-bottom: 0;'>\n";
  foreach ($GENERATOR_FILENAMES_TO_DESCRIPTIONS as $filename => $description) {
    $url = thisPageUrl(array('filename' => $filename));
    print  "  <li><a href='$url'>" .htmlencode($description).  "</a></li>\n";
  }
  print "</ul>\n";

  // show page
  $filename = @$_REQUEST['filename'];
  if ($filename && @$GENERATOR_FILENAMES_TO_DESCRIPTIONS[$filename]) {

    // show separator bar
    print adminUI_separator(t('Source Code'));

    // get function name
    $getCodeFunction = "nlbcode_" . preg_replace("/[^a-z\_]+/s", "_", $filename);
    if (!function_exists($getCodeFunction)) { die("Function $getCodeFunction doesn't exist!"); }

    // show instructions
    $instructions   = array(); // show as bullet points
    $instructions[] = t(sprintf("Save this code as <b>%s</b> (or choose your own name)", $filename));
    if ($filename == 'nl_archive.php') { $instructions[] = sprintf('Update the <a href="%s">Archive Url</a> under Newsletter Settings', '?menu=_nlb_settings'); }
    if ($filename == 'nl_manage.php')  { $instructions[] = sprintf('Update the <a href="%s">Manage Url</a> under Newsletter Settings', '?menu=_nlb_settings'); }
    
//   $instructions[] = sprintf('Update the <a href="%s">Manage Url</a> under Newsletter Settings', '?menu=_nlb_settings');

    $filenameSuffix = 'list'; // eg: tablename-FILENAMESUFFIX.php
    cg2_showCode(null, null, $instructions, null, $getCodeFunction());
    exit;
  }

  // show footer (cg2_showCode sends it automatically)
  echo cg2_footer();
  exit;
}

//
function nlbcode_nl_manage_php() {
  $settingsUrl = $GLOBALS['SETTINGS']['adminUrl'] . "?menu=_nlb_settings";

  ### generate code
  ob_start();
?>
<#php
  <?php cg2_code_loadLibraries(); ?>

  // error checking
  if (!@$GLOBALS['NEWSLETTER_BUILDER_PLUGIN']) { die("You must activate the newsletter plugin to see this page."); }

  // 
  list($errorsAndAlerts, $lists, $authUserNum, $authUserEmail) = nlb_frontend_dispatcher3();

#><?php cg2_code_header(); ?>

  <!-- INSTRUCTIONS -->
    <div class="instructions">
      <b>Sample Subscriber Script - Instructions:</b>
      <ol>
        <#php /*><li style="color: red; font-weight: bold">Rename this file to have a .php extension!</li><x */ #>
        <li>Newsletter subscribers will use this script to subscribe, confirm subscriptions, and unsubscribe</li>
        <li>Modify the HTML and design of this page as needed, then update <a href="<?php echo $GLOBALS['SETTINGS']['adminUrl'] ?>?menu=_nlb_settings">Manage Url</a> under Newsletter Settings.</li>
      </ol>
    </div>
  <!-- /INSTRUCTIONS -->

<#php /*** START: SUBSCRIBE FORM - Shown when a user uses the web form to signup ***/ #>
  <h1>Manage Subscriptions</h1>
  
  <#php if ($errorsAndAlerts): #>
    <div style="color: #C00; font-weight: bold; font-size: 14px;"><#php echo $errorsAndAlerts; #></div><br/>
  <#php endif #>

  <form method="post" action="?">
    <input type="hidden" name="submitForm" value="1" />
    <input type="hidden" name="n" value="<#php echo htmlencode(@$_REQUEST['n']); #>" /><#php // subscriber num #>
    <input type="hidden" name="a" value="<#php echo htmlencode(@$_REQUEST['a']); #>" /><#php // subscriber authkey #>
    <input type="hidden" name="m" value="<#php echo htmlencode(@$_REQUEST['m']); #>" /><#php // message num #>

    <div style="float: left; width: 100px; clear: both;">Email:</div>
    <div style="float: left; ">
      <#php if (@$authUserNum): #>
        <#php echo htmlencode($authUserEmail); #>
      <#php else: #>
        <input type="text" name="e" value="<#php echo htmlencode(@$_REQUEST['e']) #>" size="50"/>
      <#php endif #>
    </div>
    
    <div style="float: left; width: 100px; clear: both;">Newsletters:</div>
    <div style="float: left;">
      <#php foreach ($lists as $list): #>
        <#php
          $isChecked   = @$_REQUEST['lists'] && in_array($list['num'], $_REQUEST['lists']);
          $checkedAttr = checkedIf($isChecked, true, true);
        #>
        <label>
          <input type="checkbox" name="lists[]" value="<#php echo htmlencode($list['num']); #>"  <#php echo $checkedAttr; #> />
          <#php echo htmlencode($list['name']); #><br/>
        </label>
      <#php endforeach #>
      <input type="submit" name="subscribe" value="Update Subscriptions" /><br/>
    </div>
    
    <div style="clear: both;"></div>
    
  </form>
<#php /*** END: SUBSCRIBE FORM ***/ #>

</body>
</html>

<?php cg2_code_footer(); ?>

<?php
  // return code
  $code = ob_get_clean();
  return $code;
}

//
function nlbcode_nl_archive_php() {
  $settingsUrl = $GLOBALS['SETTINGS']['adminUrl'] . "?menu=_nlb_settings";

  ### generate code
  ob_start();
?>
<#php
  <?php cg2_code_loadLibraries(); ?>

  // 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);

#><?php cg2_code_header(); ?>

  <!-- 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="<?php echo $settingsUrl ?>">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 $record['num']; #>"><#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>

<?php cg2_code_footer(); ?>

<?php
  // return code
  $code = ob_get_clean();
  return $code;
}

?>