<?php

require_once dirname(__FILE__) . '/se-lib/bootstrap.php';

//session_save_path("./tmp") ;
session_start();

date_default_timezone_set('Europe/Warsaw');// PHP 5 >= 5.1.0 required by date functions

error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 0);
ini_set('display_startup_errors', '0');
//display_startup_errors(0);

#TEST $_SESSION['DEBUG'] = 3;// TODO: TEST
if (!isset($_SESSION['DEBUG'])) $_SESSION['DEBUG'] = 0;// set default value


if (file_exists(APP_PATH_ROOT . "/config/.config_{$_SERVER['SERVER_NAME']}.php")) {
	require APP_PATH_ROOT . "/config/.config_{$_SERVER['SERVER_NAME']}.php";
}

if (file_exists(APP_PATH_ROOT . "/.config.php")) include APP_PATH_ROOT . "/.config.php";

require_once APP_PATH_ROOT . "/superedit-SEF.php";

if (!User::logged()) {
	die('NotAuthenticated');
}

$DBG = ('1' == V::get('DBG', '', $_REQUEST));

$task = V::get('_task', '', $_REQUEST);
$zasobID = V::get('_zasobID', 0, $_REQUEST, 'int');
$cls = V::get('_cls', '', $_REQUEST);

if ($cls == 'UserBookmarks') {
	// ?_cls=UserBookmarks&_task=add_bookmark&_zasobID={$ID}
	Lib::loadClass('UserBookmarks');
	$userBookmarks = UserBookmarks::getInstance();

	if ($task == 'add_bookmark') {
		if ($zasobID <= 0) {
			die('Error: no resource');
		}
		$userBookmarks->addBookmark($zasobID);
		User::saveProfile();
	} else if ($task == 'remove_bookmark') {
		if ($zasobID <= 0) {
			die('Error: no resource');
		}
		$userBookmarks->removeBookmark($zasobID);
		User::saveProfile();
	} else if ($task == 'change_bookmark') {
		if ($zasobID <= 0) {
			die('Error: no resource');
		}
		$btnCls = V::get('btnCls', '', $_REQUEST);
		if (empty($btnCls)) {
			die('Error: no button class');
		}
		$userBookmarks->changeBookmark($zasobID, $btnCls);
		User::saveProfile();
	} else if ($task == 'sort_bookmarks') {
		$idsOrdered = V::get('ids', array(), $_REQUEST, 'array', array('V', 'filterPositiveInteger'));
		if (empty($idsOrdered)) {
			die('Error: no ids');
		}
		$userBookmarks->sortBookmarks($idsOrdered);
		User::saveProfile();
	}

	// default - always return bookmarks
	$userAcl = User::getAcl();
	$tbls = $userAcl->getTablesAcl();
	$urls = $userAcl->getUrls();

	$jsonData = array();
	$bookmarks = $userBookmarks->getBookmarks();
	foreach ($bookmarks as $kZasobID => $vClass) {
		if (array_key_exists($kZasobID, $tbls)) {
			$jsonData[] = (object)array('id'=>$kZasobID, 'name'=>$tbls[$kZasobID]->getName(), 'label'=>$tbls[$kZasobID]->getRawLabel(), 'opis'=>$tbls[$kZasobID]->getOpis(), 'type'=>'menu', 'class'=>$vClass);
		}
		else if (array_key_exists($kZasobID, $urls)) {
			$jsonData[] = (object)array('id'=>$kZasobID, 'name'=>$urls[$kZasobID], 'type'=>'url', 'class'=>$vClass);
		}
	}

	if ($DBG) {
		echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">tbls (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($tbls);echo'</pre>';
		echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">urls (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($urls);echo'</pre>';
		echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">bookmarks (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($bookmarks);echo'</pre>';

		$USER_PROFILE = $_SESSION['USER_PROFILE'];
		reset($USER_PROFILE);
		$VAR = " Array ( ";
		for ( ;(key($USER_PROFILE)); next($USER_PROFILE)) {
			$VAR = $VAR . "\n\t\"" .  key($USER_PROFILE) . "\" => Array ( ";
			for ( ;(key($USER_PROFILE[key($USER_PROFILE)])); next($USER_PROFILE[key($USER_PROFILE)])) {
				$VAR = $VAR . "\n\t\t \"" . key($USER_PROFILE[key($USER_PROFILE)]) . "\"=>\"" . $USER_PROFILE[key($USER_PROFILE)][key($USER_PROFILE[key($USER_PROFILE)])] . "\"," ;
			}
			$VAR = $VAR . "\n\t ), ";
		}
		$VAR = $VAR . "\n ) " ;
		echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;"> (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($VAR);echo'</pre>';

		$USER_PROFILE = $_SESSION['USER_PROFILE'];
		$VAR = " Array ( ";
		foreach ($USER_PROFILE as $key => $values) {
			$VAR = $VAR . "\n\t\"" .  $key . "\" => Array ( ";
			foreach ($values as $k => $v) {
				$VAR = $VAR . "\n\t\t \"" . $k . "\"=>\"" . $v . "\"," ;
			}
			$VAR = $VAR . "\n\t ), ";
		}
		$VAR = $VAR . "\n ) " ;
		echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;"> (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($VAR);echo'</pre>';
	}

	echo json_encode($jsonData);
	die();
}


if ($zasobID <= 0) {
	die('Error: no tbl');
}

$userAcl = User::getAcl();
$tblAcl = $userAcl->getTableAcl($zasobID);
if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">tblAcl (F.' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($tblAcl);echo'</pre>';}

if (!$tblAcl->isInitialized()) {
	echo'<p class="red">'."Brak konfiguracji dla ".$tblAcl->getName()."!".'</p>';
	return;
}

if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">tblAcl (F.' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($tblAcl);echo'</pre>';}
if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">cls (F.' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($cls);echo'</pre>';}

if (!$cls) {
	die('Error: no cls');
}
Lib::loadClass($cls);
if (!class_exists($cls)) {
	die('Error: cls not exists ' . $cls);
}

$tblObj = new $cls($tblAcl);
if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">tblObj (F.' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($tblObj);echo'</pre>';}

$task = V::get('_task', '', $_REQUEST);

if ($task == 'EDIT') {
	$id_item = V::get('ID', 0, $_REQUEST, 'int');
	if ($id_item > 0) {
		Lib::loadClass('UserActivity');
		UserActivity::add($zasobID, 'edit', $id_item);
	}
}

S::timeoutUpdate();

if (method_exists($tblObj, 'ajaxTask')) {
	$tblObj->ajaxTask($task);
} else {
	die('Error: method ajaxTask not exists in class ' . $cls);
}

?>
