ATK /relations/class.atkmanytoonerelation.inc

Language PHP Lines 2484
MD5 Hash c0bf5b202591ec9c534a0eba89cd1723
Repository https://github.com/ibuildingsnl/ATK.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
<?php

/**
 * This file is part of the Achievo ATK distribution.
 * Detailed copyright and licensing information can be found
 * in the doc/COPYRIGHT and doc/LICENSE files which should be
 * included in the distribution.
 *
 * @package atk
 * @subpackage relations
 *
 * @copyright (c)2000-2004 Ibuildings.nl BV
 * @copyright (c)2000-2004 Ivo Jansch
 * @license http://www.achievo.org/atk/licensing ATK Open Source License
 *
 * @version $Revision: 6309 $
 * $Id$
 */

/**
 * Create edit/view links for the items in a manytoonerelation dropdown.
 */
define("AF_RELATION_AUTOLINK",  AF_SPECIFIC_1);

/**
 * Create edit/view links for the items in a manytoonerelation dropdown.
 */
define("AF_MANYTOONE_AUTOLINK", AF_RELATION_AUTOLINK);

/**
 * Do not add null option under any circumstance
 */
define("AF_RELATION_NO_NULL_ITEM",  AF_SPECIFIC_2);

/**
 * Do not add null option ever
 */
define("AF_MANYTOONE_NO_NULL_ITEM", AF_RELATION_NO_NULL_ITEM);

/**
 * Use auto-completition instead of drop-down / selection page
 */
define("AF_RELATION_AUTOCOMPLETE",  AF_SPECIFIC_3);

/**
 * Use auto-completition instead of drop-down / selection page
 */
define("AF_MANYTOONE_AUTOCOMPLETE", AF_RELATION_AUTOCOMPLETE);

/**
 * Lazy load
 */
define("AF_MANYTOONE_LAZY", AF_SPECIFIC_4);

/**
 * Add a default null option to obligatory relations
 */
define("AF_MANYTOONE_OBLIGATORY_NULL_ITEM", AF_SPECIFIC_5);

/**
 * @internal include base class
 */
userelation("atkrelation");

/**
 * A N:1 relation between two classes.
 *
 * For example, projects all have one coordinator, but one
 * coordinator can have multiple projects. So in the project
 * class, there's a ManyToOneRelation to a coordinator.
 *
 * This relation essentially creates a dropdown box, from which
 * you can select from a set of records.
 *
 * @author Ivo Jansch <ivo@achievo.org>
 * @package atk
 * @subpackage relations
 *
 */
class atkManyToOneRelation extends atkRelation
{
  const SEARCH_MODE_EXACT       = "exact";
  const SEARCH_MODE_STARTSWITH  = "startswith";
  const SEARCH_MODE_CONTAINS    = "contains";

  /**
   * By default, we do a left join. this means that records that don't have
   * a record in this relation, will be displayed anyway. NOTE: set  this to
   * false only if you know what you're doing. When in doubt, 'true' is
   * usually the best option.
   * @var boolean
   */
  var $m_leftjoin = true;

  /**
   * The array of referential key fields.
   * @access private
   * @var array
   */
  var $m_refKey = array();

  /**
   * SQL statement with extra filter for the join that retrieves the
   * selected record.
   * @var String
   */
  var $m_joinFilter = "";

  /**
   * Hide the relation when there are no records to select.
   * @access private
   * @var boolean
   */
  var $m_hidewhenempty = false;

  /**
   * List columns.
   * @access private
   * @var Array
   */
  var $m_listColumns = array();

  /**
   * Always show list columns?
   * @access private
   * @var boolean
   */
  var $m_alwaysShowListColumns = false;

  /**
   * Label to use for the 'none' option.
   *
   * @access private
   * @var String
   */
  var $m_noneLabel = NULL;

  /**
   * Minimum number of character a user needs to enter before auto-completion kicks in.
   *
   * @access private
   * @var int
   */
  var $m_autocomplete_minchars = 2;

  /**
   * An array with the fieldnames of the destination node in which the autocompletion must search
   * for results.
   *
   * @access private
   * @var array
   */
  var $m_autocomplete_searchfields = "";

  /**
   * The search mode of the autocomplete fields. Can be 'startswith', 'exact' or 'contains'.
   *
   * @access private
   * @var String
   */
  var $m_autocomplete_searchmode = "contains";

  /**
   * Value determines wether the search of the autocompletion is case-sensitive.
   *
   * @var boolean
   */
  var $m_autocomplete_search_case_sensitive = false;

  /**
   * Value determines if select link for autocomplete should use atkSubmit or not (for use in admin screen for example)
   *
   * @var boolean
   */
  var $m_autocomplete_saveform = true;

  /**
   * Set the minimal number of records for showing the automcomplete. If there are less records
   * the normal dropdown is shown
   *
   * @access private
   * @var integer
   */
  var $m_autocomplete_minrecords = -1;

  /**
   * Destination node for auto links (edit, new)
   *
   * @var string
   */
  protected $m_autolink_destination = "";

  // override onchangehandler init
  var $m_onchangehandler_init = "newvalue = el.options[el.selectedIndex].value;\n";

  /**
   * Use destination filter for autolink add link?
   *
   * @access private
   * @var boolean
   */
  var $m_useFilterForAddLink = true;

  /**
   * Set a function to use for determining the descriptor in the getConcatFilter function
   *
   * @access private
   * @var string
   */
  var $m_concatDescriptorFunction = '';

  /**
   * When autosearch is set to true, this attribute will automatically submit
   * the search form onchange. This will only happen in the admin action.
   *
   * @var boolean
   */
  protected $m_autoSearch = false;

  /**
   * Selectable records for edit mode.
   *
   * @see atkManyToOneRelation::preAddToEditArray
   *
   * @var array
   */
  protected $m_selectableRecords = null;

  /**
   * Constructor.
   * @param String $name The name of the attribute. This is the name of the
   *                     field that is the referential key to the
   *                     destination.
   *                     For relations with more than one field in the
   *                     foreign key, you should pass an array of
   *                     referential key fields. The order of the fields
   *                     must match the order of the primary key attributes
   *                     in the destination node.
   * @param String $destination The node we have a relationship with.
   * @param int $flags Flags for the relation
   */
  function atkManyToOneRelation($name, $destination, $flags=0)
  {
    if (atkconfig("manytoone_autocomplete_default", false))
    $flags |= AF_RELATION_AUTOCOMPLETE;

    if (atkconfig("manytoone_autocomplete_large", true) && hasFlag($flags, AF_LARGE))
    $flags |= AF_RELATION_AUTOCOMPLETE;

    $this->m_autocomplete_minchars = atkconfig("manytoone_autocomplete_minchars", 2);
    $this->m_autocomplete_searchmode = atkconfig("manytoone_autocomplete_searchmode", "contains");
    $this->m_autocomplete_search_case_sensitive = atkconfig("manytoone_autocomplete_search_case_sensitive", false);

    if (is_array($name))
    {
      $this->m_refKey = $name;

      // ATK can't handle an array as name, so we initialize the
      // underlying attribute with the first name of the referential
      // keys.
      // Languagefiles, overrides, etc should use this first name to
      // override the relation.
      $this->atkRelation($name[0], $destination, $flags);
    }
    else
    {
      $this->m_refKey[] = $name;
      $this->atkRelation($name, $destination, $flags);
    }

    if ($this->hasFlag(AF_MANYTOONE_LAZY) && (count($this->m_refKey) > 1 || $this->m_refKey[0] != $this->fieldName()))
    {
      atkerror("AF_MANYTOONE_LAZY flag is not supported for multi-column reference key or a reference key that uses another column.");
    }
  }

  /**
   * Adds a flag to the manyToOne relation
   * Note that adding flags at any time after the constructor might not
   * always work. There are flags that are processed only at
   * constructor time.
   *
   * @param int $flag The flag to add to the attribute
   * @return atkManyToOneRelation The instance of this atkManyToOneRelation
   */
  function addFlag($flag)
  {
    parent::addFlag($flag);
    if (atkconfig("manytoone_autocomplete_large", true) && hasFlag($flag, AF_LARGE))
    $this->m_flags |= AF_RELATION_AUTOCOMPLETE;
    return $this;
  }

  /**
   * When autosearch is set to true, this attribute will automatically submit
   * the search form onchange. This will only happen in the admin action.
   * @param bool $auto
   * @return void
   */
  public function setAutoSearch($auto = false)
  {
    $this->m_autoSearch = $auto;
  }

  /**
   * Set join filter.
   *
   * @param string $filter join filter
   */
  function setJoinFilter($filter)
  {
    $this->m_joinFilter = $filter;
  }

  /**
   * Set the searchfields for the autocompletion.
   *
   * @param array $searchfields
   */
  function setAutoCompleteSearchFields($searchfields)
  {
    $this->m_autocomplete_searchfields = $searchfields;
  }

  /**
   * Set the searchmode for the autocompletion:
   * exact, startswith(default) or contains.
   *
   * @param array $mode
   */
  function setAutoCompleteSearchMode($mode)
  {
    $this->m_autocomplete_searchmode = $mode;
  }

  /**
   * Set the case-sensitivity for the autocompletion search (true or false).
   *
   * @param array $case_sensitive
   */
  function setAutoCompleteCaseSensitive($case_sensitive)
  {
    $this->m_autocomplete_search_case_sensitive = $case_sensitive;
  }

  /**
   * Sets the minimum number of characters before auto-completion kicks in.
   *
   * @param int $chars
   */
  function setAutoCompleteMinChars($chars)
  {
    $this->m_autocomplete_minchars = $chars;
  }

  /**
   * Set if the select link should save form (atkSubmit) or not (for use in admin screen for example)
   *
   * @param boolean $saveform
   */
  function setAutoCompleteSaveForm($saveform=true)
  {
    $this->m_autocomplete_saveform = $saveform;
  }

  /**
   * Set the minimal number of records for the autocomplete to show
   * If there are less records the normal dropdown is shown
   *
   * @param integer $minrecords
   */
  function setAutoCompleteMinRecords($minrecords)
  {
    $this->m_autocomplete_minrecords = $minrecords;
  }

