Files
@ f43d1a4680a9
Branch filter:
Location: hot67beta/libraries/pattemplate/patTemplate.php
f43d1a4680a9
66.7 KiB
text/x-php
menubar 0 to 10
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 | <?PHP
/**
* patTemplate
*
* $Id: patTemplate.php 10381 2008-06-01 03:35:53Z pasamio $
*
* powerful templating engine
*
* @version 3.1.0
* @package patTemplate
* @author Stephan Schmidt <schst@php.net>
* @license LGPL
* @link http://www.php-tools.net
*/
// ** Following line Joomla! specific **
require_once( dirname( __FILE__ ) . '/patErrorManager.php' );
/**
* template already exists
*/
define( 'PATTEMPLATE_ERROR_TEMPLATE_EXISTS', 5010 );
/**
* template does not exist
*/
define ( 'PATTEMPLATE_WARNING_NO_TEMPLATE', 5011 );
/**
* unknown type
*/
define ( 'PATTEMPLATE_WARNING_UNKNOWN_TYPE', 5012 );
/**
* base class for module could not be found
*/
define( 'PATTEMPLATE_ERROR_BASECLASS_NOT_FOUND', 5050 );
/**
* module could not be found
*/
define( 'PATTEMPLATE_ERROR_MODULE_NOT_FOUND', 5051 );
/**
* array expected
*/
define( 'PATTEMPLATE_ERROR_EXPECTED_ARRAY', 5052 );
/**
* No input
*/
define( 'PATTEMPLATE_ERROR_NO_INPUT', 6000 );
/**
* Recursion
*/
define( 'PATTEMPLATE_ERROR_RECURSION', 6010 );
/**
* patTemplate
*
* powerful templating engine
*
* @version 3.1.0
* @package patTemplate
* @author Stephan Schmidt <schst@php.net>
* @license LGPL
* @link http://www.php-tools.net
*/
class patTemplate
{
/**
* standard system vars that identify pat tools
* @var array
*/
var $_systemVars = array(
'appName' => 'patTemplate',
'appVersion' => '3.1.0',
'author' => array(
'Stephan Schmidt <schst@php.net>'
)
);
/**
* default attributes for new templates
* @access private
* @var array
*/
var $_defaultAttributes = array(
'type' => 'standard',
'visibility' => 'visible',
'loop' => 1,
'unusedvars' => 'strip',
'whitespace' => 'keep',
'autoclear' => 'off',
'autoload' => 'on'
);
/**
* options for patTemplate
*
* Currently the following options are implemented:
* - maintainBc defines, whether patTemplate should be backwards compatible.
* This means, that you may use 'default' and 'empty' for subtemplates.
*
* @access private
* @var array
*/
var $_options = array(
'startTag' => '{',
'endTag' => '}',
'root' => array('__default' => '.'),
'namespace' => 'patTemplate',
'maintainBc' => true,
'defaultFunction' => false
);
/**
* start tag
*
* @access private
* @var string
*/
var $_startTag = '{';
/**
* end tag
*
* @access private
* @var string
*/
var $_endTag = '}';
/**
* loaded modules
*
* Modules are:
* - Readers
* - Caches
* - Variable modifiers
* - Filters
*
* @access private
* @var array
*/
var $_modules = array();
/**
* directories, where modules can be stored
* @access private
* @var array
*/
var $_moduleDirs = array();
/**
* stores all template names
* @access private
* @var array
*/
var $_templateList = array();
/**
* stores all template data
* @access private
* @var array
*/
var $_templates = array();
/**
* stores all global variables
* @access private
* @var array
*/
var $_globals = array();
/**
* stores all local variables
* @access private
* @var array
*/
var $_vars = array();
/**
* stores the name of the first template that has been
* found
*
* @access private
* @var string
*/
var $_root;
/**
* output filters that should be used
*
* @access private
* @var array
*/
var $_outputFilters = array();
/**
* input filters that should be used
*
* @access private
* @var array
*/
var $_inputFilters = array();
/**
* template cache, that should be used
*
* @access private
* @var array
*/
var $_tmplCache = null;
/**
* placeholders, that have been discovered
*
* @access private
* @var array
*/
var $_discoveredPlaceholders = array();
/**
* Create a new patTemplate instance.
*
* The constructor accepts the type of the templates as sole parameter.
* You may choose one of:
* - html (default)
* - tex
*
* The type influences the tags you are using in your templates.
*
* @access public
* @param string type (either html or tex)
*/
function patTemplate( $type = 'html' )
{
if( !defined( 'PATTEMPLATE_INCLUDE_PATH' ) ) {
define( 'PATTEMPLATE_INCLUDE_PATH', dirname( __FILE__ ) . '/patTemplate' );
}
$this->setType( $type );
}
/**
* sets an option
*
* Currently, the following options are supported
* - maintainBc (true|false)
* - namespace (string)
*
* @access public
* @param string option to set
* @param string value of the option
*/
function setOption($option, $value)
{
$this->_options[$option] = $value;
}
/**
* gets an option
*
* @access public
* @param string option to get
* @return mixed value of the option
*/
function getOption( $option )
{
if (!isset($this->_options[$option])) {
return null;
}
return $this->_options[$option];
}
/**
* sets name of directory where templates are stored
*
* @access public
* @param string dir where templates are stored
* @deprecated please use patTemplate::setRoot() instead
*/
function setBasedir($basedir)
{
$this->setRoot($basedir);
}
/**
* sets root base for the template
*
* The parameter depends on the reader you are using.
*
* @access public
* @param string root base of the templates
*/
function setRoot($root, $reader = '__default')
{
$this->_options['root'][$reader] = $root;
}
/**
* gets name of root base for the templates
*
* @access public
* @return mixed root base
*/
function getRoot($reader = '__default')
{
return $this->_options['root'][$reader];
}
/**
* sets namespace of patTemplate tags
*
* If you want to use more than one namespace, you may set this to
* an array. All tags in these namespaces will be treated as patTemplate
* tags.
*
* @access public
* @param string|array namespace(s)
*/
function setNamespace($ns)
{
$this->_options['namespace'] = $ns;
}
/**
* gets namespace of patTemplate tags
*
* @access public
* @return string|array namespace(s)
*/
function getNamespace()
{
return $this->_options['namespace'];
}
/**
* set default attribute
*
* @access public
* @param string attribute name
* @param mixed attribute value
*/
function setDefaultAttribute( $name, $value )
{
$this->_defaultAttributes[$name] = $value;
}
/**
* set default attributes
*
* @access public
* @param array attributes
*/
function setDefaultAttributes( $attributes )
{
$this->_defaultAttributes = array_merge( $this->_defaultAttributes, $attributes );
}
/**
* get default attributes
*
* @access public
* @return return default attributes
*/
function getDefaultAttributes()
{
return $this->_defaultAttributes;
}
/**
* set the type for the templates
*
* @access public
* @param string type (html or tex)
* @return boolean true on success
*/
function setType( $type )
{
switch( strtolower( $type ) )
{
case "tex":
$this->setTags( '<{', '}>' );
break;
case "html":
$this->setTags( '{', '}' );
break;
default:
return patErrorManager::raiseWarning(
PATTEMPLATE_WARNING_UNKNOWN_TYPE,
"Unknown type '$type'. Please use 'html' or 'tex'."
);
}
return true;
}
/**
* set the start and end tag for variables
*
* @access public
* @param string start tag
* @param string end tag
* @return boolean true on success
*/
function setTags( $startTag, $endTag )
{
$this->_options['startTag'] = $startTag;
$this->_options['endTag'] = $endTag;
$this->_startTag = $startTag;
$this->_endTag = $endTag;
return true;
}
/**
* get start tag for variables
*
* @access public
* @return string start tag
*/
function getStartTag()
{
return $this->_options['startTag'];
}
/**
* get end tag for variables
*
* @access public
* @return string end tag
*/
function getEndTag()
{
return $this->_options['endTag'];
}
/**
* add a directory where patTemplate should search for
* modules.
*
* You may either pass a string or an array of directories.
*
* patTemplate will be searching for a module in the same
* order you added them. If the module cannot be found in
* the custom folders, it will look in
* patTemplate/$moduleType.
*
* @access public
* @param string module type
* @param string|array directory or directories to search.
*/
function addModuleDir( $moduleType, $dir )
{
if( !isset( $this->_moduleDirs[$moduleType] ) )
$this->_moduleDirs[$moduleType] = array();
if( is_array( $dir ) )
$this->_moduleDirs[$moduleType] = array_merge( $this->_moduleDirs[$moduleType], $dir );
else
array_push( $this->_moduleDirs[$moduleType], $dir );
}
/**
* Sets an attribute of a template
*
* supported attributes: visibilty, loop, parse, unusedvars
*
* @param string $template name of the template
* @param string $attribute name of the attribute
* @param mixed $value value of the attribute
* @access public
* @see setAttributes(),getAttribute(), clearAttribute()
*/
function setAttribute( $template, $attribute, $value )
{
$template = strtolower( $template );
if( !isset( $this->_templates[$template] ) )
{
return patErrorManager::raiseWarning(
PATTEMPLATE_WARNING_NO_TEMPLATE,
"Template '$template' does not exist."
);
}
$attribute = strtolower( $attribute );
$this->_templates[$template]['attributes'][$attribute] = $value;
return true;
}
/**
* Sets several attribute of a template
*
* $attributes has to be a assotiative arrays containing attribute/value pairs
* supported attributes: visibilty, loop, parse, unusedvars
*
* @param string $template name of the template
* @param array $attributes attribute/value pairs
* @access public
* @see setAttribute(), getAttribute(), clearAttribute()
*/
function setAttributes( $template, $attributes )
{
if( !is_array( $attributes ) )
{
return patErrorManager::raiseError( PATTEMPLATE_ERROR_EXPECTED_ARRAY, 'patTemplate::setAttributes: Expected array as second parameter, '.gettype( $attributes ).' given' );
}
$template = strtolower( $template );
$attributes = array_change_key_case( $attributes );
if( !isset( $this->_templates[$template] ) )
{
return patErrorManager::raiseWarning(
PATTEMPLATE_WARNING_NO_TEMPLATE,
"Template '$template' does not exist."
);
}
$this->_templates[$template]['attributes'] = array_merge( $this->_templates[$template]['attributes'], $attributes );
return true;
}
/**
* Get all attributes of a template
*
* @param string name of the template
* @return array attributes
* @access public
*/
function getAttributes( $template )
{
$template = strtolower( $template );
if( !isset( $this->_templates[$template] ) )
{
return patErrorManager::raiseWarning(
PATTEMPLATE_WARNING_NO_TEMPLATE,
"Template '$template' does not exist."
);
}
return $this->_templates[$template]['attributes'];
}
/**
* Gets an attribute of a template
*
* supported attributes: visibilty, loop, parse, unusedvars
*
* @param string $template name of the template
* @param string $attribute name of the attribute
* @return mixed value of the attribute
* @access public
* @see setAttribute(), setAttributes(), clearAttribute()
*/
function getAttribute( $template, $attribute )
{
$template = strtolower( $template );
$attribute = strtolower( $attribute );
if( !isset( $this->_templates[$template] ) )
{
return patErrorManager::raiseWarning(
PATTEMPLATE_WARNING_NO_TEMPLATE,
"Template '$template' does not exist."
);
}
return $this->_templates[$template]['attributes'][$attribute];
}
/**
* Clears an attribute of a template
*
* supported attributes: visibilty, loop, parse, unusedvars
*
* @param string $template name of the template
* @param string $attribute name of the attribute
* @access public
* @see setAttribute(), setAttributes(), getAttribute()
*/
function clearAttribute( $template, $attribute )
{
$template = strtolower( $template );
$attribute = strtolower( $attribute );
if( !isset( $this->_templates[$template] ) )
{
return patErrorManager::raiseWarning(
PATTEMPLATE_WARNING_NO_TEMPLATE,
"Template '$template' does not exist."
);
}
$this->_templates[$template]['attributes'][$attribute] = '';;
return true;
}
/**
* Prepare a template
*
* This can be used if you want to add variables to
* a template, that has not been loaded yet.
*
* @access public
* @param string template name
*/
function prepareTemplate( $name )
{
$name = strtolower( $name );
if( !isset( $this->_vars[$name] ) )
{
$this->_vars[$name] = array(
'scalar' => array(),
'rows' => array()
);
}
}
/**
* add a variable to a template
*
* A variable may also be an indexed array, but _not_
* an associative array!
*
* @access public
* @param string $template name of the template
* @param string $varname name of the variable
* @param mixed $value value of the variable
*/
function addVar( $template, $varname, $value )
{
$template = strtolower( $template );
$varname = strtoupper( $varname );
if( !is_array( $value ) ) {
$this->_vars[$template]['scalar'][$varname] = $value;
return true;
}
$cnt = count( $value );
for ($i = 0; $i < $cnt; $i++) {
if (!isset( $this->_vars[$template]['rows'][$i] )) {
$this->_vars[$template]['rows'][$i] = array();
}
$this->_vars[$template]['rows'][$i][$varname] = $value[$i];
}
return true;
}
/**
* get the value of a variable
*
* @access public
* @param string name of the template
* @param string name of the variable
* @return string value of the variable, null if the variable is not set
*/
function getVar( $template, $varname )
{
$template = strtolower( $template );
$varname = strtoupper( $varname );
if( isset( $this->_vars[$template]['scalar'][$varname] ) )
return $this->_vars[$template]['scalar'][$varname];
$value = array();
if(!isset($this->_vars[$template]['rows']))
return null;
$cnt = count( $this->_vars[$template]['rows'] );
for( $i = 0; $i < $cnt; $i++ )
{
if( !isset( $this->_vars[$template]['rows'][$i][$varname] ) )
continue;
array_push( $value, $this->_vars[$template]['rows'][$i][$varname] );
}
if( !empty( $value ) )
return $value;
return null;
}
/**
* clear the value of a variable
*
* @access public
* @param string name of the template
* @param string name of the variable
* @return boolean
* @see clearVars(), clearTemplate()
*/
function clearVar( $template, $varname )
{
$template = strtolower( $template );
$varname = strtoupper( $varname );
if (isset( $this->_vars[$template]['scalar'][$varname] )) {
unset ($this->_vars[$template]['scalar'][$varname]);
return true;
}
$result = false;
$cnt = count( $this->_vars[$template]['rows'] );
for ($i = 0; $i < $cnt; $i++) {
if (!isset($this->_vars[$template]['rows'][$i][$varname])) {
continue;
}
unset($this->_vars[$template]['rows'][$i][$varname]);
$result = true;
}
return $result;
}
/**
* Adds several variables to a template
*
* Each Template can have an unlimited amount of its own variables
* $variables has to be an assotiative array containing variable/value pairs
*
* @param string $template name of the template
* @param array $variables assotiative array of the variables
* @param string $prefix prefix for all variable names
* @access public
* @see addVar(), addRows(), addGlobalVar(), addGlobalVars()
*/
function addVars( $template, $variables, $prefix = '' )
{
$template = strtolower( $template );
$prefix = strtoupper( $prefix );
$variables = array_change_key_case( $variables, CASE_UPPER );
foreach ($variables as $varname => $value) {
$varname = $prefix.$varname;
if (!is_array($value)) {
if (!is_scalar($value)) {
continue;
}
$this->_vars[$template]['scalar'][$varname] = $value;
continue;
}
$cnt = count( $value );
for( $i = 0; $i < $cnt; $i++ ) {
if( !isset( $this->_vars[$template]['rows'][$i] ) )
$this->_vars[$template]['rows'][$i] = array();
$this->_vars[$template]['rows'][$i][$varname] = $value[$i];
}
}
}
/**
* Clear all variables in a template
*
* This clears only variables, but does
*
* @access public
* @param string $template name of the template
* @return boolean
* @see clearVar(), clearTemplate()
*/
function clearVars( $template )
{
$template = strtolower($template);
$this->_vars[$template] = array(
'scalar' => array(),
'rows' => array()
);
return true;
}
/**
* Adds several rows of variables to a template
*
* Each Template can have an unlimited amount of its own variables
* Can be used to add a database result as variables to a template
*
* @param string $template name of the template
* @param array $rows array containing assotiative arrays with variable/value pairs
* @param string $prefix prefix for all variable names
* @access public
* @see addVar(), addVars(), addGlobalVar(), addGlobalVars()
*/
function addRows( $template, $rows, $prefix = '' )
{
$template = strtolower( $template );
$prefix = strtoupper( $prefix );
$cnt = count( $rows );
for( $i = 0; $i < $cnt; $i++ )
{
if( !isset( $this->_vars[$template]['rows'][$i] ) )
$this->_vars[$template]['rows'][$i] = array();
$rows[$i] = array_change_key_case( $rows[$i], CASE_UPPER );
foreach( $rows[$i] as $varname => $value )
{
$this->_vars[$template]['rows'][$i][$prefix.$varname] = $value;
}
}
}
/**
* Adds an object to a template
*
* All properties of the object will be available as template variables.
*
* @access public
* @param string name of the template
* @param object|array object or array of objects
* @param string prefix for all variable names
* @param boolean ignore private properties (starting with _)
* @see addVar(), addRows(), addGlobalVar(), addGlobalVars()
*/
function addObject( $template, $object, $prefix = '', $ignorePrivate = false )
{
if( is_array( $object ) ) {
$rows = array();
foreach($object as $o) {
array_push( $rows, $this->getObjectVars($o, $ignorePrivate) );
}
return $this->addRows( $template, $rows, $prefix );
} elseif (is_object($object)) {
return $this->addVars( $template, $this->getObjectVars($object, $ignorePrivate), $prefix );
}
return false;
}
/**
* get the vars from an object
*
* @access private
* @param object
* @param boolean ignore private properties (starting with _)
* @return array
*/
function getObjectVars($obj, $ignorePrivate = false)
{
if (method_exists($obj, 'getVars')) {
return $obj->getVars();
}
$vars = get_object_vars($obj);
if ($ignorePrivate === false) {
return $vars;
}
foreach ($vars as $var => $value) {
if ($var{0} == '_') {
unset($vars[$var]);
}
}
return $vars;
}
/**
* Adds a global variable
*
* Global variables are valid in all templates of this object.
* A global variable has to be scalar, it will be converted to a string.
*
* @access public
* @param string $varname name of the global variable
* @param string $value value of the variable
* @return boolean true on success
* @see addGlobalVars(), addVar(), addVars(), addRows()
*/
function addGlobalVar( $varname, $value )
{
$this->_globals[strtoupper( $varname )] = ( string )$value;
return true;
}
/**
* Clears a global variable
*
* @access public
* @param string $varname name of the global variable
* @return boolean true on success
* @see clearVar(), clearVars(), clearGlobalVars()
*/
function clearGlobalVar( $varname )
{
$varname = strtoupper( $varname );
if (!isset($this->_globals[$varname])) {
return false;
}
unset($this->_globals[$varname]);
return true;
}
/**
* Clears all global variables
*
* @access public
* @return boolean true on success
* @see clearVar(), clearVars(), clearGlobalVar()
*/
function clearGlobalVars()
{
$this->_globals = array();
return true;
}
/**
* Adds several global variables
*
* Global variables are valid in all templates of this object.
*
* $variables is an associative array, containing name/value pairs of the variables.
*
* @access public
* @param array $variables array containing the variables
* @param string $prefix prefix for variable names
* @return boolean true on success
* @see addGlobalVar(), addVar(), addVars(), addRows()
*/
function addGlobalVars( $variables, $prefix = '' )
{
$variables = array_change_key_case( $variables, CASE_UPPER );
$prefix = strtoupper( $prefix );
foreach( $variables as $varname => $value )
{
$this->_globals[$prefix.$varname] = ( string )$value;
}
return true;
}
/**
* get all global variables
*
* @access public
* @return array global variables
*/
function getGlobalVars()
{
return $this->_globals;
}
/**
* checks wether a template exists
*
* @access public
* @param string name of the template
* @return boolean true, if the template exists, false otherwise
*/
function exists( $name )
{
return in_array( strtolower( $name ), $this->_templateList );
}
/**
* enable a template cache
*
* A template cache will improve performace, as the templates
* do not have to be read on each request.
*
* @access public
* @param string name of the template cache
* @param array parameters for the template cache
* @return boolean true on success, patError otherwise
*/
function useTemplateCache( $cache, $params = array() )
{
if( !is_object( $cache ) )
{
$cache = &$this->loadModule( 'TemplateCache', $cache, $params );
}
if( patErrorManager::isError( $cache ) )
return $cache;
$this->_tmplCache = &$cache;
return true;
}
/**
* enable an output filter
*
* Output filters are used to modify the template
* result before it is sent to the browser.
*
* They are applied, when displayParsedTemplate() is called.
*
* @access public
* @param string name of the output filter
* @param array parameters for the output filter
* @return boolean true on success, patError otherwise
*/
function applyOutputFilter( $filter, $params = array(), $template = null )
{
if (!is_object($filter)) {
$filter = &$this->loadModule( 'OutputFilter', $filter, $params );
}
if (patErrorManager::isError($filter)) {
return $filter;
}
if ($template === null) {
$this->_outputFilters[] = &$filter;
return true;
}
$template = strtolower($template);
if (!$this->exists($template)) {
return patErrorManager::raiseWarning(PATTEMPLATE_WARNING_NO_TEMPLATE, 'The selected template does not exist');
}
$this->_templates[$template]['attributes']['outputfilter'] = &$filter;
return true;
}
/**
* enable an input filter
*
* input filters are used to modify the template
* stream before it is split into smaller templates-
*
* @access public
* @param string name of the input filter
* @param array parameters for the input filter
* @return boolean true on success, patError otherwise
*/
function applyInputFilter( $filter, $params = array() )
{
if( !is_object( $filter ) )
{
$filter = &$this->loadModule( 'InputFilter', $filter, $params );
}
if( patErrorManager::isError( $filter ) )
return $filter;
$this->_inputFilters[] = &$filter;
return true;
}
/**
* open a file and parse for patTemplate tags
*
* @access public
* @param name of the file
* @return true, if the template could be parsed
* @deprecated Use patTemplate::readTemplatesFromInput() instead, as the method name is misleading
* @see readTemplatesFromInput()
*/
function readTemplatesFromFile( $filename )
{
return $this->readTemplatesFromInput( $filename, 'File' );
}
/**
* open any input and parse for patTemplate tags
*
* @access public
* @param string name of the input (filename, shm segment, etc.)
* @param string driver that is used as reader, you may also pass a Reader object
* @param array additional options that will only be used for this template
* @param string name of the template that should be used as a container, should not be used by public
* calls.
* @return boolean true, if the template could be parsed, false otherwise
*/
function readTemplatesFromInput( $input, $reader = 'File', $options = null, $parseInto = null )
{
if ((string)$input === '') {
return patErrorManager::raiseError(PATTEMPLATE_ERROR_NO_INPUT, 'No input to read has been passed.');
}
if (is_array($options)) {
$options = array_merge( $this->_options, $options );
} else {
$options = $this->_options;
}
if (!is_null($parseInto)) {
$parseInto = strtolower( $parseInto );
}
$templates = false;
if ($this->_tmplCache !== null) {
/**
* get the unique cache key
*/
$key = $this->_tmplCache->getKey($input, $options);
$templates = $this->_loadTemplatesFromCache( $input, $reader, $options, $key );
/**
* check for error returned from cache
*/
if (patErrorManager::isError($templates)) {
return $templates;
}
}
/**
* templates have not been loaded from cache
*/
if ($templates === false) {
if (!is_object( $reader)) {
$reader = &$this->loadModule('Reader', $reader);
if (patErrorManager::isError($reader)) {
return $reader;
}
}
if ($reader->isInUse()) {
$reader = &$this->loadModule( 'Reader', $reader->getName(), array(), true);
if( patErrorManager::isError( $reader ) ) {
return $reader;
}
}
$reader->setOptions($options);
/**
* set the root attributes
*/
if( !is_null( $parseInto ) )
{
$attributes = $this->getAttributes( $parseInto );
if( !patErrorManager::isError( $attributes ) )
{
$reader->setRootAttributes( $attributes );
}
}
$templates = $reader->readTemplates( $input );
/**
* check for error returned from reader
*/
if( patErrorManager::isError( $templates ) )
return $templates;
/**
* store the
*/
if( $this->_tmplCache !== null )
{
$this->_tmplCache->write( $key, $templates );
}
}
/**
* traverse all templates
*/
foreach( $templates as $name => $spec )
{
/**
* root template
*/
if( $name == '__ptroot' )
{
if( $parseInto === false )
{
continue;
}
if( !in_array( $parseInto, $this->_templateList ) )
continue;
$spec['loaded'] = true;
$spec['attributes'] = $this->_templates[$parseInto]['attributes'];
$name = $parseInto;
}
else
{
/**
* store the name
*/
array_push( $this->_templateList, $name );
}
/**
* if this is the first template that has been loaded
* set it as the root template
*/
if( $this->_root === null && is_null( $parseInto ) && isset( $spec['isRoot'] ) && $spec['isRoot'] == true )
{
$this->_root = $name;
}
/**
* set some default values
*/
$spec['iteration'] = 0;
$spec['lastMode'] = 'w';
$spec['result'] = '';
$spec['modifyVars'] = array();
$spec['copyVars'] = array();
$spec['defaultVars'] = array();
/**
* store the template
*/
$this->_templates[$name] = $spec;
$this->prepareTemplate( $name );
/**
* store the default values of the variables
*/
foreach( $spec['varspecs'] as $varname => $varspec )
{
if (isset($varspec['modifier'])) {
$this->_templates[$name]['modifyVars'][$varname] = $varspec['modifier'];
}
if( isset( $varspec['copyfrom'] ) )
{
$this->_templates[$name]['copyVars'][$varname] = $varspec['copyfrom'];
}
if( !isset( $varspec['default'] ) )
continue;
$this->_templates[$name]['defaultVars'][$varname] = $varspec['default'];
if( !is_null( $this->getVar( $name, $varname ) ) )
continue;
$this->addVar( $name, $varname, $varspec['default'] );
}
unset($this->_templates[$name]['varspecs']);
/**
* autoload the template
*
* Some error management is needed here...
*/
if( isset( $this->_templates[$name]['attributes']['src'] ) && $this->_templates[$name]['attributes']['autoload'] == 'on' )
{
if( $this->_templates[$name]['loaded'] !== true )
{
if( $this->_templates[$name]['attributes']['parse'] == 'on' )
{
$this->readTemplatesFromInput( $this->_templates[$name]['attributes']['src'], $this->_templates[$name]['attributes']['reader'], $options, $name );
}
else
{
$this->loadTemplateFromInput( $this->_templates[$name]['attributes']['src'], $this->_templates[$name]['attributes']['reader'], null, $name );
}
$this->_templates[$name]['loaded'] = true;
}
}
}
return true;
}
/**
* load from template cache
*
* @access private
* @param string name of the input (filename, shm segment, etc.)
* @param string driver that is used as reader, you may also pass a Reader object
* @param array options for the reader
* @param string cache key
* @return array|boolean either an array containing the templates, or false
*/
function _loadTemplatesFromCache( $input, &$reader, $options, $key )
{
if( is_object( $reader ) )
$statName = $reader->getName();
else
$statName = $reader;
$stat = &$this->loadModule( 'Stat', $statName );
$stat->setOptions( $options );
/**
* get modification time
*/
$modTime = $stat->getModificationTime( $input );
$templates = $this->_tmplCache->load( $key, $modTime );
return $templates;
}
/**
* open any input and load content into template
*
* @access public
* @param string name of the input (filename, shm segment, etc.)
* @param string driver that is used as reader
* @param string name of the template that should be used as a container,
* @return boolean true, if the template could be parsed, false otherwise
*/
function loadTemplateFromInput( $input, $reader = 'File', $options = null, $parseInto = false )
{
if( is_array( $options ) )
$options = array_merge( $this->_options, $options );
else
$options = $this->_options;
if( !is_null( $parseInto ) )
$parseInto = strtolower( $parseInto );
$reader = &$this->loadModule( 'Reader', $reader );
if( patErrorManager::isError( $reader ) )
{
return $reader;
}
$reader->setOptions($options);
$result = $reader->loadTemplate( $input );
if( patErrorManager::isError( $result ) )
{
return $result;
}
$this->_templates[$parseInto]['content'] .= $result;
$this->_templates[$parseInto]['loaded'] = true;
return true;
}
/**
* load a template that had autoload="off"
*
* This is needed, if you change the source of a template and want to
* load it, after changing the attribute.
*
* @access public
* @param string template name
* @return boolean true, if template could be loaded
*/
function loadTemplate( $template )
{
$template = strtolower( $template );
if( !isset( $this->_templates[$template] ) )
{
return patErrorManager::raiseWarning(
PATTEMPLATE_WARNING_NO_TEMPLATE,
"Template '$template' does not exist."
);
}
if( $this->_templates[$template]['loaded'] === true )
return true;
if( $this->_templates[$template]['attributes']['parse'] == 'on' )
{
return $this->readTemplatesFromInput( $this->_templates[$template]['attributes']['src'], $this->_templates[$template]['attributes']['reader'], null, $template );
}
else
{
return $this->loadTemplateFromInput( $this->_templates[$template]['attributes']['src'], $this->_templates[$template]['attributes']['reader'], null, $template );
}
}
/**
* loads a patTemplate module
*
* Modules are located in the patTemplate folder and include:
* - Readers
* - Caches
* - Variable Modifiers
* - Filters
* - Functions
* - Stats
*
* @access public
* @param string moduleType (Reader|TemplateCache|Modifier|OutputFilter|InputFilter)
* @param string moduleName
* @param array parameters for the module
* @return object
*/
function &loadModule( $moduleType, $moduleName, $params = array(), $new = false )
{
if( !isset( $this->_modules[$moduleType] ) )
$this->_modules[$moduleType] = array();
$sig = md5( $moduleName . serialize( $params ) );
if( isset( $this->_modules[$moduleType][$sig] ) && $new === false ) {
return $this->_modules[$moduleType][$sig];
}
if( !class_exists( 'patTemplate_Module' ) )
{
$file = sprintf( "%s/Module.php", $this->getIncludePath() );
if( !file_exists( $file ) or !include_once $file )
return patErrorManager::raiseError( PATTEMPLATE_ERROR_BASECLASS_NOT_FOUND, 'Could not load module base class.' );
}
$baseClass = 'patTemplate_' . $moduleType;
if( !class_exists( $baseClass ) )
{
$baseFile = sprintf( "%s/%s.php", $this->getIncludePath(), $moduleType );
if( !file_exists( $baseFile ) or !include_once $baseFile )
return patErrorManager::raiseError( PATTEMPLATE_ERROR_BASECLASS_NOT_FOUND, "Could not load base class for $moduleType ($baseFile)." );
}
$moduleClass = 'patTemplate_' . $moduleType . '_' .$moduleName;
if( !class_exists( $moduleClass ) )
{
if( isset( $this->_moduleDirs[$moduleType] ) )
$dirs = $this->_moduleDirs[$moduleType];
else
$dirs = array();
array_push( $dirs, $this->getIncludePath() .'/'. $moduleType );
$found = false;
foreach( $dirs as $dir )
{
$moduleFile = sprintf( "%s/%s.php", $dir, str_replace( '_', '/', $moduleName ) );
if ( file_exists( $moduleFile ) and include_once $moduleFile) {
$found = true;
break;
}
}
if( !$found ) {
return patErrorManager::raiseError( PATTEMPLATE_ERROR_MODULE_NOT_FOUND, "Could not load module $moduleClass ($moduleFile)." );
}
}
if( !class_exists( $moduleClass ) )
{
return patErrorManager::raiseError( PATTEMPLATE_ERROR_MODULE_NOT_FOUND, "Module file $moduleFile does not contain class $moduleClass." );
}
$this->_modules[$moduleType][$sig] = &new $moduleClass;
if( method_exists( $this->_modules[$moduleType][$sig], 'setTemplateReference' ) )
{
$this->_modules[$moduleType][$sig]->setTemplateReference( $this );
}
$this->_modules[$moduleType][$sig]->setParams( $params );
return $this->_modules[$moduleType][$sig];
}
/**
* checks whether a module exists.
*
* Modules are located in the patTemplate folder and include:
* - Readers
* - Caches
* - Variable Modifiers
* - Filters
* - Functions
* - Stats
*
* @access public
* @param string moduleType (Reader|TemplateCache|Modifier|OutputFilter|InputFilter)
* @param string moduleName
* @return boolean
*/
function moduleExists( $moduleType, $moduleName )
{
// !!!JOOMLA VARIATION!!!
// cache checks on files
static $paths;
if (!$paths)
{
$paths = array();
}
if (isset($this->_moduleDirs[$moduleType])) {
$dirs = $this->_moduleDirs[$moduleType];
} else {
$dirs = array();
}
array_push($dirs, $this->getIncludePath() .'/'. $moduleType);
foreach ($dirs as $dir) {
$moduleFile = sprintf( "%s/%s.php", $dir, str_replace( '_', '/', $moduleName ) );
if (!isset( $paths[$moduleFile] ))
{
if (!file_exists($moduleFile)) {
$paths[$moduleFile] = false;
}
else if (!is_readable($moduleFile)) {
$paths[$moduleFile] = false;
}
else
{
$paths[$moduleFile] = true;
}
}
if (!$paths[$moduleFile]) {
continue;
}
return true;
}
return false;
}
/**
* parses a template
*
* Parses a template and stores the parsed content.
* mode can be "w" for write (delete already parsed content) or "a" for append (appends the
* new parsed content to the already parsed content)
*
* @access public
* @param string name of the template
* @param string mode for the parsing
*/
function parseTemplate( $template, $mode = 'w' )
{
$template = strtolower($template);
if (!isset($this->_templates[$template])) {
return patErrorManager::raiseWarning(
PATTEMPLATE_WARNING_NO_TEMPLATE,
"Template '$template' does not exist."
);
}
/**
* template is not visible
*/
if ($this->_templates[$template]['attributes']['visibility'] == 'hidden') {
$this->_templates[$template]['result'] = '';
$this->_templates[$template]['parsed'] = true;
return true;
}
/**
* check, if the template has been loaded
* and load it if necessary.
*/
if ($this->_templates[$template]['loaded'] !== true) {
if ($this->_templates[$template]['attributes']['parse'] == 'on') {
$result = $this->readTemplatesFromInput( $this->_templates[$template]['attributes']['src'], $this->_templates[$template]['attributes']['reader'], null, $template );
} else {
$result = $this->loadTemplateFromInput( $this->_templates[$template]['attributes']['src'], $this->_templates[$template]['attributes']['reader'], null, $template );
}
if (patErrorManager::isError($result)) {
return $result;
}
}
/**
* check for autoclear
*/
if(
isset( $this->_templates[$template]['attributes']['autoclear'] ) &&
$this->_templates[$template]['attributes']['autoclear'] == 'yes' &&
$mode === 'w' &&
$this->_templates[$template]['lastMode'] != 'a'
) {
$this->_templates[$template]['parsed'] = false;
}
/**
* template has been parsed and mode is not 'append'
*/
if ($this->_templates[$template]['parsed'] === true && $mode === 'w') {
return true;
}
$this->_templates[$template]['lastMode'] = $mode;
$this->_initTemplate( $template );
if (!isset($this->_vars[$template]['rows'])) {
$this->_vars[$template]['rows'] = array();
}
$loop = count( $this->_vars[$template]['rows'] );
/**
* loop at least one times
*/
if ($loop < 1) {
$loop = 1;
}
if (isset($this->_templates[$template]['attributes']['maxloop'])) {
$loop = ceil( $loop / $this->_templates[$template]['attributes']['maxloop'] ) * $this->_templates[$template]['attributes']['maxloop'];
}
$this->_templates[$template]['loop'] = max( $this->_templates[$template]['attributes']['loop'], $loop );
$start = 0;
if (isset($this->_templates[$template]['attributes']['limit'])) {
$p = strpos( $this->_templates[$template]['attributes']['limit'], ',' );
if ($p === false) {
$this->_templates[$template]['loop'] = min( $this->_templates[$template]['loop'], $this->_templates[$template]['attributes']['limit'] );
$start = 0;
} else {
$start = substr( $this->_templates[$template]['attributes']['limit'], 0, $p );
$end = substr( $this->_templates[$template]['attributes']['limit'], $p+1 )+$start;
$this->_templates[$template]['loop'] = min( $this->_templates[$template]['loop'], $end );
}
}
/**
* template should be cleared before parsing
*/
if ($mode == 'w') {
$this->_templates[$template]['result'] = '';
$this->_templates[$template]['iteration'] = $start;
}
$loopCount = 0;
for ($i = $start; $i < $this->_templates[$template]['loop']; $i++) {
$finished = false;
unset( $this->_templates[$template]['vars'] );
/**
* fetch the variables
*/
$this->_fetchVariables( $template );
/**
* fetch the template
*/
$result = $this->_fetchTemplate($template);
if ($result === false) {
$this->_templates[$template]['iteration']++;
continue;
}
/**
* parse
*/
$this->_parseVariables( $template );
$result = $this->_parseDependencies( $template );
if (patErrorManager::isError($result)) {
return $result;
}
/**
* store result
*/
$this->_templates[$template]['result'] .= $this->_templates[$template]['work'];
$this->_templates[$template]['iteration']++;
++$loopCount;
/**
* check for maximum loops
*/
if (isset($this->_templates[$template]['attributes']['maxloop'])) {
if ($loopCount == $this->_templates[$template]['attributes']['maxloop'] && $i < ($loop-1)) {
$loopCount = 0;
$finished = true;
$this->_templates[$template]['parsed'] = true;
$this->parseTemplate( $this->_templates[$template]['attributes']['parent'], 'a' );
$this->_templates[$template]['parsed'] = false;
$this->_templates[$template]['result'] = '';
}
}
}
if (!$finished && isset($this->_templates[$template]['attributes']['maxloop'])) {
$this->_templates[$template]['parsed'] = true;
$this->parseTemplate( $this->_templates[$template]['attributes']['parent'], 'a', false );
$this->_templates[$template]['parsed'] = false;
$this->_templates[$template]['result'] = '';
$this->_templates[$this->_templates[$template]['attributes']['parent']]['work'] = '';
}
$this->_parseGlobals($template);
$this->_handleUnusedVars($template);
$this->_templates[$template]['parsed'] = true;
if (isset($this->_templates[$template]['attributes']['autoclear']) && $this->_templates[$template]['attributes']['autoclear'] == 'yes') {
$this->_vars[$template] = array(
'scalar' => array(),
'rows' => array()
);
}
if (isset($this->_templates[$template]['attributes']['outputfilter'])) {
if (is_object($this->_templates[$template]['attributes']['outputfilter'])) {
$filter = &$this->_templates[$template]['attributes']['outputfilter'];
} else {
$filter = &$this->loadModule('OutputFilter', $this->_templates[$template]['attributes']['outputfilter']);
}
if (patErrorManager::isError($filter)) {
return $filter;
}
$this->_templates[$template]['result'] = $filter->apply($this->_templates[$template]['result']);
}
return true;
}
/**
* Initialize a template
*
* This method checks the variable specifications and
* copys variables from other templates.
*
* @access private
* @param string name of the template
* @return boolean true on success
*/
function _initTemplate( $template )
{
foreach( $this->_templates[$template]['copyVars'] as $dest => $src )
{
/**
* copy from the same template
*/
if( !is_array( $src ) )
{
$srcTemplate = $template;
$srcVar = $src;
}
else
{
$srcTemplate = $src[0];
$srcVar = $src[1];
}
$copied = false;
/**
* copy from another template
*/
if( isset( $this->_vars[$srcTemplate] ) )
{
if( isset( $this->_vars[$srcTemplate]['scalar'][$srcVar] ) )
{
$this->_vars[$template]['scalar'][$dest] = $this->_vars[$srcTemplate]['scalar'][$srcVar];
continue;
}
$rows = count( $this->_vars[$srcTemplate]['rows'] );
for( $i = 0; $i < $rows; $i++ )
{
if( !isset( $this->_vars[$srcTemplate]['rows'][$i][$srcVar] ) )
continue;
if( !isset( $this->_vars[$template]['rows'][$i] ) )
$this->_vars[$template]['rows'][$i] = array();
$this->_vars[$template]['rows'][$i][$dest] = $this->_vars[$srcTemplate]['rows'][$i][$srcVar];
$copied = true;
}
}
if( !$copied && isset( $this->_globals[$srcVar] ))
{
$this->_vars[$template]['scalar'][$dest] = $this->_globals[$srcVar];
}
}
return true;
}
/**
* parse all variables in a template
*
* @access private
* @param string
*/
function _parseVariables( $template )
{
/**
* modify variables before parsing
*/
$this->_applyModifers($template, $this->_templates[$template]['vars']);
foreach( $this->_templates[$template]['vars'] as $key => $value )
{
if( is_array( $value ) )
{
if( count( $this->_templates[$template]['currentDependencies'] ) == 1 )
{
$child = $this->_templates[$template]['currentDependencies'][0];
}
else
{
if( isset( $this->_templates[$template]['attributes']['child'] ) )
$child = $this->_templates[$template]['attributes']['child'];
else
continue;
}
$this->setAttribute( $child, 'autoclear', 'yes' );
$this->addVar( $child, $key, $value );
continue;
}
$var = $this->_startTag.$key.$this->_endTag;
$this->_templates[$template]['work'] = str_replace( $var, $value, $this->_templates[$template]['work'] );
}
return true;
}
/**
* parse global variables in the template
*
* @access private
* @param string name of the template
* @return boolean
*/
function _parseGlobals($template)
{
$globalVars = $this->_globals;
$this->_applyModifers($template, $globalVars);
foreach( $globalVars as $key => $value )
{
if( is_array( $value ) )
{
continue;
}
$var = $this->_startTag.$key.$this->_endTag;
$this->_templates[$template]['result'] = str_replace( $var, $value, $this->_templates[$template]['result'] );
}
return true;
}
/**
* apply variable modifiers
*
* The variables will be passed by reference.
*
* @access private
* @param string name of the template (use modifiers from this template)
* @param array variables to which the modifiers should be applied
* @return boolean
*/
function _applyModifers($template, &$vars)
{
foreach ($this->_templates[$template]['modifyVars'] as $varname => $modifier) {
if (!isset($vars[$varname])) {
continue;
}
if (($modifier['type'] === 'php' || $modifier['type'] === 'auto' ) && is_callable($modifier['mod'])) {
$vars[$varname] = call_user_func($modifier['mod'], $vars[$varname]);
continue;
}
if ($modifier['type'] === 'php') {
continue;
}
$mod = &$this->loadModule( 'Modifier', ucfirst( $modifier['mod'] ) );
$vars[$varname] = $mod->modify( $vars[$varname], $modifier['params'] );
}
// apply the default modifier
if (isset($this->_templates[$template]['attributes']['defaultmodifier'])) {
$defaultModifier = $this->_templates[$template]['attributes']['defaultmodifier'];
if (is_callable($defaultModifier)) {
$type = 'php';
} else {
$type = 'custom';
$defaultModifier = &$this->loadModule('Modifier', ucfirst($defaultModifier));
}
foreach (array_keys($vars) as $varname) {
if (isset($this->_templates[$template]['modifyVars'][$varname])) {
continue;
}
if ($type === 'php') {
$vars[$varname] = call_user_func($defaultModifier, $vars[$varname]);
} else {
$vars[$varname] = $defaultModifier->modify($vars[$varname], array());
}
}
}
return true;
}
/**
* parse all dependencies in a template
*
* @access private
* @param string
*/
function _parseDependencies($template)
{
$countDep = count( $this->_templates[$template]['currentDependencies'] );
for ($i = 0; $i < $countDep; $i++) {
$depTemplate = $this->_templates[$template]['currentDependencies'][$i];
if ($depTemplate == $template) {
return patErrorManager::raiseError(PATTEMPLATE_ERROR_RECURSION, 'You have an error in your template "' . $template . '", which leads to recursion');
}
$this->parseTemplate($depTemplate);
$var = $this->_startTag.'TMPL:'.strtoupper( $depTemplate) .$this->_endTag;
$this->_templates[$template]['work'] = str_replace( $var, $this->_templates[$depTemplate]['result'], $this->_templates[$template]['work'] );
}
return true;
}
/**
* fetch plain template
*
* The template content will be stored in the template
* configuration so it can be used by other
* methods.
*
* @access private
* @param string template name
* @return boolean
*/
function _fetchTemplate( $template )
{
switch( $this->_templates[$template]['attributes']['type'] )
{
/**
* condition template
*/
case 'condition':
$value = $this->_getConditionValue($template, $this->_templates[$template]['attributes']['conditionvar']);
if ($value === false) {
$this->_templates[$template]['work'] = '';
$this->_templates[$template]['currentDependencies'] = array();
} else {
$this->_templates[$template]['work'] = $this->_templates[$template]['subtemplates'][$value]['data'];
$this->_templates[$template]['currentDependencies'] = $this->_templates[$template]['subtemplates'][$value]['dependencies'];
}
break;
/**
* condition template
*/
case 'simplecondition':
foreach( $this->_templates[$template]['attributes']['requiredvars'] as $var )
{
// different template scope
if( $var[0] !== $template ) {
$this->_fetchVariables($var[0]);
}
$value = null;
// fetch the local variable
if( isset( $this->_templates[$var[0]]['vars'][$var[1]] )
&& strlen( $this->_templates[$var[0]]['vars'][$var[1]] ) > 0 ) {
$value = $this->_templates[$var[0]]['vars'][$var[1]];
}
if (isset($this->_templates[$template]['attributes']['useglobals'])) {
if(isset($this->_globals[$var[1]]) && strlen($this->_globals[$var[1]]) > 1) {
$value = $this->_globals[$var[1]];
}
}
if ($value !== null) {
if ($var[2] === null) {
continue;
} else {
// Joomla! addition 23-June-2005
// value wrapped in ## uses regex for comparison
$condition = $var[2];
if (substr( $condition, 0, 1 ) == '#' && substr( $condition, -1, 1 ) == '#' ) {
if (preg_match( $condition, $value )) {
continue;
}
} else if ($condition == $value) {
continue;
}
/* Pat Original
if ($var[2] == $value) {
continue;
}
*/
}
}
$this->_templates[$template]['work'] = '';
$this->_templates[$template]['currentDependencies'] = array();
break 2;
}
$this->_templates[$template]['work'] = $this->_templates[$template]['content'];
$this->_templates[$template]['currentDependencies'] = $this->_templates[$template]['dependencies'];
break;
/**
* modulo template
*/
case 'modulo':
// check for empty template
if ($this->_hasVariables($template)) {
$value = (string)($this->_templates[$template]['iteration'] + 1 ) % $this->_templates[$template]['attributes']['modulo'];
} else {
$value = '__empty';
}
$value = $this->_getConditionValue($template, $value, false);
if ($value === false) {
$this->_templates[$template]['work'] = '';
$this->_templates[$template]['currentDependencies'] = array();
} else {
$this->_templates[$template]['work'] = $this->_templates[$template]['subtemplates'][$value]['data'];
$this->_templates[$template]['currentDependencies'] = $this->_templates[$template]['subtemplates'][$value]['dependencies'];
}
break;
/**
* standard template
*/
default:
$this->_templates[$template]['work'] = $this->_templates[$template]['content'];
$this->_templates[$template]['currentDependencies'] = $this->_templates[$template]['dependencies'];
break;
}
return true;
}
/**
* check, whether a template contains variables
*
* @access private
* @param string template name
* @return boolean
*/
function _hasVariables($template)
{
if (!empty($this->_vars[$template]['scalar'])) {
return true;
}
if (isset($this->_vars[$template]['rows'][$this->_templates[$template]['iteration']])) {
return true;
}
return false;
}
/**
* fetch the value of a condition variable
*
* _fetchVariables() has to be called before this
* method is being called.
*
* @access private
* @param string template name
* @param string condition value
* @param boolean flag that indicates whether value is the name of the variable that should be resolved
*
* @todo split this method into smaller check methods that will be called according to
* a priority list
*/
function _getConditionValue( $template, $value, $isVar = true )
{
if ($isVar === true) {
if (isset($this->_templates[$template]['attributes']['conditiontmpl'])) {
$_template = $this->_templates[$template]['attributes']['conditiontmpl'];
$this->_fetchVariables($_template);
} else {
$_template = $template;
}
/**
* get the value from the template variables
*/
if (!isset($this->_templates[$_template]['vars'][$value]) || strlen($this->_templates[$_template]['vars'][$value]) === 0) {
if ($this->_templates[$template]['attributes']['useglobals'] == 'yes' || $this->_templates[$template]['attributes']['useglobals'] == 'useglobals') {
if (isset( $this->_globals[$value] ) && strlen( $this->_globals[$value] ) > 0) {
$value = $this->_globals[$value];
} else {
$value = '__empty';
}
} else {
$value = '__empty';
}
} else {
$value = $this->_templates[$_template]['vars'][$value];
}
} else {
$_template = $template;
}
// if value is empty and a template for empty has been defined, this
// has priority
if ($value === '__empty' && isset($this->_templates[$template]['subtemplates']['__empty'])) {
return $value;
}
// only one iteration (but not empty), use the __single condition
if ($value !== '__empty' && $this->_templates[$_template]['loop'] === 1) {
if( isset($this->_templates[$template]['subtemplates']['__single'])) {
return '__single';
}
} else {
// is __first?
if( $this->_templates[$_template]['iteration'] == 0 ) {
if( isset( $this->_templates[$template]['subtemplates']['__first'] ) ) {
return '__first';
}
}
/**
* is __last?
*/
if (isset($this->_templates[$_template]['loop'])) {
$max = $this->_templates[$_template]['loop'] - 1;
if( $this->_templates[$_template]['iteration'] == $max ) {
if( isset( $this->_templates[$template]['subtemplates']['__last'] ) ) {
return '__last';
}
}
}
}
// search for exact match
foreach (array_keys($this->_templates[$template]['subtemplates']) as $key) {
if (isset($this->_templates[$template]['subtemplates'][$key]['attributes']['var'])) {
$var = $this->_templates[$template]['subtemplates'][$key]['attributes']['var'];
if (isset($this->_templates[$template]['vars'][$var])) {
$current = $this->_templates[$template]['vars'][$var];
} else {
$current = null;
}
} else {
$current = $key;
}
if ((string)$value === (string)$current) {
return $key;
}
}
/**
* is __default?
*/
if( isset( $this->_templates[$template]['subtemplates']['__default'] ) ) {
return '__default';
}
return false;
}
/**
* fetch variables for a template
*
* The variables will be stored in the template
* configuration so they can be used by other
* methods.
*
* @access private
* @param string template name
* @return boolean
*/
function _fetchVariables( $template )
{
/**
* variables already have been fetched
*/
if (isset($this->_templates[$template]['vars'])) {
return true;
}
$iteration = $this->_templates[$template]['iteration'];
$vars = array();
if( isset( $this->_templates[$template]['attributes']['varscope'] ) )
{
if (!is_array($this->_templates[$template]['attributes']['varscope'])) {
$this->_templates[$template]['attributes']['varscope'] = array($this->_templates[$template]['attributes']['varscope']);
}
foreach ($this->_templates[$template]['attributes']['varscope'] as $scopeTemplate) {
if ($this->exists($scopeTemplate)) {
$this->_fetchVariables( $scopeTemplate );
$vars = array_merge($this->_templates[$scopeTemplate]['vars'], $vars);
} else {
patErrorManager::raiseWarning(PATTEMPLATE_WARNING_NO_TEMPLATE, 'Template \''.$scopeTemplate.'\' does not exist, referenced in varscope attribute of template \''.$template.'\'');
}
}
} else {
$vars = array();
}
/**
* get the scalar variables
*/
if( isset( $this->_vars[$template] ) && isset( $this->_vars[$template]['scalar'] ) )
{
$vars = array_merge( $vars, $this->_vars[$template]['scalar'] );
}
/**
* get the row variables
*/
if( isset( $this->_vars[$template]['rows'][$iteration] ) )
{
$vars = array_merge( $vars, $this->_vars[$template]['rows'][$iteration] );
}
/**
* add some system variables
*/
$currentRow = $iteration + $this->_templates[$template]['attributes']['rowoffset'];
$vars['PAT_ROW_VAR'] = $currentRow;
if( $this->_templates[$template]['attributes']['type'] == 'modulo' )
{
$vars['PAT_MODULO_REP'] = ceil( $currentRow / $this->_templates[$template]['attributes']['modulo'] );
$vars['PAT_MODULO'] = ( $this->_templates[$template]['iteration'] + 1 ) % $this->_templates[$template]['attributes']['modulo'];
}
if( $this->_templates[$template]['attributes']['addsystemvars'] !== false )
{
$vars['PATTEMPLATE_VERSION'] = $this->_systemVars['appVersion'];
$vars['PAT_LOOPS'] = $this->_templates[$template]['loop'];
switch ($this->_templates[$template]['attributes']['addsystemvars'])
{
case 'boolean':
$trueValue = 'true';
$falseValue = 'false';
break;
case 'integer':
$trueValue = '1';
$falseValue = '0';
break;
default:
$trueValue = $this->_templates[$template]['attributes']['addsystemvars'];
$falseValue = '';
break;
}
$vars['PAT_IS_ODD'] = ( $currentRow % 2 == 1 ) ? $trueValue : $falseValue;
$vars['PAT_IS_EVEN'] = ( $currentRow % 2 == 0 ) ? $trueValue : $falseValue;
$vars['PAT_IS_FIRST'] = ( $currentRow == 1 ) ? $trueValue : $falseValue;
$vars['PAT_IS_LAST'] = ( $currentRow == $this->_templates[$template]['loop'] ) ? $trueValue : $falseValue;
$vars['PAT_ROW_TYPE'] = ( $currentRow % 2 == 1 ) ? 'odd' : 'even';
}
$this->_templates[$template]['vars'] = $vars;
return true;
}
/**
* handle all unused variables in a template
*
* This is influenced by the 'unusedvars' attribute of the
* template
*
* @access private
* @param string
*/
function _handleUnusedVars( $template )
{
$regexp = '/([^\\\])('.$this->_startTag.'[^a-z]+[^\\\]'.$this->_endTag.')/U';
switch( $this->_templates[$template]['attributes']['unusedvars'] )
{
case 'comment':
$this->_templates[$template]['result'] = preg_replace( $regexp, '<!-- \\1\\2 -->', $this->_templates[$template]['result'] );
break;
case 'strip':
$this->_templates[$template]['result'] = preg_replace( $regexp, '\\1', $this->_templates[$template]['result'] );
break;
case 'nbsp':
$this->_templates[$template]['result'] = preg_replace( $regexp, '\\1 ', $this->_templates[$template]['result'] );
break;
case 'ignore':
break;
default:
$this->_templates[$template]['result'] = preg_replace( $regexp, '\\1'.$this->_templates[$template]['attributes']['unusedvars'], $this->_templates[$template]['result'] );
break;
}
// replace quoted variables
$regexp = '/[\\\]'.$this->_startTag.'([^a-z]+)[\\\]'.$this->_endTag.'/U';
$this->_templates[$template]['result'] = preg_replace( $regexp, $this->_startTag.'\\1'.$this->_endTag, $this->_templates[$template]['result'] );
return true;
}
/**
* returns a parsed Template
*
* If the template already has been parsed, it just returns the parsed template.
* If the template has not been loaded, it will be loaded.
*
* @access public
* @param string name of the template
* @param boolean whether to apply output filters
* @return string Content of the parsed template
* @see displayParsedTemplate()
*/
function getParsedTemplate( $name = null, $applyFilters = false )
{
if (is_null($name)) {
$name = $this->_root;
}
$name = strtolower( $name );
$result = $this->parseTemplate( $name );
if (patErrorManager::isError( $result )) {
return $result;
}
if ($applyFilters === false) {
return $this->_templates[$name]['result'];
}
$result = $this->_templates[$name]['result'];
$cnt = count ($this->_outputFilters);
for ($i = 0; $i < $cnt; $i++) {
$result = $this->_outputFilters[$i]->apply( $result );
}
return $result;
}
/**
* displays a parsed Template
*
* If the template has not been loaded, it will be loaded.
*
* @see getParsedTemplate()
* @param string name of the template
* @param boolean whether to apply output filters
* @return boolean true on success
* @access public
*/
function displayParsedTemplate($name = null, $applyFilters = true)
{
$result = $this->getParsedTemplate($name, $applyFilters);
/**
* error happened
*/
if (patErrorManager::isError($result)) {
return $result;
}
echo $result;
return true;
}
/**
* parse a template and push the result into a variable of any other
* template
*
* If the template already has been parsed, it will just be pushed into the variable.
* If the template has not been loaded, it will be loaded.
*
* @access public
* @param string name of the template
* @return string Content of the parsed template
* @param boolean if set to true, the value will be appended to the value already stored.
* @see getParsedTemplate()
* @see addVar()
*/
function parseIntoVar( $srcTmpl, $destTmpl, $var, $append = false )
{
$srcTmpl = strtolower( $srcTmpl );
$destTmpl = strtolower( $destTmpl );
$var = strtoupper($var);
$result = $this->parseTemplate( $srcTmpl );
if( patErrorManager::isError( $result ) )
return $result;
if( $append !== true || !isset( $this->_vars[$destTmpl]['scalar'][$var] ) )
$this->_vars[$destTmpl]['scalar'][$var] = '';
$this->_vars[$destTmpl]['scalar'][$var] .= $this->_templates[$srcTmpl]['result'];
return true;
}
/**
* clears a parsed Template
*
* Parsed Content, variables and the loop attribute are cleared
*
* If you will not be using this template anymore, then you should
* call freeTemplate()
*
* @access public
* @param string name of the template
* @param boolean set this to true to clear all child templates, too
* @see clearAllTemplates()
* @see freeTemplate()
*/
function clearTemplate( $name, $recursive = false )
{
$name = strtolower( $name );
$this->_templates[$name]['parsed'] = false;
$this->_templates[$name]['work'] = '';
$this->_templates[$name]['iteration'] = 0;
$this->_templates[$name]['result'] = '';
$this->_vars[$name] = array(
'scalar' => array(),
'rows' => array()
);
if (!empty($this->_templates[$name]['defaultVars'])) {
foreach ($this->_templates[$name]['defaultVars'] as $varname => $value) {
$this->addVar($name, $varname, $value);
}
}
/**
* clear child templates as well
*/
if( $recursive === true )
{
$deps = $this->_getDependencies( $name );
foreach( $deps as $dep )
{
$this->clearTemplate( $dep, true );
}
}
return true;
}
/**
* clears all templates
*
* @access public
* @uses clearTemplate()
*/
function clearAllTemplates()
{
$templates = array_keys( $this->_templates );
$cnt = count( $templates );
for( $i = 0; $i < $cnt; $i++ )
{
$this->clearTemplate( $templates[$i] );
}
return true;
}
/**
* frees a template
*
* All memory consumed by the template
* will be freed.
*
* @access public
* @param string name of the template
* @param boolean clear dependencies of the template
* @see freeAllTemplates()
*/
function freeTemplate( $name, $recursive = false )
{
$name = strtolower( $name );
$key = array_search( $name, $this->_templateList );
if( $key === false )
{
return patErrorManager::raiseWarning(
PATTEMPLATE_WARNING_NO_TEMPLATE,
"Template '$name' does not exist."
);
}
unset( $this->_templateList[$key] );
$this->_templateList = array_values( $this->_templateList );
/**
* free child templates as well
*/
if( $recursive === true )
{
$deps = $this->_getDependencies( $name );
foreach( $deps as $dep )
{
$this->freeTemplate( $dep, true );
}
}
unset( $this->_templates[$name] );
unset( $this->_vars[$name] );
if (isset($this->_discoveredPlaceholders[$name])) {
unset($this->_discoveredPlaceholders[$name]);
}
return true;
}
/**
* frees all templates
*
* All memory consumed by the templates
* will be freed.
*
* @access public
* @see freeTemplate()
*/
function freeAllTemplates()
{
$this->_templates = array();
$this->_vars = array();
$this->_templateList = array();
}
/**
* get _all_ dependencies of a template,
* regardless of the subtemplates
*
* @access private
* @param string template name
* @return array list of all subtemplates
*/
function _getDependencies( $template )
{
$deps = array();
if( isset( $this->_templates[$template]['dependencies'] ) )
$deps = $this->_templates[$template]['dependencies'];
if( isset( $this->_templates[$template]['subtemplates'] ) )
{
foreach( $this->_templates[$template]['subtemplates'] as $sub )
{
if( isset( $sub['dependencies'] ) )
$deps = array_merge( $deps, $sub['dependencies'] );
}
}
$deps = array_unique( $deps );
return $deps;
}
/**
* Displays useful information about all or named templates
*
* This method breaks BC, as it now awaits an array instead of
* unlimited parameters.
*
* @param mixed array of templates that should be dumped, or null if you
* want all templates to be dumped
* @param string dumper
* @access public
*/
function dump( $restrict = null, $dumper = 'Html' )
{
if( is_string( $restrict ) )
$restrict = array( $restrict );
$dumper = &$this->loadModule( 'Dump', $dumper );
if( patErrorManager::isError( $dumper ) )
{
return $dumper;
}
if( is_null( $restrict ) )
{
$templates = $this->_templates;
$vars = $this->_vars;
}
else
{
$restrict = array_map( 'strtolower', $restrict );
$templates = array();
$vars = array();
foreach( $this->_templates as $name => $spec )
{
if( !in_array( $name, $restrict ) )
continue;
$templates[$name] = $spec;
$vars[$name] = $this->_vars[$name];
}
}
$dumper->displayHeader();
$dumper->dumpGlobals( $this->_globals );
$dumper->dumpTemplates( $templates, $vars );
$dumper->displayFooter();
return true;
}
/**
* get the include path
*
* @access public
* @return string
*/
function getIncludePath()
{
return PATTEMPLATE_INCLUDE_PATH;
}
/**
* apply input filters that have been set
*
* This is being called by the readers.
*
* @access public
* @param string template
* @return string filtered templeta
*/
function applyInputFilters( $template )
{
$cnt = count( $this->_inputFilters );
for( $i = 0; $i < $cnt; $i++ )
{
$template = $this->_inputFilters[$i]->apply( $template );
}
return $template;
}
/**
* checks, whether a placeholder exists in a template
*
* @access public
* @param string name of the placeholder
* @param string name of the template
* @param boolean whether to use the cached result of a previous call
*/
function placeholderExists($placeholder, $tmpl, $cached = true)
{
$tmpl = strtolower($tmpl);
$placeholder = strtoupper($placeholder);
if (!$this->exists($tmpl)) {
return false;
}
if ($cached === true) {
if (isset($this->_discoveredPlaceholders[$tmpl]) && isset($this->_discoveredPlaceholders[$tmpl][$placeholder])) {
return $this->_discoveredPlaceholders[$tmpl][$placeholder];
}
}
if (isset($this->_templates[$tmpl]['subtemplates'])) {
$content = '';
foreach ($this->_templates[$tmpl]['subtemplates'] as $temp) {
if (!isset($temp['data'])) {
continue;
}
$content .= $temp['data'];
}
} else {
$content = $this->_templates[$tmpl]['content'];
}
$search = $this->_startTag . $placeholder . $this->_endTag;
if (strstr($content, $search) !== false) {
$this->_discoveredPlaceholders[$tmpl][$placeholder] = true;
return true;
}
$this->_discoveredPlaceholders[$tmpl][$placeholder] = false;
return false;
}
/**
* Convert the template to its string representation.
*
* This method allows you to just echo the patTemplate
* object in order to display the template.
*
* Requires PHP5
*
* <code>
* $tmpl = new patTemplate();
* $tmpl->readTemplatesFromFile( 'myfile.tmpl' );
* echo $tmpl;
* </code>
*
* @access private
* @return string
*/
function __toString()
{
return $this->getParsedTemplate();
}
}
?>
|