5516faa25802a5cf9817194d25757348382d2fea
[kewensis.git] / index.php
1 <?php $cvs_id='$Id$';
2
3         include("energie/common.php");
4
5         $head_css="
6 .name { font-weight: bold; }
7 .PublAuthor { font-variant: small-caps; }
8 .Publication { }
9 ";
10         no_cache();
11
12         $buttonre=".+";
13         prepvar("find"     ,".*"     ,false /* require */);
14         if (isset($find))
15                 $find=trim($find);
16         prepvar("confirm"  ,$buttonre,false /* require */);
17         prepvar("delete"   ,$buttonre,false /* require */);
18         prepvar("isolate"  ,$buttonre,false /* require */);
19         prepvar("edit"     ,$buttonre,false /* require */);
20         prepvar("update"   ,$buttonre,false /* require */);
21         prepvar("move"     ,$buttonre,false /* require */);
22         prepvar("reorder"  ,$buttonre,false /* require */);
23         
24         prepvar("move_src","[-0-9 ,]+"    ,isset($move   ) /* require */);
25         prepvar("move_dst","[-0-9]+"      ,isset($move   ) /* require */);
26         prepvar("confirming","[a-z]+"     ,isset($confirm) /* require */);
27         prepvar("reorder_by","[+-]?[0-9]+",isset($reorder) /* require */);
28
29         prepvar("id"     ,"[-0-9]+",(isset($update) || isset($confirm) || isset($delete) || isset($isolate) || isset($edit)) || isset($reorder) /* require */);
30         prepvar("winclose" ,"1",false /* require */);
31
32         // Database fields:
33         prepvar("name"       ,".*"     ,false /* require */);
34         prepvar("PublAuthor" ,".*"     ,false /* require */);
35         prepvar("Publication",".*"     ,false /* require */);
36         prepvar("html"       ,".*"     ,false /* require */);
37
38         // "find" dump limits:
39         prepvar("limit_from","[0-9]*",false /* require */);
40         if (!isset($limit_from))
41                 $limit_from=0;
42         prepvar("limit_max" ,"[0-9]*",false /* require */);
43         if (!isset($limit_max))
44                 $limit_max=$limit_max_default;
45
46         $permanents_array=array("find","limit_from","limit_max","winclose");
47
48         if (isset($update)) {
49
50 function fieldupdate($field)
51 {
52         global $cmd,$cmdargnum;
53
54         $cmd.=(!$cmdargnum?" ":",")."$field=";
55         if ($GLOBALS[$field]!="")
56                 $cmd.="\"".mysql_escape_string($GLOBALS[$field])."\"";
57         else
58                 $cmd.="NULL";
59         $cmdargnum++;
60 }
61
62                 $cmd="update $tb_tree set";
63                 $cmdargnum=0;
64                 fieldupdate("name");
65                 fieldupdate("PublAuthor");
66                 fieldupdate("Publication");
67                 fieldupdate("html");
68                 $cmd.=" where id=\"".mysql_escape_string($id)."\"";
69                 if ($cmdargnum)
70                         db_query($cmd);
71                 // PASSTHRU redirect
72                 }
73         if (isset($delete)) {
74                 db_query("delete from $tb_tree where id=\"".mysql_escape_string($id)."\"");
75                 // PASSTHRU redirect
76                 }
77         if (isset($isolate)) {
78                 $family_id=db_item("select family_id from $tb_tree where id=\"".mysql_escape_string($id)."\"");
79                 if ($id!=$family_id)
80                         db_query("update $tb_tree set family_id=id where id=\"".mysql_escape_string($id)."\"");
81                 else {
82                         $where="where family_id=\"".mysql_escape_string($family_id)."\" and family_id<>id";
83                         $other_id=db_item("select id from $tb_tree $where limit 1");
84                         db_query("update $tb_tree set family_id=\"".mysql_escape_string($other_id)."\" $where");
85                         }
86                 // PASSTHRU redirect
87                 }
88         if (isset($move)) {
89                 $order=db_item("select max(family_order) from $tb_tree where family_id=\"".mysql_escape_string($move_dst)."\"");
90                 foreach(preg_split('/[ ,]/',$move_src,-1,PREG_SPLIT_NO_EMPTY) as $src) {
91                         // UGLY but how to do sequential setting of increasing family_order?
92                         $result=db_query("select id from $tb_tree where family_id=\"".mysql_escape_string($src)."\" order by family_order");
93                         while (($row=mysql_fetch_array($result)))
94                                 db_query("update $tb_tree set family_order=\"".(++$order)."\",family_id=\"".mysql_escape_string($move_dst)."\""
95                                                 ." where id=\"".mysql_escape_string($row["id"])."\"");
96                         }
97                 // PASSTHRU redirect
98                 }
99         if (isset($reorder)) {
100                 $row=db_array("select family_id,family_order from $tb_tree where id=\"".mysql_escape_string($id)."\"");
101                 $other_order=db_item("select family_order from $tb_tree where family_id=\"".mysql_escape_string($row["family_id"])."\""
102                                 ." and family_order".($reorder_by<0 ? "<" : ">").$row["family_order"]
103                                 ." order by family_order ".($reorder_by<0 ? "desc" : "asc")." limit 1");
104                 db_query("update $tb_tree set family_order=family_order".($reorder_by>=0 ? "+" : "").$reorder_by
105                                 ." where family_id=\"".mysql_escape_string($row["family_id"])."\""
106                                 ." and family_order".($reorder_by<0 ? "<" : ">").$other_order);
107                 db_query("update $tb_tree set family_order=".($other_order+$reorder_by)." where id=\"".mysql_escape_string($id)."\"");
108                 // PASSTHRU redirect
109                 }
110
111 function hiddens($array,$doinputs)
112 {
113         global $permanents_array;
114         $r="";
115         foreach ($permanents_array as $key)
116                 if (!isset($key,$array) && isset($GLOBALS[$key]))
117                         $array[$key]=$GLOBALS[$key];
118         foreach ($array as $key=>$val) {
119                 if ($doinputs)
120                         $r.="<input type=\"hidden\" name=\"".htmlspecialchars($key)."\" value=\"".htmlspecialchars($val)."\" />\n";
121                 else
122                         $r.=($r==""?"?":"&").addpercents($key)."=".addpercents($val);
123                 }
124         if (!$doinputs && $r=="")
125                 $r="?_dummy=1";
126         return($r);
127 }
128
129         if (isset($delete) || isset($update) || isset($move) || isset($isolate) || isset($reorder)) {
130                 if (isset($winclose)) {
131                         header("Content-type: text/html");
132                         ?>
133 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
134 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="cs">
135 <head><title>Window close</title></head>
136 <body onload="window.close();">
137 <p>Window close</p>
138 </body></html>
139 <?php
140                         }
141                 else {
142                         // OK: Redirect will NOT keep the request type "POST"!
143                         $redir="http://".$HTTP_SERVER_VARS["HTTP_HOST"].$HTTP_SERVER_VARS["REQUEST_URI"].hiddens(array(),false/*doinputs*/);
144                         header("Location: $redir");
145                         }
146                 exit();
147                 }
148
149         heading(false/*title*/);
150
151 function fieldedit($field,$size,$append="")
152 {
153         global $row;
154         return("<input type=\"text\" name=\"".htmlspecialchars($field)."\" size=\"$size\" value=\""
155                         .(!isset($row[$field]) ? "" : htmlspecialchars($row[$field]))."\"$append />");
156 }
157
158 function formconvert($id,$name,$text,$readonly,$anchor,$hiddens=array())
159 {
160         global $tb_tree,$row,$winclose;
161         $readonly=(!$readonly ? "" : " readonly=\"readonly\"");
162         if ($winclose)
163                 $text.=" (closes window)";
164         $row=db_array("select name,PublAuthor,Publication,html from $tb_tree"
165                         ." where id=\"".mysql_escape_string($id)."\"");
166         $hiddens["id"]=$id;
167         print("<!-- ID=$id -->"
168                         ."<form action=\"index.php$anchor\" method=\"POST\">\n"
169                         ."<table border=\"0\" width=\"100%\"><tr><td align=\"center\"><table border=\"0\"><tr><td>\n"
170                         .fieldedit("name",50,$readonly)
171                         ."\n".fieldedit("PublAuthor",50,$readonly)
172                         ."<br />\n".fieldedit("Publication",100,$readonly)
173                         ."<br /><textarea name=\"html\" rows=\"20\" cols=\"80\">"
174                                         .(!isset($row["html"]) ? "" : htmlspecialchars($row["html"]))
175                                         ."</textarea>\n"
176                         ."</td></tr></table><p>&nbsp;</p>\n"
177                         ."<input type=\"submit\" name=\"".htmlspecialchars($name)."\" value=\"".htmlspecialchars($text)."\" />\n"
178                         .hiddens($hiddens,true/*doinputs*/)
179                         ."</td></tr></table></form>");
180         footer(true /* delimit */);
181 }
182
183         if (isset($edit))
184                 formconvert($id,"update","Update entry",false/*readonly*/,"#id_$id");
185         if (isset($confirm) && $confirming=="delete")
186                 formconvert($id,$confirming,"Really delete entry?",true/*readonly*/,
187                                 "#family_".db_item("select family_id from $tb_tree where id=\"".mysql_escape_string($id)."\""));
188         if (isset($confirm) && $confirming=="isolate")
189                 formconvert($id,$confirming,"Really isolate entry?",true/*readonly*/,"#id_$id");
190
191         $reset_from_script="this.form.elements['limit_from'].value=0;";
192         print('<form action="index.php" method="GET">'."\n"
193                         .'<h1 align="center">'
194                         .'<input type="text" name="find" size="40" '
195                                         .(!isset($find)?"":" value=\"".htmlspecialchars($find)."\"")
196                                         ." onkeyup=\"$reset_from_script\" onchange=\"$reset_from_script\" onfocus=\"$reset_from_script\""
197                                         .' />'."\n"
198                         .'&nbsp;<input type="submit" value="Search" />'."\n"
199                         .'</h1><h2 align="center">'."\n"
200                         .'First Group: <input type="text" name="limit_from" size="6" value="'.$limit_from.'" />'."\n"
201                         .'&nbsp;&nbsp;&nbsp;'
202                         .'Max Groups: '.'<input type="text" name="limit_max"  size="6" value="'.$limit_max .'" />'."\n"
203                         .'</h2></form>'."\n");
204         if (!isset($find))
205                 footer(true /* delimit */);
206         
207         print("<hr />\n");
208
209 function fieldquote($prefix,$field,$suffix="")
210 {
211         global $row,$fieldquote_tot;
212         if (!isset($row[$field]))
213                 return("");
214         return($prefix."<span class=\"$field\">".htmlspecialchars($row[$field])."</span>".$suffix);
215 }
216
217 function formbutton($name,$text,$method="GET",$hidden=array(),$newwin=false,$anchor="")
218 {
219         $r="";
220         if ($newwin) {
221                 $r.="<table border=\"0\"><tr><td>".formbutton($name,$text,$method,$hidden,false/*newwin*/)."</td><td>";
222                 $hidden["winclose"]=1;
223                 }
224         $r.=""
225                         ."<form action=\"index.php".($anchor=="" ? "" : "#$anchor")."\" method=\"$method\""
226                                         .(!$newwin ? "" : " target=\"_blank\"").">"
227                         ."<input type=\"submit\" "
228                                         .($name=="" ? "" : " name=\"".htmlspecialchars($name)."\"")
229                                         ." value=\"".htmlspecialchars($newwin ? "(in window)" : $text)."\" />"
230                         .hiddens($hidden,true/*doinputs*/)
231                         ."</form>";
232
233         if ($newwin)
234                 $r.="</td></tr></table>";
235         return($r);
236 }
237
238         $families=db_query("select distinct family_id from $tb_tree"
239                         ." where name like \"".mysql_escape_string($find)."%\""
240                         ." order by name limit $limit_from,".($limit_max+1));
241
242         $arrows="<table border=\"0\" width=\"100%\"><tr><td align=\"center\">\n"
243                         ."<table border=\"0\" width=\"80%\"><tr><td align=\"left\">";
244         if ($limit_from>0)
245                 $arrows.=formbutton("","Previous","GET",array("limit_from"=>max(0,$limit_from-$limit_max)));
246         $arrows.="</td><td align=\"right\">";
247         if (mysql_num_rows($families)>$limit_max)
248                 $arrows.=formbutton("","Next","GET",array("limit_from"=>$limit_from+$limit_max));
249         $arrows.="</td></tr></table></td></tr></table>";
250
251         print "$arrows<hr />\n";
252         $family_tot=0;
253         while (($family_row=mysql_fetch_array($families))) {
254                 if ($family_tot)
255                         print "<hr />\n";
256                 $family_id=$family_row["family_id"];
257                 print("<table border=\"0\" width=\"90%\"><tr><td align=\"right\"><a name=\"family_$family_id\">"
258                                 ."<form action=\"index.php#family_$family_id\" method=\"POST\">" // WARNING: <form> will break the line!
259                                 ."Group&nbsp;#&nbsp;<input type=\"text\" size=\"10\" readonly=\"readonly\" value=\"$family_id\" />"
260                                 ."<br /><br />"
261                                                 ."Join to this group: <input type=\"text\" name=\"move_src\" size=\"21\" value=\"\" />"
262                                                 ."&nbsp;<input type=\"submit\" name=\"move\" value=\"Join\" />\n"
263                                                 .hiddens(array("move_dst"=>$family_id),true/*doinputs*/)
264                                                 ."</form>\n"
265                                 ."</td></tr></table>\n");
266                 $result=db_query("select id,family_order,name,PublAuthor,Publication,html from $tb_tree"
267                                 ." where family_id=\"$family_id\""
268                                 ." order by family_order");
269                 $order=0;
270                 while (($row=mysql_fetch_array($result))) {
271                         if ($order>=1)
272                                 print "<blockquote>";
273
274                         print("<a name=\"id_".$row["id"]."\"><!-- family_order=".$row["family_order"]." --><table border=\"0\" width=\"90%\">");
275
276                         print("<tr><td align=\"left\" valign=\"center\"><table border=\"0\"><tr>");
277                         if ($order>=1) {
278                                 print "<td>&bull;</td>";
279                                 $tabpfx="<td></td>";
280                                 }
281                         else
282                                 $tabpfx="";
283                         print("<td>".fieldquote("","name","\n")
284                                         .fieldquote("","PublAuthor")."</td></tr>");
285                         print(fieldquote("<tr>$tabpfx<td>","Publication","</td></tr>"));
286                         if (isset($row["html"])) {
287                                 $import="import: [".$row["id"]."]";
288                                 $html=$row["html"];
289                                 $html=eregi_replace("< *"."/? *a( [^>]*)?>","",$html);
290                                 print("<tr>$tabpfx<td><blockquote><!-- BEGIN $import -->\n".$html."\n<!-- END $import --></blockquote></td></tr>");
291                                 }
292                         print("</table></td><td align=\"right\" valign=\"center\"><table border=\"0\">");
293                         $l="<tr><td align=\"center\">";
294                         $r="</td></tr>";
295                         print(""
296                                         .($order <= 0 ? "" :
297                                                         $l.formbutton("reorder","^^^ up ^^^","POST",
298                                                                         array("id"=>$row["id"],"reorder_by"=>-1),false/*newwin*/,"family_$family_id").$r)
299                                         .$l.formbutton("confirm","Delete entry","GET",
300                                                         array("id"=>$row["id"],"confirming"=>"delete"),true/*newwin*/).$r
301                                         .$l.formbutton("edit"   ,"Edit entry"  ,"GET",
302                                                         array("id"=>$row["id"]),true/*newwin*/).$r
303                                         .(mysql_num_rows($result)<=1 ? "" :
304                                                         $l.formbutton("confirm","Isolate entry","GET",
305                                                                         array("id"=>$row["id"],"confirming"=>"isolate"),true/*newwin*/).$r)
306                                         .($order+1 >= mysql_num_rows($result) ? "" :
307                                                         $l.formbutton("reorder","vvv down vvv"  ,"POST",
308                                                                         array("id"=>$row["id"],"reorder_by"=>+1),false/*newwin*/,"family_$family_id").$r)
309                                         ."</table></td></tr></table></a>\n"
310                                         );
311
312                         if ($order>=1)
313                                 print "</blockquote>";
314                         print "\n";
315                         $order++;
316                         }
317                 mysql_free_result($result);
318                 if ($order>=2)
319                         print "</ul>\n";
320                 print "</a>\n"; // <a name="family_X"
321                 $family_tot++;
322                 if ($family_tot >= $limit_max)
323                         break;
324                 }
325         mysql_free_result($families);
326         print "<hr />$arrows\n";
327
328         footer(false /* delimit */);
329 ?>