forked from pellcorp/opendb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
item_input.php
1650 lines (1347 loc) · 66.4 KB
/
item_input.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
/*
Open Media Collectors Database
Copyright (C) 2001,2013 by Jason Pell
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, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
// This must be first - includes config.php
require_once("./include/begin.inc.php");
include_once("./lib/database.php");
include_once("./lib/auth.php");
include_once("./lib/logging.php");
include_once("./lib/utils.php");
include_once("./lib/borrowed_item.php");
include_once("./lib/item.php");
include_once("./lib/http.php");
include_once("./lib/fileutils.php");
include_once("./lib/user.php");
include_once("./lib/review.php");
include_once("./lib/item_attribute.php");
include_once("./lib/item_type.php");
include_once("./lib/widgets.php");
include_once("./lib/parseutils.php");
include_once("./lib/site_plugin.php");
include_once("./lib/item_input.php");
include_once("./lib/item_display.php");
include_once("./lib/status_type.php");
include_once("./lib/TitleMask.class.php");
include_once("./lib/HTML_Listing.class.php");
function get_related_item_search_script() {
$script = "$(document).ready(function() {
$('#parent_item_loading').hide();
$('#parent_item_filter').change(function() {
$('#parent_item').prop('disabled', true);
$('#parent_item_loading').show();
var requestData = 'ajax_op=possible-parents&item_id=' + $('form [name=\"item_id\"]').val() + '&parent_item_filter=' + $('#parent_item_filter').val();
$.ajax({
type: 'post',
url: 'item_input.php',
dataType: 'json',
data: requestData
})
.done(function(data) {
$('#parent_item').html(data.select);
})
.always(function() {
$('#parent_item').prop('disabled', false);
$('#parent_item_loading').hide();
});
});
});
";
return "\n<script language=\"JavaScript\">\n<!-- // hide from stupid browsers\n" . $script . "\n// -->\n</script>\n";
}
function format_item_parents_select($HTTP_VARS, $item_r, $filter = null) {
$titleMaskCfg = new TitleMask ( 'item_listing' );
$possible_parents = fetch_available_item_parents($HTTP_VARS, $item_r, $filter, false);
// options will be in format ITEM_ID.INSTANCE_NO, so do not clash with existing parent_item_id / parent_instance_no stuff.
$parent_item_list = '<select name="parent_item" id="parent_item">';
if (!is_null($filter) && $filter != '%parent_only%') {
if (count($possible_parents) > 0) {
$parent_item_list .= '<option value="0">' . get_opendb_lang_var('none') . '</option>';
} else {
$parent_item_list .= '<option value="0">' . get_opendb_lang_var('nothing_found') . '</option>';
}
} else {
$parent_item_list .= '<option value="0">' . get_opendb_lang_var('none') . '</option>';
}
foreach ($possible_parents as $parent) {
if (!$parent['current_parent']) {
$parent ['title'] = $titleMaskCfg->expand_item_title ( $parent );
$parent_item_list .= '<option value="' . $parent['item_id'] . '_' . $parent['instance_no'] . '">'
. utf8_encode($parent['title']) . '</option>';
}
}
$parent_item_list .= '</select> <span id="parent_item_loading">' . get_opendb_lang_var('loading') . '...</span>';
return $parent_item_list;
}
/**
* Will test the old against the new value.
*
* Assumes that filter_item_input_field(...) has already
* been called for new_value.
*/
function is_value_refreshed($s_attribute_type, $new_value, $old_value) {
// Do the simplest check first!
if (!is_array($old_value) && strlen($old_value) == 0 && !is_array($new_value) && strlen($new_value) > 0) {
return TRUE;
} else {
// they should both be arrays at this point.
if (is_multivalue_attribute_type($s_attribute_type)) {
if (is_not_empty_array($old_value) && is_not_empty_array($new_value) && count($old_value) == count($new_value)) {
for ($i = 0; $i < count($old_value); $i++) {
// case insensitive search
if (array_search2($old_value[$i], $new_value, TRUE) === FALSE) {
return TRUE;
}
}
//else
return FALSE;
} else {
return TRUE;
}
} else {
if (is_array($new_value)) { // multi-value option automatically means a refreshed field
foreach ($new_value as $val) {
if (strcmp($val, $old_value) !== 0) {
return TRUE;
}
}
return FALSE;
}
if (strcmp($new_value, $old_value) !== 0) {
return TRUE;
} else {
return FALSE;
}
}
}
}
/**
* This function is responsible for grouping input fields that
* should appear grouped together. Any hidden fields, will be
* grouped together, so the array that gets returned will look like:
*
* array(hidden_fields=>array(
* $input_field_r, $input_field_r, ...),
* display_fields=>array(
* array(prompt=>'field group prompt',
* fieldset=>array(
* $input_field_r, $input_field_r, ....)
* )
* )
* )
*/
function get_site_type_input_fields($HTTP_VARS, $site_plugin_r, $item_r) {
global $titleMaskCfg;
$input_fields_rs = NULL;
$sifresults = fetch_site_plugin_input_field_rs($site_plugin_r['site_type']);
if ($sifresults) {
$display_field_r = NULL;
while ($input_field_r = db_fetch_assoc($sifresults)) {
// Only if we are are refreshing an item.
if (is_not_empty_array($item_r)) {
$titleMaskCfg->reset();
$value = $titleMaskCfg->expand_title($item_r, $input_field_r['refresh_mask']);
} else if ($input_field_r['field_type'] != 'hidden') {
$titleMaskCfg->reset();
// a kludge to support feature request #816240 - failure when adding an item should return previous search
$value = ifempty($titleMaskCfg->expand_title($HTTP_VARS, $input_field_r['refresh_mask']), $input_field_r['default_value']);
} else {
$value = $input_field_r['default_value'];
}
switch ($input_field_r['field_type']) {
case 'hidden':
$input_fields_rs[] = array(name => $input_field_r['field'], type => 'hidden', value => $value);
break;
default:
if (strlen($input_field_r['prompt']) > 0) {
if ($inrow) {
$input_fields_rs[] = $display_field_r;
$display_field_r = NULL;
$inrow = FALSE;
}
$display_field_r['prompt'] = $input_field_r['prompt'];
$display_field_r['type'] = 'fieldset';
$display_field_r['fieldset'][] = array(name => $input_field_r['field'], type => 'text', value => $value);
$inrow = TRUE;
} else {
if (!$inrow) {
$newrow = TRUE;
$input_fields_rs['fieldset'][] = $input_field_r;
}
}
break;
}//switch
}
db_free_result($sifresults);
if ($display_field_r != NULL) {
$input_fields_rs[] = $display_field_r;
}
}
return $input_fields_rs;
}
function get_site_plugin_rs($HTTP_VARS, $item_r = NULL) {
$site_plugin_rs = NULL;
// if $HTTP_VARS['s_item_type'] is null, all site plugins will be returned.
$results = fetch_site_plugin_rs($HTTP_VARS['s_item_type']);
if ($results) {
$ischecked = FALSE;
while ($site_plugin_r = db_fetch_assoc($results)) {
if (is_exists_site_plugin($site_plugin_r['site_type'])) {
$input_field_rs = get_site_type_input_fields($HTTP_VARS, $site_plugin_r, $item_r);
if (is_array($input_field_rs)) {
$site_plugin_r['input_fields'] = $input_field_rs;
if (strlen($HTTP_VARS['s_item_type']) == 0) {
$site_plugin_r['s_item_type'] = fetch_site_item_type_r($site_plugin_r['site_type']);
} else {
$site_plugin_r['s_item_type'][] = $HTTP_VARS['s_item_type'];
}
if (is_array($site_plugin_r['s_item_type'])) {
if (!$ischecked) {
$site_plugin_r['checked_ind'] = 'Y';
$ischecked = TRUE;
}
$site_plugin_rs[] = $site_plugin_r;
}
}
}
}
db_free_result($results);
}
return $site_plugin_rs;
}
function display_site_plugin_blocks($HTTP_VARS, $item_r = NULL) {
global $titleMaskCfg;
$site_plugin_rs = get_site_plugin_rs($HTTP_VARS, $item_r);
if (is_array($site_plugin_rs)) {
echo ("<div id=\"site-add-container\">");
echo ("<ul id=\"site-add-menu\">");
reset($site_plugin_rs);
$first = TRUE;
while (list(, $site_plugin_r) = each($site_plugin_rs)) {
echo ("<li" . ($first ? ' class="first activeTab"' : ' class=""') . " id=\"menu-" . $site_plugin_r['site_type'] . "\" onClick=\"return activateTab('" . $site_plugin_r['site_type'] . "', 'site-add-menu', 'site-add-content', 'activeTab', 'sitePlugin'); return false;\">"
. $site_plugin_r['title'] . "</li>");
if ($first)
$first = FALSE;
}
echo ("</ul>");
echo ("\n<div id=\"site-add-content\">");
reset($site_plugin_rs);
while (list(, $site_plugin_r) = each($site_plugin_rs)) {
echo ("\n<div class=\"sitePlugin" . ($site_plugin_r['checked_ind'] != 'Y' ? "Hidden" : "") . "\" id=\"" . $site_plugin_r['site_type'] . "\">");
$title = "<img src=\"./images/site/" . $site_plugin_r['image'] . "\" title=\"" . strip_tags($site_plugin_r['description']) . "\" alt=\"" . strip_tags($site_plugin_r['description']) . "\">";
echo ("<h3>" . $site_plugin_r['title'] . "</h3>");
echo theme_image("images/site/" . $site_plugin_r['image'], strip_tags($site_plugin_r['description']), "siteLogo");
echo ("<h4>" . $site_plugin_r['description'] . "</h4>");
echo ("\n<form action=\"item_input.php\" method=\"GET\">");
if (is_exists_item_type($HTTP_VARS['s_item_type'])) {
echo ("\n<input type=\"hidden\" name=\"s_item_type\" value=\"" . $HTTP_VARS['s_item_type'] . "\">");
} else {
echo ("\n<label for=\"" . $site_plugin_r['site_type'] . "-s_item_type\">" . get_opendb_lang_var('item_type') . "</label>");
echo (single_select("s_item_type", fetch_item_type_for_item_types_rs($site_plugin_r['s_item_type'], TRUE), "%value% - %display%", NULL, NULL, NULL, FALSE, $site_plugin_r['site_type'] . "-s_item_type"));
}
echo ("<input type=hidden name=\"site_type\" value=\"" . $site_plugin_r['site_type'] . "\">");
echo ("<input type=hidden name=\"owner_id\" value=\"" . $HTTP_VARS['owner_id'] . "\">");
echo ("<input type=hidden name=\"s_status_type\" value=\"" . $HTTP_VARS['s_status_type'] . "\">");
echo ("<input type=hidden name=\"item_id\" value=\"" . $HTTP_VARS['item_id'] . "\">");
echo ("<input type=hidden name=\"instance_no\" value=\"" . $HTTP_VARS['instance_no'] . "\">");
echo ("<input type=hidden name=\"parent_item_id\" value=\"" . $HTTP_VARS['parent_item_id'] . "\">");
echo ("<input type=hidden name=\"parent_instance_no\" value=\"" . $HTTP_VARS['parent_instance_no'] . "\">");
if (is_array($site_plugin_r['input_fields'])) {
while (list(, $input_field_r) = each($site_plugin_r['input_fields'])) {
if ($input_field_r['type'] == 'hidden') {
echo ("<input type=hidden name=\"" . $input_field_r['name'] . "\" value=\"" . htmlspecialchars($input_field_r['value']) . "\">");
} else {
echo ("\n<label for=\"" . $site_plugin_r['site_type'] . "-" . $field_r['name'] . "\">" . $input_field_r['prompt'] . "</label>");
while (list(, $field_r) = each($input_field_r['fieldset'])) {
echo ("<input id=\"" . $site_plugin_r['site_type'] . "-" . $field_r['name'] . "\" class=\"text\" type=\"text\" name=\"" . $field_r['name'] . "\" value=\"" . htmlspecialchars($field_r['value']) . "\">");
}
}
}
}
echo ("<input type=hidden name=\"op\" value=\"site-search\">");
echo ("<input class=\"submit\" type=submit value=\"" . get_opendb_lang_var('site_search', 'site', $site_plugin_r['title']) . "\">");
echo ("</form>");
echo ("\n</div>\n");
}
echo ("</div>");
}//if($results)
// add manual entry record
if (!is_array($item_r)) {
echo ("<div id=\"site-add-manual\">");
echo ("<h3>" . get_opendb_lang_var('manual_entry') . "</h3>");
echo ("\n<form action=\"item_input.php\" method=\"GET\">");
echo ("<input type=\"hidden\" name=\"owner_id\" value=\"" . $HTTP_VARS['owner_id'] . "\">");
echo ("<input type=\"hidden\" name=\"s_status_type\" value=\"" . $HTTP_VARS['s_status_type'] . "\">");
echo ("<input type=\"hidden\" name=\"item_id\" value=\"" . $HTTP_VARS['item_id'] . "\">");
echo ("<input type=\"hidden\" name=\"instance_no\" value=\"" . $HTTP_VARS['instance_no'] . "\">");
echo ("<input type=\"hidden\" name=\"parent_item_id\" value=\"" . $HTTP_VARS['parent_item_id'] . "\">");
echo ("<input type=\"hidden\" name=\"parent_instance_no\" value=\"" . $HTTP_VARS['parent_instance_no'] . "\">");
if (is_exists_item_type($HTTP_VARS['s_item_type'])) {
echo ("\n<input type=\"hidden\" name=\"s_item_type\" value=\"" . $HTTP_VARS['s_item_type'] . "\">");
} else {
echo ("\n<label for=\"manual-s_item_type\">" . get_opendb_lang_var('item_type') . "</label>");
echo (single_select("s_item_type", fetch_item_type_rs(TRUE), "%value% - %display%", NULL, NULL, NULL, FALSE, 'manual-s_item_type'));
}
echo "\n<input type=\"hidden\" name=\"op\" value=\"new\">";
echo ("<input type=\"submit\" class=\"submit\" value=\"" . get_opendb_lang_var('submit') . "\">");
echo ("</form>");
echo ("</div>");
}
return (is_array($site_plugin_rs) || !is_array($item_r));
}
function handle_site_add_or_refresh($item_r, $status_type_r, &$HTTP_VARS, &$footer_links_r) {
global $PHP_SELF;
global $titleMaskCfg;
if (!is_array($item_r)) {
if ($HTTP_VARS['confirmed'] === 'false' || strlen($HTTP_VARS['owner_id']) == 0) {
$page_title = get_opendb_lang_var('add_new_item');
echo _theme_header($page_title);
if (is_exists_item_type($HTTP_VARS['s_item_type']))
echo ("<h2>" . $page_title . " " . get_item_image($HTTP_VARS['s_item_type']) . "</h2>");
else
echo ("<h2>" . $page_title . "</h2>");
echo ("<form action=\"$PHP_SELF\" method=\"GET\">" . get_url_fields($HTTP_VARS, array('confirmed' => 'true')));
$HTTP_VARS['owner_id'] = ifempty($HTTP_VARS['owner_id'], get_opendb_session_var('user_id'));
echo ("<h3>" . get_opendb_lang_var('what_owner_add_new_item') . "</h3>");
echo ("\n<select name=\"owner_id\">" . custom_select('owner_id', fetch_user_rs(PERM_ITEM_OWNER), '%fullname% (%user_id%)', 'NA', $HTTP_VARS['owner_id'], 'user_id') . "\n</select>");
echo ("<input type=\"submit\" class=\"submit\" value=\"" . get_opendb_lang_var('add_new_item') . "\">");
echo ("</form>");
} else { //if($show_owner_field || $show_status_type_field)
if ($HTTP_VARS['owner_id'] != get_opendb_session_var('user_id'))
$page_title = get_opendb_lang_var('add_new_item_for_name', array('user_id' => $HTTP_VARS['owner_id'], 'fullname' => fetch_user_name($HTTP_VARS['owner_id'])));
else
$page_title = get_opendb_lang_var('add_new_item');
echo _theme_header($page_title);
if (is_exists_item_type($HTTP_VARS['s_item_type']))
echo ("<h2>" . $page_title . " " . get_item_image($HTTP_VARS['s_item_type']) . "</h2>");
else
echo ("<h2>" . $page_title . "</h2>");
if (!display_site_plugin_blocks($HTTP_VARS, $item_r)) {
echo ("<p class=\"error\">" . get_opendb_lang_var('operation_not_available') . "</p>");
}
}
} else if (is_not_empty_array($item_r)) {
$HTTP_VARS['s_item_type'] = $item_r['s_item_type'];
$page_title = get_opendb_lang_var('refresh_title', array('display_title' => $titleMaskCfg->expand_item_title($item_r)));
echo _theme_header($page_title);
echo ("<h2>" . $page_title . " " . get_item_image($item_r['s_item_type']) . "</h2>\n");
if (!display_site_plugin_blocks($HTTP_VARS, $item_r)) {
echo ("<p class=\"error\">" . get_opendb_lang_var('operation_not_available') . "</p>");
}
}
if (is_not_empty_array($item_r)) {
$footer_links_r[] = array(url => "item_display.php?item_id=" . $item_r['item_id'] . "&instance_no=" . $item_r['instance_no'], text => get_opendb_lang_var('back_to_item'));
}
}
/**
Bypass get_edit_form, by choosing defaults for all data provided by site plugin, supports site / refresh operations
*/
function get_site_item_input_data($op, $item_r, $HTTP_VARS) {
$results = fetch_item_attribute_type_rs($item_r['s_item_type'], 'not_instance_field_types');
if ($results) {
while ($item_attribute_type_r = db_fetch_assoc($results)) {
$fieldname = get_field_name($item_attribute_type_r['s_attribute_type'], $item_attribute_type_r['order_no']);
if (isset($HTTP_VARS[$fieldname])) {
if (is_array($HTTP_VARS[$fieldname])) {
if (is_multivalue_attribute_type($item_attribute_type_r['s_attribute_type'])) {
$value = $HTTP_VARS[$fieldname];
} else {
$value = $HTTP_VARS[$fieldname][0];
}
} else {
$value = $HTTP_VARS[$fieldname];
}
unset($HTTP_VARS[$fieldname]);
if ($item_attribute_type_r['s_field_type'] == 'TITLE') {
$fieldname = 'title';
}
$HTTP_VARS[$fieldname] = $value;
} else if ($op == 'refresh') {
if ($item_attribute_type_r['s_field_type'] == 'TITLE') {
$fieldname = 'title';
$HTTP_VARS[$fieldname] = $item_r['title'];
} else if (is_multivalue_attribute_type($item_attribute_type_r['s_attribute_type']))
$HTTP_VARS[$fieldname] = fetch_attribute_val_r($item_r['item_id'], $item_r['instance_no'], $item_attribute_type_r['s_attribute_type'], $item_attribute_type_r['order_no']);
else
$HTTP_VARS[$fieldname] = fetch_attribute_val($item_r['item_id'], $item_r['instance_no'], $item_attribute_type_r['s_attribute_type'], $item_attribute_type_r['order_no']);
}
}
db_free_result($results);
}
return $HTTP_VARS;
}
// This function will calculate a field input_field value,
// based on $HTTP_VARS, $op and $indexes (Used for site operations)
function get_field_value($op, $item_r, $s_attribute_type, $order_no, $s_field_type, $attribute_val, &$HTTP_VARS) {
if (is_not_empty_array($HTTP_VARS)) { // Lets try to get field value, from HTTP
$fieldname = get_field_name($s_attribute_type, $order_no);
// refresh operation!
if (!is_array($HTTP_VARS[$fieldname])) {
if (preg_match("/new([0-9]+)/", $HTTP_VARS[$fieldname], $matches) && isset($HTTP_VARS[$fieldname . '_' . $matches[0]])) {
$HTTP_VARS[$fieldname] = $HTTP_VARS[$fieldname . '_' . $matches[0]];
} else if ($HTTP_VARS[$fieldname] == 'old') {
// make sure this is a refresh value and not just a field with the value 'old'
if (isset($HTTP_VARS[$fieldname . '_new0'])) {
// Using $item_r value instead.
unset($HTTP_VARS[$fieldname]);
}
}
}
// this is a kludge for when a new fails
if (!isset($HTTP_VARS[$fieldname]) && $s_field_type == 'TITLE') {
$fieldname = 'title';
}
// If $HTTP_VARS[$fieldname] is set, we have probably come back to
// edit form, after a failed insert or update.
if (isset($HTTP_VARS[$fieldname])) {
// Is it an upload operation - There is not much we can do in the case of $_FILES, as they
// cannot be cached and passed into next request. A user would have to re-upload the file.
if (is_array($_FILES) && is_array($_FILES[$fieldname]))
return NULL;
else
// normal field
return $HTTP_VARS[$fieldname];
} else if ($op == 'new') {
return get_field_default_value($s_attribute_type);
} else if ($op == 'site' && !isset($HTTP_VARS[$fieldname])) {
return get_field_default_value($s_attribute_type);
}
}
return get_old_field_value($item_r, $s_field_type, $attribute_val);
}
function get_old_field_value($item_r, $s_field_type, $attribute_val) {
if ($s_field_type == 'STATUSTYPE') {
return $item_r['s_status_type'];
} else if ($s_field_type == 'STATUSCMNT') {
return $item_r['status_comment'];
} else if ($s_field_type == 'DURATION') {
return $item_r['borrow_duration'];
} else if ($s_field_type == 'TITLE') {
return $item_r['title'];
} else {
return $attribute_val;
}
}
function get_field_default_value($s_attribute_type) {
$attribute_type_r = fetch_attribute_type_r($s_attribute_type);
$value = NULL;
switch ($attribute_type_r['input_type']) {
case 'text':
$value = $attribute_type_r['input_type_arg3'];
break;
case 'number':
$value = $attribute_type_r['input_type_arg2'];
break;
case 'filtered':
$value = $attribute_type_r['input_type_arg4'];
break;
case 'checkbox':
if ($attribute_type_r['input_type_arg3'] == $attribute_type_r['input_type_arg1']
|| $attribute_type_r['input_type_arg3'] == $attribute_type_r['input_type_arg2']) {
$value = $attribute_type_r['input_type_arg3'];
}
break;
}
return $value;
}
// If $old_value !== FALSE, then we will assume a refresh operation and display a
// special refresh row, if the $new_value is different from the $old_value.
function get_item_form_row($op, $item_r, $item_attribute_type_r, $old_value, $new_value) {
if ($item_attribute_type_r['s_field_type'] == 'TITLE') {
$fieldname = 'title';
} else {
$fieldname = get_field_name($item_attribute_type_r['s_attribute_type'], $item_attribute_type_r['order_no']);
}
$is_multi_value = is_multivalue_attribute_type($item_attribute_type_r['s_attribute_type']);
$refresh_field = FALSE;
// Hidden cannot be involved in a refresh operation directly, but refreshed hidden fields, will still be updated.
if (strcasecmp($item_attribute_type_r['input_type'], 'hidden') !== 0 && $old_value !== FALSE && (is_not_empty_array($new_value) || (!is_array($new_value) && strlen($new_value) > 0)) && is_value_refreshed($item_attribute_type_r['s_attribute_type'], $new_value, $old_value)
&&
// Do not display 'Old' & 'New' options, if there was NO previous Old value (but $old_field is not FALSE!!!)
(is_not_empty_array($old_value) || (!is_array($old_value) && strlen($old_value) > 0))) {
$refresh_field = TRUE;
$new_value = get_array_for_value($new_value);
}
if (($refresh_field && count($new_value) > 0) || (is_array($new_value) && !$is_multi_value)) {
// -------------
// REFRESH FIELD
// -------------
// If we are doing a complete refresh block, and display_type is set to
// hidden, overwrite to display it.
if ($item_attribute_type_r['display_type'] == 'hidden') {
$item_attribute_type_r['display_type'] = 'display(%value%)';
$item_attribute_type_r['display_type_arg1'] = '%value%';
}
$field .= "<ul class=\"tabMenu\" id=\"${fieldname}-tab-menu\">";
if (!$is_multi_value) {
$new_value = deduplicate_array($new_value, $old_value);
$count = count($new_value) - 1;
} else {
$count = 0;
}
$key = array_keys($new_value);
for ($i = 0; $i <= $count; $i++) {
is_numeric($key[$i]) ? $label_description = "" : $label_description = " : ${key[$i]}";
$field .= "<li id=\"menu-${fieldname}_new${i}\"" . ($i == 0 ? " class=\"first activeTab\"" : "") . ">
<label for=\"menu-${fieldname}_new${i}-cbox\">" . $item_attribute_type_r['prompt'] . "$label_description</label>" . "<input type=\"radio\" class=\"radio\" name=\"" . $fieldname
. "\" id=\"menu-${fieldname}_new${i}-cbox\" value=\"new${i}\" onclick=\"return activateTab('${fieldname}_new${i}', '${fieldname}-tab-menu', '${fieldname}-tab-content');\"" . ($i == 0 ? " CHECKED" : "") . "></li>";
}
if ($refresh_field) {
$field .= "<li id=\"menu-${fieldname}_old\" >
<label for=\"menu-${fieldname}_old-cbox\">" . get_opendb_lang_var('old_prompt', 'prompt', $item_attribute_type_r['prompt']) . "</label>" . "<input type=\"radio\" class=\"radio\" id=\"menu-${fieldname}_old-cbox\" name=\"" . $fieldname
. "\" value=\"old\" onclick=\"return activateTab('${fieldname}_old', '${fieldname}-tab-menu', '${fieldname}-tab-content');\"></li>";
}
$field .= "</ul>";
$field .= "<div class=\"tabContentContainer\" id=\"${fieldname}-tab-content\">";
for ($i = 0; $i <= $count; $i++) {
if ($is_multi_value) {
$value = $new_value;
} else {
$value = $new_value[$key[$i]];
}
$field .= "<div class=\"tabContent" . ($i > 0 ? "Hidden" : "") . "\" id=\"${fieldname}_new${i}\">" . get_item_input_field($fieldname . "_new${i}", $item_attribute_type_r, $item_r, $value, FALSE) . "</div>";
}
if ($refresh_field) {
$field .= "<div class=\"tabContentHidden\" id=\"${fieldname}_old\">" . get_item_input_field($fieldname . "_old", $item_attribute_type_r, $item_r, $old_value, FALSE) . "</div>";
}
$field .= "</div>";
$prompt_mask = NULL;
if ($refresh_field) {
$prompt_mask = theme_image('rs.gif', get_opendb_lang_var('refreshed')) . "%prompt%";
}
return format_item_data_field($item_attribute_type_r, $field, $prompt_mask, NULL); // field mask
} else { // not a choose attribute
if (is_multivalue_attribute_type($item_attribute_type_r['s_attribute_type'])) {
// if new_value is empty!
if (is_empty_or_not_array($new_value)) {
if ($old_value !== FALSE)
$value = $old_value;
else
$value = NULL;
} else {
$value = &$new_value;
}
return get_item_input_field($fieldname, $item_attribute_type_r, $item_r, $value);
} else {
$value = ifempty($new_value, $old_value === FALSE ? NULL : $old_value);
// If this is an edit operation - the value must be NOT NULL
// for some widgets to work properly.
if ($op != 'new' && $op != 'site' && $value === NULL) {
$value = '';
}
return get_item_input_field($fieldname, $item_attribute_type_r, $item_r, $value);
}
}
}
function get_edit_item_form($op, $item_r, $HTTP_VARS, &$upload_file_fields) {
// is at least one field a compulsory field?
$compulsory_fields = FALSE;
$upload_file_fields = FALSE;
$results = fetch_item_attribute_type_rs($item_r['s_item_type'], 'not_instance_field_types');
if ($results) {
$formContents .= "\n<table>";
while ($item_attribute_type_r = db_fetch_assoc($results)) {
if ($item_attribute_type_r['s_field_type'] == 'ITEM_ID') {
continue;
}
// need to provide a proper encoded form if upload encountered, assume upload where file attribute encountered
if ($item_attribute_type_r['file_attribute_ind'] == 'Y') {
$upload_file_fields = TRUE;
}
if ($op == 'refresh' || $op == 'edit' || $op == 'newinstance') {
if (is_multivalue_attribute_type($item_attribute_type_r['s_attribute_type']))
$item_attribute_type_r['attribute_val'] = fetch_attribute_val_r($item_r['item_id'], $item_r['instance_no'], $item_attribute_type_r['s_attribute_type'], $item_attribute_type_r['order_no']);
else
$item_attribute_type_r['attribute_val'] = fetch_attribute_val($item_r['item_id'], $item_r['instance_no'], $item_attribute_type_r['s_attribute_type'], $item_attribute_type_r['order_no']);
}
if ($op == 'refresh') {
$old_value = get_old_field_value($item_r, $item_attribute_type_r['s_field_type'], $item_attribute_type_r['attribute_val']);
$new_value = get_field_value($op, $item_r, $item_attribute_type_r['s_attribute_type'], $item_attribute_type_r['order_no'], $item_attribute_type_r['s_field_type'], $item_attribute_type_r['attribute_val'], $HTTP_VARS);
// this represents multiple selections for a single value, lookup / multi value attributes will
// be dealt with together.
if (is_not_empty_array($new_value) && !is_multivalue_attribute_type($item_attribute_type_r['s_attribute_type'])) {
foreach ($new_value as $key => $val) {
$new_value[$key] = filter_item_input_field($item_attribute_type_r, $val);
}
} else {
$new_value = filter_item_input_field($item_attribute_type_r, $new_value);
// If no HTTP value, especially where FILE UPLOAD is being concerned, attempt to get from database again.
if (($op == 'edit' || $op == 'newinstance') && $new_value === NULL) {
$new_value = get_old_field_value($item_r, $item_attribute_type_r['s_field_type'], $item_attribute_type_r['attribute_val']);
}
}
} else { //if($op == 'refresh')
$old_value = FALSE;
$new_value = get_field_value($op, $item_r, $item_attribute_type_r['s_attribute_type'], $item_attribute_type_r['order_no'], $item_attribute_type_r['s_field_type'], $item_attribute_type_r['attribute_val'], $HTTP_VARS);
if (is_not_empty_array($new_value) && !is_multivalue_attribute_type($item_attribute_type_r['s_attribute_type'])) {
foreach ($new_value as $key => $val) {
$new_value[$key] = filter_item_input_field($item_attribute_type_r, $val);
}
} else {
$new_value = filter_item_input_field($item_attribute_type_r, $new_value);
// If no HTTP value, especially where FILE UPLOAD is being concerned, attempt to get from database again.
if (($op == 'edit' || $op == 'newinstance') && $new_value === NULL) {
$new_value = get_old_field_value($item_r, $item_attribute_type_r['s_field_type'], $item_attribute_type_r['attribute_val']);
}
}
}
// Enforce compulsory indicator for TITLE.
if ($item_attribute_type_r['s_field_type'] == 'TITLE') {
$item_attribute_type_r['compulsory_ind'] = 'Y';
$override_title_articles_r = get_opendb_config_var('item_input', 'title_articles');
if (is_not_empty_array($override_title_articles_r)) {
if (is_not_empty_array($new_value)) {
foreach ($new_value as $key => $val) {
$new_value[$key] = trim(format_title_grammar_article($val, $override_title_articles_r));
}
} else {
$new_value = trim(format_title_grammar_article($new_value, $override_title_articles_r));
}
}
}
$formContents .= get_item_form_row($op, $item_r, $item_attribute_type_r, $old_value, $new_value);
if ($item_attribute_type_r['compulsory_ind'] == 'Y') {
$compulsory_fields = TRUE;
}
}//while
db_free_result($results);
$formContents .= "\n</table>";
$help_block_r = NULL;
if ($op == 'refresh') {
$help_block_r[] = array('img' => 'rs.gif', 'text' => get_opendb_lang_var('refreshed'));
}
$help_block_r[] = array('img' => 'compulsory.gif', 'text' => get_opendb_lang_var('compulsory_field'), id => 'compulsory');
$formContents .= format_help_block($help_block_r);
return $formContents;
} else {
return FALSE;
}
}
function get_edit_item_instance_form($op, $item_r, $status_type_r, $HTTP_VARS) {
$formContents = "<div class=\"tabContentHidden\" id=\"instance_info\">";
$formContents .= get_related_item_search_script();
$results = fetch_item_attribute_type_rs($item_r['s_item_type'], 'instance_field_types');
if ($results) {
$formContents .= "<h3>" . get_opendb_lang_var('instance_info') . "</h3>";
$formContents .= "\n<table>";
if (($op == 'edit' || $op == 'refresh') && $status_type_r['change_owner_ind'] == 'Y') {
$formContents .= format_field(get_opendb_lang_var('owner'),
"\n<select name=\"owner_id\">"
. custom_select('owner_id', fetch_user_rs(PERM_ITEM_OWNER, INCLUDE_ROLE_PERMISSIONS, INCLUDE_CURRENT_USER, EXCLUDE_DEACTIVATED_USER, 'user_id', 'ASC'), '%fullname% (%user_id%)', 'NA', ifempty($HTTP_VARS['owner_id'], $item_r['owner_id']), 'user_id') . "\n</select>");
}
if ($op == 'newinstance' || $op == 'clone_item') {
$item_r['s_status_type'] = NULL;
$item_r['status_comment'] = NULL;
$item_r['borrow_duration'] = NULL;
}
while ($item_attribute_type_r = db_fetch_assoc($results)) {
if ($item_attribute_type_r['s_field_type'] == 'STATUSTYPE') {
$status_type = ifempty(filter_item_input_field($item_attribute_type_r, $HTTP_VARS['s_status_type']), $item_r['s_status_type']);
if ($op == 'new' || $op == 'site' || $op == 'newinstance' || $op == 'clone_item') {
$lookup_results = fetch_newitem_status_type_rs();
} else {
// If item has borrowed records, then no s_status_type with borrow_ind == 'X' should be included.
$lookup_results = fetch_update_status_type_rs($status_type);
}
if ($lookup_results && db_num_rows($lookup_results) > 0) {
$formContents .= format_field($item_attribute_type_r['prompt'], radio_grid('s_status_type', $lookup_results, '%img%', // mask
'VERTICAL', $status_type)); // value
}
} else if ($item_attribute_type_r['s_field_type'] == 'STATUSCMNT') {
$status_comment = ifempty(filter_item_input_field($item_attribute_type_r, $HTTP_VARS['status_comment']), $item_r['status_comment']);
$formContents .= get_item_input_field('status_comment', $item_attribute_type_r, NULL, //$item_r
$status_comment);
} else if ($item_attribute_type_r['s_field_type'] == 'DURATION') {
$borrow_duration = ifempty(filter_item_input_field($item_attribute_type_r, $HTTP_VARS['borrow_duration']), $item_r['borrow_duration']);
// The S_DURATION lookup list will most likely include an 'Undefined' option, that equates
// to an empty string. So for Updates, we want to allow for a match, by forcing any NULL
// value to a empty string. The reason why we do this, is because the Borrow Duration was
// probably set to 'Undefined', but because this equated to an empty string, the field was
// never updated.
if ($op != 'new' && $op != 'site') {
if ($borrow_duration === NULL)
$borrow_duration = '';
}
$formContents .= get_item_input_field('borrow_duration', $item_attribute_type_r, NULL, //$item_r
$borrow_duration);
}
}//while
db_free_result($results);
$formContents .= "\n</table>";
}
if (get_opendb_config_var('item_input', 'related_item_support') !== FALSE) {
$formContents .= "<h3>" . get_opendb_lang_var ( 'add_related_parent' ) . "</h3>";
$formContents .= "\n<table>";
$formContents .= format_field(get_opendb_lang_var('parent_item_filter'), '<input type="text" name="parent_item_filter" id="parent_item_filter">');
$formContents .= format_field(get_opendb_lang_var('parent_item'), format_item_parents_select($HTTP_VARS, $item_r, '%parent_only%'));
$formContents .= "\n</table>";
$relatedItems = get_related_items_listing ( $item_r, $HTTP_VARS, RELATED_PARENTS_MODE );
if ($relatedItems != NULL) {
$formContents .= "<h3>" . get_opendb_lang_var('related_parent_item(s)') . "</h3>";
$formContents .= $relatedItems;
}
}
$formContents .= "</div>";
return $formContents;
}
function get_edit_form($op, $item_r, $status_type_r, $HTTP_VARS) {
global $PHP_SELF;
// Work out $op value to submit.
if ($op == 'edit' || $op == 'refresh' || $op == 'newinstance')
$op2 = 'update';
else if ($op == 'new' || $op == 'site' || $op == 'clone_item')
$op2 = 'insert';
else
$op2 = $op; // last resort!
$formContents = get_edit_item_form($op, $item_r, $HTTP_VARS, $upload_file_fields);
if ($formContents !== FALSE) {
$pageContents = '';
$pageContents .= "<div class=\"tabContainer\">";
if ($upload_file_fields && is_file_upload_enabled()) {
$pageContents .= "\n<form name=\"itemInput\" action=\"$PHP_SELF\" method=\"POST\" enctype=\"multipart/form-data\">";
} else {
$pageContents .= "\n<form action=\"$PHP_SELF\" method=\"POST\">";
}
if (get_opendb_config_var('widgets', 'enable_javascript_validation') !== FALSE)
$onclick_event = "if(!checkForm(this.form)){return false;}else{this.form.submit();}";
else
$onclick_event = "this.form.submit();";
$pageContents .= "<ul class=\"tabMenu\" id=\"tab-menu\">";
$pageContents .= "<li id=\"menu-details\" class=\"first activeTab\" onclick=\"return activateTab('details');\">" . get_opendb_lang_var('details') . "</li>";
$pageContents .= "<li id=\"menu-instance_info\" onclick=\"return activateTab('instance_info');\">" . get_opendb_lang_var('instance_info') . "</li>";
$pageContents .= "</ul>";
$pageContents .= "<div id=\"tab-content\">";
$pageContents .= "<ul class=\"saveButtons\">
<li><input type=\"button\" class=\"button\" onclick=\"$onclick_event\" value=\"" . get_opendb_lang_var('save_item') . "\"></li>
</ul>";
$pageContents .= "<div class=\"tabContent\" id=\"details\">";
$pageContents .= "\n<input type=\"hidden\" name=\"op\" value=\"$op2\">";
$pageContents .= "\n<input type=\"hidden\" name=\"start-op\" value=\"$op\">";
$pageContents .= "\n<input type=\"hidden\" name=\"s_item_type\" value=\"" . $item_r['s_item_type'] . "\">";
$pageContents .= "\n<input type=\"hidden\" name=\"parent_item_id\" value=\"" . $HTTP_VARS['parent_item_id'] . "\">";
$pageContents .= "\n<input type=\"hidden\" name=\"parent_instance_no\" value=\"" . $HTTP_VARS['parent_instance_no'] . "\">";
if ($op == 'clone_item' || is_not_empty_array($item_r)) {
if (is_numeric($item_r['item_id']))
$pageContents .= "\n<input type=\"hidden\" name=\"item_id\" value=\"" . $item_r['item_id'] . "\">";
if (is_numeric($item_r['instance_no']))
$pageContents .= "\n<input type=\"hidden\" name=\"instance_no\" value=\"" . $item_r['instance_no'] . "\">";
}
if (strlen($HTTP_VARS['owner_id']) > 0) {
$pageContents .= "\n<input type=\"hidden\" name=\"owner_id\" value=\"" . $HTTP_VARS['owner_id'] . "\">";
}
$pageContents .= $formContents;
$action_links_rs = NULL;
if (is_not_empty_array($action_links_rs)) {
$pageContents .= format_footer_links($action_links_rs);
}
$pageContents .= "</div>";
$pageContents .= get_edit_item_instance_form($op, $item_r, $status_type_r, $HTTP_VARS);
$pageContents .= "</div>";
$pageContents .= "</form>";
$pageContents .= "</div>";
return $pageContents;
} else {
return FALSE;
}
}
function handle_edit_or_refresh($op, $item_r, $status_type_r, $HTTP_VARS, &$errors) {
if ((is_user_granted_permission(PERM_ITEM_OWNER) && $item_r['owner_id'] == get_opendb_session_var('user_id')) || is_user_granted_permission(PERM_ITEM_ADMIN)) {
$formContents = get_edit_form($op, $item_r, $status_type_r, $HTTP_VARS);
if ($formContents != FALSE)
return $formContents;
else {
$errors = array('error' => get_opendb_lang_var('undefined_error'), detail => '');
return FALSE;
}
} else {
$errors = array('error' => get_opendb_lang_var('cannot_edit_item_not_owned'), detail => '');
opendb_logger(OPENDB_LOG_WARN, __FILE__, __FUNCTION__, 'User attempted to edit item instance they do not own', $item_r);
return FALSE;
}
}
function handle_new_or_site($op, $item_r, $status_type_r, $HTTP_VARS, &$errors) {
if ((is_user_granted_permission(PERM_ITEM_OWNER) && $item_r['owner_id'] == get_opendb_session_var('user_id')) || is_user_granted_permission(PERM_ITEM_ADMIN)) {
if (is_valid_item_type_structure($item_r['s_item_type'])) {
$formContents = get_edit_form($op, $item_r, $status_type_r, $HTTP_VARS);
if ($formContents != FALSE)
return $formContents;
else {
$errors = array('error' => get_opendb_lang_var('undefined_error'), detail => '');
return FALSE;
}
} else {
$errors = array('error' => get_opendb_lang_var('invalid_item_type_structure', 's_item_type', $item_r['s_item_type']), 'detail' => '');
return FALSE;
}
} else {
$errors = array('error' => get_opendb_lang_var('operation_not_available'));
opendb_logger(OPENDB_LOG_WARN, __FILE__, __FUNCTION__, 'User attempted to insert an item for another user', $item_r);
return FALSE;
}
}
function handle_site_search(&$sitePlugin, $HTTP_VARS, &$errors, &$footer_links_r) {
global $PHP_SELF;
$HTTP_VARS['op'] = 'site';
$formContents = '<div id="site-search">';
if ($sitePlugin->_queryListing($HTTP_VARS) !== FALSE) {
$searchQuery = $sitePlugin->getSearchQuery();
if (is_not_empty_array($searchQuery)) {
$formContents .= "<h3>" . get_opendb_lang_var('site_search_results', array('site_title' => $sitePlugin->getTitle())) . "</h3>";
$formContents .= '<div class="search-query"><dl>';
for ($i = 0; $i < count($searchQuery); $i++) {
if ($searchQuery[$i]['field_type'] != 'hidden') {
$formContents .= '<dt>' . $searchQuery[$i]['prompt'] . "</dt>";
$formContents .= '<dd>' . htmlspecialchars($searchQuery[$i]['value']) . '</dd>';
}
}
$formContents .= '</dl></div>';
}
if ($sitePlugin->getRowCount() > 0) {
// exact title match.
if ($sitePlugin->getRowCount() == 1 && $sitePlugin->isPreviousPage() === FALSE) {
// the site plugin process will have already queried for the itemData
// based on the single row returned.
return "__EXACT_TITLE_MATCH__";
} else {
$formContents .= "\n<table class=\"listing-table\">";
$class = 'oddRow';
for ($i = 0; $i < $sitePlugin->getRowCount(); $i++) {
$formContents .= "\n<tr class=\"$class\">";
$row_data_r = $sitePlugin->getRowData($i);
$file_r = file_cache_get_image_r($row_data_r['cover_image_url'], 'site-add');
if (is_not_empty_array($file_r)) {