-
Notifications
You must be signed in to change notification settings - Fork 41
/
ajaxCRUD.class.php
2895 lines (2346 loc) · 124 KB
/
ajaxCRUD.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
<?php
/*
Basic users should not need to edit this file
Instead - post questions or feature requests to our forum -
http://ajaxcrud.com/forum/viewforum.php?f=2
*/
/************************************************************************/
/* ajaxCRUD.class.php v9.1 */
/* =========================== */
/* Copyright (c) 2013 by Loud Canvas Media ([email protected]) */
/* http://www.ajaxcrud.com by http://www.loudcanvas.com */
/* */
/* This program is free software. You can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 2 of the License. */
/************************************************************************/
# thanks to the following for help on v6.0:
# Mariano Monta ez Ureta, from Argentina; twitter: @nanomo
# Jing Ling, New Hampshire
#thanks to Francisco Campos of WebLemurs.com for helping with other misc core updates for v7.2
define('EXECUTING_SCRIPT', $_SERVER['PHP_SELF']);
if (isset($_REQUEST['customAction'])){
$customAction = $_REQUEST['customAction'];
if ($customAction != ""){
if ($customAction == 'exportToCSV'){
$csvData = $_REQUEST['tableData'];
$fileName = $_REQUEST['fileName'];
header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=$fileName");
header("Pragma: no-cache");
header("Expires: 0");
echo str_replace('\"','"',$csvData);
}
exit();
}
}
#this top part is for the ajax actions themselves. the class is below
if (isset($_REQUEST['ajaxAction'])){
$ajaxAction = $_REQUEST['ajaxAction'];
if ($ajaxAction != ""){
# these lines make sure caching do not cause ajax saving/displaying issues
header("Cache-Control: no-cache, must-revalidate"); //this is why ajaxCRUD.class.php must be before any other headers (html) are outputted
# a date in the past
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
$table = isset($_REQUEST["table"]) ? $_REQUEST["table"] : "";
$pk = isset($_REQUEST["pk"]) ? trim($_REQUEST["pk"]) : "";
$field = isset($_REQUEST["field"]) ? trim($_REQUEST["field"]) : "";
$id = isset($_REQUEST["id"]) ? $_REQUEST["id"] : "";
$val = isset($_REQUEST["val"]) ? $_REQUEST["val"] : "";
$table_num = isset($_REQUEST["table_num"]) ? $_REQUEST["table_num"] : "";
if (!is_numeric($id)){
$sql_id = "\"$id\"";
}
else{
$sql_id = $id;
}
if ($ajaxAction == 'add'){
echo $_SESSION[$table];
}
if ($ajaxAction == 'filter'){
echo $_SESSION[$table];
}
if ($ajaxAction == 'sort'){
echo $_SESSION[$table];
}
if ($ajaxAction == 'getRowCount'){
echo $_SESSION[$table . '_row_count'];
}
if ($ajaxAction == 'update'){
$val = addslashes($val);
//check to see if record exists
$row_current_value = q1("SELECT $pk FROM $table WHERE $pk = $sql_id");
if ($row_current_value == ''){
qr("INSERT INTO $table ($pk) VALUES (\"$id\")");
}
$success = qr("UPDATE $table SET $field = \"$val\" WHERE $pk = $sql_id");
if ($val == '') $val = " ";
//when updating, we use the Table name, Field name, & the Primary Key (id) to feed back to client-side-processing
$prefield = trim($table . $field . $id);
if (isset($_REQUEST['dropdown_tbl'])){
$val = "{selectbox}";
}
if ($success){
echo $prefield . "|" . stripslashes($val);
}
else{
echo "error|" . $prefield . "|" . stripslashes($val);
}
}
if ($ajaxAction == 'delete'){
qr("DELETE FROM $table WHERE $pk = $sql_id");
echo $table . "|" . $id;
}
exit();
}
}
// THE AJAXCRUD CLASS FOLLOWS:
// Use:
// Create an ajaxCRUD object.
// $table = new ajaxCRUD(name of item, table name, primary key);
// Example:
// $tblFAQ = new ajaxCRUD("FAQ", "tblFAQ", "pkFAQID");
// $tblFAQ->showTable();
//
// Note: !! Your table must have AUTO_INCREMENT enabled for the primary key !!
// Note: !! Your version of mySQL must support string->INT conversion (thus "1" = 1) and "" is a NULL value !!
class ajaxCRUD{
var $ajaxcrud_root;
var $ajax_file;
var $css_file;
var $css = true; //indicates a css spredsheet WILL be used
var $add = true; //adding is ok
var $includeTableHeaders = true; //include table headers (default)
var $includeJQuery = true; //include jquery (default)
var $allowHeaderInsert = true; //insert the jquery/css files by default [you can insert whereever you want in your script with $yourObject->insertHeader();]
var $doActionOnShowTable; //boolean var. When true and showTable() is called, doAction() is also called. turn off when you want to only have a table show in certain conditions but CRUD operations can take place on the table "behind the scenes"
var $item_plural;
var $item;
var $db_table;
var $db_table_pk;
var $db_main_field;
var $row_count;
var $table_html; //the html for the table (to be modified on ADD via ajax)
var $cellspacing;
var $showPaging = true;
var $limit; // limit of rows to display on one page. defaults to 50
var $sql_limit;
var $filtered_table = false; //the table is by default unfiltered (eg no 'where clause' on it)
var $ajaxFilter_fields = array(); //array of fields that can be are filtered by ajax (creates a textbox at the top of the table)
var $ajaxFilterBoxSize = array(); //array (sub fieldname) holding size of the input box
var $AjaxFilterBoxStyle = array(); //array (sub fieldname) holding style of select box
//all fields in the table
var $fields = array();
var $field_count;
//field datatypes
var $field_datatype = array(); //$field_datatype[field] = datatype
//allow delete of fields | boolean variable set to true by default
var $delete;
//defines if the add functionality uses ajax
var $ajax_add = true;
//defines if the class allows you to edit all fields (only used to turn off ALL editing completely)
var $ajax_editing = true;
//defines if the class allows you to sort all fields
var $ajax_sorting = true;
//the fields to be displayed
var $display_fields = array();
//the fields to be inputted when adding a new entry (90% time will be all fields). can be changed via the omitAddField method
var $add_fields = array();
var $add_form_top = FALSE; //the add form (by default) is below the table. use displayAddFormTop() to bring it to the top
//the fields which are displayed, but not editable
var $uneditable_fields = array();
//the header fields which are displayed, but not sortable (i.e. click to sort)
var $unsortable_fields = array();
var $sql_where_clause;
var $sql_where_clauses = array(); //array used IF there is more than one where clause
var $sql_order_by;
var $num_where_clauses;
var $on_add_specify_primary_key = false;
//table border - default is off: 0
var $border;
var $orientation; //orientation of table (detault is horizontal)
var $showCSVExport = false; // indicates whether to show the "Export Table to CSV" button
var $exportCSVSeparator = ','; // sets the CSV field separator
//array containing values for a button next to the "go back" button at the bottom. [0] = value [1] = url [2] = extra tags/javascript
var $bottom_button = array();
//array with value being the url for the buttom to go to (passing the id) [0] = value [1] = url
var $row_button = array();
//array with value being "same" or "new" - specifying the target window for the opening the page. index of array is the button 'id'
var $addButtonToRowWindowOpen = "";
################################################
#
# The following are parallel arrays to help in the definition of a defined db relationship
#
################################################
//values will be the name(s) of the foreign key(s) for a category table
var $db_table_fk_array = array();
//values will be the name(s) of the category table(s)
var $category_table_array = array();
//values will be the name(s) of the primary key for the category table(s)
var $category_table_pk_array = array();
//values will be the name(s) of the field to return in the category table(s)
var $category_field_array = array();
//values will be the (optional) name of the field to sort by in the category table(s)
var $category_sort_field_array = array();
//values will be the (optional) whereclause for the fk clause
var $category_whereclause_array = array();
//for dropdown (to make an empty box). (format: array[field] = true/false)
var $category_required = array();
// allowable values for a field. the key is the name of the field
var $allowed_values = array();
// "on" and "off" values for a checkbox. the key is the name of the field
var $checkbox = array();
// holds the field names of columns that will have a "check all" checkbox
var $checkboxall = array();
//values to be set to a particular field when a new row is added. the array is set as $field_name => $add_value
var $add_values = array();
//destination folder to be set for a particular field that allows uploading of files. the array is set as $field_name => $destination_folder
var $file_uploads = array();
var $file_upload_info = array(); //array[$field_name]['destination_folder'], array[$field_name]['relative_folder'], and array[$field_name]['permittedFileExts']
var $filename_append_field = "";
//array dictating that "dropdown" fields do not show dropdown (but text editor) on edit (format: array[field] = true/false);
//used in defineAllowableValues function
var $field_no_dropdown = array();
//array holding the (user-defined) function to format a field with on display (format: array[field] = function_name);
//used in formatFieldWithFunction function
var $format_field_with_function = array();
var $validate_delete_with_function = ""; //used to determine if a particular row can be deleted or not (user-defined function)
//used in formatFieldWithFunctionAdvanced function (takes a second param - the id of the row)
var $format_field_with_function_adv = array();
var $onAddExecuteCallBackFunction;
var $onUpdateExecuteCallBackFunction = array(); //this is an array because callback methods for update (unlike add) are field based
var $onFileUploadExecuteCallBackFunction;
var $onDeleteFileExecuteCallBackFunction;
//(if true) put a checkbox before each row
var $showCheckbox;
var $loading_image_html;
var $emptyTableMessage;
/* these default to english words (e.g. "Add", "Delete" below); but can be
changed by setting them via $obj->addText = "Añadir"
*/
var $addText, $deleteText, $uploadText, $cancelText, $actionText, $fileDeleteText, $fileEditText; //text values for buttons and other table text
var $addButtonText; //if you want to replace the entire add button text with a phrase or other text. Added in 8.81
var $addMessage; //used when onAddExecuteCallBackFunction is leveraged
var $sort_direction; //used when sorting the table via ajax
################################################
#
# displayAs array is for linking a particular field to the name that displays for that field
#
################################################
//the indexes will be the name of the field. the value is the displayed text
var $displayAs_array = array();
//height of the textarea for certain fields. the index is the field and the value is the height
var $textarea_height = array();
var $textboxWidth = array(); //if defined for regular text input boxes, this will alter how ADD fields are displayed
//any 'notes' to display next to a field when adding a row
var $fieldNote = array();
//a placeholder text to give to the field when ADDing a new row
var $placeholderText = array();
//variable used to capture which search fields should be EXACT matches vs approximate match (using LIKE %search%)
var $exactSearchField = array(); //set by setExactSearchField (and automatically set for fields using defineRelationship and defineAllowableValues)
//set manually - initial value for a field (when adding a row)
var $initialFieldValue = array();
// Array to include css style classes in specified fields
var $display_field_with_class_style = array();
// Constructor
//by default ajaxCRUD assumes all necessary files are in the same dir as the script calling it (eg $ajaxcrud_root = "")
function __construct($item, $db_table, $db_table_pk, $ajaxcrud_root = "") {
//global variable - for allowing multiple ajaxCRUD tables on one page
global $num_ajaxCRUD_tables_instantiated;
if ($num_ajaxCRUD_tables_instantiated === "") $num_ajaxCRUD_tables_instantiated = 0;
global $headerAdded;
if ($headerAdded === "") $headerAdded = FALSE;
$this->showCheckbox = false;
$this->ajaxcrud_root = $ajaxcrud_root;
$this->ajax_file = EXECUTING_SCRIPT;
$this->item = $item;
$this->item_plural = $item . "s";
$this->db_table = $db_table;
$this->db_table_pk = $db_table_pk;
$this->fields = $this->getFields($db_table);
$this->field_count = count($this->fields);
//by default paging is turned on; limit is 50
$this->showPaging = true;
$this->limit = 50;
$this->num_where_clauses = 0;
$this->delete = true;
$this->add = true;
//assumes the primary key is auto incrementing
$this->primaryKeyAutoIncrement = true;
$this->border = 0;
$this->css = true;
$this->ajax_add = true;
$this->orientation = 'horizontal';
$this->addButtonToRowWindowOpen = 'same'; //global window to open pages in - used when adding custom buttons to a row
$this->doActionOnShowTable = true;
$this->loading_image_html = "<center><br /><br /><img src=\'" . $this->ajaxcrud_root . "css/loading.gif\'><br /><br /></center>"; //changed via setLoadingImageHTML()
$this->addText = "Add";
$this->deleteText = "Delete";
$this->uploadText = "Upload";
$this->cancelText = "Cancel";
$this->actionText = "Action";
$this->fileEditText = "edit"; //added in 8.81 (for file crud)
$this->fileDeleteText = "del"; //added in 8.81 (for file crud)
$this->emptyTableMessage = "No data in this table. Click add button below.";
$this->addButtonText = ""; //when blank, button text defaults to 'Add {Item}' text unless when $addText is set then defaults to '{addText} {Item}'; if set the entire button is replaced with {addButtonText}
$this->addMessage = ""; //when blank, defaults to generic '{Item} added' message
$this->onAddExecuteCallBackFunction = '';
$this->onFileUploadExecuteCallBackFunction = '';
$this->onDeleteFileExecuteCallBackFunction = '';
//don't allow primary key to be editable
$this->uneditable_fields[] = $this->db_table_pk;
$this->display_fields = $this->fields;
$this->add_fields = $this->fields;
//default sort direction
$this->sort_direction = "desc";
if ($this->field_count == 0){
$error_msg[] = "No fields in this table!";
echo_msg_box();
exit();
}
return true;
}
function getNumRows(){
$sql = "SELECT COUNT(*) FROM " . $this->db_table . $this->sql_where_clause;
$numRows = q1($sql);
return $numRows;
}
function setAjaxFile($ajax_file){
$this->ajax_file = $ajax_file;
}
function setOrientation($orientation){
$this->orientation = $orientation;
}
function turnOffAjaxADD(){
$this->ajax_add = false;
}
function turnOffAjaxEditing(){
$this->ajax_editing = false;
foreach ($this->fields as $field){
$this->disallowEdit($field);
}
}
function turnOffSorting(){
$this->ajax_sorting = false;
foreach ($this->fields as $field){
$this->disallowSort($field);
}
}
function turnOffPaging($limit = ""){
$this->showPaging = false;
if ($limit != ''){
$this->sql_limit = " LIMIT $limit";
}
}
function disableTableHeaders() {
$this->includeTableHeaders = false;
}
function disableJQuery() {
$this->includeJQuery = false;
}
function setCSSFile($css_file){
$this->css_file = $css_file;
}
function setLoadingImageHTML($html){
$this->loading_image_html = $html;
}
function addTableBorder(){
$this->border = 1;
}
function addAjaxFilterBox($field_name, $textboxSize = 10, $exactSearch = FALSE){
$this->ajaxFilter_fields[] = $field_name;
//defaults to size of "10" (unless changed via setAjaxFilterBoxSize)
$this->setAjaxFilterBoxSize($field_name, $textboxSize);
if ($exactSearch === TRUE){
$this->setExactSearchField($field_name);
}
}
function setAjaxFilterBoxSize($field_name, $size){
$this->ajaxFilterBoxSize[$field_name] = $size; //this function is deprecated, as of v6.0
}
function setAjaxFilterBoxStyle($field_name, $style){
$this->AjaxFilterBoxStyle[$field_name] = $style;
}
function addAjaxFilterBoxAllFields(){
//unset($this->ajaxFilter_fields);
foreach ($this->display_fields as $field){
$this->addAjaxFilterBox($field);
}
}
function disableAjaxFilterBox($field_name){
// Sequence of method usage: beware Filter fields added after this removal
if (in_array($field_name, $this->ajaxFilter_fields)) {
$key = array_search($field_name, $this->ajaxFilter_fields);
unset ($this->ajaxFilter_fields[$key]);
}
if (in_array($field_name, $this->ajaxFilterBoxSize)) {
unset ($this->ajaxFilterBoxSize[$field_name]);
}
}
function disableAjaxFilterBoxAllFields(){
foreach ($this->display_fields as $field){
$this->disableAjaxFilterBox($field);
}
}
function displayAddFormTop(){
$this->add_form_top = TRUE;
}
function addWhereClause($sql_where_clause){
$this->num_where_clauses++;
$this->sql_where_clauses[] = $sql_where_clause;
if ($this->num_where_clauses <= 1){
$this->sql_where_clause = " " . $sql_where_clause;
}
else{
//chain multiple together
$whereClause = ""; //start the clause now chain to it
$count = 0;
foreach($this->sql_where_clauses as $where_clause){
if ($count > 0){
//$where_clause = str_replace("WHERE", "AND", $where_clause);
$where_clause = preg_replace('/WHERE/', 'AND', $where_clause, 1); // Only replace the FIRST instance; the magic is in the optional fourth parameter [Limit] (this is important because of sub queries which uses a second WHERE statement)
}
$whereClause .= " $where_clause";
$count++;
}
$this->sql_where_clause = " $whereClause";
}
$_SESSION['ajaxcrud_where_clause'][$this->db_table] = $this->sql_where_clause;
}
function addOrderBy($sql_order_by){
$this->sql_order_by = " " . $sql_order_by;
}
/* added in release 6.0 */
function orderFields($fieldsString){
/* warning - if you add a field to this list which is not in the database,
you may have unintended results */
//separate fieldsString with ","
$fieldsString = str_replace(" ", "", $fieldsString); //parse out any spaces
$fieldsArray = explode(",", $fieldsString);
foreach($this->display_fields as $d){
if(!in_array($d,$fieldsArray))
$fieldsArray[] = $d;
}
$this->display_fields = $fieldsArray;
}
function formatFieldWithFunction($field, $function_name){
$this->format_field_with_function[$field] = $function_name;
}
function formatFieldWithFunctionAdvanced($field, $function_name){
$this->format_field_with_function_adv[$field] = $function_name;
}
/* added in R8.7
uses a user-defined function which will return true or false re whether THAT ROW
is deletable (validateDeleteWithFunction) or any any field in that row is editable (validateUpdateWithFunction)
Example and Documentation: http://ajaxcrud.com/api/index.php?id=validateDeleteWithFunction
Example and Documentation: http://ajaxcrud.com/api/index.php?id=validateUpdateWithFunction
*/
function validateDeleteWithFunction($function_name){
$this->validate_delete_with_function = $function_name;
}
function validateUpdateWithFunction($function_name){
$this->validate_update_with_function = $function_name;
}
function defineRelationship($field, $category_table, $category_table_pk, $category_field_name, $category_sort_field = "", $category_required = "1", $where_clause = ""){
$this->db_table_fk_array[] = $field;
$this->category_table_array[] = $category_table;
$this->category_table_pk_array[] = $category_table_pk;
$this->category_field_array[] = $category_field_name;
$this->category_sort_field_array[] = $category_sort_field;
$this->category_whereclause_array[] = $where_clause;
//make the relationship required for the field
if ($category_required == "1"){
$this->category_required[$field] = TRUE;
}
$this->setExactSearchField($field); //set search field to use exact matching (as of v7.2.1)
}
function relationshipFieldOptional(){
$this->cat_field_required = FALSE;
}
function defineAllowableValues($field, $array_values, $onedit_textbox = FALSE, $exactSearch = TRUE){
//array with the setup [0] = value [1] = display name (both the same)
$new_array = array();
foreach($array_values as $array_value){
if (!is_array($array_value)){
//a two-dimensional array --> set both the value and dropdown text to be the same
$new_array[] = array(0=> urlencode($array_value), 1=>$array_value);
}
else{
//a 2-dimensional array --> value and dropdown text are different
$new_array[] = $array_value;
}
}
if ($onedit_textbox != FALSE){
$this->field_no_dropdown[$field] = TRUE;
}
$this->allowed_values[$field] = $new_array;
if ($exactSearch === TRUE) $this->setExactSearchField($field); //set search field to use exact matching (as of 7.2.1)
else $this->resetExactSearchField($field);
}
function defineAllowableValuesFromSQL($field, $sql, $onedit_textbox = FALSE, $exactSearch = TRUE){
$values = q($sql);
$array_values = Array();
foreach ($values as $value) $array_values[] = $value[0];
$this->defineAllowableValues($field, $array_values, $onedit_textbox, $exactSearch);
}
function defineCheckbox($field, $value_on="1", $value_off="0"){
$new_array = array($value_on, $value_off);
$this->checkbox[$field] = $new_array;
}
function showCheckboxAll($field, $display_data) {
$this->checkboxall[$field] = $display_data;
}
function displayAs($field, $the_field_name){
$this->displayAs_array[$field] = $the_field_name;
}
function setTextareaHeight($field, $height){
$this->textarea_height[$field] = $height;
}
function setTextboxWidth($field, $width){
$this->textboxWidth[$field] = $width;
}
function setAddFieldNote($field, $caption){
$this->fieldNote[$field] = $caption;
}
/* added in R8.0 */
function setAddPlaceholderText($field, $placeholder){
$this->placeholderText[$field] = $placeholder;
}
/* added in R7.2.1 */
function setExactSearchField($field) {
$this->exactSearchField[$field] = true;
}
function resetExactSearchField($field) {
if(array_key_exists($field, $this->exactSearchField))
unset($this->exactSearchField[$field]);
}
function setInitialAddFieldValue($field, $value){
$this->initialFieldValue[$field] = $value;
}
function setLimit($limit){
$this->limit = $limit;
}
//DEPRECATED - use insertRowsReturned instead for realtime updating with ajax
function getRowCount(){
if ($_SESSION['row_count'] == ""){
$count = $this->getNumRows();
}
else{
$count = $_SESSION['row_count'];
}
//return $count;
return "<span id='" . $this->db_table . "_RowCount'>" . $count . "</span>";
}
function getTotalRowCount(){
$count = q1("SELECT COUNT(*) FROM " . $this->db_table);
return $count;
}
function omitField($field_name){
$key = array_search($field_name, $this->display_fields);
if ($this->fieldInArray($field_name, $this->display_fields)){
unset($this->display_fields[$key]);
}
else{
$error_msg[] = "Error in your doNotDisplay function call. There is no field named <b>$field_name</b> in the table <b>" . $this->db_table . "</b>";
}
}
function omitAddField($field_name){
$key = array_search($field_name, $this->add_fields);
if ($key !== FALSE){
unset($this->add_fields[$key]);
}
else{
$error_msg[] = "Error in your omitAddField function call. There is no field named <b>$field_name</b> in the table <b>" . $this->db_table . "</b>";
}
}
function omitFieldCompletely($field_name){
$this->omitField($field_name);
$this->omitAddField($field_name);
}
/* added with R6.0 */
function showOnly($fieldsString){
//separate fieldsString with ","
$fieldsString = str_replace(" ", "", $fieldsString); //parse out any spaces
$fieldsArray = explode(",", $fieldsString);
$this->display_fields = $fieldsArray;
$this->add_fields = $fieldsArray;
}
function addValueOnInsert($field_name, $insert_value){
$this->add_values[] = array(0 => $field_name, 1 => $insert_value);
}
function onAddExecuteCallBackFunction($function_name){
$this->onAddExecuteCallBackFunction = $function_name;
$this->ajax_add = false;
}
function onUpdateExecuteCallBackFunction($field_name, $function_name){
$this->onUpdateExecuteCallBackFunction[$field_name] = $function_name;
}
function onFileUploadExecuteCallBackFunction($function_name){
$this->onFileUploadExecuteCallBackFunction = $function_name;
}
function onDeleteFileExecuteCallBackFunction($function_name){
$this->onDeleteFileExecuteCallBackFunction = $function_name;
}
function primaryKeyNotAutoIncrement(){
$this->primaryKeyAutoIncrement = false;
}
//the forth optional param (permittedFileExts) was added in v8.9; it is an ARRAY of permitted file extensions allowed for upload; e.g. array("png", "jpg")
function setFileUpload($field_name, $destination_folder, $relative_folder = "", $permittedFileExts = ""){
//put values into array
$this->file_uploads[] = $field_name;
$this->file_upload_info[$field_name]['destination_folder'] = $destination_folder;
$this->file_upload_info[$field_name]['relative_folder'] = $relative_folder;
//added in v8.9
if (is_array($permittedFileExts)){
$this->file_upload_info[$field_name]['permittedFileExts'] = $permittedFileExts;
}
//the filenames that are saved are not editable
//$this->disallowEdit($field_name);
//have to add the row via POST now
$this->ajax_add = false;
}
function appendUploadFilename($append_field){
$this->filename_append_field = $append_field;
}
function omitPrimaryKey(){
//99% time it'll be in key 0, but just in case do search
$key = array_search($this->db_table_pk, $this->display_fields);
unset($this->display_fields[$key]);
}
function showCSVExportOption() {
$this->showCSVExport = true;
}
function modifyFieldWithClass($field, $class_name){
$this->display_field_with_class_style[$field] = $class_name;
}
function insertRowsReturned(){
$numRows = $this->getNumRows();
echo "<span class='" . $this->db_table . "_rowCount'>" . $numRows . "</span>";
}
function insertHeader($ajax_file = "ajaxCRUD.inc.php"){
global $headerAdded, $LOCAL_JS;
$headerAdded = TRUE;
if ($this->css_file == ''){
$this->css_file = 'default.css';
}
/* Load Javascript dependencies */
if ($this->includeJQuery){
//echo "<script type=\"text/javascript\" src=\"http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js\"></script>\n"; //rel 3.5 - using jquery instead of protoculous
//echo "<script type=\"text/javascript\" src=\"http://code.jquery.com/jquery-latest.min.js\"></script>\n"; //rel 6 - using latest version of jquery from jquery site (http://docs.jquery.com/Plugins/Validation/Validator)
if (isset($LOCAL_JS) && $LOCAL_JS) {
echo "<script type=\"text/javascript\" src=\"" . $this->ajaxcrud_root . "js/jquery.min.js\"></script>\n"; // EDITED 1/16/2012 - library on code.jquery site stopped working correctly!! (giving error TypeError: $.browser is undefined)
echo "<script type=\"text/javascript\" src=\"" . $this->ajaxcrud_root . "js/jquery.validate.min.js\"></script>\n"; //rel 6 - added ability to validate forms fields
echo "<script type=\"text/javascript\" src=\"" . $this->ajaxcrud_root . "js/jquery.maskedinput.js\"></script>\n"; //rel 6 - ability to mask fields (http://digitalbush.com/projects/masked-input-plugin/)
} else {
echo "<script type=\"text/javascript\" src=\"http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js\"></script>\n"; // EDITED 1/16/2012 - library on code.jquery site stopped working correctly!! (giving error TypeError: $.browser is undefined)
echo "<script type=\"text/javascript\" src=\"http://ajax.aspnetcdn.com/ajax/jquery.validate/1.7/jquery.validate.min.js\"></script>\n"; //rel 6 - added ability to validate forms fields
echo "<script type=\"text/javascript\" src=\"http://ajaxcrud.com/code/jquery.maskedinput.js\"></script>\n"; //rel 6 - ability to mask fields (http://digitalbush.com/projects/masked-input-plugin/)
}
echo "<script type=\"text/javascript\" src=\"" . $this->ajaxcrud_root . "js/validation.js\"></script>\n";
}
echo "<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\" />\n";
echo "<script type=\"text/javascript\" src=\"" . $this->ajaxcrud_root . "js/javascript_functions.js\"></script>\n";
echo "<link href=\"" . $this->ajaxcrud_root . "css/" . $this->css_file . "\" rel=\"stylesheet\" type=\"text/css\" media=\"screen\" />\n";
echo "
<script>\n
ajax_file = \"$this->ajax_file\"; \n
this_page = \"" . $_SERVER['REQUEST_URI'] . "\"\n
loading_image_html = \"$this->loading_image_html\"; \n
function validateAddForm(tableName, usePost){
var validator = $('#add_form_' + tableName).validate();
if (validator.form()){
if (!usePost){
setLoadingImage(tableName);
var fields = getFormValues(document.getElementById('add_form_' + tableName), '');
fields = fields + '&table=' + tableName;
var req = '" . $this->getThisPage() . "action=add&' + fields;
//validator.resetForm();
clearForm('add_form_' + tableName);
sndAddReq(req, tableName);
return false;
}
else{
//post the form normally (e.g. if using file uploads)
$('#add_form_' + tableName).submit();
}
}
return false;
}
$(document).ready(function(){
$(\"#add_form_{$this->db_table}\").validate();
});
</script>\n";
echo "
<style>
/* this will only work when your HTML doctype is in \"strict\" mode.
In other words - put this in your header:
<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
*/
.hand_cursor{
cursor: pointer; /* hand-shaped cursor */
cursor: hand; /* for IE 5.x */
}
.editable:hover, p.editable:hover{
background-color: #FFFF99;
}
</style>\n";
return true;
}
function disallowEdit($field){
$this->uneditable_fields[] = $field;
}
// Disallow Edit of all fields
function disallowEditAllFields(){
foreach($this->fields as $fldName)
$tblDemo->disallowEdit($fldName);
}
function disallowSort($field){
$this->unsortable_fields[] = $field;
}
function disallowDelete(){
$this->delete = false;
}
function disallowAdd(){
$this->add = false;
}
function addButton($value, $url, $tags = ""){
$this->bottom_button[] = array(0 => $value, 1 => $url, 2 => $tags);
}
function addButtonToRow($value, $url, $attach_params = "", $javascript_tags = "", $windowToOpen = "same"){
$this->row_button[] = array(0 => $value, 1 => $url, 2 => $attach_params, 3 => $javascript_tags, 4 => $windowToOpen);
}
function onAddSpecifyPrimaryKey(){
$this->on_add_specify_primary_key = true;
}
function doCRUDAction(){
if ($_REQUEST['action'] != ''){
$this->doAction($_REQUEST['action']);
}
}
function doAction($action){
global $error_msg;
global $report_msg;
$item = $this->item;
if ($action == 'delete' && $_REQUEST['id'] != ''){
$delete_id = $_REQUEST['id'];
$success = qr("DELETE FROM $this->db_table WHERE $this->db_table_pk = \"$delete_id\"");
if ($success){
$report_msg[] = "$item " . $this->deleteText . "d";
}
else{
$error_msg[] = "$item could not be deleted. Please try again.";
}
}//action = delete
#adding new item (via traditional way, non-ajax -- note: this is the ONLY way files can be uploaded with ajaxCRUD)
if ($action == 'add'){
//this if condition is so MULTIPLE ajaxCRUD tables can be used on the same page.
if ($_REQUEST['table'] == $this->db_table){
//for sql insert statement
$submitted_values = array();
//for callback function (if defined)
$submitted_array = array();
//this new row has (a) file(s) coming with it
$uploads_on = $_REQUEST['uploads_on'];
if ($uploads_on == 'true' && $_FILES){
$uploads_on = true;
}
$arrayKeyForFoundPK = array_search($this->db_table_pk, $this->fields);
foreach($this->fields as $field){
$submitted_value_cleansed = "";
if ($_REQUEST[$field] == ''){
if ($this->fieldIsInt($this->getFieldDataType($field)) || $this->fieldIsDecimal($this->getFieldDataType($field))){
$submitted_value_cleansed = 0;
}
}
else{
$submitted_value_cleansed = $_REQUEST[$field];
}
if ($uploads_on){
if ($this->fieldInArray($field, $this->file_uploads)){
$submitted_value_cleansed = $_FILES[$field]["name"];
}
}
$submitted_values[] = $submitted_value_cleansed;
//also used for callback function
$submitted_array[$field] = $submitted_value_cleansed;
}
//for adding values to the row which were not in the ADD row table - but are specified by ADD on INSERT
if (count($this->add_values) > 0){
foreach ($this->add_values as $add_value){
$field_name = $add_value[0];
$the_add_value = $add_value[1];
if ($submitted_array[$field_name] == ''){
$submitted_array[$field_name] = $the_add_value;
}