<?php


if (!defined('IN_CONTEXT')) die('access violation error!');

/**
 * Module block object
 *
 */
class Template extends RecordObject {
    public $belong_to = array('TemplateCategory');

    public static function &allRemoteTemplates($cateid) {
    	/*
        $client =& Toolkit::initSoapClient();
        $templates = unserialize($client->getAvailableTpls(EZSITE_UID, $cateid, EZSITE_LEVEL));
        */
    	$ezsite_uid = EZSITE_UID;
    	$level = EZSITE_LEVEL;
    	$templates = file_get_contents(SCREENSHOT_URL."getAvailableTpls.php?ezsite_uid=$ezsite_uid&cateid=$cateid&level=$level");
    	$templates = unserialize($templates);
        return $templates;
    }
    
    public static function &allRemoteTemplatesCategories()
    {
    	/*
    	$client =& Toolkit::initSoapClient();
    	$templates = unserialize($client->getTplCategories());
    	*/
    	$templates = file_get_contents(SCREENSHOT_URL.'getTplCategories.php');
    	$templates = unserialize($templates);
    	return $templates;
    }

    public static function &getMyTemplates($cateid) {
        $rearranged = array();
        $o_tpl = new Template();
        $templates =& $o_tpl->findAll("template_category_id=?", array($cateid), "ORDER BY id");
        if (sizeof($templates) > 0) {
            foreach ($templates as $template) {
                $curr_tpl['id'] = $template->id;
                $curr_tpl['name'] = $template->template;
                $curr_tpl['screenshot_url'] = Toolkit::fixpic('../template/'.$template->template.'/screenshot.jpg');
                $rearranged[] = $curr_tpl;
            }
        }

        return $rearranged;
    }
	public static function ResetTplData(){
		$db =& MySqlConnection::get();
        $sql = "show tables";
        $rs =& $db->query($sql);
		$rows =& $rs->fetchRows();
		foreach($rows as $row){
			$_row_val = array_values($row);
			$val = $_row_val[0];
			if($val != Config::$tbl_prefix."users" && $val != Config::$tbl_prefix."parameters"&& $val != Config::$tbl_prefix."admin_menu_categories"&& $val != Config::$tbl_prefix."admin_menu_items"&& $val != Config::$tbl_prefix."languages"){
				$sql = "delete from ".$val;
				$rs =& $db->query($sql);
			}
		}
	}
}
?>
