forked from vitstradal/opraf
/
opraf.php
1110 lines (935 loc) · 27 KB
/
opraf.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
// configuration file
require 'config.php';
function get_help_doc()
{
return <<<EOL
Nástroj pro korektury převzatý od Pikomatu.
Základní používání
* Odkaz ls ukáže seznam všech PDF dostupných pro korektury.
Soubory jsou řazeny dle data nahrání do systému, nejnovější nahoře.
* Odkaz help zobrazí tuto nápovědu.
* Po vybrání příslušného souboru opravu provedete kliknutím do textu
a zadáním opravy. Ctrl+Enter odešle formulář s korekturou.
* Korektury (i cizí!) lze mazat, upravovat, či označit jako opravené
(viz tlačítka u korektury).
* Pokud není předvyplněno správné jméno (mělo by být), opravte ho.
Přidání nového PDF
* Stačí nahrát soubor do /akce/MaM/WWW/opraf/pdf a následně ho normálním
způsobem zobrazit. Místo zobrazení souboru se objeví tlačítko na
generování obrázků.
Autorem nástroje je Viťas, vitas(šnek)matfyz(tečka)cz
a zdrojové kódy jsou dostupné na GitHubu na adrese
https://github.com/vitstradal/opraf.
Úpravy pro potřeby M&M provedl Kuba, na něj můžete
směřovat i případné otázky.
BUGS
* misto kliknuti a pak pointer neni to same (nekolik pixelu mimo)
* design error: kolize png obrazku: letak.pdf (vice str) a letak-1.pdf (1 stranka)
* CSRF, kriminalnici jsou vsude.
* jak resit aktualizovani pdf? nejlepe jinym nazvem .pdf
EOL;
} /* get_help_doc() */
#image names
$icons = array(
'undo' => 'imgs/undo.png',
'done' => 'imgs/check.png',
'dele' => 'imgs/delete.png',
'delegr' => 'imgs/delete-gr.png',
'edit' => 'imgs/edit.png',
'editgr' => 'imgs/edit-gr.png',
'link' => 'imgs/link.png',
'next' => 'imgs/next.png',
'nextgr' => 'imgs/next-gr.png',
'comment' => 'imgs/comment.png',
);
# database hanlder
$db = null;
# current pdf file name (from param 'pdf')
$pdf_file = null;
# pdf should be locked when somebody is fixing the mistakes pointed out by
# corrections
$lock = null;
// init db, dispach post, or get
function init()
{
myconnect();
# get
global $pdf_file;
$pdf_file = get_get('pdf');
# lock
global $db;
global $lock;
$sql = "SELECT * FROM zamky WHERE pdf = " . $db->quote($pdf_file) . ";";
$result = $db->query($sql);
foreach ($result as $row) {
$lock = $row;
break;
}
# post
if(count($_POST) > 0) {
do_post();
redir_to_get();
}
return true;
}
// connect to database
function myconnect()
{
global $db;
global $db_conn;
try {
$db = new PDO($db_conn);
}
catch(PDOException $e)
{
ee($e->getMessage());
}
}
function get_pdf_file_post()
{
global $pdf_file;
$pdf_file = get_post('pdf');
}
// after post handled, redirect to get
function redir_to_get()
{
global $pdf_file;
get_pdf_file_post();
$scroll = get_post('scroll');
$header = 'Location: opraf.php?pdf=' . urlencode($pdf_file) ;
if( $scroll !== null ) {
$header .= "&scroll=" . urlencode($scroll);
}
header( $header);
die();
}
// get param from _GET
function get_get($var, $default=null)
{
if(isset($_GET[$var])) {
return $_GET[$var];
}
return $default;
}
// get param from _POST
function get_post($var, $default=null)
{
if(isset($_POST[$var])) {
return $_POST[$var];
}
return $default;
}
// hande POST
function do_post()
{
global $pdf_file;
global $icons;
get_pdf_file_post();
$action = get_post('action', 'none');
//IE, posila obsah butonu, wtfff?
if( false !== strpos($action, $icons['done'])) { $action = 'done';}
if( false !== strpos($action, $icons['undo'])) { $action = 'undone';}
if( false !== strpos($action, $icons['dele'])) { $action = 'del';}
switch($action) {
case 'delall':
do_delall();
break;
case 'lock':
do_lock();
break;
case 'unlock':
do_unlock();
break;
case 'del':
do_del();
break;
case 'del-comment':
do_delete_comment();
break;
case 'done':
do_done(true);
break;
case 'undone':
do_done(false);
break;
case 'regen':
regen_pdf_img();
break;
case 'regen2':
regen2_pdf_img();
break;
case 'update':
do_update_correction();
break;
case 'update-comment':
do_update_comment();
break;
case 'comment':
do_insert_comment();
break;
default:
do_insert_correction();
}
}
// regenerate imgs from .pdf using convert
function regen_pdf_img()
{
global $pdf_file;
get_pdf_file_post();
global $pdf_file;
global $pdf_dir;
global $img_dir;
render_short_begin();
echo "<pre>Generuji obrázky, čekej...\n";
flush();
assert(preg_match('/^[-a-z_A-Z0-9\.]+$/', $pdf_file));
$pdf_base = strip_ext($pdf_file);
remove_images(get_images($pdf_base));
$pdf_path = "$pdf_dir/$pdf_file";
if( !is_file($pdf_path) ) {
ee("CHYBA: Soubor ($pdf_path) neexistuje.");
die('');
}
$img_path = "$img_dir/$pdf_base.png";
$cmd = "PATH=/usr/bin convert -density 180x180 -geometry 1024x1448 \"$pdf_path\" \"$img_path\"";
ee("\t$cmd\n");
system($cmd, $rc);
if( $rc != 0) {
render_regen_form($pdf_file, "CHYBA: Prikaz vratil chybovy kod $rc. Zkus:", false);
exit(0);
}
$cmd = "chmod 664 \"$img_dir/$pdf_base\"*";
ee("\t$cmd\n");
system($cmd, $rc);
if($rc != 0){
echo "\n CHYBA: Vygenerovanym souborum se nepodarilo nastavit potrebna prava.";
exit(0);
}
echo "hotovo.\n</pre><br>";
echo "Pokračovaní <a href=\"?pdf=".escape($pdf_file)."\">zde</a>.";
render_short_end();
exit(0);
}
function render_regen_pdf_img_done($cmd, $pdf_file)
{?>
hotovo (<?php ee($cmd)?></br>
Pokračovaní <a href='?pdf=<?php ee($pdf_file)?>'>zde</a>.
<?php }
function render_regen_pdf_img_init()
{?>
<pre>Generuji obrázky, čekej...
<?php }
function render_short_begin() {
echo("<html><head><meta http-equiv='Content-Type' content='text/html; charset=utf-8'></head><body>");
}
function render_short_end() {
echo("</body></html>");
}
function regen2_pdf_img()
{
global $pdf_file;
global $pdf_dir;
global $img_dir;
get_pdf_file_post();
render_regen_pdf_img_init();
render_short_begin();
flush();
assert(preg_match('/^[-a-z_A-Z0-9\.]+$/', $pdf_file));
$pdf_base = strip_ext($pdf_file);
remove_images(get_images($pdf_base));
$pdf_path = "$pdf_dir/$pdf_file";
if( !is_file($pdf_path) ) {
ee("file ($pdf_path) not exists");
die('');
}
$pageno = 0;
while(true) {
$img_path = "$img_dir/$pdf_base-$pageno.png";
$pdf_path_page = "${pdf_path}[$pageno]";
$cmd = "convert -density 180x180 -geometry 1024x1448 \"$pdf_path_page\" \"$img_path\"";
ee("$img_path\n");
flush();
system($cmd, $rc);
if( $rc != 0) {
render_regen_pdf_img_done($cmd, $pdf_file);
render_short_end();
die("");
}
$pageno++;
}
#die("");
}
// remove given images
function remove_images($imgs)
{
global $img_dir;
foreach($imgs as $img) {
if( strpos($img, '/') != False ) {
ee("wrong img name: $img");
die('');
}
unlink("$img_dir/$img");
}
}
// is it allowed to delete/update this correction?
function correction_change_allowed($oprava_id)
{
global $db;
$sql = "SELECT COUNT(*) as pocet FROM komentare WHERE oprava_id = " .
$db->quote($oprava_id) . ";";
$pocet = $db->query($sql)->fetchAll()[0]["pocet"];
if ($pocet > 0) {
return false;
}
return true;
}
// del correction
function do_del()
{
global $db;
$id = substr(get_post('id'), 2);
if (!correction_change_allowed($id)) {
ee("You cannot delete this correction because there are comments under it");
die();
}
$sql = "DELETE FROM opravy WHERE id = " . $db->quote($id);
$rc = $db->exec($sql);
if( $rc == 0 ) {
#print_r($db->errorInfo());
ee("cannt del from db.'$sql'");
die('');
}
}
// del correction
function do_delall()
{
global $db;
global $pdf_dir;
global $img_dir;
global $lock;
if ($lock) {
die("Zamknuté pdf nelze smazat.");
}
$pdf = get_post('pdf');
$yes = get_post('yes');
if( $yes == 'on' ) {
$sql = "DELETE FROM komentare WHERE oprava_id IN (SELECT id FROM opravy WHERE pdf = " . $db->quote($pdf) . "); " .
"DELETE FROM opravy WHERE pdf = " . $db->quote($pdf) . ";";
$rc = $db->exec($sql);
unlink($pdf_dir . "/" . basename($pdf));
foreach (get_images(strip_ext($pdf)) as $img) {
unlink("$img_dir/$img");
}
header("Location: opraf.php");
die();
}
else {
die("Pokud chcete opravdu smazat '$pdf', zaškrtněte checkbox");
//die("Cannot delete '$pdf', check agreement checkbox!");
}
}
// "lock" pdf (so that everybody sees that the mistakes are being fixed)
function do_lock()
{
global $db;
$pdf = get_post('pdf');
$au = get_post('au', 'anonym');
setcookie('author', $au);
$sql = "INSERT INTO zamky (pdf, au) VALUES (" . $db->quote($pdf) . ", " . $db->quote($au) . ");";
$rc = $db->exec($sql);
if ($rc == 0) {
ee("Cannot insert into db. '$sql'");
die('');
}
}
// "unlock" pdf
function do_unlock()
{
global $db;
$pdf = get_post('pdf');
$sql = "DELETE FROM zamky WHERE pdf = " . $db->quote($pdf) . ";";
$rc = $db->exec($sql);
if ($rc == 0) {
ee("Cannot delete from db. '$sql'");
die('');
}
}
// set correction status
function do_done($done)
{
global $db;
#echo "do_done: $done.";
$new_status = $done ? 'DONE' : 'NONE';
$id = substr(get_post('id'), 2);
$sql = "UPDATE opravy SET status = " . $db->quote($new_status) .
" WHERE id = " . $db->quote($id);
$rc = $db->exec($sql);
#die("do_done($sql)$done:$rc");
if( $rc == 0 ) {
#print_r($db->errorInfo());
ee("cannt update from db. '$sql'. ");
die('');
}
}
// update correction
function do_update_correction()
{
global $db;
#$img_id = get_post('img-id', 'img-0');
$id = get_post('id', null);
#$pdf = get_post('pdf');
#$x = get_post('x', 0);
#$y = get_post('y', 0);
$txt = get_post('txt', 'notxt');
$au = get_post('au', 'anonym');
if (!correction_change_allowed($id)) {
ee("You cannot update this correction, because there are comments under it");
die();
}
setcookie('author', $au);
if( $id === null ){
return;
}
$sql = 'UPDATE opravy SET ' .
#'pdf = ' . $db->quote($pdf) . "," .
#'img_id = '. $db->quote($img_id) . "," .
#'x = ' . $db->quote($x) . "," .
#'y = ' . $db->quote($y) . "," .
'txt = ' . $db->quote($txt) . "," .
'au = ' . $db->quote($au) . " " .
'WHERE id = ' . $db->quote($id) ;
$rc = $db->exec($sql);
if( $rc == 0 ) {
#print_r($db->errorInfo());
ee("cannt update db. '$sql'");
die('');
}
}
// insert new correction into dabase
function do_insert_correction()
{
global $db;
$img_id = get_post('img-id', 'img-0');
$pdf = get_post('pdf');
$x = get_post('x', 0);
$y = get_post('y', 0);
$txt = get_post('txt', 'notxt');
$au = get_post('au', 'anonym');
setcookie('author', $au);
$sql = "INSERT INTO opravy (pdf, img_id, x, y, txt, au) VALUES (" .
$db->quote($pdf) . "," .
$db->quote($img_id) . "," .
$db->quote($x) . "," .
$db->quote($y) . "," .
$db->quote($txt) . "," .
$db->quote($au) . ")";
$rc = $db->exec($sql);
if( $rc == 0 ) {
#print_r($db->errorInfo());
ee("cannt insert into db. '$sql'");
die('');
}
}
// insert new comment into the database
function do_insert_comment()
{
global $db;
$oprava_id = get_post('id', null);
$text = get_post('txt', 'notxt');
$au = get_post('au', 'anonym');
setcookie('author', $au);
if( $oprava_id === null ){
return;
}
$sql = 'INSERT INTO komentare (oprava_id, text, au) VALUES (' .
$db->quote($oprava_id) . ", " .
$db->quote($text) . ", " .
$db->quote($au) . ");";
$rc = $db->exec($sql);
if( $rc == 0 ) {
#print_r($db->errorInfo());
ee("cannot update db. '$sql'");
die('');
}
}
// is it allowed to remove/change this comment?
function comment_change_allowed($id)
{
global $db;
// is the comment to be removed/changed the last one under its correction?
$sql = "SELECT COUNT(*) as pocet FROM komentare WHERE oprava_id = " .
" (SELECT oprava_id FROM komentare WHERE id = " . $db->quote($id) . ") " .
" AND cas > (SELECT cas FROM komentare WHERE id = " . $db->quote($id) . "); ";
$pocet = $db->query($sql)->fetchAll()[0]["pocet"];
if ($pocet > 0) {
return false;
}
return true;
}
// delete a comment from the database
function do_delete_comment()
{
global $db;
$id = get_post("id");
if (!comment_change_allowed($id)) {
ee("You cannot remove this comment, because there are newer comments under its correction.");
die();
}
$sql = "DELETE FROM komentare WHERE id = " . $db->quote($id) . ";";
$rc = $db->exec($sql);
if ($rc == 0) {
ee("Cannot delete from database: " . $sql);
die("");
}
}
// update correction
function do_update_comment()
{
global $db;
$id = get_post('id', null);
$text = get_post('txt', 'notxt');
$au = get_post('au', 'anonym');
setcookie('author', $au);
if (!comment_change_allowed($id)) {
ee("You cannot update this comment, becasue there are newer comments under its correction.");
die();
}
$sql = "UPDATE komentare SET " .
"text = " . $db->quote($text) . ", " .
"au = " . $db->quote($au) . " WHERE id = " . $db->quote($id) . ";";
$rc = $db->exec($sql);
if ($rc == 0) {
#print_r($db->errorInfo());
ee("cannot update db. '$sql'");
die('');
}
}
// rid off .pdf
function strip_ext($file)
{
return substr($file, 0, strrpos($file, '.'));
}
// find image files, based on $pdf_base
// (letak.pdf -> letak-0.png, letak-1.png ...)
function get_images($pdf_base)
{
global $img_dir;
$ret = array();
$img = "$pdf_base.png";
$img_path = "$img_dir/$img";
if( is_file($img_path) ) {
$ret[] = $img;
}
for($i = 0; true; $i++) {
$img = "$pdf_base-$i.png";
$img_uri_path = "$img_uri/$img";
$img_path = "$img_dir/$img";
if( !is_file($img_path) ) {
break;
}
$ret[] = $img;
}
return $ret;
}
// generate html with images
function render_images()
{
#TODO
global $pdf_file;
global $img_dir;
global $img_uri;
global $lock;
$pdf_base = strip_ext($pdf_file);
$imgs = get_images($pdf_base);
if( count($imgs) == 0 ) {
render_regen_form($pdf_file, 'Obrázky je nutné nejdříve vygenerovat.');
return;
}
render_check_update($pdf_base, $imgs[0]);
foreach($imgs as $idx => $img) {
$img_uri_path = "$img_uri/$img";
$img_path = "$img_dir/$img";
list($w, $h) = getimagesize($img_path);
$id = "img-$idx";
if ($lock) {
echo "<b>Od " . $lock["cas"] . " zanáší " . $lock["au"] . " korektury.</b>";
}
render_image($img_path, $id, $w, $h);
}
if ($lock) {
render_unlock($pdf_file);
} else {
render_lock($pdf_file);
}
render_delall($pdf_file);
}
function render_lock($pdf_file)
{
global $au;
?>
<form method="post">
<input type='hidden' name='action' value='lock'/>
<input type='text' name='au' value=<?php eeq($au)?>/>
<input type='submit' value='Zamknout toto pdf' onclick='return confirm("Opravdu se jdete pustit do zanášení korektur?");'/>
<input type='hidden' name='pdf' value=<?php eeq($pdf_file)?>/>
</form>
<hr/>
<?php }
function render_unlock($pdf_file)
{
?>
<form method="post">
<input type='hidden' name='action' value='unlock'/>
<input type='submit' value='Odemknout toto pdf' onclick='return confirm("Opravdu odemknout toto pdf?");'/>
<input type='hidden' name='pdf' value=<?php eeq($pdf_file)?>/>
</form>
<hr/>
<?php }
function render_delall($pdf_file)
{?>
<form method="post">
<input type='hidden' name='action' value='delall'/>
<input type='submit' value='Smazat toto pdf' onclick='return confirm("Opravdu smazat pdf a všechny korektury?");'/>
<input type='hidden' name='pdf' value=<?php eeq($pdf_file)?>/>
<input type='checkbox' name='yes'/> Souhlasím se smazáním pdf a všech souvisejících korektur
</form>
<hr/>
<?php }
function render_image($img_path, $id, $w, $h)
{
global $lock;
?>
<div class='imgdiv'><img width=<?php eeq($w)?> height=<?php eeq($h)?>
<?php if ($lock) { ?>
onclick='if (confirm("Přidat korekturu, i když je <?php ee($lock["au"])?> právě zanáší?")) { img_click(this,event); }'
<?php } else { ?>
onclick='img_click(this,event);'
<?php } ?>
id=<?php eeq($id)?> src=<?php eeq($img_path)?>/></div><hr/>
<?php }
// check if .pdf is newer than first img
// if yes: display warning, and regen button
function render_check_update($pdf_base, $img_first)
{
global $img_dir;
global $pdf_dir;
global $pdf_file;
$pdf_path = "$pdf_dir/$pdf_file";
$img0_path = "$img_dir/$img_first";
if( !is_file($img0_path) ) {
return;
}
if( !is_file($pdf_path) ) {
return;
}
if( filemtime($pdf_path) > filemtime($img0_path) ) {
render_regen_form($pdf_file, "Obrázky jsou starší, než původní .pdf. Zkus je přegenerovat.");
}
}
/**
*
* silly template system
*
* a) output is in render_*()
* b) thruu ee() eeq() and ejs()
*/
// escape to html
function escape($txt)
{
return htmlspecialchars($txt, ENT_QUOTES, "ISO-8859-1");
}
// echo html-escaped text
function ee($txt, $id = null)
{
if( $id !== null ) {
echo escape($txt[$id]);
return;
}
echo escape($txt);
}
// echo html-escaped quote
function eeq($txt, $id = null)
{
if( $id !== null ) {
echo '"' . escape($txt[$id]) . '"';
return;
}
echo "'" . escape($txt, $id) . "'";
}
// echo js-escaped object
function ejs($obj)
{
echo json_encode($obj);
}
// generate html corrections section
function render_opravy()
{
global $db;
global $icons;
global $pdf_file;
$imgs = array();
$sql = "SELECT * from opravy WHERE pdf = " . $db->quote($pdf_file);
$opravy = array();
$statistika = array();
foreach( $db->query($sql) as $row) {
$row['img_id_num'] = (int) substr($row['img_id'], 4);
$opravy[] = $row;
$statistika[$row['au']]++;
}
if( count($opravy) > 0 ) {
arsort($statistika);
$msg = "Děkujeme opravovatelům: ";
if( count($statistika) == 1 ) {
$msg = "Děkujeme: ";
}
$sep ='';
$text ='';
foreach($statistika as $au => $count) {
$text .= "${sep}$au($count)";
$sep = ", ";
}
render_statistika($msg, $text);
}
usort($opravy, 'opravy_cmp');
for($i = 0; $i < count($opravy); $i++) {
$row = $opravy[$i];
$next_id = $i + 1< count($opravy) ? 'op' . $opravy[$i+1]['id'] : null;
$id = 'op' . $row['id'];
$img_id = $row['img_id'];
$imgs[$img_id][] = array($id, (int)$row['x'], (int)$row['y']);
// get comments for the correction
$c_sql = "SELECT * FROM komentare WHERE oprava_id = " . $db->quote($row['id']) . " ORDER BY cas;";
$comments = $db->query($c_sql)->fetchAll();
render_oprava($pdf_file, $id, $next_id, $pdf_file, $row, $icons, $comments);
}
render_opravy_script($imgs);
}
function render_opravy_script($imgs)
{?>
<script>
<?php foreach($imgs as $img_id => $opravy): ?>
place_comments_one_div(<?php ejs($img_id)?>, <?php ejs($opravy)?>);
<?php endforeach ?>
</script>
<?php }
function render_statistika($pre_msg, $text)
{?>
<?php ee($pre_msg)?><?php ee($text)?>
<hr>
<?php }
function render_oprava($pdf_file, $id, $next_id, $pdf_file, $row, $icons, $comments)
{
$len_comments = count($comments);
?>
<?php if( $row['status'] == 'DONE' ) : ?>
<div onclick='img_click(this,event)' id='<?php ee($id)?>-pointer' class='pointer-done'></div>
<div name=<?php eeq($id)?> id=<?php eeq($id)?> class='box-done' onmouseover='box_onmouseover(this,1)' onmouseout='box_onmouseout(this,1)' >
<?php else: ?>
<div onclick='img_click(this,event)' id='<?php ee($id)?>-pointer' class='pointer'></div>
<div name=<?php eeq($id)?> id=<?php eeq($id)?> class='box' onmouseover='box_onmouseover(this,0)' onmouseout='box_onmouseout(this,0)' >
<?php endif ?>
<div class='corr-header'>
<div class='author'><?php ee($row, 'au')?></div>
<div class='float-right'>
<form action='' onsubmit='save_scroll(this)' method='POST'>
<input type='hidden' name='pdf' value=<?php eeq($pdf_file)?>>
<input type='hidden' name='id' value=<?php eeq($id)?>>
<input type='hidden' name='scroll'>
<button name='action' value='del'
<?php if ($len_comments == 0) { ?>
type='submit'
title='Smaž opravu' onclick='return confirm("Opravdu smazat korekturu?");'><img src=<?php eeq($icons,'dele')?>/></button>
<?php } else { ?>
type='button'
title="Korekturu nelze smazat – už ji někdo okomentoval"><img src=<?php eeq($icons,'delegr')?>/></button>
<?php } ?>
<?php if( $row['status'] == 'DONE' ) : ?>
<button type='submit' name='action' value='undone' title='Označ jako neopravené'><img src=<?php eeq($icons,'undo')?>/></button>
<?php else: ?>
<button type='submit' name='action' value='done' title='Označ jako opravené'><img src=<?php eeq($icons,'done')?>/></button>
<?php endif ?>
<button type='button'
<?php if ($len_comments == 0) { ?>
onclick='box_edit(this, "update");' title='Oprav opravu'><img src=<?php eeq($icons, 'edit')?>/></button>
<?php } else { ?>
title="Korekturu nelze upravit – už ji někdo okomentoval"><img src=<?php eeq($icons, 'editgr')?>/></button>
<?php } ?>
<button type='button' onclick='box_edit(this, "comment");' title='Komentovat'><img src=<?php eeq($icons, 'comment')?>/></button>
<a href='#<?php ee($id)?>'><button type='button' title='Link na opravu'><img src=<?php eeq($icons, 'link')?>/></button></a>
<?php if( $next_id ) : ?>
<a href='#<?php ee($next_id)?>'><img title='Další oprava' src=<?php eeq($icons, 'next')?>/></button></a>
<?php else: ?>
<img title='Toto je poslední oprava' src=<?php eeq($icons, 'nextgr')?>/>
<?php endif ?>
</form>
</div>
</div><?php //hlavicka ?>
<div id='<?php ee($id)?>-text'><?php ee($row, 'txt'); ?></div>
<?php
$i = 0;
foreach ($comments as $c) {
$i++; ?>
<hr>
<div class='comment' id='k<?php ee($c["id"])?>'>
<div class='corr-header'>
<div class='author'><?php ee($c["au"]) ?></div>
<?php
// last comment can be deleted or updated
if ($i == $len_comments) {
?>
<div class="float-right">
<form action='' onsubmit='save_scroll(this)' method='POST'>
<input type='hidden' name='pdf' value=<?php eeq($pdf_file)?>>
<input type='hidden' name='id' value=<?php eeq($c["id"])?>>
<input type='hidden' name='scroll'>
<button type='submit' name='action' value='del-comment' title='Smaž komentář'
onclick='return confirm("Opravdu smazat komentář?")'><img src=<?php eeq($icons,'dele'); ?>/></button>
</form>
<button type='button' onclick='update_comment(this);' title='Uprav komentář'
><img src=<?php eeq($icons, 'edit')?>/></button>
</div>
<?php
}
?>
</div>
<div id='kt<?php ee($c["id"])?>'><?php ee($c["text"]) ?></div>
</div>
<?php
}
?>
</div><?php /* box-done|box */?>
<?php }
// prvky jsou radky ze selecty * from opravy
function opravy_cmp(&$a, &$b)
{
$a_id = $a['img_id_num'];
$b_id = $b['img_id_num'];
if( $a_id != $b_id ) { return $a_id - $b_id; }
$a_y = $a['y'];
$b_y = $b['y'];
if( $a_y != $b_y ) { return $a_y - $b_y; }
$a_x = $a['x'];
$b_x = $b['x'];
return $a_x - $b_x;
}
// gen javascript sectin which scrolls to page offset
// defined by scroll= param
function render_scroll($scroll)
{?>
<?php if( $scroll === null ) { return; } ?>
<script>
window.scrollTo(0,<?php ejs((int)$scroll)?>);
</script>
<?php }
// gen html documentation
function render_doc()
{
echo'<pre>'.escape(get_help_doc()).'</pre>';
}
function render_ls_files()
{
global $pdf_dir;
$files = array();
if( $dir = opendir($pdf_dir) ) {
while (($file = readdir($dir)) !== false) {
if( substr($file, -4) != '.pdf' ) continue;
$files[] = $file;
}