<?php
/*
	DevSaver Web Framework
	Copyright (c) 2013-2016 DevSaver. 
	All rights reserved.
		web:  www.devsaver.com
		mail: support@devsaver.com		
*/

// dependencies

/**
* description
*
* @library	
* @author	
* @since	
*/
class CTeam extends CPlugin{
	
	var $tplvars; 

	function __construct() {
		//$this->CPlugin($db, $tables , $templates);
	}

	function DoEvents(){
		global $base, $_CONF, $_TSM , $_VARS , $_USER , $_BASE , $_SESS;

		parent::DoEvents();

		if ($_GET["mod"] == "team") {


			$this->__init();
			$this->plugins["globalhooks"]->SetModule($this->tpl_module);

			switch ($_GET["sub"]) {
				case "detect":
					return $this->Landing();
				break;	

				case "category":
					return $this->Category();
				break;

			}
		}
		
	}

	function __init() {
		global $_CONF;

		if ($this->__inited) {
			return "";
		}

		$this->__inited = true;
		
		$path = $this->tpl_path;

		$templates = array(
			"categories"			=> "categories.htm",
			"onepage"				=> "onepage.htm",
			"category"				=> "category.htm",
			"items"					=> "items.htm",
		);

		foreach ($templates as $key => $val) {
			$this->private->templates[$key] = new CTemplateDynamic(
				$path . $val
			);
		}

		$this->tpl_module = $this->module->plugins["modules"]->LoadModuleInfo();
	} 


	/**
	* description
	*
	* @param
	*
	* @return
	*
	* @access
	*/
	function Landing() {
		global $base;

		if (STP_ENABLE_ONEPAGE) {
			$this->tpl_module["settings"]["set_style"] = "2";
		}
		

		switch ($this->tpl_module["settings"]["set_style"]) {
			//categories listing
			default:
				return $this->Categories();
			break;

			//skip listing
			case "1":
				$this->Detect();
				return $this->Category();
			break;

			//onepage
			case "2":
				return $this->OnePage();
			break;

		}
		
	}
	


