<?PHP
class content extends engine {
	var $call_sql_class = array('templates', 'templates_history', 'menus', 'roles', 'permissions', 'language_menus', 'settings', 'files');
	var $clone_class = array(
		array('templates' => 'models'),
		array('menus' => 'parent_menus')
	);
	var $call_class = array('ArrayClass', 'File', 'Functions', 'Image', 'Editor');
	var $language_menus_counter = 0;
	var $counter_start = 2;
	var $bgcolor='';
	var $last_is_single = false;
	var $last_is_bottom = false;
	var $defaultMenuPosition = 'left_menu';
	var $defaultContentPosition = 'content';

	function get_content_list_data(){

		$this->banners->Select();
		return $this;
	}
	function show_page(){

		$this->page_name = 'advanced';
		return $this;
	}

	function content_list(){

		$this->get_parent_menu();
		$this->get_models();
		$this->get_roles();

		if($_GET['search']){

			$this->menus->columns("0 AS parent_id, 1 AS is_only")->append();
			$this->menus->recursive_menu("WHERE p1.is_public = 1 and p1.language = '".$_SESSION['language']."' and p1.name LIKE '%".$_GET['search']."%' ".
			"ORDER by p1.queue ", true);
			$this->ContentList = $this->menus->Rows;
		}
		else{

			$this->recursive_content(0, $this->counter_start);
		}
		return $this;
	}

	function manipulate_rows($row){

		$row['space'] = '';
		if($row['counter'] > $this->counter_start){

			$row['space'] = str_repeat('&nbsp;', $row['counter']);
		}
		if($row['parent_id'] > 0){

			$this->templates->Select("WHERE id = ".$row['parent_id'], true);
			$row['model'] = $this->templates->Rows[0]['name'].' - '.$this->templates->Rows[0]['page'];
		}
		$row = $this->up_down($row);
		return $row;
	}

	function up_down($row){

		$row['down'] = '';
		$row['up'] = '';
		$row['down_visible'] = false;
		$row['up_visible'] = false;
		$row['margin_left_down'] = '0';
		$row['margin_left_up'] = '0';
		$left_down = ($row['level'] > 1 ? 10 : 1)*$row['level'];
		$left_up = ($row['level'] > 1 ? 18 : 15)*$row['level'];

		if($row['is_first'] == 1 && $row['is_last'] == 0){

			$row['down'] = '<img src="'._HostTemplate.'/images/admin/sort_desc.png" border=0 style="max-width:15px;" alt="content"/>';
			$row['down_visible'] = true;
			$row['margin_left_down'] = $left_down;
		}
		if($row['is_last'] == 1 && $row['is_first'] == 0){

			$row['up'] = '<img src="'._HostTemplate.'/images/admin/sort_asc.png" border=0 style="max-width:15px;" alt="content"/>';
			$row['up_visible'] = true;
			$row['margin_left_up'] = $left_up;
		}
		if($row['is_first'] == 0 && $row['is_last'] == 0 && $row['is_only'] == 0){

			$row['down'] = '<img src="'._HostTemplate.'/images/admin/sort_desc.png" border=0 style="max-width:15px;" alt="content"/>';
			$row['up'] = '<img src="'._HostTemplate.'/images/admin/sort_asc.png" border=0 style="max-width:15px;" alt="content"/>';
			$row['down_visible'] = true;
			$row['up_visible'] = true;
			$row['margin_left_down'] = $left_down;
			$row['margin_left_up'] = '0';
		}
		if($row['is_only'] == 1){

			$row['down'] = '';
			$row['up'] = '';
			$row['down_visible'] = false;
			$row['up_visible'] = false;
			$row['margin_left_down'] = '0';
			$row['margin_left_up'] = '0';
		}
		return $row;
	}

	function recursive_content($parent_id, $counter, $level = 1){

		$this->menus->columns("(SELECT 1 FROM menus WHERE parent_id = p1.id LIMIT 1) AS have_sub_menus")->append();
		$this->menus->recursive_menu("WHERE p1.is_public = 1 and p1.language = '".$_SESSION['language']."' and p1.parent_id=".$parent_id." ORDER by p1.queue ");

		$result = $this->menus->Result;

		if($this->menus->Numrows > 0){

			while ($row = mysqli_fetch_Assoc($result)){

				$row['counter'] = $counter;
				$row['level'] = $level;
				$this->ContentList[] = $row;
				$this->recursive_content($row['id'], ($counter+1), $level+1);
			}
		}
	}

