00001 <?php
00002 ############################################################################
00003 # Copyright (C) 2004 by Paolo Armani & Stefano Menegon #
00004 # armani7@supereva.it menegon@itc.it #
00005 # #
00006 # This program is free software; you can redistribute it and#or modify #
00007 # it under the terms of the GNU Library General Public License as #
00008 # published by the Free Software Foundation; either version 2 of the #
00009 # License, or (at your option) any later version. #
00010 # #
00011 # This program is distributed in the hope that it will be useful, #
00012 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
00013 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
00014 # GNU General Public License for more details. #
00015 # #
00016 # You should have received a copy of the GNU Library General Public #
00017 # License along with this program; if not, write to the #
00018 # Free Software Foundation, Inc., #
00019 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #
00020 ############################################################################
00021
00022
00024
00030 function js_sanitize($string){
00031 $string=str_replace("\\","\\\\",$string);
00032 $string=str_replace("\"","\\\"",$string);
00033 $string=str_replace("'","\'",$string);
00034 return $string;
00035 }
00036
00037
00039
00047 function array_to_html($o,$space=0, $keys=1, $encode=true){
00048 if(gettype($o)=="array"){
00049 reset($o); $val=array();
00050 while(list($k,$v)=each($o)){
00051 $tmp=str_repeat(" ",$space+4)."<li>";
00052 if($keys==1 || ($keys==2 && gettype($v)=="array")) $tmp.="$k: ";
00053 $tmp.=array_to_html($v,$space+8,$keys,$encode)."\n".str_repeat(" ",$space+4)."</li>".str_repeat(" ",$space+4);
00054 $val[]=$tmp;
00055 }
00056 $return=(count($val)>0?"\n".str_repeat(" ",$space)."<ul>\n".join("\n",$val)."\n".str_repeat(" ",$space)."</ul>":"");
00057 }
00058 else $return=$encode?htmlentities($o):$o;
00059 return $return;
00060 }
00061
00062
00063
00065
00076 function html_select(
00077 $name,$data,$default=false,$assoc=1,$padding=0,$mustdefine=1,$onchange=""){
00078 if(settype($padding,"integer")&&$padding>0){
00079 $padding=str_repeat(" ",$padding);
00080 }else $padding="";
00081 $select=$padding.
00082 "<select name=\"$name\"".o_iftrue(" onChange=\"%s\"",js_sanitize($onchange)).">\n";
00083 if(!$mustdefine)$select.="$padding <option value=\"\"> </option>\n";
00084 if(is_array($data) && count($data)){
00085 reset($data);
00086 while(list($d,$x)=each($data)){
00087 if(is_array($x))$x=$x[0];
00088 $s=((($assoc?$d:$x)==$default )&& !($default===false))?" selected":"";
00089 $select.="$padding <option".($assoc?" value=\"".htmlentities($d)."\"":"")."$s>$x</option>\n";
00090 }
00091 }
00092 $select.="$padding</select>\n";
00093 return $select;
00094 }
00095
00096
00098
00104 function history_replace($url) {
00105 $html=<<<HTML
00106 <html>
00107 <head><meta http-equiv="refresh" content="1;url=$url"></head>
00108 <BODY onLoad="window.location.replace('$url')">
00109 If you are not automatically redirected, please <a href="$url">click</a> here.
00110 </body>
00111 </html>
00112 HTML;
00113 return $html;
00114 }
00115
00116
00118
00128 function var_dump_array($arr,&$out,$var_name=''){
00129 if(is_array($arr)){
00130 foreach($arr as $key=>$val){
00131 var_dump_array($val,&$out,$var_name==''?$key:$var_name."[".$key."]");
00132 }
00133 }
00134 else{
00135 $out[]=array("name"=>$var_name, "value"=>$arr);
00136 }
00137 }
00138
00140
00152 function arr_to_hidden($arr, $baseName=''){
00153 $src='';
00154 var_dump_array($arr,&$out, $baseName);
00155 foreach($out as $key=>$val){
00156 $src.='<input type="hidden" name="'.htmlentities($val["name"]).'" value="'.htmlentities($val["value"])."\" />\n";
00157 }
00158 return($src);
00159 }
00160
00162
00174 function arr_to_url($arr, $baseName=''){
00175 $url='';
00176 var_dump_array($arr,&$out, $baseName);
00177 foreach($out as $key=>$val){
00178 $url.=$url!=''?'&':'';
00179 $url.=htmlentities(urlencode($val["name"]))."=".htmlentities(urlencode($val["value"]));
00180 }
00181 return($url);
00182 }
00183
00184 function out($string, $stdout=true){
00185 if($stdout) echo $string;
00186 else return $string;
00187 }
00188
00190
00210 function html_table($arr, $headerCol=1, $headerRow=0, $noInterpolate=0, $headerColNoInterpolate=0, $headerRowNoInterpolate=0, $noInt_type=0, $transpost=0, $stdout=true){
00211 $out='';
00212 $num=count($arr);
00213 if($num==0){
00214 return;
00215 }
00216
00217 $hCInt=$headerColNoInterpolate;
00218 $hRInt=$headerRowNoInterpolate;
00219
00220 if ($transpost){$arr=transpose($arr);}
00221
00222 reset($arr);
00223 $keyarr=key($arr);
00224 if(!$noInt_type){
00225 if(is_array($noInterpolate)){
00226 foreach($noInterpolate as $key=>$val){
00227 if(is_array($val)) continue;
00228 foreach($arr as $key1=>$val1){
00229 $int[$key][$key1]=$noInterpolate[$key];
00230 }
00231 }
00232 foreach($noInterpolate as $key=>$val){
00233 if(!is_array($val)) continue;
00234 foreach($noInterpolate[$key] as $key1=>$val1){
00235 $int[$key][$key1]=$noInterpolate[$key][$key1];
00236 }
00237 }
00238 }
00239 else{
00240 foreach($arr as $key=>$val){
00241 foreach($arr[$key] as $key1=>$val1){
00242 $int[$key1][$key]=$noInterpolate;
00243 }
00244 }
00245 }
00246 }
00247 else{
00248 if(is_array($noInterpolate)){
00249 foreach($noInterpolate as $key=>$val){
00250 if(!is_array($val)){
00251 foreach($arr[$keyarr] as $key1=>$val1){
00252 $int[$key1][$key]=$noInterpolate[$key];
00253 }
00254 }
00255 else{
00256 foreach($noInterpolate[$key] as $key1=>$val1){
00257 $int[$key1][$key]=$noInterpolate[$key][$key1];
00258 }
00259 }
00260 }
00261 }
00262 else{
00263 foreach($arr as $key=>$val){
00264 foreach($arr[$key] as $key1=>$val1){
00265 $int[$key][$key1]=$noInterpolate;
00266 }
00267 }
00268 }
00269 }
00270
00271 $noInterpolate=$int;
00272
00273 reset($arr);
00274 $key1=key($arr);
00275 if(!is_array($headerRowNoInterpolate)){
00276 $headerRowNoInterpolate=array();
00277 foreach($arr[$key1] as $key=>$val){
00278 $headerRowNoInterpolate[$key]=$hRInt;
00279 }
00280 }
00281
00282 if(!is_array($headerColNoInterpolate)){
00283 $headerColNoInterpolate=array();
00284 foreach($arr as $key=>$val){
00285 $headerColNoInterpolate[$key]=$hCInt;
00286 }
00287 }
00288
00289 $out.=out( "\n\n", $stdout);
00290 reset($arr);
00291
00292 if($headerCol){
00293 $out.=out( " <tr>\n", $stdout);
00294 if($headerRow==$headerCol){
00295 $key=($headerRow==2 && $headerCol==2)?current(current($arr)):'';
00296 $out.=out( " <th>$key</th>\n", $stdout);
00297 }
00298
00299 $i=0;
00300 $tmp=($headerRow==1 && $headerCol==2);
00301 foreach($arr[$key1] as $key=>$val){
00302 if($headerCol==2){
00303 if($i==0 && $headerRow==2){$i++; continue;}
00304 $header=$val;
00305 }
00306 else{
00307 $header=$key;
00308 }
00309 $header=($headerColNoInterpolate[$key1])?$header:htmlentities($header);
00310 if($tmp){$i--; $tmp=false; $header1=($headerColNoInterpolate[$key1])?$key1:htmlentities($key1); $out.=out( " <th>$header1</th>\n", $stdout);}
00311 $out.=out( " <th>$header</th>\n", $stdout);
00312 $i++;
00313 }
00314 $out.=out( " </tr>\n", $stdout);
00315 }
00316
00317
00318 $i=0;
00319 foreach($arr as $key1=>$val1){
00320 if($headerCol==2 && $i==0) {$i++; continue; }
00321 $out.=out( " <tr>\n", $stdout);
00322 $j=0;
00323 foreach($val1 as $key=>$val){
00324 if(($headerRow) && $j==0){
00325 if($headerRow==2){
00326 $header=$val;
00327 }
00328 else{
00329 $header=$key1;
00330 }
00331 $header=($headerRowNoInterpolate[$key])?$header:htmlentities($header);
00332 $out.=out( " <th>$header</th>\n", $stdout);
00333 }
00334 if($headerRow==2 && $j==0) {$j++; continue; }
00335
00336 $val=$noInterpolate[$key][$key1]?$val:htmlentities($val);
00337 $out.=out( " <td>$val</td>\n", $stdout);
00338 $j++;
00339 }
00340 $out.=out( " </tr>\n", $stdout);
00341 $i++;
00342 }
00343 return $out;
00344 }
00345
00346
00347
00348
00350
00365 function search_bar($searchUrl, $previousUrl, $nextUrl, $otherUrl, $total,$offset=1,$limit=10,$numPages=10, $nameOffset="searchOffset"){
00366 if ($total<=0){
00367 return "<div id=\"searchBar\"><p class=\"noResults\">Nessun dato trovato!!!</p></div>\n";
00368
00369 }
00370 if(is_array($searchUrl)){
00371 $post=true;
00372
00373 $hidden=arr_to_hidden($searchUrl);
00374 }
00375 else{
00376 $post=false;
00377 $searchUrl.=ereg('\?.*\&.*$',$searchUrl)?"&$nameOffset=":"?$nameOffset=";
00378 }
00379
00380 $pageNum=ceil(($offset-1+$limit)/$limit);
00381 $bar=" <td title=\"Pagina corrente\"><br /> $pageNum </td>\n";
00382 $pages=1;
00383 $previousPages=0;
00384 $nextPages=0;
00385 $endNext=$endPrevious=0;
00386 while(($pages<$numPages)&& !($endNext && $endPrevious)){
00387 if(($offset+$limit*($nextPages+1)<=$total)){
00388 $nextPages++;
00389 $pages++;
00390 $pageNum=ceil(($offset+$limit*($nextPages+1)-1)/$limit);
00391
00392 $bar.=$post?(' <td><form method="POST">'.$hidden.'<input type="hidden" name="'.$nameOffset.'" value="'.($offset+$limit*$nextPages).'" /> <input title="Pagina '.$pageNum.'" type="submit" value="'.$pageNum.'" /></form></td>'):(' <td><a href="'.$searchUrl.($offset+$limit*$nextPages).'" title="Pagina '.$pageNum.'">'.$pageNum.'<br /><img src="'.$otherUrl."\" /></a></td>\n");
00393 }
00394 else{
00395 $endNext=1;
00396 }
00397 if(($pages<$numPages)&& ($offset-($limit*$previousPages+1)>=1)){
00398 $previousPages++;
00399 $pages++;
00400 $pageNum=ceil(($offset-$limit*($previousPages-1)-1)/$limit);
00401 $bar=$post?(' <td><form method="POST">'.$hidden.'<input type="hidden" name="'.$nameOffset.'" value="'.($offset-$limit*$previousPages).'" /> <input title="Pagina '.$pageNum.'" type="submit" value="'.$pageNum.'" /></form></td>'.$bar):(' <td><a href="'.$searchUrl.($offset-$limit*$previousPages).'" title="Pagina '.$pageNum.'">'.$pageNum.'<br /><img src="'.$otherUrl."\" /></a></td>\n".$bar);
00402 }
00403 else{
00404 $endPrevious=1;
00405 }
00406 }
00407
00408 if($offset>1){
00409 $bar=$post?(' <td><form method="POST">'.$hidden.'<input type="hidden" name="'.$nameOffset.'" value="'.($offset-$limit).'" /> <input title="Pagina precedente" type="submit" value="<<" /></form></td>'.$bar):(' <td><a href="'.$searchUrl.($offset-$limit).'" title="Pagina precedente"><img src="'.$previousUrl."\" /></a></td>\n".$bar);
00410 }
00411
00412 if($offset+$limit<=$total){
00413 $bar.=$post?' <td><form method="POST">'.$hidden.'<input type="hidden" name="'.$nameOffset.'" value="'.($offset+$limit).'" /> <input title="Pagina successiva" type="submit" value=">>" /></form></td>':' <td><a href="'.$searchUrl.($offset+$limit).'" title="Pagina successiva"><img src="'.$nextUrl."\" /></a></td>\n";
00414 }
00415
00416 # $bar="\n<div id=\"searchBar\"><p><center>\n <table>\n <tr>\n".$bar." </tr>\n </table> \n</center></p></div>\n\n";
00417 $bar="\n<div id=\"searchBar\"><p>\n <table>\n <tr>\n".$bar." </tr>\n </table> \n</p></div>\n\n";
00418
00419 return $bar;
00420 }
00421
00422
00424
00426
00427
00429
00438 function date_select($fieldname,$default="",$mustdefine=1,$locale="it_IT",$years=""){
00439
00440 if($default){
00441 list($year,$month,$day)=split('-',$default);
00442 }elseif($mustdefine){
00443 list($year,$month,$day)=split('-',date("Y-m-d"));
00444 }else{
00445 $year=$month=$day="";
00446 }
00447
00448 $save=setlocale(LC_TIME,$locale);
00449 for($i=1;$i<=12;$i++){
00450 $months[$i]=strftime("%B",mktime(0,0,0,$i,1,2000));
00451 }
00452 setlocale(LC_TIME,$save);
00453
00454 if(!is_array($years)){
00455 $y=($year?$year:date('Y'));
00456 $years=range($y-5,$y+5);
00457 }
00458
00459 $dateselect=
00460 html_select(
00461 $fieldname."[day]",range(1,31),$day,0,0,$mustdefine
00462 )."\n".
00463 html_select(
00464 $fieldname."[month]",$months,$month,1,0,$mustdefine
00465 )."\n".
00466 html_select(
00467 $fieldname."[year]",$years,$year,0,0,$mustdefine
00468 );
00469 return $dateselect;
00470 }
00471
00473
00479 function time_select($fieldname, $default=false, $mustdefine=1){
00480 if($default){
00481 list($hour,$minute)=split(':',$default);
00482 }
00483 elseif($mustdefine){
00484 list($hour,$minute)=split('-',date("G-i"));
00485 }
00486 else{
00487 $hour=$minute=false;
00488 }
00489
00490 $timeselect=
00491 html_select(
00492 $fieldname."[hour]",range(0,23),$hour,0,0,$mustdefine
00493 )."\n".
00494 html_select(
00495 $fieldname."[minute]",range(0,59),$minute,0,0,$mustdefine
00496 );
00497 return $timeselect;
00498 }
00499
00500
00502
00507 function JavaScript($code=''){
00508 static $javascript='';
00509 $javascript.=$code;
00510 return $javascript;
00511 }
00512
00514
00519 function Alert($message=''){
00520 static $alert='';
00521 $alert.=($message!='')?"- $message\\n":'';
00522 return $alert;
00523 }
00524
00525
00527
00533 function Error($message='', $goBack=0){
00534 static $errors=array();
00535 if($message!=''){$errors[]=ereg_replace("\n"," ",$message);}
00536 if($goBack){HistoryGo(-1);}
00537 return $errors;
00538 }
00539
00541
00546 function HistoryGo($page=''){
00547 static $HistoryGo='';
00548 if($page!=''){$HistoryGo=$page;}
00549 return $HistoryGo;
00550 }
00551
00553
00557 function WriteJavascript(){
00558 $errors=Error();
00559 if(count($errors)>0){
00560 foreach($errors as $key=>$val){
00561 $val=ereg_replace("\n"," ",$val);
00562 Alert(addslashes($val));
00563 }
00564 Alert(str_repeat("- ",40)."\\nFor these errors could you contact : ".$_SERVER["SERVER_ADMIN"]."\\n".str_repeat("- ",41));
00565 }
00566 if (Alert()!=''){$javascript="alert(\"".Alert()."\");\n".JavaScript((HistoryGo()!='')?"history.go(".HistoryGo().")":'');}
00567 if ($javascript!=''){
00568 $javascript="<script language=\"javascript\"><!--\n".$javascript."//--></script>\n";
00569 echo $javascript;
00570 }
00571 }
00572
00573
00575
00580 function html_head($code=''){
00581 static $head='';
00582 $head.=$head."\n";
00583 return $head;
00584 }
00585
00586
00588
00591 function postgis_forminput($fieldname,$default='',$type='POINT'){
00592 $postgisInput='';
00593 if(ereg("(POINT|POLYGON|LINE)\(([, 0-9]+)\)",$default,$regs)){
00594 $type=$regs[1];
00595 switch($type){
00596 case 'POINT':
00597 list($east,$north) =preg_split("[ ]",$regs[2]);
00598 break;
00599 }
00600 }
00601 switch($type){
00602 case 'POINT':
00603 $postgisInput .= "<input name=" . $fieldname . "[east] value=\"$east\">";
00604 $postgisInput .= "<input name=" . $fieldname . "[north] value=\"$north\">";
00605 break;
00606 }
00607 return $postgisInput;
00608 }
00609
00610
00611
00613
00624 class Menu {
00625 var $type;
00626 var $name;
00627 var $sub_menu;
00628 var $attrib;
00629 var $position;
00630
00636 function Menu($name="Menu", $attrib='', $type="menu") {
00637 $this->name = $name;
00638 $this->type = $type;
00639 $this->attrib = $attrib;
00640 }
00641
00645 function set_type($type){
00646 $this->type=$type;
00647 if(is_array($this->sub_menu)){
00648 foreach($this->sub_menu as $key=>$val){
00649 $this->sub_menu[$key]->set_type($type);
00650 }
00651 }
00652 }
00653
00661 function add($menu,$position=''){
00662 if(is_array($position)&& (count($position)>0)){
00663 $actual=array_shift($position);
00664 $this->sub_menu[$actual]->add($menu,$position);
00665 }
00666 else{
00667 $this->sub_menu[$menu->name]=$menu;
00668 }
00669 }
00670
00676 function rm($position){
00677 if(is_array($position) && (count($position)>1)){
00678 $actual=array_shift($position);
00679 $this->sub_menu[$actual]->rm($position);
00680 }
00681 elseif(is_array($position)){
00682 unset($this->sub_menu[$position[key($position)]]);
00683 }
00684 }
00685
00686
00692 function print_section($position='', $print_selected, $indent=0){
00693 $str='';
00694 if(is_array($this->sub_menu)){
00695 foreach($this->sub_menu as $key=>$val){
00696 $attrib=$this->sub_menu[$key]->attrib;
00697 if($position==$this->sub_menu[$key]->name){
00698 $attrib["class"]='current';
00699 }
00700
00701
00702 if($print_selected || ($position!=$this->sub_menu[$key]->name) || !(is_array($this->sub_menu[$key]->sub_menu)&&(count($this->sub_menu[$key]->sub_menu)>0))){
00703 $str.=" <tr><td>";
00704 $str.=$indent?"<div style=\"margin-left: ".$indent."px;\"><a":"<a";
00705 if(is_array($attrib)){
00706 foreach($attrib as $key1=>$val1){
00707 $str.=" $key1=\"$val1\"";
00708 }
00709 }
00710 $str.='>'.$this->sub_menu[$key]->name."</a>";
00711 $str.=$indent?"</div>":'';
00712 $str.="</td></tr>\n" ;
00713 }
00714 }
00715 $str.=" <tr><td> </td></tr>\n";
00716 }
00717 return $str;
00718 }
00719
00731 function print_menu($position='', $print_all=0, $print_selected=1, $indent_px=0, $header=1){
00732 if (!is_array($position)){
00733 $position=$this->position;
00734 }
00735 if(is_array($this->sub_menu) && (count($this->sub_menu)>0)){
00736 if(!is_array($indent_px)){
00737 $indent[0]=$indent_px;
00738 $indent[1]=0;
00739 }
00740 else{
00741 $indent=$indent_px;
00742 }
00743 $str=" <tr><th>";
00744 $str.=($indent[0]*$indent[1])?'<div style="margin-left: '.($indent[0]*$indent[1]).'px;">':'';
00745 $str.="<p class=\"sezione\"";
00746
00747
00748
00749
00750
00751 $str.=">".$this->name."</p>";
00752 $str.=($indent[0]*$indent[1])?'</div>':'';
00753 $str.="</th></tr>\n";
00754
00755 $indent[1]++;
00756 $str.=$this->print_section(is_array($position)?$position[key($position)]:'',$print_selected,$indent[0]*$indent[1]);
00757 if($print_all){
00758 if(is_array($position) && count($position)>0){
00759 array_shift($position);
00760 }
00761 foreach($this->sub_menu as $key=>$val){
00762 $str.=$this->sub_menu[$key]->print_menu($position,1,$print_selected,$indent,0);
00763 }
00764 }
00765 else{
00766 if(is_array($position) && (count($position)>0) ){
00767 reset($position);
00768 $actual=array_shift($position);
00769 $str.=$this->sub_menu[$actual]->print_menu($position,0,$print_selected,$indent,0);
00770 }
00771 }
00772 }
00773 if($header){
00774 $str=" <!-- Begin Menu -->\n <div id=\"".$this->type."\">\n <table>\n".$str." </table>\n </div> \n <!-- End Menu -->\n";
00775 }
00776 return $str;
00777 }
00778 }
00779
00780 ?>