	/**
	* description
	*
	* @param
	*
	* @return
	*
	* @access
	*/
	function OnePage() {

		global $base;
		

		$text = $this->pageTitleBlock();			
		$this->plugins["seo"]->PageMeta($text);



		$items = $this->db->QFetchRowArray("
			SELECT * FROM
				{$this->tables['plugin:team_cats']} as c , 
				{$this->tables['plugin:team_cats_lang']} as l,
				{$this->tables['plugin:team_items']} as i,
				{$this->tables['plugin:team_items_lang']} as il

			WHERE
				c.cat_id = l.cat_id AND 
				c.module_id = {$this->tpl_module[mod_id]} AND 
				l.lang_id = il.lang_id AND 
				c.cat_status = 1 AND

				i.item_id = il.item_id AND 
				il.lang_id = {$this->tpl_module[lang_id]} AND
				i.item_status = 1 AND 

				i.item_cat = c.cat_id
				
			ORDER BY
				cat_order ASC,
				item_order ASC
		");

		$items = $this->ProcessItems($items , "" , $this->_t("items"));

		$template = &$this->private->templates["onepage"];

		$this->SetComments($this->tpl_module["settings"]["set_comments"], array("link" => $this->tpl_module["link"]));


		return $this->module->plugins["layouts"]->Render(
			$this->plugins["layouts"]->Detect(
				$this->tpl_module["settings"]["set_layout"],
				$this->tpl_module["mod_protected"]
			),
			$template->blockREplacE(
				"Main" , 
				array(

					"items"	=> $this->SortItems($items),
					"_title"	=> $text["text_block_1_title"] ? $template->blockReplace("Title" , $text) : "",
				),
				$text,
				$this->tpl_module["settings"],
				$this->tpl_module
			)
		);
	}

	/**
	* description
	*
	* @param
	*
	* @return
	*
	* @access
	*/
	function getItems() {

		$items = $this->db->QFetchRowArray(
			"SELECT * FROM 
				{$this->tables['plugin:team_items']} as i , 
				{$this->tables['plugin:team_items_lang']} as l 
			WHERE
				i.item_id = l.item_id and 
				i.module_id = {$this->tpl_module[mod_id]} AND 
				l.lang_id = {$this->tpl_module[lang_id]}

			ORDER BY 
				i.item_order ASC
			"
		);


		return $this->ProcessItems(
			$items , 
			$this->tpl_module["settings"] , 
			$this->private->templates["list"]
		);
	}

	/**
	* description
	*
	* @param
	*
	* @return
	*
	* @access
	*/
	function ProcessItems($items , $settings  , &$template) {
		global $base;

		if (is_array($items)) {
			foreach ($items as $key => $val) {
				$ids[] = $val["item_id"];
				$mod = $val["module_id"];
			}

			//read the extra fields
			$fields = $this->db->QFetchRowArray("
				SELECT * FROM 
					{$this->tables['plugin:team_fields_values']} as v,
					{$this->tables['plugin:team_fields']} as f
				WHERE
					v.module_id = f.module_id AND 
					v.field_id = f.item_id AND 
					v.team_id in (" . implode("," , $ids) . ") AND
					v.module_id=%d
				ORDER BY 
					item_order ASC
				", array($mod)
			);

			if (is_array($fields)) {
				foreach ($fields as $key => $val) {
					if ($val["val_value"]) {
						$contact[$val["team_id"]][] = $val;
					}					
				}			
			}

			foreach ($items as $key => $val) {


				if ($contact[$val["item_id"]]) {
					$items[$key]["social"] = $base->html->Table(
						$template, 
						"Social",
						$contact[$val["item_id"]] 
					);
				} else {
					$items[$key]["social"] = "";
				}				
				
				$items[$key]["image"] = $val["item_image"] ? "upload/team/{$val[item_id]}.jpg" : "upload/default-images/team-person.png";				

				$items[$key]["item_brief"] = $val["item_brief"] ? nl2br($val["item_brief"]) : "";
				$items[$key]["_brief_title"] = $val["item_brief_title"] ? $template->blockReplace("BriefTitle" , $val) : "";

				if ($val["item_link_type"]) {
					$items[$key]["link"] = $val["link"] = $this->plugins["modules"]->BuildLink(array(
						"type"	=> $val["item_link_type"],
						"link"	=> $val["item_link_link"],
						"module"=> $val["item_link_module"],
						"global"=> $val["item_link_global"]
					));

					
					$items[$key]["_link"] = $template->blockReplace("Button" , $val);
				} else {
					$items[$key]["_link"] = "";
				}


				if ($val["item_link_type"] || $val["item_brief"] || $val["item_brief_title"]) {
					$items[$key]["_brief"] = $template->blockReplace("Brief" , $items[$key]);
				} else {
					$items[$key]["_brief"] = $template->blockReplace("NoBrief" , $items[$key]);
				}
				
				
			}			
		}
		return $items;		
	}
		



	/**
	* description
	*
	* @param
	*
	* @return
	*
	* @access
	*/
	function SortItems($items) {
		global $base;
		if (is_array($items)) {
			foreach ($items as $key => $val) {

				if (!$cats[$val["item_cat"]]) {
					$cats[$val["item_cat"]] = $val;
				}

				$cats[$val["item_cat"]]["sub"][] = $val;			
			}			

			foreach ($cats as $key => $val) {

				$cats[$key]["content"] = $base->html->Table(
					$this->_t("items"),
					"",
					$val["sub"],
					$val
				);

				$cats[$key]["head"] = $val["cat_description"]? $this->_t("onepage")->blockReplace(
					"CatHeader",
					$val
				) : "";

				$cats[$key]["title"] = $this->tpl_module["settings"]["set_elements_details"] ? $this->_t("onepage")->blockReplace(
					"CatTitle",
					$val
				) : "";

			}
			
			return $base->html->Table(
				$this->_t("onepage"),
				"Cats",
				$cats
			);
		}
		

		return "";
	}

	

	/**
	* description
	*
	* @param
	*
	* @return
	*
	* @access
	*/
	function Categories() {
		global $base;

		$text = $this->pageTitleBlock();				
		$this->module->plugins["seo"]->PageMeta($text);


		$template = &$this->private->templates["categories"];

		$cats = $this->db->QFetchRowArray(
			"SELECT * FROM 
				{$this->tables['plugin:team_cats']} as c,
				{$this->tables['plugin:team_cats_lang']} as l
			WHERE
				c.module_id={$this->tpl_module[mod_id]} AND 
				c.cat_id = l.cat_id AND 
				c.cat_status = 1 AND 
				l.lang_id = {$this->tpl_module[lang_id]}

			ORDER BY
				c.cat_order ASC
			",
			array( time() )
		);


		$cats = $this->processCats($cats , $template);
		
		$this->SetComments($this->tpl_module["settings"]["set_comments"], array("link" => $this->tpl_module["link"]));

		return $this->module->plugins["layouts"]->Render(
			$this->plugins["layouts"]->Detect(
				$this->tpl_module["settings"]["set_layout"],
				$this->tpl_module["mod_protected"]
			),

			$template->blockREplacE(
				"Main" , 
				array(
					"items"		=> $base->html->table(
						$template,
						"" , 
						$cats
					),

					"_title"	=> $text["text_block_1_title"] ? $template->blockReplace("MainTitle" , $text) : "",

				), 
				$text,				
				$this->tpl_module,
				$this->tpl_module["settings"]
			)
		);
		

	}
	


	/**
	* description
	*
	* @param
	*
	* @return
	*
	* @access
	*/
	function ProcessCats($cats , $template , $module = null) {

		if (!$module) {
			$module = $this->tpl_module;
		}		


		if (is_array($cats)) {
			foreach ($cats as $key => $val) {
				$val["link"] = $module["link"] . $val["cat_url"] . "/";
				$cats[$key]["icon"] = $template->blockREplace($val["cat_icon"] ? "Icon" : "NoIcon" , $val);

				$cats[$key]["button"] = $module["settings"]["set_elements"] == "1" ? $template->blockReplace(
					"Button",
					array(
						"size"	=> $module["settings"]["set_button_size"],
						"style"	=> $style,

						"link"	=> $val["link"] , 
						"text"	=> $btn,
						
					)
				) : "";	

				$cats[$key]["title"] = $template->blockReplace("TitleLink" , $val);

				$cats[$key]["image"] =  "";

				switch ($val["cat_tn_type"]) {
					//image
					case "1":
						$cats[$key]["image"] = $val["cat_tn_image"] ? $template->blockREplace("TnImageLink" , $val) : "";
					break;

					//icon
					default:
					case "2":
						$cats[$key]["image"] = $val["cat_tn_icon"] ? $template->blockREplace("TnIconLink" , $val) : "";
					break;
				}
				
			}			
		}

		return $cats;
	}



	/**
	* description
	*
	* @param
	*
	* @return
	*
	* @access
	*/
	function Detect() {
		$cat = $this->db->QFetchArray(
			"SELECT * FROM 
				{$this->tables['plugin:team_cats']} as p , 
				{$this->tables['plugin:team_cats_lang']} as l

			WHERE 
				p.cat_id = l.cat_id AND 
				l.lang_id = {$this->tpl_module[lang_id]} AND 
				p.module_id = {$this->tpl_module[mod_id]} AND 
				cat_status = 1
			ORDER BY 
				cat_order ASC
			LIMIT 1"			
		);

		$_GET["cat_url"] = $cat["cat_url"];

		$this->detected = true;
	}


	/**
	* description
	*
	* @param
	*
	* @return
	*
	* @access
	*/
	function Category() {

		global $base;
		
		$cat = $this->db->QFetchArray(
			"SELECT * FROM 
				{$this->tables['plugin:team_cats']} as p , 
				{$this->tables['plugin:team_cats_lang']} as l
			WHERE 
				p.cat_id = l.cat_id AND 
				l.lang_id = {$this->tpl_module[lang_id]} AND 
				p.module_id = {$this->tpl_module[mod_id]} AND 
				cat_status = 1 AND
				l.cat_url LIKE '%s'
			",
			array(
				$_GET["cat_url"],
				time()
			)
		);

		
		$text = $this->pageTitleBlock(array(
			$this->tpl_module["link"]						=> $this->tpl_module["mod_name"],
			$this->tpl_module["link"] . "{$cat[cat_url]}/"	=> $cat["cat_title"]
		));


		if (!is_array($cat)) {

			if ($this->detected) {
				return "";
			} else {		
				return $this->module->plugins["redirects"]->ErrorPage("404" , true);
			}
		}


		$this->plugins["seo"]->PageMeta($text);
		$this->plugins["seo"]->Canonical($this->tpl_module["link"] . $cat["cat_url"] . "/");

		$items = $this->_GetItemsPage(
			"
				{$this->tables['plugin:team_items']} as p , 
				{$this->tables['plugin:team_items_lang']} as l
			",
			99999 , 
			$_GET["page"],
			"	
				p.item_id = l.item_id and 
				l.lang_id = {$this->tpl_module[lang_id]} AND 
				find_in_set({$cat[cat_id]}  , p.item_cat) AND 
				p.item_status = 1
			",
			"item_order ASC"
		);


		$items = $this->ProcessItems($items["records"] , "" , $this->_t("items"));


		$this->SetComments($this->tpl_module["settings"]["set_comments_details"], array("link" => $this->tpl_module["link"] . $cat["cat_url"] . "/"));

		return $this->module->plugins["layouts"]->Render(
			$this->plugins["layouts"]->Detect(
				$this->tpl_module["settings"]["set_layout_details"],
				$this->tpl_module["mod_protected"]
			),
			$this->_t("category")->blockREplacE(
				"Main" , 
				array(

					"items"	=> $base->html->Table(
						$this->_t("items"), 
						"" , 
						$items
					),

					"head"	=> $this->tpl_module["settings"]["set_elements_details"] ? $this->_t("category")->blockReplace("Head" , $cat) : "",

				),
				$cat,
				$text ,
				$this->tpl_module

			)
		);

	}



	function GetMenuButton($module) { 

		if (STP_ENABLE_ONEPAGE || ($module["settings"]["set_style"] == "2")) {
			$module["settings"]["set_menu_style"] = 0;
		}
		

		switch ($module["settings"]["set_menu_style"]) {
			default:
				return parent::GetMenuButton($module);
			break;

			case "2":

				if ($module["settings"]["set_menu_cats"]) {
					$cond = " AND i.cat_id IN (" . $module["settings"]["set_menu_cats"] . ")";
				}
				
			case "1":
				$items = $this->db->QFetchRowArray(
					"SELECT * " . 
					"FROM 
						{$this->tables['plugin:team_cats']} as i,
						{$this->tables['plugin:team_cats_lang']} as il
					WHERE 				
						i.cat_id = il.cat_id AND 
						il.lang_id={$module[lang_id]} AND 
						i.module_id={$module[mod_id]} AND 
						cat_status = 1 
						{$cond}

					ORDER BY  cat_order ASC",
					array(
						time()
					)
				);


				if (is_array($items)) {
					foreach ($items as $key => $val) {
						$sub[] = array(
							"title"		=> $val["cat_title"],
							"link"		=> $module["link"] . $val["cat_url"] . "/ ",
							"selected"	=> ($module["mod_id"] == $val["module_id"] ) && ( $val["cat_url"] == $_GET["cat_url"]) ? true : false,
							"target"	=> "",

							"html"		=> "",
							"sub"		=> "",

						);
					}
				
				}

				return parent::GetMenuButton(
					$module , 
					array( 
						"html"	=> $this->plugins["menus"]->dropDownHTML(								
							array(
								"size"		=> $module["settings"]["set_menu_cols"],
								"id"		=> $module["mod_id"],
								"bg"		=> $module["settings"]["set_menu_bg"],
								"title"		=> $module["mod_name"],
							),
							$sub
						)
					)
				);
			
			break;

		}				
	}


	/**
	* description
	*
	* @param
	*
	* @return
	*
	* @access
	*/
	function SitemapGetMaps($module , $lang) {

		if ($module["mod_id"]) {
			return array($module["mod_id"] => true);
		} else {
			return array($module["module_code"] => true);
		}
		
	}

	function SitemapGetLinks($module , &$links) {		
		$links[] = array(
			"url" => $module["link"],
		);

		if ($module["settings"]["set_style"] != "2") {
			$cats = $this->db->QFetchRowArray(

				"SELECT * FROM 
					{$this->tables['plugin:team_cats']} as c,
					{$this->tables['plugin:team_cats_lang']} as l
				WHERE
					c.module_id={$module[mod_id]} AND 
					c.cat_id = l.cat_id AND 
					c.cat_status = 1 AND 
					l.lang_id = {$module[lang_id]} 
				ORDER BY
					c.cat_order ASC
				",
				array( time() )
			);

			if (is_array($cats)) {
				foreach ($cats as $key => $val) {
					$links[] = array(
						"url"	=> $module["link"] . $val["cat_url"] . "/"
					);
				}				
			}

		}

	}


}

?>