$deactivated = (int) $deactivated;
 $newstype = (int) $newstype;
 $recipient->set("name", $name);
 if (!isValidMail($email)) {
     $aMessages[] = i18n("Please specify a valid e-mail address", $plugin_name);
 } else {
     $email = strtolower($email);
     // e-mail always in lower case
     if ($recipient->get("email") != $email) {
         $oRecipients->resetQuery();
         $oRecipients->setWhere("email", $email);
         $oRecipients->setWhere("idclient", $client);
         $oRecipients->setWhere("idlang", $lang);
         $oRecipients->setWhere($recipient->primaryKey, $recipient->get($recipient->primaryKey), "!=");
         $oRecipients->query();
         if ($oRecipients->next()) {
             $aMessages[] = i18n("Could not set new e-mail adress: Other recipient with same e-mail address already exists", $plugin_name);
         } else {
             $recipient->set("email", $email);
         }
     }
 }
 if ($recipient->get("confirmed") != $confirmed && $confirmed) {
     $recipient->set("confirmeddate", date("Y-m-d H:i:s"), false);
 } elseif (!$confirmed) {
     $recipient->set("confirmeddate", "0000-00-00 00:00:00", false);
 }
 $recipient->set("confirmed", $confirmed);
 $recipient->set("deactivated", $deactivated);
 $recipient->set("news_type", $newstype);
 // Check out if there are any plugins
 /**
  * Gets all active recipients as specified for the newsletter and adds for
  * every recipient a log item
  * @param integer    $idnewsjob    ID of corresponding newsletter dispatch job
  * @param integer    $idnews        ID of newsletter
  * @return integer    Recipient count
  */
 public function initializeJob($idnewsjob, $idnews)
 {
     global $cfg;
     $idnewsjob = Contenido_Security::toInteger($idnewsjob);
     $idnews = Contenido_Security::toInteger($idnews);
     $oNewsletter = new Newsletter();
     if ($oNewsletter->loadByPrimaryKey($idnews)) {
         $sDestination = $oNewsletter->get("send_to");
         $iIDClient = $oNewsletter->get("idclient");
         $iIDLang = $oNewsletter->get("idlang");
         switch ($sDestination) {
             case "all":
                 $sDistinct = "";
                 $sFrom = "";
                 $sSQL = "deactivated='0' AND confirmed='1' AND idclient='" . $iIDClient . "' AND idlang='" . $iIDLang . "'";
                 break;
             case "default":
                 $sDistinct = "distinct";
                 $sFrom = $cfg["tab"]["news_groups"] . " AS groups, " . $cfg["tab"]["news_groupmembers"] . " AS groupmembers ";
                 $sSQL = "recipientcollection.idclient = '" . $iIDClient . "' AND " . "recipientcollection.idlang = '" . $iIDLang . "' AND " . "recipientcollection.deactivated = '0' AND " . "recipientcollection.confirmed = '1' AND " . "recipientcollection.idnewsrcp = groupmembers.idnewsrcp AND " . "groupmembers.idnewsgroup = groups.idnewsgroup AND " . "groups.defaultgroup = '1' AND groups.idclient = '" . $iIDClient . "' AND " . "groups.idlang = '" . $iIDLang . "'";
                 break;
             case "selection":
                 $aGroups = unserialize($oNewsletter->get("send_ids"));
                 if (is_array($aGroups) && count($aGroups) > 0) {
                     $sGroups = "'" . implode("','", $aGroups) . "'";
                     $sDistinct = "distinct";
                     $sFrom = $cfg["tab"]["news_groupmembers"] . " AS groupmembers ";
                     $sSQL = "recipientcollection.idclient = '" . $iIDClient . "' AND " . "recipientcollection.idlang = '" . $iIDLang . "' AND " . "recipientcollection.deactivated = '0' AND " . "recipientcollection.confirmed = '1' AND " . "recipientcollection.idnewsrcp = groupmembers.idnewsrcp AND " . "groupmembers.idnewsgroup IN (" . $sGroups . ")";
                 } else {
                     $sDestination = "unknown";
                 }
                 break;
             case "single":
                 $iID = $oNewsletter->get("send_ids");
                 if (is_numeric($iID)) {
                     $sDistinct = "";
                     $sFrom = "";
                     $sSQL = "idnewsrcp = '" . $iID . "'";
                 } else {
                     $sDestination = "unknown";
                 }
                 break;
             default:
                 $sDestination = "unknown";
         }
         unset($oNewsletter);
         if ($sDestination == "unknown") {
             return 0;
         } else {
             $oRecipients = new RecipientCollection();
             $oRecipients->flexSelect($sDistinct, $sFrom, $sSQL, "", "", "");
             $iRecipients = $oRecipients->count();
             while ($oRecipient = $oRecipients->next()) {
                 $this->create($idnewsjob, $oRecipient->get($oRecipient->primaryKey));
             }
             return $iRecipients;
         }
     } else {
         return 0;
     }
 }
    if ($_REQUEST["elemperpage"] * $_REQUEST["page"] >= $iItemCount + $_REQUEST["elemperpage"] && $_REQUEST["page"] != 1) {
        $_REQUEST["page"]--;
    }
    $oRecipients->setLimit($_REQUEST["elemperpage"] * ($_REQUEST["page"] - 1), $_REQUEST["elemperpage"]);
} else {
    $iItemCount = 0;
}
$oRecipients->query();
// Output data
$oMenu = new UI_Menu();
$iMenu = 0;
// Store messages for repeated use (speeds performance, as i18n translation is only needed once)
$aMsg = array();
$aMsg["DelTitle"] = i18n("Delete recipient", $plugin_name);
$aMsg["DelDescr"] = i18n("Do you really want to delete the following recipient:<br />", $plugin_name);
while ($oRecipient = $oRecipients->next()) {
    $iMenu++;
    $idnewsrcp = $oRecipient->get("idnewsrcp");
    $sName = $oRecipient->get("name");
    if (empty($sName)) {
        $sName = $oRecipient->get("email");
    }
    $oLnk = new cHTMLLink();
    $oLnk->setMultiLink($area, "", $area, "");
    $oLnk->setCustom("idrecipient", $idnewsrcp);
    if ($oRecipient->get("deactivated") == 1 || $oRecipient->get("confirmed") == 0) {
        $oLnk->updateAttributes(array("style" => "color:#A20000"));
    }
    $oMenu->setTitle($iMenu, $sName);
    $oMenu->setLink($iMenu, $oLnk);
    if ($perm->have_perm_area_action("recipients", "recipients_delete")) {
     // Getting item count without limit (for page function) - better idea anyone (performance)?
     $sSQLLimit = " LIMIT " . $_REQUEST["outsider_elemperpage"] * ($_REQUEST["outsider_page"] - 1) . ", " . $_REQUEST["outsider_elemperpage"];
 } else {
     $iMembers = 0;
     $sSQLLimit = "";
 }
 // Get data
 $sSQLSort = " ORDER BY " . urlencode($_REQUEST["outsider_sortby"]) . " " . $_REQUEST["outsider_sortorder"];
 if ($_REQUEST["outsider_sortby"] == "name") {
     // Name field may be empty, add email as sort criteria
     $sSQLSort .= ", email " . $_REQUEST["outsider_sortorder"];
 }
 $sSQL .= $sSQLSort . $sSQLLimit;
 $oOutsiders->flexSelect("", "", $sSQL, "");
 $aItems = array();
 while ($oRecipient = $oOutsiders->next()) {
     $sName = $oRecipient->get("name");
     $sEMail = $oRecipient->get("email");
     if (empty($sName)) {
         $sName = $sEMail;
     }
     $aItems[] = array($oRecipient->get("idnewsrcp"), $sName . " (" . $sEMail . ")");
 }
 $oSelUser = new cHTMLSelectElement("adduser[]");
 $oSelUser->setSize(25);
 $oSelUser->setStyle("width: 100%;");
 $oSelUser->setMultiSelect();
 $oSelUser->autoFill($aItems);
 // Outsider list pager (-> below data, as iOutsiders is needed)
 $oPagerLink = new cHTMLLink();
 $oPagerLink->setLink("main.php");
 /**
  * checkEMail returns true, if there is no recipient with the same e-mail address; otherwise false
  * @param  $email string    e-mail
  * @return recpient item if item with e-mail exists, false otherwise
  */
 public function emailExists($sEmail)
 {
     global $client, $lang;
     $oRecipientCollection = new RecipientCollection();
     $oRecipientCollection->setWhere("idclient", $client);
     $oRecipientCollection->setWhere("idlang", $lang);
     $oRecipientCollection->setWhere("email", strtolower($sEmail));
     $oRecipientCollection->query();
     if ($oItem = $oRecipientCollection->next()) {
         return $oItem;
     } else {
         return false;
     }
 }