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

class LoginCtrl extends Ctrl {



	public function show() {
		$f = new Form('MainView'); // this is a composite view, it contains other views
		$f->template = 'app/Login/LoginForm.tpl'; // this is a template for the view, it is stored in views/ directory

		$login = new Input('loginScreen_loginBox'); // this is input widget with unique id 'loginScreen_loginBox'
		$f->add($login, 'login'); // input widget is attached to composite view with alias 'login'

		$passwd = new Input('loginScreen_passwdBox');
		$passwd->secret = true;
		$f->add($passwd, 'passwd');

		$button = new Button('loginButton'); // this is button widget
		$button->text = 'Log in';

		$button->addListener( // here an event listener is attached to button
			new ClickListener( // mouse click triggers...
				$a = new Action( // ...server side action (this action will be reused)
					'Login/LoginCtrl/authenticate', // LoginCtrl::authenticate, the class can be found in Login/ directory
					$f->valueReader() // one parameter is passed, which is the array of form values
				)
			)
		);
		// $button->addListener(new ClickListener(new Behaviour('alert(666)'))); // this way you can attach raw javascript to event
		$f->add($button); // button is placed in composite view

		$login->addListener(new KeyPressListener($a, 'Enter')); // another event listener, triggers action on Enter key press
		$passwd->addListener(new KeyPressListener($a, 'Enter')); // use 'Ctrl+Alt+Shift+F1' for shortcuts with modifiers

		// example use of StopTypingListener which triggers action if user stops typing for more than given miliseconds
		// $passwd->addListener(new StopTypingListener($a, 500));
		// this is how you can restrict triggering StopTypingListener when there is no input (eg. Delete key was pressed)
		// $passwd->addListener(new StopTypingListener(new Behaviour("if ({$passwd->valueReader()} != '') $a;"), 500));

		// here some i18n messages (use double underscore function to get translation)
		$f->loginString = __('login name');
		$f->passwordString = __('password');
		$f->blurbString = __('Just enter anything');
		// you can also use Smarty registered function {__ text="Just enter anything"} in template

		$lang = new Select('language-selector');
		$lang->options = array('en_EN' => 'English', 'pl_PL' => 'Polski');
		$lang->value = i18n::getLang();
		$lang->addListener(new ChangeListener(new Action('Login/LoginCtrl/setLanguage', $lang->valueReader())));
		$f->language = $lang; // alternative method of placing subview in a view

		return $f; // composite view is returned to client (or another method)
	}



	public function authenticate(FormContext $cx) {
		$f = $this->show(); // first, get the form from returned by show() method

		$t = new CompositeTranslator();
		$t->add('passwd', new MD5Translator());
		$data = $t->vtom($cx);

		// Form fields validation
		$v = new FocusCompositeValidator();
		$v->add(new LoginNameValidator($data['login'], $f->login));
		$v->add(new LengthValidator($cx->passwd, $f->passwd, 1, 60));
		if (!$v->valid()) {
			// SynCron::start('foo', new Behaviour("alert('SynCron message')"), 3000, false);
			// SynCron is a delayed/periodic execution facility
			return;
		}

		$this->response->alert(__('part-two-message'));
		// this calls client side handler (actually a bridged javascript function 'alert')

		return ActionService::execAction('PhoneBook/PhoneBookCtrl/show');
		// this returns the result of PhoneBook::show() method, the class can be found in PhoneBook/ directory
	}



	public function setLanguage($lang) {
		i18n::setLang($lang);
		return $this->show();
	}



} // class LoginCtrl

?>
Return current item: Tigermouse