arthurmcneil /components/com_fabrik/models/table.php

Language PHP Lines 3994
MD5 Hash b38126dd4abc91b2851f41221bc1ba95 Estimated Cost $82,155 (why?)
Repository https://github.com/chrisinammo/arthurmcneil.git View Raw File View Project SPDX
   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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
<?php
/**
 * @package Joomla
 * @subpackage Fabrik
 * @copyright Copyright (C) 2005 Rob Clayburn. All rights reserved.
 * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
 */

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();

require_once( COM_FABRIK_FRONTEND.DS.'helpers'.DS.'json.php' );
require_once( COM_FABRIK_FRONTEND.DS.'helpers'.DS.'pagination.php' );
require_once( COM_FABRIK_FRONTEND.DS.'helpers'.DS.'string.php' );


class FabrikModelTable extends JModel {

	/** @var object the tables connection object */
	var $_oConn = null;

	/** @var object table Table */
	var $_table = null;

	/** @var array filters applied to table */
	var $_aFilter = null;

	/** @var array prefilters applied to the table */
	var $_aPrefilters = array();

	/** @var object table's database connection object (loaded from connection object) */
	var $_oConnDB = null;

	/** @var object table's form model */
	var $_oForm = null;

	/** @var array joins */
	var $_aJoins = null;

	/** @var array column calculations */
	var $_aRunCalculations = array();

	/** @var string table output format - set to rss to collect correct element data within function gettData()*/
	var $_outPutFormat = 'html';

	var $_isMambot = false;

	var $_admin = false;

	/** @var object to contain access rights **/
	var $_access = null;

	/** @var int the id of the last inserted record (or if updated the last record updated) **/
	var $_lastInsertId = null;

	/** @var bol determine if we apply prefilters (0 = yes, 1 = no )*/
	var $_togglePreFilters = 0;

	/** @var array store data to create joined records from */
	var $_joinsToProcess = array();

	/** @var array database fields */
	var $_dbFields = null;

	/** @var bol force reload table calculations **/
	var $_reloadCalculations = false;

	/** @var array data contains request data **/
	var $_aData = null;

	/** 'var array data to render the table with **/
	var $_tableData = null;

	/** @var string method to use when submitting form data // post or ajax*/
	var $_postMethod = 'post';

	/** @var int package id */
	var $_packageId = null;

	/** @var object plugin manager */
	var $_pluginManager = null;

	/** @var int id of table to load */
	var $_id = null;

	/** @var string join sql **/
	var $_joinsSQL = null;

	/** @var bol is the object inside a package? */
	//var $_inPackage  = false;

	/** @var bol  when getting the tableData this decides if only the elements published to the
	 table view are loaded. Primarily used by visualization plugins to get all table data regardless
	 of whether its published to the table view */
	var $_onlyTableData = true;

	var $_formGroupElementData = array();

	var $_joinsToThisKey = null;

	var $_real_filter_action = null;

	var $_aAsFields = null;

	/** array merged request and session data used to potentially filter the table **/
	var $_request = null;

	var $_aRow = null;
	
	/** array rows to delete **/
	vaR $_rowsToDelete = null;

	/**
	 * Constructor
	 *
	 * @since 1.5
	 */

	function __construct()
	{
		parent::__construct();
		$usersConfig = &JComponentHelper::getParams( 'com_fabrik' );
		$id = JRequest::getInt( 'tableid', $usersConfig->get( 'tableid' ) );
		$this->setId($id);
		$this->_access = new stdClass();
	}

	/**
	 * main query to build table
	 *
	 */

	function render()
	{
		global $mainframe, $_PROFILER;
		$document =& JFactory::getDocument( );
		if (is_null( $this->_id ) || $this->_id == '0') {
			return JError::raiseError( 500, JText::_('Table id not set - can not render'));
		}
		$params =& $this->getParams();
		$session =& JFactory::getSession();
		$this->_outPutFormat = JRequest::getVar( 'format', 'html' );
		$pluginManager =& JModel::getInstance( 'Pluginmanager', 'FabrikModel' );
		$pluginManager->loadPlugInGroup( 'table' );
		if (!$pluginManager->runPlugins( 'onStartRender', $this, 'table' )) {
			return;
		}

		$table 		=& $this->getTable();

		//cant set time limit in safe mode so suppress warning
		@set_time_limit ( 800 );
		$this->_request 	= $this->getRequestData();
		$context					= 'com_fabrik.table.'. $this->_id.'.list.';
		$limitLength 			= $mainframe->getUserStateFromRequest( $context.'limitLength', 'limit', $table->rows_per_page );
		$limitStart			= $mainframe->getUserStateFromRequest($context.'limitstart',			'limitstart',		0,	'int');
		if ($this->_outPutFormat == 'feed') {
			$limitLength = JRequest::getVar( 'limit',  $params->get( 'rsslimit',150 ) );
			$maxLimit = $params->get( 'rsslimitmax', 2500 );
			if ($limitLength > $maxLimit) {
				$limitLength = $maxLimit;
			}
		}
		$total 						= $this->getTotalRecords( );
		$this->_pageNav		=& $this->_getPagination( $total, $limitStart, $limitLength );
		if ($limitLength == 0) {
			$pageNav->limit = 0;
		}
		$this->_tableData = $this->getData();
		$this->getCalculations( );
		$table->hit();
	}

	/**
	 * this merges session data for the fromForm with any request data
	 * allowing us to filter data results from both search forms and filters
	 *
	 * @return array
	 */

	function getRequestData()
	{
		global $_SESSION;
		//session_destroy();
		if (isset($this->_request)) {
			return $this->_request;
		}

		$aData 			= JRequest::get( 'request' );
		$formModel =& $this->getForm();
		$table =& $this->getTable();
		if (is_array( $_SESSION )) {

			if (array_key_exists( "fabrik",  $_SESSION )) {

				if (array_key_exists( 'fromForm',  $_SESSION["fabrik"] )) {
					$fromForm = $_SESSION["fabrik"]['fromForm'];
					//only merge if the fromForm is not the same as the current table's form id
					if ($_SESSION["fabrik"]['fromForm'] != $formModel->_id) {
						$this->_oFromForm =& JModel::getInstance( 'Form', 'FabrikModel' );

						$this->_oFromForm->setId( $fromForm );
						$fromFormTable = $this->_oFromForm->getForm();
							
						$fromFormParams = $this->_oFromForm->getParams();

						//fudge the sesson so that any search form data's keys are the same as the current
						//tables
						if (array_key_exists( $fromForm, $_SESSION['fabrik'] )) {
							foreach ($_SESSION['fabrik'][$fromForm] as $k1=>$v) {
								//only unset if not done previously
								if (strstr( $k1, "___" )) {
									$k2 = $table->db_table_name . "___" . array_pop(explode("___", $k1));
								} else {
									$k2 = $table->db_table_name . "___" . $k1;
								}

								if (array_key_exists( $k2, $aData )) {
									//override search form session info with any posted filters
									$v = $aData[$k2];
								}
								$_SESSION['fabrik'][$fromForm][$k2] = $v;
							}
						}
						if (array_key_exists( $fromForm, $_SESSION["fabrik"])) {
							$aData = array_merge( $aData, $_SESSION["fabrik"][$fromForm] );
						}
					}
					//$$$rob since after 1.0.5.2 DONT unset as page nav no longer picks up the filter
					//unset( $_SESSION["fabrik"]['fromForm'] );
				}
			}
		}
		return $aData;
	}

	/**
	 * get the table's data
	 *
	 * @return array of objects (rows)
	 */

	function getData()
	{
		$fabrikDb =& $this->getDb();
		$query = $this->_buildQuery();
		$fabrikDb->setQuery( $query, $this->_pageNav->limitstart, $this->_pageNav->limit );
		if (JRequest::getBool( 'fabrikdebug', 0 ) == 1) {
			echo "<pre>"; echo $fabrikDb->getQuery();echo "</pre>";
		}
		$data =  $fabrikDb->loadObjectList( );
		if ($data === false) {
			JError::raiseNotice(500,  'getData: ' . $fabrikDb->getErrorMsg( ) );
		}
		//append the cursor & total to the data
		if ($this->_outPutFormat == 'html' || $this->_outPutFormat == 'raw') {
			for ($i=0; $i<count($data); $i++) {
				$data[$i]->_cursor = $i + $this->_pageNav->limitstart;
				$data[$i]->_total = $this->_pageNav->total;
			}
		}
		$this->formatData( $data );
		return $data;
	}

	/**
	 * run the table data through element filters
	 *
	 * @param array $data
	 */

	function formatData( &$data )
	{
		jimport('joomla.filesystem.file');
		$form =& $this->getForm();
		$table =& $this->getTable();
		$pluginManager 	=& JModel::getInstance( 'Pluginmanager', 'FabrikModel' );
		$method 				= 'renderTableData_' . $this->_outPutFormat;
		$this->_aLinkElements = array();
		foreach ($form->_groups as $groupModel) {
			if (is_array( $groupModel->_aElements )) {
				foreach ($groupModel->_aElements as $elementModel) {
					$e =& $elementModel->getElement();
					$elementModel->setContext( $groupModel, $form, $this );
					$params =& $elementModel->getParams( );
					$col 	= $elementModel->getFullName( false, true, false );

					//check if there is  a custom out put handler for the tables format
					// currently supports "renderTableData_csv", "renderTableData_rss", "renderTableData_html", "renderTableData_json"
					if (!empty( $data ) && array_key_exists( $col, $data[0] )) {
						if (method_exists( $elementModel, $method )) {
							for ($i=0; $i<count( $data ); $i++) {
								$thisRow = $data[$i];
								$coldata = $data->$col;
								$d = $elementModel->$method( $coldata, $col, $thisRow );
								$data[$i]->$col = $this->_addLink( $d, $elementModel, $thisRow );
							}

						} else {
							$ec = count( $data );
							for ( $i=0; $i< $ec; $i++ ) {
								$thisRow = $data[$i];
								$coldata = $thisRow->$col;
								$d = $elementModel->renderTableData( $coldata, $thisRow );
								$data[$i]->$col = $this->_addLink( $d, $elementModel, $thisRow );
								$rawCol = $col . "_raw";
								if (!array_key_exists( $rawCol, $thisRow)){
									$data[$i]->$rawCol = $elementModel->renderRawTableData( $coldata, $thisRow );
								}
							}

							// run a final function for each fo the elements (basically here to
							// avoid you doing extraneous sql calls in renderTableData
							// ie create sql query in rendertabledata, then run it in mergeTableData
							// currently used for advanced table joins only
							$elementModel->mergeTableData( $data, $this );
						}

						//see if we replace data with icons
						/*
						if ($params->get('icon_folder') != -1 && $params->get('icon_folder') != '') {
							jimport('joomla.filesystem.file');
							for ($i=0; $i<count( $data ); $i++) {
								$thisRow = $data[$i];
								$coldata = $thisRow->$col;
								$data[$i]->$col = $elementModel->replaceWithIcons( $coldata );
							}
						}
*/
					}
				}
			}
		}
		$this->_aGroupInfo = array();
		$groupTitle = array();

		//check if the data has a group by applied to it
		if ($table->group_by != '') {
			$groupedData = array();
			$thisGroupedData = array();
			$groupBy = $table->group_by;
			//see if we can use a raw value instead
			if (!empty( $data ) && array_key_exists( $groupBy . "_raw", $data[0] )) {
				$groupBy = $groupBy . "_raw";
			}
			$groupTitle = null;
			$aGroupTitles = array();
			$groupId = 0;
			for ( $i=0; $i<count( $data ); $i++ ) {
				if (!in_array( $data[$i]->$groupBy , $aGroupTitles )) {
					$aGroupTitles[] = $data[$i]->$groupBy;
					$groupedData[$data[$i]->$groupBy] = array();
				}
				$data[$i]->_groupId = $data[$i]->$groupBy;
				$gKey = $data[$i]->$groupBy;
				//	if the group_by was added in in getAsFields remove it from the returned data set (to avoid mess in package view)
				if ($this->_group_by_added ){
					unset($data[$i]->$groupBy );
				}
				if ($this->_temp_db_key_addded ){
					$k = $table->db_primary_key;;
				}
				$groupedData[$gKey][] = $data[$i];

			}
			$data = $groupedData;
		} else {
			for( $i=0; $i<count( $data ); $i++ ){
				if ($this->_temp_db_key_addded ){
					$k = $table->db_primary_key;;
				}
			}
			//make sure that the none grouped data is in the same format
			$data = array( $data );
		}

		if ($this->_outPutFormat != 'pdf' && $this->_outPutFormat != 'csv' && $this->_outPutFormat != 'feed') {
			$this->addSelectBoxAndLinks( $data );
			if (JRequest::getVar( 'fabrikdebug' )) {
				echo "<pre>";print_r($data);echo"</pre>";
			}
		}
	}

	/**
	 * add the select box and various links into the data array
	 * @param array table row objects
	 */

	function addSelectBoxAndLinks( &$data )
	{
		global $Itemid;
		$table 		=& $this->getTable();
		$db 			=& JFactory::getDBO();
		$params 	=& $this->getParams();
		$nextview = ($this->canEdit()) ? "form" : "details";
		$tmpKey 	= '__pk_val';

		$aExisitngLinkedTables = $params->get('linkedtable', '', '_default', 'array');
		$aExisitngLinkedForms = $params->get('linkedform', '', '_default', 'array');

		$linkedform_linktype 		= $params->get( 'linkedform_linktype', '', '_default', 'array' );
		$linkedtable_linktype 	= $params->get( 'linkedtable_linktype', '', '_default', 'array' );
		$aExistingTableHeaders 	= $params->get( 'linkedtableheader', '', '_default', 'array' );
		$aExistingFormHeaders 	= $params->get( 'linkedformheader', '', '_default', 'array' );

		//get a list of fabrik tables and ids for view table and form links

		$action = ($this->_admin) ? "task" : "view";
			
		$sql = "SELECT id, label FROM #__fabrik_tables";
		$db->setQuery( $sql );
		$aTableNames = $db->loadObjectList( 'label' );
		$cx = count($data);
		$viewLinkAdded = false;
		//for ($x=0; $x<$cx; $x++) { //if grouped data then the key is not numeric
		foreach ($data as $key=>$group) {
			//$group =& $data[$key]; //Messed up in php 5.1 group positioning in data became ambiguous
			$cg = count( $group );
			for( $i=0; $i < $cg; $i++ ){
				$row =& $data[$key][$i];
				$pKeyVal = (array_key_exists( $tmpKey, $row )) ? $row->$tmpKey : '';
				$row->fabrik_delete =  ($this->canDelete()) ?  '<input type="checkbox" id="id_'.$row->_cursor .'" name="ids['.$row->_cursor.']" value="' . $pKeyVal . '" />' : '';
				//add in some default links if no element choosen to be a link
				if (empty( $this->_aLinkElements ) and ($this->canView() || $this->canEdit())){


					$link = ( $this->_outPutFormat == 'json' ) ? "#" : JRoute::_("index.php?option=com_fabrik&c=form&$action=$nextview&Itemid=$Itemid&fabrik=" . $table->form_id . "&rowid=$pKeyVal&tableid=" .$this->_id . "&fabrik_cursor=" . $row->_cursor . "&fabrik_total=" . $row->_total );
					if ($this->canEdit()) {
						$row->fabrik_edit = "<a class='fabrik___rowlink' href='$link'>" . JText::_('Edit') . "</a>";
					} else {
						if($this->canViewDetails()){
							$viewLinkAdded = true;
							$row->fabrik_edit = "<a class='fabrik___rowlink' href='$link'>" . JText::_('View') . "</a>";
						} else {
							$row->fabrik_edit = '';
						}
					}
				}
				//@TODO: test if all of these are necessary
				if ($this->canViewDetails()) {
					$link = JRoute::_( "index.php?option=com_fabrik&c=form&$action=form&Itemid=$Itemid&fabrik=" . $table->form_id . "&rowid=$pKeyVal&tableid=" .$this->_id . "&fabrik_cursor=" . $row->_cursor . "&fabrik_total=" . $row->_total );
					$row->fabrik_view = "<a class='fabrik___rowlink' href='$link'>" . JText::_('View') . "</a>";
				}

				if ($this->canViewDetails( ) && $params->get( 'detaillink' ) == '1' && !$viewLinkAdded ) {
					$row->__details_link = $this->viewDetailsLink( $pKeyVal );
				}
				// create columns containing links which point to tables associated with this table
				$joinsToThisKey = $this->getJoinsToThisKey( );
				$f = 0;
				foreach ($joinsToThisKey as $element) {
					$linkedTable 	= array_key_exists($f, $aExisitngLinkedTables) ? $aExisitngLinkedTables[$f] : false;
					$popUpLink 		= array_key_exists($f, $linkedtable_linktype) ? $linkedtable_linktype[$f] : false;
					if ($linkedTable != '0') {
						if ($element->tablelabel == $table->label) { //if the link points to the same table
							$thiskey 	= $table->db_table_name.'.'.$this->_oTable->_tbl_key;
							$key 		= $element->element_name;
							$x 			= $element->element_name;
							$val 		= $row->$thiskey;
						} else {
							$linkKey	= $element->db_table_name . "___" . $element->name;
							$key 		= $linkKey . "_table_heading";
							$val 		= $pKeyVal;
						}
						$element->table_id = ( array_key_exists( $element->tablelabel, $aTableNames)) ?  $aTableNames[$element->tablelabel]->id : '';

						if ($popUpLink != '0') {
							//pop up window link
							$url = JRoute::_( "index.php?option=com_fabrik&tmpl=component&Itemid=$Itemid&view=table&tableid=$element->table_id&$linkKey" . "[value]=$val&fabrik_cursor=" . $row->_cursor . "&fabrik_total=" . $row->_total );
							FabrikHelperHTML::mocha( 'a.popupwin' );
							$group[$i]->$key  =  '<a rel="{\'maximizable\':true}" href="'. $url.'" class="popupwin">'. JText::_('View') .'</a>';
						} else {
							$url = JRoute::_( "index.php?option=com_fabrik&Itemid=$Itemid&$action=table&tableid=$element->table_id&$linkKey" . "[value]=$val&fabrik_cursor=" . $row->_cursor . "&fabrik_total=" . $row->_total );
							$group[$i]->$key  = "<a href=\"$url\">" .  JText::_('View'). "</a>";
						}
					}
					$f ++;
				}

				$linksToForms =  $this->getLinksToThisKey( );
				$f = 0;
				//create columns containing links which point to forms assosciated with this table
				foreach ( $linksToForms as $element ) {
					if ($element != '') {
						$linkedForm 	= array_key_exists($f, $aExisitngLinkedForms) ? $aExisitngLinkedForms[$f] : false;
						$popUpLink 		= array_key_exists($f, $linkedform_linktype) ? $linkedform_linktype[$f] : false;
						$linkKey	= $element->db_table_name . "___" . $element->name;
						$key 		= $linkKey . "_form_heading";
						if ($linkedForm != '0') {
							if ($popUpLink != '0') {
								$url = JRoute::_("index.php?option=com_fabrik&tmpl=component&Itemid=$Itemid&$action=form&tableid=$element->table_id&fabrik=$element->form_id&$linkKey" . "[value]=$val");
								FabrikHelperHTML::mocha( 'a.popupwin' );
								$group[$i]->$key  =  '<a rel="{\'maximizable\':true}" href="'. $url.'" class="popupwin">'. JText::_('Add') .'</a>';
							} else {
								$url = JRoute::_("index.php?option=com_fabrik&Itemid=$Itemid&$action=form&tableid=$element->table_id&fabrik=$element->form_id&$linkKey" . "[value]=$val");
								$group[$i]->$key = "<a href=\"$url\">" . JText::_('Add') . "</a>";
							}
						}
					}
					$f ++;
				}
			}
		}
	}

