<?php
/*
	Stempora web framework
	copyright (c) 2002-2013 Stempora
		web:  www.stempora.com
		mail: support@stempora.com
*/


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

	function CPlugin() {
	}


	/**
	* description
	*
	* @param
	*
	* @return
	*
	* @access
	*/
	function DoEvents(){
		global $base, $_CONF, $_TSM , $_VARS , $_USER , $_BASE , $_SESS;


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

			switch ($_GET["sub"]) {
				default:
					return $this->BuildDashboard();
				break;
			}
		}
	}

	/**
	* description
	*
	* @param
	*
	* @return
	*
	* @access
	*/
	function BuildDashboard() {
		global $base , $_TSM , $_CONF;
		$_GET["showempty"] = 1;

		$_TSM["PUB:MODULE_NAME"] = "My Dashboard";

		$modules = $this->db->QFetchRowArray("SELECT * FROM {$this->private->tables['core:user_modules']} ORDER BY mod_order ASC");

		$core = $this->db->QFetchRowArray("SELECT * FROM {$this->private->tables['core:modules']} WHERE module_type=2 ORDER BY module_type_name");
	
		if ($_USER["user_level"]) {
			$perm = explode("," , $_USER["user_perm"]);
		}

		//remove modules without permission
		if (is_array($modules)) {
			foreach ($modules as $key => $val) {

				if (($_USER["user_level"] && !in_array($val["mod_id"] , $perm))  ) {
					unset($modules[$key]);
				}
			}			
		}

		if (is_array($core)) {
			foreach ($core as $key => $val) {
				if (is_object($this->module->plugins[$val["module_code"]])) {
					$widget = $this->module->plugins[$val["module_code"]]->DashboardWidgets();

					if (is_array($widget) && count($widget)) {
						foreach ($widget as $k => $v) {
							$_widgets[] = $v;
						}				
					}			
				}
			}
		}
		
		if (is_array($modules)) {
			foreach ($modules as $key => $val) {
				if (is_object($this->module->plugins[$val["mod_module_code"]])) {
					$widget = $this->module->plugins[$val["mod_module_code"]]->DashboardWidgets($val["mod_id"]);

					if (is_array($widget) && count($widget)) {
						foreach ($widget as $k => $v) {
							$_widgets[] = $v;
						}				
					}			
				}				
			}
		}

		//$_widgets[] = $this->DashboardWidgets();

		if (is_array($_widgets)) {
			foreach ($_widgets as $key => $val) {
			}			
		} else {

			//no widgets redirect to manage pages
			urlredirect("index.php?mod=modules&sub=user");
		}

		$template = new CTemplateDynamic($this->tpl_path . "main.htm");

		return $base->html->table(
			$template , 
			"" , 
			$_widgets
		);

	}
	


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

		return 
			array(
				"width"	=> "6",
				"type"	=> "widget",
				"pos"	=> "1",
				"body"	=> $base->htmlElements->Widget(
					array(
						"title"		=> "My Shitty Life",
						"icon"		=> "cogs",
						"skin"		=> "Pink",
						"height"	=> "260",

						"content"	=> $base->htmlElements->PieChart(
							"pie1"	,	
							array(
								array("label"	=> "Food" , "data" => "10"),
								array("label"	=> "Sleep" , "data" => "20"),
								array("label"	=> "Watching Movies" , "data" => "5"),
								array("label"	=> "Nadim Stressing Me" , "data" => "80"),
							)
						)
					)
				) 
		);

		return $base->htmlElements->Widget(
			array(
				"title"		=> "My Shitty Life",
				"icon"		=> "cogs",
				"skin"		=> "Pink",
				"height"	=> "260",

				"content"	=> $base->htmlElements->PieChart(
					"pie1"	,	
					array(
						array("label"	=> "Food" , "data" => "10"),
						array("label"	=> "Sleep" , "data" => "20"),
						array("label"	=> "Watching Movies" , "data" => "5"),
						array("label"	=> "Nadim Stressing Me" , "data" => "80"),
					)
				)
			)
		) . 
					
		$base->htmlElements->Widget(
			array(
				"title"		=> "Hourly Rate Graph",
				"icon"		=> "cogs",
				"skin"		=> "Green",

				"content"	=> $base->htmlElements->GraphChart(
					"chart2"	,	
					array(
						array(
							"label"	=> "Nadim" , 
							"data" => array(
								array( 4, 15),
								array( 5, 10),
								array( 6, 5),
								array( 7, 5),
							)
						),

						array(
							"label"	=> "Others" , 
							"data" => array(
								array( 4, 38),
								array( 5, 40),
								array( 6, 40),
								array( 7, 35),
							)
						)
					),

					array(
						"x_mode"	=> "categories",
					)

				)
			)
		) . 
							
		$base->htmlElements->Widget(
			array(
				"title"		=> "Hourly Rate Bars",
				"icon"		=> "cogs",
				"skin"		=> "Blue",

				"content"	=> $base->htmlElements->GraphChart(
					"chart3"	,	
					array(
						array(
							"label"	=> "Nadim" , 
							"data" => array(
								array( "Jan", 35),
								array( "Feb", 20),
								array( "Mar", 15),
								array( 4, 15),
								array( 5, 10),
								array( 6, 5),
								array( 7, 5),
							)
						),

						array(
							"label"	=> "Others" , 
							"data" => array(
								array( "Jan", 35),
								array( "Feb", 20),
								array( "Mar", 35),
								array( 4, 38),
								array( 5, 40),
								array( 6, 40),
								array( 7, 35),
							)
						),

						array(
							"label"	=> "Big Orange Planet" , 
							"data" => array(
								array( "Jan", 85),
								array( "Feb", 11),
								array( "Mar", 43),
								array( 4, 33),
								array( 5, 46),
								array( 6, 25),
								array( 7, 19),
							)
						)

					),

					array(
						"x_mode"	=> "categories",
					)
				)
			)
		)				;
	}
	
}

?>