  /**
   * Use destination filter for auto add link?
   *
   * @param boolean $useFilter use destnation filter for add link?
   */
  function setUseFilterForAddLink($useFilter)
  {
    $this->m_useFilterForAddLink = $useFilter;
  }

  /**
   * Set the function for determining the descriptor in the getConcatFilter function
   * This function should be implemented in the destination node
   *
   * @param string $function
   */
  function setConcatDescriptorFunction($function)
  {
    $this->m_concatDescriptorFunction = $function;
  }

  /**
   * Return the function for determining the descriptor in the getConcatFilter function
   *
   * @return string
   */
  function getConcatDescriptorFunction()
  {
    return $this->m_concatDescriptorFunction;
  }

  /**
   * Add list column. An attribute of the destination node
   * that (only) will be displayed in the recordlist.
   *
   * @param string $attr The attribute to add to the listcolumn
   * @return atkManyToOneRelation The instance of this atkManyToOneRelation
   */
  function addListColumn($attr)
  {
    $this->m_listColumns[] = $attr;
    return $this;
  }

  /**
   * Add multiple list columns. Attributes of the destination node
   * that (only) will be displayed in the recordlist.
   * @return atkManyToOneRelation The instance of this atkManyToOneRelation
   */
  function addListColumns()
  {
    $attrs = func_get_args();
    foreach ($attrs as $attr)
    $this->m_listColumns[] = $attr;
    return $this;
  }

  public function getListColumns()
  {
    return $this->m_listColumns;
  }

  /**
   * Reset the list columns and add multiple list columns. Attributes of the
   * destination node that (only) will be displayed in the recordlist.
   * @return atkManyToOneRelation The instance of this atkManyToOneRelation
   */
  public function setListColumns()
  {
    $this->m_listColumns = array();

    $attrs = func_get_args();
    if (count($attrs)===1 && is_array($attrs[0]))
    {
      $columns = $attrs[0];
    }
    else
    {
      $columns = $attrs;
    }
    
    foreach ($columns as $column)
    {
      $this->m_listColumns[] = $column;
    }
    return $this;
  }

  /**
   * Always show list columns in list view,
   * even if the attribute itself is hidden?
   *
   * @param bool $value always show list columns?
   * @return atkManyToOneRelation The instance of this atkManyToOneRelation
   */
  function setAlwaysShowListColumns($value)
  {
    $this->m_alwaysShowListColumns = $value;
    if($this->m_alwaysShowListColumns)
    $this->addFlag(AF_FORCE_LOAD);
    return $this;
  }

  /**
   * Convert value to DataBase value
   * @param array $rec Record to convert
   * @return int Database safe value
   */
  function value2db($rec)
  {
    if ($this->isEmpty($rec))
    {
      atkdebug($this->fieldName()." IS EMPTY!");
      return NULL;
    }
    else
    {
      if ($this->createDestination())
      {
        if (is_array($rec[$this->fieldName()]))
        {
          $pkfield = $this->m_destInstance->m_primaryKey[0];
          $pkattr = &$this->m_destInstance->getAttribute($pkfield);
          return $pkattr->value2db($rec[$this->fieldName()]);
        }
        else
        {
          return $rec[$this->fieldName()];
        }
      }
    }
    // This never happens, does it?
    return "";
  }

  /**
   * Fetch value out of record
   * @param array $postvars Postvars
   * @return decoded value
   */
  function fetchValue($postvars)
  {
    if ($this->isPosted($postvars))
    {
      $result = array();

      // support specifying the value as a single number if the
      // destination's primary key consists of a single field
      if (is_numeric($postvars[$this->fieldName()]))
      {
        $result[$this->getDestination()->primaryKeyField()] = $postvars[$this->fieldName()];
      }

      else
      {
        // Split the primary key of the selected record into its
        // referential key elements.
        $keyelements = decodeKeyValueSet($postvars[$this->fieldName()]);
        foreach ($keyelements as $key=>$value)
        {
          // Tablename must be stripped out because it is in the way..
          if (strpos($key,'.')>0)
          {
            $field = substr($key,strrpos($key,'.')+1);
          }
          else
          {
            $field = $key;
          }
          $result[$field] = $value;
        }
      }

      if (count($result) == 0)
      {
        return null;
      }

      // add descriptor fields, this means they can be shown in the title
      // bar etc. when updating failed for example
      $record = array($this->fieldName() => $result);
      $this->populate($record);
      $result = $record[$this->fieldName()];

      return $result;
    }
    return NULL;
  }


  /**
   * Converts DataBase value to normal value
   * @param array $rec Record
   * @return decoded value
   */
  function db2value($rec)
  {
    $this->createDestination();

    if (isset($rec[$this->fieldName()]) &&
    is_array($rec[$this->fieldName()]) &&
    (!isset($rec[$this->fieldName()][$this->m_destInstance->primaryKeyField()]) ||
    empty($rec[$this->fieldName()][$this->m_destInstance->primaryKeyField()])))
    {
      return NULL;
    }

    if (isset($rec[$this->fieldName()]))
    {
      $myrec = $rec[$this->fieldName()];
      if (is_array($myrec))
      {
        $result = array();
        if ($this->createDestination())
        {
          foreach (array_keys($this->m_destInstance->m_attribList) as $attrName)
          {
            $attr = &$this->m_destInstance->m_attribList[$attrName];
            $result[$attrName] = $attr->db2value($myrec);
          }
        }

        return $result;
      }
      else
      {
        // if the record is not an array, probably only the value of the primary key was loaded.
        // This workaround only works for single-field primary keys.
        if ($this->createDestination())
        return array($this->m_destInstance->primaryKeyField() => $myrec);
      }
    }
  }

  /**
   * Set none label.
   *
   * @param string $label The label to use for the "none" option
   */
  function setNoneLabel($label)
  {
    $this->m_noneLabel = $label;
  }

  /**
   * Get none label.
   *
   * @return String The label for the "none" option
   */
  function getNoneLabel()
  {
    if ($this->m_noneLabel !== NULL)
    return $this->m_noneLabel;
    $nodename = $this->m_destInstance->m_type;
    $modulename = $this->m_destInstance->m_module;
    $ownermodulename = $this->m_ownerInstance->m_module;
    $label = atktext($this->fieldName() . '_select_none', $ownermodulename, $this->m_owner, "", "", true);
    if ($label == "")
    $label = atktext('select_none', $modulename, $nodename);

    return $label;
  }

  /**
   * Returns a displayable string for this value.
   *
   * @param array $record The record that holds the value for this attribute
   * @param String $mode The display mode ("view" for viewpages, or "list"
   *                     for displaying in recordlists, "edit" for
   *                     displaying in editscreens, "add" for displaying in
   *                     add screens. "csv" for csv files. Applications can
   *                     use additional modes.
   * @return a displayable string
   */
  function display($record, $mode='list')
  {
    if ($this->createDestination())
    {
      if (count($record[$this->fieldName()])==count($this->m_refKey))
      $this->populate($record);

      if(!$this->isEmpty($record))
      {
        $result = $this->m_destInstance->descriptor($record[$this->fieldName()]);
        if ($this->hasFlag(AF_RELATION_AUTOLINK) && (!in_array($mode, array("csv", "plain", "list")))) // create link to edit/view screen
        {
          if (($this->m_destInstance->allowed("view"))&&!$this->m_destInstance->hasFlag(NF_NO_VIEW)&&$result!="")
          {
            $saveForm = $mode == 'add' || $mode == 'edit';
            $result = href(dispatch_url($this->m_destination,"view",array("atkselector"=>$this->m_destInstance->primaryKey($record[$this->fieldName()]))), $result, SESSION_NESTED, $saveForm);
          }
        }
      }
      else
      {
        $result = (!in_array($mode, array("csv", "plain"))?$this->getNoneLabel():''); // no record
      }
      return $result;
    }
    else
    {
      atkdebug("Can't create destination! ($this->m_destination");
    }
    return "";
  }

  /**
   * Populate the record with the destination record data.
   *
   * @param array $record        record
   * @param mixed $fullOrFields  load all data, only the given fields or only the descriptor fields?
   */
  public function populate(&$record, $fullOrFields=false)
  {
    if (!is_array($record)||$record[$this->fieldName()] == "") return;

    atkdebug("Delayed loading of ".($fullOrFields || is_array($fullOrFields) ? "" : "descriptor ")."fields for ".$this->m_name);
    $this->createDestination();

    $includes = "";
    if (is_array($fullOrFields))
    {
      $includes = array_merge($this->m_destInstance->m_primaryKey, $fullOrFields);
    }
    else if (!$fullOrFields)
    {
      $includes = $this->m_destInstance->descriptorFields();
    }

    $result = 
      $this->m_destInstance
        ->select($this->m_destInstance->primaryKey($record[$this->fieldName()]))
        ->orderBy($this->m_destInstance->getColumnConfig()->getOrderByStatement())
        ->includes($includes)
        ->firstRow();
        
    if ($result != null)
    {
      $record[$this->fieldName()] = $result;
    }
  }

  /**
   * Creates HTML for the selection and auto links.
   *
   * @param string $id attribute id
   * @param array $record record
   * @return string
   */
  function createSelectAndAutoLinks($id, $record)
  {
    $links = array();

    $newsel = $id;
    $filter = $this->parseFilter($this->m_destinationFilter, $record);
    $links[] = $this->_getSelectLink($newsel, $filter);
    if ($this->hasFlag(AF_RELATION_AUTOLINK)) // auto edit/view link
    {
      if ($this->m_destInstance->allowed("add"))
      {
        $links[] = href(dispatch_url($this->getAutoLinkDestination(),"add",array("atkpkret"=>$id,"atkfilter"=>($filter!=""?$filter:""))),
        atktext("new"),SESSION_NESTED,true);
      }

      if ($this->m_destInstance->allowed("edit") && $record[$this->fieldName()] != NULL)
      {
        //we laten nu altijd de edit link zien, maar eigenlijk mag dat niet, want
        //de app crasht als er geen waarde is ingevuld.
        $editUrl = session_url(dispatch_url($this->getAutoLinkDestination(),"edit",array("atkselector"=>"REPLACEME")),SESSION_NESTED);
        $links[] = "<span id=\"".$id."_edit\" style=\"\"><a href='javascript:atkSubmit(mto_parse(\"".atkurlencode($editUrl)."\", document.entryform.".$id.".value))'>".atktext('edit')."</a></span>";
      }
    }

    return implode("&nbsp;", $links);
  }


