define("C_FS_PATH","/home/medifatimae/httpdocs");
define("C_APP_PATH","");
$appname="medifati_site";
define("C_PATH","../vnp1/");
require C_PATH."/main.php";
define ("C_TEXT_DF",C_TEXT_PT);
?>
/*################################################################################
# AUTOR: JS
# DATA: 29/05/2002
# DESCRICAO:
#   CLASS: 		 GenPortal
#	OBJECTIVO:   Cria os m�todos e funcionalidades gen�ricas para o
#			     Desenvolvimento de um Portal
#
# VERSAO: 1.0.0
################################################################################
# TABELA DE REVISOES
# 29/05/2002 | JS | Cria��o do Ficheiro
# 18/07/2002 | JS | Altera��o get_template_for_id_childs - problema allowed_child_templates
################################################################################*/
if (!$action=$_GET["action"]) $action=$_POST["action"];
if (!$id=$_GET["id"])         $id=$_POST["id"];
if (!$lang=$_GET["lang"])     $lang=$_POST["lang"];
if (!$cache=$_GET["cache"])   $cache=$_POST["cache"];
// prevent '14924%20and%20if%' - 28 jun
if( ! empty($id))
if( ! preg_match('/^\d{4,}$/', $id)) {
	header('HTTP/1.0 404 Not Found');
	die;
}
$lang = strtoupper($lang);
class SWPortal extends AbstractFrontend
{
	var $action;
	var $mark_id;
	var $mark;
	var $do_session_cache;
	# CONSTRUTOR
	function SWPortal($app,$fe,$id,$action,$lang,$cache,$file="index.php")
	{
		parent::AbstractFrontend($app,$fe,$id,$lang,$file,true);
		$this->action = $action;
		if ($cache) $_SESSION["_session_cache_status"]=$cache;
		if (!$_SESSION["_session_cache_status"]) !$_SESSION["_session_cache_status"]="yes";
		$this->do_session_cache=$_SESSION["_session_cache_status"];
		$this->do_stats();
	}
#####################################################################
	function get_action()  { return $this->action; }
#####################################################################
	function set_pid($pid) { return $this->pid = $pid; }
#####################################################################
	function get_pid() 	   { return $this->pid; }
#####################################################################
	function get_mark_id() { return $this->mark_id; }
#####################################################################
	function set_mark($id=0,$n=1)
	{
		if (!$id) $id = $this->id;
		if ($this->id==$this->start_id)
			$this->mark_id = $this->id;
		else
			$this->mark_id = $this->find_mark($this->id,$n);
		if ($this->mark_id)  $this->mark = $this->get_item($this->mark_id);
		return $this->mark;
	}
#####################################################################
	var $use_script_file= true;
	var $use_calendar_file = false;
	function draw_html_header()
	{
		header("Content-type: text/html;charset=utf-8");
		$this->dont_cache();
		echo "\n\n";
		echo "\n";
		echo "
\n";
		echo "\n".($this->title ? $this->title : 'sem nome')."\n";
		if ($this->add_head) echo $this->add_head."\n";
		echo $this->css."\n";
       	if ($this->use_script_file)	echo "\n";
		if ($this->use_calendar_file) echo "\n";
		echo "\n";
		echo "\n";
	}
	function draw_html_footer()
	{
		echo "";
	}
#####################################################################
	var $p_mark_id;
	var $p_mark;
	function get_p_mark()
	{
		$a_path = explode(",",$this->obj_nav->get_field(C_INPT_PATH));
		$this->p_mark_id = $a_path[1];
		if ($this->p_mark_id)
		{
			$this->p_mark = $this->get_item($this->p_mark_id);
		}
	}
#####################################################################
	function draw_nav_tree($start_id,$id,$home_txt="Home",$sep=">")
	{
		echo "index_file."?id=".$start_id."\">$home_txt";
		$this->build_nav($start_id,$id,$sep);
	}
	function build_nav($start_id,$id,$sep)
	{
		$sql = "SELECT ID,TITLE,PARENT_ID FROM RELATIONS R,CONTENT C WHERE C.ID = R.CHILD_ID AND R.CHILD_ID = $id";
		$rs = $this->obj_mydb->query($sql);
		$row = $this->obj_mydb->fetch($rs);
		if ($start_id!=$id)
		{
			$this->build_nav($start_id,$row["PARENT_ID"],$sep);
			echo " $sep ";
			echo "index_file."?id=".$row["ID"]."\">".$row["TITLE"]."";
		}
		return;
	}
#####################################################################
#####################################################################
	function login_screen()
	{
		if ($this->pid) $redir=$this->pid;
		echo "";
		exit;
	}
######################################################################################################
	function create_content()
	{
		$second_time = $this->obj_nav->get_field(C_INPT_SECONDTIME);
		$campo = $this->obj_templ->get_campo();
		$mode = $this->obj_nav->get_field(C_INPT_MODE);
		$this->obj_templ->retrieve_fields();
		$this->obj_templ->set_special_args_defaults();
		if (!$this->obj_nav->get_field(C_INPT_MODE))
		{
			if ($this->item["EXPAND"]) $mode = C_MODE_NO; else $mode = C_MODE_SHOW;
			//$this->obj_nav->obj_request->set_field(C_INPT_MODE,$mode);
			//$this->obj_nav->set_field(C_INPT_MODE,$mode);
		}
		if($mode==C_MODE_NEW || $mode==C_MODE_NEW_FOLDER)
		{
			if($mode==C_MODE_NEW_FOLDER)
				$this->obj_templ->set_doc_type(C_FOLDER);
			if($second_time == 1)
				$this->obj_templ->retrieve_data_by_http_vars($this->http_vars);
			$obj_form =& $this->obj_templ->get_form();
			if($obj_form->on_new())
			{
				/// Prepara Evento
				$this->obj_templ->parse_events();
				// evento
				if($this->obj_templ->call_event(C_ON_NEW))
				{
					$this->obj_templ->desenha(C_DESENHA_EDIT);
				}
			}
		}
		elseif($mode==C_MODE_SHOW)
		{
			$this->obj_templ->retrieve_data_by_id($this->prepare_id());
			$obj_form =& $this->obj_templ->get_form();
			if($obj_form->on_show())
			{
				/// Prepara Evento
				$this->obj_templ->parse_events();
				// evento
				if($this->obj_templ->call_event(C_ON_SHOW))
				{
					$this->obj_templ->desenha(C_DESENHA_SHOW);
				}
			}
		}
		elseif($mode==C_MODE_EDIT)
		{
			if($second_time == 1)
				$this->obj_templ->retrieve_data_by_http_vars($this->http_vars);
			else
				$this->obj_templ->retrieve_data_by_id($this->prepare_id());
			$obj_form =& $this->obj_templ->get_form();
			if($obj_form->on_edit())
			{
				/// Prepara Evento
				$this->obj_templ->parse_events();
				// evento
				if($this->obj_templ->call_event(C_ON_EDIT))
				{
					$this->obj_templ->desenha(C_DESENHA_EDIT);
				}
			}
		}
		elseif($mode==C_MODE_NO || !$this->obj_nav->get_field(C_INPT_MODE) )
		{
			$this->obj_templ->lista();
		}
	}
######################################################################################################
	function do_link($text,$id,$path="",$args="",$class="",$target="")
	{
		$obj_nav = new Navigator($this->obj_mydb,$this->http_vars);
		$obj_nav->set_file( $this->index_file );
		if ($class) { $class1 = "class=\"".$class."\"";}
		if ($target) { $target1 = " target=\"".$target."\"";}
		if ($path=="root")
		{
			$obj_nav->clear_all();
			$obj_nav->arr_vars[C_INPT_MODE]="";
			$obj_nav->down_mode="";
			$obj_nav->arr_vars[C_INPT_PATH]="";
			$obj_nav->down_path= ",$id";
            $obj_nav->down_id=$id;
		}
		else
		{
			if ($path)
			{
				$obj_nav->down_path= ",".$path;
			}
			else
			{
				$obj_nav->down_path=$obj_nav->arr_vars[C_INPT_PATH].",$id";
			}
            $obj_nav->down_id=$id;
		}
	    $lk = $obj_nav->file_to."?".$obj_nav->to_get(C_GO_DOWN);
		echo "".$text."";
	}
######################################################################################################
	function do_go_back($text,$class="",$args="",$stay_in_id=false)
	{
		$obj_nav = new Navigator($this->obj_mydb,$this->http_vars);
		$obj_nav->set_file( $this->index_file );
		if ($class) { $class1 = "class=\"".$class."\"";}
		if ($target) { $target1 = " target=\"".$target."\"";}
		if (!$stay_in_id)
		{
			$arr = explode(",",$obj_nav->up_path);
			$tmp_id = $arr[count($arr)-1];
        	if(count($arr)>1)
        	{
        		$obj_nav->up_path="";
            	for($i=1;$i<(count($arr)-1);$i++) $obj_nav->up_path .=",".$arr[$i];
        	}
        	$arr = explode(",",$obj_nav->up_path);
        	$obj_nav->up_id = $arr[(count($arr)-1)];
        	if (!$obj_nav->up_id) $obj_nav->up_id =$tmp_id;
		}
		else
		{
			$obj_nav->up_id = $this->id;
		}
		$obj_nav->arr_vars[C_INPT_MODE]="";
		$obj_nav->up_mode="";
	    $lk = $obj_nav->file_to."?".$obj_nav->to_get(C_GO_UP);
		echo "".$text."";
	}
######################################################################################################
	function generate_user_name($prefix="",$n_digit=4)
	{
		### novo utilizador ####
		$sql = "SELECT MAX(ROW_ID) AS M FROM T_USERS";
		$rs  = $this->obj_mydb->query($sql);
		$row = $this->obj_mydb->fetch($rs);
		$seq = $row["M"]+1;
		$ret = $prefix.sprintf("%0".$n_digit."d",$seq);
		return $ret;
	}
######################################################################################################
	function generate_password($n_digits=6)
	{
		#### vamos atribuir uma password
		#### 48-57 numeros
		#### 65-90 alfa caps
		#### 97-122 alfa small
		srand ((double) microtime() * 10000000);
		for ($i=0;$i<$n_digits;$i++)
		{
			srand ((double) microtime() * 10000000);
			## seleccionar se � numero,alfa caps ou alfa small
			$n = rand(1,3);
			srand ((double) microtime() * 10000000);
			if ($n==1){$c = rand(48,57);}
			if ($n==2){$c = rand(65,90);}
			if ($n==3){$c = rand(97,122);}
			$ret.=chr($c);
		}
		return $ret;
	}
######################################################################################################
	function draw_ticket_box($txt,$w=300,$h=60,$speed=1)
	{
		?>
		
		
	}
}
################################################################################*/
require_once C_PATH."/class/forms/Form.php";
class SWPForm extends Form
{
	var $id;
	var $mark_id;
	var $form_mode=C_DESENHA_SHOW;
	var $item;
	var $mode;
	function desenha_show() { $this->draw_form(); }
	function desenha_edit()
	{
		$this->desenha_form_edit();
	}
	function get_value($field,$obj_templ=0)
	{
		if (!$obj_templ) $obj_templ =& $this->obj_templ;
		return $obj_templ->get_show_valor($field);
	}
	function get_arg_value($field,$obj_templ=0)
	{
		if (!$obj_templ) $obj_templ = $this->obj_templ;
		return $obj_templ->obj_navigator->get_field($field);
	}
	function get_description($field,$obj_templ=0)
	{
		if (!$obj_templ) $obj_templ = $this->obj_templ;
		return $obj_templ->arr_fields[$field]->get_descricao();
	}
	function desenha_form_edit()
	{
		$this->obj_templ->retrieve_inputs();
		$this->prepare_commit();
		$this->start_form();
		$this->form_mode = C_DESENHA_EDIT;
		$this->draw_form();
		$this->edit_scripts();
		if($this->do_recalc) $this->call_recalc();
		$this->end_form($this->obj_templ->get_step_var());
	}
	function draw_form()
	{
		$this->id = $this->obj_templ->obj_frontend->id;
		$this->mark_id = $this->obj_templ->obj_frontend->mark_id;
		$this->item = $this->obj_templ->obj_frontend->item;
		$this->mode = $this->obj_templ->obj_navigator->get_field(C_INPT_MODE);
		$this->build();
	}
	function get_search_value($field)
	{
		return $this->obj_templ->obj_navigator->get_search_field($field);
	}
	function get_external_item($id)
	{
		return $this->obj_templ->obj_frontend->get_item($id);
	}
}
function navigation_do_link(&$obj_nav,$current_page,$page_count)
{
	$obj_lk = $obj_nav->get_new_link();
	if($obj_lk->prepare_page($current_page,$page_count))
	{
		return $obj_lk->to_link();
	}
	return "";
}
require_once C_PATH."/class/listagens/AListagem.php";
class SWPList extends AListagem
{
	var $arr_data;     // guarda o show_valor
	var $arr_values;   // guarda o valor;
	var $curr_record;
	var $page_size;
	var $id;
	var $mark_id;
	var $item;
	var $mode;
	var $show_sql = false;
	function draw()
	{
		$this->link_navigation_function = "navigation_do_link";
		$this->id = $this->obj_template->obj_frontend->id;
		$this->mark_id = $this->obj_template->obj_frontend->mark_id;
		$this->item = $this->obj_template->obj_frontend->item;
		$this->mode = $this->obj_template->obj_navigator->get_field(C_INPT_MODE);
		$this->http_vars = $this->obj_template->obj_navigator->get_http_vars();
       	$this->build();
	}
	function set_page_size($page_size)
	{
		$this->obj_navigator->obj_request->set_field(C_INPT_PAGE_SIZE,$page_size);
		$this->page_size=$page_size;
	}
	function get_records_by_page($valores = false,$echo=false)
	{
		list($rs,$this->page_count,$this->total_records) = $this->obj_template->do_select($this->use_limit,$this->filter,$this->cond,$this->select_order,$echo);
		while($row = $this->obj_mydb->fetch($rs))
       	{
       		$item = $this->obj_mydb->fast_get_item($row["ID"]);
			$this->obj_template->retrieve_data_by_row($row);
			$this->arr_data[] = @array_merge($this->obj_template->get_data_array($valores),array("TEMPLATE_ID" => $item["TEMPLATE_ID"], "PARENT_ID" => $item["PARENT_ID"], "EXPAND" => $item["EXPAND"], "CHILD_TEMPLATE_ID" => $item["CHILD_TEMPLATE_ID"]));
			$this->arr_values[] = @array_merge($row,array("TEMPLATE_ID" => $item["TEMPLATE_ID"], "PARENT_ID" => $item["PARENT_ID"], "EXPAND" => $item["EXPAND"], "CHILD_TEMPLATE_ID" => $item["CHILD_TEMPLATE_ID"]));
       	}
       	if ($this->page_size>count($this->arr_data)) $this->page_size = count($this->arr_data);
	}
	function get_image($field)
	{
		$img = $this->get_field($field);
		return common::get_image($img);
	}
	function image_exists($field)
	{
		if ($this->get_field($field)) return true; else return false;
	}
	function get_row($curr_record)
	{
		$this->curr_record = $curr_record;
		$this->obj_template->retrieve_data_by_row($this->arr_data[$this->curr_record]);
	}
	function get_search_value($field)
	{
		return $this->obj_template->obj_navigator->get_search_field($field);
	}
	function get_arg_value($field)
	{
		return $this->obj_template->obj_navigator->get_field($field);
	}
	function get_value($field)
	{
		return $this->obj_template->get_show_valor($field);
	}
	function draw_page_navigator($no_pages_to_show=20)
	{
		$this->draw_navigation($this->page_count,$no_pages_to_show);
	}
	function do_url($id="")
	{
		if (!$id) $id = $this->arr_data[$this->curr_record]["ID"];
		return $this->index_file."?id=".$id;
	}
	function get_all_records($valores = false)
	{
		list($rs,$this->page_count,$this->total_records) = $this->obj_template->do_select(false,$this->filter,$this->cond,$this->select_order,$this->show_sql);
       	while($row = $this->obj_mydb->fetch($rs))
       	{
       		$item = $this->obj_mydb->fast_get_item($row["ID"]);
			$this->obj_template->retrieve_data_by_row($row);
			$this->arr_data[]   = array_merge($this->obj_template->get_data_array($valores),array("TEMPLATE_ID" => $item["TEMPLATE_ID"], "PARENT_ID" => $item["PARENT_ID"], "EXPAND" => $item["EXPAND"], "CHILD_TEMPLATE_ID" => $item["CHILD_TEMPLATE_ID"]));
			$this->arr_values[] = array_merge($row,array("TEMPLATE_ID" => $item["TEMPLATE_ID"], "PARENT_ID" => $item["PARENT_ID"], "EXPAND" => $item["EXPAND"], "CHILD_TEMPLATE_ID" => $item["CHILD_TEMPLATE_ID"]));
       	}
       	return $this->arr_data;
	}
	function set_search_cond()
	{
		$this->search_cond = "";
		$this->get_search_cond_template();
		$this->obj_template->set_cond($this->search_cond);
	}
	function prepare_search()
	{
	    $this->get_search_fields();
	}
    ###########################################################################
	### SEARCH AVANCADA
	function draw_search_avancada()
	{
		echo "\n";
    }
	###########################################################################
	### SEARCH
	function draw_search()
	{
		if(count($this->arr_fields_search_avancada)>0)
		{
			$obj_lk = $this->obj_navigator->get_new_link();
			$obj_lk->set_search_field("_SEARCH_DONE","");
			echo "to_link()."\">".C_TEXT_PESQUISA_AVANCADA."
";
		}
		if(count($this->arr_fields_search)>0)
		{
			echo "\n";
		}
	}
	function desenha_search_avancada()
	{
		$obj_lk = $this->obj_navigator->get_new_link();
		$obj_lk->set_field(C_INPT_SEARCH,"_SEARCH_DONE=1");
		$lk = $obj_lk->to_link();
		echo "
";
		echo "";
		echo "Efectue a sua busca de registos baseada nos seguintes campos,
		ou então clique aqui para os listar todos os registos: ";
		echo "";
		echo "
";
		echo "";
		common::ftext("pesquisa","navy","16pt",true);
		echo "
";
		echo "
";
		echo "";
		$arr = $this->merge_arr_fields_search();
		for($i=0;$iget_new_input();
			echo "";
			echo "| ".$obj_inpt->get_descricao()." | \n";
			echo "\n";
				$obj_inpt->desenha_pesquisa();
			echo " | ";
			echo "
";
			next($arr);
		}
		echo "
";
	}
	function desenha_search()
	{
		echo "";
		echo "";
		$arr = $this->arr_fields_search;
		for($i=0;$iget_new_input();
			echo "| ".$obj_inpt->get_descricao()." | \n";
			echo "\n";
				$obj_inpt->desenha_pesquisa();
			echo " | ";
			next($arr);
		}
		echo "";
		echo "
";
	}
	function build() { }
}
?>