<?php
/**
*  Karamelo e-Learning Platform
*  Chipotle Software TM 2002-2008
*  GPLv3 
*  @author Manuel Montoya manuel<arroba>mononeurona<punto>org
*  @version 0.3
*  @package Karamelo
*/
//file: app_controller.php

uses('L10n');

class AppController extends Controller {
 
 public $components = array('Auth', 'Cookie', 'Session'); // set Security component later
 
 public $helpers    = array('Html', 'Form', 'Session');
 
 public function beforeFilter()
 {
   //set locale (es or en by now)
   $this->__setLocale();

   // Auth configuratin
   $this->Auth->fields         = array('username'   => 'email', 'password' => 'pwd');
   $this->Auth->loginAction    = array('controller' => 'users', 'action'   => 'login');
   $this->Auth->loginRedirect  = array('controller' => 'news', 'action'    => 'display');
   $this->Auth->logoutRedirect = array('controller' => 'news', 'action'    => 'display');
   $this->Auth->loginError     = __('wrong_pass_or_email', true);
   $this->Auth->authorize      = 'controller';  
   $this->Auth->deny('*');
 }

 public function isAuthorized() 
 {      
   if (isset( $this->params[Configure::read('Routing.admin')] )):
       if ($this->Auth->user('group_id') == 1 || $this->Auth->user('group_id') == 2 ):  // admin and teachers
	      return true;
       else:
              return false;
       endif;
   endif;
 
   return true;
 }

 public function msgFlash($msg, $to)
 {
     $this->Session->setFlash($msg); // http://manual.cakephp.org/chapter/session
     $this->redirect($to);       
     return true;
 }

 private function __setLocale()
 {
     $this->L10n = new L10n();
     /* $languages  = array('es','en'); // 'nah' for nahuatl
     $paramLang  = strtok($_SERVER['HTTP_HOST'],'.');
     $lang       = $this->Session->check('lang') ? $this->Session->read('lang'):'en';

     if (isset($paramLang) && in_array($paramLang,$languages) ):
        $lang = $paramLang;
        $this->Session->write('lang',$lang);
     else:
        $this->Session->write('lang',$lang);
     endif; */
     $lang = 'en';
     $this->L10n->get($lang);
     Configure::write('Config.language', $lang);
 }

 protected function getSupport()
 {
   $this->flash(__('An error has been encountered, please ask Chipotle Software (www.chipotle-software.com) for support', true),'/',15);
 } 
}
?>
