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

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


	/**
	* description
	*
	* @param
	*
	* @return
	*
	* @access
	*/
	function GetUSP($mod_id , $system = false , $widget = false) {
		global $_CONF , $_LANG_ID;


		if ($system) {
			$usp = $this->db->QFetchArray(
				"SELECT * FROM {$this->tables['plugin:usp']} as c, {$this->tables['plugin:usp_lang']} as l  WHERE c.usp_id=l.usp_id AND lang_id={$_LANG_ID} AND find_in_set({$mod_id} , usp_modules_global) AND usp_status=1 ORDER BY rand() limit 1"
			);
		} else {
			
			$usp = $this->db->QFetchArray(
				"SELECT * FROM {$this->tables['plugin:usp']} as c, {$this->tables['plugin:usp_lang']} as l  WHERE c.usp_id=l.usp_id AND lang_id={$_LANG_ID} AND find_in_set({$mod_id} , usp_modules) AND usp_status=1 ORDER BY rand() limit 1"
			);
		}

		//load the default if exits
		if (!is_array($usp)) {

			$usp = $this->db->QFetchArray(
				"SELECT * FROM {$this->tables['plugin:usp']} as c, {$this->tables['plugin:usp_lang']} as l  WHERE c.usp_id=l.usp_id AND lang_id={$_LANG_ID} AND usp_default=1 AND usp_status=1 ORDER BY rand() limit 1"
			);

		}
		

		if (is_array($usp)) {

			$tpl = new CTemplateDynamic($this->tpl_path . "usp.htm");

			switch ($usp["usp_link_type"]) {
				case "0":
					$usp["link"] = "";
				break;

				case "1":
					$usp["link"] = $usp["usp_link"];
				break;

				case "2":
					if (is_array($this->plugins["modules"]->modules[$usp["usp_link_module"]])) {
						$usp["link"] = $this->plugins["modules"]->modules[$usp["usp_link_module"]]["link"];
					} else {
						$usp["link"] = "";
					}						
				break;
			}

			if ($usp["link"]) {
				$usp["link_htm"] = $tpl->blockReplace("Link" , $usp);
			} else {
				$usp["link_htm"] = "";
			}

			$usp["text"] = $usp["usp_text"] ? $tpl->blockREplacE("Text" , $usp) : "";
			$usp["title"] = $usp["usp_title"] ? $tpl->blockREplacE("Title" , $usp) : "";
			

			return $tpl->blockReplace(
				"Main" , 
				array(
					"content"	=> $tpl->blockReplace($usp["usp_image"] ? "ContentImage" : "Content" , $usp)
				)
			);

		}
		
	}
	



	/**
	* description
	*
	* @param
	*
	* @return
	*
	* @access
	*/
	function onGlobalHooksExecute($module) {

		if ($module["mod_id"]) {
			$bg  = 	$this->GetUSP($module["mod_id"]);
		} elseif ($module["module_id"]) {
			$bg  = 	$this->GetUSP($module["module_id"] , true);
		}
		

		global $_TSM;

		$_TSM["PUB:USP"] = $bg;

		return $bg;
	}

	
	function Widget($widget) {

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

		//$this->__init();

		switch ($widget["widget_type"]) {
			case "1":
				return $this->WidgetUSP($widget);
			break;

		}

	}



	/**
	* description
	*
	* @param
	*
	* @return
	*
	* @access
	*/
	function WidgetUSP($widget) {
		global $_LANG_ID;


			$usp = $this->db->QFetchArray(
				"SELECT * FROM {$this->tables['plugin:usp']} as c, {$this->tables['plugin:usp_lang']} as l  WHERE c.usp_id=l.usp_id AND lang_id={$_LANG_ID} AND c.usp_id={$widget[settings][set_usp]} AND usp_status=1"
			);

			if (is_array($usp)) {

				$tpl = new CTemplateDynamic($this->tpl_path . "widget.htm");

				switch ($usp["usp_link_type"]) {
					case "0":
						$usp["link"] = "";
					break;

					case "1":
						$usp["link"] = $usp["usp_link"];
					break;

					case "2":
						if (is_array($this->plugins["modules"]->modules[$usp["usp_link_module"]])) {
							$usp["link"] = $this->plugins["modules"]->modules[$usp["usp_link_module"]]["link"];
						} else {
							$usp["link"] = "";
						}						
					break;
				}

				if ($usp["link"]) {
					$usp["link_htm"] = $tpl->blockReplace("Link" , $usp);
				} else {
					$usp["link_htm"] = "";
				}

				$usp["text"] = $usp["usp_text"] ? $tpl->blockREplacE("Text" , $usp) : "";
				$usp["title"] = $usp["usp_title"] ? $tpl->blockREplacE("Title" , $usp) : "";
				

				return $tpl->blockReplace(
					"Main" , 
					array(
						"content"	=> $tpl->blockReplace($usp["usp_image"] ? "ContentImage" : "Content" , $usp)
					),
					$widget
				);
			}
		
	}


}

?>