* @since      file available since contenido release <= 4.6
 * 
 * {@internal 
 *   created 2007-01-01, Björn Behrens (HerrB)
 *   modified 2008-06-27, Dominik Ziegler, add security fix
 *
 *   $Id$:
 * }}
 * 
 */
if (!defined('CON_FRAMEWORK')) {
    die('Illegal call');
}
$plugin_name = 'newsletter';
$oPage = new cPage();
$oRecipients = new RecipientCollection();
if (is_array($cfg['plugins']['recipients'])) {
    foreach ($cfg['plugins']['recipients'] as $plugin) {
        plugin_include("recipients", $plugin . "/" . $plugin . ".php");
    }
}
// Note, that the object name has to be $recipient for plugins
if ($action == "recipients_create" && $perm->have_perm_area_action($area, $action)) {
    $recipient = $oRecipients->create("*****@*****.**", " " . i18n("-- new recipient --", $plugin_name));
    $oPage->setReload();
} elseif ($action == "recipients_delete" && $perm->have_perm_area_action($area, $action)) {
    $oRecipients->delete($idrecipient);
    $recipient = new Recipient();
    $oPage->setReload();
} elseif ($action == "recipients_purge" && $perm->have_perm_area_action($area, "recipients_delete")) {
    $oClient = new cApiClient($client);
 /**
  * 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;
     }
 }
 $sContent .= '         <td>' . i18n("Search for", $plugin_name) . '</td>' . chr(10);
 $sContent .= '         <td>' . $oTxtFilter->render() . '</td>' . chr(10);
 $sContent .= '      </tr>' . chr(10);
 $sContent .= '      <tr>' . chr(10);
 $sContent .= '         <td>' . i18n("Search in", $plugin_name) . '</td>' . chr(10);
 $sContent .= '         <td>' . $oSelSearchIn->render() . '</td>' . chr(10);
 $sContent .= '      </tr>' . chr(10);
 $sContent .= '      <tr>' . chr(10);
 $sContent .= '         <td>&nbsp;</td>' . chr(10);
 $sContent .= '         <td>' . $oSubmit->render() . '</td>' . chr(10);
 $sContent .= '      </tr>' . chr(10);
 $sContent .= '   </table>' . chr(10);
 $sContent .= '</div>' . chr(10);
 $oOutsiderListOptionRow->setContentData($sContent);
 // TODO: Try to enhance genericdb to get this working with the usual objects...
 $oOutsiders = new RecipientCollection();
 # This requires mySQL V4.1, at least...
 # TODO: Add mySQL server version switch
 #$sSQL = "idclient = '".$client."' AND idlang = '".$lang."' AND ".
 #		"idnewsrcp NOT IN (SELECT idnewsrcp FROM ".$cfg["tab"]["news_groupmembers"]." WHERE idnewsgroup = '".$_REQUEST["idrecipientgroup"]."')";
 // TODO: This works with every mySQL version but may be problematic, if a group
 // contains a lot of members (e.g. Oracle can't handle more than 1000 items in the brackets)
 $sSQL = "idclient = '" . $client . "' AND idlang = '" . $lang . "' AND " . "idnewsrcp NOT IN ('" . implode("','", $aInsiders) . "')";
 if ($_REQUEST["outsider_filter"] != "") {
     $sSQLSearchIn = "";
     if ($_REQUEST["outsider_searchin"] == "--all--" || $_REQUEST["outsider_searchin"] == "") {
         foreach ($aFields as $sKey => $aData) {
             if (strpos($aData["type"], "search") !== false) {
                 if ($sSQLSearchIn !== "") {
                     $sSQLSearchIn .= " OR ";
                 }
    }
}
if (!$bSortByFound) {
    $_REQUEST["sortby"] = "name";
    // Default sort by field, possible values see above
}
if (!$bSearchInFound) {
    $_REQUEST["searchin"] = "--all--";
}
// Free memory
unset($oUser);
unset($oClient);
##################################
# Get data
##################################
$oRecipients = new RecipientCollection();
// Updating keys, if activated; all recipients of all clients!
$sMsg = "";
if (getSystemProperty("newsletter", "updatekeys")) {
    $iUpdatedRecipients = $oRecipients->updateKeys();
    $sMsg = $notification->returnNotification("info", sprintf(i18n("%d recipients, with no or incompatible key has been updated. Deactivate update function.", $plugin_name), $iUpdatedRecipients));
}
$oRecipients->setWhere("recipientcollection.idclient", $client);
$oRecipients->setWhere("recipientcollection.idlang", $lang);
// sort by and sort order
$oRecipients->setOrder("recipientcollection." . $_REQUEST["sortby"] . " " . $_REQUEST["sortorder"]);
// Show group
if ($_REQUEST["restrictgroup"] != "--all--") {
    $oRecipients->link("RecipientGroupMemberCollection");
    $oRecipients->setWhere("RecipientGroupMemberCollection.idnewsgroup", $_REQUEST["restrictgroup"]);
}
 /**
  * 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;
     }
 }
 * @since      file available since contenido release <= 4.6
 * 
 * {@internal 
 *   created 2007-01-01, Björn Behrens (HerrB)
 *   modified 2008-06-27, Dominik Ziegler, add security fix
 *
 *   $Id$:
 * }}
 * 
 */
if (!defined('CON_FRAMEWORK')) {
    die('Illegal call');
}
$plugin_name = 'newsletter';
$oPage = new cPage();
$oRecipients = new RecipientCollection();
if (is_array($cfg['plugins']['recipients'])) {
    foreach ($cfg['plugins']['recipients'] as $plugin) {
        plugin_include("recipients", $plugin . "/" . $plugin . ".php");
    }
}
// Check form data
if ($_REQUEST["selDelimiter"] == "") {
    $_REQUEST["selDelimiter"] = "semikolon";
}
$aFields = array();
$aFieldDetails = array();
$aFields["name"] = strtolower(i18n("Name", $plugin_name));
$aFieldDetails["name"]["fieldtype"] = "field";
// field, plugin or group
$aFieldDetails["name"]["mandatory"] = false;