-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtableeditor.class.php
1425 lines (1251 loc) · 37.1 KB
/
tableeditor.class.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
<?
/**
* Table Editor Class
*
* This class can be used to very easily and quickly create a full database-driven table editor
* @author James Grant <james@lightbox.org>
* @version 1.1
* @package tableeditor
*/
//Version 2006-04-21
//Version 2006-05-04
//Version 2006-05-07
//Version 2006-06-08 - add filtering capabilities to the display
//Version 2006-06-16 - add file uploads setUploadPath() and fieldname='filename'
//Version 2006-07-21 - add ability to autodetect options for enum() fields, and show them as a <select>
//Version 2006-07-27 - fix a bug when displaying an uploaded file that no longer exists
//Version 2006-08-09 - let select_or_text fields have default values assigned to the dropdown list via setFieldOptions
// - add new function setYearSelectRange(min,max), which can specify the year spread to display on any input_year_select
// - add feature so when displaying years that dont exist, if it doesnt find the selected value in the list it adds a new item at the b ottom of the list with the value
//Version 2006-08-10 - Add paganation support for those really really really long lists. Default is 50, set using setRowsPerPage()
//Version 2006-08-13 - Add the option to add custom action icons to do various stuffs
//Version 2006-08-17 - Add an option to delete an uploaded file without having to re-upload overtop it.
//Version 2006-09-01 - Fixed abug when adding an entry that has a filename field but the filename was left blank
//Version 2006-09-05 - Fixed the style of date and time inputs because they are in their own table so the TD's need the tableedit class assigned to them or the stuff inside the table wont be the same as the rest of the editor stuff.
//Version 2006-09-25 - Fixed time formatting for 12/24 hour selection
// - Fix INSERTING with a hidden field with value of NOW()
/*
interface TableEditorInterface {
function tableEditorSetup($editor);
function tableEditorLoad();
function tableEditorSave($data);
function tableEditorDelete();
function tableEditorGetList($editor);
};
*/
//ironforge
//$icon_path="/phpscripts/icons/16";
//lightbox
//$icon_path="/icons/16";
//cfdc
//$icon_path="/phpscripts/images/16";
//sfiab
$icon_path="{$config['SFIABDIRECTORY']}/images/16";
if(!function_exists("i18n"))
{
function i18n($str,$args=array())
{
for($x=1;$x<=count($args);$x++)
{
$str=str_replace("%$x",$args[$x-1],$str);
}
return htmlspecialchars($str);
}
}
if(!function_exists("happy"))
{
function happy($str)
{
return "<div class=\"happy\">$str</div>";
}
}
if(!function_exists("error"))
{
function error($str)
{
return "<div class=\"error\">$str</div>";
}
}
if(!$icon_extension)
{
//detect the browser first, so we know what icons to use
if(stristr($_SERVER['HTTP_USER_AGENT'],"MSIE"))
$icon_extension="gif";
else
$icon_extension="png";
}
/**
* The main class
* @package tableeditor
*/
class TableEditor
{
/**#@+
* @access private
* @var array
*/
var $table;
var $listfields=array();
var $editfields=array();
var $hiddenfields=array();
var $fieldOptions=array();
var $fieldValidation=array();
var $fieldDefaults=array();
var $fieldInputType=array();
var $fieldInputOptions=array();
var $fieldFilterList=array();
var $actionButtons=array();
var $additionalListTables=array();
var $options=array();
/**#@-*/
/**#@+
* @access private
* @var string
*/
var $classname;
var $sortDefault;
var $sortCurrent;
var $recordType;
var $primaryKey;
var $timeformat;
var $dateformat;
var $uploadPath;
var $yearSelectRangeMin;
var $yearSelectRangeMax;
var $rowsPerPage;
var $activePage;
var $DEBUG;
/**#@-*/
/**#@+
* @access private
* @var boolean
*/
var $allowAdding;
/**#@-*/
/**
* Constructor
* @param array $table
* @param array $listfields
* @param array $editfields
* @param array $hiddenfields
*/
function TableEditor($classname,$listfields=null,$editfields=null,$hiddenfields=null)
{
//set defaults
$this->timeformat="12hrs";
$this->dateformat="Y-m-d";
$this->allowAdding=true;
$this->rowsPerPage=50;
$this->activePage=1;
$this->DEBUG=false;
if($_GET['DEBUG']) $this->setDebug($_GET['DEBUG']);
if(is_callable(array($classname, 'tableEditorSetup'))) {
//grab the table
$this->classname=$classname;
call_user_func(array($this->classname, 'tableEditorSetup'), &$this);
} else {
//grab the list fields
$this->listfields=$listfields;
$this->table=$classname;
//grab the edit fields, if there arent any, then edit==list
if($editfields)
$this->editfields=$editfields;
else
$this->editfields=$listfields;
if($hiddenfields)
$this->hiddenfields=$hiddenfields;
}
}
function setListFields($f)
{
$keys = array_keys($f);
$this->listfields = $f;
$this->primaryKey = $keys[0];
}
function setEditFields($f)
{
$this->editfields = $f;
}
function setHiddenFields($f)
{
$this->hiddenfields = $f;
}
function setTable($t)
{
$this->table = $t;
}
/**
* Sets the date format to use when displaying dates. Accepts anyting that php's date() function uses
* @link http://ca3.php.net/manual/en/function.date.php
* @param string $df The format string
*/
function setDateFormat($df)
{
$this->dateformat=$df;
}
/**
* Sets the time format to use when displaying times.
* @param string $tf can be either "12hrs" or "24hrs"
*/
function setTimeFormat($tf)
{
if($tf=="12hrs" || $tf=="12") $this->timeformat=="12hrs"; else $this->timeformat="24hrs";
}
function setSortField($field)
{
$_SESSION["TableEditorSort{$this->table}"]=$field;
$this->sortCurrent=$field;
}
/**
* Sets whether you are allowed to add rows to the table or not
* @param boolean $allowadd
*/
function setAllowAdding($allowadd=true)
{
$this->allowAdding=$allowadd;
}
/**
* Sets the primary autoincrement field from the database table to use to identify the rows. Usually 'id'
* @param string $field The database field name
*/
function setPrimaryKey($field)
{
$this->primaryKey=$field;
}
function setDefaultSortField($field)
{
$this->sortDefault=$field;
}
function sortField()
{
if($_SESSION["TableEditorSort{$this->table}"])
return $_SESSION["TableEditorSort{$this->table}"];
else
return $this->sortDefault;
}
function setRecordType($t)
{
$this->recordType=$t;
}
function setFieldOptions($f,$o)
{
$this->fieldOptions[$f]=$o;
}
function setFieldValidation($f,$v)
{
$this->fieldValidation[$f]=$v;
}
function setFieldDefaultValue($f,$v)
{
$this->fieldDefaults[$f]=$v;
}
function setFieldInputType($f, $t)
{
$this->fieldInputType[$f]=$t;
}
function setFieldInputOptions($f,$o)
{
$this->fieldInputOptions[$f]=$o;
}
function filterList($f,$v=false)
{
$this->fieldFilterList[$f]=$v;
}
function additionalListTable($t)
{
$this->additionalListTables[]=",`$t`";
}
function additionalListTableLeftJoin($t,$on)
{
$this->additionalListTables[]=" LEFT JOIN `$t` ON $on ";
}
function setUploadPath($p)
{
$this->uploadPath=$p;
}
function setDownloadLink($l)
{
$this->downloadLink=$l;
}
function setYearSelectRange($min,$max)
{
$this->yearSelectRangeMin=$min;
$this->yearSelectRangeMax=$max;
}
function setRowsPerPage($numrows)
{
$this->rowsPerPage=$numrows;
}
function setActivePage($page)
{
$this->activePage=$page;
}
function addActionButton($name,$link,$icon)
{
$this->actionButtons[]=array("name"=>$name,"link"=>$link, "icon"=>$icon);
}
function setDebug($d)
{
$this->DEBUG=$d;
}
function createOption($o)
{
$this->options[$o] = null;
}
function setOption($o, $v)
{
if(array_key_exists($o, $this->options)) {
$this->options[$o] = $v;
return;
}
echo "Attempt to setOption($o, $v): option doesn't exist (create it with createOption)";
exit;
}
function getOption($o)
{
if(array_key_exists($o, $this->options)) {
return $this->options[$o];
}
echo "Attempt to getOption($o): option doesn't exist (create it with createOption)";
exit;
}
function getFieldType($f)
{
$inputtype = '';
$inputmaxlen = 0;
$inputsize = 0;
//figure out what kind of input this should be
$q=mysql_query("SHOW COLUMNS FROM `{$this->table}` LIKE '$f'");
$r=mysql_fetch_object($q);
if(ereg("([a-z]*)\(([0-9,]*)\)",$r->Type,$regs))
{
switch($regs[1])
{
case "varchar":
$inputtype="text";
$inputmaxlen=$regs[2];
if($regs[2]>50) $inputsize=50; else $inputsize=$regs[2];
break;
case "int":
$inputtype="text";
$inputmaxlen=10;
$inputsize=10;
break;
case "decimal":
$inputtype="text";
$inputmaxlen=10;
$inputsize=10;
break;
case "tinyint":
$inputtype="text";
$inputmaxlen=5;
$inputsize=4;
break;
default:
$inputtype="text";
$inputmaxlen=$regs[2];
if($regs[2]>50) $inputsize=50; else $inputsize=$regs[2];
break;
}
}
else if(ereg("([a-z]*)",$r->Type,$regs))
{
switch($regs[1])
{
case "tinytext":
$inputmaxlen=255;
case "text":
$inputtype="textarea";
break;
case "date":
$inputtype="date";
break;
case "time":
$inputtype="time";
break;
case "datetime":
$inputtype="datetime";
break;
case "enum":
//an enum is a select box, but we already know what the options should be
//so rip out the options right now and add them
$inputtype="select";
$enums=substr(ereg_replace("'","",$r->Type),5,-1);
$toks=split(",",$enums);
foreach($toks as $tok)
{
$this->fieldOptions[$f][]=$tok;
}
break;
}
}
if(substr($f,0,4)=="sel_")
{
$inputtype="select_or_text";
}
if(substr($f,0,8)=="filename" && $this->uploadPath)
{
$inputtype="file";
}
if(array_key_exists($f,$this->fieldOptions))
{
//only change to select if the type is not select_or_Text
//if we are already select or text, then the options will appear
//first in the list, then any options that arent there by default
//will appear under them in the dropdown
if($inputtype!="select_or_text")
$inputtype="select";
}
return array($inputtype, $inputmaxlen, $inputsize);
}
function defaultLoad()
{
$query="SELECT {$this->primaryKey}";
foreach($this->editfields AS $f=>$n)
$query.=", `$f`";
$query.=" FROM `{$this->table}`";
$query.=" WHERE {$this->primaryKey}='{$_GET['edit']}'";
if($this->DEBUG) echo $query;
$editquery=mysql_query($query);
$editdata=mysql_fetch_assoc($editquery);
return $editdata;
}
function defaultSave($insert_mode, $keyval, $editdata)
{
$query = "";
if($insert_mode) {
$query="INSERT INTO `{$this->table}` (";
//create list of fields to insert
foreach($editdata AS $f=>$n)
$query.="`$f`,";
//rip off the last comma
$query=substr($query,0,-1);
$query.=") VALUES (";
} else {
$query="UPDATE `{$this->table}` SET ";
}
foreach($editdata AS $f=>$n)
{
if($insert_mode) $field = '';
else $field = "`$f`=";
$query .= $field.$n.",";
}
//rip off the last comma
$query=substr($query,0,-1);
if($insert_mode) {
$query.=")";
} else {
$query.=" WHERE {$this->primaryKey}='{$keyval}'";
}
if($this->DEBUG) echo $query;
mysql_query($query);
}
function defaultDelete($keyval)
{
mysql_query("DELETE FROM {$this->table} WHERE {$this->primaryKey}='{$keyval}'");
echo happy(i18n("Successfully deleted %1",array($this->recordType)));
}
function execute()
{
if($_GET['TableEditorAction']=="sort" && $_GET['sort'])
{
$this->setSortField($_GET['sort']);
}
if($_GET['TableEditorAction']=="delete" && $_GET['delete'])
{
if($this->classname)
$data = new $this->classname($_GET['delete']);
if(method_exists($data, 'tableEditorDelete')) {
$data->tableEditorDelete();
} else {
$this->defaultDelete($_GET['delete']);
}
}
if($_GET['TableEditorAction']=="page" && $_GET['page'])
{
$this->setActivePage($_GET['page']);
}
if( ($_POST['TableEditorAction']=="editsave" && $_POST['editsave'])
|| ($_POST['TableEditorAction']=="addsave") )
{
if($_POST['TableEditorAction']=="addsave") {
if($this->classname)
$data = new $this->classname();
$insert_mode = 1;
} else {
// print("Insesrt mode=0\n");
if($this->classname)
$data = new $this->classname($_POST['editsave']);
$insert_mode = 0;
}
$editdata = array();
foreach($this->editfields AS $f=>$n)
{
$inputtype = '';
if(isset($_POST['tableeditor_fieldtype'])) {
if(array_key_exists($f, $_POST['tableeditor_fieldtype'])) {
$inputtype = $_POST['tableeditor_fieldtype'][$f];
}
}
if($inputtype == 'date' || $inputtype == 'datetime') //r->Type=="date")
{
if($_POST[$f."_year"] && $_POST[$f."_month"] && $_POST[$f."_day"]) {
$yy = intval($_POST[$f."_year"]);
$mm = intval($_POST[$f."_month"]);
$dd = intval($_POST[$f."_day"]);
$editdata[$f] = "'$yy-$mm-$dd";
if($inputtype == 'date') {
$editdata[$f] .= "'";
} else if($_POST[$f."_hour"]!="" && $_POST[$f."_minute"]!="") {
$hh = intval($_POST[$f."_hour"]);
$mi = intval($_POST[$f."_minute"]);
$editdata[$f] .= " $hh:$mi:00'";
} else {
$editdata[$f] = 'NULL';
}
} else {
$editdata[$f] = 'NULL';
}
}
else if($inputtype == 'time') //r->Type=="time")
{
if($_POST[$f."_hour"]!="" && $_POST[$f."_minute"]!="") {
$editdata[$f] = "'".mysql_escape_string(stripslashes($_POST[$f."_hour"])).":".
mysql_escape_string(stripslashes($_POST[$f."_minute"])).":00'";
} else {
$editdata[$f] = 'NULL';
}
}
else if($inputtype == 'multicheck')
{
/* This one has no direct quoted translation, hope the user specified
* a save routine to handle this */
$editdata[$f] = array();
if($_POST[$f]) {
$a = $_POST[$f];
foreach($a as $k=>$val) {
$editdata[$f][$k] = $val;
}
}
}
else if(substr($f,0,4)=="sel_")
{
//chose the text field first, if its been filled in, otherwise, go with the select box
if($_POST[$f."_text"])
$editdata[$f] = "'".mysql_escape_string(stripslashes($_POST[$f."_text"]))."'";
else if($_POST[$f."_select"])
$editdata[$f] = "'".mysql_escape_string(stripslashes($_POST[$f."_select"]))."'";
else
{
//maybe the options were over-wridden, if so, just check the field name
$editdata[$f] = "'".mysql_escape_string(stripslashes($_POST[$f]))."'";
}
}
else if(strtolower($f)=="website" && $_POST[$f])
{
//intelligently handle website fields, making sure they have the protocol to use
//but allow them to enter http:// or https:// themselves.
//if no protocol is given, assume http://
if(substr(strtolower($_POST[$f]),0,4)=="http")
$editdata[$f] = "'".mysql_escape_string(stripslashes($_POST[$f]))."'";
else
$editdata[$f] = "'http://".mysql_escape_string(stripslashes($_POST[$f]))."'";
}
else if(substr($f,0,8)=="filename" && $this->uploadPath)
{
//accept the upload
if($_FILES[$f]['size']>0)
{
if(file_exists($this->uploadPath."/".$_FILES[$f]['name']))
echo error(i18n("A file with that filename already exists, it will be overwritten"));
move_uploaded_file($_FILES[$f]['tmp_name'],$this->uploadPath."/".$_FILES[$f]['name']);
$editdata[$f] = "'".mysql_escape_string(stripslashes($_FILES[$f]['name']))."'";
}
else
{
//maybe they want to delete it, in which case $_POST['clear'] will be an array and have this field name in it.
if(is_array($_POST['clear']))
{
if(in_array($f,$_POST['clear']))
$editdata[$f] = 'NULL';
}
}
}
else
{
if($this->fieldValidation[$f])
$editdata[$f] = "'".mysql_escape_string(stripslashes(ereg_replace($this->fieldValidation[$f],"",$_POST[$f])))."'";
else
$editdata[$f] = "'".mysql_escape_string(stripslashes($_POST[$f]))."'";
}
}
if(count($this->hiddenfields))
{
foreach($this->hiddenfields AS $f=>$n)
{
//well well... sometimes we want to use a function here, such as NOW(), so if thats the case then we dont want the ' ' around the value, so, lets check for NOW() and handle it differently
if(strtolower($n)=="now()")
$editdata[$f] = "$n";
else
$editdata[$f] = "'$n'";
}
}
if(method_exists($data, 'tableEditorSave')) {
$data->tableEditorSave($editdata);
} else {
$keyval = ($insert_mode == 0) ? $_POST['editsave'] : 0;
$this->defaultSave($insert_mode, $keyval, $editdata);
}
if($inser_tmode) {
$text_error = "adding new";
$text_happy = "added new";
} else {
$text_error = "saving";
$text_happy = "saved";
}
// if($this->DEBUG) echo $query;
// mysql_query($query);
if(mysql_error())
{
echo error(i18n("Error $text_error %1: %2",array($this->recordType,mysql_error())));
}
else
{
echo happy(i18n("Successfully $text_happy %1",array($this->recordType)));
}
}
if($_GET['TableEditorAction']=="add" || ($_GET['TableEditorAction']=="edit" && $_GET['edit']) )
{
if($this->uploadPath)
echo "<form name=\"TableEditor\" enctype=\"multipart/form-data\" method=\"post\" action=\"{$_SERVER['PHP_SELF']}\">";
else
echo "<form name=\"TableEditor\" method=\"post\" action=\"{$_SERVER['PHP_SELF']}\">";
$action=$_GET['TableEditorAction'];
if($action=="add")
{
echo "<h2>".i18n("Add New %1",array($this->recordType))."</h2>";
echo "<input type=\"hidden\" name=\"TableEditorAction\" value=\"addsave\">";
//check for any defaults
if(count($this->fieldDefaults))
{
foreach($this->fieldDefaults AS $f=>$n)
$editdata[$f]=$n;
}
}
else if($action=="edit")
{
echo "<h2>".i18n("Edit %1",array($this->recordType))."</h2>";
echo "<input type=\"hidden\" name=\"TableEditorAction\" value=\"editsave\">";
echo "<input type=\"hidden\" name=\"editsave\" value=\"{$_GET['edit']}\">";
if($this->classname)
$data = new $this->classname($_GET['edit']);
if(method_exists($data, 'tableEditorLoad')) {
$editdata = $data->tableEditorLoad();
} else {
$editdata = $this->defaultLoad();
}
}
echo "<script language=\"javascript\" type=\"text/javascript\">
<!--
function do_maxlength(obj, maxlength)
{
if(obj.value.length > maxlength) {
obj.value = obj.value.substr(0, maxlength);
}
}
-->
</script>";
echo "<table class=\"tableedit\">";
foreach($this->editfields AS $f=>$n)
{
$pos = strpos($n, "|");
$n2 = "";
if($pos != false) {
$n2 = i18n(substr($n, $pos + 1)).' ';
$n = substr($n, 0, $pos);
}
echo "<tr><th valign=\"top\">".i18n($n)."</th><td>$n2";
/* If we know the input type, assume the user knows what they are doing, else,
* try to query it from the databse */
if(isset($this->fieldInputType[$f])) {
$inputtype = $this->fieldInputType[$f];
$inputmaxlen = 0; // FIXME
$inputsize = 0; // FIXME
} else {
list($inputtype, $inputmaxlen, $inputsize) = $this->getFieldType($f);
}
switch($inputtype)
{
case "text":
if($this->fieldInputOptions[$f])
echo "<input type=\"text\" ".$this->fieldInputOptions[$f]." id=\"$f\" name=\"$f\" value=\"".htmlspecialchars($editdata[$f])."\"/>";
else
echo "<input type=\"text\" size=\"$inputsize\" maxlength=\"$inputmaxlen\" id=\"$f\" name=\"$f\" value=\"".htmlspecialchars($editdata[$f])."\"/>";
break;
case "textarea":
$maxlen = ($inputmaxlen > 0) ? " onkeypress=\"return do_maxlength(this, $inputmaxlen);\" " : '';
if($this->fieldInputOptions[$f])
echo "<textarea id=\"$f\" name=\"$f\" $maxlen".$this->fieldInputOptions[$f].">".htmlspecialchars($editdata[$f])."</textarea>";
else
echo "<textarea id=\"$f\" name=\"$f\" $maxlen rows=\"5\" cols=\"50\">".htmlspecialchars($editdata[$f])."</textarea>";
break;
case "select":
if($this->fieldInputOptions[$f])
echo "<select ".$this->fieldInputOptions[$f]." id=\"$f\" name=\"".$f."\">";
else
echo "<select id=\"$f\" name=\"".$f."\">";
echo "<option value=\"\">".i18n("Choose")."</option>\n";
foreach($this->fieldOptions[$f] AS $opt)
{
if(is_array($opt))
{
if("{$opt['key']}" == "{$editdata[$f]}") $sel="selected=\"selected\""; else $sel="";
echo "<option $sel value=\"".$opt['key']."\">".i18n($opt['val'])."</option>\n";
}
else
{
if("{$opt}" == "{$editdata[$f]}") $sel="selected=\"selected\""; else $sel="";
echo "<option $sel value=\"".$opt."\">".i18n($opt)."</option>\n";
}
}
echo "</select>";
break;
case "enum":
break;
case "select_or_text":
$optq=mysql_query("SELECT DISTINCT($f) AS $f FROM `{$this->table}` ORDER BY $f");
if($this->fieldInputOptions[$f])
echo "<select ".$this->fieldInputOptions[$f]." id=\"".$f."_select\" name=\"".$f."_select\">";
else
echo "<select id=\"".$f."_select\" name=\"".$f."_select\">";
echo "<option value=\"\">".i18n("Choose or type")."</option>\n";
if($this->fieldOptions[$f])
{
foreach($this->fieldOptions[$f] AS $opt)
{
if($opt == $editdata[$f]) $sel="selected=\"selected\""; else $sel="";
echo "<option $sel value=\"".$opt."\">".i18n($opt)."</option>\n";
}
echo "<option value=\"\">-------------</option>";
}
// print_r($this->fieldOptions[$f]);
while($opt=mysql_fetch_object($optq))
{
if(is_array($this->fieldOptions[$f]) && in_array($opt->$f,$this->fieldOptions[$f]))
continue;
if($opt->$f == $editdata[$f]) $sel="selected=\"selected\""; else $sel="";
echo "<option $sel value=\"".$opt->$f."\">".i18n($opt->$f)."</option>\n";
}
echo "</select>";
echo " or ";
//only show the input half-sized because its beside the select box which is already taking up space.
$inputsize=round($inputsize/2);
//input always starts emptpy as well, because we already have it selected in the list
if($this->fieldInputOptions[$f])
echo "<input type=\"text\" ".$this->fieldInputOptions[$f]." id=\"".$f."_text\" name=\"".$f."_text\" value=\"\" />";
else
echo "<input type=\"text\" size=\"$inputsize\" maxlength=\"$inputmaxlen\" id=\"".$f."_text\" name=\"".$f."_text\" value=\"\" />";
break;
case "multicheck":
$ks = array_keys($this->fieldOptions[$f]);
foreach($ks as $k) {
$ch = '';
if(is_array($editdata[$f])) {
if(array_key_exists($k, $editdata[$f])) {
$ch = ' checked=checked ';
}
}
echo "<input type=\"checkbox\" name=\"{$f}[$k]\" value=\"1\" $ch> {$this->fieldOptions[$f][$k]}<br>";
}
echo "<input type=\"hidden\" name=\"tableeditor_fieldtype[$f]\" value=\"multicheck\">";
break;
case "date":
case "datetime":
$a = split('[- :]',$editdata[$f]);
if($inputtype == 'date') {
list($yy,$mm,$dd)=$a;
$w = 10;
} else {
list($yy,$mm,$dd,$hh,$mi,$ss)=$a;
$w=15;
}
//if we put a small width here, then it prevents it from expanding to whatever width it feels like.
echo "<table width=\"$w\" align=\"left\" cellspacing=0 cellpadding=0>";
echo "<tr><td class=\"tableedit\" >";
$this->month_selector($f."_month",$mm);
echo "</td><td class=\"tableedit\">";
$this->day_selector($f."_day",$dd);
echo "</td><td class=\"tableedit\">";
$this->year_selector($f."_year",$yy);
echo "</td>";
if($inputtype == 'date') {
echo "</tr>";
echo "</table>";
echo "<input type=\"hidden\" name=\"tableeditor_fieldtype[$f]\" value=\"date\">";
break;
}
/* Else, fall through, with hh, mi, ss already set */
case "time":
if($inputtype == 'time') {
list($hh,$mi,$ss)=split(":",$editdata[$f]);
echo "<table width=\"10\" cellspacing=0 cellpadding=0>";
echo "<tr>";
}
/* Common code for time, and datetime */
echo "<td class=\"tableedit\">";
$this->hour_selector($f."_hour",$hh,false,"12hr");
echo "</td><td class=\"tableedit\">";
$this->minute_selector($f."_minute",$mi);
echo "</td></tr>";
echo "</table>";
echo "<input type=\"hidden\" name=\"tableeditor_fieldtype[$f]\" value=\"$inputtype\">";
break;
case "file":
if($editdata[$f])
{
if(file_exists($this->uploadPath."/".$editdata[$f]))
{
//only show a link to the file if the upload path is inside the document root
if(strstr(realpath($this->uploadPath),$_SERVER['DOCUMENT_ROOT']))
{
echo "<A href=\"{$this->uploadPath}/{$editdata[$f]}\">{$editdata[$f]}</a>";
}
else
{
echo $editdata[$f];
}
echo " (".filesize($this->uploadPath."/".$editdata[$f])." bytes) <input type=\"checkbox\" name=\"clear[]\" value=\"$f\">delete<br />";
}
else
{
echo $editdata[$f]." (does not exist)<br />";
}
}
echo "<input type=\"file\" ".$this->fieldInputOptions[$f]." id=\"$f\" name=\"$f\" />";
break;
default:
echo "<input type=\"text\" id=\"$f\" name=\"$f\" value=\"".htmlspecialchars($editdata[$f])."\"/>";
}
echo "</td></tr>";
}
echo "<tr><td align=\"center\" colspan=\"2\">";
echo "<table width=\"80%\"><tr><td valign=\"top\" align=\"center\" width=\"50%\">";
if($action=="add")
echo "<input type=\"submit\" value=\"".i18n("Add New %1",array($this->recordType))."\">";
else if($action=="edit")
echo "<input type=\"submit\" value=\"".i18n("Save %1",array($this->recordType))."\">";
echo "</form>";
echo "</td><td valign=\"top\" align=\"center\" width=\"50%\">";
echo "<form method=\"get\" action=\"{$_SERVER['PHP_SELF']}\">";
echo "<input type=\"submit\" value=\"".i18n("Cancel")."\">";
echo "</form>";
echo "</td></tr></table>";
echo "</td></tr>";
echo "</table>";
}
else if($_GET['TableEditorAction']=="export")
{
//fixme: how to do an export? we cant send headers because its possible that output has already started!
}
else
{
$this->displayTable();
}
}
function defaultGetList()
{
$sel = array();
$from = array();
$where = array();
foreach($this->listfields AS $f=>$n)
$sel[] = "`$f`";
$from[] = "`{$this->table}`";
if(count($this->additionalListTables)) {
foreach($this->additionalListTables as $t) {
$from[] = "$t ";
}
}
if(count($this->fieldFilterList)) {
foreach($this->fieldFilterList AS $k=>$v) {
$where[] = ($v == false) ? $k : "`$k`='$v'";
}
}
return array($sel, $from, $where);
}
function displayTable()
{
global $icon_path;
global $icon_extension;
$query="SELECT SQL_CALC_FOUND_ROWS {$this->primaryKey}";
if(is_callable(array($this->classname, 'tableEditorGetList'))) {
list($sel, $from, $where) = call_user_func(array($this->classname, 'tableEditorGetList'), &$this);