<?php

/**
* 	data needed for views
* 	$css
* 	$js
* 	$js_vars
* 	
* 	$logo
* 	$go_to_site_btn ( right top btns)
* 	$bread_crumb_data
* 	$boxes_btns
* 	$treemenu
* 	$content
* 		$actions
* 		$report
* 		$flags
* 		$is_list
* 		$js_function
*
*/
class adminMainController extends mainController
{
	
	public $model_name = 'adminMainModel';
	public $view_name = 'adminMainView';
	public $csss = array();
	public $jss = array();
	public $page_node = null;
	public $page_uid = null;
	public $page_id = null;
	public $datas = array(
		'css'				=> array(),
		'js'				=> array(),
		'js_vars'			=> array(),
		'js_func'			=> '', // only for popup template
		'logo'				=> array(),
		'actions_right'		=> array(),
		'mc_menu'			=> array(),
		'mc_id'				=> '',
		'body_class'		=> '',
		'bread_crumb'		=> array(),
		'boxes_btns'		=> array(),
		'js_tree'			=> array(),
		// 'tree_menu'			=> array(), // useless
		'content'			=> array(),
		'actions'			=> array(),
		'report'			=> array(),
		'flags'				=> array(),
		'is_list'			=> false,
		'is_float'			=> false,
		'is_frame'			=> false,
		'is_popup'			=> false,
	);

	function __construct($route = null) {

		parent::__construct($route);
		// var_dump( SESS()->exist('adminlog') );
		// exit;
		if ( $_GET['url'] == 'cube' ) {
			// var_dump( 'aaa' );
			// var_dump( $_GET );
			// exit;
			$query = array();
			unset($_GET['url']);
			foreach($_GET as $k=>$v) {
				$query[] = $k.'='.$v;
			}
			$query = (sizeof($query) > 0 ) ? '?'.implode('&', $query) : '';
			header('Location: '.ADMIN_URL.'/'.$query, true, 301);
			exit;
		}
		// var_dump( $_GET );
		// exit;
		if( isset($_GET['deco']) ){
			SESS()->clean('adminlg');
			SESS()->clean('adminlog');
			SESS()->clean('adminname');
			SESS()->clean('adminauth');
			header("HTTP/1.1 303 See Other");
			header('Location: '.ADMIN_URL.'/login');
			// header('Location: '.ADMIN_URL.'/login', true, 301);
			// var_dump( 'cleaned' );
			exit;
		}

		// change les models kan ils seront impléemntés
		$this->datas['is_float'] = (isset($_GET['cubefloat']) && $_GET['cubefloat'] == '1');
		$this->datas['is_frame'] = (isset($_GET['cubeframe']) && $_GET['cubeframe'] == '1');
		$this->datas['is_popup'] = (isset($_GET['popup']) && $_GET['popup'] == '1');

		// var_dump($_SERVER);
		// $time_start = microtime(true); 
		header('Content-type: text/html; charset=UTF-8');

		$this->datas['logo'][] = '<div id="cubelogo" data-icon=""></div>';

		$this->datas['css'][] = getFingerPrintUrl('bootstrap3.css', true);
		// $this->datas['css'][] = getFingerPrintUrl('css/flat-ui.css', true);
		//$this->datas['css'][] = getFingerPrintUrl('css/bootstrap-post.cyma.css', true);
		$this->datas['css'][] = getFingerPrintUrl('css/cube.cyma.css', true);
		//$this->datas['css'][] = getFingerPrintUrl('css/icons.css', true);
		require_once LIB_PATH.'Lib-Alm.php';
		// var_dump(SESS()->exist('adminlog'));
		if( !SESS()->exist('adminlog') ){
			$this->loadModel('adminCxModel');
			$data = $this->model->getData('adminCxModel');
			$this->datas = array_merge($this->datas, $data);
			$view_name = ( $this->datas['is_float'] ) ? 'adminCxFloatView' : $this->view_name;
			$this->loadView($view_name);
			$this->view->template_name = 'adminCxTemplate';

			$this->datas['js'][] = getFingerPrintUrl('/js/vendor/spin-min.js', true);
			$this->datas['js'][] = getFingerPrintUrl('/js/three3d.js', true);
			$this->datas['js'][] = getFingerPrintUrl('/js/cube3d.js', true);
			
			$this->view->render($this->datas);
			exit;
		}

		if ( SESS()->exist('admin_last_post') ) {
			$_POST = decodeObj( SESS()->get('admin_last_post') );
			SESS()->clean('admin_last_post');
		}

		$this->getLangFromRequest();

		$this->fillMainCat();

		$this->analyseRequest();

		$this->loadModel($this->model_name);

		$this->loadView($this->view_name);

		$this->render();
	}

