<?PHP


class header_data extends engine{

	var $call_sql_class = array('settings', 'templates', 'company_search', 'heads', 'language_menus');
	var $add_separator = '';

	function get_header_data_data(){

		return $this;
	}
	function get_template_meta_data($column){

		$limit = $column == 'title' ? " LIMIT 1 " : "";

		$this->templates->columns($column." as value")->append();
		if($_GET['model'] == 'pages' && $_GET['id']){

			$this->templates->template_join_menu("WHERE p1.name = '".$_GET['model']."' AND p1.page = '".$_GET['page']."' and p1.language = '".$_SESSION['language']."' AND p2.id = ".$_GET['id']." ".$limit, true);
		}
		else if($_GET['model'] && $_GET['page'] && !$_GET['show_page']){

			$this->templates->template_join_menu("WHERE p1.name = '".$_GET['model']."' AND p1.page = '".$_GET['page']."' and p1.language = '".$_SESSION['language']."' AND p1.page_position LIKE '%".$this->page_position."%' ".$limit, true);
		}
		else if($_GET['model'] && $_GET['page'] && $_GET['show_page']){

			$this->templates->template_join_menu("WHERE p1.name = '".$_GET['model']."' AND p1.page = '".$_GET['page']."' and p1.language = '".$_SESSION['language']."' AND p1.id IN(SELECT CASE WHEN model_id > 1 THEN model_id ELSE template_id END FROM menus WHERE short_name = '".$_GET['show_page']."') AND p1.page_position LIKE '%".$this->page_position."%' ".$limit, true);
		}
		else{

			$this->templates->template_join_menu("WHERE p1.is_first_page = 1 and p1.language = '".$_SESSION['language']."' AND p1.page_position LIKE '%".$this->page_position."%' ".$limit, true);
		}
		if($this->templates->Numrows == 0){

			$this->templates->columns($column." as value")->append();
			$this->templates->template_join_menu("WHERE p1.name = '".$_GET['model']."' AND p1.page = '".$_GET['page']."' and p1.language = '".$_SESSION['language']."' AND p1.page_position LIKE '%".$this->page_position."%' ".$limit, true);
		}
		if($column != 'title'){

			if($this->templates->Rows[0]['show_sub_as_listing'] == 1){

				$parent_id = $this->templates->Rows[0]['id'];
				$this->templates->columns($column." as value")->append();
				$this->templates->template_join_menu("WHERE p1.name = 'pages' AND p2.parent_id = ".$parent_id." AND page_position LIKE '%".$this->page_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'].") ".$limit, true);
			}
			if($this->templates->Numrows <= 0){

				$this->templates->columns($column." as value")->append();
				$this->templates->template_join_menu(
					"WHERE p1.name = 'pages' AND page_position LIKE '%".$this->page_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'].") ".
					($limit ? $limit : "LIMIT 9 "), true);
			}
		}
	}

	function get_meta_keywords_data(){

		$this->settings->Select("WHERE name = 'meta_keywords' AND language = '".$_SESSION['language']."' ");
		$this->get_template_meta_data('meta_keywords');
		$this->add_separator = ', ';

		return $this;
	}

	function get_meta_description_data(){

		$this->settings->Select("WHERE name = 'meta_description' AND language = '".$_SESSION['language']."' ");
		$this->get_template_meta_data('meta_description');
		$this->add_separator = ', ';

		return $this;
	}

	function get_title_data(){

		$this->settings->Select("WHERE name = 'title' AND language = '".$_SESSION['language']."' ");
		$this->get_template_meta_data('title');
		$this->add_separator = ' | ';

		return $this;
	}

	function get_header_data(){

		$this->settings->Select("WHERE name IN('title', 'meta_description', 'meta_keywords') AND language = '".($_GET['get_language'] ? $_GET['get_language'] : $_SESSION['language'])."' ");
		$this->language_menus->Select();

		while($row = $this->settings->Assoc()){

			$this->settings->Data[$row['name']] = $row['value'];
		}

		return $this;
	}

	//UPDATE

	function update(){

		$in_array = array('title', 'meta_description', 'meta_keywords');

		foreach($in_array as $v){

			$this->settings->Select("WHERE name = '".$v."' AND language = '".($_GET['get_language'] ? $_GET['get_language'] : $_SESSION['language'])."' ");
			if($this->settings->Numrows > 0){

				$this->settings->Update(array('name' => $v, 'value' => $_POST[$v], 'language' => ($_GET['get_language'] ? $_GET['get_language'] : $_SESSION['language']), 'created_at' => 'NOW()'), "WHERE name = '".$v."' ");
			}
			else{

				$this->settings->Insert(array('name' => $v, 'value' => $_POST[$v], 'language' => ($_GET['get_language'] ? $_GET['get_language'] : $_SESSION['language']), 'created_at' => 'NOW()'));
			}
		}
	}
}


?>