<?php
defined('_JEXEC') or die('Restricted access');
JToolBarHelper::title(JText::_('NS Pro Newsletters'), 'inbox.png');
JToolBarHelper::preferences('com_nspro', 400);

$db =& JFactory::getDBO();
$db->setQuery('SELECT `id`, `lname` FROM `#__nspro_lists` WHERE `published` = 1');
$lists = $db->loadObjectList();   

?>

<form action="index.php" method="post" name="adminForm">
<div id="editcell">
    <table class="adminlist">
    <thead>
        <tr>
            <th width="5">
                <?php echo JText::_( 'ID' ); ?>
            </th>
            <th width="20">
                <input type="checkbox" name="toggle" value="" onclick="checkAll(<?php echo count( $this->newsletters ); ?>);" />
            </th>
            <th class="edit">
                <?php echo JText::_('Edit'); ?>
            </th>
            <th class="subject">
                <?php echo JText::_( 'Subject' ); ?>
            </th>
            <th class="title">
                <?php echo JText::_('From Name'); ?>
            </th>
            <th class="email">
                <?php echo JText::_( 'From Email' ); ?>
            </th>
            <th class="datetime" align="center">
                <?php echo JText::_( 'Date and Time' ); ?>
            </th>
            <th width="5">
                <?php echo JText::_( 'Sent' ); ?>
            </th>
            <th>
               <?php echo JText::_('Mailing Lists'); ?>
            </th>
            <th width="5" align="center">
              <img src="components/com_nspro/img/attachment-icon-fornspronewsletters.png" />
            </th>
            <th width="40%" class="message">
                <?php echo JText::_( 'Message' ); ?>
            </th>
        </tr>
    </thead>
    <tfoot>
        <tr>
            <td colspan="12">
                <?php
                  print $this->pagination->getListFooter();
                ?>
            </td>
        </tr>
    </tfoot>
    <tbody>
    <?php
    $k = 0;
    for ($i=0, $n=count( $this->newsletters ); $i < $n; $i++)
    {
        $row = &$this->newsletters[$i];
        
        $query = 'SELECT * FROM `#__nspro_newsletter_attachments` WHERE `newsletter_id` = ' . $row->id . ' ORDER BY `id`';
        $db->setQuery($query);
        $mattachments = $db->loadObjectList();
        
        $hasAttachment = false;
        if (count($mattachments) > 0) {
          $hasAttachment = true;
        }

        $row->checked_out = false;
        $checked    = JHTML::_('grid.checkedout', $row, $i );
        $link       = JRoute::_( 'index.php?option=com_nspro&controller=nspro&task=nedit&cid[]='. $row->id );

        ?>
        <tr class="<?php echo "row$k"; ?>">
            <td>
                <?php echo $row->id; ?>
            </td>
            <td>
                <?php echo $checked; ?>
            </td>
            <td>
                <a href="<?php echo $link; ?>"><?php print JText::_('Edit'); ?></a>
            </td>
            <td>
                <?php echo $row->subject; ?>
            </td>
            <td>
                <?php echo $row->fName; ?>
            </td>
            <td>
                <?php echo $row->fEmail; ?>
            </td>
            <td align="center">
                <?php echo $row->datetime; ?>
            </td>
            <td align="center">
              <?php
              if ($row->sent) {
                print JText::_('Yes');
              }
              else {
                print JText::_('No');
              }
              ?>
            </td>
            <td align="center">
               <?php
                 $myLName = '';
                 $mySubLists = explode(',', $row->mailing_lists);                 
                 for ($j = 0; $j < count($lists); $j++) {
                   $arow = $lists[$j];
                   foreach ($mySubLists as $myListID) {
                     if ($arow->id == $myListID) {
                       if ($myLName == '') {
                         $myLName = $arow->lname;
                       }
                       else {
                         $myLName = $myLName . ', ' . $arow->lname;
                       }
                       break;
                     } 
                   }
                 }       
                 
                 if ($myLName == '') {
                   $myLName = JText::_('No List Assigned');
                 }
                 
                 print $myLName;
               
               ?>
            </td>
            <td align="center">
            <?php
            
              if ($hasAttachment) {
                ?><img src="components/com_nspro/img/attachment-icon-fornspronewsletters.png" /><?php
              }
            
            ?>
            </td>
            <td>
                <?php
                  $msg = $row->message;
                  if (JString::strlen($msg) > 100) {
                    print htmlentities(JString::substr($msg, 0, 100) . '...');
                  }
                  else {
                    print htmlentities($msg);
                  }
                ?>
            </td>
        </tr>
        <?php
        $k = 1 - $k;
    }
    ?>
      </tbody>
    </table>
<br/>
<center><small><i>NS Pro by <a href="http://www.mavrosxristoforos.com/">Mavrosxristoforos.com</a> Copyright 2010</i></small></center>
</div>

<input type="hidden" name="option" value="com_nspro" />
<input type="hidden" name="view" value="newsletters" />
<input type="hidden" name="task" value="" />
<input type="hidden" name="boxchecked" value="0" />
<input type="hidden" name="controller" value="nspro" />
</form>