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


class CWidgetsWidgetLinkToCategory extends CWidget {

	/**
	* description
	*
	* @var type
	*
	* @access type
	*/
	var $meta = array(
		"type"	=> "5",
		"name"	=> "Link to Page Category",
		"module"	=> array(
			"name"	=> "",
			"id"	=> "",
			"type"	=> "",
		)
	);

	/**
	* description
	*
	* @param
	*
	* @return
	*
	* @access
	*/
	function Render($widget) {
		$links = $this->plugins["modules"]->GetModulesByParent($widget["settings"]["set_category"]);

		if (!is_array($links)) {
			return "";
		}


		//need to fix the global link m to get the original module link

		foreach ($links as $key => $module) {

			if ($module["mod_id"] && !$module["mod_invisible"]) {

				if (method_exists($this->plugins[$module["module_code"]] , "GetMenuButton")) {

					$tmp = $this->plugins[$module["module_code"]]->GetMenuButton($module);

					if (is_array($tmp)) {

						if (!is_array($tmp[0]) && count($tmp)) {
							$tmp = array( 0=> $tmp);
						}
					
						foreach ($tmp as $_key => $_val) {
							if (is_array($_val)) {
								$_val["mod_color"] = $module["mod_color"];
								$_links[]  = $_val;
							}
							
						}
						
					}
					
				}
			} elseif ($module["module_id"]) {
				if (method_exists($this->plugins[$module["module_code"]] , "GetMenuButton")) {


					$tmp = $this->plugins[$module["module_code"]]->GetMenuButton($module);

					if (is_array($tmp)) {

						if (!is_array($tmp[0]) && count($tmp)) {
							$tmp = array( 0=> $tmp);
						}
					
						foreach ($tmp as $_key => $_val) {
							if (is_array($_val)) {
								$_val["mod_color"] = $module["mod_color"];
								$_links[]  = $_val;
							}
							
						}
						
					}

				}

			}
			
								
		}

		if (is_array($_links)) {

			foreach ($_links as $key => $val) {
				$_links[$key]["selected"] = $val["selected"] ? $this->tpl->blockReplace("Selected" , $val) : "";

				//keep old compatibility
				$_links[$key]["item_title"] = $val["title"];
				$_links[$key]["item_link"] = $val["link"];
				$_links[$key]["item_link_target"] = "";
			}
			
		}
		
		global $base;

		return $this->tpl->blockReplace(
			"Main" , 
			array(
				"items"	=> $base->html->table(
					$this->tpl , 
					"",	
					$_links
				)
			),
			$widget 
		);
		

	}
	

}


?>