<?php

/**
 * NewsTable
 * 
 * This class has been auto-generated by the Doctrine ORM Framework
 */
class NewsTable extends Doctrine_Table
{
 const SIMPLE_NEWS  = 0;
  const PHOTO_NEWS   = 1;
  const VIDEO_NEWS   = 2;


  public static $_types = array(

          0 => 'Мэдээ',

          1 => 'Ярилцлага',

          2 => 'Видео мэдээ',

          3 => 'Энтертэйнмэнт мэдээ',

          4 => 'Содон мэдээ',
		  
		  5 => 'Шуурхай мэдээ',
		  
		  6 => 'Ярилцлага'
          );
  public static function getTypes()

  {

    return self::$_types;

  }
    public static function getInstance()
    {
        return Doctrine_Core::getTable('News');
    }
 public function getAllListQueryNew($category_id = 0, $limit = 0, $order = 0, $NoCatID = 0)
    {
        $q = Doctrine_Query::create()->from('News n');
        // $q->leftJoin('n.NewsStat s');

        if($category_id){
		
		    $q->innerJoin('n.NewsCategory nc');
            $q->where('nc.category_id = ?', $category_id);
		}
       if($NoCatID){
		
		    $q->innerJoin('n.NewsCategory nc');
            $q->where('nc.category_id <> ?', $NoCatID);
		}        
		
        if($order==1){
            $q->orderBy('s.nb_view Desc');
        }else{
            $q->orderBy('created_at Desc');
        }

        if($limit){
            $q->limit($limit);
        }
        
        return $q;
    }
 public function getAllListQuery($category_id = 0, $limit = 0, $order = 0)
    {
        $q = Doctrine_Query::create()->from('News n');
        // $q->leftJoin('n.NewsStat s');

        if($category_id){
		
		    $q->innerJoin('n.NewsCategory nc');
            $q->where('nc.category_id = ?', $category_id);
		}
          
        if($order==1){
            $q->orderBy('s.nb_view Desc');
        }else{
            $q->orderBy('created_at Desc');
        }

        if($limit){
            $q->limit($limit);
        }
        
        return $q;
    }
	
	 public function getListSudalgaa($org_id = 0, $category_id = 0, $type = 0, $limit = 0)
    {
        $q = Doctrine_Query::create()
                    ->from('News n')->innerJoin('n.NewsCategory nc')->where('n.is_top = ?', $type)->andWhere('n.is_published =?', 1)->orderBy('n.published_at DESC');
        if($category_id)
            $q->andWhere('nc.category_id = ?', $category_id);


        return $q;
    }
	 public function getListQuery($org_id = 0, $category_id = 0, $type = 0, $limit = 0)
    {
        $q = Doctrine_Query::create()
                    ->from('News n')->orderBy('n.published_at DESC');
        if($category_id)
            $q->where('n.category_id = ?', $category_id);

        if($org_id)
            $q->addWhere('n.org_id = ?', $org_id);
		
		if($type)
            $q->andWhere('n.type = ?', $type);

        if($limit){
            $q->limit($limit);
        }
        
		
	   $q->andWhere('n.is_published =?', 1);
		
        return $q;
    }

        public function getMlist($org_id = 0, $category_id = 0, $limit = 0)
    {
        return $this->getListQuery($org_id, $category_id, $limit);
    }


     	public function getOList($org_id = 0, $category_id = 0, $limit = 0)
    {
        return $this->getListQuery($org_id, 0, $limit)->andWhereNotIn('n.org_id', 4);
    }


    	public function getResult($string, $org_id = 0, $category_id = 0, $limit = 0)
    {
        return $this->getListQuery($category_id, $org_id, $limit)->andWhere('n.title LIKE ?','%'.$string.'%');
    }


}