<?php

class Eppe_Form extends Zend_Form implements ArrayAccess {

	protected $yesNoOpt = array('1' => 'Tak', '0' => 'Nie');

	/**
	 * tablica do przechowywania prostej informacji o błędach
	 * @var type
	 */
	public $errors = array();

	/**
	 * Wyłącza dekoratory dla Zend Form Element pozostawiając jedynie ViewHelper
	 * @var bool
	 */
	protected $_disableLoadDefaultDecoratorsForFields = false;
	protected $bean;

	public function setBean(RedBean_OODBBean $bean) {
		$this->bean = $bean;
	}

	public function getValuesForDb() {
		return $this->getValues();
	}

	public function populateWithBean($bean) {
		return $this->populate($bean->export());
	}

	public function populate(array $values) {
		foreach (array_keys($values) as $element) {
			$formElement = $this->getElement($element);

			// Jesli element ma metody serialize/deserialize to ustawiamy mu wartosc odrebnie z uzyciem tych metod
			if ($formElement instanceof Eppe_Form_ElementInterface) {
				$formElement->setValue($formElement->deserializeValue($values[$element]));

				// i usuwamy go z przekazanej tablicy wartosci
				unset($values[$element]);
			}
		}

		// pozostale wartosci nadajemy klasycznie metodą populate
		return parent::populate($values);
	}

	protected function _getRequest() {
		return Zend_Controller_Front::getInstance()->getRequest();
	}

	public function uploadImage($column, $thumbsDefs) {
		$element = $this->$column;
		if (!$element->isUploaded())
			return false;
		$file = $element->getFilename();
		if (!$file)
			return false;
		$path = Eppe_Tools::setUniqueName($file);
		Eppe_Image_Thumbs::factory($path)
			->setThumbDefs($thumbsDefs)
			->saveThumbs($element->getDestination());
		return $path;
	}

	protected static function getSubmitButton($label = 'Zapisz') {
		return new Eppe_Form_Element_Submit('submit', array('Label' => $label));
	}

	protected static function getCancelButton($label = 'Anuluj') {
		return new Eppe_Form_Element_Cancel('cancel', array('Label' => $label));
	}

	protected function addToolbar() {
		$this->addElement(self::getSubmitButton());
		$this->addElement(self::getCancelButton());
	}

	public function getOpenTag() {
		$tag = '<form';

		foreach ($this->getAttribs() as $key => $value) {
			$tag .= ' ' . $key . '="' . $value . '"';
		}

		$tag .= '>';
		return $tag;
	}

	public function getCloseTag() {
		return '</form>';
	}

	protected function _disableLoadDefaultDecoratorsForField(&$element) {
		$element->clearDecorators();
		$element->setDecorators(array('ViewHelper'));
	}

	public function addElement($element, $name = null, $options = null, $disableDecorator = true) {
		if ($this->_disableLoadDefaultDecoratorsForFields && $disableDecorator) {
			$this->_disableLoadDefaultDecoratorsForField($element);
		}

		parent::addElement($element, $name, $options);
	}

	public function getValuesForDbWithSerialized($field, $regex) {
		$values = $this->getValues();
		$returnValues = array();
		$toSerialize = array();
		foreach (array_keys($values) as $element) {
			$formElement = $this->getElement($element);
			if (preg_match($regex, $formElement->getName())) {
				$toSerialize[$formElement->getName()] = $formElement->getValue();
			}
			else {
				$returnValues[$formElement->getName()] = $formElement->getValue();
			}
		}
		$returnValues[$field] = serialize($toSerialize);
		return $returnValues;
	}

	public function offsetExists($offset) {
		return $this->getElement($offset)? true: false;
	}

	public function offsetGet($offset) {
		return $this->getElement($offset)->getValue();
	}

	public function offsetSet($offset, $value) {
		$this->getElement($offset)->setValue($value);
	}

	public function offsetUnset($offset) {
		$this->removeElement($offset);
	}
	
	public function addStandardButtons() {
		$e = new Eppe_Form_Element_Submit(__('Submit'));
		$this->addElement($e);
		
		$e = new Eppe_Form_Element_Cancel(__('Cancel'));
		$this->addElement($e);		
	}

}