	function get_parent_menu(){

		$this->parent_menus->columns("id AS value, name");
		$this->parent_menus->Select("WHERE language = '".$_SESSION['language']."' and is_public = 1 ", true);
	}

	function get_models(){

		$this->models->columns("id AS value, CONCAT(name, ' - ', page) AS name");
		$this->models->Select("WHERE page_position = 'public,select' ORDER BY name ", true);
	}

	function get_plugins(){

		$this->File->GetAllFiles(_RootFolder.'/app/plugins');
		$this->plugins = array();
		foreach($this->File->FoldersRecursive as $k => $v){

			if(!preg_match('/plugins\/([a-zA-Z0-9\__]+)\//i', $v['dir'])){

				$this->plugins[] = basename($v['dir']);
			}
		}
		unset($this->File->FoldersRecursive);
	}

	function get_roles(){

		$this->roles->columns('id AS value, name');
		$this->roles->Select("", true);
	}

	function getTemplates(){

		$this->File->GetAllFiles(_CurrentTemplate, '.php', '', 'file', false);
	}

	function edit(){

		$this->get_models();
		$this->get_plugins();
		$this->get_roles();
		$this->getTemplates();

		$this->language_menus->Select();

		$selected_language = $_GET['get_language'] ? $_GET['get_language'] : _defaultLanguage;
		$this->menus->Select("WHERE related_id = ".$_GET['id']." AND language = '".$selected_language."' ");
		if($this->menus->Numrows == 0)
			$selected_language = _defaultLanguage;

		$this->menus->recursive_menu("WHERE p1.related_id = ".$_GET['id']." AND p1.language = '".$selected_language."' ", true);

		$this->parent_menus->Select("WHERE id != ".$_GET['id']." AND language = '".$_SESSION['language']."' and is_public = 1 ");

		//siia tuleb teha default position ja page_position valik

		$this->permissions->columns('role_id');
		if($this->menus->Rows[0]['model_id'] > 0)
			$this->permissions->Select("WHERE table_name = 'templates' AND table_id = ".$this->menus->Rows[0]['model_id'], true);
		else
			$this->permissions->Select("WHERE table_name = 'templates' AND table_id = ".$_GET['template_id'], true);
		if(is_array($this->permissions->Rows)){

			foreach($this->permissions->Rows as $v){

				$this->perm[] = $v['role_id'];
			}
		}
		$this->files->dataSeek();
		$this->files->Select("WHERE related_id = ".$_GET['id']." AND table_name = 'templates' ");
		return $this;
	}

	function register_parent_id(){

		$array = array();
		if($_SESSION['register_parent_id']){

			$array = $_SESSION['register_parent_id'];
		}
		if(!in_Array($_GET['parent_id'], $array)){

			$_SESSION['register_parent_id'][$_GET['parent_id']] = $_GET['parent_id'];
		}
	}

	function unregister_parent_id(){

		$this->parent_id_list[] = $_GET['parent_id'];

		unset($_SESSION['register_parent_id'][$_GET['parent_id']]);
		$this->unregister_parent_id_recursive($_GET['parent_id']);
		die(json_encode($this->parent_id_list));
	}

	function unregister_parent_id_recursive($parent_id){

		$this->menus->menus("WHERE is_public = 1 and language = '".$_SESSION['language']."' and parent_id=".$parent_id." ");

		$result = $this->menus->Result;

		if($this->menus->Numrows > 0){

			while ($row = mysqli_fetch_Assoc($result)){

				unset($_SESSION['register_parent_id'][$row['id']]);
				$this->parent_id_list[] = $row['id'];
				$this->unregister_parent_id_recursive($row['id']);
			}
		}
	}

	function dashboard(){

		$this->templates->template_join_menu("WHERE p2.is_public = 1 and p1.language = '".$_SESSION['language']."' ");
		return $this;
	}


