<?
/* PLUGINS START */

/*
* ------------------------------------------------------------- 
* Type:     resource
* Name:     db_fetch
* Purpose:  Fetches templates from a database
* -------------------------------------------------------------
*/

function db_get_template ($tpl_name, &$tpl_source, &$smarty_obj)
{
    $row = @pg_fetch_object(@pg_query("select * from html_templates where code='$tpl_name'"));
	if ($row->id!="") {
        $tpl_source = stripslashes($row->content);
        return true;
    } else {
        return false;
    }
}

function db_get_timestamp($tpl_name, &$tpl_timestamp, &$smarty_obj)
{
    $row = @pg_fetch_object(@pg_query("select * from html_templates where code='$tpl_name'"));
	if ($row->id!="") {
        $tpl_source = $row->last_modified;
        return true;
    } else {
        return false;
    }
}

function db_get_secure($tpl_name, &$smarty_obj)
{
    // assume all templates are secure
    return true;
}

function db_get_trusted($tpl_name, &$smarty_obj)
{
    // not used for templates
}
/* PLUGINS END   */

function smarty_strip_tags($str){
	return strip_tags($str[str]);
}


function smarty_array_size($params){
    return sizeof($params['arr']);
}

function smarty_common_add_get_input($params){
	$arr = $params['arr'];
	$field_name = $params['field_name'];

	global $$field_name;
	global $DOCUMENT_ROOT,$PROJECT_ROOT,$ADMIN_ROOT;
	global $SETTINGS;
	
	if(is_array($arr[0])){/* multiple radio */
		$input = "";
		foreach($arr[0] as $key=>$value){
			$params['arr'] = Array($value);
	 		$params['field_name'] = $field_name;
			$input.=smarty_common_add_get_input($params);
		}
	}
	elseif (eregi('.name=',$arr[0])){
		if ((eregi("^<select name=\"dd\"",$arr[0]))&&
		    (eregi(".name=\"mm\"",$arr[0]))&&
		    (eregi(".name=\"yy\"",$arr[0])))
			{
    			global ${$field_name."dd"};
				global ${$field_name."mm"};
				global ${$field_name."yy"};
				$dt_params=getdate($$field_name);
				if (!${$field_name."dd"})
					${$field_name."dd"}=$dt_params[mday];//intval(substr($$field_name,0,2));
				if (!${$field_name."mm"})
					${$field_name."mm"}=$dt_params[mon];//intval(substr($$field_name,3,2));
				if (!${$field_name."yy"})
					${$field_name."yy"}=$dt_params[year];//intval(substr($$field_name,6,4));
//				echo ${$field_name."mm"};
				$input = str_replace("<select name=\"dd\"","<select name=\"".$field_name."dd\" ",$arr[0]);
    			$input = str_replace("<select name=\"mm\"","<select name=\"".$field_name."mm\" ",$input);
    			$input = str_replace("<select name=\"yy\"","<select name=\"".$field_name."yy\" ",$input);
				$tmparr=explode("</select>",$input);
				$tmparr[0] = str_replace('<option value="'.${$field_name."dd"}.'"','<option value="'.${$field_name."dd"}.'" selected ',$tmparr[0]);
	    		$tmparr[1] = str_replace('<option value="'.${$field_name."mm"}.'"','<option value="'.${$field_name."mm"}.'" selected ',$tmparr[1]);
	    		$tmparr[2] = str_replace('<option value="'.${$field_name."yy"}.'"','<option value="'.${$field_name."yy"}.'" selected ',$tmparr[2]);
				$input=implode("</select>",$tmparr);
				
			}
		else
			$input = $arr[0];
		}
	elseif(eregi('^<input type="checkbox"',$arr[0])){
		$input = str_replace(">"," name=\"$field_name\" ".(isset($$field_name)?"checked":"")." style='border:0;'>",$arr[0]);
	}
	elseif(eregi('<input type="radio" value="([A-Za-z0-9]*)"',$arr[0],$regs)){
		$input = str_replace("<input","<input name=\"$field_name\" ".($$field_name+0==$regs[1]+0?"checked":"")." style='border:0;'",$arr[0]);
	}
	elseif(eregi('^<input type="file"',$arr[0])){
		$input = str_replace(">"," name=\"$field_name\">",$arr[0]);
	}
	elseif(eregi("^<input",$arr[0])){
		$input = str_replace(">"," name=\"$field_name\" value=\"".$$field_name."\" >",$arr[0]);
	
	}
	elseif(eregi("^<select",$arr[0])){
		$input = str_replace("<select","<select name=\"$field_name\" ",$arr[0]);
		$input = str_replace('<option value="'.$$field_name.'"','<option value="'.$$field_name.'" selected ',$input);
	}
	elseif(preg_match("/^<htmleditor width=\"(.*)\" height=\"(.*)\"/",$arr[0],$m)){
		if ($SETTINGS['use_wysiwyg']) {
//			$DOCUMENT_ROOT.$SETTINGS['html_editor_path']."/fckeditor.php";
			include_once($ADMIN_ROOT.$SETTINGS['html_editor_path']."/fckeditor.php");
			ob_start();
			$sBasePath = "/admin/".$SETTINGS['html_editor_path']."/";
			$oFCKeditor = new FCKeditor($field_name) ;
			$oFCKeditor->BasePath	= $sBasePath ;
			$oFCKeditor->Width		= $m[1];
			$oFCKeditor->Height		= $m[2];
			$oFCKeditor->Value		= $$field_name;
			$oFCKeditor->Create() ;
//			print_r($oFCKeditor);

			$html = ob_get_contents();
			ob_end_clean();
			$input = eregi_replace("<htmleditor[^>]{1,}>",$html,$arr[0]);
		}
		else{
			$arr[0]="<textarea cols=80 rows=15>";
		}
	}
	else{
    	$input = $arr[0]."<input type=hidden name=$field_name value=\"".$$field_name."\">";
	}
	
	if(eregi("^<textarea([^>]+)>(.*)",$arr[0],$regs)){
		$input = "<textarea $regs[1] name=\"$field_name\">".$$field_name."</textarea>".$regs[2]; 
	}
	return $input;
}

