forked from opendocman/opendocman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathudf_functions.php
654 lines (580 loc) · 29.1 KB
/
udf_functions.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
<?php
/*
udf_functions.php - adds user definced functions
Copyright (C) 2007 Stephen Lawrence Jr., Jonathan Miner
Copyright (C) 2008-2012 Stephen Lawrence Jr.
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 3
of the License, or 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.
*/
// User Defined Fields START
if ( !defined('udf_functions') )
{
define('udf_functions', 'true', false);
function udf_add_file_form()
{
$query = "SELECT table_name,field_type,display_name FROM {$GLOBALS['CONFIG']['db_prefix']}udf ORDER by id";
$result = mysql_query($query) or die ("Error in query32: $query. " . mysql_error());
while ($row = mysql_fetch_row($result))
{
echo '<tr><td>';
if (file_exists("udf_help.html"))
{
echo '<a class="body" href="udf_help.html#Add_File_'.$row[2].'" onClick="return popup(this,\'Help\')" style="text-decoration:none">'.$row[2].'</a>';
}
else
{
echo $row[2];
}
echo '</td><td>';
//Type is Select List
if ( $row[1] == 1 )
{
echo '<select name="'.$row[0].'">';
$query = "SELECT id,value FROM ".$row[0];
$subresult = mysql_query($query) or die ("Error in query52: $query. " . mysql_error());
while ($subrow = mysql_fetch_row($subresult))
{
echo '<option value="'.$subrow[0].'">'.$subrow[1].'</option>';
}
mysql_free_result($subresult);
echo '</select>';
}
// Type is Radio
if ( $row[1] == 2 )
{
$query = "SELECT id,value FROM ".$row[0];
$subresult = mysql_query($query) or die ("Error in query65: $query. " . mysql_error());
while ($subrow = mysql_fetch_row($subresult))
{
echo '<input type=radio name="'.$row[0].'" value="'.$subrow[0].'">'.$subrow[1];
}
mysql_free_result($subresult);
}
// Type is Text
if ( $row[1] == 3 )
{
echo '<input tabindex="5" type="Text" name="'.$row[0].'" size="16">';
}
//CHM
// Type is Sub-Select
if ( $row[1] == 4 )
{
$explode_row = explode('_',$row[0]);
$field_name = $explode_row[2];
$query = "SELECT * FROM ".$row[0];
$subresult = mysql_query($query) or die ("Error in query65: $query. " . mysql_error());
echo '<select name="'.$row[0].'" onchange="showdropdowns(this.value, \'add\',\'' . $field_name . '\')">';
echo '<option value="">Please select</option>';
while ($subrow = mysql_fetch_row($subresult))
{
echo '<option value="'.$subrow[0].'">'.$subrow[1].'</option>';
}
echo '</select>';
echo '<div id="txtHint'.$field_name.'">Secondary items will show up here.</div>';
mysql_free_result($subresult);
}
//CHM
echo '</td></tr>';
}
mysql_free_result($result);
}
function udf_add_file_insert($fileId)
{
$query = "SELECT table_name,field_type FROM {$GLOBALS['CONFIG']['db_prefix']}udf ORDER BY id";
$result = mysql_query($query) or die("Error in query86: $query . " . mysql_error());
$i = 0; //CHM
while ($row = mysql_fetch_row($result)) {
if ($row[1] == 1 || $row[1] == 2 || $row[1] == 3 || $row[1] == 4) { //CHM
if (isset($_REQUEST[$row[0]]) && $_REQUEST[$row[0]] != "") {
$explode_row = explode('_', $row[0]);
$field_name = $explode_row[2];
$query = "UPDATE {$GLOBALS['CONFIG']['db_prefix']}data SET `{$row['0']}` = '{$_REQUEST[$row['0']]}' WHERE id = '$fileId'";
mysql_query($query) or die("Error in query94: $query. " . mysql_error());
//CHM
if (isset($_REQUEST['secondary' . $i]) && $_REQUEST['secondary' . $i] != '' && $row[1] == 4) {
$query = "UPDATE {$GLOBALS['CONFIG']['db_prefix']}data SET odm_udftbl_{$field_name}_secondary = '{$_REQUEST['secondary' . $i]}' WHERE id = '$fileId'";
mysql_query($query) or die("Error in query94: $query. " . mysql_error());
$i++;
}
//CHM
}
}
}
mysql_free_result($result);
}
function udf_edit_file_form()
{
$query = "SELECT display_name,field_type,table_name FROM {$GLOBALS['CONFIG']['db_prefix']}udf ORDER BY id";
$result = mysql_query($query) or die ("Error in query104: $query. " . mysql_error());
while ($row = mysql_fetch_row($result))
{
if ( $row[1] == 1 || $row[1] == 2)
{
echo '<tr><td>' . $row[0] . '</td><td>';
if ( $row[1] == 1 )
{
echo '<select name="'.$row[2].'">';
}
$query = "SELECT {$row['2']} FROM {$GLOBALS['CONFIG']['db_prefix']}data WHERE id = '{$_REQUEST['id']}'";
$subresult = mysql_query($query) or die ("Error in query116: $query. " . mysql_error());
$subrow = mysql_fetch_row($subresult);
$sel = $subrow[0];
mysql_free_result($subresult);
$query = 'SELECT id, value FROM ' . $row[2];
$subresult = mysql_query($query) or die ("Error in query122: $query. " . mysql_error());
while ($subrow = mysql_fetch_row($subresult))
{
if ( $row[1] == 1 )
{
echo '<option value="' . $subrow[0] . '"';
if ( $sel == $subrow[0] )
{
echo ' selected';
}
echo '>' . $subrow[1] . '</option>';
}
elseif ($row[1] == 2)
{
echo '<input type=radio name="' . $row[2] . '" value="' . $subrow[0] . '"';
if ($sel == $subrow[0])
echo ' checked';
echo '>' . $subrow[1];
}
}
mysql_free_result($subresult);
if ($row[1] == 1)
echo '</select>';
echo '</td></tr>';
}
elseif ($row[1] == 3)
{
echo '<tr><td>' . $row[0] . '</td><td>';
$query = "SELECT {$row['2']} FROM {$GLOBALS['CONFIG']['db_prefix']}data WHERE id = '{$_REQUEST['id']}'";
$subresult = mysql_query($query) or die ("Error in query151: $query. " . mysql_error());
$subrow = mysql_fetch_row($subresult);
echo '<input type="text" name="' . $row[2] . '" size="50" value="' . $subrow[0] . '">';
mysql_free_result($subresult);
}
//CHM
elseif( $row[1] == 4)
{
$explode_row = explode('_',$row[2]);
$field_name = $explode_row[2];
echo '<tr><td>' . $row[0] . '</td><td>';
echo '<select name="'.$row[2].'" onchange="showdropdowns(this.value, \'edit\',\'' . $field_name . '\')">';
echo '<option value="">Please select one</option>';
$query = "SELECT {$row['2']} FROM {$GLOBALS['CONFIG']['db_prefix']}data WHERE id = '{$_REQUEST['id']}'";
$subresult = mysql_query($query) or die ("Error in query116: $query. " . mysql_error());
$subrow = mysql_fetch_row($subresult);
$sel_pri = $subrow[0];
mysql_free_result($subresult);
$query = 'SELECT id, value FROM ' . $row[2];
$subresult = mysql_query($query) or die ("Error in query117: $query. " . mysql_error());
while ($subrow = mysql_fetch_row($subresult))
{
if ( $row[1] == 4 )
{
echo '<option value="' . $subrow[0] . '"';
if ( $sel_pri == $subrow[0] )
{
echo ' selected';
}
echo '>' . $subrow[1] . '</option>';
}
}
echo '</select>';
echo '</td></tr>';
//secondary dropdown
echo '<tr><td> </td><td><div id="txtHint'.$field_name.'">';
$query = "SELECT odm_udftbl_{$field_name}_secondary FROM {$GLOBALS['CONFIG']['db_prefix']}data WHERE id = '{$_REQUEST['id']}'";
$subresult = mysql_query($query) or die ("Error in query116: $query. " . mysql_error());
$subrow = mysql_fetch_row($subresult);
$sel = $subrow[0];
mysql_free_result($subresult);
if($sel ==''){
echo 'Secondary items will show up here.';
}else{
$query = "SELECT id, value FROM odm_udftbl_{$field_name}_secondary WHERE pr_id = '{$sel_pri}'";
$subresult = mysql_query($query) or die ("Error in query123: $query. " . mysql_error());
echo '<select id="odm_udftbl_'.$field_name.'_secondary" name="odm_udftbl_'.$field_name.'_secondary">';
while ($subrow = mysql_fetch_row($subresult))
{
if ( $row[1] == 4 )
{
echo '<option value="' . $subrow[0] . '"';
if ( $sel == $subrow[0] )
{
echo ' selected';
}
echo '>' . $subrow[1] . '</option>';
}
}
}
echo '</select>';
echo '</div></td></tr>';
}
//CHM
}
mysql_free_result($result);
}
function udf_edit_file_update()
{
$query = "SELECT display_name,field_type,table_name FROM {$GLOBALS['CONFIG']['db_prefix']}udf ORDER BY id";
$result = mysql_query($query) or die ("Error in query163: $query. " . mysql_error());
$i=0; //CHM
while ($row = mysql_fetch_row($result))
{
if ( $row[1] == 1 || $row[1] == 2 || $row[1] == 3 || $row[1] == 4) //CHM sub select option 4 added
{
if ( isset($_REQUEST[$row[2]]) && $_REQUEST[$row[2]] != "" )
{
$query = "UPDATE {$GLOBALS['CONFIG']['db_prefix']}data SET `{$row['2']}`='{$_REQUEST[$row['2']]}' WHERE id = {$_REQUEST['id']}";
$subresult = mysql_query($query) or die ("Error in query171: $query. " . mysql_error());
//CHM secondary values
if($_REQUEST['secondary'.$i] != '' && $row[1] == 4){
$explode_row = explode('_',$row[2]);
$field_name = $explode_row[2];
$query = "UPDATE {$GLOBALS['CONFIG']['db_prefix']}data SET `odm_udftbl_{$field_name}_secondary`='{$_REQUEST['secondary'.$i]}' WHERE id = {$_REQUEST['id']}";
$subresult = mysql_query($query) or die ("Error in query171: $query. " . mysql_error());
$i++;
}
}
}
}
mysql_free_result($result);
}
function udf_details_display($fileId)
{
$query = "SELECT display_name,field_type,table_name FROM {$GLOBALS['CONFIG']['db_prefix']}udf ORDER BY id";
$result = mysql_query($query) or die ("Error in query181: $query. " . mysql_error());
while ($row = mysql_fetch_row($result))
{
if ( $row[1] == 1 || $row[1] == 2)
{
$query = "SELECT value FROM {$GLOBALS['CONFIG']['db_prefix']}data, {$row['2']} WHERE {$GLOBALS['CONFIG']['db_prefix']}data.id = $fileId AND {$GLOBALS['CONFIG']['db_prefix']}data.{$row['2']}={$row['2']}.id";
$subresult = mysql_query($query) or die ("Error in query187: $query. " . mysql_error());
if($subresult)
{
$subrow = mysql_fetch_row($subresult);
echo '<th valign=top align=right>' . $row[0] . ':</th><td>' . $subrow[0] . '</td></tr>';
mysql_free_result($subresult);
}
}
elseif ($row[1] == 3)
{
$query = "SELECT {$row[2]} FROM {$GLOBALS['CONFIG']['db_prefix']}data WHERE {$GLOBALS['CONFIG']['db_prefix']}data.id = $fileId ";
$subresult = mysql_query($query) or die ("Error in query198: $query. " . mysql_error());
if ($subresult)
{
$subrow = mysql_fetch_row($subresult);
echo '<th valign=top align=right>' . $row[0] . ':</th><td>' . $subrow[0] . '</td></tr>';
mysql_free_result($subresult);
}
}
//CHM
elseif ($row[1] == 4)
{
$query = "SELECT value FROM {$GLOBALS['CONFIG']['db_prefix']}data, {$row['2']} WHERE {$GLOBALS['CONFIG']['db_prefix']}data.id = $fileId AND {$GLOBALS['CONFIG']['db_prefix']}data.{$row['2']}={$row['2']}.id";
$subresult = mysql_query($query) or die ("Error in query199: $query. " . mysql_error());
if ($subresult)
{
$subrow = mysql_fetch_row($subresult);
echo '<th valign=top align=right>' . $row[0] . ':</th><td>' . $subrow[0] . '</td></tr>';
mysql_free_result($subresult);
}
}
//CHM
}
mysql_free_result($result);
}
function udf_admin_header()
{
echo '<th bgcolor ="#83a9f7"><font color="#FFFFFF">' .msg('label_user_defined_fields'). '</font></th>';
}
function udf_admin_menu($secureurl)
{
echo '<td valign=top><table border=0>';
echo '<tr><td><b><a href="'.$secureurl->encode('udf.php?submit=add&state=' . ($_REQUEST['state']+1)).'">' .msg('label_add'). '</a></b></td></tr>';
echo '<tr><td><b><a href="'.$secureurl->encode('udf.php?submit=deletepick&state=' . ($_REQUEST['state']+1)).'">' .msg('label_delete'). '</a></b></td></tr>';
echo '<tr><td><hr></td></tr>';
$query = "SELECT table_name,field_type,display_name FROM {$GLOBALS['CONFIG']['db_prefix']}udf ORDER BY id";
$result = mysql_query($query) or die ("Error in query223: $query. " . mysql_error());
while ($row = mysql_fetch_row($result))
{
echo '<tr><td><b><a href="'.$secureurl->encode('udf.php?submit=edit&udf='.$row[0].'&state=' . ($_REQUEST['state']+1)).'">'.$row[2].'</a></b></td></tr>';
}
mysql_free_result($result);
echo '</table></td>';
}
function udf_functions_java_menu()
{
$query = "SELECT table_name,field_type,display_name FROM {$GLOBALS['CONFIG']['db_prefix']}udf order by id";
$result = mysql_query($query) or die ("Error in query235: $query. " . mysql_error());
while ( $row = mysql_fetch_row($result))
{
if ( $row[1] == 1 || $row[1] == 2 || $row[1] == 3 )
{
echo "case '".$row[2]."':\n";
echo " info_Array = ".$row[0]."_array;\n";
echo " break;\n";
}
}
mysql_free_result($result);
}
function udf_functions_java_array()
{
$query = "SELECT table_name,field_type FROM {$GLOBALS['CONFIG']['db_prefix']}udf ORDER BY id";
$result = mysql_query($query) or die ("Error in query251: $query. " . mysql_error());
while ($row = mysql_fetch_row($result))
{
if ($row[1] == 1 || $row[1] == 2)
{
$query = "SELECT id,value FROM " . $row[0];
$subresult = mysql_query($query) or die("Error in query255: $query. " . mysql_error());
echo $row[0] . "_array = new Array();\n";
$index = 0;
while ($subrow = mysql_fetch_row($subresult))
{
echo "\t" . $row[0] . "_array[" . $index . "] = new Array(\"" . $subrow[1] . "\", " . $subrow[0] . ");\n";
$index++;
}
}
}
mysql_free_result($result);
}
function udf_functions_java_options($id)
{
$query = "SELECT table_name,field_type,display_name FROM {$GLOBALS['CONFIG']['db_prefix']}udf order by id";
$result = mysql_query($query) or die ("Error in query270: $query. " . mysql_error());
while ( $row = mysql_fetch_row($result))
{
if ( $row[1] == 1 || $row[1] == 2)
{
echo '<option id="'.$id.'" value="'.$row[2].'">'.$row[2].'</option>';
$id++;
}
}
mysql_free_result($result);
}
function udf_functions_add_udf()
{
if(empty($_REQUEST['table_name']))
{
$secureurl = new phpsecureurl;
header('Location: ' . $secureurl->encode('admin.php?last_message=' . msg('message_udf_cannot_be_blank') ));
exit;
}
if(empty($_REQUEST['display_name']))
{
$secureurl = new phpsecureurl;
header('Location: ' . $secureurl->encode('admin.php?last_message=' . msg('message_udf_cannot_be_blank') ));
exit;
}
$table_name = str_replace(' ', '', $GLOBALS['CONFIG']['db_prefix'] . 'udftbl_' . $_REQUEST['table_name']);
if(!preg_match('/^\w+$/',$table_name))
{
$secureurl = new phpsecureurl;
header('Location: ' . $secureurl->encode('admin.php?last_message=Error+:+Invalid+Name+(A-Z 0-9 Only)'));
exit;
}
// Check for duplicate table name
$query = "SELECT * FROM {$GLOBALS['CONFIG']['db_prefix']}udf WHERE table_name='$table_name'";
$result = mysql_query($query);
//echo mysql_num_rows($result);
if (mysql_numrows($result) == "0")
{
if ($_REQUEST['field_type'] == 1 || $_REQUEST['field_type'] == 2)
{
// They have chosen Select list of Radio list
//
// First we add a new column in the data table
$query = 'ALTER TABLE ' . $GLOBALS['CONFIG']['db_prefix'] . 'data ADD COLUMN ' . $table_name . ' int AFTER category';
$result = mysql_query($query);
if (!$result)
{
$secureurl = new phpsecureurl;
header('Location: ' . $secureurl->encode('admin.php?last_message=Error+:+Problem+With+Alter'));
exit;
}
// Now we need to create a new table to store the UDF Info
$query = 'CREATE TABLE ' . $table_name . ' ( id int auto_increment unique, value varchar(64) )';
$result = mysql_query($query);
if (!$result)
{
// If the CREATE fails, rollback the ALTER
$query = 'ALTER TABLE ' . $GLOBALS['CONFIG']['db_prefix'] . 'data DROP COLUMN ' . $table_name;
$result = mysql_query($query);
$secureurl = new phpsecureurl;
header('Location: ' . $secureurl->encode('admin.php?last_message=Error+:+Problem+With+Create'));
exit;
}
// And finally, add an entry into the udf table
$query = 'INSERT into ' . $GLOBALS['CONFIG']['db_prefix'] . 'udf (table_name,display_name,field_type) VALUES ("' . $table_name . '","' . $_REQUEST['display_name'] . '",' . $_REQUEST['field_type'] . ')';
$result = mysql_query($query);
if (!$result)
{
// If the INSERT fails, rollback the CREATE and ALTER
$query = 'DROP TABLE ' . $table_name;
$result = mysql_query($query);
$query = 'ALTER TABLE ' . $GLOBALS['CONFIG']['db_prefix'] . 'data DROP COLUMN ' . $table_name;
$result = mysql_query($query);
$secureurl = new phpsecureurl;
header('Location: ' . $secureurl->encode('admin.php?last_message=Error+:+Problem+With+INSERT'));
exit;
}
}
//CHM
elseif($_REQUEST['field_type'] == 4){
// They have chosen Select list of Radio list
//
// First we add a new column in the data table
$query = 'ALTER TABLE ' . $GLOBALS['CONFIG']['db_prefix'] . 'data ADD COLUMN ' . $table_name . '_primary int AFTER category,
ADD COLUMN ' . $table_name . '_secondary int AFTER ' . $table_name . '_primary';
//$query = 'ALTER TABLE ' . $GLOBALS['CONFIG']['db_prefix'] . 'data ADD COLUMN ' . $table_name . ' int AFTER category';
$result = mysql_query($query);
if (!$result)
{
$secureurl = new phpsecureurl;
header('Location: ' . $secureurl->encode('admin.php?last_message=Error+:+Problem+With+Alter'));
exit;
}
// Now we need to create a new table to store the UDF Info
$query = 'CREATE TABLE ' . $table_name . '_primary ( id int auto_increment unique, value varchar(64) )';
$result = mysql_query($query);
if (!$result)
{
// If the CREATE fails, rollback the ALTER
$query = 'ALTER TABLE ' . $GLOBALS['CONFIG']['db_prefix'] . 'data DROP COLUMN ' . $table_name . '_primary';
//$query = 'ALTER TABLE ' . $GLOBALS['CONFIG']['db_prefix'] . 'data DROP COLUMN ' . $table_name . '_primary, DROP COLUMN ' . $table_name . '_secondary';
$result = mysql_query($query);
$secureurl = new phpsecureurl;
header('Location: ' . $secureurl->encode('admin.php?last_message=Error+:+Problem+With+Create'));
exit;
}
$query = 'CREATE TABLE ' . $table_name . '_secondary ( id int auto_increment unique, value varchar(64), pr_id int )';
$result = mysql_query($query);
if (!$result)
{
// If the CREATE fails, rollback the ALTER
$query = 'ALTER TABLE ' . $GLOBALS['CONFIG']['db_prefix'] . 'data DROP COLUMN ' . $table_name . '_secondary';
$result = mysql_query($query);
$secureurl = new phpsecureurl;
header('Location: ' . $secureurl->encode('admin.php?last_message=Error+:+Problem+With+Create'));
exit;
}
// And finally, add an entry into the udf table
$query = 'INSERT into ' . $GLOBALS['CONFIG']['db_prefix'] . 'udf (table_name,display_name,field_type) VALUES ("' . $table_name . '_primary","' . $_REQUEST['display_name'] . '",' . $_REQUEST['field_type'] . ')';
$result = mysql_query($query);
if (!$result)
{
// If the INSERT fails, rollback the CREATE and ALTER
$query = 'DROP TABLE ' . $table_name . '_primary';
$result = mysql_query($query);
$query = 'DROP TABLE ' . $table_name . '_secondary';
$result = mysql_query($query);
//$query = 'ALTER TABLE ' . $GLOBALS['CONFIG']['db_prefix'] . 'data DROP COLUMN ' . $table_name;
$query = 'ALTER TABLE ' . $GLOBALS['CONFIG']['db_prefix'] . 'data DROP COLUMN ' . $table_name . '_primary, DROP COLUMN ' . $table_name . '_secondary';
$result = mysql_query($query);
$secureurl = new phpsecureurl;
header('Location: ' . $secureurl->encode('admin.php?last_message=Error+:+Problem+With+INSERT'));
exit;
}
}
//CHM
elseif ($_REQUEST['field_type'] == 3)
{
// The have chosen a text field
$query = 'ALTER TABLE ' . $GLOBALS['CONFIG']['db_prefix'] . 'data ADD COLUMN ' . $table_name . ' varchar(255) AFTER category';
$result = mysql_query($query);
if (!$result)
{
$secureurl = new phpsecureurl;
header('Location: ' . $secureurl->encode('admin.php?last_message=Error+:+Problem+With+Alter'));
exit;
}
$query = 'INSERT into ' . $GLOBALS['CONFIG']['db_prefix'] . 'udf (table_name,display_name,field_type) VALUES ("' . $table_name . '","' . $_REQUEST['display_name'] . '",' . $_REQUEST['field_type'] . ')';
$result = mysql_query($query);
if (!$result)
{
// If the INSERT fails, rollback the ALTER
$query = 'ALTER TABLE ' . $GLOBALS['CONFIG']['db_prefix'] . 'data DROP COLUMN ' . $table_name;
$result = mysql_query($query);
$secureurl = new phpsecureurl;
header('Location: ' . $secureurl->encode('admin.php?last_message=Error+:+Problem+With+INSERT'));
exit;
}
}
}
else
{
$secureurl = new phpsecureurl;
header('Location: ' . $secureurl->encode('admin.php?last_message=Error+:+Duplicate+Table+Name'));
exit;
}
}
function udf_functions_delete_udf()
{
// If we are deleting a sub-select, we have two entries to delete
// , a _primary, and a _secondary
if(isset($_REQUEST['type']) && $_REQUEST['type'] == 4) {
$explode_row = explode('_', $_REQUEST['id']);
$subselect_table_name = $explode_row[2];
foreach (array('primary', 'secondary') as $loop) {
$query = "DELETE FROM {$GLOBALS['CONFIG']['db_prefix']}udf where table_name LIKE '%{$subselect_table_name}_{$loop}'";
mysql_query($query) or die("Error removing primary udf row from udf table: $query. " . mysql_error());
$query = "ALTER TABLE {$GLOBALS['CONFIG']['db_prefix']}data DROP COLUMN {$GLOBALS['CONFIG']['db_prefix']}udftbl_{$subselect_table_name}_{$loop}";
$result = mysql_query($query, $GLOBALS['connection']) or die("Error in query: $query. " . mysql_error());
$query = "DROP TABLE IF EXISTS {$GLOBALS['CONFIG']['db_prefix']}udftbl_{$subselect_table_name}_{$loop}";
mysql_query($query) or die("Error dropping $loop table: $query. " . mysql_error());
}
} else {
$query = "DELETE FROM {$GLOBALS['CONFIG']['db_prefix']}udf where table_name = '{$_REQUEST['id']}'";
mysql_query($query) or die("Error in query343: $query. " . mysql_error());
$query = "ALTER TABLE {$GLOBALS['CONFIG']['db_prefix']}data DROP COLUMN {$_REQUEST['id']}";
$result = mysql_query($query, $GLOBALS['connection']) or die("Error in query: $query. " . mysql_error());
$query = 'DROP TABLE IF EXISTS ' . $_REQUEST['id'];
mysql_query($query) or die("Error in query346: $query. " . mysql_error());
}
}
function udf_functions_search_options()
{
$query = "SELECT table_name,field_type,display_name FROM {$GLOBALS['CONFIG']['db_prefix']}udf ORDER BY id";
$result = mysql_query($query) or die ("Error in query355: $query. " . mysql_error());
while ($row = mysql_fetch_row($result))
{
$name = str_replace(' ', '_', $row[2]);
echo '<option value="'.$name.'">'.$name.' only</option>';
}
mysql_free_result($result);
}
function udf_functions_search($lwhere,$lquery_pre,$lquery,$lequate,$lkeyword)
{
$tmp = $lwhere;
$dn = str_replace('_', ' ', $tmp);
$query = "SELECT table_name,field_type FROM {$GLOBALS['CONFIG']['db_prefix']}udf WHERE display_name = \"" . $dn . "\"";
$result = mysql_query($query) or die ("Error in query369: $query. " . mysql_error());
$row = mysql_fetch_row($result);
if ($row[1] == 1 || $row[1] == 2 || $row[1] = 4)
{
$lquery_pre .= ', ' . $row[0];
$lquery .= $row[0] . '.value' . $lequate . '\'' . $lkeyword . '\'';
$lquery .= ' AND ' . $GLOBALS['CONFIG']['db_prefix'] . 'data.' . $row[0] . ' = ' . $row[0] . '.id';
}
elseif ($row[1] == 3)
{
$lquery .= $row[0] . $lequate . '\'' . $lkeyword . '\'';
}
mysql_free_result($result) or die ("Error in query381: $query. " . mysql_error());
return array($lquery_pre,$lquery);
}
// User Defined Fields END
}