<?php

class CategoryTable extends Doctrine_Table
{
   
    public static function getInstance()
    {
        return Doctrine_Core::getTable('Category');
    }
    public function getList()
    {
        $q = self::getInstance()->createQuery();
       
        
        $q->orderBy('sort_order ASC');
		$q->where('is_visible=1');
        
        return $q;
    }
    const ALL       = -1;

  const VISIBLE   = 1;

  const INVISIBLE = 0;



  public function getCatogoryReturnById($enname)

  {

    $q = Doctrine_Query::create()

            ->from('Category c')

            ->where('c.enname=?',$enname)

            ->fetchOne();

    return $q;

  }

  public function getCatogoriesQuery($parent_id = -1, $is_visible = -1)

  {

    $q = Doctrine_Query::create()

            ->from('Category c');
			
	if ($is_visible != -1)

    {

      $q->where('c.is_visible = ?', $is_visible);

    }
	
    if ($parent_id != -1)

    {

      $q->andWhere('c.parent_id = ?', $parent_id);

    }



    //is visible

  

    return $q;

  }

    public function getCategoryBackendOption($parent_id=0,$type=0) {
       
        $q = Doctrine_Query::create()
                ->from('Category c')
                ->orderBy('c.sort_order ASC');
			
            if($type!=-1)	
			      {   
  			        $q->where('c.is_visible=?',$type); 
				  }			
			if($parent_id!=-1)	
			      {   
  			        $q->where('c.parent_id=?',$parent_id); 
				  }
				
				
         return $q->execute();
		 
    }

    public function getCategoryBackend($parent_id=0) {
       
        $q = Doctrine_Query::create()
                ->from('Category c')
                ->where('c.is_visible=1')
                ->orderBy('c.sort_order ASC');
				
			if($parent_id!=-1)	
			      {   
  			        $q->andWhere('c.parent_id=?',$parent_id); 
				  }
				
				
         return $q->execute();
		 
    }
    public function getAllCategories($parent_id = 0) {
       
        $q = Doctrine_Query::create()
                ->from('Category c')
                ->orderBy('c.sort_order ASC');
        if($parent_id!=0){
            $q->andWhere('c.parent_id=?',$parent_id);
        }
         return $q->execute();
    }

public function getCategoryLegalOptions($parent_id = -1, $is_visible = -1)

  {

    $q = Doctrine_Query::create()->from('Category c');
	
		$q->where('c.is_visible=5')->andWhere('c.parent_id=0');		
		
		
		

		$q->orderBy('c.parent_id ASC, c.is_visible DESC, c.sort_order ASC');
	
    $results = $q->execute();
	
	
	 $option = array();
	
    foreach ($results as $result)
    {
    
       $option[$result->getId()] = $result;
	   
	 
	   
	   $shine = Doctrine_Query::create()->from('Category a')->where('a.parent_id=?',$result->getId())->execute();
   
       
   
		 foreach ($shine as $sd)
			{
			
			   $option[$sd->getId()] = "--".$sd;
			   
			   
			   $sub = Doctrine_Query::create()->from('Category a')->where('a.parent_id=?',$sd->getId())->execute();
			   
			 
			 
			  foreach ($sub as $sd)
			{
			
			   $option[$sd->getId()] = "----".$sd;
			   
			}  
			 
			}
	}
	   return $option;
  }
 public function getCategoryData($parent_id = -1, $is_visible = -1)

  {

    $q = Doctrine_Query::create()->from('Category c');
	
		$q->where('c.is_visible=8')->andWhere('c.parent_id=0');		

		$q->orderBy('c.parent_id ASC, c.is_visible DESC, c.sort_order ASC');
	
    $results = $q->execute();
	
	
	 $option = array();
	
    foreach ($results as $result)
    {
    
       $option[$result->getId()] = $result;
	   
	 
	   
	   $shine = Doctrine_Query::create()->from('Category a')->where('a.parent_id=?',$result->getId())->execute();
   
       
   
		 foreach ($shine as $sd)
			{
			
			   $option[$sd->getId()] = "--".$sd;
			   
			   
			   $sub = Doctrine_Query::create()->from('Category a')->where('a.parent_id=?',$sd->getId())->execute();
			   
			 
			 
			  foreach ($sub as $sd)
			{
			
			   $option[$sd->getId()] = "----".$sd;
			   
			}  
			 
			}

   
    }
	
	
   
	
    return $option;
	
  }
public function getCategoryTailan($parent_id = -1, $is_visible = -1)

  {

    $q = Doctrine_Query::create()->from('Category c');
	
		$q->where('c.is_visible=10')->andWhere('c.parent_id=0');		

		$q->orderBy('c.parent_id ASC, c.is_visible DESC, c.sort_order ASC');
	
    $results = $q->execute();
	
	
	 $option = array();
	
    foreach ($results as $result)
    {
    
       $option[$result->getId()] = $result;
	   
	 
	   
	   $shine = Doctrine_Query::create()->from('Category a')->where('a.parent_id=?',$result->getId())->execute();
   
       
   
		 foreach ($shine as $sd)
			{
			
			   $option[$sd->getId()] = "--".$sd;
			   
			   
			   $sub = Doctrine_Query::create()->from('Category a')->where('a.parent_id=?',$sd->getId())->execute();
			   
			 
			 
			  foreach ($sub as $sd)
			{
			
			   $option[$sd->getId()] = "----".$sd;
			   
			}  
			 
			}

   
    }
	
	
   
	
    return $option;
	
  }  
 public function getCategoryJuram($parent_id = -1, $is_visible = -1)