	public function getLangFromRequest(){
		if(isset($_GET['lang'])){
			S()->setLang($_GET['lang']);
		}
	}

	public function fillMainCat(){
		S()->InitAdminCats();
	}

	public function analyseRequest(){
		/***********
		Get CAT NODE : priority : GET > @admindefault="1" > cat[@id="site"]
		************/
		$page = (isset($_GET['page']) && strlen($_GET['page'])>0) ? $_GET['page'] : '';
		if ( strlen($page)>0 ) $this->page_node = S()->cat->getElement('//struct/cat[@id="'.implode('"]/cat[@id="',explode('-',$page)).'"]');
		if ( !$this->page_node ) $this->page_node = S()->cat->getElement("//cat[@admindefault='1']");
		if ( !$this->page_node ) $this->page_node = S()->cat->getElement("//cat[@id='site']");
		if ( !$this->page_node ) {
			die('Page non trouvée');
		}
		if ( $this->page_node->getAttribute('noadmin') == '1' ) {
			$this->page_node = S()->cat->getElement('./cat', $this->page_node);
		}
		$this->page_uid = $this->page_node->getAttribute('uid');
		$this->page_id = $this->page_node->getAttribute('id');
		$this->page_node->setAttribute('getsel','1');
	}

	public function render(){
		$this->datas['css'][] = getFingerPrintUrl('css/select2.css', true);
		$this->datas['css'][] = 'http://fonts.googleapis.com/css?family=Oswald';

		// start js processing
		// array of js files, 0=>filename, 1=>is_asset
		$this->datas['js'][] = getFingerPrintUrl('jquery110.js', true);
		// $this->datas['js'][] = getFingerPrintUrl('js/jquery-ui-1-8-24-custom-min.js', true);
		$this->datas['js'][] = getFingerPrintUrl('js/jquery-ui-1-10-3-custom-min.js', true);
		$this->datas['js'][] = getFingerPrintUrl('mootools.js', true);
		$this->datas['js'][] = getFingerPrintUrl('js/bootstrap.js', true);
		$this->datas['js'][] = getFingerPrintUrl('js/select2.js', true);
		$this->datas['js'][] = getFingerPrintUrl('js/vendor/spin-min.js', true);
		// $this->datas['js'][] = getFingerPrintUrl('cube/ckfinder/ckfinder.js', false);
		$this->datas['js'][] = getFingerPrintUrl('js/test.js', true);
		$this->datas['js'][] = getFingerPrintUrl('js/test2.js', true);

		$js_assets = array();
		$rep = opendir(ASSET_PATH.'js'.DS.'cube');
		while($file = readdir($rep)){
			if($file == '..' || $file =='.' || $file =='' || strpos($file, '.crc') !== false || strpos($file, 'ccmin.js') !== false) continue;
			$js_assets[] = $file;
		}
		asort($js_assets);
		foreach($js_assets as $jsa){
			$this->datas['js'][] = getFingerPrintUrl('js/cube/'.$jsa, true);
		}

		$this->datas['js_vars'][] = 'var CUBE = CUBE || {};';
		$this->datas['js_vars'][] = 'var SITE_ROOT = \''. SITE_ROOT .'\';var ASSET_URL = \''.ASSETS_URL.'\';';
		$this->datas['js_vars'][] = 'var CI = new Object();';
		$this->datas['js_vars'][] = 'var AUTHS = \';'.SESS()->get('adminauth').';\';';
		$this->datas['js_vars'][] = 'var ALM = new Object();';
		// Mots utilisés par js
		$words = array('areyousure','pagehavebeenmodified');
		foreach($words as $w) 
			$this->datas['js_vars'][] = 'ALM[\''.$w.'\'] = \''.Alm::singleton()->L($w).'\';';
		$customCkConfig = (is_file(ROOT_PATH.'site'.DS.'assets'.DS.'configfck.js')) ? 'true' : 'false';
		$this->datas['js_vars'][] = 'var customCkConfig = '.$customCkConfig.';';

		// new add js from modules
		foreach ( CubeModules::_()->getAdminJss() as $js_data ) {
			$this->datas['js'][] = getFingerPrintUrl($js_data[0], $js_data[1]);
		}

		// new add css from modules
		foreach ( CubeModules::_()->getAdminCsss() as $css_data ) {
			$this->datas['css'][] = getFingerPrintUrl($css_data[0], $css_data[1]);
		}

		$this->datas['js_vars'][] = 'var admin_lang = \''.S()->LG->GetLangId().'\';';
		$js_inframe = ( $this->datas['is_frame'] ) ? 'true' : 'false';
		$this->datas['js_vars'][] = 'var inframe = '.$js_inframe.';';

		$this->datas['js'][] = getFingerPrintUrl('js/cube-list.js', true);
		$this->datas['js'][] = getFingerPrintUrl('js/cube.js', true);

		// $head = array();
		// $head[] = '
		// <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		// <title>Admin - '. S()->title .'</title>
		// <link rel="shortcut icon" href="'.ASSETS_URL.'css/design/cube.gif" />
		// ';
		// function addCssHtml($src){return '<style type="text/css" title="currentStyle" media="screen">@import "'.$src.'";</style>';}
		// $head[] = implode('', array_map('addCssHtml', $this->datas['css']));
		// function addJsHtml($src){return '<script src="'.$src.'" type="text/javascript"></script>';}
		// $head[] = '<script type="text/javascript">'.implode('', $this->datas['js_vars']).'</script>';
		// $head[] = implode('', array_map('addJsHtml', $this->datas['js']));
		
		// move before main admin model getData call	
		if ( $this->page_node->getAttribute('id') == 'mediamanager' ){
			
		}
		else {
			$this->model->getData($this->page_node);
			$form = $this->model;
			// $form = new Admin($this->page_node);
			// $positioncontent = implode('',$form->posarr); // a virer
			$this->datas['actions'] = $form->actions;
			$this->datas['actions_right'] = $form->actions_right;
			$this->datas['content'][] = implode("\r\n",$form->data);
			$this->datas['report'][] = ( !empty($form->report) ) ? '<div class="reporttxt">'.$form->report.'</div>' : '';
			$this->datas['js_func'] = $form->jsFunction;
			$this->datas['flags'] = $form->langhtml;
			$this->datas['is_list'] = $form->is_list_page;

			$this->datas['content'][] = '<script>currentmenu = \''.$this->page_id.'\';</script>';

			$this->datas['js'] = array_merge($this->datas['js'], $form->js);
			$this->datas['css'] = array_merge($this->datas['css'], $form->css);
		}

		

		// read menu

		S()->readCats();

		$this->page_node = S()->cat->getElement('//cat[@uid="'.$this->page_uid.'"]');

		// breadcrumb start
		$branches = S()->cat->getElements('ancestor-or-self::cat', $this->page_node);
		$mc_node = array_shift($branches);
		$mc_node->setAttribute('curcubecat', '1');
		foreach($branches as $branch) {
			$branch->setAttribute('curcubecat', '1');
			$this->datas['bread_crumb'][] = '<a href="?page='.$branch->getAttribute('uid').'&lang='.S()->LG->GetLangId().'">'.txtLineCut($branch->getAttribute('name'), 50).'</a>';
		}
		if ( sizeof($this->datas['bread_crumb']) > 0 ) {
			array_unshift($this->datas['bread_crumb'], ' ');
		}
		// breadcrumb end

		$this->datas['mc_id'] = $mc_node->getAttribute('id');

		// treemenu start
		$jsmenu = array();
		$i = -1;
		$pid = -1;
		if ( $this->datas['is_float'] ){
			readCats3(S()->cat->getElement('//struct'), $i, $jsmenu, $pid);
			include_once LIB_PATH.'assets'.DS.'cube'.DS.'templates'.DS.'maintoolbar.php';
			exit;
		}
		if ( $mc_node )
			readCats2($mc_node, $i, $jsmenu, $pid);
		// treemenu end

		// main menu start
		foreach(S()->cat->getElements('//struct/cat') as $mc){
			$sel = ( $mc->getAttribute('id') == $mc_node->getAttribute('id') ) ? ' current' : '';
			$mc_name = ucfirst($mc->getAttribute('name'));
			//if ( sizeof($auths) == 0 || in_array($mc->getAttribute('id'), $auth_mc) )
				//$mc_menu[] = '<a href="?page='.$mc->getAttribute('uid').'" class="'.$mc->getAttribute('id').$sel.' btn_50x50 sp" title="'.$mc_name.'" urlid="'.$mc->getAttribute('id').'"></a>';
				// added special case for help (if attribute onlyowner="1" hide for others users)
				if ( $mc->getAttribute('onlyowner') == '1' && SESS()->get('adminownerlvl') != '1' ) {
					continue;
				}
				$this->datas['mc_menu'][] = '<a href="?page='.$mc->getAttribute('uid').'" data-icon="'.$mc->getAttribute('icon').'" class="'.$sel.' btn_50x50" title="'.$mc_name.'" urlid="'.$mc->getAttribute('id').'" data-behavior="BS.Twipsy"></a>';
		}
		// main menu end

		$this->datas['js_tree'] = $jsmenu;
		
		

		// go to site btn start
		$url = SITE_ROOT;
		if ( $mc_node->getAttribute('id') == 'site' && $this->page_node && $this->page_node->getAttribute('nositemap') != '1') {
			$url .= '/'.$this->page_node->getAttribute('mainwu');
		}
		$this->datas['top_right_btns'][] = '<a id="btn_showsite" class="btn_52x28" data-icon="&#xe029;" href="'.$url.'" title="'.Alm::singleton()->L('opensite').'" target="_blank"></a>';
		$this->datas['top_right_btns'][] = '<a id="btn_logout" class="btn_52x28" data-icon="&#xe010;" href="?deco=1" title="'.Alm::singleton()->L('disconnect').'"></a>';
		// go to site btn end
		
		if ( is_file(ROOT_PATH.'cube'.DS.'action-postproc.php') ) {
			require_once ROOT_PATH.'cube'.DS.'action-postproc.php';	
		}

		$this->datas['body_class'][] = ( $this->datas['is_popup'] ) ? 'popup' : ( ( $this->datas['is_frame'] ) ? 'frame' : '' );
		$this->view->template_name = 'adminMainTemplate';
		// $view_template = ( $this->datas['is_popup'] ) ? 'adminPopupTemplate' : ( $this->datas['is_frame'] ) ? 'adminFrameTemplate' : 'adminMainTemplate';
		// $this->view->template_name = $view_template;

		$this->view->render($this->datas);
	}

