<?php // no direct access
defined('_JEXEC') or die('Restricted access');

$message = '';

if ($_POST["meditlists"]) {
  $db =& JFactory::getDBO();

  $db->setQuery('SELECT * FROM `#__nspro_subs` WHERE `email` = "' . $_POST["realmail"] .'"');
  $msub = $db->loadObject();

  $newList = $_POST["mailing_lists2"];
  $nstr = '';
  if (count($newList) > 0) {
    foreach($newList as $nlid) {
      if ($nstr == '') {
        $nstr = mysql_escape_string($nlid);
      }
      else {
        $nstr = $nstr . ',' . mysql_escape_string($nlid);
      }
    }
  }

  $db->setQuery('UPDATE `#__nspro_subs` SET `mailing_lists` = "' . $nstr . '" WHERE `id` = ' . $msub->id);
  $db->Query();

  $myprecious = $msub->id;
  $myRealEmail = $msub->email;
  $message = JText::_('Lists Saved Successfully');
}
else {
  $myEmail = $_REQUEST['u'];

  $myprecious = -1;

  $myRealEmail = '';

  for ($i=0, $n=count( $this->subs ); $i < $n; $i++) {
    $row = &$this->subs[$i];

    if (md5($row[2]) == $myEmail) {
      $myprecious = $row[0];
      $myRealEmail = $row[2];
      break;
    }
  }
}

if ($myprecious != -1) {
  $db =& JFactory::getDBO();

  $db->setQuery('SELECT * FROM `#__nspro_subs` WHERE `id` = ' . $myprecious);
  $msub = $db->loadObject();

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

  print '<div id="componentheading">'.JText::_('Edit Mailing List Subscriptions').'</div><br/><div id="nspro_editlists">';
  if ($message != '') {
    print $message . '<br/>';
  }
  print '<form action="index.php?option=com_nspro&layout=editlists" method="post">';
  print '<fieldset id="nspro_editlist_fieldset" style="width:500px;">';
  print '<legend>'.JText::_('Subscriptions for Email: '). $myRealEmail .'</legend>';
  print '<table cellpadding="2" id="nspro_editlist_table">';
  $mySubLists = explode(',', $msub->mailing_lists);
  for ($j = 0; $j < count($lists); $j++) {
    $arow = $lists[$j];
    print '<td><input type="checkbox" name="mailing_lists2[]" value="'.$arow->id.'"';
    if (count($mySubLists) > 0) {
      foreach ($mySubLists as $myListID) {
  	    if ($arow->id == $myListID) {
  	      print ' checked="checked"';
  	    }
  	  }
    }
    print '/></td><td>'.$arow->lname . '</td></tr>';
  }
  print '</table>';
  print '</fieldset><br/>';
  print '<input type="submit" name="submiteditlists" value="'.JText::_('Save Lists').'"/>';
  print '<input type="hidden" name="meditlists" value="true"/>';
  print '<input type="hidden" name="realmail" value="' . $myRealEmail . '"/>';
  print '</form>';
  print '</div>';
}
else {
  print JText::_('This link has expired');
}


?>
