<?php

class LoginController extends Controller {

    public $defaultAction = 'login';

    /**
     * Displays the login page
     */
    public function actionLogin() {
        if (Yii::app()->user->isGuest) {
            $model = new FormularioLoginUsuario;
            // collect user input data
            if (isset($_POST['FormularioLoginUsuario'])) {
                $model->attributes = $_POST['FormularioLoginUsuario'];
                // validate user input and redirect to previous page if valid
                if ($model->validate()) {
                    $this->lastViset();
                    if (Yii::app()->getBaseUrl() . "/index.php" === Yii::app()->user->returnUrl)
                        $this->redirect(Yii::app()->controller->module->returnUrl);
                    else
                        $this->redirect(Yii::app()->user->returnUrl);
                }
            }
            // display the login form
            $this->render('login', array('model' => $model));
        }
        else
            $this->redirect(Yii::app()->controller->module->returnUrl);
    }

    private function lastViset() {
        $ultimaVisita = Usuario::model()->notsafe()->findByPk(Yii::app()->user->id);
        $ultimaVisita->ultima_visita = date('Y-m-d H:i:s');
        $ultimaVisita->save();
    }

}