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

	function __construct() {
		//$this->CPlugin($db, $tables , $templates);
		$this->name = "cta";
	}

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

		if ($_GET["mod"] == $this->name) {
			//read the module
			$this->tpl_module = $this->module->plugins["modules"]->LoadDefaultModule($this->name);

			parent::DoEvents();

			$sub = $_GET["sub"];
			$action = $_GET["action"];

			
			switch ($sub) {
				case "landing":
					
					$data = new CSQLAdmin($sub, $this->__parent_templates,$this->db,$this->tables,$extra);
					$data->setAclMod($this->tpl_module);

					$this->PrepareFields($data->forms["forms"]);
					return $data->DoEvents();
				break;


				case "duplicate":
					return $this->duplicateCta();
				break;
			}
		}
	}



	
	/**
	* description
	*
	* @param
	*
	* @return
	*
	* @access
	*/

	/**
	* description
	*
	* @param
	*
	* @return
	*
	* @access
	*/
	function duplicateCTA() {

		$item = $this->db->QFetchArray("SELECT * FROM {$this->tables['plugin:cta']} WHERE cta_id={$_GET['cta_id']}");
		$item_lang = $this->db->QFetchRowArray("SELECT * FROM {$this->tables['plugin:cta_lang']} WHERE cta_id={$_GET['cta_id']}");

		if (!is_array($item)) {
			return "Invalid link !";
		}

		unset($item["cta_id"]);

		$item["cta_name"] .= " (Copy)";

		$sid = $this->db->QueryInsert(
			$this->tables['plugin:cta'] , 
			$item
		);

		if (is_array($item_lang)) {
			foreach ($item_lang as $key => $val) {
				$val["cta_id"] = $sid;

				$this->db->QueryInsert(
					$this->tables['plugin:cta_lang'] , 
					$val
				);
			}			
		}
		

		//duplicate image if attached
		if ($item["cta_image "]) {
			CFile::copy(
				"../upload/cta/{$_GET[cta_id]}.png",
				"../upload/cta/{$sid}.png"
			);
		}
				
		//redirect to details page
		urlredirect(
			"index.php?mod=cta&sub=landing&cta_id={$sid}&action=edit&returnurl=" . 
				urlencode("index.php?mod=cta&sub=landing")
		);
	}
	


}

?>