/**
  * 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["searchin"] == "--all--" || $_REQUEST["searchin"] == "") {
        foreach ($aFields as $sKey => $aData) {
            if (strpos($aData["type"], "search") !== false) {
                $oRecipients->setWhereGroup("filter", "recipientcollection." . $aData["field"], $_REQUEST["filter"], "LIKE");
            }
        }
        $oRecipients->setInnerGroupCondition("filter", "OR");
    } else {
        $oRecipients->setWhere("recipientcollection." . $_REQUEST["searchin"], $_REQUEST["filter"], "LIKE");
    }
}
// Items / page
if ($_REQUEST["elemperpage"] > 0) {
    // Getting item count without limit (for page function) - better idea anyone (performance)?
    $oRecipients->query();
    $iItemCount = $oRecipients->count();
    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);
             if (strpos($aData["type"], "search") !== false) {
                 if ($sSQLSearchIn !== "") {
                     $sSQLSearchIn .= " OR ";
                 }
                 $sSQLSearchIn .= $aData["field"] . " LIKE '%" . urlencode($_REQUEST["outsider_filter"]) . "%'";
             }
         }
     } else {
         $sSQLSearchIn .= urlencode($_REQUEST["outsider_searchin"]) . " LIKE '%" . urlencode($_REQUEST["outsider_filter"]) . "%'";
     }
     $sSQL .= " AND (" . $sSQLSearchIn . ")";
 }
 // If elemperpage is something else than "all", get item count based on filters
 if ($_REQUEST["outsider_elemperpage"] > 0) {
     $oOutsiders->flexSelect("", "", $sSQL, "");
     $iOutsiders = $oOutsiders->count();
     // 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();
 /**
  * Purge method to delete recipients which hasn't been confirmed since over a month
  * @param  $timeframe int    Days after creation a not confirmed recipient will be removed
  * @return int             Count of deleted recipients
  */
 public function purge($timeframe)
 {
     global $client, $lang;
     $oRecipientCollection = new RecipientCollection();
     // DATEDIFF(created, NOW()) > 30 would be better, but it's only available in MySQL V4.1.1 and above
     // Note, that, TO_DAYS or NOW may not be available in other database systems than MySQL
     $oRecipientCollection->setWhere("idclient", $client);
     $oRecipientCollection->setWhere("idlang", $lang);
     $oRecipientCollection->setWhere("confirmed", 0);
     $oRecipientCollection->setWhere("(TO_DAYS(NOW()) - TO_DAYS(created))", $timeframe, ">");
     $oRecipientCollection->query();
     while ($oItem = $oRecipientCollection->next()) {
         $oRecipientCollection->delete($oItem->get("idnewsrcp"));
     }
     return $oRecipientCollection->count();
 }