<?php
class Admin_WeblinkController extends Zend_Controller_Action 
{
	public function init()
	{
		Zend_Loader::loadClass('Weblink');
		$this->smarty=Zend_Registry::get('smarty');
		$this->config=Zend_Registry::get('config');
		$this->smarty->assign('baseUrl',$this->_request->getBaseUrl());
	}
	
	
	public function indexAction()
	{
			$weblink=new Weblink();
			$page=$this->_request->getParam('page');
			//if('0'>=$page) $page=1;
			$condition=NULL;
			$sortby='id DESC';
			Zend_Loader::loadClass('Custom_Controller_Plugin_Pager');
			$basePageIndex=0;
			$Pagers = new Custom_Controller_Plugin_Pager($weblink,$page,'10',$condition,$sortby,$basePageIndex) ;
			
			$pageInfo = $Pagers->getNavBar(WEB_ROOT.'admin/weblink/index/');
			$this->smarty->weblinks = $Pagers->findAll();
	    	$this->smarty->assign('arrArticles',$this->smarty->weblinks->toArray());
	    	
	    	$this->smarty->assign("pageInfo",$pageInfo);
             //显示模版
			$this->smarty->display('link/index.phtml');
             
	}
	
	public function addAction()
	{	
		if(strtolower($_SERVER['REQUEST_METHOD'])=='post'){
			Zend_Loader::loadClass('Zend_Filter_StripTags');
			$filter=new Zend_Filter_StripTags();
			$title=$filter->filter(($this->_request->getPost('title')));
			$content=$filter->filter(($this->_request->getPost('content')));
			$url=$this->_request->getPost('url');
			$show=$filter->filter(($this->_request->getPost('show')));
			$email=$this->_request->getPost('email');
			$imgurl=$filter->filter(($this->_request->getPost('imageUrl')));
			$id=$filter->filter(($this->_request->getPost('id')));
			$dateFormat =$this->config->Format->dateFormat;
			$urls=$this->_request->getPost('filenames');
			$attachdesc=$this->_request->getPost('attachdesc');
			$imgname=$this->_request->getPost('imgname');
			$attachment=new Attachment();
			$weblink=new Weblink();
			if($url!=''&&$title!=''){
				$db = $weblink->getAdapter();
				if(''==$id){
					$data=array(
					'title'=>$title,
					'content'=>$content,
					'url'=>$url,
					'show'=>$show,
					'email'=>$email,
					
					'imgurl'=>$imgurl,
					'addtime'=>date($dateFormat),
					'updatetime'=>date($dateFormat)
					);
					$weblink->insert($data);
					$typeid = $db->lastInsertId();
					unset($data);
					$messages="您的友情链接增加成功!";
				}else{
					$set=array(
					'title'=>$title,
					'content'=>$content,
					'url'=>$url,
					'show'=>$show,
					'email'=>$email,
					'imageUrl'=>$imageUrl,
					'updatetime'=>date($dateFormat)
					);
					$where = $db->quoteInto('id = ?', $id);
					//更新表数据,返回更新的行数
					$typeid=$id;
					$rows=$weblink->update($set, $where);
					unset($set);
					$messages="您友情链接编辑成功!";
				}
			
				if(0!=count($urls))
				{
					//增加文件
					$num=count(array_filter($urls));
					for ($i = 0; $i < $num; $i++) {
						$data=array(
						'typeid'=>$typeid,
						'type'=>'article',
						'url'=>$urls[$i],
						'attachdesc'=>$attachdesc[$i],
						'imgname'=>$imgname[$i],
						'downs'=>'0',
						'addtime'=>date($dateFormat)
						);
						$attachment->insert($data);
					
					}
				}
				
				Zend_Loader::loadClass('Custom_Controller_Plugin_Message');
				$Message = new Custom_Controller_Plugin_Message();
				$this->smarty->display('header.phtml');
				$Message->showMsg('<font color=\"red\">'.$messages.'</font>',WEB_ROOT.'admin/weblink/index/');
				$this->smarty->display('footer.phtml');	
			}
		}else{
			$this->smarty->display('link/add.phtml');
		}
		
	}

	public function editAction()
	{	
		Zend_Loader::loadClass('Zend_Filter_StripTags');
		$filter=new Zend_Filter_StripTags();
		$weblink=new Weblink();
		$id=$filter->filter(($this->_request->getParam('id')));
		$this->smarty->article = $weblink->fetchAll('id='.$id);
		$Articleone=$this->smarty->article->toArray();
		$attachment=new Attachment();
		$sortby='id DESC';
		$this->smarty->attachs = $attachment->fetchAll('typeid='.$id,$sortby);
		$attachs=$this->smarty->attachs->toArray();
		//print_r($attachs);
		$this->smarty->assign('Articleone',$Articleone);
		$this->smarty->assign('attachs',$attachs);
		$this->smarty->display('link/edit.phtml');
	}
	
	
	public function delAction()
	{		
		$weblink=new Weblink();
		$attachment=new Attachment();
		if (strtolower($_SERVER['REQUEST_METHOD'])=='post') {
            $id =$this->_request->getPost('id');
            
            $StringId = join ( ",", $id );
            $where = 'id IN ('.$StringId.')';
            $weblink->delete($where);
            
			$num=count(array_filter($id));
			for ($i = 0; $i < $num; $i++) {
				$this->smarty->attach = $attachment->fetchAll('typeid='.$id[$i]);
				$Articleone=$this->smarty->attach->toArray();
				//print_r($Articleone);
				$nums=count(array_filter($Articleone));
				for ($j = 0; $j < $nums; $j++) {
					$imgurl=ROOT_DIR.'/blog/public/upload/'.$Articleone[$j]['imgname'];
					@unlink($imgurl);
				}
			}
			$where = 'typeid IN ('.$StringId.')';
           	$attachment->delete($where);
        } else {
            $id = (int)$this->_request->getParam('id');
        	if ($id > 0) {
                $where = 'id = ' . $id;
                $weblink->delete($where);
            }
            $this->smarty->attach = $attachment->fetchAll('typeid='.$id);
			$Articleone=$this->smarty->attach->toArray();
        	$nums=count(array_filter($Articleone));
			for ($j = 0; $j < $nums; $j++) {
					$imgurl=ROOT_DIR.'/blog/public/upload/'.$Articleone[$j]['imgname'];
					@unlink($imgurl);
			}
			$where = 'typeid ='.$id;
           	$attachment->delete($where);
        }
		Zend_Loader::loadClass('Custom_Controller_Plugin_Message');
		$Message = new Custom_Controller_Plugin_Message();
		$this->smarty->display('header.phtml');
		$Message->showMsg('<font color=\"red\">您友情链接删除成功!</font>',WEB_ROOT.'admin/weblink/index/');
		$this->smarty->display('footer.phtml');
	}
	
	
}
