<?php
defined('PHP168_PATH') or die();

class P8_Plugin_Ad_flash extends P8_Plugin{

function __construct(&$core, $name){
	$this->core = &$core;
	parent::__construct($name);
	$this->table = $this->TABLE_;
}

function P8_Plugin_Ad_flash(&$core, $name){
	$this->__construct($core, $name);
}

function add($data){
	$insert_id = $this->DB_master->insert(
		$this->table,
		array(
			'title' => $data['title'],
			'link' => $data['link'],
			'bigpic' => $data['bigpic'],
			'smallpic' => $data['smallpic'],
			'timestamp' => P8_TIME,
		),
		array('return_id' => true)
	);
	return $insert_id;

}

function get($id=''){
	$select=select();
	$select -> from($this->table,'*');
	$id && $select->in('id',$id);
	$data=$this->core->list_item($select);
	foreach ((array)$data as $key => $val){
		$data[$key]['bigpic']=attachment_url($val['bigpic']);
		$data[$key]['smallpic']=attachment_url($val['smallpic']);
	}
	return $data;

}

function edit($data){
	$this->DB_master->update(
		$this->table,
		array(
			'title' => $data['title'],
			'link' => $data['link'],
			'bigpic' => $data['bigpic'],
			'smallpic' => $data['smallpic'],
			'timestamp' => P8_TIME,
		),
		"id='". $data['id']."'"
	);
}

function show($id,$step){
	$this->DB_master->update(
		$this->table,
		array(
			'ifshow' => $step
		),
		"id='". $id."'"
	);
}

function delete($id){
	$this->DB_master->delete(
	$this->table,
	"id='". $id."'"
	);
}

function _cache(){
	$data=$this->get();
	$charset = $this->core->CONFIG['page_charset'];
	$config=$this->get_config();
	$config['textbolds']=isset($config['textbolds'])? 'true' : 'false';
	$cd = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<imgList>\r\n<pic>\r\n";
	$count=0;
	foreach($data as $k=>$v){
		if(empty($v['ifshow']))continue;
		$cd .= '<list path="'.$v['bigpic'].'" ';
		$cd .= 'smallpath="'.$v['smallpic'].'" ';
		$cd .= 'smallinfo="'.$v['title'].'">';
		$cd .= $v['link'];
		$cd .= "</list>\r\n";
		$count++;
	}
	$cd .= "</pic>\r\n";
	$cd .= "<rollTime fade_in=\"".$config['rolltimes']."\">".$config['rolltimes']."</rollTime>\r\n";
	$cd .=	"<text font=\"".$config['textfonts']."\" size=\"".$config['textsizes']."\" bold=\"".$config['textbolds']."\" color=\"0xFFFFFF\"></text>\r\n";
	$cd .=	"</imgList>";
	$file=CACHE_PATH."core/plugin/".$this->name."/";
	md($file);
	$file .="ad_flash.xml";
	$fp = fopen($file, 'w');
	flock($fp, LOCK_EX);
	$status = fwrite($fp, convert_encode($charset,'utf-8',$cd) );
	flock($fp, LOCK_UN);

	return $status;
}

function _display(){
	global $RESOURCE;
	$config=$this->get_config();
	$data="<div id=\"flashcontent\"></div>";
	$data.="<script src=\"".$RESOURCE."/js/swfobject.js\"></script>";
	$data.="<script type=\"text/javascript\">";
	$data.="var so = new SWFObject(\"".$RESOURCE."/plugin/ad_flash/ad_flash.swf?RESOURCE=".$RESOURCE."\", \"sotester\", \"".$config['width']."\", \"".$config['height']."\", \"0\", \"#FFFFFF\");	";
	$data.="so.write(\"flashcontent\");";
	$data.="</script>";
	echo $data;
}

}