<?php
class Admin_AlbumController extends Zend_Controller_Action 
{
	public function init()
	{
		Zend_Loader::loadClass('Album');
		$this->smarty=Zend_Registry::get('smarty');
		$this->config=Zend_Registry::get('config');
		$this->smarty->assign('baseUrl',$this->_request->getBaseUrl());
	}
	
	public function indexAction()
	{
			$album=new Album();
			$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($album,$page,'5',$condition,$sortby,$basePageIndex) ;
			
			$pageInfo = $Pagers->getNavBar(WEB_ROOT.'admin/album/index/');
			$this->smarty->albums = $Pagers->findAll();
	    	$this->smarty->assign('arrAlbums',$this->smarty->albums->toArray());
	    	$this->smarty->assign("pageInfo",$pageInfo);
             //显示模版
			$this->smarty->display('album/index.phtml');
             
	}
	public function searchAction()
	{
		$this->smarty->display('article/search.phtml');
	}
	
	
	public function addAction()
	{	
		if(strtolower($_SERVER['REQUEST_METHOD'])=='post'){
			Zend_Loader::loadClass('Zend_Filter_StripTags');
			$filter=new Zend_Filter_StripTags();
			$name=$filter->filter(($this->_request->getPost('name')));
			$content=$filter->filter(($this->_request->getPost('content')));
			$password=$filter->filter(($this->_request->getPost('password')));
			$show=$filter->filter(($this->_request->getPost('show')));
			$dateFormat =$this->config->Format->dateFormat;
			$imgname=$this->_request->getPost('imgname');
			$imgurl=$this->_request->getPost('filenames');
		
			$album=new Album();
			if($name!=''){
				$db = $album->getAdapter();
				$data=array(
				'name'=>$name,
				'content'=>$content,
				'password'=>md5($password),
				'show'=>$show,
				'imgname'=>$imgname,
				'imgurl'=>$imgurl,
				'addtime'=>date($dateFormat),
				'updatetime'=>date($dateFormat)
				);
				$album->insert($data);
				$typeid = $db->lastInsertId();
				unset($data);
				$messages="您增加相册成功!";
				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/album/index/');
				$this->smarty->display('footer.phtml');
				//$this->_redirect('admin/article/addarticle');
				
			}
		}else{
	      	$this->smarty->display('album/add.phtml');
		}
		
	}
	
	
	public function editAction()
	{	
		$album=new Album();
		Zend_Loader::loadClass('Zend_Filter_StripTags');
		$filter=new Zend_Filter_StripTags();
			
		if(strtolower($_SERVER['REQUEST_METHOD'])=='post'){
			$name=$filter->filter(($this->_request->getPost('name')));
			$password=$filter->filter(($this->_request->getPost('password')));
			$show=$filter->filter(($this->_request->getPost('show')));
			$content=$filter->filter(($this->_request->getPost('content')));
			$dateFormat =$this->config->Format->dateFormat;
			$imgname=$this->_request->getPost('imgname');
			$id=$filter->filter(($this->_request->getPost('id')));
			$imgurl=$this->_request->getPost('filenames');
			$db = $album->getAdapter();
			$set=array(
			'name'=>$name,
			'content'=>$content,
			'password'=>md5($password),
			'show'=>$show,
			'imgname'=>$imgname,
			'imgurl'=>$imgurl,
			'updatetime'=>date($dateFormat)
			);
			$where = $db->quoteInto('id = ?', $id);
			//更新表数据,返回更新的行数
			$album->update($set, $where);
			unset($set);
			$messages="您编辑相册成功!";
			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/album/index/');
			$this->smarty->display('footer.phtml');
		}else{
			$id=$filter->filter(($this->_request->getParam('id')));
			$this->smarty->albums = $album->fetchAll('id='.$id);
			$Albumone=$this->smarty->albums->toArray();
			//print_r($Albumone);
			$this->smarty->assign('Albumone',$Albumone);
			$this->smarty->display('album/edit.phtml');
		}
	}
	
	
	
