<?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 CNews extends CPlugin{
	
	var $tplvars; 

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

		$this->name = "news";
	}

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

		parent::DoEvents();

		if ($_GET["mod"] == "news") {
			$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 "texts":
					return $this->__adminTexts();
				break;


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

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

					return $data->DoEvents();
				break;
			}
		}
	}

	function adminGetWidgets($module) {

		$widgets = array(
			array(
				"type"				=> "1",
				"name"				=> "Latest News",
				"module_name"		=> $module["mod_name"],
				"module"			=> $module["mod_id"],
				"module_type"		=> "1",
			),
		);

		return $widgets;

	}


	/**
	* 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:news_images']} WHERE item_parent={$record[item_parent]} AND item_type=1 AND item_image=1 ORDER BY item_order ASC LIMIT 1");

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

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

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


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

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

	}
	

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

		$tables  = array(
			"plugin:news_items" , 
			"plugin:news_items_lang",
			"plugin:news_images",
			"plugin:news_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);	
	}


	
}

?>