	/**
	 * add a custom link to the element data
	 *
	 * @param string element $data
	 * @param object element
	 * @param object of all row data
	 */

	function _addLink( $data, &$elementModel, &$row )
	{
		global $Itemid;
		if ($this->_outPutFormat == 'csv') {
			return $data;
		}

		$nextview = ($this->canEdit()) ? "form" : "details";
		$params 	=& $elementModel->getParams();
		$element 	=& $elementModel->getElement();
		$table 		=& $this->getTable();

		if ($element->link_to_detail == '1' && ( $this->canEdit() || $this->canView())) {
			$this->_aLinkElements = $element->name;
			if ($this->_postMethod == 'post') {
				$primaryKeyVal = $this->getKeyIndetifier( $row );
				if ($this->_admin) {
					$link = JRoute::_( "index.php?option=com_fabrik&c=form&task=$nextview&fabrik=" . $table->form_id . "$primaryKeyVal&fabrik_cursor=" . @$row->_cursor . "&fabrik_total=" . @$row->_total . "&tableid=" .$this->_id );
				} else {
					$link = JRoute::_( "index.php?option=com_fabrik&c=form&view=$nextview&Itemid=$Itemid&fabrik=" . $table->form_id . "$primaryKeyVal&fabrik_cursor=" . @$row->_cursor . "&fabrik_total=" . @$row->_total . "&tableid=" .$this->_id );
				}
			} else {
				$link = '#';
			}
			//try to remove any previously entered links
			$data = preg_replace( '/<a(.*?)>|<\/a>/', '', $data );
			$data = "<a class='fabrik___rowlink' href='$link'>$data</a>";
		} else {
			$customLink = $params->get( 'custom_link' );
			if ($customLink != '') {
				//$w = new FabrikWorker();
				//$customLink = $w->parseMessageForPlaceHolder($customLink, JArrayHelper::fromObject( $row ));
				$customLink = $this->parseMessageForRowHolder($customLink, JArrayHelper::fromObject( $row ));
				//try to remove any previously entered links
				$data = preg_replace( '/<a(.*?)>|<\/a>/', '', $data );
				$data = "<a class='fabrik___rowlink' href='$customLink'>$data</a>";
			}
		}
		return $data;
	}

	/**
	 * get query to make records
	 * @return string sql
	 */

	function _buildQuery()
	{
		$query = $this->_buildQuerySelect();
		$query .= $this->_buildQueryJoin();
		$query .= $this->_buildQueryWhere();
		$query .= $this->_buildQueryOrder();
		return $query;
	}

	function _buildQuerySelect()
	{
		$form =& $this->getForm();
		$table =& $this->getTable();
		$form->getGroupsHiarachy( $this->_onlyTableData, true );
		$this->_getAsFields();
		$fields = (empty( $this->_aFields )) ? '' : implode( ", \n ", $this->_aFields ) . "\n ";
		if (trim( $table->db_primary_key ) != '' && ( $this->_outPutFormat == 'html' || $this->_outPutFormat == 'feed' ) ) {
			if ($this->isView()) {
				$strPKey = ''; //view dont have primary key!
			} else {
				$fields .= ", ";
				$strPKey = $table->db_primary_key . " AS __pk_val\n";
			}
			$query = 'SELECT DISTINCT ' . $fields . $strPKey;
		} else {
			$query = 'SELECT DISTINCT ' . trim($fields, ", \n")  . "\n";
		}
		$query .= " FROM `$table->db_table_name` \n" ;
		return $query;
	}

	/**
	 * get the part of the sql statement that orders the table data
	 * @return string ordering part of sql statement
	 */

	function _buildQueryOrder()
	{
		global $mainframe;
		$params = $this->getParams();
		if ($this->_outPutFormat == 'feed' )
		{
			$dateCol = $params->get( 'feed_date', '' );
			if( $dateCol != '' ){
				$this->order_dir = 'DESC';
				$this->order_by 	= $dateCol;
				return "\n ORDER BY `$dateCol` DESC";
			}
		}
		$session =& JFactory::getSession();
		$table =& $this->getTable();

		$postOrderBy =  JRequest::getVar( 'orderby', '', 'post' );
		$postOrderBy = str_replace( ".", "___", $postOrderBy );

		$postOrderDir = JRequest::getVar( 'orderdir', '', 'post' );
		$arOrderVals = array('asc', 'desc', '-');
		if (in_array( $postOrderDir, $arOrderVals )) {
			$context			= 'com_fabrik.table.'. $this->_id.'.order.'.$postOrderBy;
			$session->set( $context, $postOrderDir );
		}
		//build the order by statement from the session
		$strOrder = '';
		foreach ($this->_aAsFields as $field) {
			$field = str_replace('`', '', $field);
			$context			= 'com_fabrik.table.'. $this->_id.'.order.'.$field;
			$dir = $session->get( $context );
			if ($dir != '' && $dir != '-' && trim( $dir ) != 'Array' ) {
				$field = str_replace( "___", ".", $field );
				if (!strstr( $field, '.' )) {
					$field =  $table->db_table_name.'.'. $field ;
				}
				//ensure its quoted

				$field = explode( ".", $field );
				$field = "`" . $field[0] . "`.`" . $field[1] . "`";
				$strOrder == '' ? $strOrder = "\n ORDER BY " : $strOrder .= ',';
				$strOrder .= " $field $dir";
			}
		}

		//if nothing found in session use default ordering
		if ($strOrder == '' ) {
			if ($table->order_by != '') {
				$table->order_dir != ''? $dir = $table->order_dir : $dir = 'desc';
				$field = str_replace( "___", ".", $table->order_by );
				$field = explode( ".", $field );
				if(count($field) == 2 && $field[1] != '') {
					$field = "`" . $field[0] . "`.`" . $field[1] . "`";
					$strOrder = "\n ORDER BY $field $dir";
				}
			}
		}

		// apply group ordering
		$groupOrderBy = $params->get( 'group_by_order' );
		if ($groupOrderBy != '' && in_array( $groupOrderBy, $this->_aAsFields )) {
			$groupOrderDir = $this->_params->get( 'group_by_order_dir' );
			$strOrder == '' ? $strOrder = "\n ORDER BY " : $strOrder .= ',';
			$strOrder .= " `$groupOrderBy`  $groupOrderDir ";
		}
		return $strOrder;
	}

	/**
	 * get the part of the sql query that creates the joins
	 * used when building the table's data
	 *
	 * @return string join sql
	 */

	function _buildQueryJoin()
	{
		if (isset( $this->_joinSQL )) {
			return $this->_joinsSQL;
		}
		$sql = '';
		$joins 	=& $this->getJoins();
		
		$tableGroups = array();
		foreach ($joins as $join) {
			$sql .= strtoupper($join->join_type) ." JOIN `$join->table_join`" ;
			
			if ($join->table_join_alias == '') {
				/*$sql .=	" ON `$join->table_join`.`$join->table_join_key` = " .
									"`$join->join_from_table`.`$join->table_key` \n";*/
				$sql .=	" ON `$join->table_join`.`$join->table_join_key` = " .
									"`$join->keytable`.`$join->table_key` \n";
			} else {
				/*$sql .= " AS `" . $join->table_join_alias .
				"` ON `" . $join->table_join_alias . "`.`$join->table_join_key` = " .
				"`$join->join_from_table`.`$join->table_key` \n ";*/
				
				$sql .= " AS `" . $join->table_join_alias .
				"` ON `" . $join->table_join_alias . "`.`$join->table_join_key` = " .
				"`$join->keytable`.`$join->table_key` \n ";
				
			}
		}
		return $sql;
	}

	/**
	 * get the part of the sql query that relates to the where statement
	 *
	 * @param bol $incFilters
	 * @return string where query
	 */

	function _buildQueryWhere( $incFilters = true )
	{
		if (isset( $this->_whereSQL )) {
			return $this->_whereSQL[$incFilters];
		}
		$aFilters	=& $this->getFilterArray();
		$params =& $this->getParams();
		if (isset( $params ) && $this->_togglePreFilters == 0) {
			$aPrefilters = $this->getPrefilterArray( );
		} else {
			$aPrefilters = array();
		}
		$aOrSQL = array( );
		$aFoundOrGroupings = array( );
		$i = 0;

		$aSQLBits = array();
		if (is_array( $aFilters )) {
			/* stores sql for or statements */
			$c = 0;
			foreach ($aFilters as $key=>$val) {
				/*work through or columns first to build sql ( col = val or col = val...)
				 * then remove them from the rest of the array
				 */
				if ( isset( $val['aOrCols'] ) ) {

					$aOrColumns = $val['aOrCols'] ;
					$filterVal = isset( $val['value'] ) ? $val['value'] : '';
					/* check if we've already performed it elsewhere */
					$done 		= false;
					foreach ($aFoundOrGroupings as $aFoundOrGroupSet) {
						$a = filter_unused( $aFoundOrGroupSet, $aOrColumns );
						if (empty( $a )){
							$done = true;
						}
					}
					if (!$done) {
						/*
						 * ok we havent processed this group -
						 * lets add it to the groups the we have processed
						 */
						$aFoundOrGroupings[] = $aOrColumns;
						/*
						 *now lets build that query string!
						 */
						$orSql ='(';
						foreach ($aOrColumns as $col) {
							$col = FabrikWorker::getDbSafeName( $col );
							$orSql .= "`$col` = '$filterVal' OR " ;
						}
						$orSql = substr($orSql, 0, strlen($orSql)-3) . ')';
						$aOrSQL[] = $orSql;
					}
					/* ok, even if its been done before we still need to remove it from the filter array */
					unset ( $aFilters[$key] );
					$c++;
				}
			}
			foreach ($aFilters as $key=>$val) {
				$filterType 			= isset( $val['type'] )  ? $val['type']: 'dropdown';
				$filterVal 				= isset( $val['value'] ) ? $val['value'] : '';
				$filterExactMatch = isset( $val['match'] ) ? $val['match'] 	: '';
				$fullWordsOnly 		= isset( $val['full_words_only'] )? $val['full_words_only'] : '0';

				if (array_key_exists( $key, $aPrefilters )){
					if (array_key_exists( 'sqlCond' , $aPrefilters[$key] )) {
						$sqlCond = "( " .$val['sqlCond'] . " AND " . $aPrefilters[$key]['sqlCond'] . " )";
					} else {
						//$$$rob used for prefilter and table - "Tables with database join elements linking to this table " pointing to same prefilter opt
						$sqlCond = "( " .$val['sqlCond'];
						foreach ($aPrefilters[$key] as $tmpC) {
							$sqlCond .= " AND " . $tmpC['sqlCond'];
						}
						$sqlCond .= " )";
					}
					unset( $aPrefilters[$key] );
				} else {
					$sqlCond = $val['sqlCond'];
				}

				if ( $filterVal != "" ) {
					$aSQLBits[] = " AND ";
					$aSQLBits[] = $sqlCond;
					$i ++;
				}
			}
		}

		//add in any prefiltres not duplicated by filters
		//put them at the beginning of query as well

		$aSQLBits2 = array();
		foreach ($aPrefilters as $key=>$ar) {
			if ($key !== '') {
				$aSQLBits2[] = $ar['concat'];
				$aSQLBits2[] = $ar['sqlCond'];
			}
		}
		// $$$rob work out the where statment minus any filters (so only include prefilters)
		// this is needed to ensure that the filter drop downs contain the correct info.

		$sqlNoFilter = '';
		if (!empty( $aSQLBits2 )) {
			$aSQLBits2[0] = "WHERE";
			$sqlNoFilter .= implode(  ' ', $aSQLBits2 );
			if (count( $aOrSQL ) > 0) {
				if (empty( $aSQLBits2 )) {
					$sqlNoFilter .= " WHERE " . implode( ' AND ', $aOrSQL );
				} else {
					$sqlNoFilter .=  ' AND ' . implode( ' AND ', $aOrSQL );
				}
			}
		}
		//apply advanced filter query
		$advancedFilter = JRequest::getVar('advancedFilterContainer', array('value' => ''), 'default', 'none', 2);

		$sql = '';
		$aSQLBits = array_merge( $aSQLBits2, $aSQLBits );

		if (!empty( $aSQLBits )) {
			$aSQLBits[0] = "WHERE";
			$sql .= implode( ' ', $aSQLBits );
			if (count( $aOrSQL ) > 0) {
				if (empty( $aSQLBits )) {
					$sql .= " WHERE " . implode( ' AND ', $aOrSQL );
				} else {
					$sql .=  ' AND ' . implode( ' AND ', $aOrSQL );
				}
			}
		}
		if ($advancedFilter['value'] != '') {
			$sql .= empty( $sql ) ? " WHERE " : " AND ";
			$sql .= trim( trim( $advancedFilter['value'], "AND" ), "OR" );
		}
		$this->_whereSQL = array( '0'=>$sqlNoFilter, '1'=>$sql );
		return $this->_whereSQL[$incFilters];
	}

	/**
	 * get the part of the table sql statement that selects which fields to load
	 * (both this_>_aASFields and this->_aFields)
	 *
	 * @return array field names to select in getelement data sql query
	 */

	function &_getAsFields()
	{
		if (!is_null( $this->_aAsFields )) {
			return $this->_aAsFields;
		}
		$form 			=& $this->getForm();
		$table 			=& $this->getTable();
		$aJoinObjs 	=& $this->getJoins();
		$this->_aAsFields = array();
		$this->_aFields = array();
		$this->_temp_db_key_addded = false;

		foreach ($form->_groups as $groupModel) {
			$table_name = $table->db_table_name;
			$group =& $groupModel->getGroup();
			if ($group->is_join) {
				foreach ($aJoinObjs as $join) {
					//also ignore any joins that are elements
					if (array_key_exists( 'group_id', $join ) && $join->group_id == $group->id && $join->element_id == 0 ) {
						$table_name =  $join->table_join;
					}
				}
			}
			foreach ($groupModel->_aElements as $elementModel) {
				if (!$this->_onlyTableData || $elementModel->inTableFields( $this )) {
					$method = "getAsField_" . $this->_outPutFormat;
					if (!method_exists( $elementModel, $method )) {
						$method = "getAsField_html";
					}
					$elementModel->$method( $this->_aFields, $this->_aAsFields, $table_name );
				}
			}
		}
		//temporaraily add in the db key so that the edit links work, must remove it before final return
		//	of getData();

		if (!$this->isView()) {
			if (!$this->_temp_db_key_addded && $table->db_primary_key != '') {
				$str = str_replace( '___', '.', $table->db_primary_key ) . " AS " . str_replace( '.', '___', $table->db_primary_key );
				//if we are quoting the priamry key in the db then we need to remove these quotes
				$str = str_replace( '`___`', '___', $str );
				$this->_aFields[] = $str;
				$this->_aAsFields[] = $table->db_primary_key;
			}
		}
		//for raw data in packages

		if ($this->_outPutFormat == 'raw') {
			$str = str_replace( '___', '.', $table->db_primary_key ) . " AS __pk_val";
			$str = str_replace( '`___`', '___', $str );
			$this->_aFields[] = $str;
		}
		//end

		$this->_group_by_added = false;
		//if the group by element isnt in the fields add it (otherwise group by wont work)
		if (!in_array( $table->group_by, $this->_aAsFields ) && trim( $table->group_by ) != '') {
			$this->_aFields[] = str_replace( '___', '.', $table->group_by ) . " AS `$table->group_by`";
			$this->_aAsFields[] = $table->group_by;
			$this->_group_by_added = true;
		}
		return $this->_aAsFields;
	}

