<?php
/*
+-----------------------------------------------------------------------------+
| $Id: manage.php 2010-05-24 12:37:44Z Bleakwind $
| Manage channel manager
| 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'] == "add"){
    if ( $sys->post['username'] == "" ||
         $sys->post['rank'] == ""){
        $sys->prompt("failed","用户名,排序必须填写!<!-- username,rank empty -->");
    } elseif ( !preg_match("/^[1-9][0-9]{0,10}$/", $sys->get['channel_id']) ) {
        $sys->prompt("failed","频道ID错误!<!-- channel_id error -->");
    } elseif ( !preg_match("/^[0-9]{1,11}$/",$sys->post['rank']) ) {
        $sys->prompt("failed","排序必须为数字!<!-- rank error -->");
    } else {
        $member_info = func::db_select(DB_TABLE_MEMBER, "id", "username='".$sys->post['username']."'");
        $member_info = $member_info[0];
        if (empty($member_info['id'])) {
            $sys->prompt("failed","用户名不存在!<!-- username not_exist -->");
        } else {
            $sys->post['color']    = preg_match("/^[1-9][0-9]{0,10}$/i",$sys->post['color']) ? $sys->post['color'] : "0";
            $sys->post['bold']     = $sys->post['bold'] == "1" ? "1" : "2";

            $sys->return_channel_info($sys->get['channel_id']);
            $channel_allsub = $sys->return_channel_list($sys->channel_info['id'], null, array('allsub' => "1"));
            foreach($channel_allsub as $k) {
                $if_exist = func::db_select(DB_TABLE_CHANNEL_MANAGE, "admin_id", "channel_id=".$k['id']." AND admin_id=".$member_info['id']);
                $if_exist = $if_exist[0];
                if (empty($if_exist['admin_id'])) {
                    $sql = "INSERT ".DB_TABLE_CHANNEL_MANAGE." SET
                                channel_id      ='".addslashes($k['id'])."',
                                admin_id        =".addslashes($member_info['id']).",
                                color           ='".addslashes($sys->post['color'])."',
                                bold            ='".addslashes($sys->post['bold'])."',
                                rank            ='".addslashes((int)$sys->post['rank'])."',
                                time_created    ='".$sys->nowtime."'";
                    $result_friend = $db->Execute($sql);
                    if (!$result_friend) {
                        echo $db->ErrorMsg();
                    }
                }
            }
            $sql = "INSERT ".DB_TABLE_CHANNEL_MANAGE." SET
                        channel_id      ='".addslashes($sys->get['channel_id'])."',
                        admin_id        =".addslashes($member_info['id']).",
                        color           ='".addslashes($sys->post['color'])."',
                        bold            ='".addslashes($sys->post['bold'])."',
                        rank            ='".addslashes((int)$sys->post['rank'])."',
                        time_created    ='".$sys->nowtime."'";
            $result_friend = $db->Execute($sql);
            if (!$result_friend) {
                echo $db->ErrorMsg();
            }
            $sys->prompt("jump",$CONFIGURE['common']['control_admin']."?act=manage&channel_id=".$sys->get['channel_id']);
        }

    }

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

    if (!preg_match("/^[1-9][0-9]{0,10}$/", $sys->get['channel_id']) ) {
        $sys->prompt("failed","频道ID错误!<!-- channel_id error -->");
    }elseif(!preg_match("/^[1-9][0-9]{0,10}$/i",$sys->get['admin_id'])){
        $sys->prompt("failed","用户名错误!<!-- admin_id error -->");
    }else{
        $sql = "DELETE FROM ".DB_TABLE_CHANNEL_MANAGE." WHERE
                    channel_id=".$sys->get['channel_id']." AND admin_id=".$sys->get['admin_id']."";
        $result = $db->Execute($sql);
        if (!$result) {
            echo $db->ErrorMsg();
        }
    }
    $sys->prompt("jump",$CONFIGURE['common']['control_admin']."?act=manage&channel_id=".$sys->get['channel_id']);

}else{

    if( !preg_match("/^[1-9][0-9]{0,10}$/i",$sys->get['channel_id']) ){
        $sys->prompt("failed","频道ID错误!<!-- channel_id error -->");
    } else {
        $channel_info = func::db_select(DB_TABLE_CHANNEL, "id", "id=".$sys->get['channel_id']."");
        $channel_info = $channel_info[0];

        if( !preg_match("/^[1-9][0-9]{0,10}$/i",$channel_info['id']) ){
            $sys->prompt("failed","此频道不存在!<!-- channel_id error -->");
        } else {
            function set_value_onfocus($name, $id, $value, $type="text", $maxlength="255", $style="")
            {
                global $SETTING, $LANGUAGE, $MEMBER, $ADMIN, $db, $sys, $select_option;
                $ajax_response = new xajaxResponse();
                if (!empty($style)) { $style = "style=\"".$style."\""; }

                if ($type == "text") {
                    $result = '<input type="text" id="'.$name.'_'.$id.'_input" name="'.$name.'_'.$id.'_input" value="'.$value.'" maxlength="'.$maxlength.'" size="6" class="input" '.$style.' onblur="xajax_set_value_onblur(\''.$name.'\', \''.$id.'\', this.value);">';
                    $ajax_response->assign($name.'_'.$id, "innerHTML", $result);
                    $ajax_response->script("$('#".$name."_".$id."_input').focus();");
                } elseif($type == "textarea") {
                    $result = '<textarea id="'.$name.'_'.$id.'_input" name="'.$name.'_'.$id.'_input" rows="1" cols="6" class="textarea" '.$style.' onblur="xajax_set_value_onblur(\''.$name.'\', \''.$id.'\',  this.value);">'.$value.'</textarea>';
                    $ajax_response->assign($name.'_'.$id, "innerHTML", $result);
                    $ajax_response->script("$('#".$name."_".$id."_input').focus();");
                } elseif ($type == "textarea_resize") {
                    $result = '<textarea id="'.$name.'_'.$id.'_input" name="'.$name.'_'.$id.'_input" rows="1" cols="6" class="textarea" '.$style.' onblur="xajax_set_value_onblur(\''.$name.'\', \''.$id.'\', this.value);">'.$value.'</textarea>';
                    $ajax_response->assign($name.'_'.$id, "innerHTML", $result);
                    $ajax_response->script("$('#".$name."_".$id."_input').resizable({ autohide: true, minWidth: 200, minHeight: 100,  maxWidth: 800, maxHeight: 600 });");
                    $ajax_response->script("$('#".$name."_".$id."_input').focus();");
                } elseif($type == "checkbox") {
                    $checked = $value == "1" ? "checked" : "";
                    $result = '<input type="checkbox" id="'.$name.'_'.$id.'_input" name="'.$name.'_'.$id.'_input" value="1" '.$checked.' class="checkbox" '.$style.' onblur="xajax_set_value_onblur(\''.$name.'\', \''.$id.'\', this.checked == true ? 1 : 2);">';
                    $ajax_response->assign($name.'_'.$id, "innerHTML", $result);
                    $ajax_response->script("$('#".$name."_".$id."_input').focus();");
                } elseif($type == "color") {
                    if (!empty($select_option['manage_color'])) {
                        $result = '<select id="'.$name.'_'.$id.'_input" name="'.$name.'_'.$id.'_input" class="select" '.$style.' onblur="xajax_set_value_onblur(\''.$name.'\', \''.$id.'\', this.value);">';
                        $result .= '<option value=""></option>';
                        foreach ($select_option['manage_color'] as $v) {
                            if ($v['id'] == $value){
                                $result .= '<option value="'.$v['id'].'" style="background-color:'.$v['value'].'" selected="selected">'.$v['name'].'</option>';
                            } else {
                                $result .= '<option value="'.$v['id'].'" style="background-color:'.$v['value'].'">'.$v['name'].'</option>';
                            }
                        }
                        $result .= '</select>';
                        $ajax_response->assign($name.'_'.$id, "innerHTML", $result);
                        $ajax_response->script("$('#".$name."_".$id."_input').focus();");
                    }
                }
                return $ajax_response;
            }
            $bwajax->register(XAJAX_FUNCTION, "set_value_onfocus");

            function set_value_onblur($name, $id, $value="")
            {
                global $SETTING, $LANGUAGE, $ASESSION, $ADMIN, $db, $sys;
                $ajax_response = new xajaxResponse();
                $if_error   = false;
                $error_msg  = array();

                // check
                if ($name == "rank") {
                    if ( !preg_match("/^[0-9]{1,11}$/",$value) ) {
                        $if_error       = true;
                        $error_msg[]    = "排序必须为数字!";
                    }
                }

                // update
                if (!$if_error) {
                    $sql = "UPDATE ".DB_TABLE_CHANNEL_MANAGE." SET
                                ".$name."       = '".addslashes($value)."',
                                time_created    = '".$sys->nowtime."'
                            WHERE channel_id=".$sys->get['channel_id']." AND admin_id=".$id."";
                    $result = $db->Execute($sql);
                    if (!$result) {
                        $if_error       = true;
                        $error_msg[]    = $db->ErrorMsg();
                    }
                }

                // select and set hidden value
                if (!$if_error) {
                    $sql = "SELECT channel_manage.*, member.username as username
                            FROM ".DB_TABLE_CHANNEL_MANAGE." channel_manage
                                LEFT JOIN ".DB_TABLE_MEMBER." member ON member.id=channel_manage.admin_id
                            WHERE channel_manage.admin_id=".$id." AND channel_manage.channel_id=".$sys->get['channel_id']."";
                    $result = $db->Execute($sql);
                    if (!$result) {
                        $if_error       = true;
                        $error_msg[]    = $db->ErrorMsg();
                    } else {
                        $ajax_response->assign($name."_".$id."_value", "value", $result->fields[$name]);
                    }
                }

                // show new
                if (!$if_error) {
                    if ($name == "color"||
                        $name == "bold") {
                        $update_username['value']               = $result->fields['username'];

                        $select_color  = $sys->return_select_value($result->fields['color']);
                        if (preg_match("/^#?[0-F]{6}$/i",$select_color['value'])) {
                            $update_username['color']           = "color:".$select_color['value'].";";
                            $update_color['value']              = $select_color['name'];
                            $update_color['background_color']   = "background-color:".$select_color['value'].";";
                        } else {
                            $update_username['color']           = "";
                            $update_color['value']              = "&nbsp;";
                            $update_color['background_color']   = "";
                        }

                        if ($result->fields['bold'] == "1") {
                            $update_username['font_weight']     = "font-weight:bold;";
                            $update_bold['font_weight']         = "&radic;";
                        } else {
                            $update_username['font_weight']     = "";
                            $update_bold['font_weight']         = "";
                        }

                        $ajax_response->assign("username_".$id, "innerHTML", '<div style="'.$update_username['color'].$update_username['font_weight'].'">'.$update_username['value'].'</div>');
                        $ajax_response->assign("color_".$id, "innerHTML", '<div style="width: 60px;height:18px;border:1px solid #EEEEEE;'.$update_color['background_color'].'">'.$update_color['value'].'</div>');
                        $ajax_response->assign("bold_".$id, "innerHTML", $update_bold['font_weight']);

                    } elseif ($name == "rank") {
                        $ajax_response->assign("rank_".$id, "innerHTML", $result->fields['rank']);
                        $ajax_response->redirect($CONFIGURE['common']['control_admin']."?act=manage&channel_id=".$sys->get['channel_id']);

                    } else {
                        $ajax_response->assign($name."_".$id, "innerHTML", $result->fields[$name]);
                    }
                }

                // alert error
                if ($if_error) {
                    $ajax_response->alert("发生错误,错误信息如下:\n- ".implode("\n- ", $error_msg));
                }
                return $ajax_response;
            }
            $bwajax->register(XAJAX_FUNCTION, "set_value_onblur");

            $sql = "SELECT channel_manage.*,
                        member.username as username,
                        s.value as color_value,s.name as color_name
                    FROM ".DB_TABLE_CHANNEL_MANAGE." channel_manage
                        LEFT JOIN ".DB_TABLE_MEMBER." member ON member.id=channel_manage.admin_id
                        LEFT JOIN ".DB_TABLE_SELECT." s ON s.id=channel_manage.color
                    WHERE channel_manage.channel_id=".$sys->get['channel_id']."
                    ORDER BY channel_manage.rank, channel_manage.admin_id";
            $result = &$db->Execute($sql);
            if (!$result) {
                echo $db->ErrorMsg();
            }else {
                while(!$result->EOF){
                    $channel_manage_list[] = array(
                        "channel_id"    => $result->fields['channel_id'],
                        "admin_id"      => $result->fields['admin_id'],
                        "color"         => $result->fields['color'],
                        "bold"          => $result->fields['bold'],
                        "rank"          => $result->fields['rank'],
                        "time_created"  => $result->fields['time_created'],

                        "username"      => $result->fields['username'],

                        'color_value'   => $result->fields['color_value'],
                        'color_name'    => $result->fields['color_name'],
                    );
                    $result->MoveNext();
                }
            }
            $t->assign(array(
                "channel_manage_list"    => $channel_manage_list,
            ));

            $array_select  = array("manage_color");
            $select_option = $sys->return_select($array_select);

            $add_color = '<select id="color" name="color" class="select" style="width:60px">';
            $add_color .= '<option value="">颜色</option>';
            if (count($select_option['manage_color']) > 0) {
                foreach ($select_option['manage_color'] as $color) {
                    if ($color['if_default'] == "1"){
                        $add_color .= '<option value="'.$color['id'].'" style="background-color:'.$color['value'].'" selected="selected">'.$color['name'].'</option>';
                    } else {
                        $add_color .= '<option value="'.$color['id'].'" style="background-color:'.$color['value'].'">'.$color['name'].'</option>';
                    }
                }
            }
            $add_color .= '</select>';

            $t->assign(array(
                "add_color"    => $add_color,
            ));
        }
    }
}
?>
