<?php
/*
	Stempora Web Framework
	Copyright (c) 2002-2014 Stempora. 
	All rights reserved.
		web:  www.stempora.com
		mail: support@stempora.com		
*/

// dependencies

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

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

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

		parent::DoEvents();

		if ($_GET["mod"] == "portfolio") {
			$sub = $_GET["sub"];
			$action = $_GET["action"];

			//read the module
			$this->tpl_module = $this->module->plugins["modules"]->getModuleInfo($_GET["module_id"]);
			$this->plugins["modules"]->ValidateModule($this->tpl_module);

			switch ($sub) {


				case "landing":
					$sub = "items";
				case "items":
				case "items.images":
				case "cats":
					$data = new CSQLAdmin($sub, $this->__parent_templates,$this->db,$this->tables,$extra);
					$this->PrepareFields($data->forms["forms"]);
					$this->PrepareImageFields($data->forms["forms"] , "items.images");

					$data->functions = array( 
							"onstore" => array(&$this , "StoreRecord"),
							"ondelete" => array(&$this , "DeleteRecord"),
					);					

					return $data->DoEvents();
				break;


				case "texts":
					return $this->__adminTexts();
				break;

			}
		}
	}


	function DeleteRecord($record) {

		switch ($_GET["sub"]) {
			case "topics.images":

				$last = $this->db->QFetchArray("SELECT * FROM {$this->tables['plugin:portfolio_images']} WHERE item_parent={$record[item_parent]} AND item_id!={$record[item_id]} AND item_type=1 AND item_image=1 ORDER BY item_order ASC LIMIT 1");

				$this->db->QueryUpdate(
					$this->tables["plugin:portfolio_items"],
					array(
						"item_tn"	=> $last["item_id"]
					),
					"item_id={$record[item_parent]}"
				);

			break;
		}
		

	}

	/**
	* description
	*
	* @param
	*
	* @return
	*
	* @access
	*/
	function StoreRecord($record) {

		switch ($_GET["sub"]) {
			case "items.images":

				if ($record["item_id"]) {

					$last = $this->db->QFetchArray("SELECT * FROM {$this->tables['plugin:portfolio_images']} WHERE item_parent={$record[item_parent]} AND item_type=1 AND item_image=1 ORDER BY item_order ASC LIMIT 1");

					if (is_Array($last)) {
						$this->db->QueryUpdate(
							$this->tables["plugin:portfolio_items"],
							array(
								"item_tn"	=> $last["item_id"]
							),
							"item_id={$record[item_parent]}"
						);
					}
					
				}
			break;

			case "items":
				$this->db->QueryUpdate(
					$this->tables["plugin:portfolio_cats"],
					array(
						"cat_items"	=> $this->db->RowCount($this->tables['plugin:portfolio_items'] , " WHERE item_cat={$record[item_cat]} ")
					),
					"cat_id={$record[item_cat]}"
				);

			break;
		}
		
	}

	
	function adminGetWidgets($module) {

		$widgets = array(
/*
			array(
				"type"				=> "1",
				"name"				=> "Categories",
				"module_name"		=> $module["mod_name"],
				"module"			=> $module["mod_id"],
				"module_type"		=> "1",
			),
*/

			array(
				"type"				=> "2",
				"name"				=> "Latest Projects",
				"module_name"		=> $module["mod_name"],
				"module"			=> $module["mod_id"],
				"module_type"		=> "1",
			),


		);
		return $widgets;

	}

	/**
	* description
	*
	* @param
	*
	* @return
	*
	* @access
	*/
	function onDelete($module_data) {

		$tables  = array(
			"plugin:portfolio_cats" , 
			"plugin:portfolio_cats_lang",
			"plugin:portfolio_items",
			"plugin:portfolio_items_lang",
			"plugin:portfolio_images",
			"plugin:portfolio_images_lang",
		);

		foreach ($tables as $key => $val) {
			$this->db->Query(
				"DELETE FROM " . $this->tables[$val] . " WHERE module_id={$module_data[mod_id]}"
			);
		}

		parent::onDelete($module_data);	
	}
	

	/**
	* description
	*
	* @param
	*
	* @return
	*
	* @access
	*/
	function PrepareImageFields(&$forms , $sub) {
		if ($_GET["sub"] == $sub) {

		}		
	}
	
}

?>