	//updates

	function removeFirefoxBr($content){

		$content = str_replace('&amp;', '&#38;', $content);
		$content = addslashes(str_replace('<br type="_moz" />', '', stripslashes($content)));

		return $content;
	}

	function set_defaults(){

		$_POST['is_link'] = $_POST['is_link'] || $_POST['form_type'] == 'basic' ? 1 : 0;
		$_POST['is_first_page'] = $_POST['is_first_page'] ? 1 : 0;
		$_POST['show_sub_as_listing'] = $_POST['show_sub_as_listing'] ? 1 : 0;
		$_POST['is_big'] = $_POST['is_big'] ? 1 : 0;
		$_POST['is_active'] = $_POST['is_active'] ? 1 : 0;
		$_POST['is_enabled'] = $_POST['is_enabled'] ? 1 : 0;
		$_POST['model_id'] = $_POST['model_id'] ? $_POST['model_id'] : 0;
		$_POST['to_next_menu'] = $_POST['to_next_menu'] ? 1 : 0;
		$_POST['is_static'] = $_POST['is_static'] ? 1 : 0;
	}

	function insert(){

		if(strlen($_POST['append']['name']) > 0){

			foreach($_POST['append'] as $k => $v){

				$_POST[$k] = $v;
			}
		}

		$_POST['type'] = 'PAGE';
		$_POST['page_position'] = 'public';
		$_POST['title'] = !$_POST['title'] ? $_POST['name'] : $_POST['title'];
		$_POST['language'] = $_GET['get_language'] ? $_GET['get_language'] : _defaultLanguage;
		$_POST['content'] = $this->removeFirefoxBr($_POST['content']);
		$_POST['parent_id'] = $_GET['action'] == 'append' ? $_GET['id'] : 0;
		$_POST['position'] = !$_POST['position'] ? $this->defaultMenuPosition : $_POST['position'];
		$_POST['meta_description'] = $_POST['meta_description'] ? $_POST['meta_description'] : trim(substr(strip_tags($_POST['content']), 0, 350));
		$_POST['meta_keywords'] = $_POST['meta_keywords'] ? $_POST['meta_keywords'] : strtolower(str_replace('.,', ',', str_replace(',,', ',', str_replace(' ', ',', trim(substr(strip_tags($_POST['content']), 0, 350))))));

		$array = $_POST;
		$array['name'] = 'pages';
		$array['short_name'] = trim(str_replace(' ', '_', $_POST['name']));
		//$array['page'] = trim(strtolower(str_replace(' ', '_', $_POST['name'])));
		$array['page'] = 'page';
		$array['position'] = $this->defaultContentPosition;
		$_POST['is_active'] = $_POST['is_active'] ? 1 : 0;
		$_POST['is_link'] = 1;

		$this->templates->Insert($array);
		$_POST['template_id'] = $this->templates->Insertid;
		$this->insert_template_permissions($_POST['template_id']);

		$this->menus->Select("WHERE language = '".$_POST['language']."' and parent_id = ".$_POST['parent_id']." and is_public = 1 ".
			"ORDER BY queue DESC ".
			"LIMIT 1 ", true);

		$array = $_POST;
		$array['queue'] = $this->menus->Rows[0]['queue']+1;
		$array['is_public'] = 1;

		$this->menus->Insert($array);
		$_POST['menu_id'] = $this->menus->Insertid;
		$this->menus->Update(array('related_id' => $_POST['menu_id']), "WHERE id = ".$_POST['menu_id']);
		$this->generateRedirectUrl($_POST['menu_id']);
	}

