<?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;

	//user modules
	$widgets = $this->db->QFetchRowArray(
		"
			SELECT * 
			FROM 
				{$this->tables['core:widgets']} as w,
				{$this->tables['core:widgets_lang']} as wl,
				{$this->tables['core:user_modules']} as m, 
				{$this->tables['core:user_modules_lang']} as l
			WHERE 
				w.widget_id = wl.widget_id AND 
				wl.lang_id=l.lang_id AND 
				widget_module=m.mod_id AND 
				widget_status=1 AND 
				m.mod_id = l.mod_id AND 
				l.lang_id = {$_LANG_ID} AND
				widget_module_type=1 
		"
	);

	$core = $this->db->QFetchRowArray(
		"
			SELECT * 
			FROM 
				{$this->tables['core:widgets']} as w,
				{$this->tables['core:widgets_lang']} as wl,
				{$this->tables['core:modules']} 
			WHERE 
				w.widget_id = wl.widget_id AND 
				wl.lang_id = {$_LANG_ID} AND
				module_status=1 AND 
				widget_module=module_id AND 
				widget_status=1 AND 
				widget_module_type=2 
		"
	);

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

			if (is_object($this->module->plugins[$val["mod_module_code"]])) {
				$this->module->plugins[$val["mod_module_code"]]->loadWidgets();

				if (is_array($this->module->plugins[$val["mod_module_code"]]->widgets)) {

					foreach ($this->module->plugins[$val["mod_module_code"]]->widgets as $k => $v) {
						$this->module->plugins[$val["mod_module_code"]]->initWidget($k);							
						//$this->module->plugins[$val["mod_module_code"]]->_widgets[$v->meta["type"]] = $k;

						$_widgets[$val["mod_id"]][$this->module->plugins[$val["mod_module_code"]]->widgets[$k]->meta["type"]] = strtolower(get_class($this->module->plugins[$val["mod_module_code"]]->widgets[$k]));
					}						
				}					
			}
		}
	}

	if (is_array($core)) {
		foreach ($core as $key => $val){
			if (is_object($this->module->plugins[$val["module_code"]])) {
				$this->module->plugins[$val["module_code"]]->loadWidgets();

				if (is_array($this->module->plugins[$val["module_code"]]->widgets)) {

					foreach ($this->module->plugins[$val["module_code"]]->widgets as $k => $v) {
						$this->module->plugins[$val["module_code"]]->initWidget($k);							

						$_widgets[$val["module_id"]][$this->module->plugins[$val["module_code"]]->widgets[$k]->meta["type"]] = strtolower(get_class($this->module->plugins[$val["module_code"]]->widgets[$k]));
					}						
				}

			}
		}
	}


	//convert all widgets
	foreach ($_widgets as $key => $val) {

		foreach ($val as $k => $v) {
			$this->db->QueryUpdate(
				$this->tables["core:widgets"],
				array(
					"widget_type"	=> $v
				),
				"widget_type={$k} AND widget_module={$key}"
			);
		}			
	}


?>