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

// dependencies

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

	/**
	* description
	*
	* @var type
	*
	* @access type
	*/
	var $template;
	
		

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

		global $site;

		$site->shortcodes = new Shortcodes();
	}

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

		parent::DoEvents();		
	}

	/**
	* description
	*
	* @param
	*
	* @return
	*
	* @access
	*/
	function Process($content) {
		global $site , $base;


		$items = $this->db->QFetchRowArray(
			"SELECT * FROM {$this->tables['plugin:shortcodes']} WHERE shortcode_status=1"
		);

		if (is_array($items)) {
			foreach ($items as $key => $val) {

				//process device selection
				$device = explode("," , $val["shortcode_device"]);

				if (
						($base->isTablet && !in_array(2, $device)) ||
						($base->isMobile && !in_array(3, $device)) ||
						(!($base->isMobile||$base->isTablet) && !in_array(1, $device))
				) {
					$val["shortcode_multi_replace"] = $val["shortcode_replace"] = "";
				}
					

				if ($val["shortcode_multi"] && $val["shortcode_multi_replace"]) {
					$site->shortcodes->Add(
						$val["shortcode_code"]  , 
						"html" , 
						array(
							"single"	=> $val["shortcode_replace"],
							"multi"		=> $val["shortcode_multi_replace"]
						), 
						$val["shortcode_nested"]
					);

				} else {				
					$site->shortcodes->Add(
						$val["shortcode_code"]  , 
						"html" , 
						$val["shortcode_replace"] , 
						$val["shortcode_nested"]
					);
				}
			}
		}

		//add developer codes
		$template = new CTemplateDynamic($this->tpl_path . "developer.htm");

		$site->shortcodes->Add("developer"  , "html" , $template->blockReplace("Developer"));

		if (is_array($this->dynamic)) {
			foreach ($this->dynamic as $key => $val) {

				$site->shortcodes->Add($val["code"] , "func" , $val["method"]);
			}			
		}	


		$site->shortcodes->SetContent($content);
		$site->shortcodes->Parse();

		return $site->shortcodes->GetContent();

	}	
	
}

?>