<?php
class Admin_ClassController extends Zend_Controller_Action 
{	
	public $fenleiString;
	public function init()
	{	Zend_Loader::loadClass('Articleclass');
		$this->smarty=Zend_Registry::get('smarty');
		$this->config=Zend_Registry::get('config');
		$this->smarty->assign('baseUrl',$this->_request->getBaseUrl());
	}
	
	public function indexAction()
	{	
		$class=new Articleclass();
		$this->smarty->classpid= $class->fetchAll('pid=0','id ASC');
		$classArray=$this->smarty->classpid->toArray();
		$this->smarty->assign('classArray',$classArray);
		$this->smarty->display('class/index.phtml');
		
	}
	
	public function addAction()
	{	
		$class=new Articleclass();
		
		if(strtolower($_SERVER['REQUEST_METHOD'])=='post'){
			Zend_Loader::loadClass('Zend_Filter_StripTags');
			$filter=new Zend_Filter_StripTags();
			$name=$filter->filter(($this->_request->getPost('name')));
			$type =$filter->filter(($this->_request->getPost('type')));
			$pid=$filter->filter(($this->_request->getPost('sunid')));
			$id=$filter->filter(($this->_request->getPost('id')));
			
			if($name!=''&&$pid!=''){
				$db = $class->getAdapter();
				if('edit'==$type){
					$data=array(
					'pid'=>$pid,
					'name'=>$name
					);
					$where = $db->quoteInto('id = ?', $id);
	
					$class->update($data, $where);
					$messages="您编辑分类成功!";
				}else{
					$data=array(
					'pid'=>$pid,
					'name'=>$name
					);
					$class->insert($data);
					
					$messages="您增加分类成功!";
					
				}
				unset($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/class/index/');
				$this->smarty->display('footer.phtml');
			}
		}else{
				$type =$this->_request->getParam('type');
				$id =(int)$this->_request->getParam('id');
				$pid =(int)$this->_request->getParam('pid');
				$name=$this->_request->getParam('name');
				$this->smarty->assign('type',$type);
				$this->smarty->assign('name',$name);
				if($id!=0){
					$this->smarty->assign('fenleiString',$this->articleclassAction($id));
					$this->smarty->assign('id',$id);
					$this->smarty->assign('pid',$pid);
					
				}else{
					$this->smarty->articleclass = $class->fetchAll('pid=0');
					$classAll=$this->smarty->articleclass->toArray();
					$this->smarty->assign('classAll',$classAll);
					
				}
				
				if($type=='edit'){
					$this->smarty->display('class/edit.phtml');
				}else{
					$this->smarty->display('class/add.phtml');
				}
			
		}
		
      	
	}
	
	public function articleclassAction($id)
	{	
		$class=new Articleclass();
		$this->smarty->articlepid= $class->fetchAll('id='.$id);
		$classpid=$this->smarty->articlepid->toArray();
		$thisString='';
		$pid=$classpid[0]['pid'];
		$this->smarty->articleclass = $class->fetchAll('pid='.$pid);
		$thisArray=$this->smarty->articleclass->toArray();
		//if(0==$pid){$innerid=1;}else{$innerid=$pid;}
		if(0==$pid){
			$classid=1;
		}else{
			$classid=$pid;
		}
		$thisString.='<select id="class'.$classid.'" name="class'.$classid.'" onchange="ChangeClass('.$classid.')">';
		for($i=0;$i<count($thisArray);$i++){
			if($thisArray[$i]['id']==$id){
				$thisString.='<option selected value="'.$thisArray[$i]['id'].'">'.$thisArray[$i]['name'].'</option>';
			}
			else{
				$thisString.='<option value="'.$thisArray[$i]['id'].'">'.$thisArray[$i]['name'].'</option>';
			}
		}
		$thisString.='</select>';
		if(0==$pid){
			$thisString.='<span id="inner1">';
		}else{
			$thisString.='<span id="inner'.$pid.'">';
		}
		$this->fenleiString=$thisString.$this->fenleiString;
		
		if($pid!=0){
		$this->articleclassAction($pid);
		
		}
		return $class=$this->fenleiString;
		
	}	
	
	
	public function ajaxAction()
	{	$class=new Articleclass();
		$id =(int)$this->_request->getPost('id');
		$this->smarty->articleclass = $class->fetchAll('pid='.$id);
		$classAll=$this->smarty->articleclass->toArray();
		$num=count(array_filter($classAll));
		$this->smarty->assign('classAll',$classAll);
		$this->smarty->assign('id',$id);
		$this->smarty->assign('num',$num);
		if($num>0){$sunid=$classAll[0]['id'];}else{$sunid=$id;}
		$this->smarty->assign('sunid',$sunid);
		$this->smarty->display('class/ajax.phtml');
		
	}
	
	public function delAction()
	{	
		$class=new Articleclass();
		$id = (int)$this->_request->getParam('id');
        if ($id > 0) {
                $where = 'id = ' . $id;
                $class->delete($where);
                $where = 'pid = ' . $id;
                $class->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/class/index/');
		$this->smarty->display('footer.phtml');
	}
	
	public function delclassAction()
	{
		if ($this->db->query ( "SELECT id FROM " . B_PRE . "{$type}fls WHERE pid='$flid'" )) {
			
			$fenleiIdArray = $this->db->getOne ();
			
			$this->db->deleteSet ( B_PRE . "{$type}fls", "flid", array ($flid ) );
			
			if (count ( $fenleiIdArray ) > 0) {
				
				foreach ( $fenleiIdArray as $thisFlid ) {
					
					$this->db->deleteSet ( B_PRE . "{$type}fls", "flid", array ($thisFlid ) );
					
					$this->delFenlei ( $type, $thisFlid );
					
					return true;
				
				}
			
			} else {
				
				return true;
			}
		}
	}
	public function classoneAction()
	{	$class=new Articleclass();
		$id =(int)$this->_request->getPost('id');
		$this->smarty->articleclass = $class->fetchAll('pid='.$id);
		$classArray=$this->smarty->articleclass->toArray();
		$num=count(array_filter($classArray));
		$this->smarty->assign('classArray',$classArray);
		$this->smarty->assign('id',$id);
		$this->smarty->assign('num',$num);
		if($num>0){$sunid=$classArray[0]['id'];}else{$sunid=$id;}
		$this->smarty->assign('sunid',$sunid);
		$this->smarty->display('class/classone.phtml');
		
	}

	
	
	
	
	
	
	
	
	
	
	
	
}