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

if (!defined("STPBase")) {
	die("This file can't be accessed directly!");
}
	global $_LANG_ID;


#### CONVERT SETTINGS TO NEW SYSTEM
	//user modules
	$widgets = $this->db->QFetchRowArray("SELECT * FROM {$this->tables['core:widgets']}");

	if (is_array($widgets)) {
		foreach ($widgets as $key => $val) {
			$val["widget_settings"] = base64_decode($val["widget_settings"]);

			$this->db->QueryUpdate(
				$this->tables["core:widgets"],
				$val,
				"widget_id={$val[widget_id]}"
			);
		}		
	}
	
	$widgets = $this->db->QFetchRowArray("SELECT * FROM {$this->tables['core:widgets_lang']}");

	if (is_array($widgets)) {
		foreach ($widgets as $key => $val) {
			$val["widget_set_lang"] = base64_decode($val["widget_set_lang"]);

			$this->db->QueryUpdate(
				$this->tables["core:widgets_lang"],
				$val,
				"widget_id={$val[widget_id]} AND lang_id={$val[lang_id]}"
			);
		}		
	}

#### CONVERT LINKS TO NEW STATIC
	$tables = array(
		array(
			"items"			=>	$this->db->QFetchRowArray("SELECT * FROM site_core_widgets_cards"),
			"items_lang"	=> $this->db->QFetchRowArray("SELECT * FROM site_core_widgets_cards_lang"),
		),

		array(
			"items"			=> $this->db->QFetchRowArray("SELECT * FROM site_core_widgets_links"),
			"items_lang"	=> $this->db->QFetchRowArray("SELECT * FROM site_core_widgets_links_lang"),
		),

		array(
			"items"			=> $this->db->QFetchRowArray("SELECT * FROM site_core_widgets_acordeon"),
			"items_lang"	=> $this->db->QFetchRowArray("SELECT * FROM site_core_widgets_acordeon_lang"),
		),
	);

	foreach ($tables as $___k => $___v) {

		$owid = array();
		$old = array();


		$items = $___v["items"];
		$items_lang = $___v["items_lang"];
	
		if (is_array($items)) {
			//main fields
			foreach ($items as $key => $val) {
		
				$oid = $val["item_id"];			
				unset($val["item_order"]);

				$owid[$oid] = $val["widget_id"];

				$old[$oid] = $this->db->QueryInsert(
					$this->tables["core:widgets_static"],
					array(
						"widget_id"	=> $val["widget_id"],
						"item_vars"	=> serialize($val)
					)
				);

				$this->db->QueryUpdate(
					$this->tables["core:widgets_static"],
					array(
						"item_order"	=> $old[$oid]
					),
					"item_id = " . $old[$oid]
				);

				if ($val["item_image"] && file_exists("../upload/widgets/cards/{$oid}.jpg")) {
					CFile::copy(
						"../upload/widgets/cards/{$oid}.jpg",
						"../upload/widgets/static/{$old[$oid]}.jpg"
					);
				}

				if ($val["item_image"] && file_exists("../upload/widgets/cards/tn_{$oid}.jpg")) {
					CFile::copy(
						"../upload/widgets/cards/tn_{$oid}.jpg",
						"../upload/widgets/static/tn_{$old[$oid]}.jpg"
					);
				}

				if ($val["item_image"] && file_exists("../upload/widgets/cards/b_{$oid}.jpg")) {
					CFile::copy(
						"../upload/widgets/cards/b_{$oid}.jpg",
						"../upload/widgets/static/b_{$old[$oid]}.jpg"
					);
				}
				
			}	

			
			//language fields
			foreach ($items_lang as $key => $val) {
		
				$oid = $val["item_id"];			
				$lang = $val["lang_id"];

				unset($val["item_id"]);
				unset($val["lang_id"]);

				$this->db->QueryInsert(
					$this->tables["core:widgets_static_lang"],
					array(
						"widget_id"			=> $owid[$oid],
						"lang_id"			=> $lang,
						"item_id"			=> $old[$oid],					
						"item_vars_lang"	=> serialize($val)
					)
				);
			}	

		}
	}
	

?>