typo3/ext/direct_mail/mod/class.mod_web_dmail.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *  
00005 *  (c) 1999-2004 Kasper Skaarhoj (kasper@typo3.com)
00006 *  All rights reserved
00007 *
00008 *  This script is part of the TYPO3 project. The TYPO3 project is 
00009 *  free software; you can redistribute it and/or modify
00010 *  it under the terms of the GNU General Public License as published by
00011 *  the Free Software Foundation; either version 2 of the License, or
00012 *  (at your option) any later version.
00013 * 
00014 *  The GNU General Public License can be found at
00015 *  http://www.gnu.org/copyleft/gpl.html.
00016 *  A copy is found in the textfile GPL.txt and important notices to the license 
00017 *  from the author is found in LICENSE.txt distributed with these scripts.
00018 *
00019 * 
00020 *  This script is distributed in the hope that it will be useful,
00021 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023 *  GNU General Public License for more details.
00024 *
00025 *  This copyright notice MUST APPEAR in all copies of the script!
00026 ***************************************************************/
00078 class mod_web_dmail     {
00079         var $TSconfPrefix = "mod.web_modules.dmail.";
00080         var $fieldList="uid,name,title,email,phone,www,address,company,city,zip,country,fax,module_sys_dmail_category,module_sys_dmail_html,description";       // Taken from class.t3lib_dmailer.php (,firstname is automatically set), added description 050301
00081         //Where does descriptin come from?
00082                 // Internal
00083         var $modList="";
00084         var $params=array();
00085         var $perms_clause="";
00086         var $pageinfo="";
00087         var $sys_dmail_uid;
00088         var $CMD;
00089         var $pages_uid;
00090         var $categories;
00091         var $id;
00092         var $urlbase;
00093         var $back;
00094         var $noView;
00095         var $url_plain;
00096         var $url_html;
00097         var $mode;
00098         var $implodedParams=array();
00099         var $userTable;         // If set a valid user table is around
00100         
00101         function mod_web_dmail ($id,$pageinfo,$perms_clause,$CMD,$sys_dmail_uid,$pages_uid,$modTSconfig)        {
00102                 $this->id = $id;
00103                 $this->pageinfo = $pageinfo;
00104                 $this->perms_clause = $perms_clause;
00105                 $this->CMD=$CMD;
00106                 $this->sys_dmail_uid=$sys_dmail_uid;
00107                 $this->pages_uid=$pages_uid;
00108                 $this->modList = t3lib_BEfunc::getListOfBackendModules(array("dmail"),$this->perms_clause,$GLOBALS["BACK_PATH"]);
00109                 $this->params=$modTSconfig["properties"];
00110                 $this->implodedParams = t3lib_BEfunc::implodeTSParams($this->params);
00111                 if ($this->params["userTable"] && is_array($GLOBALS["TCA"][$this->params["userTable"]]))        {
00112                         $this->userTable = $this->params["userTable"];
00113                         t3lib_div::loadTCA($this->userTable);
00114                 }
00115 
00116                 t3lib_div::loadTCA("sys_dmail");
00117 
00118 
00119                         // Local lang for dmail module:
00120                 include (t3lib_extMgm::extPath("direct_mail")."mod/locallang.php");
00121                 $GLOBALS["LOCAL_LANG"] = t3lib_div::array_merge_recursive_overrule($GLOBALS["LOCAL_LANG"],$LOCAL_LANG_DMAIL);
00122                 
00123 
00124                 
00125 //              debug($this->implodedParams);
00126 //              $this->params=t3lib_BEfunc::processParams($this->pageinfo["abstract"]);
00127 //              debug($this->params);
00128         }
00129         function createDMail()  {
00130                 global $TCA;
00131                 if ($createMailFrom = t3lib_div::_GP("createMailFrom")) {
00132                                 // Set default values:
00133                         $dmail = array();
00134                         $dmail["sys_dmail"]["NEW"] = array (
00135                                 "from_email" => $this->params["from_email"],
00136                                 "from_name" => $this->params["from_name"],
00137                                 "replyto_email" => $this->params["replyto_email"],
00138                                 "replyto_name" => $this->params["replyto_name"],
00139                                 "return_path" => $this->params["return_path"],
00140                                 "long_link_rdct_url" => $this->params["long_link_rdct_url"],
00141                                 "long_link_mode" => $this->params["long_link_mode"],
00142                                 "organisation" => $this->params["organisation"]
00143                         );
00144                         $dmail["sys_dmail"]["NEW"]["sendOptions"] = $TCA["sys_dmail"]["columns"]["sendOptions"]["config"]["default"];
00145                                 // If params set, set default values:
00146                         if (isset($this->params["sendOptions"]))        $dmail["sys_dmail"]["NEW"]["sendOptions"] = $this->params["sendOptions"];
00147                         if (isset($this->params["HTMLParams"]))         $dmail["sys_dmail"]["NEW"]["HTMLParams"] = $this->params["HTMLParams"];
00148                         if (isset($this->params["plainParams"]))        $dmail["sys_dmail"]["NEW"]["plainParams"] = $this->params["plainParams"];
00149         
00150                                 // If createMailFrom is an integer, it's an internal page. If not, it's an external url 
00151                         if (t3lib_div::testInt($createMailFrom))        {
00152                                 $createFromMailRec = t3lib_BEfunc::getRecord ("pages",$createMailFrom);
00153                                 if (t3lib_div::inList($GLOBALS["TYPO3_CONF_VARS"]["FE"]["content_doktypes"],$createFromMailRec["doktype"]))     {
00154                                         $dmail["sys_dmail"]["NEW"]["subject"] = $createFromMailRec["title"];
00155                                         $dmail["sys_dmail"]["NEW"]["type"] = 0;
00156                                         $dmail["sys_dmail"]["NEW"]["page"] = $createFromMailRec["uid"];
00157                                         $dmail["sys_dmail"]["NEW"]["pid"]=$this->pageinfo["uid"];
00158                                 }
00159                         } else {
00160                                 $dmail["sys_dmail"]["NEW"]["subject"] = $createMailFrom;
00161                                 $dmail["sys_dmail"]["NEW"]["type"] = 1;
00162                                 $dmail["sys_dmail"]["NEW"]["sendOptions"] = 0;
00163         
00164                                 $dmail["sys_dmail"]["NEW"]["plainParams"] = t3lib_div::_GP("createMailFrom_plainUrl");
00165                                 $this->params["enablePlain"] = $dmail["sys_dmail"]["NEW"]["plainParams"] ? 1 : 0;
00166         
00167                                 $dmail["sys_dmail"]["NEW"]["HTMLParams"] = t3lib_div::_GP("createMailFrom_HTMLUrl");
00168                                 $this->params["enableHTML"] = $dmail["sys_dmail"]["NEW"]["HTMLParams"] ? 1 : 0;
00169         
00170                                 $dmail["sys_dmail"]["NEW"]["pid"]=$this->pageinfo["uid"];
00171                         }
00172         
00173                                 // Finally the enablePlain and enableHTML flags ultimately determines the sendOptions, IF they are set in the pageTSConfig
00174                         if (isset($this->params["enablePlain"]))        {if ($this->params["enablePlain"]) {$dmail["sys_dmail"]["NEW"]["sendOptions"]|=1;} else {$dmail["sys_dmail"]["NEW"]["sendOptions"]&=254;}}
00175                         if (isset($this->params["enableHTML"])) {if ($this->params["enableHTML"]) {$dmail["sys_dmail"]["NEW"]["sendOptions"]|=2;} else {$dmail["sys_dmail"]["NEW"]["sendOptions"]&=253;}}
00176 
00177                         if ($dmail["sys_dmail"]["NEW"]["pid"])  {
00178                                 $tce = t3lib_div::makeInstance("t3lib_TCEmain");
00179                                 $tce->stripslashes_values=0;
00180                                 $tce->start($dmail,Array());
00181                                 $tce->process_datamap();
00182         //                      debug($tce->substNEWwithIDs["NEW"]);
00183                                 $this->sys_dmail_uid = $tce->substNEWwithIDs["NEW"];
00184                         } else {
00185                                 // wrong page, could not...
00186                         }
00187                 }
00188         }
00189 
00190         // ********************
00191         // MAIN function
00192         // ********************
00193         function main() {
00194 //              debug($this->modList);
00195                 if (t3lib_div::inList($GLOBALS["TYPO3_CONF_VARS"]["FE"]["content_doktypes"],$this->pageinfo["doktype"]))        {               // Regular page, show menu to create a direct mail from this page.
00196                         if ($this->pageinfo["group_id"]>0 || $this->pageinfo["hidden"]) {
00197                                 $theOutput.= $GLOBALS["SOBE"]->doc->section($GLOBALS["LANG"]->getLL("dmail_newsletters"),'<span class="typo3-red">'.$GLOBALS["LANG"]->getLL("dmail_noCreateAccess").'</span>',0,1);
00198                         } else {
00199                                 $isNewsletterPage=0;
00200                                 if (is_array($this->modList["rows"]))   {
00201                                         reset($this->modList["rows"]);
00202                                         while(list(,$rData)=each($this->modList["rows"]))       {
00203                                                 if ($rData["uid"]==$this->pageinfo["pid"])      {
00204                                                         $isNewsletterPage=1;
00205                                                 }
00206                                         }
00207                                 }
00208                                 if ($isNewsletterPage)  {
00209                                         header('Location: index.php?id='.$this->pageinfo["pid"].'&CMD=displayPageInfo&pages_uid='.$this->pageinfo["uid"].'&SET[dmail_mode]=news');
00210                                         exit;
00211                                 }
00212                         }
00213                 } elseif ($this->pageinfo["doktype"]==254 && $this->pageinfo["module"]=="dmail")        {       // Direct mail module
00214                         $theOutput.= $this->mailModule_main();
00215                 } elseif ($this->id!=0) {
00216                         $theOutput.= $GLOBALS["SOBE"]->doc->section($GLOBALS["LANG"]->getLL("dmail_newsletters"),'<span class="typo3-red">'.$GLOBALS["LANG"]->getLL("dmail_noRegular").'</span>',0,1);
00217                 }
00218 
00219                 if ($this->id!=0) {
00220                         $theOutput.=$GLOBALS["SOBE"]->doc->spacer(10);
00221                 }
00222                 return $theOutput;
00223         }
00224         function mailModule_main()      {
00225                         // Top menu
00226                 $menuHTML = t3lib_BEfunc::getFuncMenu($GLOBALS["SOBE"]->id,"SET[dmail_mode]",$GLOBALS["SOBE"]->MOD_SETTINGS["dmail_mode"],$GLOBALS["SOBE"]->MOD_MENU["dmail_mode"]);
00227 
00228                 $theOutput.=$GLOBALS["SOBE"]->doc->divider(5);
00229                 $theOutput.=$GLOBALS["SOBE"]->doc->section("",$menuHTML);
00230 //              $theOutput.= $GLOBALS["SOBE"]->doc->spacer(20);
00231                 $mode = $GLOBALS["SOBE"]->MOD_SETTINGS["dmail_mode"];
00232 
00233                 if (!$this->sys_dmail_uid || $mode!="direct")   {
00234                         $this->makeCategories();
00235 
00236                                 // COMMAND:
00237                         switch($this->CMD) {
00238                                 case "displayPageInfo":
00239                                         $theOutput.= $this->cmd_displayPageInfo();
00240                                 break;
00241                                 case "displayUserInfo":
00242                                         $theOutput.= $this->cmd_displayUserInfo();
00243                                 break;                          
00244                                 case "displayMailGroup":
00245                                         $result = $this->cmd_compileMailGroup(intval(t3lib_div::_GP("group_uid")));
00246                                         $theOutput.= $this->cmd_displayMailGroup($result);
00247                                 break;
00248                                 case "displayImport":
00249                                         $theOutput.= $this->cmd_displayImport();
00250                                 break;
00251                                 default:
00252                                         $theOutput.= $this->cmd_default($mode);
00253                                 break;
00254                         }
00255                 } else {
00256                                 // Here the single dmail record is shown.
00257                         $this->urlbase = substr(t3lib_div::getIndpEnv("TYPO3_REQUEST_DIR"),0,-(strlen(TYPO3_MOD_PATH)+strlen(TYPO3_mainDir))).'index.php';
00258                         $this->sys_dmail_uid = intval($this->sys_dmail_uid);
00259                         
00260                         $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', 'sys_dmail', 'pid='.intval($this->id).' AND uid='.intval($this->sys_dmail_uid).t3lib_BEfunc::deleteClause('sys_dmail'));
00261 
00262                         $this->noView = 0;
00263                         $this->back = '<input type="Submit" value="< BACK" onClick="jumpToUrlD(\'index.php?id='.$this->id.'&sys_dmail_uid='.$this->sys_dmail_uid.'\'); return false;">';
00264                         if($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))  {
00265                                         // Finding the url to fetch content from
00266                                 switch((string)$row["type"])    {
00267                                         case 1:
00268                                                 $this->url_html = $row["HTMLParams"];
00269                                                 $this->url_plain = $row["plainParams"];
00270                                         break;
00271                                         default:
00272                                                 $this->url_html = $this->urlbase."?id=".$row["page"].$row["HTMLParams"];
00273                                                 $this->url_plain = $this->urlbase."?id=".$row["page"].$row["plainParams"];
00274                                         break;
00275                                 }
00276                                 if (!($row["sendOptions"]&1) || !$this->url_plain)      {       // plain
00277                                         $this->url_plain="";
00278                                 } else {
00279                                         $urlParts = parse_url($this->url_plain);
00280                                         if (!$urlParts["scheme"])       {
00281                                                 $this->url_plain="http://".$this->url_plain;
00282                                         }
00283                                 }
00284                                 if (!($row["sendOptions"]&2) || !$this->url_html)       {       // html
00285                                         $this->url_html="";
00286                                 } else {
00287                                         $urlParts = parse_url($this->url_html);
00288                                         if (!$urlParts["scheme"])       {
00289                                                 $this->url_html="http://".$this->url_html;
00290                                         }
00291                                 }                               
00292                                 
00293                                         // COMMAND:
00294                                 switch($this->CMD) {
00295                                         case "fetch":
00296                                                 $theOutput.=$this->cmd_fetch($row);
00297                                                 $row = t3lib_befunc::getRecord("sys_dmail",$row["uid"]);
00298                                         break;
00299                                         case "prefetch":
00300                                                 $theOutput.=$this->cmd_prefetch($row);
00301                                         break;
00302                                         case "testmail":
00303                                                 $theOutput.=$this->cmd_testmail($row);
00304                                         break;
00305                                         case "finalmail":
00306                                                 $theOutput.=$this->cmd_finalmail($row);
00307                                         break;
00308                                         case "send_mail_test":
00309                                         case "send_mail_final":
00310                                                 $theOutput.=$this->cmd_send_mail($row);
00311                                         break;
00312                                         case "stats":
00313                                                 $theOutput.=$this->cmd_stats($row);
00314                                         break;
00315                                 }
00316                                 if (!$this->noView)     {
00317                                         $theOutput.=$this->directMail_defaultView($row);
00318                                 }
00319                         }
00320                 }
00321                 return $theOutput;
00322         }
00323         function makeCategories()       {
00324                 $this->categories = array();
00325                 if (is_array($this->params["categories."]))     {
00326                         reset($this->params["categories."]);
00327                         while(list($pKey,$pVal)=each($this->params["categories."]))     {
00328                                 if (trim($pVal))        {
00329                                         $catNum = intval($pKey);
00330                                         if ($catNum>=0 && $catNum<=30)  {
00331                                                 $this->categories[$catNum] = $pVal;
00332                                         }
00333                                 }
00334                         }
00335                 }
00336         }
00337 
00338         // ********************
00339         // CMD functions
00340         // ********************
00341         function cmd_displayPageInfo()  {
00342                 global $TCA, $HTTP_POST_VARS, $LANG;            
00343 
00344                         // Here the dmail list is rendered:
00345                 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
00346                                                 'uid,pid,subject,tstamp,issent,renderedsize,attachment,type', 
00347                                                 'sys_dmail', 
00348                                                 'pid='.intval($this->id).' AND type=0 AND page='.intval($this->pages_uid).t3lib_BEfunc::deleteClause('sys_dmail'),
00349                                                 '',
00350                                                 $GLOBALS['TYPO3_DB']->stripOrderBy($TCA['sys_dmail']['ctrl']['default_sortby'])
00351                                         );
00352                 if ($GLOBALS['TYPO3_DB']->sql_num_rows($res))   {
00353                         $onClick = ' onClick="return confirm('.$GLOBALS['LANG']->JScharCode(sprintf($LANG->getLL("nl_l_warning"),$GLOBALS['TYPO3_DB']->sql_num_rows($res))).');"';
00354                 } else {
00355                         $onClick = "";
00356                 }
00357 
00358                 
00359                 
00360                 $out="";
00361                 $out.='<a href="#" onClick="'.t3lib_BEfunc::viewOnClick($this->pages_uid,$GLOBALS["BACK_PATH"]).'"><img src="'.$GLOBALS["BACK_PATH"].'gfx/zoom.gif" width="12" height="12" hspace=3 vspace=2 border="0" align=top>'.$LANG->getLL("nl_viewPage").'</a><BR>';
00362                 $out.='<a href="#" onClick="'.t3lib_BEfunc::editOnClick('&edit[pages]['.$this->pages_uid.']=edit&edit_content=1',$GLOBALS["BACK_PATH"],"").'"><img src="'.$GLOBALS["BACK_PATH"].'gfx/edit2.gif" width="11" height="12" hspace=3 vspace=2 border="0" align=top>'.$LANG->getLL("nl_editPage").'</a><BR>';
00363                 $out.='<a href="index.php?id='.$this->id.'&createMailFrom='.$this->pages_uid.'&SET[dmail_mode]=direct"'.$onClick.'><img src="'.$GLOBALS["BACK_PATH"].'/gfx/newmail.gif" width="18" height="16" border="0" align=top>'.$LANG->getLL("nl_createDmailFromPage").'</a><BR>';                                
00364         
00365                 if ($GLOBALS['TYPO3_DB']->sql_num_rows($res))   {
00366                         $out.="<BR><b>".$LANG->getLL("nl_alreadyBasedOn").":</b><BR><BR>";
00367                         $out.="<table border=0 cellpadding=0 cellspacing=0>";
00368                                 $out.='<tr>
00369                                         <td class="bgColor5">'.fw("&nbsp;").'</td>
00370                                         <td class="bgColor5"><b>'.fw($LANG->getLL("nl_l_subject")."&nbsp;&nbsp;").'</b></td>
00371                                         <td class="bgColor5"><b>'.fw($LANG->getLL("nl_l_lastM")."&nbsp;&nbsp;").'</b></td>
00372                                         <td class="bgColor5"><b>'.fw($LANG->getLL("nl_l_sent")."&nbsp;&nbsp;").'</b></td>
00373                                         <td class="bgColor5"><b>'.fw($LANG->getLL("nl_l_size")."&nbsp;&nbsp;").'</b></td>
00374                                         <td class="bgColor5"><b>'.fw($LANG->getLL("nl_l_attach")."&nbsp;&nbsp;").'</b></td>
00375                                         <td class="bgColor5"><b>'.fw($LANG->getLL("nl_l_type")."&nbsp;&nbsp;").'</b></td>
00376                                 </tr>';
00377                         while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))       {
00378                                 $out.='<tr>
00379                                         <td><img src="'.$GLOBALS["BACK_PATH"].'gfx/i/mail.gif" width=18 height=16 border=0 align="top"></td>
00380                                         <td>'.$this->linkDMail_record(fw(t3lib_div::fixed_lgd($row["subject"],30)."&nbsp;&nbsp;"),$row["uid"]).'</td>
00381                                         <td>'.fw(t3lib_BEfunc::date($row["tstamp"])."&nbsp;&nbsp;").'</td>
00382                                         <td>'.($row["issent"] ? fw("YES") : "").'</td>
00383                                         <td>'.($row["renderedsize"] ? fw(t3lib_div::formatSize($row["renderedsize"])."&nbsp;&nbsp;") : "").'</td>
00384                                         <td>'.($row["attachment"] ? '<img src="attach.gif" width=9 height=13>' : "").'</td>
00385                                         <td>'.fw($row["type"] ? $LANG->getLL("nl_l_tUrl") : $LANG->getLL("nl_l_tPage")).'</td>
00386                                 </tr>';
00387                                 
00388                         }
00389                         $out.='</table>';
00390                 }
00391         
00392                 $theOutput.= $GLOBALS["SOBE"]->doc->section($LANG->getLL("nl_info"),$out,0,1);
00393                 $theOutput.= $GLOBALS["SOBE"]->doc->divider(20);
00394 
00395 
00396 
00397 
00398                 if (is_array($HTTP_POST_VARS["indata"]["categories"]))  {
00399                         $data=array();
00400                         reset($HTTP_POST_VARS["indata"]["categories"]);
00401                         while(list($recUid,$recValues)=each($HTTP_POST_VARS["indata"]["categories"]))   {
00402 //                                              debug($recValues);
00403                                 reset($recValues);
00404                                 $data["tt_content"][$recUid]["module_sys_dmail_category"]=0;
00405                                 while(list($k,$b)=each($recValues))     {
00406                                         if ($b) {$data["tt_content"][$recUid]["module_sys_dmail_category"]|= pow (2,$k);}
00407                                 }
00408                         }
00409                         $tce = t3lib_div::makeInstance("t3lib_TCEmain");
00410                         $tce->stripslashes_values=0;
00411                         $tce->start($data,Array());
00412                         $tce->process_datamap();
00413 //                                              debug($data);
00414                 }
00415 
00416                 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
00417                                         'colPos, CType, uid, header, bodytext, module_sys_dmail_category', 
00418                                         'tt_content', 
00419                                         'pid='.intval($this->pages_uid).t3lib_BEfunc::deleteClause('tt_content').' AND NOT hidden',
00420                                         '',
00421                                         'colPos,sorting'
00422                                 );
00423                 if (!$GLOBALS['TYPO3_DB']->sql_num_rows($res))  {
00424                         $theOutput.= $GLOBALS["SOBE"]->doc->section($LANG->getLL("nl_cat"),$LANG->getLL("nl_cat_msg1"));
00425                 } else {
00426                         $out="";
00427                         $colPosVal=99;
00428                         while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))       {
00429                                 $out.='<tr><td colspan=3><img src="clear.gif" width=1 height=15></td></tr>';
00430                                 if ($colPosVal!=$row["colPos"]) {
00431                                         $out.='<tr><td colspan=3 class="bgColor5">'.fw($LANG->getLL("nl_l_column").": <strong>".htmlspecialchars(t3lib_BEfunc::getProcessedValue("tt_content","colPos",$row["colPos"]))."</strong>").'</td></tr>';
00432                                         $colPosVal=$row["colPos"];
00433                                 }
00434                                 $out.='<tr>';
00435                                 $out.='<td valign=top width=75%>'.fw('<img src="'.$GLOBALS["BACK_PATH"].t3lib_iconWorks::getIcon("tt_content",$row).'" width=18 height=16 border=0 title="'.htmlspecialchars(t3lib_BEfunc::getProcessedValue("tt_content","CType",$row["CType"])).'" align=top> '.
00436                                         $row["header"].'<BR>'.t3lib_div::fixed_lgd(strip_tags($row["bodytext"]),200).'<BR>').'</td>';
00437 
00438                                 $out.='<td>&nbsp;&nbsp;</td><td nowrap valign=top>';
00439                                 $out_check="";
00440                                 if ($row["module_sys_dmail_category"]) {
00441                                         $out_check.='<font color=red><strong>'.$LANG->getLL("nl_l_ONLY").'</strong></font>';
00442                                 } else {
00443                                         $out_check.='<font color=green><strong>'.$LANG->getLL("nl_l_ALL").'</strong></font>';
00444                                 }
00445                                 $out_check.="<BR>";
00446                                 reset($this->categories);
00447                                 while(list($pKey,$pVal)=each($this->categories))        {
00448                                         $out_check.='<input type="hidden" name="indata[categories]['.$row["uid"].']['.$pKey.']" value="0"><input type="checkbox" name="indata[categories]['.$row["uid"].']['.$pKey.']" value="1"'.(($row["module_sys_dmail_category"]&pow (2,$pKey)) ?" checked":"").'> '.$pVal.'<BR>';
00449                                 }
00450                                 $out.=fw($out_check).'</td></tr>';
00451                         }
00452                         $out='<table border=0 cellpadding=0 cellspacing=0>'.$out.'</table>';
00453                         $out.='<input type="hidden" name="pages_uid" value="'.$this->pages_uid.'"><input type="hidden" name="CMD" value="'.$this->CMD.'"><BR><input type="submit" value="'.$LANG->getLL("nl_l_update").'">';
00454                         $theOutput.= $GLOBALS["SOBE"]->doc->section($LANG->getLL("nl_cat"),$LANG->getLL("nl_cat_msg2")."<BR><BR>".$out);
00455                 }
00456                 return $theOutput;
00457         }
00458         function getCsvValues($str,$sep=",")    {
00459                 $fh=tmpfile();
00460                 fwrite ($fh, trim($str));
00461                 fseek ($fh,0);
00462                 $lines=array();
00463                 while ($data = fgetcsv ($fh, 1000, $sep)) {
00464                         $lines[]=$data;
00465                 }
00466                 return $lines;
00467         }
00468         function rearrangeCsvValues($lines)     {
00469                 $out=array();
00470                 if (is_array($lines) && count($lines)>0)        {
00471                                 // Analyse if first line is fieldnames. 
00472                                 // Required is it that every value is either 1) found in the list, fieldsList in this class (see top) 2) the value is empty (value omitted then) or 3) the field starts with "user_".
00473                                 // In addition fields may be prepended with "[code]". This is used if the incoming value is true in which case '+[value]' adds that number to the field value (accummulation) and '=[value]' overrides any existing value in the field
00474                         $first = $lines[0];
00475                         $fieldListArr = explode(",",$this->fieldList);
00476 //                      debug($fieldListArr);
00477                         reset($first);
00478                         $fieldName=1;
00479                         $fieldOrder=array();
00480                         while(list(,$v)=each($first))   {
00481                                 list($fName,$fConf) = split("\[|\]",$v);
00482                                 $fName =trim($fName);
00483                                 $fConf =trim($fConf);
00484                                 
00485                                 $fieldOrder[]=array($fName,$fConf);
00486                                 if ($fName && substr($fName,0,5)!="user_" && !in_array($fName,$fieldListArr))   {$fieldName=0; break;}
00487                         }
00488                                 // If not field list, then:
00489                         if (!$fieldName)        {
00490                                 $fieldOrder = array(array("name"),array("email"));
00491                         }
00492 //                      debug($fieldOrder);
00493 //                      debug($fieldName);
00494 //debug($lines);
00495                                 // Re map values
00496                         reset($lines);
00497                         if ($fieldName) {
00498                                 next($lines);   // Advance pointer if the first line was field names
00499                         }
00500                         $c=0;
00501                         while(list(,$data)=each($lines))        {
00502                                 if (count($data)>1 || $data[0]) {       // Must be a line with content. This sorts out entries with one key which is empty. Those are empty lines.
00503 //                                      debug($data);
00504                                                 // Traverse fieldOrder and map values over
00505                                         reset($fieldOrder);
00506                                         while(list($kk,$fN)=each($fieldOrder))  {
00507                                                 if ($fN[0])     {
00508                                                         if ($fN[1])     {
00509                                                                 if (trim($data[$kk]))   {       // If is true
00510                                                                         if (substr($fN[1],0,1)=="=")    {
00511                                                                                 $out[$c][$fN[0]]=trim(substr($fN[1],1));
00512                                                                         } elseif (substr($fN[1],0,1)=="+")      {
00513                                                                                 $out[$c][$fN[0]]+=substr($fN[1],1);
00514                                                                         }
00515                                                                 }
00516                                                         } else {
00517                                                                 $out[$c][$fN[0]]=$data[$kk];
00518                                                         }
00519                                                 }
00520                                         }
00521                                         $c++;
00522                                 }
00523                         }
00524                 }
00525                 return $out;
00526         }
00527         function rearrangePlainMails($plainMails)       {
00528                 $out=array();
00529                 if (is_array($plainMails))      {
00530                         reset($plainMails);
00531                         $c=0;
00532                         while(list(,$v)=each($plainMails))      {
00533                                 $out[$c]["email"]=$v;
00534                                 $out[$c]["name"]="";
00535                                 $c++;
00536                         }
00537                 }
00538                 return $out;
00539         }
00540         function makePidListQuery($table,$pidList,$fields,$cat) {
00541                 $cat = intval($cat);
00542                 if ($cat>0)     {
00543                         $addQ = ' AND module_sys_dmail_category&'.$cat.' > 0';
00544                 } else {
00545                         $addQ = '';
00546                 }
00547 
00548                 $query = $GLOBALS['TYPO3_DB']->SELECTquery(
00549                                                 $fields,
00550                                                 $table, 
00551                                                 'pid IN ('.$pidList.')'.
00552                                                         $addQ.
00553                                                         t3lib_BEfunc::deleteClause($table).
00554                                                         t3lib_pageSelect::enableFields($table)
00555                                         );
00556 
00557                 return $query;
00558         }
00559         function getIdList($table,$pidList,$cat)        {
00560                 $query = $this->makePidListQuery($table,$pidList,"uid",$cat);
00561                 $res = $GLOBALS['TYPO3_DB']->sql(TYPO3_db,$query);
00562                 $outArr = array();
00563                 while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))       {
00564                         $outArr[] = $row["uid"];
00565                 }
00566                 return $outArr;
00567         }
00568         function makeStaticListQuery($table,$uid,$fields)       {
00569                 $query = $GLOBALS['TYPO3_DB']->SELECTquery(
00570                                                 $fields,
00571                                                 $table.',sys_dmail_group,sys_dmail_group_mm', 
00572                                                 'sys_dmail_group_mm.uid_local=sys_dmail_group.uid AND
00573                                                 sys_dmail_group.uid = '.$uid.' AND
00574                                                                 sys_dmail_group_mm.uid_foreign='.$table.'.uid AND sys_dmail_group_mm.tablenames="'.$table.'"'.
00575                                                                 t3lib_pageSelect::enableFields($table). // Enable fields includes 'deleted'
00576                                                                 t3lib_pageSelect::enableFields("sys_dmail_group")
00577                                         );
00578                 return $query;
00579         }
00580         function getStaticIdList($table,$uid)   {
00581                 $query = $this->makeStaticListQuery($table,$uid,$table.".uid");
00582                 $res = $GLOBALS['TYPO3_DB']->sql(TYPO3_db,$query);
00583                 $outArr=array();
00584                 while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))       {
00585                         $outArr[]=$row["uid"];
00586                 }
00587                 return $outArr;
00588         }
00589         function getMailGroups($list,$parsedGroups)     {
00590                 $groupIdList = t3lib_div::intExplode(",",$list);
00591                 $groups = array();
00592 
00593                 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('sys_dmail_group.*', 'sys_dmail_group,pages', '
00594                                         sys_dmail_group.uid IN ('.implode(',',$groupIdList).') 
00595                                         AND pages.uid=sys_dmail_group.pid 
00596                                         AND '.$this->perms_clause.
00597                                         t3lib_BEfunc::deleteClause('pages').
00598                 //                      t3lib_BEfunc::deleteClause('sys_dmail_group').  // Enable fields includes 'deleted'
00599                 //                      t3lib_pageSelect::enableFields('pages').                // Records should be selected from hidden pages...
00600                                         t3lib_pageSelect::enableFields('sys_dmail_group'));
00601                         
00602                 while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))       {
00603                         if ($row["type"]==4)    {       // Other mail group...
00604                                 if (!in_array($row["uid"],$parsedGroups))       {
00605                                         $parsedGroups[]=$row["uid"];
00606                                         $groups=array_merge($groups,$this->getMailGroups($row["mail_groups"],$parsedGroups));
00607                                 }
00608                         } else {
00609                                 $groups[]=$row["uid"];  // Normal mail group, just add to list
00610                         }
00611                 }
00612                 return $groups;
00613         }
00614         function cmd_displayMailGroup($result)  {
00615                 $count=0;
00616                 $idLists = $result["queryInfo"]["id_lists"];
00617                 if (is_array($idLists["tt_address"]))   $count+=count($idLists["tt_address"]);
00618                 if (is_array($idLists["fe_users"]))     $count+=count($idLists["fe_users"]);
00619                 if (is_array($idLists["PLAINLIST"]))    $count+=count($idLists["PLAINLIST"]);
00620                 if (is_array($idLists[$this->userTable]))       $count+=count($idLists[$this->userTable]);
00621                 
00622                 $group = t3lib_befunc::getRecord("sys_dmail_group",t3lib_div::_GP("group_uid"));
00623                 $out=t3lib_iconWorks::getIconImage("sys_dmail_group",$group,$GLOBALS["BACK_PATH"],'align="top"').$group["title"]."<BR>";
00624                 
00625                 $lCmd=t3lib_div::_GP("lCmd");
00626 
00627                 $mainC = $out."Total number of recipients: <strong>".$count."</strong>";
00628                 if (!$lCmd)     {
00629                         $mainC.= '<BR>';
00630                         $mainC.= '<BR>';
00631                         $mainC.= '<a href="'.t3lib_div::linkThisScript(array("lCmd"=>"listall")).'">List all recipients</a>';
00632                 }
00633                 
00634                 $theOutput.= $GLOBALS["SOBE"]->doc->section("Recipients from group:",$mainC);
00635                 $theOutput.= $GLOBALS["SOBE"]->doc->spacer(20);
00636 
00637                 switch($lCmd)   {
00638                         case "listall":
00639                                 $theOutput.= $GLOBALS["SOBE"]->doc->section("ADDRESS TABLE",$this->getRecordList($this->fetchRecordsListValues($idLists["tt_address"],"tt_address"),"tt_address"));
00640                                 $theOutput.= $GLOBALS["SOBE"]->doc->spacer(20);
00641                                 $theOutput.= $GLOBALS["SOBE"]->doc->section("WEBSITE USERS TABLE",$this->getRecordList($this->fetchRecordsListValues($idLists["fe_users"],"fe_users"),"fe_users"));
00642                                 $theOutput.= $GLOBALS["SOBE"]->doc->spacer(20);
00643                                 $theOutput.= $GLOBALS["SOBE"]->doc->section("PLAIN LIST",$this->getRecordList($idLists["PLAINLIST"],"default",1));
00644                                 $theOutput.= $GLOBALS["SOBE"]->doc->spacer(20);
00645                                 $theOutput.= $GLOBALS["SOBE"]->doc->section($this->userTable." TABLE",$this->getRecordList($this->fetchRecordsListValues($idLists[$this->userTable],$this->userTable),$this->userTable));
00646                         break;
00647                         default:
00648                                 if (t3lib_div::_GP("csv"))      {
00649                                         $csvValue=t3lib_div::_GP("csv");
00650                                         if ($csvValue=="PLAINLIST")     {
00651                                                 $this->downloadCSV($idLists["PLAINLIST"]);
00652                                         } elseif (t3lib_div::inList("tt_address,fe_users,".$this->userTable, $csvValue)) {
00653                                                 $this->downloadCSV($this->fetchRecordsListValues($idLists[$csvValue],$csvValue,$this->fieldList.",tstamp"));
00654                                         }
00655                                 } else {
00656                                         $theOutput.= $GLOBALS["SOBE"]->doc->section("ADDRESS TABLE","Recipients: ".(is_array($idLists["tt_address"])?count($idLists["tt_address"]):0).'<BR><a href="'.t3lib_div::linkThisScript(array("csv"=>"tt_address")).'">Download CSV file</a>');
00657                                         $theOutput.= $GLOBALS["SOBE"]->doc->spacer(20);
00658                                         $theOutput.= $GLOBALS["SOBE"]->doc->section("WEBSITE USERS TABLE","Recipients: ".(is_array($idLists["fe_users"])?count($idLists["fe_users"]):0).'<BR><a href="'.t3lib_div::linkThisScript(array("csv"=>"fe_users")).'">Download CSV file</a>');
00659                                         $theOutput.= $GLOBALS["SOBE"]->doc->spacer(20);
00660                                         $theOutput.= $GLOBALS["SOBE"]->doc->section("PLAIN LIST","Recipients: ".(is_array($idLists["PLAINLIST"])?count($idLists["PLAINLIST"]):0).'<BR><a href="'.t3lib_div::linkThisScript(array("csv"=>"PLAINLIST")).'">Download CSV file</a>');
00661                                         $theOutput.= $GLOBALS["SOBE"]->doc->spacer(20);
00662                                         $theOutput.= $GLOBALS["SOBE"]->doc->section($this->userTable." TABLE","Recipients: ".(is_array($idLists[$this->userTable])?count($idLists[$this->userTable]):0).'<BR><a href="'.t3lib_div::linkThisScript(array("csv"=>$this->userTable)).'">Download CSV file</a>');
00663                                 }
00664                         break;
00665                 }               
00666                 return $theOutput;
00667         }
00668         function downloadCSV($idArr)    {
00669                 $lines=array();
00670 #debug($idArr);
00671                 if (is_array($idArr) && count($idArr))  {
00672                         reset($idArr);
00673                         $lines[]=t3lib_div::csvValues(array_keys(current($idArr)),",","");
00674                         
00675                         reset($idArr);
00676                         while(list($i,$rec)=each($idArr))       {
00677         //                      debug(t3lib_div::csvValues($rec),1);
00678                                 $lines[]=t3lib_div::csvValues($rec);
00679                         }
00680                 }
00681 
00682                         $filename="DirectMail_export_".date("dmy-Hi").".csv";
00683                         $mimeType = "application/octet-stream";
00684                         Header("Content-Type: ".$mimeType);
00685                         Header("Content-Disposition: attachment; filename=".$filename);
00686                         echo implode(chr(13).chr(10),$lines);
00687                         exit;
00688         }
00689         function cmd_displayMailGroup_test($result)     {
00690                 $count=0;
00691                 $idLists = $result["queryInfo"]["id_lists"];
00692                 $out="";
00693                 if (is_array($idLists["tt_address"]))   {$out.=$this->getRecordList($this->fetchRecordsListValues($idLists["tt_address"],"tt_address"),"tt_address");}
00694                 if (is_array($idLists["fe_users"]))     {$out.=$this->getRecordList($this->fetchRecordsListValues($idLists["fe_users"],"fe_users"),"fe_users");}
00695                 if (is_array($idLists["PLAINLIST"]))    {$out.=$this->getRecordList($idLists["PLAINLIST"],"default",1);}
00696                 if (is_array($idLists[$this->userTable]))       {$out.=$this->getRecordList($this->fetchRecordsListValues($idLists[$this->userTable],$this->userTable),$this->userTable);}
00697 
00698                 return $out;
00699         }
00700         function cmd_compileMailGroup($group_uid,$makeIdLists=1)        {
00701                 // $makeIdLists: Set to 0 if you don't want the list of table ids to be collected but only the queries to be stored.
00702                 $queries=array();
00703                 $id_lists=array();
00704                 if ($group_uid) {
00705                         $mailGroup=t3lib_BEfunc::getRecord("sys_dmail_group",$group_uid);
00706                         if (is_array($mailGroup) && $mailGroup["pid"]==$this->id)       {
00707                                 $head = '<img src="'.$GLOBALS["BACK_PATH"].t3lib_iconWorks::getIcon("sys_dmail_group").'" width=18 height=16 border=0 align="top">'.t3lib_div::fixed_lgd($mailGroup["title"],30)."<BR>";
00708                                 $theOutput.=$head;
00709                                 switch($mailGroup["type"])      {
00710                                         case 0: // From pages
00711                                                 $thePages = $mailGroup["pages"] ? $mailGroup["pages"] : $this->id;              // use current page if no else
00712                                                 $pages = t3lib_div::intExplode(",",$thePages);  // Explode the pages
00713                                                 reset($pages);
00714                                                 $pageIdArray=array();
00715                                                 while(list(,$pageUid)=each($pages))     {
00716                                                         if ($pageUid>0) {
00717                                                                 $pageinfo = t3lib_BEfunc::readPageAccess($pageUid,$this->perms_clause);
00718                                                                 if (is_array($pageinfo))        {
00719                                                                         $info["fromPages"][]=$pageinfo;
00720                                                                         $pageIdArray[]=$pageUid;
00721                                                                         if ($mailGroup["recursive"])    {
00722                                                                                 $pageIdArray=array_merge($pageIdArray,$GLOBALS["SOBE"]->getRecursiveSelect($pageUid,$this->perms_clause));
00723                                                                         }
00724                                                                 }
00725                                                         }
00726                                                                 
00727                                                 }
00728                                                         // Remove any duplicates
00729                                                 $pageIdArray=array_unique($pageIdArray);
00730                                                 $pidList = implode(",",$pageIdArray);
00731                                                 $info["recursive"]=$mailGroup["recursive"];
00732 //                                              debug($pageIdArray);
00733 //                                              debug($info);
00734                                                         // Make queries
00735                                                 if ($pidList)   {
00736                                                         $whichTables = intval($mailGroup["whichtables"]);
00737                                                         if ($whichTables&1)     {       // tt_address
00738                                                                 $queries["tt_address"]=$this->makePidListQuery("tt_address",$pidList,"*",$mailGroup["select_categories"]);
00739                                                                 if ($makeIdLists)       $id_lists["tt_address"]=$this->getIdList("tt_address",$pidList,$mailGroup["select_categories"]);
00740                                                         }
00741                                                         if ($whichTables&2)     {       // tt_address
00742                                                                 $queries["fe_users"]=$this->makePidListQuery("fe_users",$pidList,"*",$mailGroup["select_categories"]);
00743                                                                 if ($makeIdLists)       $id_lists["fe_users"]=$this->getIdList("fe_users",$pidList,$mailGroup["select_categories"]);
00744                                                         }
00745                                                         if ($this->userTable && ($whichTables&4))       {       // tt_address
00746                                                                 $queries[$this->userTable]=$this->makePidListQuery($this->userTable,$pidList,"*",$mailGroup["select_categories"]);
00747                                                                 if ($makeIdLists)       $id_lists[$this->userTable]=$this->getIdList($this->userTable,$pidList,$mailGroup["select_categories"]);
00748                                                         }
00749                                                 }
00750                 //                              debug($queries);
00751                         //                      debug($id_lists);
00752                                         break;
00753                                         case 1: // List of mails
00754                                                 if ($mailGroup["csv"]==1)       {
00755                                                         $recipients = $this->rearrangeCsvValues($this->getCsvValues($mailGroup["list"]));
00756 //                                                      debug($recipients);
00757                                                 } else {
00758                                                         $recipients = $this->rearrangePlainMails(array_unique(split("[[:space:],;]+",$mailGroup["list"])));
00759 //                                                      debug($recipients);
00760                                                 }
00761                                                 $id_lists["PLAINLIST"] = $this->cleanPlainList($recipients);
00762 //                                              debug($id_lists);
00763                                         break;
00764                                         case 2: // Static MM list
00765                                                 $queries["tt_address"] = $this->makeStaticListQuery("tt_address",$group_uid,"tt_address.*");
00766                                                 if ($makeIdLists)       $id_lists["tt_address"] = $this->getStaticIdList("tt_address",$group_uid);
00767                                                 $queries["fe_users"] = $this->makeStaticListQuery("fe_users",$group_uid,"fe_users.*");
00768                                                 if ($makeIdLists)       $id_lists["fe_users"] = $this->getStaticIdList("fe_users",$group_uid);
00769                                                 if ($this->userTable)   {
00770                                                         $queries[$this->userTable] = $this->makeStaticListQuery($this->userTable,$group_uid,$this->userTable."*");
00771                                                         if ($makeIdLists)       $id_lists[$this->userTable] = $this->getStaticIdList($this->userTable,$group_uid);
00772                                                 }
00773 //                                              debug($queries);
00774 //                                              debug($id_lists);
00775                                         break;
00776                                         case 3: // QUERY
00777                                                 //$theOutput.=$this->cmd_query($group_uid);
00778                                                 $theOutput.=$GLOBALS["SOBE"]->doc->section("Special Query","UNDER CONSTRUCTION...");
00779                                         break;
00780                                         case 4: // 
00781                                                 $groups = array_unique($this->getMailGroups($mailGroup["mail_groups"],array($mailGroup["uid"])));
00782                                                 reset($groups);
00783                                                 $queries=array();
00784                                                 $id_lists=array();
00785                                                 while(list(,$v)=each($groups))  {
00786                                                         $collect=$this->cmd_compileMailGroup($v);
00787                                                         if (is_array($collect["queryInfo"]["queries"])) {
00788                                                                 $queries=t3lib_div::array_merge_recursive_overrule($queries,$collect["queryInfo"]["queries"]);
00789                                                         }
00790                                                         if (is_array($collect["queryInfo"]["id_lists"]))        {
00791                                                                 $id_lists=t3lib_div::array_merge_recursive_overrule($id_lists,$collect["queryInfo"]["id_lists"]);
00792                                                         }
00793                                                 }
00794                                                         // Make unique entries
00795                                                 if (is_array($id_lists["tt_address"]))  $id_lists["tt_address"] = array_unique($id_lists["tt_address"]);
00796                                                 if (is_array($id_lists["fe_users"]))    $id_lists["fe_users"] = array_unique($id_lists["fe_users"]);
00797                                                 if (is_array($id_lists[$this->userTable]) && $this->userTable)  $id_lists[$this->userTable] = array_unique($id_lists[$this->userTable]);
00798                                                 if (is_array($id_lists["PLAINLIST"]))   {$id_lists["PLAINLIST"] = $this->cleanPlainList($id_lists["PLAINLIST"]);}
00799 
00800 //                                              debug($id_lists);
00801