<?php
/*
This file is part of Thomas Bley's moregroupware webmail2/calendar2/admin2 module. (tb-mgw)

More information:
-----------------

Simple Groupware Solutions Thomas Bley
Fichtenstr. 23
Germany, 85640 Putzbrunn

Contact:
--------

info@simple-groupware.de

http://www.simple-groupware.de


Copyright 2002-2003 Thomas Bley
tb-mgw is distributed under the terms of the GNU General Public License 

tb-mgw is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

tb-mgw is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with tb-mgw; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

// Maillist-inc
// Moregroupware.org


function make_sorts() {
  global $MGW, $thesort, $ad_type, $nextsort, $newsort, $newsort2, $thesort2;
  
  if ($newsort != "") { $_SESSION["webmail2_mynewsort"] = $newsort; } elseif (!isset($_SESSION["webmail2_mynewsort"])) {$_SESSION["webmail2_mynewsort"] = $MGW->settings["wm2_sort"];}
  
  $nextsort["from"]="fa";
  $nextsort["date"]="dd";
  $nextsort["subject"]="sa";
  $nextsort["messageid"]="ma";

  if ($newsort2 == "ac") { $_SESSION["webmail2_sortbyaccount"] = "true"; }
    elseif ($newsort2 == "ac2") { $_SESSION["webmail2_sortbyaccount"] = ""; }
    elseif (isset($_SESSION["webmail2_sortbyaccount"])) {}
    elseif ($MGW->settings["wm2_sortbyaccount"]) { $_SESSION["webmail2_sortbyaccount"] = "true"; }
    else { $_SESSION["webmail2_sortbyaccount"] = ""; }

  if ($_SESSION["webmail2_sortbyaccount"]=="true") {$thesort2 = "account_name,";} else {$thesort2 = "";}

  $newsort = $_SESSION["webmail2_mynewsort"];

  switch ($newsort) {
      case "fa": $thesort = "header_from"; $nextsort["from"]="fd"; break;
      case "fd": $thesort = "header_from"; break;
      case "sa": $thesort = "header_subject"; $nextsort["subject"]="sd"; break;
      case "sd": $thesort = "header_subject"; break;
      case "da": $thesort = "header_date"; break;
      case "dd": $thesort = "header_date"; $nextsort["date"]="da"; break;
      case "ma": $thesort = "message_id"; $nextsort["messageid"]="md"; break;
      default: $newsort = "md"; $thesort = ""; break;
  }

  $ad_type=$newsort[1];
  $ad_type=str_replace(array("a","d"),array("asc","desc"),$ad_type);
}  
  
function get_maillist_searches() {
  global $hashmap,$mbox_id,$thesort,$mboxname,$user_id,$searchin,$searchfor,$conn;

  if ($searchfor!="") {
    if ($searchin != "local" and $searchin != "public") {
      $mboxname .= " / ".$hashmap["maillist_search_result"];
    } else {
      $mboxname = $hashmap["maillist_search_result"];
    }
  
    $searchfor2 = $conn->Quote("%".$searchfor."%");
    $search = " (header_subject like ".$searchfor2." or header_from like ".$searchfor2." or header_to like ".$searchfor2.")";
    if ($searchin == "local") {
      $sql = "select mes.* from mgw_webmail2_messages as mes,mgw_webmail2_mailboxes as box where box.user_id=".(int)$user_id." and box.mbox_id=mes.mbox_id and ".$search." order by mbox_id,".$thesort;
      $sql2 = "select mes.message_id from mgw_webmail2_messages as mes,mgw_webmail2_mailboxes as box where box.user_id=".(int)$user_id." and box.mbox_id=mes.mbox_id and ".$search;
    } elseif ($searchin == "public") {
      $sql = "select mes.* from mgw_webmail2_messages as mes,mgw_webmail2_mailboxes as box where box.user_id=0 and box.mbox_id=mes.mbox_id and ".$search." order by mbox_id,".$thesort;
      $sql2 = "select mes.message_id from mgw_webmail2_messages as mes,mgw_webmail2_mailboxes as box where box.user_id=0 and box.mbox_id=mes.mbox_id and ".$search;
    } else {
      $sql = "select * from mgw_webmail2_messages where mbox_id=".(int)$mbox_id." and ".$search." order by ".$thesort;
      $sql2 = "select message_id from mgw_webmail2_messages where mbox_id=".(int)$mbox_id." and ".$search;
    }
  } else {
    $sql = "select * from mgw_webmail2_messages where mbox_id=".(int)$mbox_id." order by ".$thesort;
    $sql2 = "select message_id from mgw_webmail2_messages where mbox_id=".(int)$mbox_id;
  }
  return array($sql,$sql2);  
}
  
function get_maillist_pages($messagecount) {
  global $MGW,$msg_begin,$mbox_id,$account,$searchfor, $searchin;
  
  $i = 1;
  $pages = array();

  if ($messagecount > $MGW->settings["wm2_emailsperpage"]) {
    while ($messagecount > 0) {
   
      if ($messagecount > $MGW->settings["wm2_emailsperpage"]) {
        $limiter = $MGW->settings["wm2_emailsperpage"];
      } else { $limiter = $messagecount; }
  
      if ($msg_begin==$i) {
        $pages[] = '[<b>'.$i."-".($i+$limiter-1)."</b>]";
      } else {
        $pages[] = '[<a href="maillist.php?searchfor='.$searchfor.'&searchin='.$searchin.'&account='.$account.'&mbox_id='.$mbox_id.'&msg_begin='.$i.'&'.SID.'">'.$i."-".($i+$limiter-1)."</a>]";
      }
      $i = $i + $MGW->settings["wm2_emailsperpage"];
      $messagecount = $messagecount - $MGW->settings["wm2_emailsperpage"];
    }
  } elseif ($messagecount > 0) { $pages[] = ''; }
  return $pages;
}



?>