  /**
   * Set destination node for the Autolink links (new/edit)
   *
   * @param string $node
   */
  function setAutoLinkDestination($node)
  {
    $this->m_autolink_destination = $node;
  }

  /**
   * Get destination node for the Autolink links (new/edit)
   *
   * @return string
   */
  function getAutoLinkDestination()
  {
    if(!empty($this->m_autolink_destination))
    {
      return $this->m_autolink_destination;
    }
    return $this->m_destination;
  }

  /**
   * Prepare for editing, make sure we already have the selectable records
   * loaded and update the record with the possible selection of the first
   * record.
   *
   * @param array  $record      reference to the record
   * @param string $fieldPrefix field prefix
   * @param string $mode        edit mode
   */
  public function preAddToEditArray(&$record, $fieldPrefix, $mode)
  {
    if ((!$this->hasFlag(AF_RELATION_AUTOCOMPLETE) && !$this->hasFlag(AF_LARGE)) ||
        $this->m_autocomplete_minrecords > -1)
    {
      $this->m_selectableRecords = $this->_getSelectableRecords($record, 'select');

      if (count($this->m_selectableRecords) > 0 &&
          !atkconfig("list_obligatory_null_item") &&
          (($this->hasFlag(AF_OBLIGATORY) && !$this->hasFlag(AF_MANYTOONE_OBLIGATORY_NULL_ITEM)) ||
           (!$this->hasFlag(AF_OBLIGATORY) && $this->hasFlag(AF_RELATION_NO_NULL_ITEM))))
      {
        if (!isset($record[$this->fieldName()]) || !is_array($record[$this->fieldName()]))
        {
          $record[$this->fieldName()] = $this->m_selectableRecords[0];
        }
        else if (!$this->_isSelectableRecord($record, 'select'))
        {
          $record[$this->fieldName()] = $this->m_selectableRecords[0];
        }
        else
        {
          $current = $this->getDestination()->primaryKey($record[$this->fieldName()]);
          $record[$this->fieldName()] = null;
          foreach ($this->m_selectableRecords as $selectable)
          {
            if ($this->getDestination()->primaryKey($selectable) == $current)
            {
              $record[$this->fieldName()] = $selectable;
              break;
            }
          }
        }
      }
    }
    else if (is_array($record[$this->fieldName()]) && !$this->_isSelectableRecord($record, 'select'))
    {
      $record[$this->fieldName()] = null;
    }
    else if (is_array($record[$this->fieldName()]))
    {
      $this->populate($record);
    }
  }

  /**
   * Returns a piece of html code that can be used in a form to edit this
   * attribute's value.
   * @param array $record The record that holds the value for this attribute.
   * @param String $fieldprefix The fieldprefix to put in front of the name
   *                            of any html form element for this attribute.
   * @param String $mode The mode we're in ('add' or 'edit')
   * @return Piece of html code that can be used in a form to edit this
   */
  function edit($record, $fieldprefix="", $mode="edit")
  {
    if (!$this->createDestination())
    {
      return atkerror("Could not create destination for destination: $this->m_destination!");
    }

    $recordset = $this->m_selectableRecords;

    // load records for bwc
    if ($recordset === null && $this->hasFlag(AF_RELATION_AUTOCOMPLETE) && $this->m_autocomplete_minrecords > -1)
    {
      $recordset = $this->_getSelectableRecords($record, 'select');
    }

    if ($this->hasFlag(AF_RELATION_AUTOCOMPLETE) && (is_object($this->m_ownerInstance)) && ((is_array($recordset) && count($recordset) > $this->m_autocomplete_minrecords) || $this->m_autocomplete_minrecords == -1))
    {
      return $this->drawAutoCompleteBox($record, $fieldprefix, $mode);
    }


    $id = $fieldprefix.$this->fieldName();
    $filter = $this->parseFilter($this->m_destinationFilter, $record);
    $autolink = $this->getRelationAutolink($id, $filter);
    $editflag = true;

    $value = isset($record[$this->fieldName()]) ? $record[$this->fieldName()] : null;
    $currentPk = $value != null ? $this->getDestination()->primaryKey($value) : null;

    if (!$this->hasFlag(AF_LARGE)) // normal dropdown..
    {
      // load records for bwc
      if ($recordset == null)
      {
        $recordset = $this->_getSelectableRecords($record, 'select');
      }

      if (count($recordset) == 0)
      {
        $editflag = false;
      }

      $onchange='';
      if (count($this->m_onchangecode))
      {
        $onchange = 'onChange="'.$id.'_onChange(this);"';
        $this->_renderChangeHandler($fieldprefix);
      }

      // autoselect if there is only one record (if obligatory is not set,
      // we don't autoselect, since user may wist to select 'none' instead
      // of the 1 record.
      $result = '';

      if (count($recordset) == 0)
      {
        $result = $this->getNoneLabel();
      }
      else
      {
        $this->registerKeyListener($id, KB_CTRLCURSOR|KB_LEFTRIGHT);
        $this->registerJavaScriptObservers($id);

        $result = '<select id="'.$id.'" name="'.$id.'" class="atkmanytoonerelation" '.$onchange.'>';

        // relation may be empty, so we must provide an empty selectable..
        if ($this->hasFlag(AF_MANYTOONE_OBLIGATORY_NULL_ITEM) ||
            (!$this->hasFlag(AF_OBLIGATORY) && !$this->hasFlag(AF_RELATION_NO_NULL_ITEM)) ||
            (atkconfig("list_obligatory_null_item") && !is_array($value)))
        {
          $result.= '<option value="">'.$this->getNoneLabel();
        }

        foreach ($recordset as $selectable)
        {
          $pk = $this->getDestination()->primaryKey($selectable);
          $sel = $pk == $currentPk ? 'selected="selected"' : '';
          $result .= '<option value="'.$pk.'" '.$sel.'>'.str_replace(' ', '&nbsp;', atk_htmlentities($this->m_destInstance->descriptor($selectable)));
        }

        $result .= '</select>';
      }
    }
    else
    {
      $destrecord = $record[$this->fieldName()];
      if (is_array($destrecord))
      {
        $result.= '<span id="'.$id.'_current" >'.$this->m_destInstance->descriptor($destrecord)."&nbsp;&nbsp;";
        if (!$this->hasFlag(AF_OBLIGATORY))
        {
          $result.= '<a href="#" onClick="document.getElementById(\''.
          $id.'\').value=\'\'; document.getElementById(\''.$id.'_current\').style.display=\'none\'">'.atktext("unselect").'</a>&nbsp;&nbsp;';
        }
        $result.= '</span>';
      }

      $result .= $this->hide($record, $fieldprefix);
      $result .= $this->_getSelectLink($id, $filter);
    }

    $result .= $editflag && isset($autolink['edit']) ? $autolink['edit'] : "";
    $result .= isset($autolink['add']) ? $autolink['add'] : "";

    return $result;
  }

  /**
   * Get the select link to select the value using a select action on the destination node
   *
   * @param string $selname
   * @param string $filter
   * @return String HTML-code with the select link
   */
  function _getSelectLink($selname, $filter)
  {
    $result = "";
    // we use the current level to automatically return to this page
    // when we come from the select..
    $atktarget = atkurlencode(getDispatchFile()."?atklevel=".atkLevel()."&".$selname."=[atkprimkey]");
    $linkname = atktext("link_select_".getNodeType($this->m_destination), $this->getOwnerInstance()->getModule(), $this->getOwnerInstance()->getType(),'','',true);
    if (!$linkname) $linkname = atktext("link_select_".getNodeType($this->m_destination), getNodeModule($this->m_destination),getNodeType($this->m_destination),'','',true);
    if (!$linkname) $linkname = atktext("select_a").' '.strtolower(atktext(getNodeType($this->m_destination), getNodeModule($this->m_destination),getNodeType($this->m_destination)));
    if ($this->m_destinationFilter!="")
    {
      $result.= href(dispatch_url($this->m_destination,"select",array("atkfilter"=>$filter,"atktarget"=>$atktarget)),
      $linkname,
      SESSION_NESTED,
      $this->m_autocomplete_saveform,'class="atkmanytoonerelation"');
    }
    else
    {
      $result.= href(dispatch_url($this->m_destination,"select",array("atktarget"=>$atktarget)),
      $linkname,
      SESSION_NESTED,
      $this->m_autocomplete_saveform,'class="atkmanytoonerelation"');
    }
    return $result;
  }

  /**
   * Creates and returns the auto edit/view links
   * @param String $id          The field id
   * @param String $filter      Filter that we want to apply on the destination node
   * @return array              The HTML code for the autolink links
   */
  function getRelationAutolink($id, $filter)
  {
    $autolink = array();
    if ($this->hasFlag(AF_RELATION_AUTOLINK)) // auto edit/view link
    {
      $page = &atkPage::getInstance();
      $page->register_script(atkconfig("atkroot")."atk/javascript/class.atkmanytoonerelation.js");

      if ($this->m_destInstance->allowed("edit"))
      {
        $editlink = session_url(dispatch_url($this->getAutoLinkDestination(),"edit",array("atkselector"=>"REPLACEME")),SESSION_NESTED);
        $autolink['edit'] = "&nbsp;<a href='javascript:atkSubmit(mto_parse(\"".atkurlencode($editlink)."\", document.entryform.".$id.".value))'>".atktext('edit')."</a>";
      }
      if ($this->m_destInstance->allowed("add"))
      {
        $autolink['add'] = "&nbsp;".href(dispatch_url($this->getAutoLinkDestination(),"add",array("atkpkret"=>$id,"atkfilter"=>($this->m_useFilterForAddLink && $filter != "" ? $filter : ""))),
        atktext("new"),SESSION_NESTED,true);
      }
    }
    return $autolink;
  }

