<?PHP

class templates_SQL extends SqlRequest{

	var $table = 'templates';

	function templates($add_sql = '', $execute = false){

		$this->LINE = __FILE__.' '.__LINE__;
		return $this->Select("select * from ".$this->table." ".
			$add_sql,
			$execute
		);
	}

	function template_join_menu($add_sql = '', $execute = false){

		$this->LINE = __FILE__.' '.__LINE__;
		return $this->Select("SELECT p1.*, p2.*, p2.id AS menu_id, DATE_FORMAT(p2.active_at, '%d.%m.%Y') AS active_at, DATE_FORMAT(p2.un_active_at, '%d.%m.%Y') AS un_active_at, CONCAT(p1.name, ' - ', page) AS model, p1.name AS model_name, p2.show_sub_as_listing,
		CASE WHEN
				p2.queue = (select min(queue) from menus where parent_id=p2.parent_id and is_public =1 and language='".$_SESSION['language']."' group by parent_id) THEN
				1 ELSE
				0 END as is_first,
			CASE WHEN
				p2.queue = (select max(queue) from menus where parent_id=p2.parent_id and is_public =1 and language='".$_SESSION['language']."' group by parent_id) THEN
				1 ELSE
				0 END as is_last,
			CASE WHEN
				(select count(id) from menus where parent_id=p2.parent_id and is_public=1 and language='".$_SESSION['language']."' group by parent_id) > 1 THEN
				0 ELSE
				1 END as is_only,

		CASE WHEN p2.url THEN p2.url ELSE (

			SELECT CASE WHEN p2_1.parent_id > 0 THEN CONCAT('?model=', p2_2.name, '&page=', p2_2.page) ELSE CONCAT('?model=pages', '&page=', p2_1.name) END AS url FROM `templates` as p2_1
			LEFT JOIN `templates` as p2_2 on p2_2.id = p2_1.parent_id
			WHERE p2_1.id = p1.id

		) END as base_url
		FROM ".$this->table." AS p1 ".
			"JOIN menus AS p2 ON CASE WHEN p2.model_id > 1 THEN p1.id = p2.model_id ELSE p1.id = p2.template_id END ".
			$add_sql,
			$execute
		);
	}

	function models(){

		$this->LINE = __FILE__.' '.__LINE__;
		return $this->Select(
			"select * from ".$this->table." ".
			"where type = 'MODEL' ".
			"group by name ".
			"order by name "
		);
	}

	function page($add_sql = '', $execute = false){

		$this->LINE = __FILE__.' '.__LINE__;
		return $this->Select(
			"SELECT templates.id, templates.name, templates.content, templates.type, templates.queue FROM templates ".
			"JOIN permissions p2 ON p2.table_name = 'templates' and p2.table_id = templates.id ".
		$add_sql,
		$execute
		);
	}

	function page_by_model($add_sql = '', $execute = false){

		$this->LINE = __FILE__.' '.__LINE__;
		return $this->Select(
			"SELECT p2.id, p2.name, p2.content, p2.type, p2.queue FROM templates  p1 ".
			"LEFT JOIN templates p2 ON p2.parent_id = p1.id ".
			"JOIN permissions p3 ON p3.table_name = 'templates' and p3.table_id = p1.id ".
		$add_sql,
		$execute
		);
	}

	function template_queue($add_sql = '', $execute = false){

		$this->LINE = __FILE__.' '.__LINE__;
		return $this->Select(
			"SELECT p1.*,
			CASE WHEN
				p1.queue = (select min(queue) from templates where parent_id=p1.parent_id and page_position LIKE '%public%' and is_static=1 group by parent_id) THEN
				1 ELSE
				0 END as is_first,
			CASE WHEN
				p1.queue = (select max(queue) from templates where parent_id=p1.parent_id and page_position LIKE '%public%' and is_static=1 group by parent_id) THEN
				1 ELSE
				0 END as is_last,
			CASE WHEN
				(select count(id) from templates where parent_id=p1.parent_id and page_position LIKE '%public%' and is_static=1 group by parent_id) > 1 THEN
				0 ELSE
				1 END as is_only
			FROM templates  as p1 ".
		$add_sql,
		$execute
		);
	}

	function template_and_other_language($add_sql = '', $execute = false){

		$this->LINE = __FILE__.' '.__LINE__;
		return $this->Select(
			"SELECT p1.* FROM templates  p1 ".
			"JOIN templates p2 ON p2.name = p1.name ".
		$add_sql,
		$execute
		);
	}

}
?>