<?php

abstract class SimpleScaffoldCtrl extends ScaffoldCtrl {



	protected $modelClass;
	protected $notNulls = array();



	public function __construct() {
		if (!$this->modelClass) {
			throw new Exception(get_class($this) . '::modelClass must not be empty');
		}
	}



	protected function buildModel($id = null) {
		$className = $this->modelClass;
		$ar = new $className();
		if ($id) {
			$ar->loadById($id);
		}
		return $ar;
	}



	protected function buildValidator(Form $f, FormContext $cx) {
		$v = parent::buildValidator($f, $cx);
		foreach ((array)$this->notNulls as $field) {
			$v->add(new NotNullValidator($cx->$field, $f->getChildById($field)));
		}
		return $v;
	}



	protected static function addButton(Form $f, $buttonId, $text, $action, $id = null, $skipArgs = false) {
		$button = new Button($buttonId);
		$button->text = $text;
		$action = Action::buildAction($action);
		if (!$skipArgs) {
			if ($id !== null) {
				$action->unshift($id);
			}
			$action->unshift($f->valueReader());
		}
		$button->addListener(new ClickListener($action));
		$button->addListener(new KeyPressListener($action, 'Enter Space'));
		$f->add($button);
	}



} // class SimpleScaffoldCtrl

?>