  /**
   * Returns a piece of html code for hiding this attribute in an HTML form,
   * while still posting its value. (<input type="hidden">)
   *
   * @param array $record The record that holds the value for this attribute
   * @param String $fieldprefix The fieldprefix to put in front of the name
   *                            of any html form element for this attribute.
   * @return String A piece of htmlcode with hidden form elements that post
   *                this attribute's value without showing it.
   */
  function hide($record="", $fieldprefix="")
  {
    if (!$this->createDestination()) return '';

    $currentPk = "";
    if (isset($record[$this->fieldName()]) && $record[$this->fieldName()] != null)
    {
      $this->fixDestinationRecord($record);
      $currentPk = $this->m_destInstance->primaryKey($record[$this->fieldName()]);
    }

    $result =
        '<input type="hidden" id="'.$fieldprefix.$this->formName().'"
                name="'.$fieldprefix.$this->formName().'"
                value="'.$currentPk.'">';

    return $result;
  }

  /**
   * Support for destination "records" where only the id is set and the
   * record itself isn't converted to a real record (array) yet
   *
   * @param array $record The record to fix
   */
  function fixDestinationRecord(&$record)
  {
    if ($this->createDestination() &&
    isset($record[$this->fieldName()]) &&
    $record[$this->fieldName()] != null &&
    !is_array($record[$this->fieldName()]))
    {
      $record[$this->fieldName()] = array($this->m_destInstance->primaryKeyField() => $record[$this->fieldName()]);
    }
  }

  /**
   * Retrieve the html code for placing this attribute in an edit page.
   *
   * The difference with the edit() method is that the edit() method just
   * generates the HTML code for editing the attribute, while the getEdit()
   * method is 'smart', and implements a hide/readonly policy based on
   * flags and/or custom override methodes in the node.
   * (<attributename>_edit() and <attributename>_display() methods)
   *
   * Framework method, it should not be necessary to call this method
   * directly.
   *
   * @param String $mode The edit mode ("add" or "edit")
   * @param array $record The record holding the values for this attribute
   * @param String $fieldprefix The fieldprefix to put in front of the name
   *                            of any html form element for this attribute.
   * @return String the HTML code for this attribute that can be used in an
   *                editpage.
   */
  function getEdit($mode, &$record, $fieldprefix)
  {
    $this->fixDestinationRecord($record);
    return parent::getEdit($mode, $record, $fieldprefix);
  }

  /**
   * Converts a record filter to a record array.
   *
   * @param string $filter filter string
   * @return array record
   */
  protected function filterToArray($filter)
  {
    $result = array();

    $values = decodeKeyValueSet($filter);
    foreach ($values as $field => $value)
    {
      $parts = explode('.', $field);
      $ref = &$result;

      foreach ($parts as $part)
      {
        $ref = &$ref[$part];
      }

      $ref = $value;
    }

    return $result;
  }

  /**
   * Returns a piece of html code that can be used to get search terms input
   * from the user.
   *
   * @param array $record Array with values
   * @param boolean $extended if set to false, a simple search input is
   *                          returned for use in the searchbar of the
   *                          recordlist. If set to true, a more extended
   *                          search may be returned for the 'extended'
   *                          search page. The atkAttribute does not
   *                          make a difference for $extended is true, but
   *                          derived attributes may reimplement this.
   * @param string $fieldprefix The fieldprefix of this attribute's HTML element.
   * @param atkDataGrid $grid The datagrid
   *
   * @return String A piece of html-code
   */
  function search($record=array(), $extended=false, $fieldprefix="", atkDataGrid $grid=null)
  {
    $useautocompletion = atkConfig("manytoone_search_autocomplete", true) && $this->hasFlag(AF_RELATION_AUTOCOMPLETE);
    if (!$this->hasFlag(AF_LARGE) && !$useautocompletion)
    {
      if ($this->createDestination())
      {
        if ($this->m_destinationFilter!="")
        {
          $filterRecord = array();

          if ($grid != null)
          {
            foreach ($grid->getFilters() as $filter)
            {
              $filter = $filter['filter'];
              $arr = $this->filterToArray($filter);
              $arr = (isset($arr[$this->getOwnerInstance()->m_table]) && is_array($arr[$this->getOwnerInstance()->m_table])) ? $arr[$this->getOwnerInstance()->m_table] : array();
              foreach ($arr as $attrName => $value)
              {
                $attr = $this->getOwnerInstance()->getAttribute($attrName);
                if (!is_array($value) && is_a($attr, 'atkManyToOneRelation') && count($attr->m_refKey) == 1)
                {
                  $attr->createDestination();
                  $arr[$attrName] = array($attr->getDestination()->primaryKeyField() => $value);
                }
              }

              $filterRecord = array_merge($filterRecord, $arr);
            }
          }

          $record = array_merge($filterRecord, is_array($record) ? $record : array());
        }

        $recordset = $this->_getSelectableRecords($record, 'search');

        $result = '<select class="'.get_class($this).'" ';
        if ($extended)
        {
          $result.='multiple size="'.min(5,count($recordset)+1).'"';

          if(isset($record[$this->fieldName()][$this->fieldName()]))
          $record[$this->fieldName()] = $record[$this->fieldName()][$this->fieldName()];

        }

        // if we use autosearch, register an onchange event that submits the grid
        if (!is_null($grid) && !$extended && $this->m_autoSearch) {
           $id = $this->getSearchFieldName($fieldprefix);
           $result .= '  id="'.$id.'" ';
           $code = '$(\''.$id.'\').observe(\'change\', function(event) { ' .
             $grid->getUpdateCall(array('atkstartat' => 0), array(), 'ATK.DataGrid.extractSearchOverrides') .
             ' return false; });'
             ;
           $this->getOwnerInstance()->getPage()->register_loadscript($code);
        }

        $result.='name="'.$this->getSearchFieldName($fieldprefix).'[]">';

        $pkfield = $this->m_destInstance->primaryKeyField();

        $result.= '<option value="">'.atktext('search_all').'</option>';
        if (!$this->hasFlag(AF_OBLIGATORY))
        $result.= '<option value="__NONE__"'.(isset($record[$this->fieldName()]) && atk_in_array('__NONE__', $record[$this->fieldName()]) ? ' selected="selected"' : '').'>'.atktext('search_none').'</option>';

        for ($i=0;$i<count($recordset);$i++)
        {
          $pk = $recordset[$i][$pkfield];

          if (is_array($record)&&isset($record[$this->fieldName()])&&
          atk_in_array($pk, $record[$this->fieldName()])) $sel = "selected"; else $sel = "";
          $result.= '<option value="'.$pk.'" '.$sel.'>'.str_replace(' ', '&nbsp;', atk_htmlentities($this->m_destInstance->descriptor($recordset[$i]))).'</option>';
        }
        $result.='</select>';
        return $result;
      }
      return "";
    }
    else
    {
      $id = $this->getSearchFieldName($fieldprefix);
      if(is_array($record[$this->fieldName()]) && isset($record[$this->fieldName()][$this->fieldName()]))
      $record[$this->fieldName()] = $record[$this->fieldName()][$this->fieldName()];

      $this->registerKeyListener($id, KB_CTRLCURSOR|KB_UPDOWN);
      $result = '<input type="text" id="'.$id.'" class="'.get_class($this).'" name="'.$id.'" value="'.$record[$this->fieldName()].'"'.
      ($useautocompletion ? ' onchange=""' : '').
      ($this->m_searchsize > 0 ? ' size="'.$this->m_searchsize.'"' : '').
      ($this->m_maxsize > 0 ? ' maxlength="'.$this->m_maxsize.'"' : '').'>';

      if ($useautocompletion)
      {
        $page = &$this->m_ownerInstance->getPage();
        $url = partial_url($this->m_ownerInstance->atkNodeType(), $this->m_ownerInstance->m_action, 'attribute.'.$this->fieldName().'.autocomplete_search');
        $code = "ATK.ManyToOneRelation.completeSearch('{$id}', '{$id}_result', '{$url}', {$this->m_autocomplete_minchars});";
        $this->m_ownerInstance->addStyle("atkmanytoonerelation.css");
        $page->register_script(atkconfig('atkroot').'atk/javascript/class.atkmanytoonerelation.js');
        $page->register_loadscript($code);
        $result .= '<div id="'.$id.'_result" style="display: none" class="atkmanytoonerelation_result"></div>';
      }

      return $result;
    }
  }

  /**
   * Retrieve the list of searchmodes supported by the attribute.
   *
   * Note that not all modes may be supported by the database driver.
   * Compare this list to the one returned by the databasedriver, to
   * determine which searchmodes may be used.
   *
   * @return array List of supported searchmodes
   */
  function getSearchModes()
  {
    if ($this->hasFlag(AF_LARGE) || $this->hasFlag(AF_MANYTOONE_AUTOCOMPLETE))
    {
      return array("substring","exact","wildcard","regex");
    }
    return array("exact"); // only support exact search when searching with dropdowns
  }

  /**
   * Creates a smart search condition for a given search value, and adds it
   * to the query that will be used for performing the actual search.
   *
   * @param Integer  $id         The unique smart search criterium identifier.
   * @param Integer  $nr         The element number in the path.
   * @param Array    $path       The remaining attribute path.
   * @param atkQuery $query      The query to which the condition will be added.
   * @param String   $ownerAlias The owner table alias to use.
   * @param Mixed    $value      The value the user has entered in the searchbox.
   * @param String   $mode       The searchmode to use.
   */
  function smartSearchCondition($id, $nr, $path, &$query, $ownerAlias, $value, $mode)
  {
    if (count($path) > 0)
    {
      $this->createDestination();

      $destAlias = "ss_{$id}_{$nr}_".$this->fieldName();

      $query->addJoin(
      $this->m_destInstance->m_table, $destAlias,
      $this->getJoinCondition($query, $ownerAlias, $destAlias),
      false
      );

      $attrName = array_shift($path);
      $attr = &$this->m_destInstance->getAttribute($attrName);

      if (is_object($attr))
      {
        $attr->smartSearchCondition($id, $nr + 1, $path, $query, $destAlias, $value, $mode);
      }
    }
    else
    {
      $this->searchCondition($query, $ownerAlias, $value, $mode);
    }
  }

