/**
  * 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;
     }
 }
         foreach ($aFields as $sKey => $aData) {
             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, "");