	public function delAction()
	{		
		$album=new Album();
		if (strtolower($_SERVER['REQUEST_METHOD'])=='post') {
            $id =$this->_request->getPost('id');
            //print_r($id);
            $StringId = join ( ",", $id );
			$num=count(array_filter($id));
			for ($i = 0; $i < $num; $i++) {
				$this->smarty->albums = $album->fetchAll('id='.$id[$i]);
				$Albumone=$this->smarty->albums->toArray();
				//print_r($Albumone);
				$imgurl=ROOT_DIR.'/blog/public/upload/album/'.$Albumone[0]['imgname'];
				@unlink($imgurl);
				
			}
			$where = 'id IN ('.$StringId.')';
            $album->delete($where);
			
        } else {
            $id = (int)$this->_request->getParam('id');
        	if ($id > 0) {
        		 $this->smarty->albums = $album->fetchAll('id='.$id);
				$Albumone=$this->smarty->albums->toArray();
				//print_r($Albumone);
				$imgurl=ROOT_DIR.'/blog/public/upload/album/'.$Albumone[0]['imgname'];
				@unlink($imgurl);
                $where = 'id = ' . $id;
                $album->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/album/index/');
		$this->smarty->display('footer.phtml');
	}
	
	public function delattachAction()
	{		
		$attachment=new Attachment();
		if (strtolower($_SERVER['REQUEST_METHOD'])=='post') {
            $id =$this->_request->getPost('id');
            $typeid =$this->_request->getPost('typeid');
            $imgurl=ROOT_DIR.'/blog/public/upload/album/'.$this->_request->getPost('imgname');
			@unlink($imgurl);
			if ($id > 0) {
                $where = 'id = ' . $id;
                $attachment->delete($where);
            }
        }else {
            $id = (int)$this->_request->getParam('id');
        	if ($id > 0) {
                $where = 'id = ' . $id;
                $attachment->delete($where);
            }
        }
        $sortby='id DESC';
		$this->smarty->attachs = $attachment->fetchAll('typeid='.$typeid,$sortby);
		$attachs=$this->smarty->attachs->toArray();
		//print_r($attachs);
		$this->smarty->assign('attachs',$attachs);
		$this->smarty->display('article/attach.phtml');
		
	}

	
	
	public function delimageAction()
	{	$imgurl=ROOT_DIR.'/blog/public/upload/album/'.$this->_request->getPost('url');
		@unlink($imgurl);
	}
	
	
	public function uploadAction()
	{	
		Zend_Loader::loadClass('Custom_Controller_Plugin_Upload');
    	$uploadfile=new Custom_Controller_Plugin_Upload(ROOT_DIR.'/blog/public/upload/album/','uploadfile','10','2048000',array("gif","png","jpg","bmp","jpeg"),600,600);
		
		//$num = count ( array_filter ( $uploadfile->Error));
    	if(''==$uploadfile->Error){
			$imgUrl=WEB_ROOT.'public/upload/album/'.$uploadfile->lastFileName;
			echo "<script language=\"javascript\" type=\"text/javascript\">
			//parent.alert('上传成功！请返回！');
			parent.document.getElementById(\"ImgInputSpan\").innerHTML='<a onclick=delImages(\"thisHeadImg\",\"ImgInputSpan\",\"fileupload1\",\"$uploadfile->lastFileName\");  href=\"#\" >删除</a>';
			parent.document.getElementById(\"fileupload1\").innerHTML='<input name=\"filenames\" value=\"$imgUrl\" type=\"hidden\"  /><input name=\"imgname\" value=\"$uploadfile->lastFileName\" type=\"hidden\"  />';
			parent.document.getElementById(\"thisHeadImg\").src=\"$imgUrl\";
			</script>";
			
		}else{
			echo "<script language=\"javascript\" type=\"text/javascript\">
			parent.alert('".$uploadfile->Error."');
			</script>";
		}
		
	}
}