<?PHP
class pages extends engine {
	var $call_sql_class = array('templates', 'menus', 'language_menus', 'files', 'permissions');
	var $call_class = array('Obj');
	var $is_big = 0;

	function get_page_data(){

		return $this;
	}

	function get_pages($position){

		$this->page = 'page';
		$this->is_big = 0;

		$this->permissions->Select(
			"JOIN templates AS p2 ON p2.id = permissions.table_id AND permissions.table_name = 'templates' ".
			"WHERE `name` LIKE 'content' AND permissions.role_id = ".$_SESSION['UserData']['role_id']." ".
			"LIMIT 1"
		);

		if($_GET['search'] && $position == 'content'){

			$this->templates->useMethod(array($this, 'get_images'));
			$this->templates->template_join_menu("WHERE p1.name = 'pages' AND p1.position = '".$position."' and p1.language = '".$_SESSION['language']."' and p2.model_id = 0 and p1.is_active = 1 AND
				(CONCAT(p1.content, ' ', p2.name) LIKE '%".$_GET['search']."%' OR p1.is_static = 1)
			and exists(SELECT 1 FROM permissions WHERE table_name = 'templates' AND table_id = p1.id AND role_id = ".$_SESSION['UserData']['role_id'].") ", true);
			$this->is_big = 1;
			if($this->templates->Numrows <= 0){

				$this->page = 'no_search_result';
			}
			return $this;
		}

		if($_GET['model'] == 'pages' && $_GET['menu_id']){

			$this->templates->useMethod(array($this, 'get_images'));
			$this->templates->template_join_menu("WHERE p1.name = 'pages' AND (p2.id = ".$_GET['menu_id']." OR p1.is_static = 1) AND p1.position = '".$position."' and p1.language = '".$_SESSION['language']."' and p2.model_id = 0 and p1.is_active = 1 and exists(SELECT 1 FROM permissions WHERE table_name = 'templates' AND table_id = p1.id AND role_id = ".$_SESSION['UserData']['role_id'].") ", true);
		}
		if($_GET['model'] == 'pages' && !$_GET['menu_id'] && $_GET['show_page']){

			$this->templates->useMethod(array($this, 'get_images'));
			$this->templates->template_join_menu("WHERE p1.name = 'pages' AND (p2.name like '".$_GET['show_page']."' OR p1.is_static = 1) AND p1.position = '".$position."' and p1.language = '".$_SESSION['language']."' and p2.model_id = 0 and p1.is_active = 1 ", true);
		}
		if(!$_GET['model'] && !$_GET['page'] && !$_GET['menu_id'] && !$_GET['show_page']){

			$this->templates->useMethod(array($this, 'get_images'));
			$this->templates->template_join_menu("WHERE p1.name = 'pages' AND p1.position = '".$position."' and p1.language = '".$_SESSION['language']."' and p2.model_id = 0 and (p1.is_first_page = 1 OR p1.is_static = 1) and p1.is_active = 1 and exists(SELECT 1 FROM permissions WHERE table_name = 'templates' AND table_id = p1.id AND role_id = ".$_SESSION['UserData']['role_id'].") ", true);
		}
		if($this->templates->Numrows <= 0){

			$parent_id = $this->templates->Rows[0]['id'];
			$this->templates->useMethod(array($this, 'get_images'));
			$this->templates->template_join_menu(
				"WHERE p1.name = 'pages' AND p1.position = '".$position."' and p2.parent_id > 0 and p1.language = '".$_SESSION['language']."' and p2.model_id = 0 and p1.is_active = 1 and exists(SELECT 1 FROM permissions WHERE table_name = 'templates' AND table_id = p1.id AND role_id = ".$_SESSION['UserData']['role_id'].") ".
				"ORDER BY is_big DESC, p2.queue ASC ".
				"LIMIT 9 ", true);
		}

		/*if($this->templates->Numrows <= 0){

			$this->page = 'no_result';
		}*/
		return $this;
	}

	function breadcrumbs($row){

		$this->menus->menus("WHERE id = ".$row['parent_id']);
		$result = $this->menus->Result;

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

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

				$this->breadcrumbs[] = $row;
				//$this->breadcrumbs($row);
			}
		}
		return $row;
	}

	function get_images($row){

		$this->files->Select("WHERE related_id = ".$row['id']." AND table_name = 'templates' AND file_type = 'IMAGE' ");
		/*$this->breadcrumbs = array();
		$this->breadcrumbs($row);
		$this->breadcrumbs[] = $row;*/

		if($_GET['search']){

			$row['is_big'] = $this->is_big;
		}
		return $row;
	}
}
?>