<?php
/*
+-----------------------------------------------------------------------------+
| $Id: pm_out.php 2010-05-24 12:37:44Z Bleakwind $
| Outbox for admin
| Copyright (c) 2003-2010 Bleakwind (www.weaverdream.com)
| http://www.weaverdream.com/
| Release under the GNU Lesser General Public License Version 3 (LGPLv3):
|   http://www.gnu.org/licenses/lgpl.html
+-----------------------------------------------------------------------------+
*/

if (!defined( 'ENTRY_INDEX')){
    echo "<h1>Forbidden</h1><p>You don't have permission to access on this server.</p>";
    exit;
}

if($sys->get['ope'] == "save"){
    // check the post
    if (!empty($sys->post['del'])){
        $where = "id IN (".implode(",", $sys->post['del']).")";
    }else{
        $where = "id IN (0)";
    }

    // delete
    $sql = "DELETE FROM ".DB_TABLE_PM_OUT."
                WHERE ".$where."";
    $result = $db->Execute($sql);            
    if (!$result) {
        echo $db->ErrorMsg();
    }

    $sys->prompt("jump",$CONFIGURE['common']['control_admin']."?act=pm_out");

}elseif($sys->get['ope'] == "del"){

    if(!preg_match("/^[0-9]+$/i",$sys->get['piid'])){
        $sys->prompt("failed","短信ID错误!<!-- piid error -->");
    }else{
        // delete
        $sql = "DELETE FROM ".DB_TABLE_PM_OUT."
                    WHERE id=".$sys->get['piid'];
        $result = $db->Execute($sql);            
        if (!$result) {
            echo $db->ErrorMsg();
        }
    
        $sys->prompt("jump",$CONFIGURE['common']['control_admin']."?act=pm_out&pag=".$sys->get['pag']);
    }

}elseif($sys->get['ope'] == "detail"){
    if(!preg_match("/^[0-9]+$/i",$sys->get['piid'])){
        $sys->prompt("failed","短信ID错误!<!-- piid error -->");
    }else{
        $sql = "SELECT pi.*
                FROM ".DB_TABLE_PM_OUT." pi
                WHERE pi.id=".$sys->get['piid']."";
        $result = &$db->Execute($sql);
        if (!$result) {
            echo $db->ErrorMsg();
        } else {
            if (!$result->EOF) {

                $member_info = $sys->return_member($result->fields['tid'], array("id","username"));

                $pm_out_list = array(
                       'id'         => $result->fields['id'],
                       'subject'    => $result->fields['subject'],
                       'content'    => $result->fields['content'],
                       'fid'        => $result->fields['fid'],
                       'tid'        => $result->fields['tid'],
                       'mid'        => $result->fields['mid'],
                       'if_read'    => $result->fields['if_read'],
                       'time'       => $result->fields['time'],
                       'm_id'       => $member_info['m_id'],
                       'm_username' => $member_info['m_username'],
        		);
            }
        }

        $sql = "UPDATE ".DB_TABLE_PM_OUT." SET
                    if_read =1
                WHERE id=".$sys->get['piid'];
        $result = $db->Execute($sql);
        if (!$result) {
            echo $db->ErrorMsg();
        }

        $t->assign(array(
            "pm_out_list" => $pm_out_list,
            "pagination"     => $bwpage->pagination,
        ));
    }
}else{
    
    $total_record = func::db_count_record(DB_TABLE_PM_OUT, "fid=".$MEMBER['id']."");
    $bwpage->set_record_listnum($CONFIG['page_record_pm_out']);
    $bwpage->set_record($total_record);
    $bwpage->page($CONFIGURE['common']['control_admin']."?act=pm_out&");
    $sql = "SELECT pi.*
            FROM ".DB_TABLE_PM_OUT." pi
            WHERE pi.fid=".$MEMBER['id']."
            ORDER BY pi.time DESC, pi.id DESC
            LIMIT ".$bwpage->record_listfirst.",".$bwpage->record_listnum;
    $result = &$db->Execute($sql);
    if (!$result) {
        echo $db->ErrorMsg();
    } else {
        while (!$result->EOF) {

            $member_info = $sys->return_member($result->fields['tid'], array("id","username"));

            $pm_out_list[] = array(
                   'id'         => $result->fields['id'],
                   'subject'    => $result->fields['subject'],
                   'content'    => $result->fields['content'],
                   'fid'        => $result->fields['fid'],
                   'tid'        => $result->fields['tid'],
                   'mid'        => $result->fields['mid'],
                   'if_read'    => $result->fields['if_read'],
                   'time'       => $result->fields['time'],
                   'm_id'       => $member_info['m_id'],
                   'm_username' => $member_info['m_username'],
    		);
    		$result->MoveNext();
        }
    }
    
    $t->assign(array(
        "pm_out_list" => $pm_out_list,
        "pagination"    => $bwpage->pagination,
    ));

}
?>