  {

    $q = Doctrine_Query::create()->from('Category c');
	
		$q->where('c.is_visible=6')->andWhere('c.parent_id=0');		

		$q->orderBy('c.parent_id ASC, c.is_visible DESC, c.sort_order ASC');
	
    $results = $q->execute();
	
	
	 $option = array();
	
    foreach ($results as $result)
    {
    
       $option[$result->getId()] = $result;
	   
	 
	   
	   $shine = Doctrine_Query::create()->from('Category a')->where('a.parent_id=?',$result->getId())->execute();
   
       
   
		 foreach ($shine as $sd)
			{
			
			   $option[$sd->getId()] = "--".$sd;
			   
			   
			   $sub = Doctrine_Query::create()->from('Category a')->where('a.parent_id=?',$sd->getId())->execute();
			   
			 
			 
			  foreach ($sub as $sd)
			{
			
			   $option[$sd->getId()] = "----".$sd;
			   
			}  
			 
			}

   
    }
	
	
   
	
    return $option;
	
  }
  
  public function getCategoryOptions($parent_id = -1, $is_visible = -1)

  {

    $q = $this->getCatogoriesQuery($parent_id, $is_visible);

	//$q->andWhere('c.is_visible<>?',5);
	
    $q->orderBy('c.parent_id ASC, c.is_visible DESC, c.sort_order ASC');


    $results = $q->execute();



    $parents = array();

    $childs  = array();

    foreach ($results as $result)

    {

      $parent_id = $result->getParentId();

      if ($parent_id > 0)

      {

        $childs[$parent_id][] = $result;

      }

      else

      {

        $parents[] = $result;

      }

    }

    $option = array();

    foreach($parents as $parent)

    {

      if ($parent->getIsVisible())

      {

        $option[$parent->getId()] = $parent->getName();

      }

      else

      {

        $option[$parent->getId()] = '*'. $parent->getName();

      }

      if (isset($childs[$parent->getId()]))

      {

        foreach($childs[$parent->getId()] as $child)

        {

          $option[$child->getId()] = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--'.$child->getName();

        }

      }

    }

    return $option;

  }

 public function getCategoriesBack($parent_id = -1, $is_visible = -1)

  {

    $q = $this->getCatogoriesQuery($parent_id, $is_visible);

    $q->orderBy('c.parent_id ASC, c.is_visible DESC, c.sort_order ASC');

    return $q->execute();

  }

  public function getCategories($parent_id = -1, $is_visible = -1)

  {

    $q = $this->getCatogoriesQuery($parent_id, $is_visible);

    $q->orderBy('c.parent_id ASC, c.is_visible DESC, c.sort_order ASC');

    return $q->execute();

  }

  public function getCategoryOne($parent_id = -1, $is_visible = -1)

  {

    $q = $this->getCatogoriesQuery($parent_id, $is_visible);

    $q->orderBy('c.parent_id ASC, c.is_visible DESC, c.sort_order ASC');

    return $q->fetchOne();

  }

  public function getVisibleCategories($parent_id = -1)

  {

    return $this->getCategories($parent_id, CategoryTable::VISIBLE);

  }





  public function retrieveCategory(Doctrine_Query $q)

  {

    //$alias = $q->getRootAlias();

    //$q->andWhere("{$alias}.is_visible=1");

    return $q->fetchOne();

  }

  public function getParentCategories()

  {

    return $this->getCategories(0, CategoryTable::ALL);

  }



  public function getParentOptions()

  {

    return $this->getCategoryOptions(0, -1);

  }



  public function getInvisibleParents()

  {

    return $this->getCategories(0, 0);

  }
  public function getChildOneCategory($parent_id){

	return $this->getCategoryOne($parent_id);
  }

  public function getChildCategories($parent_id)

  {

    return $this->getCategories($parent_id);

  }
  
  
   public function getCategoriesOptions($parent_id = 0) {
       
        $q = Doctrine_Query::create()
                ->from('Category c')
                ->where('c.parent_id=0')
                ->orderBy('c.sort_order ASC')->fetchArray();
				
				
				
        $option = array();
		$option[0] = "========================";
		
		
		 foreach($q as $fq){
			 
			 $option[$fq['id']] = $fq['name'];	 
			 
			 $dl = Doctrine_Query::create()
					->from('Category c')
					->where('c.parent_id=?',$fq['id'])					
					->orderBy('c.sort_order ASC')->fetchArray();	 
					
			 foreach($dl as $mi){
				 
				$option[$mi['id']] = ' -- '.$mi['name'];	
				
				$li = Doctrine_Query::create()
					->from('Category c')
					->where('c.parent_id=?',$mi['id'])					
					->orderBy('c.sort_order ASC')->fetchArray();
				
				foreach($li as $tse){
					$option[$tse['id']] = ' ---- '.$tse['name'];	
				}
				
			 }
		  
			 
		 }
		
		 
		 return $option;
		 
		 
		 
		 
    }
  
}