  /**
   * Creates a searchcondition for the field,
   * was once part of searchCondition, however,
   * searchcondition() also immediately adds the search condition.
   *
   * @param atkQuery $query     The query object where the search condition should be placed on
   * @param String $table       The name of the table in which this attribute
   *                              is stored
   * @param mixed $value        The value the user has entered in the searchbox
   * @param String $searchmode  The searchmode to use. This can be any one
   *                              of the supported modes, as returned by this
   *                              attribute's getSearchModes() method.
   * @param string $fieldaliasprefix The prefix for the field
   * @return String The searchcondition to use.
   */
  function getSearchCondition(&$query, $table, $value, $searchmode, $fieldaliasprefix='')
  {
    if (!$this->createDestination()) return;

    if (is_array($value))
    {
      foreach ($this->m_listColumns as $attr)
      {
        $attrValue = $value[$attr];
        if (!empty($attrValue))
        {
          $p_attrib = &$this->m_destInstance->m_attribList[$attr];
          if (!$p_attrib == NULL)
          {
            $p_attrib->searchCondition($query, $this->fieldName(), $attrValue, $this->getChildSearchMode($searchmode, $p_attrib->formName()));
          }
        }
      }

      if (isset($value[$this->fieldName()]))
      {
        $value = $value[$this->fieldName()];
      }
    }

    if (empty($value))
    {
      return '';
    }
    else if (!$this->hasFlag(AF_LARGE) && !$this->hasFlag(AF_RELATION_AUTOCOMPLETE))
    {
      // We only support 'exact' matches.
      // But you can select more than one value, which we search using the IN() statement,
      // which should work in any ansi compatible database.
      if (!is_array($value)) // This last condition is for when the user selected the 'search all' option, in which case, we don't add conditions at all.
      {
        $value = array($value);
      }

      if (count($value)==1) // exactly one value
      {
        if ($value[0] == "__NONE__")
        {
          return $query->nullCondition($table.".".$this->fieldName(), true);
        }
        elseif ($value[0] != "")
        {
          return $query->exactCondition($table.".".$this->fieldName(),$this->escapeSQL($value[0]));
        }
      }
      else // search for more values using IN()
      {
        return $table.".".$this->fieldName()." IN ('".implode("','",$value)."')";
      }
    }
    else // AF_LARGE || AF_RELATION_AUTOCOMPLETE
    {
      // If we have a descriptor with multiple fields, use CONCAT
      $attribs = $this->m_destInstance->descriptorFields();
      $alias = $fieldaliasprefix . $this->fieldName();
      if(count($attribs)>1)
      {
        $searchcondition = $this->getConcatFilter($value,$alias);
      }
      else
      {
        // ask the destination node for it's search condition
        $searchcondition = $this->m_destInstance->getSearchCondition($query, $alias, $fieldaliasprefix, $value, $this->getChildSearchMode($searchmode, $this->formName()));
      }
      return $searchcondition;
    }
  }

  /**
   * Adds this attribute to database queries.
   *
   * Database queries (select, insert and update) are passed to this method
   * so the attribute can 'hook' itself into the query.
   *
   * @param atkQuery $query The SQL query object
   * @param String $tablename The name of the table of this attribute
   * @param String $fieldaliasprefix Prefix to use in front of the alias
   *                                 in the query.
   * @param Array $rec The record that contains the value of this attribute.
   * @param int $level Recursion level if relations point to eachother, an
   *                   endless loop could occur if they keep loading
   *                   eachothers data. The $level is used to detect this
   *                   loop. If overriden in a derived class, any subcall to
   *                   an addToQuery method should pass the $level+1.
   * @param String $mode Indicates what kind of query is being processing:
   *                     This can be any action performed on a node (edit,
   *                     add, etc) Mind you that "add" and "update" are the
   *                     actions that store something in the database,
   *                     whereas the rest are probably select queries.
   */
  function addToQuery(&$query, $tablename="", $fieldaliasprefix="", $rec="", $level=0, $mode="")
  {
    if ($this->hasFlag(AF_MANYTOONE_LAZY))
    {
      parent::addToQuery($query, $tablename, $fieldaliasprefix, $rec, $level, $mode);
      return;
    }

    if ($this->createDestination())
    {
      if ($mode != "update" && $mode != "add")
      {
        $alias = $fieldaliasprefix . $this->fieldName();
        $query->addJoin($this->m_destInstance->m_table,
        $alias,
        $this->getJoinCondition($query, $tablename, $alias),
        $this->m_leftjoin);
        $this->m_destInstance->addToQuery($query, $alias, $level+1, false, $mode, $this->m_listColumns);
      }
      else
      {
        for ($i=0, $_i=count($this->m_refKey); $i<$_i; $i++)
        {
          if ($rec[$this->fieldName()]===NULL)
          {
            $query->addField($this->m_refKey[$i],"NULL","","",false);
          }
          else
          {
            $value = $rec[$this->fieldName()];
            if (is_array($value))
            {
              $fk = &$this->m_destInstance->getAttribute($this->m_destInstance->m_primaryKey[$i]);
              $value = $fk->value2db($value);
            }

            $query->addField($this->m_refKey[$i],$value,"","",!$this->hasFlag(AF_NO_QUOTES));
          }
        }
      }
    }
  }


  /**
   * Retrieve detail records from the database.
   *
   * Called by the framework to load the detail records.
   *
   * @param atkDb $db The database used by the node.
   * @param array $record The master record
   * @param String $mode The mode for loading (admin, select, copy, etc)
   *
   * @return array Recordset containing detailrecords, or NULL if no detail
   *               records are present. Note: when $mode is edit, this
   *               method will always return NULL. This is a framework
   *               optimization because in edit pages, the records are
   *               loaded on the fly.
   */
  function load(&$db, $record, $mode)
  {
    return $this->_getSelectedRecord($record, $mode);
  }

  /**
   * Determine the load type of this attribute.
   *
   * With this method, the attribute tells the framework whether it wants
   * to be loaded in the main query (addToQuery) or whether the attribute
   * has its own load() implementation.
   * For the atkOneToOneRelation, this depends on the presence of the
   * AF_ONETOONE_LAZY flag.
   *
   * Framework method. It should not be necesary to call this method
   * directly.
   *
   * @param String $mode The type of load (view,admin,edit etc)
   *
   * @return int Bitmask containing information about load requirements.
   *             POSTLOAD|ADDTOQUERY when AF_ONETOONE_LAZY is set.
   *             ADDTOQUERY when AF_ONETOONE_LAZY is not set.
   */
  function loadType($mode)
  {
    if (isset($this->m_loadType[$mode]) && $this->m_loadType[$mode] !== null)
    return $this->m_loadType[$mode];
    else if (isset($this->m_loadType[null]) && $this->m_loadType[null] !== null)
    return $this->m_loadType[null];
    // Default backwardscompatible behaviour:
    else if ($this->hasFlag(AF_MANYTOONE_LAZY))
    return POSTLOAD|ADDTOQUERY;
    else
    return ADDTOQUERY;
  }

  /**
   * Validate if the record we are referring to really exists.
   *
   * @param array $record
   * @param string $mode
   */
  function validate(&$record, $mode)
  {
    $sessionmanager = atkGetSessionManager();
    if ($sessionmanager) $storetype =  $sessionmanager->stackVar('atkstore');
    if ($storetype!=='session' && !$this->_isSelectableRecord($record))
    {
      triggerError($record, $this->fieldName(), 'error_integrity_violation');
    }
  }

  /**
   * Check if two records have the same value for this attribute
   *
   * @param array $recA Record A
   * @param array $recB Record B
   * @return boolean to indicate if the records are equal
   */
  function equal($recA, $recB)
  {
    if ($this->createDestination())
    {
      return (($recA[$this->fieldName()][$this->m_destInstance->primaryKeyField()]
      ==
      $recB[$this->fieldName()][$this->m_destInstance->primaryKeyField()])
      ||
      ($this->isEmpty($recA)&&$this->isEmpty($recB)));
      // we must also check empty values, because empty values need not necessarily
      // be equal (can be "", NULL or 0.
    }
    return false;
  }

  /**
   * Return the database field type of the attribute.
   *
   * Note that the type returned is a 'generic' type. Each database
   * vendor might have his own types, therefor, the type should be
   * converted to a database specific type using $db->fieldType().
   *
   * If the type was read from the table metadata, that value will
   * be used. Else, the attribute will analyze its flags to guess
   * what type it should be. If AF_AUTO_INCREMENT is set, the field
   * is probaly "number". If not, it's probably "string".
   *
   * @return String The 'generic' type of the database field for this
   *                attribute.
   */
  function dbFieldType()
  {
    // The type of field that we need to store the foreign key, is equal to
    // the type of field of the primary key of the node we have a
    // relationship with.
    if ($this->createDestination())
    {
      if(count($this->m_refKey)>1)
      {
        $keys = array();
        for($i=0, $_i=count($this->m_refKey); $i<$_i; $i++)
        {
          $keys [] = $this->m_destInstance->m_attribList[$this->m_destInstance->m_primaryKey[$i]]->dbFieldType();
        }
        return $keys;
      }
      else
      return $this->m_destInstance->m_attribList[$this->m_destInstance->primaryKeyField()]->dbFieldType();
    }
    return "";
  }

  /**
   * Return the size of the field in the database.
   *
   * If 0 is returned, the size is unknown. In this case, the
   * return value should not be used to create table columns.
   *
   * Ofcourse, the size does not make sense for every field type.
   * So only interpret the result if a size has meaning for
   * the field type of this attribute. (For example, if the
   * database field is of type 'date', the size has no meaning)
   *
   * @return int The database field size
   */
  function dbFieldSize()
  {
    // The size of the field we need to store the foreign key, is equal to
    // the size of the field of the primary key of the node we have a
    // relationship with.
    if ($this->createDestination())
    {
      if(count($this->m_refKey)>1)
      {
        $keys = array();
        for($i=0, $_i=count($this->m_refKey); $i<$_i; $i++)
        {
          $keys [] = $this->m_destInstance->m_attribList[$this->m_destInstance->m_primaryKey[$i]]->dbFieldSize();
        }
        return $keys;
      }
      else
      return $this->m_destInstance->m_attribList[$this->m_destInstance->primaryKeyField()]->dbFieldSize();
    }
    return 0;
  }