function smarty_helptip($params){
	global $smarty;
	$html_code = '<a href="#" OnClick="ShowHelpTip(\''.$params['code'].'\',\''.$params['anchor'].'\');return false;" title="Нажмите для получения подсказки">'.$smarty->fetch('ws_helptip_ico.tpl').'</a>';
    return $html_code;
}

function smarty_get_left_menu($params){
	global $smarty;
	global $SETTINGS;
	global $CATEGORY_DIR;
	
	$type = $params['type']==1?1:0;

	$menu_arr = Array();
	$res = pg_query("select * from ".($type==0?"school_categories":"thematic_categories")." where LENGTH(articul)<=2 and status=1 order by name asc");
	
	while($row = pg_fetch_object($res)){
		$tmp_arr = Array();
		$tmp_arr['name'] = stripslashes($row->name);
		$tmp_arr['url'] = $SITE_URL.$CATEGORY_DIR.($type==0?$SETTINGS['school_category_path']:$SETTINGS['thematic_category_path']).$row->url;
		$tmp_arr['level'] = strlen($row->articul)<=2?1:2;
		array_push($menu_arr,$tmp_arr);
		$res2 = pg_query("select * from ".($type==0?"school_categories":"thematic_categories")." where parent_id=$row->id and status=1 order by name asc");
		while($row2 = pg_fetch_object($res2)){
			$tmp_arr = Array();
			$tmp_arr['name'] = stripslashes($row2->name);
			$tmp_arr['url'] = $SITE_URL.$CATEGORY_DIR.($type==0?$SETTINGS['school_category_path']:$SETTINGS['thematic_category_path']).$row2->url;
			$tmp_arr['level'] = strlen($row2->articul)<=2?1:2;
			array_push($menu_arr,$tmp_arr);
		
		}
	}
	
	$smarty->assign("menu_arr",$menu_arr);
	
	$tpl_name = $type==0?"ws_left_menu_school.tpl":"ws_left_menu_thematic.tpl";
		
	$html_code = $smarty->fetch($tpl_name);

	return $html_code;
}

function smarty_get_top_menu($params){
	global $smarty;
	
/*
	$menu_arr = Array();
	$res = pg_query("select * from top_menu_items where status=1 order by pos");
	
	while($row = pg_fetch_object($res)){
		$tmp_arr = Array();
		$tmp_arr['name']  = stripslashes($row->name);
		$tmp_arr['title'] = stripslashes($row->title);
		$tmp_arr['url']   = stripslashes($row->url);
		$class = "menu1";
		if(eregi("^[/]?$row->url",$_SERVER['REQUEST_URI'])){
			$class = "menu2";
		}
		$tmp_arr['class']   = $class;

		array_push($menu_arr,$tmp_arr);


	}
	$smarty->assign("menu_arr",$menu_arr);
*/	
	$html_code = $smarty->fetch("ws_top_menu.tpl");

	return $html_code;
}

function smarty_get_bottom_menu($params){
	global $smarty;
	
	$menu_arr = Array();
	$res = pg_query("select * from bottom_menu_items where status=1 order by pos");
	
	while($row = pg_fetch_object($res)){
		$tmp_arr = Array();
		$tmp_arr['name']  = stripslashes($row->name);
		$tmp_arr['title'] = stripslashes($row->title);
		$tmp_arr['url']   = stripslashes($row->url);
		array_push($menu_arr,$tmp_arr);
	}
	$smarty->assign("menu_arr",$menu_arr);
	
	$html_code = $smarty->fetch("ws_bottom_menu.tpl");

	return $html_code;
}

function smarty_get_maths_topics($params){
	global $smarty;
	global $SITE_URL;
	global $CATEGORY_DIR;
	global $SETTINGS;
	
	$cols_num = 3;
	
	$them_cats_arr = Array();
	
	$res = pg_query("select * from proc_catalog(1,NULL,' -> ') where status=1");
	
	$items_in_col = ceil((pg_num_rows($res)/$cols_num));

	$curr_value = 1;
	
	while($row = pg_fetch_object($res)){
		$tmp_arr  = Array();
		
		$is_first = false;
		$is_last = false;
		if($curr_value==1){
			$is_first = true;
		}

		if($curr_value==$items_in_col){
			$curr_value = 1;
			$is_last = true;
		}
		else{
			$curr_value++;
		}	

		$tmp_arr["name"] = $row->name;
		$tmp_arr["is_first"] = $is_first;
		$tmp_arr["is_last"] = $is_last;
		$tmp_arr['url'] = $SITE_URL.$CATEGORY_DIR.$SETTINGS['thematic_category_path'].$row->url;
		array_push($them_cats_arr,$tmp_arr);
	}
	
	$smarty->assign("them_cats_arr",$them_cats_arr);

	$html_code = $smarty->fetch("ws_maths_topics.tpl");

	return $html_code;
}
?>