controller.php
75.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
<?php
/**
* @package FrameworkOnFramework
* @subpackage controller
* @copyright Copyright (C) 2010-2016 Nicholas K. Dionysopoulos / Akeeba Ltd. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
// Protect from unauthorized access
defined('FOF_INCLUDED') or die;
/**
* FrameworkOnFramework controller class. FOF is based on the thin controller
* paradigm, where the controller is mainly used to set up the model state and
* spawn the view.
*
* @package FrameworkOnFramework
* @since 1.0
*/
class FOFController extends FOFUtilsObject
{
/**
* @var int Bit mask to enable Routing on redirects.
* 0 = never
* 1 = frontend only
* 2 = backend only
* 3 = always
*/
protected $autoRouting = 0;
/**
* The current component's name without the com_ prefix
*
* @var string
*/
protected $bareComponent = 'foobar';
/**
* The base path of the controller
*
* @var string
*/
protected $basePath;
/**
* The tasks for which caching should be enabled by default
*
* @var array
*/
protected $cacheableTasks = array('browse', 'read');
/**
* The current component's name; you can override it in the configuration
*
* @var string
*/
protected $component = 'com_foobar';
/**
* A cached copy of the class configuration parameter passed during initialisation
*
* @var array
*/
protected $config = array();
/**
* An instance of FOFConfigProvider to provision configuration overrides
*
* @var FOFConfigProvider
*/
protected $configProvider = null;
/**
* Set to true to enable CSRF protection on selected tasks. The possible
* values are:
* 0 Disabled; no token checks are performed
* 1 Enabled; token checks are always performed
* 2 Only on HTML requests and backend; token checks are always performed in the back-end and in the front-end only when format is 'html'
* 3 Only on back-end; token checks are performer only in the back-end
*
* @var integer
*/
protected $csrfProtection = 2;
/**
* The default view for the display method.
*
* @var string
*/
protected $default_view;
/**
* The mapped task that was performed.
*
* @var string
*/
protected $doTask;
/**
* The input object for this MVC triad; you can override it in the configuration
*
* @var FOFInput
*/
protected $input = array();
/**
* Redirect message.
*
* @var string
*/
protected $message;
/**
* Redirect message type.
*
* @var string
*/
protected $messageType;
/**
* The current layout; you can override it in the configuration
*
* @var string
*/
protected $layout = null;
/**
* Array of class methods
*
* @var array
*/
protected $methods;
/**
* The prefix of the models
*
* @var string
*/
protected $model_prefix;
/**
* Overrides the name of the view's default model
*
* @var string
*/
protected $modelName = null;
/**
* The set of search directories for resources (views).
*
* @var array
*/
protected $paths;
/**
* URL for redirection.
*
* @var string
*/
protected $redirect;
/**
* Current or most recently performed task.
*
* @var string
*/
protected $task;
/**
* Array of class methods to call for a given task.
*
* @var array
*/
protected $taskMap;
/**
* The name of the controller
*
* @var array
*/
protected $name;
/**
* The current view name; you can override it in the configuration
*
* @var string
*/
protected $view = '';
/**
* Overrides the name of the view's default view
*
* @var string
*/
protected $viewName = null;
/**
* A copy of the FOFView object used in this triad
*
* @var FOFView
*/
private $_viewObject = null;
/**
* A cache for the view item objects created in this controller
*
* @var array
*/
protected $viewsCache = array();
/**
* A copy of the FOFModel object used in this triad
*
* @var FOFModel
*/
private $_modelObject = null;
/**
* Does this tried have a FOFForm which will be used to render it?
*
* @var boolean
*/
protected $hasForm = false;
/**
* Gets a static (Singleton) instance of a controller class. It loads the
* relevant controller file from the component's directory or, if it doesn't
* exist, creates a new controller object out of thin air.
*
* @param string $option Component name, e.g. com_foobar
* @param string $view The view name, also used for the controller name
* @param array $config Configuration parameters
*
* @return FOFController
*/
public static function &getAnInstance($option = null, $view = null, $config = array())
{
static $instances = array();
// Make sure $config is an array
if (is_object($config))
{
$config = (array) $config;
}
elseif (!is_array($config))
{
$config = array();
}
$hash = $option . $view;
if (!array_key_exists($hash, $instances))
{
$instances[$hash] = self::getTmpInstance($option, $view, $config);
}
return $instances[$hash];
}
/**
* Gets a temporary instance of a controller object. A temporary instance is
* not a Singleton and can be disposed off after use.
*
* @param string $option The component name, e.g. com_foobar
* @param string $view The view name, e.g. cpanel
* @param array $config Configuration parameters
*
* @return \FOFController A disposable class instance
*/
public static function &getTmpInstance($option = null, $view = null, $config = array())
{
// Make sure $config is an array
if (is_object($config))
{
$config = (array) $config;
}
elseif (!is_array($config))
{
$config = array();
}
// Get an input object
if (array_key_exists('input', $config))
{
$input = $config['input'];
}
else
{
$input = null;
}
if (array_key_exists('input_options', $config))
{
$input_options = $config['input_options'];
}
else
{
$input_options = array();
}
if (!($input instanceof FOFInput))
{
$input = new FOFInput($input, $input_options);
}
// Determine the option (component name) and view
$config['option'] = !is_null($option) ? $option : $input->getCmd('option', 'com_foobar');
$config['view'] = !is_null($view) ? $view : $input->getCmd('view', 'cpanel');
// Get the class base name, e.g. FoobarController
$classBaseName = ucfirst(str_replace('com_', '', $config['option'])) . 'Controller';
// Get the class name suffixes, in the order to be searched for: plural, singular, 'default'
$classSuffixes = array(
FOFInflector::pluralize($config['view']),
FOFInflector::singularize($config['view']),
'default'
);
// Get the path names for the component
$componentPaths = FOFPlatform::getInstance()->getComponentBaseDirs($config['option']);
$filesystem = FOFPlatform::getInstance()->getIntegrationObject('filesystem');
// Look for the best classname match
foreach ($classSuffixes as $suffix)
{
$className = $classBaseName . ucfirst($suffix);
if (class_exists($className))
{
// The class is already loaded. We have a match!
break;
}
// The class is not already loaded. Try to find and load it.
$searchPaths = array(
$componentPaths['main'] . '/controllers',
$componentPaths['admin'] . '/controllers'
);
// If we have a searchpath in the configuration please search it first
if (array_key_exists('searchpath', $config))
{
array_unshift($searchPaths, $config['searchpath']);
}
else
{
$configProvider = new FOFConfigProvider;
$searchPath = $configProvider->get($config['option'] . '.views.' . FOFInflector::singularize($config['view']) . '.config.searchpath', null);
if ($searchPath)
{
array_unshift($searchPaths, $componentPaths['admin'] . '/' . $searchPath);
array_unshift($searchPaths, $componentPaths['main'] . '/' . $searchPath);
}
}
/**
* Try to find the path to this file. First try to find the
* format-specific controller file, e.g. foobar.json.php for
* format=json, then the regular one-size-fits-all controller
*/
$format = $input->getCmd('format', 'html');
$path = null;
if (!empty($format))
{
$path = $filesystem->pathFind(
$searchPaths, strtolower($suffix) . '.' . strtolower($format) . '.php'
);
}
if (!$path)
{
$path = $filesystem->pathFind(
$searchPaths, strtolower($suffix) . '.php'
);
}
// The path is found. Load the file and make sure the expected class name exists.
if ($path)
{
require_once $path;
if (class_exists($className))
{
// The class was loaded successfully. We have a match!
break;
}
}
}
if (!class_exists($className))
{
// If no specialised class is found, instantiate the generic FOFController
$className = 'FOFController';
}
$instance = new $className($config);
return $instance;
}
/**
* Public constructor of the Controller class
*
* @param array $config Optional configuration parameters
*/
public function __construct($config = array())
{
// Make sure $config is an array
if (is_object($config))
{
$config = (array) $config;
}
elseif (!is_array($config))
{
$config = array();
}
$this->methods = array();
$this->message = null;
$this->messageType = 'message';
$this->paths = array();
$this->redirect = null;
$this->taskMap = array();
// Cache the config
$this->config = $config;
// Get the input for this MVC triad
if (array_key_exists('input', $config))
{
$input = $config['input'];
}
else
{
$input = null;
}
if (array_key_exists('input_options', $config))
{
$input_options = $config['input_options'];
}
else
{
$input_options = array();
}
if ($input instanceof FOFInput)
{
$this->input = $input;
}
else
{
$this->input = new FOFInput($input, $input_options);
}
// Load the configuration provider
$this->configProvider = new FOFConfigProvider;
// Determine the methods to exclude from the base class.
$xMethods = get_class_methods('FOFController');
// Some methods must always be considered valid tasks
$iMethods = array('accesspublic', 'accessregistered', 'accessspecial',
'add', 'apply', 'browse', 'cancel', 'copy', 'edit', 'orderdown',
'orderup', 'publish', 'read', 'remove', 'save', 'savenew',
'saveorder', 'unpublish', 'display', 'archive', 'trash', 'loadhistory');
// Get the public methods in this class using reflection.
$r = new ReflectionClass($this);
$rMethods = $r->getMethods(ReflectionMethod::IS_PUBLIC);
foreach ($rMethods as $rMethod)
{
$mName = $rMethod->getName();
// If the developer screwed up and declared one of the helper method public do NOT make them available as
// tasks.
if ((substr($mName, 0, 8) == 'onBefore') || (substr($mName, 0, 7) == 'onAfter') || substr($mName, 0, 1) == '_')
{
continue;
}
// Add default display method if not explicitly declared.
if (!in_array($mName, $xMethods) || in_array($mName, $iMethods))
{
$this->methods[] = strtolower($mName);
// Auto register the methods as tasks.
$this->taskMap[strtolower($mName)] = $mName;
}
}
// Get the default values for the component and view names
$classNameParts = FOFInflector::explode(get_class($this));
if (count($classNameParts) == 3)
{
$defComponent = "com_" . $classNameParts[0];
$defView = $classNameParts[2];
}
else
{
$defComponent = 'com_foobar';
$defView = 'cpanel';
}
$this->component = $this->input->get('option', $defComponent, 'cmd');
$this->view = $this->input->get('view', $defView, 'cmd');
$this->layout = $this->input->get('layout', null, 'cmd');
// Overrides from the config
if (array_key_exists('option', $config))
{
$this->component = $config['option'];
}
if (array_key_exists('view', $config))
{
$this->view = $config['view'];
}
if (array_key_exists('layout', $config))
{
$this->layout = $config['layout'];
}
$this->layout = $this->configProvider->get($this->component . '.views.' . FOFInflector::singularize($this->view) . '.config.layout', $this->layout);
$this->input->set('option', $this->component);
// Set the bareComponent variable
$this->bareComponent = str_replace('com_', '', strtolower($this->component));
// Set the $name variable
$this->name = $this->bareComponent;
// Set the basePath variable
$componentPaths = FOFPlatform::getInstance()->getComponentBaseDirs($this->component);
$basePath = $componentPaths['main'];
if (array_key_exists('base_path', $config))
{
$basePath = $config['base_path'];
}
$altBasePath = $this->configProvider->get(
$this->component . '.views.' .
FOFInflector::singularize($this->view) . '.config.base_path', null
);
if (!is_null($altBasePath))
{
$platformDirs = FOFPlatform::getInstance()->getPlatformBaseDirs();
$basePath = $platformDirs['public'] . '/' . $altBasePath;
}
$this->basePath = $basePath;
// If the default task is set, register it as such
$defaultTask = $this->configProvider->get(
$this->component . '.views.' .
FOFInflector::singularize($this->view) . '.config.default_task', 'display'
);
if (array_key_exists('default_task', $config))
{
$this->registerDefaultTask($config['default_task']);
}
else
{
$this->registerDefaultTask($defaultTask);
}
// Set the models prefix
if (empty($this->model_prefix))
{
if (array_key_exists('model_prefix', $config))
{
// User-defined prefix
$this->model_prefix = $config['model_prefix'];
}
else
{
$this->model_prefix = $this->name . 'Model';
$this->model_prefix = $this->configProvider->get(
$this->component . '.views.' .
FOFInflector::singularize($this->view) . '.config.model_prefix', $this->model_prefix
);
}
}
// Set the default model search path
if (array_key_exists('model_path', $config))
{
// User-defined dirs
$this->addModelPath($config['model_path'], $this->model_prefix);
}
else
{
$modelPath = $this->basePath . '/models';
$altModelPath = $this->configProvider->get(
$this->component . '.views.' .
FOFInflector::singularize($this->view) . '.config.model_path', null
);
if (!is_null($altModelPath))
{
$modelPath = $this->basePath . '/' . $altModelPath;
}
$this->addModelPath($modelPath, $this->model_prefix);
}
// Set the default view search path
if (array_key_exists('view_path', $config))
{
// User-defined dirs
$this->setPath('view', $config['view_path']);
}
else
{
$viewPath = $this->basePath . '/views';
$altViewPath = $this->configProvider->get(
$this->component . '.views.' .
FOFInflector::singularize($this->view) . '.config.view_path', null
);
if (!is_null($altViewPath))
{
$viewPath = $this->basePath . '/' . $altViewPath;
}
$this->setPath('view', $viewPath);
}
// Set the default view.
if (array_key_exists('default_view', $config))
{
$this->default_view = $config['default_view'];
}
else
{
if (empty($this->default_view))
{
$this->default_view = $this->getName();
}
$this->default_view = $this->configProvider->get(
$this->component . '.views.' .
FOFInflector::singularize($this->view) . '.config.default_view', $this->default_view
);
}
// Set the CSRF protection
if (array_key_exists('csrf_protection', $config))
{
$this->csrfProtection = $config['csrf_protection'];
}
$this->csrfProtection = $this->configProvider->get(
$this->component . '.views.' .
FOFInflector::singularize($this->view) . '.config.csrf_protection', $this->csrfProtection
);
// Set any model/view name overrides
if (array_key_exists('viewName', $config))
{
$this->setThisViewName($config['viewName']);
}
else
{
$overrideViewName = $this->configProvider->get(
$this->component . '.views.' .
FOFInflector::singularize($this->view) . '.config.viewName', null
);
if ($overrideViewName)
{
$this->setThisViewName($overrideViewName);
}
}
if (array_key_exists('modelName', $config))
{
$this->setThisModelName($config['modelName']);
}
else
{
$overrideModelName = $this->configProvider->get(
$this->component . '.views.' .
FOFInflector::singularize($this->view) . '.config.modelName', null
);
if ($overrideModelName)
{
$this->setThisModelName($overrideModelName);
}
}
// Caching
if (array_key_exists('cacheableTasks', $config))
{
if (is_array($config['cacheableTasks']))
{
$this->cacheableTasks = $config['cacheableTasks'];
}
}
else
{
$cacheableTasks = $this->configProvider->get(
$this->component . '.views.' .
FOFInflector::singularize($this->view) . '.config.cacheableTasks', null
);
if ($cacheableTasks)
{
$cacheableTasks = explode(',', $cacheableTasks);
if (count($cacheableTasks))
{
$temp = array();
foreach ($cacheableTasks as $t)
{
$temp[] = trim($t);
}
$temp = array_unique($temp);
$this->cacheableTasks = $temp;
}
}
}
// Bit mask for auto routing on setRedirect
$this->autoRouting = $this->configProvider->get(
$this->component . '.views.' .
FOFInflector::singularize($this->view) . '.config.autoRouting', $this->autoRouting
);
if (array_key_exists('autoRouting', $config))
{
$this->autoRouting = $config['autoRouting'];
}
// Apply task map
$taskmap = $this->configProvider->get(
$this->component . '.views.' .
FOFInflector::singularize($this->view) . '.taskmap'
);
if (is_array($taskmap) && !empty($taskmap))
{
foreach ($taskmap as $aliasedtask => $realmethod)
{
$this->registerTask($aliasedtask, $realmethod);
}
}
}
/**
* Adds to the stack of model paths in LIFO order.
*
* @param mixed $path The directory (string) , or list of directories (array) to add.
* @param string $prefix A prefix for models
*
* @return void
*/
public static function addModelPath($path, $prefix = '')
{
FOFModel::addIncludePath($path, $prefix);
}
/**
* Adds to the search path for templates and resources.
*
* @param string $type The path type (e.g. 'model', 'view').
* @param mixed $path The directory string or stream array to search.
*
* @return FOFController A FOFController object to support chaining.
*/
protected function addPath($type, $path)
{
// Just force path to array
settype($path, 'array');
$filesystem = FOFPlatform::getInstance()->getIntegrationObject('filesystem');
if (!isset($this->paths[$type]))
{
$this->paths[$type] = array();
}
// Loop through the path directories
foreach ($path as $dir)
{
// No surrounding spaces allowed!
$dir = rtrim($filesystem->pathCheck($dir, '/'), '/') . '/';
// Add to the top of the search dirs
array_unshift($this->paths[$type], $dir);
}
return $this;
}
/**
* Add one or more view paths to the controller's stack, in LIFO order.
*
* @param mixed $path The directory (string) or list of directories (array) to add.
*
* @return FOFController This object to support chaining.
*/
public function addViewPath($path)
{
$this->addPath('view', $path);
return $this;
}
/**
* Authorisation check
*
* @param string $task The ACO Section Value to check access on.
*
* @return boolean True if authorised
*
* @deprecated 2.0 Use JAccess instead.
*/
public function authorise($task)
{
FOFPlatform::getInstance()->logDeprecated(__CLASS__ . '::' .__METHOD__ . ' is deprecated. Use checkACL() instead.');
return true;
}
/**
* Create the filename for a resource.
*
* @param string $type The resource type to create the filename for.
* @param array $parts An associative array of filename information. Optional.
*
* @return string The filename.
*/
protected static function createFileName($type, $parts = array())
{
$filename = '';
switch ($type)
{
case 'controller':
if (!empty($parts['format']))
{
if ($parts['format'] == 'html')
{
$parts['format'] = '';
}
else
{
$parts['format'] = '.' . $parts['format'];
}
}
else
{
$parts['format'] = '';
}
$filename = strtolower($parts['name'] . $parts['format'] . '.php');
break;
case 'view':
if (!empty($parts['type']))
{
$parts['type'] = '.' . $parts['type'];
}
else
{
$parts['type'] = '';
}
$filename = strtolower($parts['name'] . '/view' . $parts['type'] . '.php');
break;
}
return $filename;
}
/**
* Executes a given controller task. The onBefore<task> and onAfter<task>
* methods are called automatically if they exist.
*
* @param string $task The task to execute, e.g. "browse"
*
* @throws Exception Exception thrown if the onBefore<task> returns false
*
* @return null|bool False on execution failure
*/
public function execute($task)
{
$this->task = $task;
$method_name = 'onBefore' . ucfirst($task);
if (!method_exists($this, $method_name))
{
$result = $this->onBeforeGenericTask($task);
}
elseif (method_exists($this, $method_name))
{
$result = $this->$method_name();
}
else
{
$result = true;
}
if ($result)
{
$plugin_event = FOFInflector::camelize('on before ' . $this->bareComponent . ' controller ' . $this->view . ' ' . $task);
$plugin_result = FOFPlatform::getInstance()->runPlugins($plugin_event, array(&$this, &$this->input));
if (in_array(false, $plugin_result, true))
{
$result = false;
}
}
if (!$result)
{
throw new Exception(JText::_('JLIB_APPLICATION_ERROR_ACCESS_FORBIDDEN'), 403);
}
// Do not allow the display task to be directly called
$task = strtolower($task);
if (isset($this->taskMap[$task]))
{
$doTask = $this->taskMap[$task];
}
elseif (isset($this->taskMap['__default']))
{
$doTask = $this->taskMap['__default'];
}
else
{
$doTask = null;
}
if ($doTask == 'display')
{
FOFPlatform::getInstance()->setHeader('Status', '400 Bad Request', true);
throw new Exception('Bad Request', 400);
}
$this->doTask = $doTask;
$ret = $this->$doTask();
$method_name = 'onAfter' . ucfirst($task);
if (method_exists($this, $method_name))
{
$result = $this->$method_name();
}
else
{
$result = true;
}
if ($result)
{
$plugin_event = FOFInflector::camelize('on after ' . $this->bareComponent . ' controller ' . $this->view . ' ' . $task);
$plugin_result = FOFPlatform::getInstance()->runPlugins($plugin_event, array(&$this, &$this->input, &$ret));
if (in_array(false, $plugin_result, true))
{
$result = false;
}
}
if (!$result)
{
throw new Exception(JText::_('JLIB_APPLICATION_ERROR_ACCESS_FORBIDDEN'), 403);
}
return $ret;
}
/**
* Default task. Assigns a model to the view and asks the view to render
* itself.
*
* YOU MUST NOT USETHIS TASK DIRECTLY IN A URL. It is supposed to be
* used ONLY inside your code. In the URL, use task=browse instead.
*
* @param bool $cachable Is this view cacheable?
* @param bool $urlparams Add your safe URL parameters (see further down in the code)
* @param string $tpl The name of the template file to parse
*
* @return bool
*/
public function display($cachable = false, $urlparams = false, $tpl = null)
{
$document = FOFPlatform::getInstance()->getDocument();
if ($document instanceof JDocument)
{
$viewType = $document->getType();
}
else
{
$viewType = $this->input->getCmd('format', 'html');
}
$view = $this->getThisView();
// Get/Create the model
if ($model = $this->getThisModel())
{
// Push the model into the view (as default)
$view->setModel($model, true);
}
// Set the layout
$view->setLayout(is_null($this->layout) ? 'default' : $this->layout);
// Display the view
$conf = FOFPlatform::getInstance()->getConfig();
if (FOFPlatform::getInstance()->isFrontend() && $cachable && ($viewType != 'feed') && $conf->get('caching') >= 1)
{
// Get a JCache object
$option = $this->input->get('option', 'com_foobar', 'cmd');
$cache = JFactory::getCache($option, 'view');
// Set up a cache ID based on component, view, task and user group assignment
$user = FOFPlatform::getInstance()->getUser();
if ($user->guest)
{
$groups = array();
}
else
{
$groups = $user->groups;
}
$importantParameters = array();
// Set up safe URL parameters
if (!is_array($urlparams))
{
$urlparams = array(
'option' => 'CMD',
'view' => 'CMD',
'task' => 'CMD',
'format' => 'CMD',
'layout' => 'CMD',
'id' => 'INT',
);
}
if (is_array($urlparams))
{
$app = JFactory::getApplication();
$registeredurlparams = null;
if (version_compare(JVERSION, '3.0', 'ge'))
{
if (property_exists($app, 'registeredurlparams'))
{
$registeredurlparams = $app->registeredurlparams;
}
}
else
{
$registeredurlparams = $app->get('registeredurlparams');
}
if (empty($registeredurlparams))
{
$registeredurlparams = new stdClass;
}
foreach ($urlparams AS $key => $value)
{
// Add your safe url parameters with variable type as value {@see JFilterInput::clean()}.
$registeredurlparams->$key = $value;
// Add the URL-important parameters into the array
$importantParameters[$key] = $this->input->get($key, null, $value);
}
if (version_compare(JVERSION, '3.0', 'ge'))
{
$app->registeredurlparams = $registeredurlparams;
}
else
{
$app->set('registeredurlparams', $registeredurlparams);
}
}
// Create the cache ID after setting the registered URL params, as they are used to generate the ID
$cacheId = md5(serialize(array(JCache::makeId(), $view->getName(), $this->doTask, $groups, $importantParameters)));
// Get the cached view or cache the current view
$cache->get($view, 'display', $cacheId);
}
else
{
// Display without caching
$view->display($tpl);
}
return true;
}
/**
* Implements a default browse task, i.e. read a bunch of records and send
* them to the browser.
*
* @return boolean
*/
public function browse()
{
if ($this->input->get('savestate', -999, 'int') == -999)
{
$this->input->set('savestate', true);
}
// Do I have a form?
$model = $this->getThisModel();
if (empty($this->layout))
{
$formname = 'form.default';
}
else
{
$formname = 'form.' . $this->layout;
}
$model->setState('form_name', $formname);
$form = $model->getForm();
if ($form !== false)
{
$this->hasForm = true;
}
$this->display(in_array('browse', $this->cacheableTasks));
return true;
}
/**
* Single record read. The id set in the request is passed to the model and
* then the item layout is used to render the result.
*
* @return bool
*/
public function read()
{
// Load the model
$model = $this->getThisModel();
if (!$model->getId())
{
$model->setIDsFromRequest();
}
// Set the layout to item, if it's not set in the URL
if (is_null($this->layout))
{
$this->layout = 'item';
}
// Do I have a form?
$model->setState('form_name', 'form.' . $this->layout);
$item = $model->getItem();
if (!($item instanceof FOFTable))
{
return false;
}
$itemKey = $item->getKeyName();
if ($item->$itemKey != $model->getId())
{
return false;
}
$formData = is_object($item) ? $item->getData() : array();
$form = $model->getForm($formData);
if ($form !== false)
{
$this->hasForm = true;
}
// Display
$this->display(in_array('read', $this->cacheableTasks));
return true;
}
/**
* Single record add. The form layout is used to present a blank page.
*
* @return false|void
*/
public function add()
{
// Load and reset the model
$model = $this->getThisModel();
$model->reset();
// Set the layout to form, if it's not set in the URL
if (!$this->layout)
{
$this->layout = 'form';
}
// Do I have a form?
$model->setState('form_name', 'form.' . $this->layout);
$item = $model->getItem();
if (!($item instanceof FOFTable))
{
return false;
}
$formData = is_object($item) ? $item->getData() : array();
$form = $model->getForm($formData);
if ($form !== false)
{
$this->hasForm = true;
}
// Display
$this->display(in_array('add', $this->cacheableTasks));
}
/**
* Single record edit. The ID set in the request is passed to the model,
* then the form layout is used to edit the result.
*
* @return bool
*/
public function edit()
{
// Load the model
$model = $this->getThisModel();
if (!$model->getId())
{
$model->setIDsFromRequest();
}
$status = $model->checkout();
if (!$status)
{
// Redirect on error
if ($customURL = $this->input->get('returnurl', '', 'string'))
{
$customURL = base64_decode($customURL);
}
$url = !empty($customURL) ? $customURL : 'index.php?option=' . $this->component . '&view=' . FOFInflector::pluralize($this->view) . $this->getItemidURLSuffix();
$this->setRedirect($url, $model->getError(), 'error');
return false;
}
// Set the layout to form, if it's not set in the URL
if (is_null($this->layout))
{
$this->layout = 'form';
}
// Do I have a form?
$model->setState('form_name', 'form.' . $this->layout);
$item = $model->getItem();
if (!($item instanceof FOFTable))
{
return false;
}
$itemKey = $item->getKeyName();
if ($item->$itemKey != $model->getId())
{
return false;
}
$formData = is_object($item) ? $item->getData() : array();
$form = $model->getForm($formData);
if ($form !== false)
{
$this->hasForm = true;
}
// Display
$this->display(in_array('edit', $this->cacheableTasks));
return true;
}
/**
* Save the incoming data and then return to the Edit task
*
* @return bool
*/
public function apply()
{
// CSRF prevention
if ($this->csrfProtection)
{
$this->_csrfProtection();
}
$model = $this->getThisModel();
$result = $this->applySave();
// Redirect to the edit task
if ($result)
{
$id = $this->input->get('id', 0, 'int');
$textkey = strtoupper($this->component) . '_LBL_' . strtoupper($this->view) . '_SAVED';
if ($customURL = $this->input->get('returnurl', '', 'string'))
{
$customURL = base64_decode($customURL);
}
$url = !empty($customURL) ? $customURL : 'index.php?option=' . $this->component . '&view=' . $this->view . '&task=edit&id=' . $id . $this->getItemidURLSuffix();
$this->setRedirect($url, JText::_($textkey));
}
return $result;
}
/**
* Duplicates selected items
*
* @return bool
*/
public function copy()
{
// CSRF prevention
if ($this->csrfProtection)
{
$this->_csrfProtection();
}
$model = $this->getThisModel();
if (!$model->getId())
{
$model->setIDsFromRequest();
}
$status = $model->copy();
// Redirect
if ($customURL = $this->input->get('returnurl', '', 'string'))
{
$customURL = base64_decode($customURL);
}
$url = !empty($customURL) ? $customURL : 'index.php?option=' . $this->component . '&view=' . FOFInflector::pluralize($this->view) . $this->getItemidURLSuffix();
if (!$status)
{
$this->setRedirect($url, $model->getError(), 'error');
return false;
}
else
{
if(!FOFPlatform::getInstance()->isCli())
{
FOFPlatform::getInstance()->setHeader('Status', '201 Created', true);
}
$this->setRedirect($url);
return true;
}
}
/**
* Save the incoming data and then return to the Browse task
*
* @return bool
*/
public function save()
{
// CSRF prevention
if ($this->csrfProtection)
{
$this->_csrfProtection();
}
$result = $this->applySave();
// Redirect to the display task
if ($result)
{
$textkey = strtoupper($this->component) . '_LBL_' . strtoupper($this->view) . '_SAVED';
if ($customURL = $this->input->get('returnurl', '', 'string'))
{
$customURL = base64_decode($customURL);
}
$url = !empty($customURL) ? $customURL : 'index.php?option=' . $this->component . '&view=' . FOFInflector::pluralize($this->view) . $this->getItemidURLSuffix();
$this->setRedirect($url, JText::_($textkey));
}
return $result;
}
/**
* Save the incoming data and then return to the Add task
*
* @return bool
*/
public function savenew()
{
// CSRF prevention
if ($this->csrfProtection)
{
$this->_csrfProtection();
}
$result = $this->applySave();
// Redirect to the display task
if ($result)
{
$textkey = strtoupper($this->component) . '_LBL_' . strtoupper($this->view) . '_SAVED';
if ($customURL = $this->input->get('returnurl', '', 'string'))
{
$customURL = base64_decode($customURL);
}
$url = !empty($customURL) ? $customURL : 'index.php?option=' . $this->component . '&view=' . $this->view . '&task=add' . $this->getItemidURLSuffix();
$this->setRedirect($url, JText::_($textkey));
}
return $result;
}
/**
* Cancel the edit, check in the record and return to the Browse task
*
* @return bool
*/
public function cancel()
{
$model = $this->getThisModel();
if (!$model->getId())
{
$model->setIDsFromRequest();
}
$model->checkin();
// Remove any saved data
JFactory::getSession()->set($model->getHash() . 'savedata', null);
// Redirect to the display task
if ($customURL = $this->input->get('returnurl', '', 'string'))
{
$customURL = base64_decode($customURL);
}
$url = !empty($customURL) ? $customURL : 'index.php?option=' . $this->component . '&view=' . FOFInflector::pluralize($this->view) . $this->getItemidURLSuffix();
$this->setRedirect($url);
return true;
}
/**
* Method to load a row from version history
*
* @return boolean True if the content history is reverted, false otherwise
*
* @since 2.2
*/
public function loadhistory()
{
$app = JFactory::getApplication();
$lang = JFactory::getLanguage();
$model = $this->getThisModel();
$table = $model->getTable();
$historyId = $app->input->get('version_id', null, 'integer');
$status = $model->checkout();
$alias = $this->component . '.' . $this->view;
if (!$model->loadhistory($historyId, $table, $alias))
{
$this->setMessage($model->getError(), 'error');
$url = !empty($customURL) ? $customURL : 'index.php?option=' . $this->component . '&view=' . FOFInflector::pluralize($this->view) . $this->getItemidURLSuffix();
$this->setRedirect($url);
return false;
}
// Determine the name of the primary key for the data.
if (empty($key))
{
$key = $table->getKeyName();
}
$recordId = $table->$key;
// To avoid data collisions the urlVar may be different from the primary key.
$urlVar = empty($this->urlVar) ? $key : $this->urlVar;
// Access check.
$privilege = $this->configProvider->get(
$this->component . '.views.' .
FOFInflector::singularize($this->view) . '.acl.edit', 'core.edit'
);
if (!$this->checkACL($privilege))
{
$this->setError(JText::_('JLIB_APPLICATION_ERROR_EDIT_NOT_PERMITTED'));
$this->setMessage($this->getError(), 'error');
$url = !empty($customURL) ? $customURL : 'index.php?option=' . $this->component . '&view=' . FOFInflector::pluralize($this->view) . $this->getItemidURLSuffix();
$this->setRedirect($url);
$table->checkin();
return false;
}
$table->store();
$url = !empty($customURL) ? $customURL : 'index.php?option=' . $this->component . '&view=' . FOFInflector::pluralize($this->view) . $this->getItemidURLSuffix();
$this->setRedirect($url);
$this->setMessage(JText::sprintf('JLIB_APPLICATION_SUCCESS_LOAD_HISTORY', $model->getState('save_date'), $model->getState('version_note')));
return true;
}
/**
* Sets the access to public. Joomla! 1.5 compatibility.
*
* @return bool
*
* @deprecated since 2.0
*/
public function accesspublic()
{
// CSRF prevention
if ($this->csrfProtection)
{
$this->_csrfProtection();
}
return $this->setaccess(0);
}
/**
* Sets the access to registered. Joomla! 1.5 compatibility.
*
* @return bool
*
* @deprecated since 2.0
*/
public function accessregistered()
{
// CSRF prevention
if ($this->csrfProtection)
{
$this->_csrfProtection();
}
return $this->setaccess(1);
}
/**
* Sets the access to special. Joomla! 1.5 compatibility.
*
* @return bool
*
* @deprecated since 2.0
*/
public function accessspecial()
{
// CSRF prevention
if ($this->csrfProtection)
{
$this->_csrfProtection();
}
return $this->setaccess(2);
}
/**
* Publish (set enabled = 1) an item.
*
* @return bool
*/
public function publish()
{
// CSRF prevention
if ($this->csrfProtection)
{
$this->_csrfProtection();
}
return $this->setstate(1);
}
/**
* Unpublish (set enabled = 0) an item.
*
* @return bool
*/
public function unpublish()
{
// CSRF prevention
if ($this->csrfProtection)
{
$this->_csrfProtection();
}
return $this->setstate(0);
}
/**
* Archive (set enabled = 2) an item.
*
* @return bool
*/
public function archive()
{
// CSRF prevention
if ($this->csrfProtection)
{
$this->_csrfProtection();
}
return $this->setstate(2);
}
/**
* Trash (set enabled = -2) an item.
*
* @return bool
*/
public function trash()
{
// CSRF prevention
if ($this->csrfProtection)
{
$this->_csrfProtection();
}
return $this->setstate(-2);
}
/**
* Saves the order of the items
*
* @return bool
*/
public function saveorder()
{
// CSRF prevention
if ($this->csrfProtection)
{
$this->_csrfProtection();
}
$model = $this->getThisModel();
if (!$model->getId())
{
$model->setIDsFromRequest();
}
$ordering = $model->getTable()->getColumnAlias('ordering');
$ids = $model->getIds();
$orders = $this->input->get('order', array(), 'array');
if ($n = count($ids))
{
for ($i = 0; $i < $n; $i++)
{
$model->setId($ids[$i]);
$neworder = (int) $orders[$i];
$item = $model->getItem();
if (!($item instanceof FOFTable))
{
return false;
}
$key = $item->getKeyName();
if ($item->$key == $ids[$i])
{
$item->$ordering = $neworder;
$model->save($item);
}
}
}
$status = $model->reorder();
// Redirect
if ($customURL = $this->input->get('returnurl', '', 'string'))
{
$customURL = base64_decode($customURL);
}
$url = !empty($customURL) ? $customURL : 'index.php?option=' . $this->component . '&view=' . FOFInflector::pluralize($this->view) . $this->getItemidURLSuffix();
$this->setRedirect($url);
return $status;
}
/**
* Moves selected items one position down the ordering list
*
* @return bool
*/
public function orderdown()
{
// CSRF prevention
if ($this->csrfProtection)
{
$this->_csrfProtection();
}
$model = $this->getThisModel();
if (!$model->getId())
{
$model->setIDsFromRequest();
}
$status = $model->move(1);
// Redirect
if ($customURL = $this->input->get('returnurl', '', 'string'))
{
$customURL = base64_decode($customURL);
}
$url = !empty($customURL) ? $customURL : 'index.php?option=' . $this->component . '&view=' . FOFInflector::pluralize($this->view) . $this->getItemidURLSuffix();
if (!$status)
{
$this->setRedirect($url, $model->getError(), 'error');
}
else
{
$this->setRedirect($url);
}
return $status;
}
/**
* Moves selected items one position up the ordering list
*
* @return bool
*/
public function orderup()
{
// CSRF prevention
if ($this->csrfProtection)
{
$this->_csrfProtection();
}
$model = $this->getThisModel();
if (!$model->getId())
{
$model->setIDsFromRequest();
}
$status = $model->move(-1);
// Redirect
if ($customURL = $this->input->get('returnurl', '', 'string'))
{
$customURL = base64_decode($customURL);
}
$url = !empty($customURL) ? $customURL : 'index.php?option=' . $this->component . '&view=' . FOFInflector::pluralize($this->view) . $this->getItemidURLSuffix();
if (!$status)
{
$this->setRedirect($url, $model->getError(), 'error');
}
else
{
$this->setRedirect($url);
}
return $status;
}
/**
* Delete selected item(s)
*
* @return bool
*/
public function remove()
{
// CSRF prevention
if ($this->csrfProtection)
{
$this->_csrfProtection();
}
$model = $this->getThisModel();
if (!$model->getId())
{
$model->setIDsFromRequest();
}
$status = $model->delete();
// Redirect
if ($customURL = $this->input->get('returnurl', '', 'string'))
{
$customURL = base64_decode($customURL);
}
$url = !empty($customURL) ? $customURL : 'index.php?option=' . $this->component . '&view=' . FOFInflector::pluralize($this->view) . $this->getItemidURLSuffix();
if (!$status)
{
$this->setRedirect($url, $model->getError(), 'error');
}
else
{
$this->setRedirect($url);
}
return $status;
}
/**
* Redirects the browser or returns false if no redirect is set.
*
* @return boolean False if no redirect exists.
*/
public function redirect()
{
if ($this->redirect)
{
$app = JFactory::getApplication();
$app->enqueueMessage($this->message, $this->messageType);
$app->redirect($this->redirect);
return true;
}
return false;
}
/**
* Returns true if there is a redirect set in the controller
*
* @return boolean
*/
public function hasRedirect()
{
return !empty($this->redirect);
}
/**
* Register the default task to perform if a mapping is not found.
*
* @param string $method The name of the method in the derived class to perform if a named task is not found.
*
* @return FOFController A FOFController object to support chaining.
*/
public function registerDefaultTask($method)
{
$this->registerTask('__default', $method);
return $this;
}
/**
* Register (map) a task to a method in the class.
*
* @param string $task The task.
* @param string $method The name of the method in the derived class to perform for this task.
*
* @return FOFController A FOFController object to support chaining.
*/
public function registerTask($task, $method)
{
if (in_array(strtolower($method), $this->methods))
{
$this->taskMap[strtolower($task)] = $method;
}
return $this;
}
/**
* Unregister (unmap) a task in the class.
*
* @param string $task The task.
*
* @return FOFController This object to support chaining.
*/
public function unregisterTask($task)
{
unset($this->taskMap[strtolower($task)]);
return $this;
}
/**
* Sets the internal message that is passed with a redirect
*
* @param string $text Message to display on redirect.
* @param string $type Message type. Optional, defaults to 'message'.
*
* @return string Previous message
*/
public function setMessage($text, $type = 'message')
{
$previous = $this->message;
$this->message = $text;
$this->messageType = $type;
return $previous;
}
/**
* Sets an entire array of search paths for resources.
*
* @param string $type The type of path to set, typically 'view' or 'model'.
* @param string $path The new set of search paths. If null or false, resets to the current directory only.
*
* @return void
*/
protected function setPath($type, $path)
{
// Clear out the prior search dirs
$this->paths[$type] = array();
// Actually add the user-specified directories
$this->addPath($type, $path);
}
/**
* Registers a redirection with an optional message. The redirection is
* carried out when you use the redirect method.
*
* @param string $url The URL to redirect to
* @param string $msg The message to be pushed to the application
* @param string $type The message type to be pushed to the application, e.g. 'error'
*
* @return FOFController This object to support chaining
*/
public function setRedirect($url, $msg = null, $type = null)
{
// Do the logic only if we're parsing a raw url (index.php?foo=bar&etc=etc)
if (strpos($url, 'index.php') === 0)
{
$isAdmin = FOFPlatform::getInstance()->isBackend();
$auto = false;
if (($this->autoRouting == 2 || $this->autoRouting == 3) && $isAdmin)
{
$auto = true;
}
elseif (($this->autoRouting == 1 || $this->autoRouting == 3) && !$isAdmin)
{
$auto = true;
}
if ($auto)
{
$url = JRoute::_($url, false);
}
}
$this->redirect = $url;
if ($msg !== null)
{
// Controller may have set this directly
$this->message = $msg;
}
// Ensure the type is not overwritten by a previous call to setMessage.
if (empty($type))
{
if (empty($this->messageType))
{
$this->messageType = 'message';
}
}
// If the type is explicitly set, set it.
else
{
$this->messageType = $type;
}
return $this;
}
/**
* Sets the published state (the enabled field) of the selected item(s)
*
* @param integer $state The desired state. 0 is unpublished, 1 is published.
*
* @return bool
*/
protected function setstate($state = 0)
{
$model = $this->getThisModel();
if (!$model->getId())
{
$model->setIDsFromRequest();
}
$status = $model->publish($state);
// Redirect
if ($customURL = $this->input->get('returnurl', '', 'string'))
{
$customURL = base64_decode($customURL);
}
$url = !empty($customURL) ? $customURL : 'index.php?option=' . $this->component . '&view=' . FOFInflector::pluralize($this->view) . $this->getItemidURLSuffix();
if (!$status)
{
$this->setRedirect($url, $model->getError(), 'error');
}
else
{
$this->setRedirect($url);
}
return $status;
}
/**
* Sets the access level of the selected item(s).
*
* @param integer $level The desired viewing access level ID
*
* @return bool
*/
protected function setaccess($level = 0)
{
$model = $this->getThisModel();
if (!$model->getId())
{
$model->setIDsFromRequest();
}
$id = $model->getId();
$item = $model->getItem();
if (!($item instanceof FOFTable))
{
return false;
}
$accessField = $item->getColumnAlias('access');
$key = $item->getKeyName();
$loadedid = $item->$key;
if ($id == $loadedid)
{
$item->$accessField = $level;
$status = $model->save($item);
}
else
{
$status = false;
}
// Redirect
if ($customURL = $this->input->get('returnurl', '', 'string'))
{
$customURL = base64_decode($customURL);
}
$url = !empty($customURL) ? $customURL : 'index.php?option=' . $this->component . '&view=' . FOFInflector::pluralize($this->view) . $this->getItemidURLSuffix();
if (!$status)
{
$this->setRedirect($url, $model->getError(), 'error');
}
else
{
$this->setRedirect($url);
}
return $status;
}
/**
* Common method to handle apply and save tasks
*
* @return boolean Returns true on success
*/
final private function applySave()
{
// Load the model
$model = $this->getThisModel();
if (!$model->getId())
{
$model->setIDsFromRequest();
}
$id = $model->getId();
$data = $this->input->getData();
if (!$this->onBeforeApplySave($data))
{
return false;
}
// Set the layout to form, if it's not set in the URL
if (is_null($this->layout))
{
$this->layout = 'form';
}
// Do I have a form?
$model->setState('form_name', 'form.' . $this->layout);
$status = $model->save($data);
if ($status && ($id != 0))
{
FOFPlatform::getInstance()->setHeader('Status', '201 Created', true);
// Try to check-in the record if it's not a new one
$status = $model->checkin();
}
if ($status)
{
$status = $this->onAfterApplySave();
}
$this->input->set('id', $model->getId());
if (!$status)
{
// Redirect on error
$id = $model->getId();
if ($customURL = $this->input->get('returnurl', '', 'string'))
{
$customURL = base64_decode($customURL);
}
if (!empty($customURL))
{
$url = $customURL;
}
elseif ($id != 0)
{
$url = 'index.php?option=' . $this->component . '&view=' . $this->view . '&task=edit&id=' . $id . $this->getItemidURLSuffix();
}
else
{
$url = 'index.php?option=' . $this->component . '&view=' . $this->view . '&task=add' . $this->getItemidURLSuffix();
}
$this->setRedirect($url, '<li>' . implode('</li><li>', $model->getErrors()) . '</li>', 'error');
return false;
}
else
{
$session = JFactory::getSession();
$session->set($model->getHash() . 'savedata', null);
return true;
}
}
/**
* Returns the default model associated with the current view
*
* @param array $config Configuration variables for the model
*
* @return FOFModel The global instance of the model (singleton)
*/
final public function getThisModel($config = array())
{
if (!is_object($this->_modelObject))
{
// Make sure $config is an array
if (is_object($config))
{
$config = (array) $config;
}
elseif (!is_array($config))
{
$config = array();
}
if (!empty($this->modelName))
{
$parts = FOFInflector::explode($this->modelName);
$modelName = ucfirst(array_pop($parts));
$prefix = FOFInflector::implode($parts);
}
else
{
$prefix = ucfirst($this->bareComponent) . 'Model';
$modelName = ucfirst(FOFInflector::pluralize($this->view));
}
if (!array_key_exists('input', $config) || !($config['input'] instanceof FOFInput))
{
$config['input'] = $this->input;
}
$this->_modelObject = $this->getModel($modelName, $prefix, $config);
}
return $this->_modelObject;
}
/**
* Method to get a model object, loading it if required.
*
* @param string $name The model name. Optional.
* @param string $prefix The class prefix. Optional.
* @param array $config Configuration array for model. Optional.
*
* @return object The model.
*/
public function getModel($name = '', $prefix = '', $config = array())
{
// Make sure $config is an array
if (is_object($config))
{
$config = (array) $config;
}
elseif (!is_array($config) || empty($config))
{
// array_merge is required to create a copy instead of assigning by reference
$config = array_merge($this->config);
}
if (empty($name))
{
$name = $this->getName();
}
if (empty($prefix))
{
$prefix = $this->model_prefix;
}
if ($model = $this->createModel($name, $prefix, $config))
{
// Task is a reserved state
$model->setState('task', $this->task);
// Let's get the application object and set menu information if it's available
if (!FOFPlatform::getInstance()->isCli())
{
$app = JFactory::getApplication();
$menu = $app->getMenu();
if (is_object($menu))
{
if ($item = $menu->getActive())
{
$params = $menu->getParams($item->id);
// Set default state data
$model->setState('parameters.menu', $params);
}
}
}
}
return $model;
}
/**
* Returns current view object
*
* @param array $config Configuration variables for the model
*
* @return FOFView The global instance of the view object (singleton)
*/
final public function getThisView($config = array())
{
if (!is_object($this->_viewObject))
{
// Make sure $config is an array
if (is_object($config))
{
$config = (array) $config;
}
elseif (!is_array($config) || empty($config))
{
// array_merge is required to create a copy instead of assigning by reference
$config = array_merge($this->config);
}
$prefix = null;
$viewName = null;
$viewType = null;
if (!empty($this->viewName))
{
$parts = FOFInflector::explode($this->viewName);
$viewName = ucfirst(array_pop($parts));
$prefix = FOFInflector::implode($parts);
}
else
{
$prefix = ucfirst($this->bareComponent) . 'View';
$viewName = ucfirst($this->view);
}
$document = FOFPlatform::getInstance()->getDocument();
if ($document instanceof JDocument)
{
$viewType = $document->getType();
}
else
{
$viewType = $this->input->getCmd('format', 'html');
}
if (($viewType == 'html') && $this->hasForm)
{
$viewType = 'form';
}
if (!array_key_exists('input', $config) || !($config['input'] instanceof FOFInput))
{
$config['input'] = $this->input;
}
$config['input']->set('base_path', $this->basePath);
$this->_viewObject = $this->getView($viewName, $viewType, $prefix, $config);
}
return $this->_viewObject;
}
/**
* Method to get the controller name
*
* The dispatcher name is set by default parsed using the classname, or it can be set
* by passing a $config['name'] in the class constructor
*
* @throws Exception
*
* @return string The name of the dispatcher
*/
public function getName()
{
if (empty($this->name))
{
if (empty($this->bareComponent))
{
$r = null;
if (!preg_match('/(.*)Controller/i', get_class($this), $r))
{
throw new Exception(JText::_('JLIB_APPLICATION_ERROR_CONTROLLER_GET_NAME'), 500);
}
$this->name = strtolower($r[1]);
}
else
{
$this->name = $this->bareComponent;
}
}
return $this->name;
}
/**
* Get the last task that is being performed or was most recently performed.
*
* @return string The task that is being performed or was most recently performed.
*/
public function getTask()
{
return $this->task;
}
/**
* Gets the available tasks in the controller.
*
* @return array Array[i] of task names.
*/
public function getTasks()
{
return $this->methods;
}
/**
* Method to get a reference to the current view and load it if necessary.
*
* @param string $name The view name. Optional, defaults to the controller name.
* @param string $type The view type. Optional.
* @param string $prefix The class prefix. Optional.
* @param array $config Configuration array for view. Optional.
*
* @throws Exception
*
* @return FOFView Reference to the view or an error.
*/
public function getView($name = '', $type = '', $prefix = '', $config = array())
{
// Make sure $config is an array
if (is_object($config))
{
$config = (array) $config;
}
elseif (!is_array($config))
{
$config = array();
}
if (empty($name))
{
$name = $this->getName();
}
if (empty($prefix))
{
$prefix = $this->getName() . 'View';
}
$signature = md5($name . $type . $prefix . serialize($config));
if (empty($this->viewsCache[$signature]))
{
if ($view = $this->createView($name, $prefix, $type, $config))
{
$this->viewsCache[$signature] = & $view;
}
else
{
throw new Exception(JText::sprintf('JLIB_APPLICATION_ERROR_VIEW_NOT_FOUND', $name, $type, $prefix), 500);
}
}
return $this->viewsCache[$signature];
}
/**
* Creates a new model object
*
* @param string $name The name of the model class, e.g. Items
* @param string $prefix The prefix of the model class, e.g. FoobarModel
* @param array $config The configuration parameters for the model class
*
* @return FOFModel The model object
*/
protected function createModel($name, $prefix = '', $config = array())
{
// Make sure $config is an array
if (is_object($config))
{
$config = (array) $config;
}
elseif (!is_array($config))
{
$config = array();
}
$result = null;
// Clean the model name
$modelName = preg_replace('/[^A-Z0-9_]/i', '', $name);
$classPrefix = preg_replace('/[^A-Z0-9_]/i', '', $prefix);
$result = FOFModel::getAnInstance($modelName, $classPrefix, $config);
return $result;
}
/**
* Method to load and return a model object.
*
* @param string $name The name of the model.
* @param string $prefix Optional model prefix.
* @param array $config Configuration array for the model. Optional.
*
* @return mixed Model object on success; otherwise null
*/
protected function &_createModel($name, $prefix = '', $config = array())
{
FOFPlatform::getInstance()->logDeprecated(__CLASS__ . '::' .__METHOD__ . ' is deprecated. Use createModel() instead.');
return $this->createModel($name, $prefix, $config);
}
/**
* Creates a View object instance and returns it
*
* @param string $name The name of the view, e.g. Items
* @param string $prefix The prefix of the view, e.g. FoobarView
* @param string $type The type of the view, usually one of Html, Raw, Json or Csv
* @param array $config The configuration variables to use for creating the view
*
* @return FOFView
*/
protected function createView($name, $prefix = '', $type = '', $config = array())
{
// Make sure $config is an array
if (is_object($config))
{
$config = (array) $config;
}
elseif (!is_array($config))
{
$config = array();
}
$result = null;
// Clean the view name
$viewName = preg_replace('/[^A-Z0-9_]/i', '', $name);
$classPrefix = preg_replace('/[^A-Z0-9_]/i', '', $prefix);
$viewType = preg_replace('/[^A-Z0-9_]/i', '', $type);
if (!isset($config['input']))
{
$config['input'] = $this->input;
}
if (($config['input'] instanceof FOFInput))
{
$tmpInput = $config['input'];
}
else
{
$tmpInput = new FOFInput($config['input']);
}
// Guess the component name and view
if (!empty($prefix))
{
preg_match('/(.*)View$/', $prefix, $m);
$component = 'com_' . strtolower($m[1]);
}
else
{
$component = '';
}
if (empty($component) && array_key_exists('input', $config))
{
$component = $tmpInput->get('option', $component, 'cmd');
}
if (array_key_exists('option', $config))
{
if ($config['option'])
{
$component = $config['option'];
}
}
$config['option'] = $component;
$view = strtolower($viewName);
if (empty($view) && array_key_exists('input', $config))
{
$view = $tmpInput->get('view', $view, 'cmd');
}
if (array_key_exists('view', $config))
{
if ($config['view'])
{
$view = $config['view'];
}
}
$config['view'] = $view;
if (array_key_exists('input', $config))
{
$tmpInput->set('option', $config['option']);
$tmpInput->set('view', $config['view']);
$config['input'] = $tmpInput;
}
// Get the component directories
$componentPaths = FOFPlatform::getInstance()->getComponentBaseDirs($config['option']);
// Get the base paths where the view class files are expected to live
$basePaths = array(
$componentPaths['main'],
$componentPaths['alt']
);
$basePaths = array_merge($this->paths['view']);
// Get the alternate (singular/plural) view name
$altViewName = FOFInflector::isPlural($viewName) ? FOFInflector::singularize($viewName) : FOFInflector::pluralize($viewName);
$suffixes = array(
$viewName,
$altViewName,
'default'
);
$filesystem = FOFPlatform::getInstance()->getIntegrationObject('filesystem');
foreach ($suffixes as $suffix)
{
// Build the view class name
$viewClass = $classPrefix . ucfirst($suffix);
if (class_exists($viewClass))
{
// The class is already loaded
break;
}
// The class is not loaded. Let's load it!
$viewPath = $this->createFileName('view', array('name' => $suffix, 'type' => $viewType));
$path = $filesystem->pathFind($basePaths, $viewPath);
if ($path)
{
require_once $path;
}
if (class_exists($viewClass))
{
// The class was loaded successfully
break;
}
}
if (!class_exists($viewClass))
{
$viewClass = 'FOFView' . ucfirst($type);
}
$templateOverridePath = FOFPlatform::getInstance()->getTemplateOverridePath($config['option']);
// Setup View configuration options
if (!array_key_exists('template_path', $config))
{
$config['template_path'][] = $componentPaths['main'] . '/views/' . FOFInflector::pluralize($config['view']) . '/tmpl';
if ($templateOverridePath)
{
$config['template_path'][] = $templateOverridePath . '/' . FOFInflector::pluralize($config['view']);
}
$config['template_path'][] = $componentPaths['main'] . '/views/' . FOFInflector::singularize($config['view']) . '/tmpl';
if ($templateOverridePath)
{
$config['template_path'][] = $templateOverridePath . '/' . FOFInflector::singularize($config['view']);
}
$config['template_path'][] = $componentPaths['main'] . '/views/' . $config['view'] . '/tmpl';
if ($templateOverridePath)
{
$config['template_path'][] = $templateOverridePath . '/' . $config['view'];
}
}
$extraTemplatePath = $this->configProvider->get($config['option'] . '.views.' . $config['view'] . '.config.template_path', null);
if ($extraTemplatePath)
{
array_unshift($config['template_path'], $componentPaths['main'] . '/' . $extraTemplatePath);
}
if (!array_key_exists('helper_path', $config))
{
$config['helper_path'] = array(
$componentPaths['main'] . '/helpers',
$componentPaths['admin'] . '/helpers'
);
}
$extraHelperPath = $this->configProvider->get($config['option'] . '.views.' . $config['view'] . '.config.helper_path', null);
if ($extraHelperPath)
{
$config['helper_path'][] = $componentPaths['main'] . '/' . $extraHelperPath;
}
// Set up the page title
$setFrontendPageTitle = $this->configProvider->get($config['option'] . '.views.' . $config['view'] . '.config.setFrontendPageTitle', null);
if ($setFrontendPageTitle)
{
$setFrontendPageTitle = strtolower($setFrontendPageTitle);
$config['setFrontendPageTitle'][] = in_array($setFrontendPageTitle, array('1', 'yes', 'true', 'on'));
}
$defaultPageTitle = $this->configProvider->get($config['option'] . '.views.' . $config['view'] . '.config.defaultPageTitle', null);
if ($defaultPageTitle)
{
$config['defaultPageTitle'][] = in_array($defaultPageTitle, array('1', 'yes', 'true', 'on'));
}
// Set the use_hypermedia flag in $config if it's not already set
if (!isset($config['use_hypermedia']))
{
$config['use_hypermedia'] = $this->configProvider->get($config['option'] . '.views.' . $config['view'] . '.config.use_hypermedia', false);
}
// Set also the linkbar_style
if (!isset($config['linkbar_style']))
{
$style = $this->configProvider->get($config['option'] . '.views.' . $config['view'] . '.config.linkbar_style', false);
if ($style) {
$config['linkbar_style'] = $style;
}
}
/**
* Some administrative templates force format=utf (yeah, I know, what the heck, right?) when a format
* URL parameter does not exist in the URL. Of course there is no such thing as FOFViewUtf (why the heck would
* it be, there is no such thing as a format=utf in Joomla! for crying out loud) which causes a Fatal Error. So
* we have to detect that and force $type='html'...
*/
if (!class_exists($viewClass) && ($type != 'html'))
{
$type = 'html';
$result = $this->createView($name, $prefix, $type, $config);
}
else
{
$result = new $viewClass($config);
}
return $result;
}
/**
* Deprecated function to create a View object instance
*
* @param string $name The name of the view, e.g. 'Items'
* @param string $prefix The prefix of the view, e.g. 'FoobarView'
* @param string $type The view type, e.g. 'html'
* @param array $config The configuration array for the view
*
* @return FOFView
*
* @see FOFController::createView
*
* @deprecated since version 2.0
*/
protected function &_createView($name, $prefix = '', $type = '', $config = array())
{
FOFPlatform::getInstance()->logDeprecated(__CLASS__ . '::' . __METHOD__ . ' is deprecated. Use createView() instead.');
return $this->createView($name, $prefix, $type, $config);
}
/**
* Set the name of the view to be used by this Controller
*
* @param string $viewName The name of the view
*
* @return void
*/
public function setThisViewName($viewName)
{
$this->viewName = $viewName;
}
/**
* Set the name of the model to be used by this Controller
*
* @param string $modelName The name of the model
*
* @return void
*/
public function setThisModelName($modelName)
{
$this->modelName = $modelName;
}
/**
* Checks if the current user has enough privileges for the requested ACL
* area.
*
* @param string $area The ACL area, e.g. core.manage.
*
* @return boolean True if the user has the ACL privilege specified
*/
protected function checkACL($area)
{
if (in_array(strtolower($area), array('false','0','no','403')))
{
return false;
}
if (in_array(strtolower($area), array('true','1','yes')))
{
return true;
}
elseif (empty($area))
{
return true;
}
else
{
// Check if we're dealing with ids
$ids = null;
// First, check if there is an asset for this record
$table = $this->getThisModel()->getTable();
if ($table && $table->isAssetsTracked())
{
$ids = $this->getThisModel()->getId() ? $this->getThisModel()->getId() : null;
}
// Generic or Asset tracking
if (empty($ids))
{
return FOFPlatform::getInstance()->authorise($area, $this->component);
}
else
{
if (!is_array($ids))
{
$ids = array($ids);
}
$resource = FOFInflector::singularize($this->view);
$isEditState = ($area == 'core.edit.state');
foreach ($ids as $id)
{
$asset = $this->component . '.' . $resource . '.' . $id;
// Dedicated permission found, check it!
if (FOFPlatform::getInstance()->authorise($area, $asset) )
{
return true;
}
// Fallback on edit.own, if not edit.state. First test if the permission is available.
if ((!$isEditState) && (FOFPlatform::getInstance()->authorise('core.edit.own', $asset)))
{
$table = $this->getThisModel()->getTable();
$table->load($id);
$created_by = $table->getColumnAlias('created_by');
if ($table && isset($table->$created_by))
{
// Now test the owner is the user.
$owner_id = (int) $table->$created_by;
// If the owner matches 'me' then do the test.
if ($owner_id == FOFPlatform::getInstance()->getUser()->id)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
}
}
}
return false;
}
/**
* A catch-all method for all tasks without a corresponding onBefore
* method. Applies the ACL preferences defined in fof.xml.
*
* @param string $task The task being executed
*
* @return boolean True to allow execution of the task
*/
protected function onBeforeGenericTask($task)
{
$privilege = $this->configProvider->get(
$this->component . '.views.' .
FOFInflector::singularize($this->view) . '.acl.' . $task, ''
);
return $this->checkACL($privilege);
}
/**
* Execute something before applySave is called. Return false to prevent
* applySave from executing.
*
* @param array &$data The data upon which applySave will act
*
* @return boolean True to allow applySave to run
*/
protected function onBeforeApplySave(&$data)
{
return true;
}
/**
* Execute something after applySave has run.
*
* @return boolean True to allow normal return, false to cause a 403 error
*/
protected function onAfterApplySave()
{
return true;
}
/**
* ACL check before changing the access level; override to customise
*
* @return boolean True to allow accesspublic() to run
*/
protected function onBeforeAccesspublic()
{
$privilege = $this->configProvider->get(
$this->component . '.views.' .
FOFInflector::singularize($this->view) . '.acl.accesspublic', 'core.edit.state');
return $this->checkACL($privilege);
}
/**
* ACL check before changing the access level; override to customise
*
* @return boolean True to allow the method to run
*/
protected function onBeforeAccessregistered()
{
$privilege = $this->configProvider->get(
$this->component . '.views.' .
FOFInflector::singularize($this->view) . '.acl.accessregistered', 'core.edit.state'
);
return $this->checkACL($privilege);
}
/**
* ACL check before changing the access level; override to customise
*
* @return boolean True to allow the method to run
*/
protected function onBeforeAccessspecial()
{
$privilege = $this->configProvider->get(
$this->component . '.views.' .
FOFInflector::singularize($this->view) . '.acl.accessspecial', 'core.edit.state'
);
return $this->checkACL($privilege);
}
/**
* ACL check before adding a new record; override to customise
*
* @return boolean True to allow the method to run
*/
protected function onBeforeAdd()
{
$privilege = $this->configProvider->get(
$this->component . '.views.' .
FOFInflector::singularize($this->view) . '.acl.add', 'core.create'
);
return $this->checkACL($privilege);
}
/**
* ACL check before saving a new/modified record; override to customise
*
* @return boolean True to allow the method to run
*/
protected function onBeforeApply()
{
$model = $this->getThisModel();
if (!$model->getId())
{
$model->setIDsFromRequest();
}
$id = $model->getId();
if(!$id)
{
$defaultPrivilege = 'core.create';
}
else
{
$defaultPrivilege = 'core.edit';
}
$privilege = $this->configProvider->get(
$this->component . '.views.' .
FOFInflector::singularize($this->view) . '.acl.apply', $defaultPrivilege
);
return $this->checkACL($privilege);
}
/**
* ACL check before allowing someone to browse
*
* @return boolean True to allow the method to run
*/
protected function onBeforeBrowse()
{
$defaultPrivilege = '';
$privilege = $this->configProvider->get(
$this->component . '.views.' .
FOFInflector::singularize($this->view) . '.acl.browse', $defaultPrivilege
);
return $this->checkACL($privilege);
}
/**
* ACL check before cancelling an edit
*
* @return boolean True to allow the method to run
*/
protected function onBeforeCancel()
{
$model = $this->getThisModel();
if (!$model->getId())
{
$model->setIDsFromRequest();
}
$id = $model->getId();
if(!$id)
{
$defaultPrivilege = 'core.create';
}
else
{
$defaultPrivilege = 'core.edit';
}
$privilege = $this->configProvider->get(
$this->component . '.views.' .
FOFInflector::singularize($this->view) . '.acl.cancel', $defaultPrivilege
);
return $this->checkACL($privilege);
}
/**
* ACL check before editing a record; override to customise
*
* @return boolean True to allow the method to run
*/
protected function onBeforeEdit()
{
$privilege = $this->configProvider->get(
$this->component . '.views.' .
FOFInflector::singularize($this->view) . '.acl.edit', 'core.edit'
);
return $this->checkACL($privilege);
}
/**
* ACL check before changing the ordering of a record; override to customise
*
* @return boolean True to allow the method to run
*/
protected function onBeforeOrderdown()
{
$privilege = $this->configProvider->get(
$this->component . '.views.' .
FOFInflector::singularize($this->view) . '.acl.orderdown', 'core.edit.state'
);
return $this->checkACL($privilege);
}
/**
* ACL check before changing the ordering of a record; override to customise
*
* @return boolean True to allow the method to run
*/
protected function onBeforeOrderup()
{
$privilege = $this->configProvider->get(
$this->component . '.views.' .
FOFInflector::singularize($this->view) . '.acl.orderup', 'core.edit.state'
);
return $this->checkACL($privilege);
}
/**
* ACL check before changing the publish status of a record; override to customise
*
* @return boolean True to allow the method to run
*/
protected function onBeforePublish()
{
$privilege = $this->configProvider->get(
$this->component . '.views.' .
FOFInflector::singularize($this->view) . '.acl.publish', 'core.edit.state'
);
return $this->checkACL($privilege);
}
/**
* ACL check before removing a record; override to customise
*
* @return boolean True to allow the method to run
*/
protected function onBeforeRemove()
{
$privilege = $this->configProvider->get(
$this->component . '.views.' .
FOFInflector::singularize($this->view) . '.acl.remove', 'core.delete'
);
return $this->checkACL($privilege);
}
/**
* ACL check before saving a new/modified record; override to customise
*
* @return boolean True to allow the method to run
*/
protected function onBeforeSave()
{
$model = $this->getThisModel();
if (!$model->getId())
{
$model->setIDsFromRequest();
}
$id = $model->getId();
if(!$id)
{
$defaultPrivilege = 'core.create';
}
else
{
$defaultPrivilege = 'core.edit';
}
$privilege = $this->configProvider->get(
$this->component . '.views.' .
FOFInflector::singularize($this->view) . '.acl.save', $defaultPrivilege
);
return $this->checkACL($privilege);
}
/**
* ACL check before saving a new/modified record; override to customise
*
* @return boolean True to allow the method to run
*/
protected function onBeforeSavenew()
{
$privilege = $this->configProvider->get(
$this->component . '.views.' .
FOFInflector::singularize($this->view) . '.acl.savenew', 'core.create'
);
return $this->checkACL($privilege);
}
/**
* ACL check before changing the ordering of a record; override to customise
*
* @return boolean True to allow the method to run
*/
protected function onBeforeSaveorder()
{
$privilege = $this->configProvider->get(
$this->component . '.views.' .
FOFInflector::singularize($this->view) . '.acl.saveorder', 'core.edit.state'
);
return $this->checkACL($privilege);
}
/**
* ACL check before changing the publish status of a record; override to customise
*
* @return boolean True to allow the method to run
*/
protected function onBeforeUnpublish()
{
$privilege = $this->configProvider->get(
$this->component . '.views.' .
FOFInflector::singularize($this->view) . '.acl.unpublish', 'core.edit.state'
);
return $this->checkACL($privilege);
}
/**
* Gets a URL suffix with the Itemid parameter. If it's not the front-end of the site, or if
* there is no Itemid set it returns an empty string.
*
* @return string The &Itemid=123 URL suffix, or an empty string if Itemid is not applicable
*/
public function getItemidURLSuffix()
{
if (FOFPlatform::getInstance()->isFrontend() && ($this->input->getCmd('Itemid', 0) != 0))
{
return '&Itemid=' . $this->input->getInt('Itemid', 0);
}
else
{
return '';
}
}
/**
* Applies CSRF protection by means of a standard Joomla! token (nonce) check.
* Raises a 403 Access Forbidden error through the platform if the check fails.
*
* TODO Move this check inside the platform
*
* @return boolean True if the CSRF check is successful
*
* @throws Exception
*/
protected function _csrfProtection()
{
static $isCli = null, $isAdmin = null;
if (is_null($isCli))
{
$isCli = FOFPlatform::getInstance()->isCli();
$isAdmin = FOFPlatform::getInstance()->isBackend();
}
switch ($this->csrfProtection)
{
// Never
case 0:
return true;
break;
// Always
case 1:
break;
// Only back-end and HTML format
case 2:
if ($isCli)
{
return true;
}
elseif (!$isAdmin && ($this->input->get('format', 'html', 'cmd') != 'html'))
{
return true;
}
break;
// Only back-end
case 3:
if (!$isAdmin)
{
return true;
}
break;
}
$hasToken = false;
$session = JFactory::getSession();
// Joomla! 1.5/1.6/1.7/2.5 (classic Joomla! API) method
if (method_exists('JUtility', 'getToken'))
{
$token = JUtility::getToken();
$hasToken = $this->input->get($token, false, 'none') == 1;
if (!$hasToken)
{
$hasToken = $this->input->get('_token', null, 'none') == $token;
}
}
// Joomla! 2.5+ (Platform 12.1+) method
if (!$hasToken)
{
if (method_exists($session, 'getToken'))
{
$token = $session->getToken();
$hasToken = $this->input->get($token, false, 'none') == 1;
if (!$hasToken)
{
$hasToken = $this->input->get('_token', null, 'none') == $token;
}
}
}
// Joomla! 2.5+ formToken method
if (!$hasToken)
{
if (method_exists($session, 'getFormToken'))
{
$token = $session->getFormToken();
$hasToken = $this->input->get($token, false, 'none') == 1;
if (!$hasToken)
{
$hasToken = $this->input->get('_token', null, 'none') == $token;
}
}
}
if (!$hasToken)
{
FOFPlatform::getInstance()->raiseError(403, JText::_('JLIB_APPLICATION_ERROR_ACCESS_FORBIDDEN'));
return false;
}
}
}