	function update(){

		$template_id = $_GET['template_id'];
		$menu_id = $_GET['id'];
		$selected_language = $_GET['get_language'] ? $_GET['get_language'] : _defaultLanguage;
		$_POST['content'] = $this->removeFirefoxBr($_POST['content']);
		if(strlen(trim($_POST['content_source'])) > 10)
			$this->content_source();

		$_POST['meta_description'] = $_POST['meta_description'] ? $_POST['meta_description'] : trim(substr(strip_tags($_POST['content']), 0, 350));
		$_POST['meta_keywords'] = $_POST['meta_keywords'] ? $_POST['meta_keywords'] : strtolower(str_replace('.,', ',', str_replace(',,', ',', str_replace(' ', ',', trim(substr(strip_tags($_POST['content']), 0, 350))))));
		$_POST['url'] = $_POST['url'] == 'http://' ? '' : $_POST['url'];
		$_POST['is_link'] = 1;

		$this->menus->Select("WHERE related_id = ".$menu_id." AND language = '".$selected_language."' ", true);
		if($this->menus->Numrows > 0){

			$menu_id = $this->menus->Rows[0]['id'];
			$template_id = $this->menus->Rows[0]['template_id'];
			$model_id = $_POST['model_id'];
			$_POST['short_name'] = trim(strtolower(str_replace(' ', '_', $_POST['name'])));
			$_POST['page'] = 'page';

			$this->menus->Update($_POST, "WHERE id = ".$menu_id);

			unset($_POST['position']);
			$_POST['name'] = 'pages';
			$_POST['position'] = $_POST['content_position'] ? $_POST['content_position'] : $this->defaultContentPosition;
			$_POST['template_id'] = $template_id;
			$this->templates_history->Insert($_POST);
			$this->templates->Update($_POST, "WHERE id = ".$template_id);
			if($model_id > 0){

				$this->templates->Update(array(
					'position' => $_POST['position'],
					'template' => $_POST['template'],
					'title' => $_POST['title'],
					'meta_description' => $_POST['meta_description'],
					'meta_keywords' => $_POST['meta_keywords']
				), "WHERE id = '".$model_id."'");
				$this->menus->Update(array('model_id' => $model_id), "WHERE related_id = ".$_POST['related_id']);
			}
			if($model_id <= 0 && $_POST['prev_model_id'] > 0){

				$this->templates->Update(array(
					'template' => '',
					'title' => '',
					'meta_description' => $_POST['meta_description'],
					'meta_keywords' => $_POST['meta_keywords'],
				), "WHERE name = '".$_POST['prev_model_id']."'");
				$this->menus->Update(array('model_id' => 0), "WHERE related_id = ".$_POST['related_id']);
			}

			$this->insert_template_permissions($template_id);

			/*if($model_id > 0){

				die($this->Redirect(_RootUrl.'/?action=reassign'));
			}*/

			if($_POST['is_first_page']){

				$this->templates->Update(array('is_first_page' => 0), "WHERE type = 'PAGE' AND language = '".$selected_language."' ");
				$this->templates->Update(array('is_first_page' => 1), "WHERE id = ".$template_id);
			}
			if($_POST['is_big']){

				$this->menus->Update(array('is_big' => 0), "WHERE is_public = 1 ");
				$this->menus->Update(array('is_big' => 1), "WHERE id = ".$_GET['id']);
			}
			$this->generateRedirectUrl($_GET['id']);
		}
		else{

			$this->insert();
			$this->menus->Update(array('related_id' => $menu_id), "WHERE id = ".$_POST['menu_id']);
		}
		if($_POST['update_and_out']){

			die($this->Redirect('?model=content&page=content_list'));
		}
	}