  /**
   * Returns the selected record for this many-to-one relation. Uses
   * the owner instance $this->fieldName()."_selected" method if it exists.
   *
   * @param array $record The record
   * @param string $mode The mode we're in
   * @return Array with the selected record
   */
  function _getSelectedRecord($record=array(), $mode="select")
  {
    $method = $this->fieldName()."_selected";
    if (method_exists($this->m_ownerInstance, $method))
    return $this->m_ownerInstance->$method($record, $mode);
    else return $this->getSelectedRecord($record, $mode);
  }

  /**
   * Returns the currently selected record.
   *
   * @param array $record The record
   * @param string $mode The mode we're in
   * @return Array with the selected record
   */
  function getSelectedRecord($record=array(), $mode="select")
  {
    $this->createDestination();

    $condition = $this->m_destInstance->m_table.'.'.$this->m_destInstance->primaryKeyField().
                   "='".$record[$this->fieldName()][$this->m_destInstance->primaryKeyField()]."'";

    $filter = $this->createFilter($record);
    if (!empty($filter))
    {
      $condition = $condition.' AND '.$filter;
    }

    $record = $this->m_destInstance->select($condition)->mode($mode)->firstRow();
    return $record;
  }

  /**
   * Returns the selectable records for this many-to-one relation. Uses
   * the owner instance $this->fieldName()."_selection" method if it exists.
   *
   * @param array $record The record
   * @param string $mode The mode we're in
   * @return Array with the selectable records
   */
  function _getSelectableRecords($record=array(), $mode="select")
  {
    $method = $this->fieldName()."_selection";
    if (method_exists($this->m_ownerInstance, $method))
    return $this->m_ownerInstance->$method($record, $mode);
    else return $this->getSelectableRecords($record, $mode);
  }

  /**
   * Is selectable record? Uses the owner instance $this->fieldName()."_selectable"
   * method if it exists.
   *
   * @param array $record The record
   * @param string $mode The mode we're in
   * @return Boolean to indicate if the record is selectable
   */
  function _isSelectableRecord($record=array(), $mode="select")
  {
    $method = $this->fieldName()."_selectable";
    if (method_exists($this->m_ownerInstance, $method))
    return $this->m_ownerInstance->$method($record, $mode);
    else return $this->isSelectableRecord($record, $mode);
  }

  /**
   * Create the destination filter for the given record.
   *
   * @param array $record
   * @return string filter
   */
  function createFilter($record)
  {
    if ($this->m_destinationFilter != "")
    {
      atkimport("atk.utils.atkstringparser");
      $parser = new atkStringParser($this->m_destinationFilter);
      return $parser->parse($record);
    }
    else
    {
      return "";
    }
  }

  /**
   * Is selectable record?
   *
   * Use this one from your selectable override when needed.
   *
   * @param array $record The record
   * @param string $mode The mode we're in
   * @return Boolean to indicate if the record is selectable
   */
  function isSelectableRecord($record=array(), $mode="select")
  {
    if ($record[$this->fieldName()] == NULL) return false;

    $this->createDestination();

    // if the value is set directly in the record field we first
    // need to convert the value to an array
    if (!is_array($record[$this->fieldName()]))
    {
      $record[$this->fieldName()] = array(
      $this->m_destInstance->primaryKeyField() => $record[$this->fieldName()]
      );
    }

    $selectedKey = $this->m_destInstance->primaryKey($record[$this->fieldName()]);
    if ($selectedKey == NULL) return false;

    // If custom selection method exists we use this one, although this is
    // way more inefficient, so if you create a selection override you should
    // also think about creating a selectable override!
    $method = $this->fieldName()."_selection";
    if (method_exists($this->m_ownerInstance, $method))
    {
      $rows = $this->m_ownerInstance->$method($record, $mode);
      foreach ($rows as $row)
      {
        $key = $this->m_destInstance->primaryKey($row);
        if ($key == $selectedKey) return true;
      }

      return false;
    }

    // No selection override exists, simply add the record key to the selector.
    $filter = $this->createFilter($record);
    $selector = "($selectedKey)".($filter != NULL ? " AND ($filter)" : "");
 
    return $this->m_destInstance->select($selector)->getRowCount() > 0;
  }

  /**
   * Returns the selectable records.
   *
   * Use this one from your selection override when needed.
   *
   * @param array $record The record
   * @param string $mode The mode we're in
   * @return Array with the selectable records
   */
  function getSelectableRecords($record=array(), $mode="select")
  {
    $this->createDestination();

    $selector = $this->createFilter($record);
    $result = 
      $this->m_destInstance
        ->select($selector)
        ->orderBy($this->getDestination()->getOrder())
        ->includes(atk_array_merge($this->m_destInstance->descriptorFields(),$this->m_destInstance->m_primaryKey))
        ->mode($mode)
        ->allRows();

    return $result;
  }

  /**
   * Returns the condition (SQL) that should be used when we want to join a relation's
   * owner node with the parent node.
   *
   * @param atkQuery $query The query object
   * @param String $tablename  The tablename on which to join
   * @param String $fieldalias The fieldalias
   * @return String SQL string for joining the owner with the destination.
   *                Returns false when impossible (f.e. attrib is not a relation).
   */
  function getJoinCondition(&$query, $tablename="",$fieldalias="")
  {
    if (!$this->createDestination()) return false;

    if ($tablename!="") $realtablename=$tablename;
    else $realtablename = $this->m_ownerInstance->m_table;
    $joinconditions = array();

    for ($i=0, $_i=count($this->m_refKey); $i<$_i; $i++)
    {
      $joinconditions[] = $realtablename.".".$this->m_refKey[$i].
        "=".
      $fieldalias.".".$this->m_destInstance->m_primaryKey[$i];
    }

    if ($this->m_joinFilter!="")
    {
      atkimport('atk.utils.atkstringparser');
      $parser = new atkStringParser($this->m_joinFilter);
      $filter = $parser->parse(array('table' => $realtablename, 'owner' => $realtablename, 'destination' => $fieldalias));
      $joinconditions[] = $filter;
    }
    return implode(" AND ",$joinconditions);
  }

  /**
   * Make this relation hide itself from the form when there are no items to select
   *
   * @param boolean $hidewhenempty true - hide when empty, false - always show
   */
  function setHideWhenEmpty($hidewhenempty)
  {
    $this->m_hidewhenempty = $hidewhenempty;
  }

  /**
   * Adds the attribute's edit / hide HTML code to the edit array.
   *
   * This method is called by the node if it wants the data needed to create
   * an edit form.
   *
   * This is a framework method, it should never be called directly.
   *
   * @param String $mode     the edit mode ("add" or "edit")
   * @param array  $arr      pointer to the edit array
   * @param array  $defaults pointer to the default values array
   * @param array  $error    pointer to the error array
   * @param String $fieldprefix   the fieldprefix
   */
  function addToEditArray($mode, &$arr, &$defaults, &$error, $fieldprefix)
  {
    if ($this->createDestination())
    {
      // check if destination table is empty
      // only check if hidewhenempty is set to true
      if ($this->m_hidewhenempty)
      {
        $recs = $this->_getSelectableRecords($defaults, 'select');
        if (count($recs)==0) return $this->hide($defaults, $fieldprefix);
      }
    }
    return parent::addToEditArray($mode, $arr, $defaults, $error, $fieldprefix);
  }

  /**
   * Retrieves the ORDER BY statement for the relation.
   *
   * @param Array $extra A list of attribute names to add to the order by
   *                     statement
   * @param String $table The table name (if not given uses the owner node's table name)
   * @param String $direction Sorting direction (ASC or DESC)
   * @return String The ORDER BY statement for this attribute
   */
  function getOrderByStatement($extra='', $table='', $direction='ASC')
  {
    if (!$this->createDestination())
    return parent::getOrderByStatement();

    if (!empty($table))
    {
      $table = $table.'_AE_'.$this->fieldName();
    }
    else
    {
      $table = $this->fieldName();
    }

    if (!empty($extra) && in_array($extra, $this->m_listColumns))
    {
      return $this->getDestination()->getAttribute($extra)->getOrderByStatement('', $table, $direction);
    }

    $order = $this->m_destInstance->getOrder();

    if (!empty($order))
    {
      $newParts = array();
      $parts = explode(',', $order);

      foreach ($parts as $part)
      {
        $split = preg_split('/\s+/', trim($part));
        $field = isset($split[0]) ? $split[0] : null;
        $fieldDirection = empty($split[1]) ? 'ASC' : strtoupper($split[1]);

        // if our default direction is DESC (the opposite of the default ASC)
        // we always have to switch the given direction to be the opposite, e.g.
        // DESC => ASC and ASC => DESC, this way we respect the default ordering
        // in the destination node even if the default is descending
        if ($fieldDirection == 'DESC')
        {
          $fieldDirection = $direction == 'DESC' ? 'ASC' : 'DESC';
        }
        else
        {
          $fieldDirection = $direction;
        }

        if (strpos($field, '.') !== false)
        {
          list(,$field) = explode('.', $field);
        }

        $newPart = $this->getDestination()->getAttribute($field)->getOrderByStatement('', $table, $fieldDirection);

        // realias if destination order contains the wrong tablename.
        if (strpos($newPart, $this->m_destInstance->m_table.'.') !== false)
        {
          $newPart= str_replace($this->m_destInstance->m_table.'.', $table.'.', $newPart);
        }
        $newParts[] = $newPart;
      }

      return implode(', ', $newParts);
    }
    else
    {
      $fields = $this->m_destInstance->descriptorFields();
      if (count($fields) == 0)
      $fields = array($this->m_destInstance->primaryKeyField());

      $order = "";
      foreach ($fields as $field)
      $order .= (empty($order) ? '' : ', ').$table.".".$field;

      return $order;
    }
  }