	/**
	 * checks if the params object has been created and if not creates and returns it
	 * @return object params
	 */

	function &getParams()
	{
		$table =& $this->getTable();
		if (!isset( $this->_params )) {
			$this->_params = &new fabrikParams( $table->attribs, JPATH_SITE . '/administrator/components/com_fabrik/models/table.xml', 'component' );
		}
		return $this->_params;
	}


	/**
	 * Method to set the table id
	 *
	 * @access	public
	 * @param	int	table ID number
	 */

	function setId( $id )
	{
		$this->_id		= $id;
	}

	/**
	 * sets the instances admin state
	 * @param bol admin state
	 */

	function setAdmin( $bol )
	{
		$this->_admin = $bol;
	}

	/**
	 * get the table object for the models _id
	 *
	 * @return object table
	 */

	function &getTable()
	{
		if (is_null( $this->_table )) {
			JTable::addIncludePath( JPATH_ADMINISTRATOR.DS.'components'.DS.'com_fabrik'.DS.'tables' );
			$row = JTable::getInstance( 'table', 'Table' );
			$row->load( $this->_id );
			$this->_table =& $row;
		}
		return $this->_table;
	}

	/**
	 * load  the database associated with the table
	 *@return object database
	 */

	function &getDb()
	{
		if (!isset( $this->_oConnDB )) {
			$cnn =& $this->getConnection();
			if (!is_object( $cnn )) {
				return JError::raiseError( 500, JText::_( 'Fabrik was unable to load the database object for this table' ) );
			}
			$this->_oConnDB =& $cnn->getDb( );
		}
		return $this->_oConnDB;
	}

	/**
	 * function get the tables connection object
	 * sets $this->_oConn to the tables connection
	 * @return object connection
	 */

	function &getConnection( )
	{
		$config =& JFactory::getConfig();
		if (!isset( $this->_oConn )) {
			$table =& $this->getTable();
			$connectionModel =& JModel::getInstance( 'connection', 'FabrikModel' );
			$connId = ( is_null( $table->connection_id ) ) ? JRequest::getVar( 'connection_id', null ) : $table->connection_id;
			$connectionModel->setId( $connId );
			if ($connId == '' || is_null( $connId ) ||  $connId == '-1' ){ //-1 for creating new table
				$connectionModel->loadDefaultConnection();
				$connectionModel->setId( $connectionModel->_connection->id );
			}
			$connection =& $connectionModel->getConnection( );
			// if its the default connection then load from the
			// config file
			if ($connectionModel->isDefault() ){
				$connection->host = $config->getValue('config.host');
				$connection->user = $config->getValue('config.user');
				$connection->password = $config->getValue('config.password');
				$connection->database = $config->getValue('config.db');
			}
			$this->_oConn =& $connectionModel;
		}
		return $this->_oConn;
	}

	/**
	 * is the table published
	 *
	 * @return bol published state
	 */

	function canPublish()
	{
		$table =& $this->getTable();
		
		global $mainframe;
		$db =& JFactory::getDBO();
		if (method_exists( $db, 'getNullDate' )) {
			$nullDate = $db->getNullDate( );
		} else {
			$nullDate = $this->getNullDate( );
		}
		$publishup =& JFactory::getDate($table->publish_up, $mainframe->getCfg('offset'));
		$publishup = $publishup->toUnix();
		
		$publishdown =& JFactory::getDate($table->publish_down, $mainframe->getCfg('offset'));
		$publishdown = $publishdown->toUnix();
		
		$jnow		=& JFactory::getDate();
		$now		= $jnow->toUnix();
		if ($table->state == '1') {
			if ($now >= $publishup || $table->publish_up == '' || $table->publish_up == $nullDate) {
				if ($now <= $publishdown || $table->publish_down == '' || $table->publish_down == $nullDate) {
					return true;
				}
			}
		}
		return false;
		
		return $table->state;
	}
	
	/**
	 * 
	 */
	
	function canEmpty()
	{
		$user  	=& JFactory::getUser();
		$acl 		=& JFactory::getACL();
		$table 	=& $this->getTable();
		$params =& $this->getParams();
		$a = $params->get( 'allow_drop', 0 );
		if ($a == '29'|| $a == ''|| $a == 0) {
			$this->_access->allow_drop =  true;
		} else {
			if (!is_object( $this->_access ) || !array_key_exists( 'allow_drop', $this->_access )) {
				$groupNames =& FabrikWorker::getACLGroups( $a );
				foreach ($groupNames as $name) {
					FabrikWorker::setACL( 'action', 'allow_drop', 'fabrik', $name, 'components', null );
				}
				if ($acl->acl_check( 'action', 'allow_drop', 'fabrik', $user->get(' usertype' ), 'components', null )) {
					$this->_access->allow_drop = true;
				} else {
					$this->_access->allow_drop = false;
				}
			}
		}
		return $this->_access->allow_drop;		
	}

	/**
	 * check if the user can view the detailed records
	 *
	 * @return bol
	 */

	function canViewDetails()
	{
		$user  	=& JFactory::getUser();
		$acl 		=& JFactory::getACL();
		$table 	=& $this->getTable();
		$params =& $this->getParams();
		$a = $params->get( 'allow_view_details', 0 );
		if ($a == '29'|| $a == ''|| $a == 0) {
			$this->_access->viewdetails =  true;
		} else {
			if (!is_object( $this->_access ) || !array_key_exists( 'viewdetails', $this->_access )) {
				$groupNames =& FabrikWorker::getACLGroups( $a );
				foreach ($groupNames as $name) {
					FabrikWorker::setACL( 'action', 'viewdetails', 'fabrik', $name, 'components', null );
				}
				if ($acl->acl_check( 'action', 'viewdetails', 'fabrik', $user->get(' usertype' ), 'components', null )) {
					$this->_access->viewdetails = true;
				} else {
					$this->_access->viewdetails = false;
				}
			}
		}
		return $this->_access->viewdetails;
	}

	/**
	 * checks user access for editing records
	 *
	 * @return bol access allowed
	 */

	function canEdit()
	{
		$user  	=& JFactory::getUser();
		$acl 		=& JFactory::getACL();
		$table 	=& $this->getTable();
		$params =& $this->getParams();
		$a = $params->get( 'allow_edit_details', 25 );
		if ($a == '29'|| $a == ''|| $a == 0) {
			$this->_access->edit =  true;
		} else {
			if(!is_object($this->_access) || !array_key_exists('edit', $this->_access)){
				$groupNames = FabrikWorker::getACLGroups( $a );
				foreach ($groupNames as $name) {
					FabrikWorker::setACL( 'action', 'edit', 'fabrik', $name, 'components', null );
				}
				if ($acl->acl_check( 'action', 'edit', 'fabrik', $user->get( 'usertype' ), 'components', null )) {
					$this->_access->edit = true;
				} else {
					$this->_access->edit = false;
				}
			}
		}
		return $this->_access->edit;
	}

	/**
	 * checks user access for deleting records
	 *
	 * @return bol access allowed
	 */

	function canDelete()
	{
		$user  	= &JFactory::getUser();
		$acl 		=& JFactory::getACL();
		$table 	=& $this->getTable();
		$params =& $this->getParams();
		$a = $params->get( 'allow_delete', 25 );
		if ($a == '29'|| $a == '' || $a == 0 ){
			$this->_access->delete =  true;
		} else {
			if (!is_object( $this->_access ) || !array_key_exists( 'delete', $this->_access )) {
				$groupNames = FabrikWorker::getACLGroups( $a );
				foreach ($groupNames as $name) {
					FabrikWorker::setACL( 'action', 'delete', 'fabrik', $name, 'components', null );
				}
				if ($acl->acl_check( 'action', 'delete', 'fabrik', $user->get( 'usertype' ), 'components', null )) {
					$this->_access->delete = true;
				} else {
					$this->_access->delete = false;
				}
			}
		}
		return $this->_access->delete;
	}

	/**
	 * checks user access for adding records
	 *
	 * @return bol access allowed
	 */

	function canAdd()
	{
		$user  	=& JFactory::getUser();
		$acl 		=& JFactory::getACL();
		$params =& $this->getParams();
		$table 	=& $this->getTable();
		$a = $params->get( 'allow_add', 25 );
		if ($a == '29' || $a == ''|| $a == 0){
			$this->_access->add =  true;
		} else {
			if (!is_object( $this->_access ) || !array_key_exists( 'add', $this->_access )) {
				$groupNames = FabrikWorker::getACLGroups( $a );
				foreach ($groupNames as $name) {
					FabrikWorker::setACL( 'action', 'add', 'fabrik', $name, 'components', null );
				}
				if ($acl->acl_check( 'action', 'add', 'fabrik', $user->get( 'usertype' ), 'components', null )) {
					$this->_access->add = true;
				} else {
					$this->_access->add = false;
				}
			}
		}
		return $this->_access->add;
	}

	/**
	 * check use can view the table
	 * @return bol can view or not
	 */

	function canView( ){
		$user  	=& JFactory::getUser();
		$acl 		=& JFactory::getACL();
		$table 	=& $this->getTable();
		if ($table->access == '29'|| $table->access == '' || $table->access == 0) {
			$this->_access->view =  true;
		} else {
			if (!is_object($this->_access) || !array_key_exists('view', $this->_access)) {
				$groupNames = FabrikWorker::getACLGroups( $table->access );
				foreach ($groupNames as $name) {
					FabrikWorker::setACL( 'action', 'view', 'fabrik', $name, 'components', null );
				}
				if ($acl->acl_check( 'action', 'view', 'fabrik', $user->get('usertype'), 'components', null )) {
					$this->_access->view =  true;
				} else {
					$this->_access->view = false;
				}
			}
		}
		return $this->_access->view;
	}

	/**
	 * load the table from the form_id value
	 * @param int $formId
	 * @return object table row
	 */

	function loadFromFormId( $formId ){
		JTable::addIncludePath( JPATH_ADMINISTRATOR.DS.'components'.DS.'com_fabrik'.DS.'table' );
		$row = JTable::getInstance( 'table', 'Table' );
		$origKey = $row->_tbl_key;
		$row->_tbl_key = "form_id";
		$row->load( $formId );
		$this->_table = $row;
		$row->_tbl_key = $origKey;
		$this->setId( $row->id );
		return $row;
	}

	/**
	 * alias to loadJoins - should be used instead
	 * @return array join objects (table rows - not table objects or models)
	 */

	function &getJoins()
	{
		if (!isset( $this->_aJoins )) {
			$form =& $this->getForm();
			$form->getGroupsHiarachy( false );
			$ids = $form->getElementIds();
			$table =& $this->getTable();
			$db =& JFactory::getDBO();
			$sql = "SELECT * FROM #__fabrik_joins WHERE table_id = '$this->_id'";
			if (!empty( $ids )) {
				$sql .= " OR element_id IN ( " . implode(", ", $ids) .")";
			}
			//maybe we will have to order by element_id asc to ensure that table joins are loaded
			//before element joins (if an element join is in a table join then its 'join_from_table' key needs to be updated
			$sql .= " ORDER BY id";
			$db->setQuery( $sql );
			$this->_aJoins = $db->loadObjectList( );
			if ($db->getErrorNum()) {
				JError::raiseError( 500, $db->stderr());
			}

			$aliases = array();
			$tableGroups = array();
			
			foreach ($this->_aJoins as $join) {
				//if their 'table_join' has been used then update with an alais
				if (in_array( $join->table_join, $aliases )) {
					$base = $join->table_join;
					$a = $base;
					$c = 0;
					while (in_array( $a, $aliases )) {
						$a = "{$base}_{$c}";
						$c ++;
					}
					$join->table_join_alias = $a;
				} else {
					$join->table_join_alias = $join->table_join;
				}
				$aliases[] = $join->table_join;

				//if they are element joins add in this tables name as the calling joining table.
				if ($join->join_from_table == '') {
					$join->join_from_table = $table->db_table_name;
				}
				
				// test case:
				/*
				 * you have a talbe that joins to a 2nd table
				 * in that 2nd table there is a database join element
				 * that 2nd elements key needs to point to the 2nd tables name and not the first
				 * 
				 * e.g. when you want to create a n-n relationship
				 * 
				 * events -> (table join) events_artists -> (element join) artist  
				 */
				
				$join->keytable = $join->join_from_table;
				if (!array_key_exists( $join->group_id, $tableGroups )) {
					$tableGroups[$join->group_id] = $join->table_join_alias;
				} else {
					if ($join->element_id != 0) {
						$join->keytable = $tableGroups[$join->group_id];
					}
				}
			}
		}
		return $this->_aJoins;
	}


	/**
	 * gets the field names for the given table
	 * @param string table name
	 * @return array table fields
	 */

	function getDBFields( $tbl = null )
	{
		if (is_null( $tbl )) {
			$table =& $this->getTable();
			$tbl = $table->db_table_name ;
		}
		if (!strstr( $tbl, '`' )) {
			$tbl = "`$tbl`";
		}
		if (!isset( $this->_dbFields[$tbl] ) ){
			$db =& $this->getDb();
			$db->setQuery( "DESCRIBE $tbl" );
			$this->_dbFields[$tbl] = $db->loadObjectList( );
		}
		return $this->_dbFields[$tbl];
	}

	/**
	 * add or update a database column via sql
	 * @param object element plugin
	 * @param bol is new
	 * @param string origional field name
	 */

	function alterStructure( &$elementModel, $new, $origColName=null )
	{
		$db =& JFactory::getDBO();
		$element =& $elementModel->getElement();
		$pluginManager =& JModel::getInstance( 'Pluginmanager', 'FabrikModel' );
		$basePlugIn =& $pluginManager->getPlugIn( $element->plugin, 'element' );
		$fbConfig = JComponentHelper::getParams( 'com_fabrik' );
		$fabrikDb =$this->getDb();
		$table =& $this->getTable();
		$tableName = $table->db_table_name;
		$objtype 		= $basePlugIn->getFieldDescription();
		$dbdescriptions = $this->getDBFields( $tableName );
		if (!$fbConfig->get( 'fbConf_alter_existing_db_cols' )) {
			foreach ($dbdescriptions as $f) {
				if ($f->Field == $origColName) {
					$objtype = $f->Type;
				}
			}
		}

		if (!is_null( $objtype )) {
			foreach ($dbdescriptions as $dbdescription) {
				$fieldname = $dbdescription->Field;
				$exitingfields[] = strtolower($fieldname);
			}
			$lastfield = $fieldname;
			if (in_array( strtolower($element->name), $exitingfields )) {
				return ;
			}

			FabrikString::safeColName( $element->name );
			FabrikString::safeColName( $tableName );
			FabrikString::safeColName( $lastfield );

			if (( $new && !in_array( strtolower($element->name), $exitingfields) ) || !in_array( strtolower($origColName), $exitingfields )) {
				$sql = "ALTER TABLE $tableName ADD COLUMN $element->name $objtype AFTER $lastfield";
			} else {
				if ($origColName == null) {
					$origColName = $element->name;
				}
				FabrikString::safeColName( $origColName );
				$sql = "ALTER TABLE $tableName CHANGE $origColName " . $element->name ;
				$sql .= " $objtype";
			}
			$fabrikDb->setQuery( $sql );
			if (!$fabrikDb->query( )) {
				return JError::raiseError( 500, 'alter structure: ' . $fabrikDb->getErrorMsg( ));
			}
			$this->createCacheQuery();
		}
		return true;
	}

	/**
	 * if not loaded this loads in the table's form object
	 * also binds a reference of the table to the form.
	 * @return object form model with form table loaded
	 */

	function &getForm()
	{
		if (is_null( $this->_oForm )) {
			$this->_oForm =& JModel::getInstance( 'Form', 'FabrikModel' );
			$table =& $this->getTable();
			$this->_oForm->setId( $table->form_id );
			$this->_oForm->getForm();
			$this->_oForm->setTableModel( $this );
			//ensure joins are loaded
		}
		return $this->_oForm;
	}

	/**
	 * tests if the table is in fact a view
	 * @returns true if table is a view
	 */

	function isView()
	{
		$params =& $this->getParams();
		$isview = $params->get('isview', null);

		if (is_null($isview)) {
			$db =& JFactory::getDBO();
			$table =& $this->getTable();
			$cn = $this->getConnection();
			$c = $cn->getConnection();
			$dbname = $c->database;
			$sql = "show table status like '$table->db_table_name'";
			$sql = " select table_name, table_type, engine from INFORMATION_SCHEMA.tables ".
			"where table_name = '$table->db_table_name' and table_type = 'view' and table_schema = '$dbname'";
			$db->setQuery( $sql );
			$row = $db->loadObjectList();
			$isview = empty($row) ? false : true;
			$intisview = $isview ? 1 : 0;
			//store and save param for following tests
			$params->set('isview', $isview);
			$table->attribs .= "\nisview=$intisview\n";
			$table->store();
		}
		return $isview;
	}

	/** DEPRECIATED SHOULD NOT BE NEEDED NOW
	 function _loadJoinOnce( $join )
	 {
		return $join->table_join;
		}
	 **/

	/**
	 * filter array is created in $this->_loadFilterArray
	 *@return array filters
	 */