	function __constructold() {
		$include = array(
			'Lib-Alm',
			'Lib-Admin',
		);

		foreach ($include as $v) 
			require_once LIB_PATH.$v.'.php';

		// change les models kan ils seront impléemntés
		$FLOAT = (isset($_GET['cubefloat']) && $_GET['cubefloat'] == '1');
		$FRAME = (isset($_GET['cubeframe']) && $_GET['cubeframe'] == '1');

		// var_dump($_SERVER);
		$time_start = microtime(true); 
		header('Content-type: text/html; charset=UTF-8');

		if(isset($_GET['deco'])){
			SESS()->clean('adminlg');
			SESS()->clean('adminlog');
			SESS()->clean('adminname');
			SESS()->clean('adminauth');
			header("HTTP/1.1 303 See Other");
			header('Location: '.ADMIN_URL);
			exit;
		}

		$csss = array();
		$csss[] = getFingerPrintUrl('bootstrap3.css', true);
		// $csss[] = getFingerPrintUrl('css/flat-ui.css', true);
		//$csss[] = getFingerPrintUrl('css/bootstrap-post.cyma.css', true);
		$csss[] = getFingerPrintUrl('css/cube.cyma.css', true);
		//$csss[] = getFingerPrintUrl('css/icons.css', true);

		// var_dump(SESS()->exist('adminlog'));
		if(!SESS()->exist('adminlog')){
			if ( $FLOAT )
				include_once LIB_PATH.'assets'.DS.'cube'.DS.'templates'.DS.'maintoolbar-cx.php';
			else
				include LIB_PATH.'admin-template-cx.php';
		}

		$csss[] = getFingerPrintUrl('css/select2.css', true);
		$csss[] = 'http://fonts.googleapis.com/css?family=Oswald';

		// start js processing
		// array of js files, 0=>filename, 1=>is_asset
		$jss = array();
		$jss[] = getFingerPrintUrl('jquery110.js', true);
		// $jss[] = getFingerPrintUrl('js/jquery-ui-1-8-24-custom-min.js', true);
		$jss[] = getFingerPrintUrl('js/jquery-ui-1-10-3-custom-min.js', true);
		$jss[] = getFingerPrintUrl('mootools.js', true);
		$jss[] = getFingerPrintUrl('js/bootstrap.js', true);
		$jss[] = getFingerPrintUrl('js/select2.js', true);
		$jss[] = getFingerPrintUrl('js/vendor/spin-min.js', true);
		$jss[] = getFingerPrintUrl('cube/ckfinder/ckfinder.js', false);
		$jss[] = getFingerPrintUrl('js/test.js', true);
		$jss[] = getFingerPrintUrl('js/test2.js', true);

		$js_assets = array();
		$rep = opendir(ASSET_PATH.'js'.DS.'cube');
		while($file = readdir($rep)){
			if($file == '..' || $file =='.' || $file =='' || strpos($file, '.crc') !== false || strpos($file, 'ccmin.js') !== false) continue;
			$js_assets[] = $file;
		}
		asort($js_assets);
		foreach($js_assets as $jsa){
			$jss[] = getFingerPrintUrl('js/cube/'.$jsa, true);
		}

		$js_vars = array();
		$js_vars[] = 'var CUBE = CUBE || {};';
		$js_vars[] = 'var SITE_ROOT = \''. SITE_ROOT .'\';var ASSET_URL = \''.ASSETS_URL.'\';';
		$js_vars[] = 'var CI = new Object();';
		$js_vars[] = 'var AUTHS = \';'.SESS()->get('adminauth').';\';';
		$js_vars[] = 'var ALM = new Object();';
		// Mots utilisés par js
		$words = array('areyousure','pagehavebeenmodified');
		foreach($words as $w) 
			$js_vars[] = 'ALM[\''.$w.'\'] = \''.Alm::singleton()->L($w).'\';';
		$customCkConfig = (is_file(ROOT_PATH.'site'.DS.'assets'.DS.'configfck.js')) ? 'true' : 'false';
		$js_vars[] = 'var customCkConfig = '.$customCkConfig.';';

		Site::singleton();
		if(isset($_GET['lang'])){
			S()->setLang($_GET['lang']);
		}
		S()->InitAdminCats();

		// new add js from modules
		foreach ( CubeModules::_()->getAdminJss() as $js_data ) {
			$jss[] = getFingerPrintUrl($js_data[0], $js_data[1]);
		}

		// new add css from modules
		foreach ( CubeModules::_()->getAdminCsss() as $css_data ) {
			$csss[] = getFingerPrintUrl($css_data[0], $css_data[1]);
		}

		$js_vars[] = 'var admin_lang = \''.S()->LG->GetLangId().'\';';
		$js_inframe = ( $FRAME ) ? 'true' : 'false';
		$js_vars[] = 'var inframe = '.$js_inframe.';';

		$jss[] = getFingerPrintUrl('js/cube-list.js', true);
		$jss[] = getFingerPrintUrl('js/cube.js', true);

		$head = array();
		$head[] = '
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<title>Admin - '. S()->title .'</title>
		<link rel="shortcut icon" href="'.ASSETS_URL.'css/design/cube.gif" />
		';
		function addCssHtml($src){return '<style type="text/css" title="currentStyle" media="screen">@import "'.$src.'";</style>';}
		$head[] = implode('', array_map('addCssHtml', $csss));
		function addJsHtml($src){return '<script src="'.$src.'" type="text/javascript"></script>';}
		$head[] = '<script type="text/javascript">'.implode('', $js_vars).'</script>';
		$head[] = implode('', array_map('addJsHtml', $jss));
		
		/***********
		Get CAT NODE : priority : GET > @admindefault="1" > cat[@id="site"]
		************/
		$page_node = null;
		$page = (isset($_GET['page']) && strlen($_GET['page'])>0) ? $_GET['page'] : '';
		if ( strlen($page)>0 ) $page_node = S()->cat->getElement('//struct/cat[@id="'.implode('"]/cat[@id="',explode('-',$page)).'"]');
		if ( !$page_node ) $page_node = S()->cat->getElement("//cat[@admindefault='1']");
		if ( !$page_node ) $page_node = S()->cat->getElement("//cat[@id='site']");
		if ( !$page_node ) {echo 'Page non trouvée';exit;}
		if ( $page_node->getAttribute('noadmin') == '1' )
			$page_node = S()->cat->getElement('./cat', $page_node);
		$page_uid = $page_node->getAttribute('uid');
		if ( $page_node ) {
			$page_node->setAttribute('getsel','1');
		}

		$content_data = array();

		$positioncontent = 'Accueil';
		$titrecontent = 'Accueil';
		$action = array();
		$actions_right = array();
		$report = '';
		$datacontent = 'Administration '.S()->getTitle();
		$langhtml = array();
		$js_action = '';
		$is_list_page = false;

		// new admin = model qui renvoi les données
		if ( $page_node && strlen($page_node->getAttribute('model'))>0 ) {
			$form = new Admin($page_node);
			// $positioncontent = implode('',$form->posarr);
			$action = $form->actions;
			$actions_right = $form->actions_right;
			// $titrecontent = $form->titrecontent;
			$datacontent = implode("\r\n",$form->data);
			// if (!empty($form->report)) $form->report = '<img class="reporticon" src="'.ASSETS_URL.'css/imgs/_blank.gif"/>'.$form->report;
			if (!empty($form->report)) $form->report = '<div class="reporttxt">'.$form->report.'</div>';
			$report = $form->report;
			$js_action = $form->jsFunction;
			$langhtml = $form->langhtml;
			$is_list_page = $form->is_list_page;
		}

		$ctitre = ($is_list_page) ? '<div id="listhead"></div>' : ((strlen($titrecontent)>0) ? '<h1>'.$titrecontent.'</h1>' : '');
		$content_data[] = $datacontent;
		$page_id = ($page_node) ? $page_node->getAttribute('id') : '';
		$content_data[] = '<script>currentmenu = \''.$page_id.'\';</script>';

		// read menu

		S()->readCats();

		$page_node = S()->cat->getElement('//cat[@uid="'.$page_uid.'"]');

		// breadcrumb start
		$pos_arr = array();

		/*$pn = $page_node;
		while($pn && $pn->nodeName == 'cat'){
			
			$pn->setAttribute('curcubecat', '1');
			$pos_arr[] = '<a href="?page='.$pn->getAttribute('uid').'&lang='.S()->LG->GetLangId().'">'.txtLineCut($pn->getAttribute('name'), 50).'</a>';
			$pn = $pn->parentNode;
			
		}
		
		// remove main parent : already visible as icon
		array_pop($pos_arr);
		$pos_arr = array_reverse($pos_arr);
		$mc_node = S()->cat->getElement('//struct/cat[@curcubecat="1"]');
		$positioncontent = implode(' <span class="bcico"></span> ',$pos_arr);
		if ( !empty($positioncontent) ) {
			$positioncontent = '  <span class="bcico"></span>  '.$positioncontent;
		}*/
		/// new !!
		$branches = S()->cat->getElements('ancestor-or-self::cat', $page_node);
		$mc_node = array_shift($branches);
		$mc_node->setAttribute('curcubecat', '1');
		$bc_list = array();
		foreach($branches as $branch) {
			$branch->setAttribute('curcubecat', '1');
			$bc_list[] = '<a href="?page='.$branch->getAttribute('uid').'&lang='.S()->LG->GetLangId().'">'.txtLineCut($branch->getAttribute('name'), 50).'</a>';
		}
		if ( sizeof($bc_list) > 0 ) {
			array_unshift($bc_list, ' ');
		}
		$positioncontent = implode(' <span class="bcico"></span> ', $bc_list);
		// breadcrumb end

		$lefttop_menu = '';

		// treemenu start
		$jsmenu = array();
		$i = -1;
		$pid = -1;
		if ( $FLOAT ){
			readCats3(S()->cat->getElement('//struct'), $i, $jsmenu, $pid);
			include_once LIB_PATH.'assets'.DS.'cube'.DS.'templates'.DS.'maintoolbar.php';
			exit;
		}
		if ( $mc_node )
			readCats2($mc_node, $i, $jsmenu, $pid);
		// treemenu end

		// main menu start
		$mc_menu = array();
		foreach(S()->cat->getElements('//struct/cat') as $mc){
			$sel = ( $mc->getAttribute('id') == $mc_node->getAttribute('id') ) ? ' current' : '';
			$mc_name = ucfirst($mc->getAttribute('name'));
			//if ( sizeof($auths) == 0 || in_array($mc->getAttribute('id'), $auth_mc) )
				//$mc_menu[] = '<a href="?page='.$mc->getAttribute('uid').'" class="'.$mc->getAttribute('id').$sel.' btn_50x50 sp" title="'.$mc_name.'" urlid="'.$mc->getAttribute('id').'"></a>';
				$mc_menu[] = '<a href="?page='.$mc->getAttribute('uid').'" data-icon="'.$mc->getAttribute('icon').'" class="'.$sel.' btn_50x50" title="'.$mc_name.'" urlid="'.$mc->getAttribute('id').'" data-behavior="BS.Twipsy"></a>';
		}
		// main menu end
		$left_menu = '
		<div class="mainleftcol">
			<div id="menubg">
				<script>var maintreemenu = [\''.str_replace('>', '>',implode('\',\'',$jsmenu)).'\'];</script>
				<div id="menu">
				</div>
			</div>
		</div>';
				
		if ( $mc_node->getAttribute('id') == 'mediamanager' ){
			$left_menu = '';
			$content_data = array('<div id="contentcadremm"></div>');
		}

		// go to site btn start
		$suf_url = '';
		if ( $mc_node->getAttribute('id') == 'site' && $page_node && $page_node->getAttribute('nositemap') != '1')
			$suf_url = '/'.$page_node->getAttribute('mainwu');
		$view_site_btn = '<a id="btn_showsite" class="btn_52x28" data-icon="&#xe029;" href="'.SITE_ROOT.$suf_url.'" title="'.Alm::singleton()->L('opensite').'" target="_blank"></a>';
		// go to site btn end
		
		if ( is_file(ROOT_PATH.'cube'.DS.'action-postproc.php') )
			require_once ROOT_PATH.'cube'.DS.'action-postproc.php';	

		if ( isset($_GET['ajax']) ){
			include_once LIB_PATH.'admin-template-ajax.php';
			exit;
		}
		echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
		<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr"><head>'.implode('', $head).'</head>';
		if ( isset($_GET['popup']) ) include_once LIB_PATH.'admin-template-popup.php';
		elseif ( $FRAME ) include_once LIB_PATH.'assets'.DS.'cube'.DS.'templates'.DS.'mainframe.php';
		else include_once LIB_PATH.'admin-template-main.php';
		echo '</html>';

		$mu = memory_get_usage(true);
		$mpu = memory_get_peak_usage(true);

		CymaDebug::writeLine($mu);
		CymaDebug::writeLine($mpu);
	}

	
}


?>
