<?php
class Admin_PhotoController extends Zend_Controller_Action 
{
	public function init()
	{
		Zend_Loader::loadClass('Photo');
		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()
	{
			
			$photo=new Photo();
			$db = $photo->getAdapter();
			/*
			$select = $db->select();
			$select->from('oophp_photo', '*')
				   	->join('oophp_album', 'oophp_album.id = oophp_photo.albumid', 'oophp_album.name as albumname');
       		echo $sql = $select->__toString();
			//$articles=$db->fetchAll($select);	
       		*/
       		$query="SELECT `".TABLE_PRE."photo`.*, `".TABLE_PRE."album`.`name` AS `albumname` FROM `".TABLE_PRE."photo` INNER JOIN `".TABLE_PRE."album` ON ".TABLE_PRE."album.id = `".TABLE_PRE."photo`.albumid ";
			$page=$this->_request->getParam('page');
			$condition=NULL;
			$sortby="`".TABLE_PRE."photo`.id DESC";
			Zend_Loader::loadClass('Custom_Controller_Plugin_Pager');
			$basePageIndex=0;
			$Pagers = new Custom_Controller_Plugin_Pager($db,$page,'5',$condition,$sortby,$basePageIndex,$query) ;
			
			$pageInfo = $Pagers->getNavBar(WEB_ROOT.'admin/photo/index/');
			$this->smarty->photos = $Pagers->findAll();
	    	$this->smarty->assign('arrPhoto',$this->smarty->photos);
	    	$this->smarty->assign("pageInfo",$pageInfo);
             //显示模版
			$this->smarty->display('photo/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();
			$title=$filter->filter(($this->_request->getPost('title')));
			$author=$filter->filter(($this->_request->getPost('author')));
			$content=$filter->filter(($this->_request->getPost('content')));
			$albumid=$filter->filter(($this->_request->getPost('albumid')));
			$dateFormat =$this->config->Format->dateFormat;
			$imgname=$this->_request->getPost('imgname');
			$imgurl=$this->_request->getPost('filenames');
			$photo=new Photo();
			if($title!=''){
				$db = $photo->getAdapter();
					$data=array(
					'title'=>$title,
					'content'=>$content,
					'author'=>$author,
					'albumid'=>$albumid,
					'viewnum'=>'0',
					'imgname'=>$imgname,
					'imgurl'=>$imgurl,
					'addtime'=>date($dateFormat),
					'updatetime'=>date($dateFormat)
					);
					$photo->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/photo/index/');
				$this->smarty->display('footer.phtml');
				//$this->_redirect('admin/article/addarticle');
				
			}
		}else{
			$album=new Album();
			$this->smarty->albums = $album->fetchAll();
			$albumAll=$this->smarty->albums->toArray();
			$this->smarty->assign('albumAll',$albumAll);
	      	$this->smarty->display('photo/add.phtml');
		}
		
	}
	
	
	
	public function editAction()
	{	
		$photo=new Photo();
		Zend_Loader::loadClass('Zend_Filter_StripTags');
		$filter=new Zend_Filter_StripTags();
			
		if(strtolower($_SERVER['REQUEST_METHOD'])=='post'){
			
			$title=$filter->filter(($this->_request->getPost('title')));
			$author=$filter->filter(($this->_request->getPost('author')));
			$content=$filter->filter(($this->_request->getPost('content')));
			$viewnum=$filter->filter(($this->_request->getPost('viewnum')));
			$albumid=$filter->filter(($this->_request->getPost('albumid')));
			$dateFormat =$this->config->Format->dateFormat;
			$imgname=$this->_request->getPost('imgname');
			$imgurl=$this->_request->getPost('filenames');
			$id=$filter->filter(($this->_request->getPost('id')));
			
			$db = $photo->getAdapter();
			$set=array(
				'title'=>$title,
				'content'=>$content,
				'author'=>$author,
				'albumid'=>$albumid,
				'viewnum'=>$viewnum,
				'imgname'=>$imgname,
				'imgurl'=>$imgurl,
				'updatetime'=>date($dateFormat)
			);
			$where = $db->quoteInto('id = ?', $id);
			//更新表数据,返回更新的行数
			$photo->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/photo/index/');
			$this->smarty->display('footer.phtml');
		}else{
			$id=$filter->filter(($this->_request->getParam('id')));
			$this->smarty->photos = $photo->fetchAll('id='.$id);
			$PhotoOne=$this->smarty->photos->toArray();
			//print_r($Albumone);
			$album=new Album();
			$this->smarty->albums = $album->fetchAll();
			$albumAll=$this->smarty->albums->toArray();
			$this->smarty->assign('albumAll',$albumAll);
			$this->smarty->assign('PhotoOne',$PhotoOne);
			$this->smarty->display('photo/edit.phtml');
		}
	}
	
	
	
	public function delAction()
	{		
		$photo=new Photo();
		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->photos = $photo->fetchAll('id='.$id[$i]);
				$Albumone=$this->smarty->photos->toArray();
				//print_r($Albumone);
				$imgurl=ROOT_DIR.'/blog/public/upload/photo/'.$Albumone[0]['imgname'];
				@unlink($imgurl);
				
			}
			$where = 'id IN ('.$StringId.')';
            $photo->delete($where);
			
        } else {
            $id = (int)$this->_request->getParam('id');
        	if ($id > 0) {
        		 $this->smarty->photos = $photo->fetchAll('id='.$id);
				$Albumone=$this->smarty->photos->toArray();
				//print_r($Albumone);
				$imgurl=ROOT_DIR.'/blog/public/upload/photo/'.$Albumone[0]['imgname'];
				@unlink($imgurl);
                $where = 'id = ' . $id;
                $photo->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/photo/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/'.$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/photo/'.$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/photo/','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/photo/'.$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>";
		}
		
	}
}