<?php

defined('HOSTCMS') || exit('HostCMS: access denied.');

/**
 * Admin form dataset.
 *
 * @package HostCMS 6\Admin
 * @version 6.x
 * @author Hostmake LLC
 * @copyright © 2005-2013 ООО "Хостмэйк" (Hostmake LLC), http://www.hostcms.ru
 */
abstract class Admin_Form_Dataset
{
	/**
	 * Limit
	 * @var int
	 */
	protected $_limit = NULL;
	
	/**
	 * Offset
	 * @var int
	 */
	protected $_offset = 0;

	/**
	 * Load status
	 * @var boolean
	 */
	protected $_loaded = FALSE;

	/**
	 * List of changed fields
	 * @var array
	 */
	protected $_changedFields = array();

	/**
	 * Form controller
	 * @var Admin_Form_Controller
	 */
	protected $_Admin_Form_Controller = NULL;

	/**
	 * Set controller 
	 * @param Admin_Form_Controller controller
	 * @return self
	 */
	public function controller(Admin_Form_Controller $controller)
	{
		$this->_Admin_Form_Controller = $controller;
		return $this;
	}

	/**
	 * Array of conditions
	 * array (
	 * 	array('where' => array('a', '=', 'b')),
	 * 	array('where' => array('c', '=', 'd'))
	 * )
	 */
	protected $_conditions = array();

	/**
	 * Set limit
	 * @param int $limit limit
	 * @return self
	 */
	public function limit($limit)
	{
		$this->_limit = intval($limit);
		return $this;
	}

	/**
	 * Set offset 
	 * @param int $offset offset
	 * @return self
	 */
	public function offset($offset)
	{
		$this->_offset = intval($offset);
		return $this;
	}

	/**
	 * Set load status
	 * @param boolean $loaded status
	 * @return self
	 */
	public function loaded($loaded)
	{
		$this->_loaded = $loaded;
		return $this;
	}

	/**
	 * Add condition
	 * @param array $condition condition
	 * @return self
	 */
	public function addCondition($condition)
	{
		$this->_conditions[] = $condition;
		return $this;
	}

	/**
	 * Изменение данных поля Admin_Form_Field для источника
	 * используется, например, для изменение типа выводимого поля
	 * Change the data for the field Admin_Form_Field
	 * @param string $name field name
	 * @param string $property property name
	 * @param string $value property value
	 * @return self
	 */
	public function changeField($name, $property, $value)
	{
		$this->_changedFields[$name][$property] = $value;
		return $this;
	}

	/**
	 * Get changed field
	 * @param string $name field name
	 * @return string
	 */
	public function getFieldChanges($name)
	{
		if (isset($this->_changedFields[$name]))
		{
			return $this->_changedFields[$name];
		}

		return NULL;
	}

	/**
	 * Get count of finded objects
	 * @return int
	 */
	abstract public function getCount();

	/**
	 * Load objects
	 * @return array
	 */
	abstract public function load();

	/**
	 * Get typical entity
	 * @return object
	 */
	abstract public function getEntity();
	
	/**
	 * User-defined comparison functions.
	 * @param mixed $m
	 * @param mixed $n
	 * @return int
	 */
	static public function _sortAsc($m, $n)
	{
		$sortField = $m->getSortField();
		if ($m->$sortField == $n->$sortField)
		{
			return 0;
		}

		return ($m->$sortField < $n->$sortField) ? -1 : 1;
	}

	/**
	 * User-defined comparison functions.
	 * @param mixed $m
	 * @param mixed $n
	 * @return int
	 */
	static public function _sortDesc($m, $n)
	{
		$sortField = $m->getSortField();
		if ($m->$sortField == $n->$sortField)
		{
			return 0;
		}

		return ($m->$sortField > $n->$sortField) ? -1 : 1;
	}
}