<?php
/*  
	[Phpup.Net!] (C)2009-2011 Phpup.net.
	This is NOT a freeware, use is subject to license terms

	$Id: admin.class.php 2010-08-24 10:42 $
*/

if(!defined('IN_PHPUP')) {
	exit('Access Denied');
}
header("Content-type:text/html;charset=".$GLOBALS['charset']);

class admin_controller
{
	function admin_controller()
	{
		if($_REQUEST['act']=='admin_ajax' || $_REQUEST['act']=='admin_delete')
		{
			if(!$GLOBALS['session']->get('adminid'))
			{
				exit('未登录');
			}
		}
		if($_REQUEST['act']!='login' && $_REQUEST['act']!='logout')
		{

			if(!$GLOBALS['session']->get('adminid'))
			{
				sheader('index.php?con='.$GLOBALS['setting']['adminpath'].'&act=login');
			}
			
		}
		$this->site=array('renmin'=>array('site'=>'人民集市网','url'=>'http://renminjishi.com/market/index.php?act=tj.wowotuan','apirep'=>'data-goodsdata-goods=title:limengqikey-title,city_name:limengqikey-city_name,goods_url:limengqikey-goods_url,img_url:limengqikey-img_url,original_price:limengqikey-original_price,sale_price:limengqikey-sale_price,sales_num:limengqikey-sales_num,merchant_name:limengqikey-merchant_name,merchant_addr:limengqikey-merchant_addr,start_time:limengqikey-start_time,close_time:limengqikey-close_time,class:limengqikey-categories'));
		include ROOT_PATH.'/models/common.php';
		
	}
	function couponcate_action()
	{
		$couponcate_mod=new common('plugin_coupon_cate');
		$cates = $couponcate_mod->GetPage ();
		include ROOT_PATH.'/plugins/coupon/views/admin/couponcate.php';
	}
	function multicoupon_action()
	{
		if(submitcheck('commit'))
		{
			$ids=implode(',',$_REQUEST['gid']);
			$coupon_mod=new common('plugin_coupon');
			if($ids)
			{
				if($coupon_mod->DeleteData('1 and id in('.$ids.')'))
				{
					sheader ( 'index.php?pluginid=coupon&con=admin&act=coupon', 3, '删除成功', 'redirect', true );
				}
				else
				{
					sheader ( 'index.php?pluginid=coupon&con=admin&act=coupon', 3, '删除失败', 'redirect', true );
				}
			}
			else
			{
				sheader ( 'index.php?pluginid=coupon&con=admin&act=coupon', 3, '删除失败', 'redirect', true );
			}
		}
	}
	function couponmodify_action()
	{
		$updateid=intval($_REQUEST['updateid']);
		$coupon_mod=new common('plugin_coupon');
		if(submitcheck('commit'))
		{
			$data['title']=trim(strip_tags($_POST['title']));
			$data['merchant_name']=trim(strip_tags($_POST['merchant_name']));
			$data['cityid']=trim(strip_tags($_POST['city']));
			$data['goods_url']=trim(strip_tags($_POST['goods_url']));
			$data['sales_num']=intval($_POST['sales_num']);
			$data['sortorder']=intval($_POST['sortorder']);
			$data['class']=intval($_POST['big_catelist']);
			$data['start_time']=strtotime($_POST['starttime']);
			$data['close_time']=strtotime($_POST['lasttime']);
			$data['city_name']=$GLOBALS['city'][$data['cityid']]['cityname'];
			if ($_FILES ['thumb'] ['name']) {
				$filetime = date ( "Y/m", time () );
				$dir = "data/upload/group/" . $filetime;
				$data ['img_url'] = _upload ( 'thumb', $dir);
			}
			if ($updateid > 0) {
				if ($coupon_mod->UpdateData ( $data, 'and id =' . $updateid )) {
					
					sheader ( 'index.php?pluginid=coupon&con=admin&act=coupon', 3, '更新成功！', 'redirect', true );
				}
			} else {
				if ($id = $coupon_mod->InsertData ( $data )) {
					
					sheader ( 'index.php?pluginid=coupon&con=admin&act=couponmodify', 3, '添加成功,继续添加！', 'redirect', true );
				}
			}
		}
		else
		{
			$cate_mod=new common('plugin_coupon_cate');
			$cates=$cate_mod->GetPage();
			foreach($cates as $k=>$v)
			{
				$tempcate[$v['id']]=$v['catename'];
			}
			if($updateid)
			{
				
				$coupon_info= $coupon_mod->GetOne('and id='.$updateid);
				
			}
			include ROOT_PATH.'/plugins/coupon/views/admin/coupon_form.php';
		}
	}
	function coupon_action()
	{
		$coupon_mod=new common('plugin_coupon');
		$showpage = array ('isshow' => 1, 'currentpage' => $_REQUEST ['page'], 'pagesize' => 20, 'url' => 'index.php?pluginid=coupon&con=admin&act=coupon', 'example' => 2 );
		$cate_mod=new common('plugin_coupon_cate');
		$cates=$cate_mod->GetPage();
		foreach($cates as $k=>$v)
		{
			$tempcate[$v['id']]=$v['catename'];
		}
		$coupons = $coupon_mod->GetPage ( $showpage );
		include ROOT_PATH.'/plugins/coupon/views/admin/coupon.php';
	}
	function attach_action()
	{
		if(submitcheck('commit'))
		{
			include ROOT_PATH.'/models/attach.php';
			$siteid=trim(strip_tags($_REQUEST['siteid']));
			$site=$this->site[$siteid];
			if($site)
			{
				$attach=new attach($site);
				$content=$attach->curl->get($site['url']);
				$data=$attach->parse_rule($content,$site['apirep']);
				if($data)
				{	
					$cate_mod=new common('plugin_coupon_cate');
					$cates=$cate_mod->GetPage();
					foreach($cates as $k=>$v)
					{
						$tempcate[$v['catename']]=$v['id'];
					}
					$count=count($data);
					$all =ceil($count/200);
					$newcate=array();
					for($i=0;$i<$all;$i++)
					{
						$start=$i*200;
						$last=($i+1)*200>$count?$count:($i+1)*200;
						for($j=$start;$j<$last;$j++)
						{
							
							if (isset($data[$j]['start_time']))
							{
								if (strlen($data[$j]['start_time']) == 14)
								{
									$data[$j]['start_time'] = mysql_to_unix($data[$j]['start_time']);
								}
								else if (stripos($data[$j]['start_time'], '+') !== false)
								{
									$data[$j]['start_time'] = strtotime($data[$j]['start_time']);
								}
								else if (stripos($data[$j]['start_time'], '-') !== false && strlen($data[$j]['start_time']) == 19)
								{
									$data[$j]['start_time'] = mysql_to_unix($data[$j]['start_time']);
								}
							}
							

							if (isset($data[$j]['close_time']))
							{
								$data[$j]['close_time'] = trim($data[$j]['close_time']);
								if (strlen($data[$j]['close_time']) == 14)
								{
									$data[$j]['close_time'] = mysql_to_unix($data[$j]['close_time']);
								}
								else if (stripos($data[$j]['close_time'], '+') !== false)
								{
									$data[$j]['close_time'] = strtotime($data[$j]['close_time']);
								}
								else if (stripos($data[$j]['close_time'], '-') !== false && strlen($data[$j]['close_time']) == 19)
								{
									$data[$j]['close_time'] = mysql_to_unix($data[$j]['close_time']);
								}
							}
							if(isset($data[$j]['city_name']))
							{
								$data[$j]['cityid']=Pinyin($data[$j]['city_name'],$GLOBALS['charset']);
							}
							$newcate[$data[$j]['class']]=1;
							if(isset($data[$j]['class']))
							{
								if(!isset($tempcate[$data[$j]['class']]))
								{
									$cdata['catename']=$data[$j]['class'];
									$tempcate[$data[$j]['class']]=$cate_mod->InsertData($cdata);
								}
								$data[$j]['class']=$tempcate[$data[$j]['class']];
							}
							
							$linedata[]='"'.implode('","',$data[$j]).'"';
						}
						$newcates="'".implode("','",array_keys($newcate))."'";
						$cate_mod->DeleteData("1 and catename not in (".$newcates.")");
						$fields='`'.implode('`,`',array_keys($data[0])).'`';
						
						$sql="replace into ".tname('plugin_coupon')."(".$fields.") values(".implode("),(",$linedata).");\n";
						$GLOBALS['db']->query($sql);
						
					}
				}
				sheader ( 'index.php?pluginid=coupon&con=admin&act=coupon', 3, '采集完成', 'redirect', true );
			}
			else
			{
				sheader ( 'index.php?pluginid=coupon&con=admin&act=coupon', 3, '网站不存在', 'redirect', true );
			}
		}
	}
	function couponattach_action()
	{
		$sites=$this->site;
		include ROOT_PATH.'/plugins/coupon/views/admin/couponattach.php';
		
	}
	function catemodify_action()
	{
		if(submitcheck('commit'))
		{
			$cate_mod=new common('plugin_coupon_cate');
			$data ['catename'] = trim ( strip_tags ( $_POST ['catename'] ) );
			if ($id = $cate_mod->InsertData ( $data )) {
				
				echo "<SCRIPT LANGUAGE='JavaScript'>var objdata={}; objdata.dotype='add';objdata.id='$id';objdata.catename='" . global_addslashes ( $data ['catename'], 1 ) . "';parent.parseMyCateData(objdata);</SCRIPT>";
			}
		}
		else
		{
			include ROOT_PATH.'/plugins/coupon/views/admin/cate_ajax_form.php';
		}
	}
}