<?php
defined('_JEXEC') or die('Restricted access');
JToolBarHelper::title(JText::_('NS Pro Subscribers'), 'user.png');

jimport('joomla.application.component.helper');
$params = JComponentHelper::getParams( 'com_nspro' );
$component = JComponentHelper::getComponent( 'com_nspro' );
$params = new JParameter( $component->params );

$import_delim = $params->get('import_delimiter', ',');
$import_ns_delim = $params->get('import_ns_delimiter', '0');
$ns_ldelim = '(';
$ns_rdelim = ')';
if ($import_ns_delim == '1') {
  $ns_ldelim = '&lt;';
  $ns_rdelim = '&gt;';
}
else if ($import_ns_delim == '2') {
  $ns_ldelim = '[';
  $ns_rdelim = ']';
}
else if ($import_ns_delim == '3') {
  $ns_ldelim = '{';
  $ns_rdelim = '}';
}

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

?>
<script type="text/javascript"><!--

  function submitForm() {
    document.adminForm.submit();
  }

--></script>
<div id="statcell">
    <table class="adminlist">
    <thead>
        <tr>
            <th>
                <?php echo JText::_('Subscription Count'); ?>
            </th>
            <th>
                <?php echo JText::_( 'Confirmed Subscriptions' ); ?>
            </th>
            <th>
                <?php echo JText::_( 'First Subscription' ); ?>
            </th>
            <th>
                <?php echo JText::_( 'Last Subscription' ); ?>
            </th>
            <th>
                <?php echo JText::_( 'Subscriptions per day' ); ?>
            </th>
        </tr>
    </thead>
    <tbody>
      <tr>
        <td align="center">
          <?php
            $mycount = count($this->allSubs);
            print $mycount;
          ?>
        </td>
        <td align="center">
          <?php
            $actives = 0;
            for ($i=0, $n=count( $this->allSubs ); $i < $n; $i++)
            {
              $row = $this->allSubs[$i];
              if ($row->confirmed == 1) {
                $actives = $actives + 1;
              }
            }
            if (count($this->allSubs) != 0) {
              $cpt = ($actives * 100) / count($this->allSubs);
              print $actives . ' (';
              printf("%01.2f", $cpt);
              print ' %)';
            }
            else {
              print $actives;
            }
          ?>
        </td>
        <td align="center">
          <?php
            $row = $this->allSubs[0];
            $firstDate = $row->datetime;
            for ($i=0, $n=count( $this->allSubs ); $i < $n; $i++)
            {
              $row = $this->allSubs[$i];
              if ($row->datetime < $firstDate) {
                $firstDate = $row->datetime;
              }
            }
            print $firstDate;
          ?>
        </td>
        <td align="center">
          <?php
            
            $row = $this->allSubs[0];
            $lastDate = $row->datetime;
            for ($i=0, $n=count( $this->allSubs ); $i < $n; $i++)
            {              
              $row = $this->allSubs[$i];
              if ($row->datetime >= $lastDate) {
                $lastDate = $row->datetime;
              }
            }
            print $lastDate;
          ?>
        </td>
        <td align="center">
          <?php

            $row = $this->allSubs[0];

            $firstDate = strtotime($firstDate);
            $lastDate = strtotime($lastDate);
            $diff = intval(($lastDate-$firstDate) / 86400) + 1;

            if ($diff == 0) {
              $diff = 1;
            }
            $spd  = count($this->allSubs) / $diff;
            printf("%01.2f", $spd);
            //print ' '.JText::_('in').' '.$diff.' '.JText::_('days');

            ?>
        </td>
      </tr>
      </tbody>
    </table>