	function generateRedirectUrl($menud_id){

		$this->menus->columns("menus.*, templates.name as model, templates.page, CASE WHEN
				menus.to_next_menu = 1 AND (SELECT p1.id FROM menus AS p1 WHERE p1.parent_id = menus.id ORDER BY p1.queue DESC LIMIT 1 ) IS NOT NULL THEN
				(SELECT p1.name FROM menus AS p1 WHERE p1.parent_id = menus.id ORDER BY p1.queue DESC LIMIT 1 ) ELSE
				menus.name
			END AS url_menu_name ");
		$this->menus->Select(
			"JOIN templates ON CASE WHEN menus.model_id > 1 THEN templates.id = menus.model_id ELSE templates.id = menus.template_id END ".
			"WHERE related_id = ".$menud_id
		);
		while($row = $this->menus->Assoc()){

			$redirect_url = !$_POST['url'] ? str_replace(_RootUrl.'/', '', $this->Link('?model='.$row['model'].'&page='.$row['page'].'&language='.$row['language'].'&menu_id='.$row['id'].'&show_page='.$row['url_menu_name'], array('menu_id', 'language'), true)) : '';
			$this->menus->Update(array('redirect_url' => $redirect_url), "WHERE id = ".$row['id']);
		}
	}

	function content_source(){

		$content = '';
		$content_source = ($_POST['content_source']);

		preg_match_all('/\{\[TEXT=(.*?)\]\}/s', $content_source, $matches);

		if(!empty($matches[1]) && is_array($_POST['content_source_result']) && !$_POST['clear_source']){

			$ret_source = $content_source;
			$ret_content = $content_source;
			foreach($matches[1] as $k => $v){

				if($_POST['content_source_result'][$k]){

					$ret_source = str_replace($v, $_POST['content_source_result'][$k], $ret_source);
					$ret_content = str_replace('{[TEXT='.$v.']}', $_POST['content_source_result'][$k], $ret_content);
				}
				else{

					$ret_content = str_replace('{[TEXT='.$v.']}', $v, $ret_content);
				}
			}
			$content_source = $ret_source;
			$content = $ret_content;
		}
		else{

			$content = $_POST['content_source'];
			$content_source = $_POST['content_source'];

			$content = str_replace('{[TEXT=', '', $content);
			$content = str_replace(']}', '', $content);
		}
		$content = preg_replace("/<p>(.*?)&nbsp;<\/p>/s", '', $content);
		//$this->pre($_POST);
		/*echo '<br><br>';
		echo htmlspecialchars($content);
		echo '<br><br>';
		die(htmlspecialchars($_POST['content_source']));*/

		$content = $this->removeFirefoxBr($content);
		$content_source = $this->removeFirefoxBr($content_source);
		$_POST['content'] = $content;
		$_POST['content_source'] = $content_source;
	}

	function disable_menu(){

		$this->menus->Update(array('is_enabled' => 0), "WHERE id = ".$_GET['id']);
	}

	function enable_menu(){

		$this->menus->Update(array('is_enabled' => 1), "WHERE id = ".$_GET['id']);
	}

	function disable_template(){

		$this->templates->Update(array('is_active' => 0), "WHERE id = ".$_GET['id']);
	}

	function enable_template(){

		$this->templates->Update(array('is_active' => 1), "WHERE id = ".$_GET['id']);
	}

	function enable_next_menu(){

		$this->menus->Update(array('to_next_menu' => 1), "WHERE id = ".$_GET['id']);
	}

	function disable_next_menu(){

		$this->menus->Update(array('to_next_menu' => 0), "WHERE id = ".$_GET['id']);
	}

	function enable_sub_menu_listing(){

		$this->menus->Update(array('show_sub_as_listing' => 1), "WHERE id = ".$_GET['id']);
	}

	function disable_sub_menu_listing(){

		$this->menus->Update(array('show_sub_as_listing' => 0), "WHERE id = ".$_GET['id']);
	}

	function enable_firstpage_menu(){

		$this->templates->Update(array('is_first_page' => 0), "WHERE type = 'PAGE' ");
		$this->templates->Update(array('is_first_page' => 1), "WHERE id = ".$_GET['id']);
	}

	function disable_firstpage_menu(){

		$this->templates->Update(array('is_first_page' => 0), "WHERE id = ".$_GET['id']);
	}

	function insert_file(){

		if($_FILES['file']['size'] > 0){

			$this->File->UploadFile('file', _RootFolder.'/files/page', $_FILES['file']['name'], '', 0);
			$_POST['file_name'] = $_FILES['file']['name'];
			$_POST['is_visible'] = 1;
			$this->files->Insert($_POST);
		}
		if($_FILES['image']['size'] > 0){

			$this->File->UploadFile('image', _RootFolder.'/files/page', $_FILES['image']['name'], '', 0);
			$this->Image->imageresize(_RootFolder.'/files/page/'.$_FILES['image']['name'], $_FILES['image']['name'], 1000, 1000, _RootFolder.'/files/page/', false);
			$_POST['file_name'] = $_FILES['image']['name'];
			$_POST['is_visible'] = $_POST['is_visible'] ? 1 : 0;
			$this->files->Insert($_POST);
		}
		if($_POST['image_url']){

			$fileContent = file_get_contents($_POST['image_url']);
			file_put_contents(_RootFolder.'/files/page/'.basename($_POST['image_url']), $fileContent);
			$this->Image->imageresize(_RootFolder.'/files/page/'.basename($_POST['image_url']), basename($_POST['image_url']), 1000, 1000, _RootFolder.'/files/page/', false);
			$_POST['file_name'] = basename($_POST['image_url']);
			$_POST['is_visible'] = $_POST['is_visible'] ? 1 : 0;
			$this->files->Insert($_POST);
		}
		if($_POST['file_url']){

			$fileContent = file_get_contents($_POST['file_url']);
			file_put_contents(_RootFolder.'/files/page/'.basename($_POST['file_url']), $fileContent);
			$_POST['file_name'] = basename($_POST['file_url']);
			$_POST['is_visible'] = 1;
			$this->files->Insert($_POST);
		}
		$_POST['image'] = '';
		if($_POST['is_menu_image']){

			$_POST['image'] = $_POST['file_name'];
		}
		$this->update();
	}

	function update_file(){

		/*
		$this->pre($_FILES);
		$this->pre($_POST);
		exit;
		*/

		$files = $_FILES;
		$post = $_POST;
		foreach($post['current_file_name'] as $k => $file){

			if(!$post['is_menu_image'][$k]){

				$_POST['image'] = '';
			}
		}
		foreach($files['file_upload']['name'] as $k => $file){

			unset($_POST['file_name']);

			$_FILES['file_upload']['name'] = $files['file_upload']['name'][$k];
			$_FILES['file_upload']['type'] = $files['file_upload']['type'][$k];
			$_FILES['file_upload']['tmp_name'] = $files['file_upload']['tmp_name'][$k];
			$_FILES['file_upload']['error'] = $files['file_upload']['error'][$k];
			$_FILES['file_upload']['size'] = $files['file_upload']['size'][$k];

			if($_FILES['file_upload']['size'] > 0){

				$this->File->UploadFile('file_upload', _RootFolder.'/files/page', $_FILES['file_upload']['name'], '', 0);
				if($_POST['update_image'])
					$this->Image->imageresize(_RootFolder.'/files/page/'.$_FILES['file_upload']['name'], $_FILES['file_upload']['name'], 1000, 1000, _RootFolder.'/files/page/', false);
				$_POST['file_name'] = $_FILES['file_upload']['name'];
			}
			if($_POST['file_upload_url'][$k]){

				$fileContent = file_get_contents($_POST['file_upload_url'][$k]);
				file_put_contents(_RootFolder.'/files/page/'.basename($_POST['file_upload_url'][$k]), $fileContent);
				if($_POST['update_image'])
					$this->Image->imageresize(_RootFolder.'/files/page/'.basename($_POST['file_upload_url'][$k]), basename($_POST['file_upload_url'][$k]), 1000, 1000, _RootFolder.'/files/page/', false);
				$_POST['file_name'] = basename($_POST['file_upload_url'][$k]);
			}
			$_POST['description'] = $post['description'][$k] ? $post['description'][$k] : '';
			$_POST['is_main'] = $post['is_main_update'][$k] ? 1 : 0;
			$_POST['is_visible'] = $post['is_visible_update'][$k] ? 1 : 0;
			if($post['is_menu_image'][$k]){

				$_POST['image'] = $_POST['file_name'] ? $_POST['file_name'] : $post['current_file_name'][$k];
			}
			unset($_POST['related_id']);
			unset($_POST['table_name']);
			unset($_POST['file_type']);
			$this->files->Update($_POST, "WHERE id = ".$k);
		}
		$this->update();
	}

	function delete_file(){

		$this->menus->Update(array('image' => ''), "WHERE id = ".$_GET['id']);

		$this->files->Delete("WHERE id = ".$_GET['file_id']);
	}

	function insert_template_permissions($id){

		if($_POST['form_type'] == 'basic'){

			$this->permissions->Delete("WHERE table_name = 'templates' AND table_id = ".$id);
			$this->roles->Select();
			while($row = $this->roles->Assoc()){

				$array = array(
					'role_id'	 => $row['id'],
					'table_name' => 'templates',
					'table_id' => $id,
					'created_at' => 'NOW()'
				);

				$this->permissions->Insert($array);
			}
		}

		if($_POST['permissions']){

			$this->permissions->Delete("WHERE table_name = 'templates' AND table_id = ".$id);
			foreach($_POST['permissions'] as $v){

				$array = array(
					'role_id'	 => $v,
					'table_name' => 'templates',
					'table_id' => $id,
					'created_at' => 'NOW()'
				);

				$this->permissions->Insert($array);
			}
		}
	}

	function save(){

		$_POST['content'] = str_replace('&amp;', '&', $_POST['fckeditor']);
		$_POST['content'] = $this->removeFirefoxBr($_POST['content']);

		$this->templates->Update($_POST, "WHERE id = ".$_POST['id']);
	}

	function publish(){

		$_POST['content'] = str_replace('&amp;', '&', $_POST['fckeditor']);
		$_POST['content'] = $this->removeFirefoxBr($_POST['content']);

		$this->File->WriteFile(_PageFolder.'/'.md5($_POST['id']).'.html', stripslashes($_POST['content']));
	}

	function update_first_page(){

		if($_POST['is_first_page'] == 1)
			$this->templates->Update(array('is_first_page' => 0), " WHERE language = '".$_POST['selected_language']."' and type = 'PAGE' ");
	}

	function delete(){

		$this->menus->Select("WHERE id = ".$_GET['id'], true);
		$parent_id = $this->menus->Rows[0]['parent_id'];
		$template_id = $this->menus->Rows[0]['template_id'];

		if($template_id){

			$this->permissions->Delete("WHERE table_name = 'templates' and table_id = ".$template_id);
			$this->templates->Delete("WHERE id = ".$template_id);
			$this->menus->Delete("WHERE id = ".$_GET['id']);
			$this->correct_queue($parent_id);
			$this->delete_recursive($_GET['id']);
		}
	}

	function delete_recursive($parent_id){

		$this->menus->menus("WHERE is_public = 1 and language = '".$_SESSION['language']."' and parent_id=".$parent_id." ");

		$result = $this->menus->Result;

		if($this->menus->Numrows > 0){

			while ($row = mysqli_fetch_Assoc($result)){

				$this->permissions->Delete("WHERE table_name = 'templates' and table_id = ".$row['template_id']);
				$this->templates->Delete("WHERE id = ".$row['template_id']);
				$this->menus->Delete("WHERE id = ".$row['id']);
				$this->correct_queue($row['parent_id']);
				$this->delete_recursive($row['id']);
			}
		}
	}

	function up(){

		if(($_GET['queue']-1) > 0){

			$new_queue = $_GET['queue']-1;
			$next_menu_queue = $_GET['queue'];

			$this->menus->Select("WHERE id=".$_GET['id']." and language = '".$_SESSION['language']."' ", true);

			if($this->menus->Numrows > 0){

				$parent_id = $this->menus->Rows[0]['parent_id'];
				$is_public = $this->menus->Rows[0]['is_public'];

				$this->menus->Update(array('queue' => $next_menu_queue, 'changed_at' => 'NOW()'), "WHERE queue=".$new_queue." and language = '".$_SESSION['language']."' and parent_id=".$parent_id." and is_public = ".$is_public);
				$this->menus->Update(array('queue' => $new_queue, 'changed_at' => 'NOW()'), "WHERE id=".$_GET['id']);
			}
		}
	}

	function down(){

		$new_queue = $_GET['queue']+1;
		$next_menu_queue = $_GET['queue'];

		$this->menus->Select("WHERE id=".$_GET['id']." and language = '".$_SESSION['language']."' ", true);

		$parent_id = $this->menus->Rows[0]['parent_id'];
		$is_public = $this->menus->Rows[0]['is_public'];

		$this->menus->Update(array('queue' => $next_menu_queue, 'changed_at' => 'NOW()'), "WHERE queue=".$new_queue." and language = '".$_SESSION['language']."' and parent_id=".$parent_id." and is_public = ".$is_public);
		$this->menus->Update(array('queue' => $new_queue, 'changed_at' => 'NOW()'), "WHERE id=".$_GET['id']);
	}

	function move_menu(){

		$this->pre($_GET);
		$prevMenuParent = 0;
		$currentMenuParent = 0;
		$nextMenuParent = 0;
		$curMenuParent = 0;
		$prevMenuQueue = 0;
		$currentMenuQueue = 0;
		$nextMenuQueue = 0;

		if($_GET['prevMenu']){

			$this->menus->Select("WHERE id = ".$_GET['prevMenu'], true);
			$prevMenuParent = $this->menus->Rows[0]['parent_id'];
			$prevMenuQueue = $this->menus->Rows[0]['queue'];
		}
		if($_GET['currentMenu']){

			$this->menus->Select("WHERE id = ".$_GET['currentMenu'], true);
			$currentMenuParent = $this->menus->Rows[0]['parent_id'];
			$currentMenuQueue = $this->menus->Rows[0]['queue'];
		}
		if($_GET['nextMenu']){

			$this->menus->Select("WHERE id = ".$_GET['nextMenu'], true);
			$nextMenuParent = $this->menus->Rows[0]['parent_id'];
			$nextMenuQueue = $this->menus->Rows[0]['queue'];
		}

		echo $prevMenuParent.'['.$currentMenuParent.']'.$nextMenuParent."\n";

		//ting1
		if($prevMenuParent == $nextMenuParent){

			echo("\n".'jah1=queue'.$nextMenuQueue);
			echo $this->menus->Query("UPDATE menus SET queue=queue+1 WHERE parent_id = ".$nextMenuParent." AND queue >= ".$nextMenuQueue);
			$this->menus->Query("UPDATE menus SET queue=".$nextMenuQueue.", parent_id = ".$nextMenuParent." WHERE id = ".$_GET['currentMenu']);
			$curMenuParent = $nextMenuParent;
		}
		//ting2
		if($prevMenuParent < $nextMenuParent){

			echo("\n".'jah2=queue'.$nextMenuQueue);
			$this->menus->Query("UPDATE menus SET queue=queue+1 WHERE parent_id = ".$nextMenuParent." AND queue > ".$nextMenuQueue);
			$this->menus->Query("UPDATE menus SET queue=".$nextMenuQueue.", parent_id = ".$nextMenuParent." WHERE id = ".$_GET['currentMenu']);
			$curMenuParent = $nextMenuParent;
		}
		//ting3
		if($prevMenuParent > $nextMenuParent && $currentMenuParent != $nextMenuParent){

			echo("\n".'jah3=queue'.$prevMenuQueue);
			$this->menus->Query("UPDATE menus SET queue=queue-1 WHERE parent_id = ".$currentMenuParent." AND queue <= ".$prevMenuQueue);
			$this->menus->Query("UPDATE menus SET queue=".$prevMenuQueue.", parent_id = ".$currentMenuParent." WHERE id = ".$_GET['currentMenu']);
			$curMenuParent = $currentMenuParent;
		}
		//ting4
		if($prevMenuParent > $nextMenuParent && $currentMenuParent == $nextMenuParent){

			echo("\n".'jah4=queue'.$nextMenuQueue);
			$this->menus->Query("UPDATE menus SET queue=queue+1 WHERE parent_id = ".$nextMenuParent." AND queue >= ".$nextMenuQueue);
			$this->menus->Query("UPDATE menus SET queue=".$nextMenuQueue.", parent_id = ".$nextMenuParent." WHERE id = ".$_GET['currentMenu']);
			$curMenuParent = $nextMenuParent;
		}
		$this->correct_queue($curMenuParent);
		$this->correct_queue($currentMenuParent);
		exit;
	}

	function correct_queue($parent_id=0){

		$parent_id = !is_numeric($parent_id) ? 0 : $parent_id;

		$this->menus->Select("WHERE parent_id=".$parent_id." and language = '".$_SESSION['language']."' and is_public=1 ORDER BY queue ", true);

		$i=1;

		if(is_Array($this->menus->Rows)){

			foreach($this->menus->Rows as $v){

				$this->menus->Update(array('queue' => $i), "WHERE id=".$v['id']);
				$i++;
			}
		}
	}
}



?>