<?php
/**
* 数据调用
* @author moufer<moufer@163.com>
* @copyright www.modoer.com
* @version $Id 1 2008-09-16 17:39 $
*/
(!defined('IN_ADMIN') || !defined('IN_MUDDER')) && exit('Access Denied');

$op = empty($op) ? 'list' : $op;
switch($op) {
    case 'list':
        if(!check_submit('datacallsubmit')) {
            $tplname = 'datacall_list';
            $where = "1=1";
            $module && $where .= " AND module='$module'";
            $total = $db->get_value("SELECT COUNT(*) FROM {$dbpre}datacall WHERE $where");
            if($total > 0) {
                $limit = get_limit($page, $offset=20);
                $multipage = multi($total, $offset, $page, 'admincp.php?action=modoer&file=datacall&op=list');
                $query = $db->query("SELECT * FROM {$dbpre}datacall WHERE $where ORDER BY callid DESC LIMIT $limit");
            }
        } else {
            if(is_array($callids)) {
                $ids = "'".implode("','", $callids)."'";
                $db->query("DELETE FROM {$dbpre}datacall WHERE callid IN($ids)");
                cache_datacall();
                cpmsg('操作完毕！', 'admincp.php?action=modoer&file=datacall&op=list');
            } else {
                cpmsg('未选择操作项，请返回选择。');
            }
        }
        break;
    case 'add':
    case 'edit':
        if(!check_submit('datacallsubmit')) {
            $tplname = 'datacall_funcall';
            if($op == 'edit') {
                empty($callid) and cpmsg('未知的调用ID。','stop');
                $datacall = $db->get_one("SELECT * FROM {$dbpre}datacall WHERE callid='$callid'");
                empty($datacall) and cpmsg('调用项目不存在。', 'stop');
                $datacall['expression'] = unserialize($datacall['expression']);
                if(is_array($datacall['expression'])) foreach($datacall['expression'] as $key => $val) {
                    if(empty($val)) continue;
                    if(in_array($key, array('cachetime','row','order'))) continue;
                    $datacall['expression']['params'] = $split.$key.'='.$val;
                    $split = "\r\n";
                }
            } else {
                $datacall = array();
                $datacall['var'] = 'mydata';
                $datacall['expression']['cachetime'] = 1000;
                $datacall['expression']['row'] = 10;
            }
        } else {
            empty($datacall['name']) and cpmsg('未填写名称，请返回填写。');
            createDatacall($datacall,'fun',$op=='add'?0:$callid);
            if($op == 'add') {
                $msg = '新调用添加完毕！';
            } elseif($op == 'edit') {
                $msg = '调用更新完毕！';
            }
            cache_datacall();
            cpmsg($msg, 'admincp.php?action='.$action.'&file='.$file.'&op=list');
        }
        break;
    case 'addsql':
    case 'editsql':
        if(!check_submit('datacallsubmit')) {
            $tplname = 'datacall_sqlcall';
            if($op == 'editsql') {
                empty($callid) and cpmsg('未知的调用ID。','stop');
                $datacall = $db->get_one("SELECT * FROM {$dbpre}datacall WHERE callid='$callid'");
                empty($datacall) and cpmsg('调用项目不存在。', 'stop');
                $datacall['expression'] = unserialize($datacall['expression']);
            } else {
                $datacall = array();
                $datacall['var'] = 'mydata';
                $datacall['expression']['cachetime'] = 1000;
                $datacall['expression']['limit'] = '0,10';
            }
        } else {
            empty($datacall['name']) and cpmsg('未填写名称，请返回填写。');
            createDatacall($datacall,'sql',$op=='addsql'?0:$callid);
            if($op == 'addsql') {
                $msg = '新调用添加完毕！';
            } elseif($op == 'editsql') {
                $msg = '调用更新完毕！';
            }
            cache_datacall();
            cpmsg($msg, 'admincp.php?action='.$action.'&file='.$file.'&op=list');
        }
        break;
    case 'code':
        $tplname = 'datacall_code';
        if(empty($callid)) cpmsg('无效的调用ID，请返回检查。');
        $datacall = $db->get_one("SELECT * FROM {$dbpre}datacall WHERE callid='$callid'");
        if(empty($datacall)) cpmsg('调用数据不存在，请返回检查。');
        break;
}
include cptpl($tplname);
?>