<?php
class AppController extends Controller {

    var $helpers = array('Html', 'Form', 'Session', 'Time');
    var $components = array(
		'Session', 'RequestHandler',
		'Acl',
		'Auth' => array(
			'autoRedirect' => false,
			'authorize' => 'crud',
			'userScope' => array('not' => array('user_level_id' => null)),
			'logoutRedirect' => array('controller' => 'users', 'action' => 'login'),
		),
	);
	var $uses = array('User', 'UserLevel');

    var $me = array();    // user data from session

	function  beforeFilter() {
		parent::beforeFilter();

        // XMLHttpRequests will be rendered with ajax layout (== empty layout)
        if ($this->RequestHandler->isAjax()) {
            $this->layout = 'ajax';
        }
		
		$this->Auth->mapActions(array('display' => 'read'));

		// get user data for our convenience
        $this->me = $this->_buildUserProfile($this->Auth->user());
        $this->set('me', $this->me);
	}

    function beforeRender() {
        parent::beforeRender();
		
		if (!empty($this->nyro)) $this->set('nyro', $this->nyro);
    }

	function _buildUserProfile($user) {
		if (empty($user)) return null;

		$u = $this->User->read('', $user['User']['id']);
		unset($u['password']);

		$u['is_admin'] = ($user['User']['user_level_id'] <= 20);
		$u['is_super_admin'] = ($user['User']['user_level_id'] <= 10);

		return $u;
	}

	function _faUserLevels() {
		$conditions = array('UserLevel.id >=' => $this->Auth->user('user_level_id'));
		return $this->UserLevel->find('list', array('conditions' => $conditions));
	}



	function get_item($sql, $db_res = "", $test_mode = 0){
		
		if($db_res){
			$rez = mysql_query($sql, $db_res);
		}else{
			$rez = mysql_query($sql);
		}
		
		// ova funkcija uvijek mora vratiti samo nula ili jedan redak
		$l_br_redaka = mysql_num_rows($rez);
		switch($l_br_redaka){
			case 0:
				$result = "";
				break;
			case 1:
				$row = mysql_fetch_array ($rez);
				$result = trim($row[0]);
				break;
			default:
				if($test_mode == 1){
					die("Funkcija <i>get_item()</i> vratila je više od jednog retka iako je dozvoljeno vraćanje samo jednog".
						"<br><br> &nbsp; :::=>> <b>".$sql."</b>");
				}
		}
		
		return $result;
	}
	
	function get_data($sql, $force_array = 0, $db_res = ""){
		$la_polje = array();
		
		if($db_res){
			$rez = mysql_query($sql, $db_res);
		}else{
			$rez = mysql_query($sql);
		}
		//echo $sql."\n<br><br>";
		//echo mysql_errno() . ": " . mysql_error() . "\n<br><br>";
		

		$l_br_polja = mysql_num_fields($rez);
		for($i = 0; $i < $l_br_polja; $i++){
			array_push($la_polje, mysql_field_name($rez, $i));
		}
		
		$l_br_redaka = mysql_num_rows($rez);
		if($force_array && $l_br_redaka == 1){
			// uvijek vrati array
			$l_br_redaka = -1;
		}
		switch($l_br_redaka){
			case 0:
				$result = array("");
				break;
			case 1:
				$row = mysql_fetch_array ($rez);
				for($i = 0; $i < $l_br_polja; $i++){
					$result[$la_polje[$i]] = $row[$i];
				}
				break;
			default:
				for($i = 0; $i < $l_br_polja; $i++){
					$result[$la_polje[$i]] = array();
				}
				while ($row = mysql_fetch_array ($rez)){
					for($i = 0; $i < $l_br_polja; $i++){
						array_push($result[$la_polje[$i]], $row[$i]);
					}
				}
		}
		return $result;
	}
	
}
