<?php
/*
+-----------------------------------------------------------------------------+
| $Id: box.php 2010-05-24 12:37:44Z Bleakwind $
| Public return something
| 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;
}

// quicklaunch
if ($ADMIN['iflogin'] == "1") {

    // select_sort
    $t->assign(array(
        "select_sort"       => $SELECTSORT,
    ));
    //////

    // config_sort
    $config_sort    = func::db_select(DB_TABLE_CONFIG_SORT, "*", "", "", "cs_rank, cs_id");
    $t->assign(array(
        "config_sort"       => $config_sort,
    ));
    //////

    //return_channel_all
    $sys->return_channel_all();
    $t->assign(array(
        "channel_all"       => $sys->channel_all,
        "channel_upper"     => $sys->channel_upper,
    ));
    //////

    // channel_top
    $channel_top    = $sys->return_channel_list("0");
    $t->assign(array(
        "channel_top"       => $channel_top,
    ));
    //////

    // channel_tree
    $channel_tree   = $sys->return_channel_tree();
    $t->assign(array(
        "channel_tree"      => $sys->channel_tree,
    ));
    //////

    // setmodel_list
    $setmodel_list  = $sys->return_setmodel_list();
    $t->assign(array(
        "setmodel_list"     => $setmodel_list,
    ));
    //////

    $quicklaunch = array();
    $sql = "SELECT aq.*, s.value as name_color_value, s.name as name_color_name
            FROM ".DB_TABLE_ADMIN_QUICKLAUNCH." aq
                LEFT JOIN ".DB_TABLE_SELECT." s ON s.id=aq.name_color
            WHERE admin_id=".$ADMIN['mid']."
            ORDER BY aq.rank,aq.id";
    $result = &$db->Execute($sql);
    if (!$result) {
        echo $db->ErrorMsg();
    } else {
        while (!$result->EOF) {
            $quicklaunch[] = array(
                'id'                => $result->fields['id'],
                'name'              => $result->fields['name'],
                'name_color'        => $result->fields['name_color'],
                'name_bold'         => $result->fields['name_bold'],
                'url'               => $result->fields['url'],
                'time'              => $result->fields['time'],
                'rank'              => $result->fields['rank'],
                'name_color_value'  => $result->fields['name_color_value'],
                'name_color_name'   => $result->fields['name_color_name'],
            );
            $result->MoveNext();
        }
    }
    $t->assign(array(
        "quicklaunch"   => $quicklaunch,
    ));

    // datum_list
    if ($sys->get['act'] == "index_datum_show" || $sys->get['act'] == "datum_show") {
        $where          = "";
        $where_total    = "";

        $where          .= " AND ((datum.if_enable=1) || (datum.if_enable=3 AND datum.if_enable_begin<".$sys->nowtime." AND datum.if_enable_end>".$sys->nowtime."))";
        $where_total    .= " AND ((if_enable=1) || (if_enable=3 AND if_enable_begin<".$sys->nowtime." AND if_enable_end>".$sys->nowtime."))";

        $where       = substr(trim($where), 0, 3) == "AND" ? substr(trim($where), 3) : trim($where);
        $where_total = substr(trim($where_total), 0, 3) == "AND" ? substr(trim($where_total), 3) : trim($where_total);
        $where       = $where != "" ? "WHERE ".$where : "";
        $where_total = $where_total != "" ? "WHERE ".$where_total : "";

        $total_record = 0;
        $sql = "SELECT count(*) as total_record
                FROM ".DB_TABLE_DATUM."
                ".$where_total;
        $result = &$db->Execute($sql);
        if ($result) {
            $total_record = (int)$result->fields['total_record'];
        }
        $sql = "SELECT datum.*,
                    cover.id as cover_id, cover.dir as cover_dir, cover.filename as cover_filename
                FROM ".DB_TABLE_DATUM." datum
                    LEFT JOIN ".DB_TABLE_DATUM_COVER." cover ON cover.datum_id=datum.id
                ".$where."
                ORDER BY datum.if_top, datum.rank, datum.post_time DESC";
        $result = &$db->Execute($sql);
        if (!$result) {
            echo $db->ErrorMsg();
        } else {
            while (!$result->EOF) {
                $datum_list[] = array(
                    'id'                    => $result->fields['id'],
                    'phrase'                => $result->fields['phrase'],
                    'phrase_len'            => mb_strwidth($result->fields['phrase'],"UTF-8"),
                    'subject'               => $result->fields['subject'],
                    'subject_len'           => mb_strwidth($result->fields['subject'],"UTF-8"),
                    'prefix'                => $result->fields['prefix'],
                    'color'                 => $result->fields['color'],
                    'fontstyle'             => $result->fields['fontstyle'],
                    'decoration'            => $result->fields['decoration'],
                    'keyword'               => $result->fields['keyword'],
                    'brief'                 => $result->fields['brief'],
                    'post_id'               => $result->fields['post_id'],
                    'post_username'         => $result->fields['post_username'],
                    'post_ip'               => $result->fields['post_ip'],
                    'post_time'             => $result->fields['post_time'],
                    'amend_id'              => $result->fields['amend_id'],
                    'amend_username'        => $result->fields['amend_username'],
                    'amend_ip'              => $result->fields['amend_ip'],
                    'amend_time'            => $result->fields['amend_time'],
                    'click'                 => $result->fields['click'],
                    'if_enable'             => $result->fields['if_enable'],
                    'if_enable_begin'       => $result->fields['if_enable_begin'],
                    'if_enable_end'         => $result->fields['if_enable_end'],
                    'if_top'                => $result->fields['if_top'],
                    'if_top_begin'          => $result->fields['if_top_begin'],
                    'if_top_end'            => $result->fields['if_top_end'],
                    'if_turn'               => $result->fields['if_turn'],
                    'if_turn_type'          => $result->fields['if_turn_type'],
                    'if_turn_url'           => $result->fields['if_turn_url'],
                    'rank'                  => $result->fields['rank'],

                    'cover_id'              => $result->fields['cover_id'],
                    'cover_dir'             => $result->fields['cover_dir'],
                    'cover_filename'        => $result->fields['cover_filename'],
                );
                $result->MoveNext();
            }
        }
        $t->assign(array(
            "datum_list"  => $datum_list,
        ));
    }
}
//////
?>