  /**
   * Adds the attribute / field to the list header. This includes the column name and search field.
   *
   * Framework method. It should not be necessary to call this method directly.
   *
   * @param String          $action       the action that is being performed on the node
   * @param array           $arr          reference to the the recordlist array
   * @param String          $fieldprefix  the fieldprefix
   * @param int             $flags        the recordlist flags
   * @param array           $atksearch    the current ATK search list (if not empty)
   * @param atkColumnConfig $columnConfig Column configuration object
   * @param atkDataGrid     $grid         The atkDataGrid this attribute lives on.
   * @param string          $column       child column (null for this attribute, * for this attribute and all childs)     
  */
  public function addToListArrayHeader($action, &$arr, $fieldprefix, $flags, $atksearch, $atkorderby, atkDataGrid $grid=null, $column='*')
  {
    if ($column == null || $column == '*')
    {
      $prefix = $fieldprefix.$this->fieldName()."_AE_";
      parent::addToListArrayHeader($action, $arr, $prefix, $flags, $atksearch[$this->fieldName()], $atkorderby, $grid, null);
    }
    
    if ($column == '*')
    {
      // only add extra columns when needed
      if ($this->hasFlag(AF_HIDE_LIST) && !$this->m_alwaysShowListColumns) return;
      if (!$this->createDestination() || count($this->m_listColumns) == 0) return;

      foreach ($this->m_listColumns as $column)
      {
        $this->_addColumnToListArrayHeader($column, $action, $arr, $fieldprefix, $flags, $atksearch, $atkorderby, $grid);
      }
    }
    else if ($column != null)
    {
      $this->_addColumnToListArrayHeader($column, $action, $arr, $fieldprefix, $flags, $atksearch, $atkorderby, $grid);
    }
  }
  
  /**
   * Adds the child attribute / field to the list row.
   *
   * Framework method. It should not be necessary to call this method directly.
   *
   * @param string      $column      child column (null for this attribute, * for this attribute and all childs)     
   * @param String      $action      the action that is being performed on the node
   * @param array       $arr         reference to the the recordlist array
   * @param String      $fieldprefix the fieldprefix
   * @param int         $flags       the recordlist flags
   * @param array       $atksearch   the current ATK search list (if not empty)
   * @param string      $atkorderby  order by
   * @param atkDataGrid $grid        The atkDataGrid this attribute lives on.
   */
  protected function _addColumnToListArrayHeader($column, $action, &$arr, $fieldprefix, $flags, $atksearch, $atkorderby, atkDataGrid $grid=null)
  {
    $prefix = $fieldprefix.$this->fieldName()."_AE_";

    $p_attrib = $this->m_destInstance->getAttribute($column);
    if ($p_attrib == null)
    {
      throw new Exception("Invalid list column {$column} for atkManyToOneRelation ".$this->getOwnerInstance()->atkNodeType().'::'.$this->fieldName());
    }
    
    $p_attrib->m_flags |= AF_HIDE_LIST;
    $p_attrib->m_flags ^= AF_HIDE_LIST;
    $p_attrib->addToListArrayHeader($action, $arr, $prefix, $flags, $atksearch[$this->fieldName()], $atkorderby, $grid, null);

    // fix order by clause
    $needle = $prefix.$column;
    foreach (array_keys($arr['heading']) as $key)
    {
      if (strpos($key, $needle) !== 0) continue;
      
      $order = $arr['heading'][$key]['order'];
      if (empty($order)) continue;
      
      $order = $this->fieldName().'.'.$order;

      if (is_object($atkorderby) &&
      isset($atkorderby->m_colcfg[$this->fieldName()])
          && isset($atkorderby->m_colcfg[$this->fieldName()]['extra'])
          && $atkorderby->m_colcfg[$this->fieldName()]['extra'] == $column)
      {
        $direction = $atkorderby->getDirection($this->fieldName());
        if ($direction=="asc") $order.=" desc";
      }

      $arr['heading'][$key]['order'] = $order;
    }
  }

  /**
   * Adds the attribute / field to the list row. And if the row is totalisable also to the total.
   *
   * Framework method. It should not be necessary to call this method directly.
   *
   * @param String      $action      the action that is being performed on the node
   * @param array       $arr         reference to the the recordlist array
   * @param int         $nr          the current row number
   * @param String      $fieldprefix the fieldprefix
   * @param int         $flags       the recordlist flags
   * @param boolean     $edit        editing?
   * @param atkDataGrid $grid        data grid
   * @param string      $column      child column (null for this attribute, * for this attribute and all childs)
   */
  public function addToListArrayRow($action, &$arr, $nr, $fieldprefix, $flags, $edit=false, atkDataGrid $grid=null, $column='*')
  {
    if ($column == null || $column == '*')
    {
      $prefix = $fieldprefix.$this->fieldName()."_AE_";      
      parent::addToListArrayRow($action, $arr, $nr, $prefix, $flags, $edit, $grid, null);
    }
    
    if ($column == '*')
    {
      // only add extra columns when needed
      if ($this->hasFlag(AF_HIDE_LIST) && !$this->m_alwaysShowListColumns) return;
      if (!$this->createDestination() || count($this->m_listColumns) == 0) return;

      foreach ($this->m_listColumns as $column)
      {
        $this->_addColumnToListArrayRow($column, $action, $arr, $nr, $fieldprefix, $flags, $edit, $grid);
      }
    }
    else if ($column != null)
    {
      $this->_addColumnToListArrayRow($column, $action, $arr, $nr, $fieldprefix, $flags, $edit, $grid);      
    }    
  }

  /**
   * Adds the child attribute / field to the list row.
   *
   * @param string      $column      child attribute name
   * @param String      $action      the action that is being performed on the node
   * @param array       $arr         reference to the the recordlist array
   * @param int         $nr          the current row number
   * @param String      $fieldprefix the fieldprefix
   * @param int         $flags       the recordlist flags
   * @param boolean     $edit        editing?
   * @param atkDataGrid $grid        data grid
   */
  protected function _addColumnToListArrayRow($column, $action, &$arr, $nr, $fieldprefix, $flags, $edit=false, atkDataGrid $grid=null)
  {
    $prefix = $fieldprefix.$this->fieldName()."_AE_";

    // small trick, the destination record is in a subarray. The destination
    // addToListArrayRow will not expect this though, so we have to modify the
    // record a bit before passing it to the detail columns.
    $backup = $arr["rows"][$nr]["record"];
    $arr["rows"][$nr]["record"] = $arr["rows"][$nr]["record"][$this->fieldName()];

    $p_attrib = $this->m_destInstance->getAttribute($column);
    if ($p_attrib == null)
    {
      throw new Exception("Invalid list column {$column} for atkManyToOneRelation ".$this->getOwnerInstance()->atkNodeType().'::'.$this->fieldName());
    }

    $p_attrib->m_flags |= AF_HIDE_LIST;
    $p_attrib->m_flags ^= AF_HIDE_LIST;

    $p_attrib->addToListArrayRow($action, $arr, $nr, $prefix, $flags, $edit, $grid, null);

    $arr["rows"][$nr]["record"] = $backup;
  }

  /**
   * Adds the needed searchbox(es) for this attribute to the fields array. This
   * method should only be called by the atkSearchHandler.
   * Overridden method; in the integrated version, we should let the destination
   * attributes hook themselves into the fieldlist instead of hooking the relation
   * in it.
   *
   * @param array   $fields            The array containing fields to use in the
   *                                   extended search
   * @param atkNode $node              The node where the field is in
   * @param array   $record            A record containing default values to put
   *                                   into the search fields.
   * @param array   $fieldprefix       search / mode field prefix
   */
  function addToSearchformFields(&$fields, &$node, &$record, $fieldprefix = "")
  {
    $prefix = $fieldprefix.$this->fieldName()."_AE_";

    parent::addToSearchformFields($fields, $node, $record, $prefix);

    // only add extra columns when needed
    if ($this->hasFlag(AF_HIDE_LIST) && !$this->m_alwaysShowListColumns) return;
    if (!$this->createDestination() || count($this->m_listColumns) == 0) return;

    foreach ($this->m_listColumns as $attribname)
    {
      $p_attrib = &$this->m_destInstance->m_attribList[$attribname];
      $p_attrib->m_flags |= AF_HIDE_LIST;
      $p_attrib->m_flags ^= AF_HIDE_LIST;

      if (!$p_attrib->hasFlag(AF_HIDE_SEARCH))
      {
        $p_attrib->addToSearchformFields($fields,$node,$record[$this->fieldName()], $prefix);
      }
    }
  }

  /**
   * Retrieve the sortorder for the listheader based on the
   * atkColumnConfig
   *
   * @param atkColumnConfig $columnConfig The config that contains options for
   *                                      extended sorting and grouping to a
   *                                      recordlist.
   * @return String Returns sort order ASC or DESC
   */
  function listHeaderSortOrder(&$columnConfig)
  {
    $order = $this->fieldName();

    // only add desc if not one of the listColumns is used for the sorting
    if (isset($columnConfig->m_colcfg[$order]) && empty($columnConfig->m_colcfg[$order]['extra']))
    {
      $direction = $columnConfig->getDirection($order);
      if ($direction=="asc") $order.=" desc";
    }

    return $order;
  }

  /**
   * Creates and registers the on change handler caller function.
   * This method will be used to message listeners for a change
   * event as soon as a new value is selected.
   *
   * @param string $fieldId
   * @param string $fieldPrefix
   * @param string $none
   * @return String function name
   */
  function createOnChangeCaller($fieldId, $fieldPrefix, $none='null')
  {
    $function = $none;
    if (count($this->m_onchangecode) > 0)
    {
      $function = "{$fieldId}_callChangeHandler";

      $js = "
          function {$function}() {
          {$fieldId}_onChange(\$('{$fieldId}'));
          }
        ";

          $this->m_onchangehandler_init = "newvalue = el.value;\n";
          $page = &$this->m_ownerInstance->getPage();
          $page->register_scriptcode($js);
          $this->_renderChangeHandler($fieldPrefix);
    }

    return $function;
  }

