<?php

class Cms_Cms_LoginController extends Pampa_Controller_Action {

  public function init(){
    parent::init();

    // disable layout
    $this->_helper->Layout->setLayout('iframe');
  }

  public function indexAction(){
    // the default message
    $this->_helper->messenger('info', 'Por favor, entre com seu usuário e senha.');
  }

  public function loginAction(){
    // check for post submit
    if(!$this->_request->isPost()){
      // shows the error
      $this->_helper->messenger('error', 'Invalid request.');

      $this->render('index');
    }

    // call the login
    $modelCmsUsers = new Model_Cms_Users();
    $login = $modelCmsUsers->login($_POST['login']);

    // check for errors
    if($login->error){
      // shows the error
      $this->_helper->messenger('error', $login->message);

      $this->render('index');
    } else {
      // redirect
      Pampa_Log::log("Logou no sistema");
      $this->redirectTo("index");
    }
  }

  public function logoutAction(){
    // clear the instance
    Pampa_Auth_Cms::getInstance()->clearIdentity();

    // redirect
    $this->_redirect('/');
  }

}