<?php

/**
 *      
 */


if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}
$do=$_GET['do'];
if($do=='getlist'){
	$page = empty($_GET['page'])?1:intval($_GET['page']);
		$classid=intval($_GET['classid']);
		$subid=intval($_GET['subid']);
		$perpage = 12;
		$start = ($page-1)*$perpage;
		$gets = array(
				'mod'=>'widget',
				'op'=>'index',
				'do'=>'getlist',
				'classid'=>$classid,
				'subid'=>$subid,
			);
		$theurl = DZZSCRIPT."?".url_implode($gets);
		//获取所有分类
		$wheresql="1";
		$ordersql='order by disp DESC';
			//主分类选项
			if($classid) $wheresql.=" and classid='{$classid}'";
			else{
				$ordersql=' ORDER BY setupnum DESC';
			}
			//子分类选项
			if($subid){
				$wids=array();
				$query=DB::query("select wid from ".DB::table('dzz_widget_relative')." where classid='{$subid}'");
				while($value=DB::fetch($query)){
					$wids[]=$value['wid'];
				}
				if($wids) $wheresql.=" and wid IN (".dimplode($wids).")";
				else $wheresql.=" and wid ='0'";
			}
			
			
			//取得所有符合条件的应用列表
			$list=array();
			$count=DB::result_first("SELECT COUNT(*) FROM ".DB::table('dzz_widget_market')." where $wheresql  ");
			if($count){
				$query=DB::query("SELECT * FROM ".DB::table('dzz_widget_market')."  where $wheresql $ordersql   LIMIT $start,$perpage" );
				while($values=dstripslashes(DB::fetch($query))){
					$values['dateline']=dgmdate($values['dateline'],'Y-m-d');
					if($values['icon']=='') $values['icon']=='dzz/images/default/widgetdefault.png';
					if($values['icon']!='dzz/images/default/widgetdefault.png' && !preg_match("/^(http|ftp|https|mms)\:\/\/(.+?)/i", $values['icon'])){
						$values['icon']=$_G['setting']['attachurl'].str_replace($_G['setting']['attachurl'],'',$values['icon']);
					}
					$list[$values['wid']]=$values;
				}
			}
			$multi = multi($count, $perpage, $page, $theurl);	
	include dzztemplate('widget/widget_getlist');
}elseif($do=='addtodesktop'){
	$msg='';
	if(!$space['self']){
		echo json_encode_gbk(array('msg'=>dzzlang('message','no_privilege')));
		exit();
	}
	$ukey=trim($_GET['ukey']);
	if($ukey!=DB::result_first("select `ukey` from ".DB::table('dzz_userconfig')." where uid='{$space[uid]}'")){
		echo json_encode_gbk(array('msg'=>dzzlang('message','need_refresh')));
		exit();
	}
	if($msg){
		echo json_encode_gbk(array('msg'=>$msg));
		exit();
	}
	//添加挂件
	$wid=intval($_GET['wid']);
	if(!$widget=dstripslashes(DB::fetch_first("select * from ".DB::table('dzz_widget_market')." where wid='{$wid}'"))){
		echo json_encode_gbk(array('msg'=>dzzlang('message','widget_not_exist')));
		exit();
	}
	$setarr=array(
						'uid'=>$space['uid'],
						'username'=>$space['username'],
						'oid'=>$wid,
						'notdelete'=>intval($widget['notdelete']),
						'url'=>$widget['url'],
						'type'=>$widget['type'],
						'open'=>$widget['open'],
						'height'=>$widget['height'],
						'width'=>$widget['width'],
						'idtype'=>$widget['idtype'],
						'typeid'=>$widget['typeid'],
						'href'=>$widget['href'],
						'classname'=>$widget['classname'],
						'dateline'=>$_G['timestamp']
					);
	
	if($setarr['gid']=DB::insert('dzz_widget',daddslashes($setarr),1)){
		DB::update('dzz_widget_market',array('setupnum'=>$widget['setupnum']+1),"wid = '{$wid}'");
		$setarr['url']=replace_canshu($setarr['url']);
		echo json_encode_gbk(array('msg'=>'success','data'=>$setarr));
		exit();
	}
	
}else{
	$class=array();
		$query=DB::query("select * from ".DB::table('dzz_widget_class')." where 1 order by disp ");
		while($value=dstripslashes(DB::fetch($query))){
			$class[$value['fupid']][]=$value;
		}
	$navtitle=$lang['widget_market'];
	include dzztemplate('widget/widget_widget');
}

?>