	function &getFilterArray( )
	{
		if (isset( $this->_aFilter )) {
			return $this->_aFilter;
		}

		$user  					= &JFactory::getUser();
		$request 				= $this->getRequestData();
		$this->_aFilter = array();

		$this->_aPrefilters =& $this->getPrefilterArray();
		$form 					=& $this->getForm();

		$filterCondSQL	= '';
		$aPostFilters 	= array();
		$useFullName 		=  JRequest::getBool( 'fullName', 1 );

		if (is_null( $form->_groups )) {
			$form->getGroupsHiarachy();
		}
		foreach ($form->_groups as $groupModel) {
			$group =& $groupModel->getGroup();
			foreach ($groupModel->_aElements as $elementModel) {
				$element =& $elementModel->getElement();
				$thisData =  $this->_aData;
				if ($group->is_join) {
					if (@array_key_exists( 'join', $request ) && @is_array( $request['join'][$group->join_id] )) {
						$thisData = $this->_aData['join'][$group->join_id];
					}
					$key 	= $elementModel->getFilterFullName( false, true, false );
				} else {
					$key = $elementModel->getFilterFullName( true, true, false );
				}
				$dbKey = str_replace( "___", ".", $key );
				$dbKey = FabrikWorker::getDbSafeName( $dbKey );
				if (array_key_exists( $key, $request )) {
					$safeKey = FabrikWorker::getDbSafeName( $dbKey );
					$arr = $elementModel->getFilterConditionSQL( $request[$key], $this->_aFilter, $safeKey, $key );
					$arr['no-filter-setup'] = ($element->filter_type == '') ? 1 : 0;
					$arr['ellabel'] = $element->label;
					if (!empty( $arr['sqlCond'] )) {

						$sqlCond = "( " . $arr['sqlCond'] ;
						//check if the filter has a corresponding prefilter set.
						if (array_key_exists( $key, $this->_aPrefilters )) {
							$sqlCond .=  " AND " . $this->_aPrefilters[$key]['sqlCond'];
							unset( $this->_aPrefilters[$key] );
						}
						$sqlCond .= ")";
						$arr['sqlCond'] = $sqlCond;
						$arr['type'] = 'postfilter';
						//TODO: make this and/or choice somehow for use perhaps?
						$arr['concat'] = ' AND ';
						$arr['grouped_to_previous'] = false;
						$this->_aFilter[$key] = $arr;
					}
				}
			}
		}
		$this->_searchesNoFilterSetup = array();
		return $this->_aFilter;
	}

	/**
	 * creates array of prefilters
	 * @return array prefilter
	 */

	function &getPrefilterArray( )
	{
		$params =& $this->getParams();
		$afilterJoins 			= $params->get( 'filter-join', '', '_default', 'array' );
		$afilterFields 			= $params->get( 'filter-fields', '', '_default', 'array' );
		$afilterConditions 	= $params->get( 'filter-conditions', '', '_default', 'array' );
		$afilterValues 			= $params->get( 'filter-value', '', '_default', 'array' );
		$afilterAccess 			= $params->get( 'filter-access', '', '_default', 'array' );
		$afilterEval	 			= $params->get( 'filter-eval', '', '_default', 'array' );
		$afilterGrouped	 		= $params->get( 'filter-grouped', '', '_default', 'array' );
		$appliedPrefilters = 0;
		$selJoin = 'WHERE';
		$aReturn = array();
		$w = new FabrikWorker();
		for ( $i=0;$i<count( $afilterFields );$i++ ) {
			if (!array_key_exists(0, $afilterJoins) || $afilterJoins[0] == '' ){
				$afilterJoins[0] = 'and';
			}
			$selJoin 	  = $afilterJoins[$i];

			if (trim( strtolower( $selJoin ) ) == 'where' ) {
				$selJoin = "AND";
			}
			$selFilter 	  = $afilterFields[$i];
			$selCondition = $afilterConditions[$i];
			$selValue 	  = $afilterValues[$i];
			$filerEval	  = $afilterEval[$i]; // "" or "1"
			$filterGrouped= $afilterGrouped[$i];

			if ($filerEval == '1' ){
				$selValue 	= stripslashes( htmlspecialchars_decode( $selValue, ENT_QUOTES ) );
				$selValue 	  = eval( $selValue );
			}

			$selAccess 	  = $afilterAccess[$i];
			if (!$this->mustApplyFilter( $selAccess, $i )) {
				continue;
			}
			$selValue = $this->_prefilterParse( $selValue );

			$selValue 	  = $w->parseMessageForPlaceHolder( $selValue );

			//strip any quotes from the selValue (replaced back in the switch beneath)
			if (!in_array( $selCondition, array( '>',  '&gt;', ' > ', '<', '&lt;' ))) {
				$selValue = ltrim( $selValue, "'" );
				$selValue = rtrim( $selValue, "'" );
				$selValue = ltrim( $selValue, '"' );
				$selValue = rtrim( $selValue, '"' );
			}
			switch( $selCondition ){
				case 'notequals':
					$selCondition = " <> ";
					$selValue = "'$selValue'";
					break;
				case 'equals':
					$selCondition = " = ";
					$selValue = "'$selValue'";
					break;
				case 'begins':
					$selCondition = " LIKE ";
					//if its a quoted string put the % inside the quote
					if ($string{0} == '"' || $string{0} == '"') {
						$selValue = substr(0, -1) . "%" . $string{0};
					} else {
						$selValue = "'$selValue%'";
					}
					break;
				case 'ends':
					$selCondition = " LIKE ";
					if ($string{0} == '"' || $string{0} == '"') {
						$selValue = $string{0} . "%" .  substr(0, 1) ;
					} else {
						$selValue = "'%$selValue'";
					}
					break;
				case 'contains':
					$selCondition = " LIKE ";
					if ($string{0} == '"' || $string{0} == '"') {
						$selValue = $string{0} . "%" .  substr(0, 1) ;
					} else {
						$selValue = "'%$selValue%'";
					}
					break;
				case '>':
				case '&gt;':
					$selCondition = ' > ';
					break;
				case '<':
				case '&lt;':
					$selCondition = ' < ';
					break;
			}
				
			if ($selCondition == ' = ' && $selValue == "'_null_'") {
				$strCond = " $selFilter IS NULL ";
			} else {
				$strCond = " $selFilter $selCondition $selValue ";
			}
			$aReturn[] = array('type' => '', 'value' =>$selValue, 'filterVal' => $selValue,  'sqlCond' => $strCond, 'concat' => $selJoin,
				 'type' => 'prefilter', 'grouped_to_previous' => $filterGrouped);
			$appliedPrefilters ++;
		}
		return $aReturn;
	}

	/**
	 * get the total number of records in the table
	 * @return int total number of records
	 */

	function getTotalRecords( )
	{
		$db =& $this->getDb();
		$table = $this->getTable();
		$formModel =& $this->getForm();
		$this->getFormGroupElementData( );
		$count = "DISTINCT " . $table->db_primary_key;
		foreach ( $formModel->_groups as $groupModel ) {
			foreach ( $groupModel->_aElements as $elementModel ) {
				$element =& $elementModel->getElement();
				if ( $element->show_in_table_summary && $element->plugin == "fabrikDatabasejoin" ) {
					$count = "*";
				}
			}
		}
		$totalSql  	= "SELECT COUNT(" . $count . ") AS t FROM " . $table->db_table_name . " " . $this->_buildQueryJoin( );
		$totalSql 	.= " " . $this->_buildQueryWhere();
		$db->setQuery( $totalSql );
		$total  	= $db->loadResult( );
		return $total;
	}

	/**
	 * load in the elements for the table's form
	 * If no form loaded for the table object then one is loaded
	 * @param bol if true only those elements marked as 'show_in_table_view' are returned (leave null for default admin behaviour)
	 * @param bol if true only those elements that are published are returned
	 * @return array element objects
	 */

	function &getFormGroupElementData( $tableViewOnly = null, $excludeUnpublished = true )
	{
		$formModel =& $this->getForm( );
		if (is_null( $tableViewOnly )) {
			$tableViewOnly = true;
		}
			
		$k = $tableViewOnly.'.'.$excludeUnpublished;
		if (!array_key_exists( $k, $this->_formGroupElementData)) {
			$formModel->setTableModel( $this );
			$this->_formGroupElementData[$k] =& $formModel->getGroupsHiarachy( $tableViewOnly, $excludeUnpublished );
		}

		return $this->_formGroupElementData[$k];
	}

	/**
	 * require the correct pagenav class based on template
	 *
	 * @param int total
	 * @param int start
	 * @param int length
	 * @return object pageNav
	 */

	function &_getPagination( $total, $limitstart, $limit ){
		$o =& new FPagination( $total, $limitstart, $limit );
		$o->_postMethod = $this->_postMethod;
		return $o;
	}

	/**
	 * used to determine which filter action to use
	 *if a filter is a range then override tables setting with onsubmit
	 */

	function getFilterAction()
	{
		if (!isset( $this->_real_filter_action )) {
			$form =& $this->getForm();
			$table =& $this->getTable();
			$this->_real_filter_action = $table->filter_action;
			foreach ($form->_groups as $groupModel) {
				foreach ($groupModel->_aElements as $elementModel) {
					$element =& $elementModel->getElement();
					if ($element->filter_type <> '') {
						if ($elementModel->canView() && $elementModel->canUseFilter() && $element->show_in_table_summary == '1' ) {
							if($element->filter_type == 'range'){
								$this->_real_filter_action = 'submitform';
								return $this->_real_filter_action;
							}
						}
					}
				}
			}
		}
		return  $this->_real_filter_action;
	}

	/**
	 * gets the part of a url to describe the key that the link links to
	 * if a table this is rowid=x
	 * if a view this is view_primary_key={where statement}
	 *
	 * @param object row $data
	 * @return string
	 */

	function getKeyIndetifier( $data )
	{
		$table =& $this->getTable();
		if (!$this->isView()) {
			return "&rowid=$data->__pk_val";
		} else {
			$k = array();
			$fields = $this->_fetchFields();
			foreach ($fields as $f) {
				if ($f->primary_key == 1){
					$y = "{$f->table}___{$f->name}";
					$k[] = "{$f->table}.{$f->name} = " . $data->$y;
				}
			}
			$k = implode(' AND ', $k);
			$k = "&view_primary_key=" . urlencode($k);
			return $k;
		}
	}

	/**
	 * *get detailed info on each of the tables fields
	 *
	 * @return unknown
	 */

	function _fetchFields()
	{
		$table =& $this->getTable();
		$db =& $this->getDb();
		$db->setQuery("select * from $table->db_table_name limit 1");
		if (!($result = $db->query())) {
			return null;
		}
		$fields       = array();
		$num_fields   = mysql_num_fields($result);
		for ($i = 0; $i < $num_fields; $i++) {
			$fields[] = mysql_fetch_field($result, $i);
		}
		return $fields;
	}


	/**
	 * @return array of element objects that are database joins and that
	 * use this table's key as their foregin key
	 *
	 */

	function getJoinsToThisKey()
	{
		if (is_null( $this->_joinsToThisKey )) {
			$db =& JFactory::getDBO();
			$table =& $this->getTable();
			if ($table->id == 0 ) {
				$this->_joinsToThisKey = array();
			} else {
				$sql = "SELECT *, t.label AS tablelabel, t.db_table_name FROM #__fabrik_elements AS el " .
			  			"LEFT JOIN #__fabrik_formgroup AS fg 
						ON fg.group_id = el.group_id 
						LEFT JOIN #__fabrik_forms AS f 
						ON f.id = fg.form_id 
						LEFT JOIN #__fabrik_tables AS t 
						ON t.form_id = f.id " .
			  			"WHERE " . 
			  			" plugin = 'fabrikDatabasejoin' AND" .
			  			" el.attribs like '%join_db_name=".$table->db_table_name."%' " .
			  			"AND el.attribs like  '%join_conn_id=".$table->connection_id."%' ";
					
				$db->setQuery( $sql );
				$this->_joinsToThisKey = $db->loadObjectList( );
			}
		}
		return $this->_joinsToThisKey;
	}

	/**
	 * get an array of elements that point to a form where their data will be filtered
	 * @return array
	 */

	function getLinksToThisKey()
	{
		$params =& $this->getParams();
		$aExisitngLinked = $params->get( 'linkedform', '', '_default', 'array' );
		$aAllJoinsToThisKey = $this->getJoinsToThisKey( );
		$aJoinsToThisKey= array();
		foreach ( $aAllJoinsToThisKey as $join ) {
			if ( in_array( $join->db_table_name, $aExisitngLinked )){
				$aJoinsToThisKey[] = $join;
			} else {
				$aJoinsToThisKey[] = '';
			}
		}
		return $aJoinsToThisKey;
	}

	/**
	 * creates an array of html code for each filter
	 * @param object database
	 * @param string table name
	 * @param array current filter states
	 * @return array of html code for each filter
	 */

