<?php

/**
 * Description of Table
 *
 * @author arson
 */
class Eppe_Db_Table extends Zend_Db_Table {

	/**
	 * @return Eppe_Db_Table
	 */
	final static function _new() {
		return new static();
	}

	protected $_rowsetClass = 'Eppe_Db_Table_Rowset_Abstract';
	protected $_rowClass = 'Eppe_Db_Table_Row_Abstract';

	/**
	 * @param type $withFromPart
	 * @return \Eppe_Db_Table_Select 
	 */
	public function select($withFromPart = self::SELECT_WITHOUT_FROM_PART) {
		$select = new Eppe_Db_Table_Select($this);
		if ($withFromPart == self::SELECT_WITH_FROM_PART) {
			$select->from($this->info(self::NAME), Eppe_Db_Table_Select::SQL_WILDCARD, $this->info(self::SCHEMA));
		}
		return $select;
	}

	/**
	 * Zwraca row
	 * 
	 * @param int $id
	 * @return Eppe_Db_Table_Row_Abstract
	 */
	final static public function getRow($id, $lang = null) {
		$model = new static();
		if ($lang !== null) {
			return $model->find($id, $lang)->current();
		} else {
			return $model->find($id)->current();
		}
	}
	
	
	public function getRowByColumn($column, $value) {
		$model = new static();
		$select = $this->select()->where($column . ' = ?', $value);
		return $model->fetchRow($select);
	}

	/**
	 * Tworzy select który bierze pod uwagę tylko elementy z flaga active ustawiona na true
	 * @return Zend_Db_Table_Select
	 */
	public function selectActive($select = null) {
		if ($select == null)
			$select = $this->select();
		return $select->where('active = 1')->order('order DESC');
	}

	/**
	 * Pobiera z bazy tylko elementy z flaga active ustawiona na true
	 * @return Zend_Db_Table_Rowset 
	 */
	public function fetchActive($select = null) {
		$select = $this->selectActive($select);
		return $this->fetchAll($select);
	}

	public function fetchActiveByLangCode($langCode, $select = null) {
		if ($select == null)
			$select = $this->select();
		$select->where('active = 1')->order('order DESC');

		return $this->fetchByLangCode($langCode, $select);
	}

	public function fetchByLangCode($langCode, $select = null) {
		if ($select == null)
			$select = $this->select();
		$select->where('lang_code = "' . $langCode . '"')->order('order DESC');
		return $this->fetchAll($select);
	}

	/**
	 *
	 * @param string $column
	 * @param int $id
	 * @param bool $active
	 * @param bool $in_order
	 * @return Zend_Db_Table_Rowset 
	 */
	public function fetchByParams($column = false, $id = null, $active = true, $in_order = true) {

		$where = $this->select();

		if ($column && $id) {
			$where->where('`' . $column . '`' .'= ?', $id);
		}

		if ($active) {
			$where->where('`active` = 1');
		}
		if ($in_order) {
			$where->order('order DESC');
		}

		return $this->fetchAll($where);
	}

	public function getNewPosition($column = null, $id = null) {
		$where = $this->select();

		if ($column && $id) {
			$where->where($column . '=' . $id);
		}

		$where->limit(1)->order('order DESC');

		$row = $this->fetchAll($where)->current();
		if ($row == null)
			return 1;
		return $row->order + 1;
	}

	public function fetchDefaultRow() {
		$select = $this->select()
				->where('`default` = 1');

		return $this->fetchRow($select);
	}

}

?>
