Current File : /home/users/barii/public_html/finansenl.com.pl/wodki/application/classes/Controller/Core.php
<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Core extends Controller_Template
{
    /**
     * Domyślny szablon do napisania w klasach potomnych
     * @var String
     */
    public $template = 'core';

    protected $_ajax = FALSE;

    protected $lang;

    /**
     * Tablica dozwolnoych akcji bez logowania
     *
     * W klasach potomnych wystarczy tylko zdeklarowac akcje dostepne dla wszystkich
     * np. protected $allowed_actions = array('orders', 'settings');
     * Jesli sie tego nie zrobi tablica jest pusta czyli wszystkie akcje
     * tylko dla zalogowanych majacych role "$allowed_role"
     *
     * @var array
     */
    protected $allowed_actions = array();

    /**
     * Rola uzytkownika ktora ma dostep do wszystkich akcji
     *
     * Dla frontendu domyslnie 'login' i nie musi byc zmienianie dalej
     * Dla backendu domyslnie 'admin'
     * Dla core niestniejaca rola zeby wymusic domyslny brak dostepu
     *
     * @var string
     */
    public $allowed_role = 'non_existing';

    /**
     * Czy wszystkie akcje są dostępne bez logowania
     *
     * Domyślnie FALSE czyli potrzeba albo się zalogować albo samemu
     * podać dostępne akcje bez logowania
     *
     * @var boolean
     */
    public $all_actions_allowed = FALSE;



    public function action_index()
	{
        $this->template->content = 'index';
	}


    public function before()
    {
		if( Request::current()->controller() == 'Account' AND !Auth::instance()->logged_in() ) {
			$this->redirect('/');
		}
		if( !empty( $_GET['lang'] ) ) {
			$referer = !empty($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '/';
		
			$lang = strip_tags( $_GET['lang'] );
			$lang = DB::query(Database::SELECT, "SELECT * FROM `languages` WHERE `code` = '$lang'")->execute();
			Session::instance()->set('language', $lang[0]['id']);
			$this->redirect($referer);
		}
		
		$viewed = ORM::Factory('stat')->where('keyname','=','viewed')->find();
		$viewed->value += 1;
		$viewed->save();
		
        if (!$this->all_actions_allowed) {
	        if (sizeof($this->allowed_actions) > 0) {
	            if (!in_array($this->request->action, $this->allowed_actions)) {
	                // nie jest to akcja dozwolona wiec sprawdzamy role login
	                if (!$this->user_has_allowed_role()){
	                    // przykladowa reakcja na brak dostepu
	                    $this->action_not_allowed();
	                }
	            }
	        } else {
	            if (!$this->user_has_allowed_role()) {
	                $this->action_not_allowed();
	            }
	        }
    	}

        return parent::before();
    }

    public function after() {
        if (!isset($this->template->title))
			$this->template->title = Kohana::$config->load('site.appName');
			
			//$lang = Session::instance()->get('language');
			$language = ORM::Factory('language', !empty($lang) ? $lang : 1);
			
			$this->template->language = $language;
        return parent::after();
    }

    public function user_has_allowed_role()
    {
        if (Auth::instance()->logged_in($this->allowed_role)){
            return TRUE;
        }
        return FALSE;
    }

    /**
     * przykladowa reakcja na brak dostepu
     */
    public function action_not_allowed()
    {
        Message::notice(__t('Dostęp wyłącznie dla zalogowanych użytkowników serwisu.'));
        $this->redirect('/infosite/login');
        
        exit;
        //$this->redirect('/');
    }

    public function __call($method,$args)
    {
		echo("Could not find the url you requested.");
		exit;
    }

} // ~Controller_Core