	function &makeFilters( )
	{
		$aFilters = array();
		$this->getFilterArray( );
		$form =& $this->getForm();
		$table =& $this->getTable();
		foreach ($form->_groups as $k => $val) {
			$groupModel =& $form->_groups[$k];
			foreach ($groupModel->_aElements as $kk => $val2) {
				$elementModel = $groupModel->_aElements[$kk]; //dont do with =& as this foobars up the last elementMOdel
				$element =& $elementModel->getElement();

				if ($element->filter_type <> '') {
					if ($elementModel->canView() && $elementModel->canUseFilter() && $element->show_in_table_summary == '1' ) {
						//force the correct group model into the element model to ensure no wierdness in getting the element name
						$elementModel->_group =& $groupModel;
						$aFilters[$element->label] = $elementModel->getFilter( );
					}
				}
			}
		}

		//check for search form filters - if they exists create hidden elements for them
		foreach( $this->_aFilter as $key=>$f) {
			if ($f['no-filter-setup']) {
				$name = str_replace('.', '___', $key) . "[value]";
				$aFilters[$f['ellabel']] = "<input class='fabrik_filter' type='hidden' name='$name' value='" . $f['filterVal'] . "' />" . $f['filterVal'];
			}
		}

		//new moved advanced filters to table settings
		$params =& $this->getParams();
		if ($params->get( 'advanced-filter', '0' )) {
			$fieldNames[] = JHTML::_( 'select.option', '', JText::_( 'Please select' ) );
			$longLabel = false;
			foreach ($form->_groups as $groupModel) {
				$group =& $groupModel->getGroup();
				if ($group->is_join) {
					$longLabel = true;
				}
			}
			foreach ($form->_groups as $groupModel) {
				foreach ($groupModel->_aElements as $elementModel) {
					$element =& $elementModel->getElement();
					$elParams =& $elementModel->getParams();
					if ($elParams->get('inc_in_adv_search', 1)) {
						$elName = $elementModel->getFullName( false, false, false );
						//@TODO add a switch to use $elName as the label??
						$l =  $longLabel ? $elName : $element->label;
						$fieldNames[] = JHTML::_( 'select.option', $elName, $l );
					}
				}
			}
			$fields = FastJSON::encode($fieldNames);
			//todo: make database join elements filtereable on all their join table's fields

			$advancedFilters = JRequest::getVar( 'advancedFilterContainer', array(), 'default', 'none', 2 );
			$document =& JFactory::getDocument();
			FabrikHelperHTML::mocha( 'a.popupwin' );
			$url = '#';
			$advancedSearch = '<a rel="{\'id\':\'advanced-search-win\',\'width\':690,\'loadMethod\':\'html\', \'title\':\'' .  JText::_('Advanced search') . '\', \'maximizable\':\'1\',\'contentType\':\'html\'}" href="'. $url.'" class="popupwin">'. JText::_('Advanced search') .'</a>';
			$aWords = (array_key_exists( 'value', $advancedFilters )) ? explode( " ", $advancedFilters['value'] )  : array('');
			if (!($aWords[0] == "AND" || $aWords[0] == "OR" )) {
				array_unshift( $aWords, "AND" );
			}

			$aGroupedAdvFilters = array();
			$g = 0;
			$tmp = array();
			foreach ($aWords as $word) {
				$tmp[] = $word;
				if ($g == 3) {
					$aGroupedAdvFilters[] = $tmp;
					$tmp = array();
					$g = 0;
				} else {
					$g ++;
				}
			}
			$searchOpts = "$fields, ";
			$searchOpts .= empty($aGroupedAdvFilters) ? 'true' : 'false';
			$searchOpts .= ( $table->filter_action != 'submitform') ? ', true' : ', false';
			$searchOpts .=",{
			tableid:'$table->id'
		}";
		$script = "mochaSearch.conf($searchOpts);";
			
		foreach ($aGroupedAdvFilters as $bits) {
			$selJoin  = $bits[0];
			if ($bits[0] != "AND" && $bits[0] != "OR") {
				array_unshift($bits, "WHERE");
			}
			$selJoin 			= $bits[0];
			$selFilter 		= $bits[1];
			$selCondition = $bits[2];
			$selValue 		= $bits[3];

			switch( $selCondition )
			{
				case "<>":
					$jsSel = 'NOT EQUALS';
					break;
				case "=":
					$jsSel = 'EQUALS';
					break;
				case "<":
					$jsSel = 'LESS THAN';
					break;
				case ">":
					$jsSel = 'GREATER THAN';
					break;
				default:
					$firstChar = substr( $selValue, 1, 1 );
					$lastChar = substr( $selValue, -2, 1 );
					switch( $firstChar )
					{
						case "%":
							$jsSel =( $lastChar == "%")? 'CONTAINS' : $jsSel = 'ENDS WITH';
							break;
						default:
							if( $lastChar == "%"){
								$jsSel = 'BEGINS WITH';
							}
							break;
					}
					break;
			}
			$selValue = trim( trim( $selValue, '"' ), "%" );
			$script .= "\n mochaSearch.addFilterOption('$selJoin', '$selFilter', '$jsSel', '$selValue');\n";
		}
		$document->addScriptDeclaration( $script );
		$aFilters['']  = $advancedSearch . "<input type='hidden' name=\"advancedFilterContainer[value]\" value='' id=\"advancedFilterContainer\" />";
		}
		return $aFilters;
	}

	/**
	 * returns the table headings, seperated from writetable function as
	 * when group_by is selected mutliple tables are written
	 * @return array (table headings, array columns, $aLinkElements)
	 */

	function _getTableHeadings( )
	{
		global $mainframe;
		$table =& $this->getTable();
		$aLinkElements 				= array();
		$aNamedTableHeadings 	= array( );
		$aTableHeadings 			= array();
		$aCols 								= array();

		$session =& JFactory::getSession();
		//$session->destroy();
		$formModel =& $this->getForm();
		foreach ($formModel->_groups as $groupModel) {
			foreach ($groupModel->_aElements as $key => $elementModel) {
				$viewLinkAdded = false;
				$element =& $elementModel->getElement();
				if (!$element->show_in_table_summary) {
					continue;
				}

				//hide any elements that should not be access by the user for their group id
				if (!$elementModel->canView( )) {
					continue;
				}
				$key 		= $elementModel->getFullName( false, true, false );
				$orderKey 	= $elementModel->getOrderbyFullName( false, false );
				$aCols[$key] = '';
				if ($element->link_to_detail == '1') {
					$aLinkElements[] = $key;
				}
				if ($element->can_order == '1' && $this->_outPutFormat != 'csv' ) {
					$context			= 'com_fabrik.table.' . $this->_id . '.order.' . $key;
					$orderDir			= $session->get( $context );
					$class 		= "";
					$currentOrderDir = $orderDir;
					switch ($orderDir) {
						case "desc":
							$orderDir = "-";
							$class = "class='fabrikorder-desc'";
							break;
						case "asc":
							$orderDir = "desc";
							$class = "class='fabrikorder-asc'";
							break;
						case "":
						case "-":
							$orderDir = "asc";
							$class = "class='fabrikorder'";
							break;
					}

					if ($class == '') {
						if ($table->order_by == $key) {
							if (strtolower( $table->order_dir ) == 'desc') {
								$class = "class='fabrikorder-desc'";
							}
						}
					}
					$orderjs = "oPackage.fabrikNavOrder( $table->id, ";
					$heading = "<a $class href='#'>$element->label</a>";
				} else {
					$heading = $element->label;
				}
				$aTableHeadings[$key] = $heading;
				$aNamedTableHeadings[$key . "_heading"] = $heading;
			}
		}
		if ( $this->_outPutFormat != 'pdf' && $this->_outPutFormat != 'csv' ){
			if ($this->canDelete()) {
				$delete = '<label><input type="checkbox" id="table_' . $this->_id . '_checkAll" />' . JText::_('Delete') . "</label>";
				$aTableHeadings['fabrik_delete'] = $delete;
				$aNamedTableHeadings['fabrik_delete_heading'] = $delete;
			}
			//if no elements linking to the edit form add in a edit column (only if we have the right to edit/view of course!)
			if (empty( $aLinkElements ) and ($this->canView() || $this->canEdit())) {
				if ($this->canEdit()) {
					$aTableHeadings['fabrik_edit'] = JText::_('Edit');
					$aNamedTableHeadings['fabrik_edit_heading'] = JText::_('Edit');
				} else {
					if ($this->canViewDetails()) {
						$aTableHeadings['fabrik_edit'] = 'View';
						$viewLinkAdded = true;
						$aNamedTableHeadings['fabrik_edit_heading'] = JText::_('View');
					}
				}
			}
			if ($this->canViewDetails() && $this->_params->get( 'detaillink' ) == '1' && !$viewLinkAdded ) {
				$aTableHeadings['__details_link'] = JText::_( 'View' );
				$aNamedTableHeadings['__details_link'] = JText::_( 'View' );
			}

			// create columns containing links which point to tables associated with this table
			$params =& $this->getParams();
			$aExisitngLinkedTables = $params->get('linkedtable', '', '_default', 'array');
			$aExistingTableHeaders = $params->get( 'linkedtableheader', '', '_default', 'array' );
			$joinsToThisKey = $this->getJoinsToThisKey( );
			$f = 0;
			foreach ($joinsToThisKey as $element) {
				if (is_object( $element )) {
					$linkedTable 	= array_key_exists( $f, $aExisitngLinkedTables ) ? $aExisitngLinkedTables[$f] : false;
					$heading = array_key_exists( $f, $aExistingTableHeaders ) ? $aExistingTableHeaders[$f] : false;
					if ($linkedTable != '0') {
						$prefix 	= ( $element->tablelabel == $table->label ) ? $table->db_primary_key : $element->db_table_name . "___" . $element->name;
						$aTableHeadings[$prefix . "_table_heading"] = empty($heading) ? $element->tablelabel . " " . JText::_('Table') : $heading;
						$aNamedTableHeadings[ $prefix . "_table_heading"] = empty($heading) ? $element->tablelabel . " " . JText::_('Table') : $heading;
					}
				}
				$f ++;
			}
			$linksToForms =  $this->getLinksToThisKey( );
			$aExisitngLinkedForms = $params->get( 'linkedform', '', '_default', 'array' );
			$aExistingFormHeaders 	= $params->get( 'linkedformheader', '_default', '', 'array' );

			$f = 0;
			foreach ($linksToForms as $element ) {
				$linkedForm 	= array_key_exists( $f, $aExisitngLinkedForms ) ? $aExisitngLinkedForms[$f] : false;
				if ($linkedForm != '0') {
					$heading = array_key_exists( $f, $aExistingFormHeaders ) ? $aExistingFormHeaders[$f] : '';
					$prefix	= $element->db_table_name . "___" . $element->name;
					$aTableHeadings[$prefix . "_form_heading"] = empty($heading) ? $element->tablelabel . " " . JText::_('Form') : $heading;
					$aNamedTableHeadings[ $prefix . "_form_heading"] = empty($heading) ? $element->tablelabel . " " . JText::_('Form') : $heading ;
				}
				$f ++;
			}
		}
		return array( $aTableHeadings, $aCols, $aLinkElements, $aNamedTableHeadings );
	}

	/**
	 * return mathematical column calculations (run at doCalculations() on for submission)
	 */

	function getCalculations( ){
		$user  = &JFactory::getUser();
		$aCalculations = array();
		$formModel =& $this->getForm();
		$aAvgs = array();
		$aSums = array();
		$aMedians = array();
		$aCounts = array();
		if ( !is_array($formModel->_groups ) ){
			$formModel->getGroupsHiarachy( );
		}
		if ( is_array( $formModel->_groups ) ){
			foreach ($formModel->_groups as $groupModel ){
				if ( is_array( $groupModel->_aElements ) ){
					foreach( $groupModel->_aElements as $elementModel ){
						$params = $elementModel->getParams();
						$elName = $elementModel->getFullName( false, true, false );
						$sum 			= $params->get( 'sum_on', '0' );
						$avg 			= $params->get( 'avg_on', '0' );
						$median 		= $params->get( 'median_on', '0' );
						$countOn 		= $params->get( 'count_on', '0' );

						$sumAccess 		= $params->get( 'sum_access', 0 );
						$avgAccess 		= $params->get( 'avg_access', 0 );
						$medianAccess 	= $params->get( 'median_access', 0 );
						$countAccess 	= $params->get( 'count_access', 0 );

						if ($sumAccess <= $user->get('gid') && $params->get('sum_value', '') != ''){
							$aSums[ $elName ] = $params->get('sum_value', '');
							$aSums[ $elName . "_obj" ] = unserialize($params->get('sum_value_serialized'));
						}

						if ($avgAccess <= $user->get('gid') && $params->get('avg_value', '') != ''){
							$aAvgs[ $elName ] = $params->get('avg_value', '');
							$aSums[ $elName . "_obj" ] = unserialize($params->get('avg_value_serialized'));
						}
						if ($medianAccess <= $user->get('gid') && $params->get('median_value', '') != ''){
							$aMedians[ $elName ] = $params->get('median_value', '');
						}
						if ($countAccess <= $user->get('gid') && $params->get('count_value', '') != ''){
							$aCounts[ $elName ] = $params->get('count_value', '');
							$aSums[ $elName . "_obj" ] = unserialize($params->get('count_value_serialized'));
						}
					}
				}
			}
		}
		$aCalculations['sums'] 			= $aSums;
		$aCalculations['avgs'] 			= $aAvgs;
		$aCalculations['medians'] 		= $aMedians;
		$aCalculations['count'] 		= $aCounts;
		$this->_aRunCalculations =& $aCalculations;
		return $aCalculations;
	}

	/**
	 * get table headings to pass into table js oject
	 *
	 * @return string headings tablename___name
	 */

	function _jsonHeadings()
	{
		$aHeadings = array();
		$table =& $this->getTable();
		$formModel =& $this->getForm();
		foreach ($formModel->_groups as $groupModel) {
			foreach ($groupModel->_aElements as $elementModel) {
				$element =& $elementModel->getElement();
				if ($element->show_in_table_summary) {
					$aHeadings[] = $table->db_table_name . '___' . $element->name;
				}
			}
		}
		return "['" . implode("','", $aHeadings) . "']";
	}

	/**
	 * when form saved (and set to record in database)
	 * this is run to see if there is any table join data, if there is it stores it in $this->_joinsToProcess
	 *
	 * @param array form post data
	 * @param int row id (of main database table atm - need to test this out)
	 * @param string to split up table___col data with
	 * @return array [joinid] = array(join, group array);
	 */

	function preProcessJoin( $aData, $rowId, $split='___' )
	{
		$this->_joinsToProcess = array( );
		$formModel = $this->getForm( );
		if (!is_array( $formModel->_groups )) {
			$formModel->getGroupsHiarachy( );
		}
		$aJoinGroups = array( );
		foreach ($formModel->_groups as $groupModel) {
			$group =& $groupModel->getGroup();
			if ($group->is_join) {
				$joinModel =& JModel::getInstance( 'Join', 'FabrikModel' );
				$join = $joinModel->loadFromGroupId( $group->id, $this->_id );
				if (!array_key_exists( $join->id, $aJoinGroups )) {
					$aJoinGroups[$join->id] = array("join"=>$join, "groups"=>array($groupModel));
				} else {
					$aJoinGroups[$join->id]["groups"][] = $groupModel;
				}
			}
		}
		return $aJoinGroups;
	}

	/**
	 * check to see if a table exists
	 * @param string name of table (ovewrites form_id val to test)
	 * @param object database that contains the table if null then default $db object used
	 * @return boolean false if no table fodund true if table found
	 */

	function databaseTableExists( $tableName = null, $fabrikDatabase = null ){
		$db =& $this->getDb();
		if ($tableName === '') {
			return false;
		}
		$table =& $this->getTable();
		if (is_null( $tableName )) {
			$tableName = $table->db_table_name;
		}
		$sql = "SHOW TABLES LIKE '$tableName'";
		/* use the default Joomla database if no table database specified */
		if (is_null( $fabrikDatabase ) || !is_object( $fabrikDatabase )) {
			$fabrikDatabase = $this->getDb();
		}
		$fabrikDatabase->setQuery( $sql );
		$total = $fabrikDatabase->loadResult();
		if ($total == "") {
			return false;
		} else {
			return true;
		}
	}

	/**
	 * strip the table names from the front of the key
	 * @param array data to strip
	 * @return array stripped data
	 */

	function removeTableNameFromSaveData( $data, $split='___' ){
		foreach ($data as $key=>$val) {
			$akey = explode( $split, $key);
			if (count($akey) > 1) {
				$newKey = $akey[1];
				unset($data[$key]);
			} else {
				$newKey = $akey[0];
			}
			$data[$newKey] = $val;
		}
		return $data;
	}

	/**
	 * saves posted form data into a table, you can supply table name etc to override the objects
	 * variables, this is needed to allow form class to use this object to save to a database table
	 * that doesnt have a table view associated with it
	 * @param array data to save
	 * @param int row id to edit/updated
	 * @param bol is the data being saved into a join table
	 * @param string table name (optional - if not supplied uses the objects db_table_name
	 * @return bol true if saved ok
	 */

	function storeRow( $data, $rowId, $isJoin = false, $table = null )
	{
		//dont save a record if no data collected
		if ($isJoin && implode( $data ) == '') {
			return;
		}
		$fabrikDb 	=& $this->getDb();

		if (is_null( $table )) {
			$table 		= $this->_table->db_table_name;
		}
		FabrikString::safeColName( $table );

		$formModel =& $this->getForm();
		if ($isJoin) {
			$this->getFormGroupElementData( false, false );
		}
		$oRecord = new stdClass();
		$noRepeatFields = array();
		$c = 0;
		foreach ($formModel->_groups as $groupModel) {
			$group =& $groupModel->getGroup();

			if (($isJoin && $group->is_join) || (!$isJoin && !$group->is_join)) {
				foreach ($groupModel->_aElements as $elementModel) {
					$element = $elementModel->getElement();
					$key = $element->name;
					if ($elementModel->recordInDatabase( $data )) {
						if (array_key_exists( $key, $data ) && !in_array( $key, $noRepeatFields )) {
							$noRepeatFields[] = $key;
							$lastKey = $key;
							$oRecord->$key = $elementModel->storeDatabaseFormat( $data[$key], $data, $key );
							$c++;
						}
					}
				}
			}
		}
		$primaryKey = $this->_shortKey();

		if ($rowId != '' && $c == 1 && $lastKey == $primaryKey) {
			return;
		}
		if($isJoin){
		//	echo trim( $primaryKey, '`' ). " $table ";print_r($oRecord);exit;
		}
		if ($rowId == '' || $rowId == 0) {
			$ok = $fabrikDb->insertObject( $table, $oRecord, trim( $primaryKey, '`' ), false );
		} else {
			$ok = $fabrikDb->updateObject( $table, $oRecord, trim( $primaryKey, '`' ), false );
		}
		$this->_tmpSQL = $fabrikDb->getQuery();
		if (!$ok) {
			return JError::raiseWarning( 500, 'Store row failed: ' . $fabrikDb->getQuery() ) ;
		} else {
			$this->_lastInsertId = $fabrikDb->insertid() ;
			return true;
		}
	}


	/**
	 * get the short version of the primary key, e.g if key = table.key return "key"
	 *
	 * @return string short version of the primary key
	 */

	function _shortKey( $key = null, $removeQuotes = false ){
		if (is_null( $key )) {
			$key = $this->_table->db_primary_key;
		}
		if (strstr( $key, "." )) {
			$bits = explode( ".", $key );
			$key = array_pop( $bits );
		}
		if($removeQuotes){
			$key = str_replace("`", "", $key );
		}
		return $key;
	}

	/**
	 * called when the form is submitted to perform calculations
	 */

	function doCalculations()
	{
		$db =& JFactory::getDBO();
		$formModel =& $this->getForm();
		if (!is_array( $formModel->_groups )) {
			$formModel->getGroupsHiarachy( );
		}
		foreach ($formModel->_groups as $groupModel) {
			foreach ($groupModel->_aElements as $elementModel) {
				$params =& $elementModel->getParams();
				$update = false;
				if ($params->get( 'sum_on', 0 )) {
					$aSumCals = $elementModel->sum( $this );
					$params->set( 'sum_value_serialized', serialize( $aSumCals[1] ) );
					$params->set( 'sum_value', $aSumCals[0] );
					$update = true;
				}
				if ($params->get( 'avg_on', 0 )) {
					$aAvgCals = $elementModel->avg( $this );
					$params->set( 'avg_value_serialized', serialize( $aAvgCals[1] ) );
					$params->set( 'avg_value', $aAvgCals[0] );
					$update = true;
				}
				if ($params->get( 'median_on', 0 )) {
					$params->set( 'median_value', $this->median( $elementModel->median( $this ) ));
					$update = true;
				}
				if ($params->get( 'count_on', 0 )) {
					$aCountCals = $elementModel->avg( $this );
					$params->set( 'count_value_serialized', serialize( $aCountCals[1] ) );
					$params->set( 'count_value', $aCountCals[0] );
					$update = true;
				}
				if ($update) {
					$element =& $elementModel->getElement();
					$element->attribs = $params->updateAttribsFromParams( $params->toArray() );
					$element->store();
				}
			}
		}
	}

	/**
	 * calculate the median of an array of data values
	 * @param array data to use
	 * @return int median value
	 */

	function median( $aData ){
		sort( $aData );
		if ((count( $aData ) % 2)==1){
			/* odd */
			$midKey = floor( count( $aData ) / 2);
			return $aData[$midKey];
		} else {
			$midKey = floor( count( $aData ) / 2) - 1;
			$midKey2 = floor( count( $aData ) / 2) ;
			return ($aData[$midKey] + $aData[$midKey2]) / 2;
		}
	}


	/**
	 * check to see if prefilter should be applied
	 * Kind of an inverse access lookup
	 * @param int group id to check against
	 * @param string ref for filter
	 * @return bol must apply filter - true, ignore filter (user has enough access rights) false;
	 */

	function mustApplyFilter( $gid, $ref )
	{
		$user  	=& JFactory::getUser();
		$acl 		=& JFactory::getACL();
		if ($user->get( 'gid' ) == 0 || $gid == 100 ) {
			return true;
		}
		$groupNames = FabrikWorker::getACLGroups( $gid, '<=' );
		foreach ($groupNames as $name) {
			FabrikWorker::setACL( 'action', 'prefilter' . $ref, 'fabrik', $name, 'components', null );
		}
		if ($acl->acl_check( 'action', 'prefilter' . $ref, 'fabrik', $user->get('usertype'), 'components', null )) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * set the connection id - used when creating a new table
	 * @param int connection id
	 */

	function setConnectionId( $id )
	{
		$this->_table->connection_id = $id;
	}

	/**
	 * run each time a table is saved (plus when an element is saved )
	 * builds the sql query that the table getData function will use
	 * stores it in the cache/fabrik folder
	 * retrieved by getData when table rendered
	 */

	function createCacheQuery()
	{
		$q = $this->_buildQuery();
		$cache = &JFactory::getCache( 'com_fabrik' );
		$cache->store( $q, 'tablequery' . $this->_id );
	}

	/**
	 * return the default set of attributes when creating a new
	 * fabrik table
	 *
	 * @return string attributes
	 */

	function getDefaultAttribs()
	{
		return "detaillink=0
empty_data_msg=
advanced-filter=0
show-table-nav=1
pdf=
rss=0
feed_title=
feed_date=
rsslimit=150
rsslimitmax=2500
csv_import_frontend=0
csv_export_frontend=0
csvfullname=0
access=0
allow_view_details=0
allow_edit_details=0
allow_add=0
allow_delete=0
group_by_order=
group_by_order_dir=ASC
prefilter_query="; 
	}

	/**
	 * save the table from admin
	 *
	 * @return bol true if saved ok
	 */

	function save( )
	{
		$db 		=& JFactory::getDBO();
		$user 	=& JFactory::getUser();
		$config =& JFactory::getConfig();
		$id 		= JRequest::getInt( 'id', 0, 'post' );
		$this->setId( $id );
		$row =& $this->getTable();
		$formModel 	=& JModel::getInstance( 'Form', 'FabrikModel' );

		$post	= JRequest::get( 'post' );
		if (!$row->bind( $post )) {
			return JError::raiseWarning( 500, $row->getError() );
		}

		$filter	= new JFilterInput( null, null, 1, 1 );
		$introduction = JRequest::getVar( 'introduction', '', 'post', 'string', JREQUEST_ALLOWRAW );
		$row->introduction =$filter->clean( $introduction );

		$details	= JRequest::getVar( 'details', array(), 'post', 'array' );
		$row->bind( $details );

		if ($id == 0) {
			$newtable = trim( JRequest::getVar( '_database_name', '', 'post' ));

			//check the entered database table doesnt already exist
			if ($newtable != '' && $this->databaseTableExists( $newtable )) {
				return JError::raiseWarning( 500, JText::_( 'Database table already exists' ) );
			}

			//create fabrik form
			$formModel =& $this->_createLinkedForm( );

			//create fabrik group
			$groupData = array( "name" => $row->label , "label" => $row->label );

			JRequest::setVar( '_createGroup', 1, 'post' );
			$groupId = $this->_createLinkedGroup( $groupData, false );

			if ($newtable != '') {
				$connectionModel =& JModel::getInstance( 'Connection', 'FabrikModel' );
				$connectionModel->setId( JRequest::getInt( 'connection_id', -1, 'post' ) );

				$fabrikDb =& $connectionModel->getDb();
				//update some default table values
				$row->db_table_name = $newtable;
				$row->db_primary_key = "`".$newtable.'`.`fabrik_internal_id`';
				$row->auto_inc = 1;

				$this->createDBTable( $formModel, $newtable, $fabrikDb );

			} else {
				//new fabrik table but existing db table
				$this->_createLinkedElements( $groupId, $post );

			}
			//	set the tables form id
			$this->_updateFormId( $formModel->_form->id );
		}

		// 	save params - this file no longer exists? do we use models/table.xml instead??
		$params = new fabrikParams( $row->attribs, JPATH_COMPONENT.DS.'xml'.DS.'table.xml' );

		$row->attribs = $params->updateAttribsFromParams( JRequest::getVar( 'params', array(), 'post', 'array' ) );
		$row->rows_per_page 	= JRequest::getInt( 'rows_per_page', 10, 'post' );
		$row->auto_inc		 		= JRequest::getInt( 'auto_inc', 1, 'post' );

		FabrikHelperFabrik::publishDown( $row->publish_down );

		$pk = JRequest::getVar( 'db_primary_key' );
		if ($pk == '') {
			$aKey = $this->getPrimaryKeyAndExtra();
			$row->db_primary_key = "`".$row->db_table_name . "`.`" . $aKey['colname'] . "`";
		}
		if (!strstr( $row->order_by, $row->db_table_name . "___" )) {
			$row->order_by = $row->db_table_name . "___" . $row->order_by;
		}
		if (!$row->store( )) {
			return JError::raiseWarning( 500, $row->getError() );
		}

		// load in all the tables data - even if it wasnt in the post data
		$table =& $this->getTable();
		//needed if saving a table for first time (otherwise id = 0)
		$this->setId( $table->id );
		$this->updateJoins( );
		if (!$this->isView()) {
			// this was only run on a new table - but I've put it here so that if you upload a new table you can ensure that its columns are fixed
			$this->makeSafeTableColumns( );
			$this->updatePrimaryKey( $row->db_primary_key, $row->auto_inc );
		}
		$row->checkin( );

		$this->createCacheQuery();
		return true;
	}

	/**
	 * when saving a table that links to a database for the first time we
	 * automatically create a form to allow the update/creation of that tables
	 * records
	 * @access private
	 * @return object form model
	 */

	function &_createLinkedForm( )
	{
		$config		=& JFactory::getConfig();
		$user			=& JFactory::getUser();

		jimport('joomla.utilities.date');
		$createdate =& JFactory::getDate();
		$createdate = $createdate->toMySQL();

		$form = JTable::getInstance( 'Form', 'Table' );
		$table =& $this->getTable();
		$form->label							= $table->label;
		$form->record_in_database = 1;

		$form->created 						= $createdate;
		$form->created_by 				= $user->get( 'id' );
		$form->created_by_alias 	= $user->get( 'username' );
		$form->error							= JText::_( 'Some parts of your form have not been fully filled in, please check the specific error messages below and try again' );
		// @TODO : fabrik : 0.5 : create a overall fabrik params admin page
		//which sets the default settings for all forms, from which these settings below can be populated
		$form->submit_button_label 	= JText::_( 'Save' );
		$form->state							= $table->state;
		$form->form_template			= 'default';
		$form->view_only_template	= 'default';

		if(! $form->store() ){
			return JError::raiseError( 500, $form->getError() );
		}

		$this->_oForm =& JModel::getInstance( 'Form', 'FabrikModel' );
		$this->_oForm->setId( $form->id );
		$this->_oForm->getForm();
		return $this->_oForm;
	}

	/**
	 * create a group
	 * used when creating a fabrik table from an existing db table
	 *
	 * NEW also creates the formgroup
	 *
	 * @access private
	 * @param array group data
	 * @param bol is the group a join default false
	 * @return int group id
	 */

	function _createLinkedGroup( $data, $isJoin = false ) {
		$user =& JFactory::getUser();
		//jimport( 'joomla.utilities.date' );
		$createdate = JFactory::getDate();
		$createdate = $createdate->toMySQL();

		$group 										=& JTable::getInstance( 'Group', 'Table' );
		$group->bind( $data );
		$group->created 					= $createdate;
		$group->created_by 				= $user->get('id');
		$group->created_by_alias 	= $user->get('username');
		$group->state 						= 1;
		$group->attribs = "repeat_group_button=0
repeat_group_show_first=1
repeat_group_js_add=
repeat_group_js_delete=";
		$group->is_join = ($isJoin == true) ? 1 : 0;

		$group->store( );
		if (!$group->store()) {
			JError::raiseError( 500, $group->getError() );
		}


		//create form group
		$formid = $this->_oForm->_id;
		$formGroup = JTable::GetInstance( 'FormGroup', 'Table' );
		$formGroup->form_id = $formid;
		$formGroup->group_id = $group->id;
		$formGroup->ordering = 999999;
		if (!$formGroup->store()) {
			JError::raiseError( 500, $formGroup->getError() );
		}

		$formGroup->reorder( " form_id = '$formid'" );
		return $group->id;
	}

	/**
	 * Create a table to store the forms' data depending upon what groups are assigned to the form
	 * @param object form model
	 * @param string table name - taken from the table oject linked to the form
	 * @param obj tables database object
	 */

	function createDBTable( &$formModel, $dbTableName = null, $tableDatabase = null ){
		$db 		=& JFactory::getDBO();

		if (is_null( $tableDatabase )) {
			$tableDatabase = $db;
		}
		$user  	=& JFactory::getUser();
		$config =& JFactory::getConfig();
		if (is_null( $dbTableName )) {
			$dbTableName = $this->_table->db_table_name;
		}
		$sql = "CREATE TABLE `$dbTableName` ( " ;

		$db->setQuery( "SELECT group_id FROM #__fabrik_formgroup WHERE form_id = $formModel->_id" );
		$groupIds = $db->loadResultArray( );
		/* create elements for the internal id and time_date fields */
		$element =& JTable::getInstance( 'Element', 'Table' );
		$element->name			= "fabrik_internal_id";
		$element->label			= "id";
		$element->plugin 		= 'fabrikinternalid';
		$element->hidden 		= 1;
		$element->group_id 	= $groupIds[0];
		$element->primary_key		= 1;
		$element->auto_increment	= 1;
		$element->created 			= JHTML::_( 'date', date( '%Y-%m-%d %H:%M:%S' ), '%Y-%m-%d %H:%M:%S', - $config->getValue( 'offset' ) );
		$element->created_by 		= $user->get( 'id' );
		$element->created_by_alias = $user->get( 'username' );
		$element->state 			= '1';
		$element->show_in_table_summary = '1';
		$element->link_to_detail = '1';
		$element->width 	= '30';
		$element->ordering 		= 0;
		if (!$element->store()) {
			return JError::raiseWarning( 500, $element->getError() );
		}
		$element =& JTable::getInstance( 'Element', 'Table' );
		$element->name		= "time_date";
		$element->label		= "time_date";
		$element->plugin 	= 'fabrikdate';
		$element->hidden 			= 1;
		$element->eval				= 1;
		$element->default			= "return date('Y-m-d h:i:s');";
		$element->group_id 		= $groupIds[0];
		$element->primary_key		= 0;
		$element->auto_increment	= 0;
		$element->created 			= JHTML::_( 'date', date( '%Y-%m-%d %H:%M:%S' ), '%Y-%m-%d %H:%M:%S', - $config->getValue( 'offset' ) );
		$element->created_by 		= $user->get( 'id' );
		$element->created_by_alias = $user->get( 'username' );
		$element->state 			= '1';
		$element->show_in_table_summary = '1';
		$element->width 	= '30';
		$element->ordering 		= 1;
		if (!$element->store( )) {
			return JError::raiseWarning( 500, $element->getError() );
		}
		$formModel->_loadGroupIds();
		$aGroups = $formModel->getGroupsHiarachy( false, false );
		$arAddedObj = array();
		//these two should be real elements not hacked in here
		$pluginManager =& JModel::getInstance('Pluginmanager', 'FabrikModel');
		foreach ($aGroups as $groupModel) {
			foreach ($groupModel->_aElements as $obj) {
				$element = $obj->getElement( );
				/* replace all non alphanumeric characters with _ */
				$objname = preg_replace( "/[^A-Za-z0-9]/", "_", $element->name );
				/* any elements that are names the same (eg radio buttons) can not be entered twice into the database */
				if (!in_array( $objname, $arAddedObj )) {
					$arAddedObj[] = $objname;
					$objtype = $obj->getFieldDescription();
					if ($objname != "" && !is_null( $objtype )) {
						if (stristr( $objtype, 'not null')) {
							$sql .= " `$objname` $objtype, ";
						} else {
							$sql .= " `$objname` $objtype null, ";
						}
					}
				}
			}
		}
		$sql .= " primary key (fabrik_internal_id))";
		$tableDatabase->setQuery( $sql );
		if (!$tableDatabase->query()) {
			return JError::raiseWarning( 500, $db->getErrorMsg() );
		}
	}

	/**
	 * when saving a table that links to a database for the first time we
	 * need to create all the elements based on the database table fields and their
	 * column type
	 *
	 * @access private
	 * @param int group id
	 * @param array newly created table data
	 * @param array of element objects - if this is not empty then we've come from the csv import and the elements
	 * have already been defined, use this instead of the field analysis to create correctly typed elements
	 */

	function _createLinkedElements( $groupId, $aTableData, $aSpecificElements = array() )
	{
		$db 			=& JFactory::getDBO();
		$user  		= &JFactory::getUser();
		$config		=& JFactory::getConfig();
		$createdate = JFactory::getDate();
		$createdate = $createdate->toMySQL();

		$tableName = JRequest::getVar( 'db_table_name' );

		$ordering = 0;
		$fabrikDb =& $this->getDb();

		if (!empty( $aSpecificElements )) {
			//we're asking the method to create predefined elements - e.g. when installing sample data.
			foreach ($aSpecificElements as $elementModel) {
				$element =& $element->getElement();
				if ($element->label == 'id' || $element->label == 'fabrik_internal_id') {
					$element->hidden = '1';
					$element->primary_key = '1';
					$element->auto_increment = '1';
				} else {
					$element->hidden = '0';
				}
				$element->name = strtolower(str_replace(' ', '', $element->name ));
				$element->group_id = $groupId;
				$element->created = $createdate;
				$element->created_by = $user->get('id');
				$element->created_by_alias = $user->get('username');
				$element->state = '1';
				$element->show_in_table_summary = '1';
				$element->width = '30';
				$element->height = '6';
				$element->ordering = 99999;
				$element->store( );
				$where = " group_id = '" . $element->group_id . "'";
				$element->updateOrder( $where );
			}
		} else {
			//here we're importing directly from the database schema
			$db->setQuery( "SELECT id FROM #__fabrik_tables WHERE `db_table_name` = '$tableName'" );
			$id = $db->loadResult();
			if ($id) {
				//a fabrik table already exists - so we can copy the formatting of its elements

				$groupTableModel = JModel::getInstance('table', 'FabrikModel' );

				$groupTableModel->setId($id);
				$table = $groupTableModel->getTable();
				//$this->_oForm = null; //reset form so that it loads new table form
				$groups = $groupTableModel->getFormGroupElementData( false, false );
				foreach ($groups as $groupModel) {
					foreach ($groupModel->_aElements as $elementModel) {
						$element =& $elementModel->getElement();
						$copy = $elementModel->copyRow( $element->id, '', $groupId );
					}
				}
			} else {
				$fields = $fabrikDb->getTableFields( array ("`$tableName`") );
				$fields = $fields["`$tableName`"];
				// no existing fabrik table so we take a guess at the most
				//relavent element types to  create
				foreach ($fields as $label => $type) {
					$element =& JTable::getInstance( 'Element', 'Table' );
					$element->label = str_replace( "_", " ", $label );
					switch ( $type )
					{
						case "int" :
						case "tinyint" :
						case "varchar" :
							$plugin = 'fabrikfield';
							break;
						case "text" :
						case "tinytext" :
						case "mediumtext" :
						case "longtext" :
							$plugin = 'fabriktextarea';
							break;
						case "datetime" :
						case "date" :
						case "time" :
						case "timestamp" :
							$plugin = 'fabrikdate';
							break;
						default :
							$plugin = 'fabrikfield';
							break;
					}
					$element->plugin = $plugin;
					$element->hidden = ( $element->label == 'id' )? '1' : '0';
					$element->group_id 			= $groupId;
					$element->name				 		= $label;
					$element->created 				= JHTML::_('date', date('%Y-%m-%d %H:%M:%S'), '%Y-%m-%d %H:%M:%S', - $config->getValue('offset'));
					$element->created_by 			= $user->get('id');
					$element->created_by_alias 	= $user->get('username');
					$element->state 				= '1';
					$element->show_in_table_summary = '1';
					$element->width 		= ($plugin == 'fabriktextarea') ? '60' : '30';
					$element->height 		= '6';
					$element->ordering 		= $ordering;
					if (!$element->store( )) {
						return JError::raiseError( 500, $element->getError() );
					}
					$ordering ++;
				}
			}
		}
	}

	/**
	 * updates the table record to point to the newly created form
	 * @params int form id
	 */

	function _updateFormId( $formId )
	{
		$db =& JFactory::getDBO();
		$table =& $this->getTable();
		$table->form_id = $formId;
		if (!$table->store()) {
			return JError::raiseWarning( 500, $db->getError() );
		}
	}


	/**
	 * get the tables primary key and if the primary key is auto increment
	 * @return mixed if ok returns array (key, extra, type, name) otherwise
	 * returns false
	 */

	function getPrimaryKeyAndExtra( )
	{
		$origColNames = $this->getDBFields( );
		if (is_array( $origColNames )) {
			foreach ($origColNames as $origColName) {
				$colName 	= $origColName->Field;
				$key 			= $origColName->Key;
				$extra 		= $origColName->Extra;
				$type 		= $origColName->Type;
				if ($key == "PRI") {
					return array( "key"=>$key, "extra"=>$extra, "type"=>$type, "colname"=>$colName );
				}
			}
		}
		return false;
	}

	/**
	 * deals with ensuring joins are managed correctly when table is saved
	 */

	function updateJoins( )
	{
		$db =& JFactory::getDBO();
		// $$$rob getJoins adds in element joins as well - dont use as we can get ids to delete that
		//arent table joins
		//$aOldJoins 			= $this->getJoins( );
		$db->setQuery("SELECT * FROM #__fabrik_joins WHERE table_id = '$this->_id'");
		$aOldJoins = $db->loadObjectList( );

		$aOldJoinsToKeep 	= array( );
		$joinModel			=& JModel::getInstance( 'Join', 'FabrikModel' );
		$joinIds 				= JRequest::getVar( 'join_id', array(), 'post' );
		$joinTypes 			= JRequest::getVar( 'join_type', array(), 'post' );
		$joinTableFrom  = JRequest::getVar( 'join_from_table', array(), 'post' );
		$joinTable 			= JRequest::getVar( 'table_join', array(), 'post' );
		$tableKey				= JRequest::getVar( 'table_key', array(), 'post' );
		$joinTableKey		= JRequest::getVar( 'table_join_key', array(), 'post' );
		$groupIds				= JRequest::getVar( 'group_id', array(), 'post' );

		for ($i = 0;$i < count( $joinTypes );$i++) {
			$existingJoin = false;
			foreach ($aOldJoins as $oOldJoin ){
				if ($joinIds[$i] == $oOldJoin->id) {
					$existingJoin = true;
				}
			}
			if (!$existingJoin) {
				$this->_makeNewJoin( $tableKey[$i], $joinTableKey[$i], $joinTypes[$i], $joinTable[$i], $joinTableFrom[$i] );
			} else {
				/* load in the exisitng join
				 * if the table_join has changed we need to create a new join and mark the loaded one as to be deleted
				 */
				$joinModel->setId( $joinIds[$i] );
				$join =& $joinModel->getJoin();
				if ($join->table_join != $joinTable[$i]) {
					$this->_makeNewJoin( $tableKey[$i], $joinTableKey[$i], $joinTypes[$i], $joinTable[$i], $joinTableFrom[$i] );
				} else {
					//the talbe_join has stayed the same so we simply update the join info
					$join->table_key 		= $tableKey[$i];
					$join->table_join_key 	= $joinTableKey[$i];
					$join->join_type 		= $joinTypes[$i];
					$join->store();
					$aOldJoinsToKeep[] 		= $joinIds[$i];
				}
			}
		}
		/* remove non exisiting joins */
		if (is_array( $aOldJoins )) {
			foreach ($aOldJoins as $oOldJoin) {
				if (!in_array( $oOldJoin->id, $aOldJoinsToKeep )) {
					/* delete join */
					$join =& JTable::getInstance( 'Join', 'Table' );
					$joinModel->setId( $oOldJoin->id );
					$joinModel->getJoin();
					$joinModel->deleteAll( $oOldJoin->group_id );
				}
			}
		}
	}

	/**
	 * run the prefilter sql and replace any placeholders in the subsequent prefilter
	 *
	 * @param string prefilter condition
	 * @return string prefilter condition
	 */

	function _prefilterParse( $selValue )
	{
		$preSQL = htmlspecialchars_decode( $this->_params->get('prefilter_query'), ENT_QUOTES );
		if (trim( $preSQL ) != '') {
			$db =& JFactory::getDBO();
			$w = new FabrikWorker();
			$preSQL = $w->parseMessageForPlaceHolder( $preSQL );
			$db->setQuery( $preSQL );
			$q = $db->loadObject( );
		}
		if (isset( $q )) {
			foreach ( $q as $key=>$val ) {
				if (substr( $key, 0, 1 ) != '_') {
					$found = false;
					if (strstr( $selValue, '{$q-&gt;'. $key )) {
						$found = true;
						$pattern = '{$q-&gt;'. $key. "}";
					}
					if (strstr( $selValue, '{$q->' . $key )) {
						$found = true;
						$pattern = '{$q->'. $key . "}";
					}
					if ($found) {
						$selValue = str_replace( $pattern, $val, $selValue );
					}
				}
			}
		} else {
			//parse for default values only
			$pattern = "/({[^}]+}).*}?/s";
			$ok = preg_match( $pattern, $selValue, $matches );
			foreach ($matches as $match) {
				$matchx = substr( $match, 1, strlen( $match ) - 2 );
				//a default option was set so lets use that
				if (strstr( $matchx, '|' )) {
					$bits = explode( '|', $matchx );
					$selValue = str_replace( $match, $bits[1], $selValue );
				}
			}
		}
		return $selValue;
	}

	/**
	 * replaces the table column names with a safer name - ie removes white
	 * space and none alpha numeric characters
	 */

	function makeSafeTableColumns()
	{
		$db = $this->getDb();
		$table =& $this->getTable();
		$origColNames = $this->getDBFields( );
		foreach ($origColNames as $origColName) {
			$colName = strtolower($origColName->Field);
			$type = $origColName->Type;
			$newColName = preg_replace("/[^A-Za-z0-9]/", "_", $colName);
			if ( $colName != $newColName ) {
				$sql = "ALTER TABLE `$table->db_table_name` CHANGE `$colName` `$newColName` $type";
				$db->setQuery( $sql );
				if (!$db->query( )) {
					JError::raiseWarning( 500, $db->getErrorMsg( ));
				}
			}
		}
	}

	/**
	 * adds a primary key to the database table
	 * @param string the column name to make into the primary key
	 * @param bol is the column an auto incrementing number
	 * @param string column type definition (eg varchar(255))
	 */

	function updatePrimaryKey( $fieldName, $autoIncrement, $type = 'int(11)' )
	{
		$fbConfig =& JComponentHelper::getParams( 'com_fabrik' );
		if (!$fbConfig->get( 'fbConf_alter_existing_db_cols' )) {
			$fabrikDatabase =& $this->getDb( );
			$aPriKey = $this->getPrimaryKeyAndExtra( );
			if (!$aPriKey) { // no primary key set so we should set it
				$this->_addKey( $fieldName, $autoIncrement, $type );
			} else {
				$shortKey = $this->_shortKey( $fieldName );
				if ($fieldName !=  $aPriKey['colname'] && $shortKey != $aPriKey['colname']) {
					$this->_dropKey( $aPriKey ); // primary key already exists so we should drop it
					$this->_addKey( $fieldName, $autoIncrement, $aPriKey['type'] );
				} else {
					//update the key
					$this->_updateKey( $fieldName, $autoIncrement, $type );
				}
			}
		}
	}

	/**
	 * internal function: update an exisitng key in the table
	 * @param string primary key column name
	 * @param bol is the column auto incrementing
	 * @param string the primary keys column type
	 */

	function _updateKey( $fieldName, $autoIncrement, $type = "INT(11)" ){
		$db =& $this->getDb();
		if(strstr($fieldName, '.')){
			$fieldName = array_pop(explode(".", $fieldName));
		}
		$table =& $this->getTable();
		$sql = "ALTER TABLE `$table->db_table_name` CHANGE `$fieldName` `$fieldName` $type NOT NULL ";
		/* update primary key */
		if ( $autoIncrement ){
			$sql .= " AUTO_INCREMENT";
		}
		$db->setQuery( $sql );
		if(!$db->query( )){
			JError::raiseWarning( 500, 'update key:'.$db->getErrorMsg( ));
		}
	}


	/**
	 * internal function: add a key to the table
	 * @param string primary key column name
	 * @param bol is the column auto incrementing
	 * @param string the primary keys column type
	 */

	function _addKey( $fieldName, $autoIncrement, $type = "INT(11)" ){
		$db =& $this->getDb();
		$table =& $this->getTable();
		if(strstr($fieldName, '.')){
			$fieldName = array_pop(explode(".", $fieldName));
		}
		$sql = "ALTER TABLE `$table->db_table_name` ADD PRIMARY KEY ($fieldName)";
		/* add a primary key */
		$db->setQuery( $sql );
		if(!$db->query( )){
			JError::raiseWarning(500, $db->getErrorMsg( ));
		}
		if ( $autoIncrement ){
			$sql = "ALTER TABLE `$table->db_table_name` CHANGE $fieldName $fieldName " . $type .  " NOT NULL AUTO_INCREMENT"; //add the autoinc
			$db->setQuery( $sql );
			if (!$db->query( ) ){
				return JError::raiseError( 500, 'add key: ' . $db->getErrorMsg( ));
			}
		}
	}


	/**
	 * internal function: drop the table's key
	 * @param array existing key data
	 * @param object fabrik database object
	 * @return bol true if ke droped
	 */

	function _dropKey( $aPriKey, $fabrikDatabase ){
		$db = $this->getDb();
		$table =& $this->getTable();
		$sql = "ALTER TABLE `$table->db_table_name` CHANGE `" . $aPriKey['colname'] . '` `'. $aPriKey['colname'] . '` '  . $aPriKey['type'] . " NOT NULL";
		/* removes the autoinc */
		$db->setQuery( $sql );
		if(!$db->query( )){
			JError::raiseWarning( 500, $db->getErrorMsg()) ;
			return false;
		}
		$sql = "ALTER TABLE `$table->db_table_name` DROP PRIMARY KEY";
		/* drops the primary key */
		$db->setQuery( $sql );
		if(!$db->query( )){
			JError::raiseWarning( 500, 'alter table: ' . $db->getErrorMsg()) ;
			return false;
		}
		return true;
	}

	/**
	 * deletes records from a table
	 * @param string key value to delete
	 * @param string key to use (leave empty to default to the table's key)
	 * @return string error message
	 */

	function deleteTableRows( $val, $key = '' )
	{
		$table =& $this->getTable();
		$db =& $this->getDb();


		if ($key == '') {
			$key = $table->db_primary_key;
			if ($key == '') {
				return JError::raisWarning( JText::_( "no key found for this table" ));
			}
		}

		if (is_array( $val )) {
			$c = count($val);
			$val = implode("','", $val);
		} else {
			$c = 1;
		}

		//@todo: run element->onDeleteRow()
		//load in rows to be deleted#
		$this->_pageNav =& $this->_getPagination( $c, 0, $c );
		$this->_whereSQL[true] = " WHERE " . $key . " IN ('" . $val . "')";
		$rows 	= $this->getData( );
		$this->_rowsToDelete =& $rows;
		$groups = $this->getFormGroupElementData( false, false );

		foreach ($groups as $group) {
			foreach ($group->_aElements as $element) {
				$element->onDeleteRows( $rows );
			}
		}
		
		$pluginManager =& JModel::getInstance( 'Pluginmanager', 'FabrikModel' );
		$pluginManager->loadPlugInGroup( 'table' );
		if (!$pluginManager->runPlugins( 'onDeleteRows', $this, 'table' )) {
			return;
		}
		
		$sql = 	"DELETE FROM `" .  $table->db_table_name  . "` WHERE " . $key . " IN ('" . $val . "')";
		$db->setQuery( $sql );
		if ( !$db->query( ) ) {
			return JError::raisWarning( $db->getErrorMsg( ) );
		}
		return true;
	}

	/**
	 * remove all records from the table
	 */

	function dropData()
	{
		$db =& $this->getDb();
		$sql = "DELETE FROM " .  $this->db_table_name;
		$db->setQuery( $sql );
		$msg= '';
		if (!$db->query( )) {
			return JError::raisWarning( JText::_($db->getErrorMsg( ) ));
		}
		return '';
	}

	/**
	 * drop the table containing the fabriktables data
	 */

	function drop()
	{
		$db =& $this->getDb();
		$sql = "DROP TABLE IF EXISTS `$this->db_table_name`;";
		$db->setQuery( $sql );
		if ( !$db->query( ) ) {
			return JError::raisWarning( JText::_($db->getErrorMsg( ) ));
		}
		return '';
	}
	
	function truncate()
	{
		$db =& $this->getDb();
		$table =& $this->getTable();
		$db->setQuery( "TRUNCATE `$table->db_table_name`");
		$db->query();
	}

	/**
	 *  new join make the group, group elements and formgroup entries for the join data
	 * @param string table key
	 * @param string join to table key
	 * @param string join type
	 * @param string join to table
	 * @param string join table
	 */

	function _makeNewJoin( $tableKey, $joinTableKey, $joinType, $joinTable, $joinTableFrom )
	{
		$db 	=& JFactory::getDBO();
		$formModel 	=& $this->getForm( );
		$aData = array(
			"name" => $this->_table->label ."- [" .$joinTable. "]",
			"label" =>  $joinTable,
		);
		$groupId = $this->_createLinkedGroup( $aData, true );

		$origTable = JRequest::getVar( 'db_table_name' );
		JRequest::setVar( 'db_table_name', $joinTable );
		$this->_createLinkedElements( $groupId, array() );
		JRequest::setVar('db_table_name', $origTable);
		$join =& JTable::getInstance( 'Join', 'Table');
		$join->table_id 		= $this->_id;
		$join->join_from_table = $joinTableFrom;
		$join->table_join 		= $joinTable;
		$join->table_join_key 	= $joinTableKey;
		$join->table_key 		= $tableKey;
		$join->join_type 		= $joinTypes;
		$join->group_id 		= $groupId;
		if(!$join->store( )){
			return JError::raiseWarning( 500, $join->getError() );
		}
	}


	/**
	 * Alter the forms' data collection table when the forms' groups and/or
	 * elements are altered
	 * @param object form
	 * @param string table name
	 * @param object database connection object
	 */

	function ammendTable( &$oForm, $tableName = null, $tableDatabase = null ){
		$db 		=& JFactory::getDBO();
		$user  	=& JFactory::getUser();
		$table =& $this->getTable();
		$pluginManager =& JModel::getInstance( 'Pluginmanager', 'FabrikModel' );
		$ammend 		= false;
		if (is_null( $tableName )) {
			$tableName = $table->db_table_name;
		}
		if (is_null( $tableDatabase ) || !is_object( $tableDatabase )) {
			$tableDatabase = $db;
		}
		$dbdescriptions = $this->getDBFields( $tableName );
		//@TODO: test (was strtolower($dbdescription->Field))  if this is going to cause issues, think fields should be case insenitvely compared as some joomla core fields are mixed case
		foreach ($dbdescriptions as $dbdescription) {
			$exitingfields[] = ($dbdescription->Field);
		}
		$lastfield = $exitingfields[count($exitingfields)-1];
		$sql = "ALTER TABLE `$tableName` " ;
		if (!isset( $_POST['current_groups_str'] )) {
			/* get a list of groups used by the form */
			$groupsql = "SELECT group_id FROM #__fabrik_formgroup WHERE form_id = '" . $oForm->_id . "'";
			$db->setQuery( $groupsql );
			$groups = $db->loadObjectList( );
			if (!$groups) {
				JError::raiseWarning(500,  'ammendTable: ' . $fabrikDb->getErrorMsg( ) );
			}
			$arGroups = array( );
			foreach ($groups as $g) {
				$arGroups[] = $g->group_id;
			}
		} else {
			$current_groups_str = JRequest::getVar( 'current_groups_str' );

			$arGroups = explode( ",", $current_groups_str );
		}

		$arAddedObj = array();
		foreach ($arGroups as $group_id) {
			$group = JTable::getInstance( 'Group', 'Table' );
			$group->load( $group_id );
			if ($group->is_join == '0'){
				$groupsql = "SELECT * FROM #__fabrik_elements WHERE group_id = '$group_id'";
				$db->setQuery( $groupsql );
				$elements = $db->loadObjectList( );
				foreach ($elements as $obj) {
					$objname = strtolower(preg_replace("/[^A-Za-z0-9]/", "_", $obj->name));
					/* replace all non alphanumeric characters with _*/
					if (!in_array( $objname, $exitingfields )) {
						/* make sure that the object is not already in the table*/
						if(!in_array( $objname, $arAddedObj )) {
							/* any elements that are names the same (eg radio buttons) can not be entered twice into the database*/
							$arAddedObj[] 		= $objname;
							$objtypeid 				= $obj->plugin;
							$pluginClassName 	= $obj->plugin;
							$plugin 					= $pluginManager->loadPlugIn( $pluginClassName, 'element' );
							$objtype 					= $plugin->getFieldDescription();
							if ($objname != "" && !is_null( $objtype )) {
								$ammend = true;
								$sql .= ", ADD COLUMN `$objname` $objtype null AFTER `$lastfield`";
							}
						}
					}
				}
			}
		}
		if ($ammend) {
			$tableDatabase->setQuery( $sql );
			if (!$tableDatabase->query()) {
				return JError::raiseWarning( 500, 'amend table: ' . $tableDatabase->getErrorMsg() ) ;
			}
			$this->createCacheQuery();
		}
	}

	/**
	 * @param int connection id to use
	 * @param string table to load fields for
	 * @param string show "please select" top option
	 * @param bol append field name values with table name
	 * @param string name of drop down
	 * @param string selected option
	 * @param string class name
	 * @return string html to be added to DOM
	 */

	function getFieldsDropDown( $cnnId, $tbl, $incSelect, $incTableName = false, $selectListName = 'order_by', $selected = null, $className = "inputbox" ){
		$this->setConnectionId( $cnnId );
		$aFields = $this->getDBFields( $tbl );
		$fieldNames = array( );
		if ($incSelect != '') {
			$fieldNames[] = JHTML::_('select.option', '', $incSelect );
		}
		if (is_array( $aFields ) ) {
			foreach( $aFields as $oField ) {
				if ($incTableName ){
					$fieldNames[] = JHTML::_('select.option', $tbl . "___" . $oField->Field, $oField->Field );
				} else {
					$fieldNames[] = JHTML::_('select.option', $oField->Field );
				}
			}
		}
		$fieldDropDown = JHTML::_('select.genericlist',  $fieldNames, $selectListName, "class=\"$className\"  size=\"1\" ", 'value', 'text', $selected );
		return str_replace("\n", "", $fieldDropDown);
	}

	/**
	 * used in advanced search
	 * @param bol add slashes to reutrn data
	 */

	function getFilterJoinDd( $addSlashes = true, $name = 'join' ){
		$aConditions = array( );
		$aConditions[] = JHTML::_('select.option', 'AND' );
		$aConditions[] = JHTML::_('select.option', 'OR' );
		$dd = str_replace("\n", "", JHTML::_('select.genericlist',  $aConditions, $name, "class=\"inputbox\"  size=\"1\" ", 'value', 'text', '' ));
		if ($addSlashes ){
			$dd = addslashes( $dd );
		}
		return $dd;
	}

	/**
	 *used in advanced search
	 *@param bol add slashes to reutrn data
	 *@param string name of the drop down
	 *@param int mode - states what values get put into drop down
	 */

	function getFilterConditionDd( $addSlashes = true, $name = 'conditions', $mode = 1 ){
		$aConditions = array( );
		switch ($mode){
			case 1:
				/* used for search filter */
				$aConditions[] = JHTML::_('select.option', '<>', 'NOT EQUALS' );
				$aConditions[] = JHTML::_('select.option', '=', 'EQUALS' );
				$aConditions[] = JHTML::_('select.option', 'like', 'BEGINS WITH' );
				$aConditions[] = JHTML::_('select.option', 'like', 'CONTAINS' );
				$aConditions[] = JHTML::_('select.option', 'like', 'ENDS WITH' );
				$aConditions[] = JHTML::_('select.option', '>', 'GREATER THAN' );
				$aConditions[] = JHTML::_('select.option', '<', 'LESS THAN' );
				break;
			case 2:
				/* used for prefilter */
				$aConditions[] = JHTML::_('select.option', 'equals', 'EQUALS' );
				$aConditions[] = JHTML::_('select.option', 'notequals', 'NOT EQUAL TO' );
				$aConditions[] = JHTML::_('select.option', 'begins', 'BEGINS WITH' );
				$aConditions[] = JHTML::_('select.option', 'contains', 'CONTAINS' );
				$aConditions[] = JHTML::_('select.option', 'ends', 'ENDS WITH' );
				$aConditions[] = JHTML::_('select.option', '>', 'GREATER THAN' );
				$aConditions[] = JHTML::_('select.option', '<', 'LESS THAN' );
				$aConditions[] = JHTML::_('select.option', 'IS NULL', 'IS NULL' );
				break;
		}
		$dd = str_replace("\n", "", JHTML::_('select.genericlist',  $aConditions, $name, "class=\"inputbox\"  size=\"1\" ", 'value', 'text', '' ));
		if ($addSlashes ) {
			$dd = addslashes( $dd );
		}
		return $dd;
	}

	/**
	 * create the RSS href link to go in the table template
	 * @return string RSS link
	 */

	function getRSSFeedLink( )
	{
		global $Itemid;
		$link = '';
		if ($this->_params->get('rss') == '1' ) {
			$link = 'index.php?option=com_fabrik&view=table&format=feed&tableid=' . $this->_id . '&Itemid=' . $Itemid . '&type=rss';
			if (!$this->_admin) {
				$link = JRoute::_( $link );
			}
		}
		return $link;
	}

	/**
	 * iterates through string to replace every
	 * {placeholder} with row data
	 * (added by hugh, does the same thing as parseMessageForPlaceHolder in parent
	 * class, but for rows instead of forms)
	 * @param string text to parse
	 */
	 
	function parseMessageForRowHolder( $msg, $row ){
		$this->_aRow = $row;
		$msg = FabrikWorker::_replaceWithUserData( $msg );
		$msg = FabrikWorker::_replaceWithGlobals( $msg );
		$msg = preg_replace( "/{}/", "", $msg ); 
		/* replace {element name} with form data */
		$msg = preg_replace_callback( "/{[^}]+}/i", array($this,'_replaceWithRowData'), $msg ); 
		return $msg;
	}

	/**
	 * PRVIATE:
	 * called from parseMessageForRowHolder to iterate through string to replace
	 * {placeholder} with row data
	 * @param string placeholder e.g. {placeholder}
	 * @param array row
	 * @return string posted data that corresponds with placeholder
	 */
	 
	function _replaceWithRowData( $matches ){
		$match = $matches[0];
		/* strip the {} */
		$match = substr( $match, 1, strlen($match) - 2 ); 

		$match = strtolower( $match );
		$match = str_replace('.','___',$match);
		// $$$ hugh - allow use of {$rowpk} or {rowpk} to mean the rowid of the rwo within a table
		if ($match == 'rowpk' or $match == '$rowpk')
		{
			$match = $this->_table->db_primary_key;
			$match = preg_replace('#^(\w+\.)#','',$match);
		}
		$match = preg_replace( "/ /", "_", $match );
		return($this->_aRow[$match]);
	}
	
	/**
	 * get the link to view the records details
	 * @param int record id
	 * @param int record cursor
	 */

	function viewDetailsLink( $id, $cursor = 0 )
	{
		global $Itemid;
		$link = '';
		if($this->canViewDetails()){
			//TODO: test this in admin table & front end
			$table =& $this->getTable();
			$action = $this->_admin ? 'task' : 'view';
			$link = 'index.php?option=com_fabrik&c=form&'.$action.'=details&tableid=' . $this->_id . '&fabrik=' . $table->form_id . '&rowid=' . $id . '&Itemid=' . $Itemid;
			if (!$this->_admin) {
				$link = JRoute::_( $link );
			}
			$link = "<a href=\"$link\">" . JText::_('View') . "</a>";
		}
		return $link;
	}


	/**
	 * make the drop slq statement for the table
	 * @return string drop table sql
	 */

	function getDropTableSQL()
	{
		$genTable 	= $this->getGenericTableName( );
		$sql = "DROP TABLE IF EXISTS `$genTable`;";
		return $sql;
	}

	function getGenericTableName()
	{
		global $mainframe;
		$table = $this->getTable();
		return str_replace( $mainframe->getCfg('dbprefix'), '#__', $table->db_table_name );
	}

	/**
	 * make the create sql statement for the table
	 * @return string sql to drop & or create table
	 */

	function getCreateTableSQL( )
	{
		$table 	= $this->getGenericTableName( );
		$fields 	=  $this->getDBFields( );
		$primaryKey = "";
		$sql 		= "";
		FabrikString::safeColName( $table );
		if (is_array( $fields )) {
			$sql .= "CREATE table " .  $table ." (\n";
			foreach ( $fields as $field ) {
				FabrikString::safeColName( $field->Field );
				if ($field->Key == 'PRI'){
					$primaryKey = "PRIMARY KEY ($field->Field)";
				}

				$sql .=	"$field->Field ";
					
				if ($field->Key == 'PRI'){
					$sql .= ' INT(6) ';
				} else {
					$sql .= ' ' . $field->Type . ' ';
				}
				if ($field->Null == '' ) {
					$sql .= " NOT NULL ";
				}
				if ($field->Default != '' && $field->Key != 'PRI' ) {
					if($field->Default == 'CURRENT_TIMESTAMP'){
						$sql .= "DEFAULT $field->Default";
					} else {
						$sql .= "DEFAULT '$field->Default'";
					}
				}
				if ($field->Key == 'PRI') {
					$sql .= " AUTO_INCREMENT ";
				}

				$sql .= $field->Extra . ",\n";
			}
			if ($primaryKey == '') {
				$sql = rtrim($sql,",\n" );
			}
			$sql .= $primaryKey . ");";
		}
		return $sql;
	}

	/**
	 * make the create sql statement for inserting the table data
	 * used in package export
	 * @param object exporter
	 * @return string sql to drop & or create table
	 */

	function getInsertRowsSQL( $oExporter )
	{
		@set_time_limit(300);
		$table =& $this->getTable();
		$memoryLimit = ini_get( 'memory_limit' );
		$db 		=& $this->getDb();
		//dont load in all the table data as on large tables this gives a memory error
		//in fact this wasnt the problem, but rather the $sql var becomes too large to hold in memory
		//going to try saving to a file on the server and then compressing that and sending it as a header for download

		$db->setQuery( "SELECT $table->db_primary_key FROM `$table->db_table_name`" );
		$keys = $db->loadResultArray( );
		$sql = "";
		$dump_buffer_len = 0;
		if (is_array( $keys )) {
			foreach ($keys as $id) {
				$db->setQuery( "SELECT * FROM `$table->db_table_name` WHERE $table->db_primary_key = $id" );
				$row = $db->loadObject();
				$fmtsql = "\t<query>INSERT INTO $table->db_table_name ( %s ) VALUES ( %s )</query>";
				$values = array();
				$fields = array();
				foreach ( $row as $k => $v) {
					$fields[] = $db->NameQuote( $k );
					$values[] = $db->Quote( $v );
				}
				$sql .= sprintf( $fmtsql, implode( ",", $fields ) ,  implode( ",", $values ) );
				$sql .= "\n";

				$dump_buffer_len += strlen( $sql );
				if ($dump_buffer_len  > $memoryLimit) {
					$oExporter->writeExportBuffer( $sql );
					$sql = "";
					$dump_buffer_len = 0;
				}
				unset( $values );
				unset( $fmtsql );
			}
		}
		$oExporter->writeExportBuffer( $sql );
	}

	/**
	 * records data from a fabrik RSS feed into the table
	 * feeds need to be in <dl> format
	 * @param string html containing <dl> list
	 * @return string out come message can be'saved','error saving',return 'duplicate', 'error'
	 */

	function recordFromRSSFeed( $html )
	{
		$table =& $this->getTable();
		$db =& JFactory::getDBO();
		require_once(JPATH_SITE . '/includes/domit/xml_domit_lite_include.php');
		//strip cdata text from html
		$html = str_replace( array(']]>', '<![CDATA['), '', $html );

		$xmlDoc = new DOMIT_Lite_Document();
		$ok = $xmlDoc->parseXML($html);
		$aData = array('test');
		if ($ok ){
			$key = '';
			$val = '';
			$debug = '';
			$aPseudoKeys = array();
			$isPseudoKey = false;
			$numDefinitionLists = count($xmlDoc->documentElement->childNodes);

			for ( $i = 0; $i < $numDefinitionLists; $i++ ) {
				$currentChannel =& $xmlDoc->documentElement->childNodes[$i];

				if (($i % 2) == 0){ // even so its the col heading
					$label = $currentChannel->firstChild->nodeValue;
					$key = $currentChannel->getAttribute("value");
					$isPseudoKey = $currentChannel->getAttribute("key");

				} else {
					$val = $currentChannel->firstChild->nodeValue;

					if ($key != $table->db_primary_key && $key != '') {
						$aData[$key] = $val;
					}
					if ($isPseudoKey == '1' ) {
						$aPseudoKeys[] = "$key = '$val'";
					}
				}
			}
			//test if record already exists?
			$res = 0;
			if (count( $aPseudoKeys ) > 0) {
				$sql = "SELECT count(*) FROM $table->db_table_name WHERE " . implode( ' AND ', $aPseudoKeys );
				$db->setQuery( $sql );
				$res = $db->loadResult( );
			}
			if ($res == 0) {
				if ($this->storeRow( $aData, 0 ) ) {
					return 'saved';
				} else {
					return 'error saving';
				}
			} else {
				return 'duplicate';
			}
		}
		return 'error';
	}

	/**
	 * ajax get record specified by row id
	 */

	function xRecord($id)
	{
		$cursor = JRequest::getInt( 'cursor', 1 );
		$this->getConnection( );
		$this->_outPutFormat = 'json';
		//$pageNav = new fabrikPageNav( 1, 0, 1 );
		$this->_pageNav		=& $this->_getPagination( 1, 0, 1 );
		$data = $this->getData( );
		return FastJSON::encode($data);
	}

	/**
	 * ajax get next record
	 * @return string json object representing record/row
	 */

	function nextRecord()
	{
		$cursor = JRequest::getInt( 'cursor', 1 );
		$this->getConnection( );
		$this->_outPutFormat = 'json';
		$this->_pageNav		=& $this->_getPagination( 1, $cursor, 1 );
		$data = $this->getData( );
		echo FastJSON::encode($data);
	}

	/**
	 * ajax get previous record
	 * @return string json object representing record/row
	 */

	function previousRecord()
	{
		$cursor = JRequest::getInt( 'cursor', 1 );
		$this->getConnection( );
		$this->_outPutFormat = 'json';
		$this->_pageNav		=& $this->_getPagination( 1, $cursor-2, 1 );
		$data = $this->getData( );
		return FastJSON::encode( $data );
	}

	/**
	 * ajax get first record
	 * @return string json object representing record/row
	 */

	function firstRecord()
	{
		$cursor = JRequest::getInt( 'cursor', 1 );
		$this->getConnection( );
		$this->_outPutFormat = 'json';
		$this->_pageNav		=& $this->_getPagination( 1, 0, 1 );
		$data = $this->getData( );
		return FastJSON::encode( $data );
	}

	/**
	 * ajax get last record
	 * @return string json object representing record/row
	 */

	function lastRecord()
	{
		$total = JRequest::getInt( 'total', 0 );
		$this->getConnection( );
		$this->_outPutFormat = 'json';
		$this->_pageNav		=& $this->_getPagination( 1, $total-1, 1 );
		$data = $this->getData( );
		return FastJSON::encode( $data );
	}


	/**
	 *  get a single column of data from the table, test for element filters
	 * @param string column to get
	 * @return array values for the column
	 */

	function getColumnData( $col )
	{
		$table =& $this->getTable();
		$db =& $this->getDb();
		FabrikString::safeColName( $col );
		$tablename =  $table->db_table_name;
		FabrikString::safeColName( $tablename );
		$query  	= "SELECT DISTINCT($col) FROM " . $tablename . ' ' . $this->_buildQueryJoin();
		$query 	.= $this->_buildQueryWhere( false );
		$db->setQuery( $query );
		$res  	= $db->loadResultArray( );
		if ($db->getErrorNum()) {
			JError::raiseError( 500, $db->getErrorMsg() );
		}
		return $res;
	}


	function importCSV( )
	{
		$db 				=& JFactory::getDBO();
		$oImport 		= new FabrikModelImportCsv();
		$formModel 	=& $this->getForm();
		$table 			=& $this->getTable();
		$oImport->checkUpload();
		$db->setQuery( "SELECT plugin AS value, name AS text FROM #__fabrik_plugins" );
		$elementTypes = $db->loadObjectList();
		$_SESSION['fabrik']['csvImportData'] 	 = $oImport->data;
		$_SESSION['fabrik']['csvImportHeadings'] = $oImport->headings;
		$fabrik_table = 0;
		$pKey = '';

		$table = $table->label;
		$this->getFormGroupElementData( false, false );
		$lists['matchedHeadings'] = array();
		$lists['newHeadings']	 = array();
		$pKey = $oTable->db_primary_key;

		//
		$pluginManager =& JModel::getInstance( 'Pluginmanager', 'FabrikModel' );
		$pluginManager->loadPlugInGroup( 'table' );
		$aUsedElements = array();

		foreach ( $oImport->headings as $heading ) {
			foreach ( $formModel->_groups as $groupModel ) {
				$found = false;
				foreach ($groupModel->_aElements as $elementModel) {
					$element =& $elementModel->getElement();
					$fullName = $elementModel->getFullName( true, true, false );
					if (strtolower( $heading ) == strtolower( $element->name ) || strtolower( $heading ) == $fullName) {
						/** heading found in table */
						$lists['matchedHeadings'][] = strtolower( $heading );
						$aUsedElements[strtolower( $heading )] = $elementModel;
						$found = true;
					}
				}
				if (!$found) {
					$lists['newHeadings'][] = $heading;
				}
			}
		}

		$intKey= 0;
		foreach ( $aUsedElements as $elementModel ) {
			$oImport->data = $elementModel->prepareCSVData( $oImport->data, $intKey );
			$intKey ++;
		}

		if (!empty( $lists['newHeading'] )) {
			return false;
		}
		$dropData	= JRequest::getInt( 'drop_data', 0, 'post' );
		$overWrite	= JRequest::getInt( 'overwrite', 0, 'post' );
		if ($dropData) {
			$this->dropData();
		}
		$key = str_replace( ".", "___", $table->db_primary_key );
		$key2 = str_replace( $table->db_table_name . "___", "", $key );
			
		//get a list of exisitng primary key vals
		$db =& $this->getDb();
		$db->setQuery( "SELECT $table->db_primary_key FROM $table->db_table_name" );
		$aExistingKeys = $db->loadResultArray();
		foreach ($oImport->data as $data) {
			$aRow = array();
			if ($overWrite) {
				$ch = count( $lists['matchedHeadings'] );
				for ($i=0; $i < $ch; $i++) {
					$tmpKey = $lists['matchedHeadings'][$i];
					if ($tmpKey != $key && $tmpKey != $key2 ) {
						$pkVal = $data[$i];
					}
					$aRow[$tmpKey] = $data[$i];
				}
				$aRow = $this->removeTableNameFromSaveData( $aRow );
				if (in_array( $pkVal, $aExistingKeys )) {
					$this->storeRow( $aRow, $pkVal );
				} else {
					$this->storeRow( $aRow, 0 );
				}
			} else {
				for ($i=0;$i<count($lists['matchedHeadings']);$i++) {
					$tmpKey = $lists['matchedHeadings'][$i];
					if ($tmpKey != $key && $tmpKey != $key2 ) {
						$aRow[$tmpKey] = $data[$i];
					}
				}
				$aRow = $this->removeTableNameFromSaveData( $aRow );
				$this->storeRow( $aRow, 0 );
			}
		}
		return true;
	}

}
?>
Back to Top