Location: PHPKode > projects > Tigermouse > tigermouse-DR1.7/ctrl/PhoneBook/FormCtrl.class.php
<?php

class FormCtrl extends Ctrl {



	private function getForm() {
		$f = new Form('PhoneBookEntry');
		$f->template = 'app/PhoneBook/Form.tpl';
		$f->add(new Input('name'));
		$f->add(new Input('phone'));
		$f->add($ok = new Button('ok'));
		$ok->text = 'Ok';
		return $f;
	}



	private function getWindow() {
		$w = new Window('PhoneBookWindow');
		$w->title = __('phonebook-entry-window-title'); // internationalization support, see i18n/* file
		return $w;
	}



	public function open($id = null) {
		$f = $this->getForm();
		$f->ok->addListener(
			new ClickListener(
				new Action(
					'PhoneBook/FormCtrl/save',
					array( // if you want to pass more than one argument you need to wrap it with array
						$f->valueReader(), // valueReader() called on Form returns form data wrapped with FormContext object
						$id // primitive value or valueReader called on non-Form returns a primitive value, see save() method below
					)
				)
			)
		);
		$w = $this->getWindow();
		$w->add($f);
		if ($id) {
			$entry = new PhoneBookEntry();
			$entry->loadById($id);
			$f->name->value = $entry->name;
			$f->phone->value = $entry->phone;
		}
		$w->open(); // this calls client side handler that opens a window
	}



	public function save(FormContext $cx, $id = NULL) {
		$f = $this->getForm();
		$validator = new CompositeValidator();
		$validator->add(new LengthValidator($cx->name, $f->name, 1, 120));
		$validator->add(new LengthValidator($cx->phone, $f->phone, 1, 50));
		if (!$validator->valid()) {
			return;
		};

		$entry = new PhoneBookEntry();
		if ($id) {
			$entry->loadById($id);
		}
		$entry->name = $cx->name;
		$entry->phone = $cx->phone;
		try {
			$entry->save();
		} catch (Exception $e) {
			echo $e->getMessage();
		}

		$this->getWindow()->close();
		return ActionService::execAction('PhoneBook/PhonesListCtrl/show');
	}



} // class FormCtrl

?>
Return current item: Tigermouse