Location: PHPKode > projects > Mocovie web framework > webs/common/controls/formreceiver.php
<?php
/**
 *  Copyright (C) 2011 Kai Dorschner
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author Kai Dorschner <the-hide@address.com>
 * @copyright Copyright 2011, Kai Dorschner
 * @license http://www.gnu.org/licenses/gpl.html GPLv3
 * @package mocovi
 * @subpackage controls
 */

ControlFactory::load('dataprovider');
/*
interface FormreceiverDelegate extends DataproviderDelegate
{
	public function inputReceivesValue($name);
}
*/

class formreceiver_control extends dataprovider_control /*implements FormreceiverDelegate*/
{
	protected $formControl;

	protected $defaultOptions = array
		( 'reference'	=> false
		);

	/**
	 * @override
	 */
	protected function _beforeCreateNode(DomNode $parent)
	{
		if(!($id = $this->getOption('reference')))
			throw new MvcException('A reference-attribute is obligatory. It must contain an xml:id, defined anywhere in the filesystem.');

		if($this->dataSent() && $form = $this->getForm())
			foreach($form->find('input') as $inputField)
			{
				try
				{
					$this->data[$inputField->getOption('name')] = $inputField->receiveData($this->getHTTPMethod());
					//if(!isset($this->delegate) || $this->delegate->inputReceivesValue($inputField->getOption('name')))
						$inputField->setValue($this->data[$inputField->getOption('name')]);
				}
				catch(Exception $e)
				{
					$inputField->highlight();
					$inputField->setPlaceholder(Translator::translateToken(get_class($e))->nodeValue);
					$this->data[] = get_class($e).': '.$e->getMessage();
				}
			}
		$this->deleteNode();
		return false;
	}

	/* public function setDelegate(DataproviderDelegate $delegate)
	{
		if($delegate instanceof FormreceiverDelegate)
			$this->delegate = $delegate;
		else
			throw new Exception('Delegate for formreceiver control must implement the FormreceiverDelegate interface');
	} */

	protected function dataSent()
	{
		return
		(
			($method = $this->getHTTPMethod())
			&&
			(
				$method == 'post' && count($_POST) > 0
				||
				$method == 'get' && count($_GET) > 0
			)
		);
	}

	protected function getHTTPMethod()
	{
		return strtolower($this->getForm()->getOption('method'));
	}

	protected function getForm()
	{
		if($this->formControl)
			return $this->formControl;
		if($form = $GLOBALS['filesystem']->getDom()->getElementById($this->getOption('reference')))
		{
			$this->formControl = ControlFactory::getControl($form);
			if(!$this->formControl->nodeCreated())
				$this->formControl->load($this->dom->createElement('dump'))->run();
			return $this->formControl;
		}
		throw new MvcException('Necessary xml:id "'.$this->getOption('reference').'" not found in filesystem.');
	}

	public function inputReceivesValue($name)
	{
		return false;
	}
}
Return current item: Mocovie web framework