  /**
   * Draw the auto-complete box.
   *
   * @param array $record The record
   * @param string $fieldPrefix The fieldprefix
   * @param string $mode The mode we're in
   */
  function drawAutoCompleteBox($record, $fieldPrefix, $mode)
  {
    $this->createDestination();

    // register base JavaScript code and stylesheet
    $page = &$this->m_ownerInstance->getPage();
    $page->register_script(atkconfig('atkroot').'atk/javascript/class.atkmanytoonerelation.js');
    $this->m_ownerInstance->addStyle("atkmanytoonerelation.css");

    $id = $this->getHtmlId($fieldPrefix);

    // validate is this is a selectable record and if so
    // retrieve the display label and hidden value
    if ($this->_isSelectableRecord($record, 'select'))
    {
      $current = $record[$this->fieldName()];
      $label = $this->m_destInstance->descriptor($record[$this->fieldName()]);
      $value = $this->m_destInstance->primaryKey($record[$this->fieldName()]);
    }
    else
    {
      $current = NULL;
      $label = '';
      $value = '';
    }

    // create the widget
    $links = $this->createSelectAndAutoLinks($id, $record);

    $result =
        '<input type="hidden" id="'.$id.'" name="'.$id.'" value="'.$value.'" />
         <input type="text" id="'.$id.'_search" value="'.atk_htmlentities($label).'" class="atkmanytoonerelation_search" size="30" onfocus="this.select()" />
         <img id="'.$id.'_spinner" src="atk/images/spinner.gif" style="vertical-align: middle; display: none"> '.$links.'
         <div id="'.$id.'_result" style="display: none" class="atkmanytoonerelation_result"></div>';

    // register JavaScript code that attaches the auto-complete behaviour to the search box
    $url = partial_url($this->m_ownerInstance->atkNodeType(), $mode, 'attribute.'.$this->fieldName().'.autocomplete');
    $function = $this->createOnChangeCaller($id, $fieldPrefix);
    $code = "ATK.ManyToOneRelation.completeEdit('{$id}_search', '{$id}_result', '$id', '{$id}_spinner', '$url', $function, 1);";
    $page->register_loadscript($code);

    return $result;
  }

  /**
   * Auto-complete partial.
   *
   * @param string $mode add/edit mode?
   */
  function partial_autocomplete($mode)
  {
    $searchvalue = $this->m_ownerInstance->m_postvars['value'];
    if (atk_strlen($searchvalue) < $this->m_autocomplete_minchars)
    {
      return '<ul><li class="minimum_chars">'.sprintf($this->text('autocomplete_minimum_chars'), $this->m_autocomplete_minchars).'</li></ul>';
    }

    $this->createDestination();

    $fieldprefix = (isset($this->m_ownerInstance->m_postvars['atkfieldprefix'])?$this->m_ownerInstance->m_postvars['atkfieldprefix']:"");
    $searchvalue = $this->escapeSQL($searchvalue);
    $record = $this->m_ownerInstance->updateRecord();

    $filter = $this->createSearchFilter($searchvalue);
    $this->addDestinationFilter($filter);

    $records = $this->_getSelectableRecords($record, 'select');

    if (count($records) == 0)
    {
      if(in_array($this->m_autocomplete_searchmode,array("exact","startswith","contains")))
      $str = $this->text('autocomplete_no_results_'.$this->m_autocomplete_searchmode);
      else
      $str = $this->text('autocomplete_no_results');

      return '<ul><li class="no_results">'.$str.'</li></ul>';
    }

    $result = '';
    foreach ($records as $rec)
    {
      $option = atk_htmlentities($this->m_destInstance->descriptor($rec));
      $value = $this->m_destInstance->primaryKey($rec);
      $highlightedOption = $this->highlight_search_result_match($searchvalue, $option);
      $result .= '
          <li title="'.$option.'">
            '.$highlightedOption.'
            <span class="selection" style="display: none">'.$option.'</span>
            <span class="value" style="display: none">'.$value.'</span>
          </li>';
    }

    return "<ul>$result</ul>";
  }

  function highlight_search_result_match($search_value, $result)
  {
      $escaped_searchvalue = str_replace('/', '\/', preg_quote($search_value));
      return preg_replace('/('.$escaped_searchvalue.')/i', '<span class="atkmanytoone_highlite">\\1</span>', $result);
  }

  /**
   * Auto-complete search partial.
   *
   * @return HTML code with autocomplete result
   */
  function partial_autocomplete_search()
  {
    $this->createDestination();

    $searchvalue = $this->m_ownerInstance->m_postvars['value'];
    $searchvalue = $this->escapeSQL($searchvalue);
    $filter = $this->createSearchFilter($searchvalue);
    $this->addDestinationFilter($filter);

    $record = array();
    $records = $this->_getSelectableRecords($record, 'search');

    $result = '';
    foreach ($records as $rec)
    {
      $option = $this->m_destInstance->descriptor($rec);
      $value = $this->m_destInstance->primaryKey($rec);
      $result .= '
          <li title="'.atk_htmlentities($option).'">'.atk_htmlentities($option).'</li>';
    }

    return "<ul>$result</ul>";
  }

  /**
   * Creates a search filter with the given search value on the given
   * descriptor fields
   *
   * @param String $searchvalue A searchstring
   * @return String a search string (WHERE clause)
   */
  function createSearchFilter($searchvalue)
  {
    if($this->m_autocomplete_searchfields=="")
    $searchfields = $this->m_destInstance->descriptorFields();
    else
    $searchfields = $this->m_autocomplete_searchfields;

    $parts = preg_split('/\s+/', $searchvalue);

    $mainFilter = array();
    foreach ($parts as $part)
    {
      $filter = array();
      foreach($searchfields as $attribname)
      {
        if (strstr($attribname, '.')) $table = '';
        else $table = $this->m_destInstance->m_table.".";

        if(!$this->m_autocomplete_search_case_sensitive)
        $tmp = "LOWER(".$table.$attribname.")";
        else
        $tmp = $table.$attribname;

        switch($this->m_autocomplete_searchmode)
        {
          case self::SEARCH_MODE_EXACT:
            if(!$this->m_autocomplete_search_case_sensitive)
            $tmp.= " = LOWER('{$part}')";
            else
            $tmp.= " = '{$part}'";
            break;
          case self::SEARCH_MODE_STARTSWITH:
            if(!$this->m_autocomplete_search_case_sensitive)
            $tmp.= " LIKE LOWER('{$part}%')";
            else
            $tmp.= " LIKE '{$part}%'";
            break;
          case self::SEARCH_MODE_CONTAINS:
            if(!$this->m_autocomplete_search_case_sensitive)
            $tmp.= " LIKE LOWER('%{$part}%')";
            else
            $tmp.= " LIKE '%{$part}%'";
            break;
          default:
            $tmp.= " = LOWER('{$part}')";
        }

        $filter[] = $tmp;
      }

      if (count($filter) > 0)
      $mainFilter[] = "(".implode(") OR (", $filter).")";
    }

    if (count($mainFilter) > 0)
    $searchFilter =  "(".implode(") AND (", $mainFilter).")";
    else $searchFilter = "";

    // When no searchfields are specified and we use the CONTAINS mode
    // add a concat filter
    if($this->m_autocomplete_searchmode == self::SEARCH_MODE_CONTAINS && $this->m_autocomplete_searchfields=="")
    {
      $filter = $this->getConcatFilter($searchvalue);
      if($filter)
      {
        if($searchFilter!='') $searchFilter.= " OR ";
        $searchFilter.= $filter;
      }
    }
    return $searchFilter;
  }


  /**
   * Get Concat filter
   *
   * @param string $searchValue Search value
   * @param string $fieldaliasprefix Field alias prefix
   * @return string|boolean
   */
  function getConcatFilter($searchValue,$fieldaliasprefix="")
  {
    // If we have a descriptor with multiple fields, use CONCAT
    $attribs = $this->m_destInstance->descriptorFields();
    if(count($attribs)>1)
    {
      $fields = array();
      foreach($attribs as $attribname)
      {
        $post = '';
        if (strstr($attribname, '.'))
        {
          if ($fieldaliasprefix != '') $table = $fieldaliasprefix.'_AE_';
          else $table = '';
          $post = substr($attribname,strpos($attribname,'.'));
          $attribname = substr($attribname,0,strpos($attribname,'.'));
        }
        elseif($fieldaliasprefix!='') $table = $fieldaliasprefix.".";
        else $table = $this->m_destInstance->m_table.".";

        $p_attrib = $this->m_destInstance->m_attribList[$attribname];
        $fields[$p_attrib->fieldName()] = $table.$p_attrib->fieldName().$post;
      }
      $value = $this->escapeSQL(trim($searchValue));
      $value = str_replace("  " , " ", $value);
      if(!$value)
      {
        return false;
      }
      else
      {
        $function = $this->getConcatDescriptorFunction();
        if ($function != '' && method_exists($this->m_destInstance, $function))
        {
          $descriptordef = $this->m_destInstance->$function();
        }
        elseif ($this->m_destInstance->m_descTemplate != NULL)
        {
          $descriptordef = $this->m_destInstance->m_descTemplate;
        }
        elseif(method_exists($this->m_destInstance,"descriptor_def"))
        {
          $descriptordef = $this->m_destInstance->descriptor_def();
        }
        else
        {
          $descriptordef = $this->m_destInstance->descriptor();
        }

        atkimport("atk.utils.atkstringparser");
        $parser = new atkStringParser($descriptordef);
        $concatFields = $parser->getAllParsedFieldsAsArray($fields, true);
        $concatTags       = $concatFields['tags'];
        $concatSeparators = $concatFields['separators'];

        // to search independent of characters between tags, like spaces and comma's,
        // we remove all these separators so we can search for just the concatenated tags in concat_ws [Jeroen]
        foreach ($concatSeparators as $separator)
        {
          $value = str_replace($separator, "", $value);
        }

        $db = $this->getDb();
        $searchcondition = "UPPER(".$db->func_concat_ws($concatTags, "", true).") LIKE UPPER('%".$value."%')";

      }
      return $searchcondition;
    }
    return false;
  }

}
?>
Back to Top