</div>
<form action="index.php" method="post" name="adminForm">
<div id="editcell">
    <table style="padding-bottom: 5px; width: 100%;">
    <tr><td align="right"><?php
           
      $db->setQuery('SELECT `id`, `lname` FROM `#__nspro_lists` WHERE `published` = 1');
      $mlist = $db->loadObjectList();     
      
      $first_element = array();
      $firstEl = new stdClass();
      $firstEl->id = -1;
      $firstEl->lname = JText::_('No List');
      $first_element[] = $firstEl;
      $mlist = array_merge($first_element, $mlist);      
      $first_element = array();
      $firstEl = new stdClass();
      $firstEl->id = 0;
      $firstEl->lname = JText::_('All Lists');
      $first_element[] = $firstEl;
      $mlist = array_merge($first_element, $mlist);
      print JHTML::_('select.genericlist',  $mlist , 'select_list', 'onChange="submitForm();"', 'id', 'lname', $this->filter_subs);
    
    ?></td></tr>
    </table>
    <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->subs ); ?>);" />
            </th>
            <th class="title">
                <?php echo JText::_('Name'); ?>
            </th>
            <th class="email">
                <?php echo JText::_( 'Email' ); ?>
            </th>
            <th class="datetime" align="center">
                <?php echo JText::_( 'Date and Time' ); ?>
            </th>            
            <th width="5">
                <?php echo JText::_( 'Confirmed' ); ?>
            </th>
            <th>
                <?php echo JText::_( 'Mailing Lists' ); ?>
            </th>            
            <th width="40%" class="notes">
                <?php echo JText::_( 'Notes' ); ?>
            </th>
        </tr>
    </thead>
    <tfoot>
        <tr>
            <td colspan="8">
                <?php
                  print $this->pagination->getListFooter();
                ?>
            </td>
        </tr>
    </tfoot>
    <tbody>
    <?php
    $k = 0;
    for ($i=0, $n=count( $this->subs ); $i < $n; $i++)
    {
        $row = &$this->subs[$i];
        $row->checked_out = false;
        $checked    = JHTML::_('grid.checkedout', $row, $i );
        $link       = JRoute::_( 'index.php?option=com_nspro&controller=nspro&task=edit&cid[]='. $row->id );
        $conflink   = JRoute::_( 'index.php?option=com_nspro&controller=nspro&task=qconfirm&cid[]='. $row->id );
        $uconflink  = JRoute::_( 'index.php?option=com_nspro&controller=nspro&task=quconfirm&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 echo $row->name; ?></a>
            </td>
            <td>
                <?php echo $row->email; ?>
            </td>
            <td align="center">
                <?php echo $row->datetime; ?>
            </td>
            <td align="center">
              <?php
              if ($row->confirmed) {
                print '<a href="'.$uconflink.'">'.JText::_('Yes').'</a>';
              }
              else {
                print '<a href="'.$conflink.'">'.JText::_('No').'</a>';
              }
              ?>
            </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>
                <?php echo $row->notes; ?>
            </td>
        </tr>
        <?php
        $k = 1 - $k;
    }
    ?>
      </tbody>
    </table>
</div>

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

<div class="icon-48-cpanel header">NS Pro Tools</div>
<div id="nspro_tools">
<table class="toolbar nspro">
<tr>
<td id="toolbar-archive">
<a class="toolbar" onclick="javascript:submitbutton('exporttxt');" href="#">
<span class="icon-32-archive" title="Export Txt"> </span>
Export Text File (*.txt)
</a>
</td>
<td id="toolbar-archive">
<a class="toolbar" onclick="javascript:submitbutton('exportcsv');" href="#">
<span class="icon-32-archive" title="Export Csv"> </span>
Export Excel File (*.csv)
</a>
</td>
<td id="toolbar-archive">
<a class="toolbar" onclick="javascript:submitbutton('exportns');" href="#">
<span class="icon-32-archive" title="Export Newsletter Subscriber List File"> </span>
Export Newsletter Subscriber List File
</a>
</td>
<td id="toolbar-archive">
<a class="toolbar" onclick="javascript:submitbutton('exportxml');" href="#">
<span class="icon-32-archive" title="Export XML"> </span>
Export XML
</a>
</td>
<td id="toolbar-unarchive">
<a class="toolbar" onclick="javascript:submitbutton('importFromJoomla');" href="#">
<span class="icon-32-unarchive" title="Import From Joomla Users"> </span>
Import From Joomla Users
</a>
</td></tr></table>
<br />

<form enctype="multipart/form-data" action="index.php" method="POST" name="fileform">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000" />

<table class="adminform">
<tr><th colspan="2"><?php print JText::_('Import from Text/Excel File') ?></th></tr>
<tr><td width="150"><?php print JText::_('Import File:') ?></td><td><input class="input_box" name="uploadedfile" type="file" size="57"/> <input type="submit" value="<?php print JText::_('Upload File & Import') ?>"/></td></tr>
<tr><td><?php print JText::_('Import Source/Type');?></td>
<td><input type="radio" name="inputtype" value="0" checked>Newsletter Subscriber List File&nbsp;&nbsp;<i>Name <?php print $ns_ldelim; ?>email@site.com<?php print $ns_rdelim; ?>;</i></input>
<br/><input type="radio" name="inputtype" value="1">Text File separated by Import Delimiter (see parameters)&nbsp;&nbsp;<i>Name<?php print $import_delim; ?>email@site.com<?php print $import_delim; ?>date<?php print $import_delim; ?>confirmed<?php print $import_delim; ?>mailing lists<?php print $import_delim; ?></i></input>
<br/><input type="radio" name="inputtype" value="2">Excel File separated by Import Delimiter (see parameters)&nbsp;&nbsp;<i>Name<?php print $import_delim; ?>email@site.com<?php print $import_delim; ?>date<?php print $import_delim; ?>confirmed<?php print $import_delim; ?>mailing lists<?php print $import_delim; ?></i></input></td></tr>
</table>

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

<center><small><i>NS Pro by <a href="http://www.mavrosxristoforos.com/">Mavrosxristoforos.com</a> Copyright 2010</i></small></center>
</div>
