DTUI_201105 /library/XenForo/Model/StyleProperty.php

Language PHP Lines 3183
MD5 Hash a0b35b7e7a56d16fa984fa521a9ea90f
Repository https://github.com/hanguyenhuu/DTUI_201105.git View Raw File View Project SPDX
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
<?php

/**
 * Model for style properties and style property definitions.
 * Note that throughout, style ID -1 is the ACP master.
 *
 * @package XenForo_StyleProperty
 */
class XenForo_Model_StyleProperty extends XenForo_Model
{
	protected static $_tempProperties = null;

	/**
	 * Gets the specified style property by its ID. Includes
	 * definition info.
	 *
	 * @param integer $id
	 *
	 * @return array|false
	 */
	public function getStylePropertyById($id)
	{
		return $this->_getDb()->fetchRow('
			SELECT property_definition.*,
				style_property.*
			FROM xf_style_property AS style_property
			INNER JOIN xf_style_property_definition AS property_definition ON
				(property_definition.property_definition_id = style_property.property_definition_id)
			WHERE style_property.property_id = ?
		', $id);
	}

	/**
	 * Gets the specified style property by its definition ID
	 * and style ID. Includes definition info.
	 *
	 * @param integer $definitionId
	 * @param integer $styleId
	 *
	 * @return array|false
	 */
	public function getStylePropertyByDefinitionAndStyle($definitionId, $styleId)
	{
		return $this->_getDb()->fetchRow('
			SELECT property_definition.*,
				style_property.*
			FROM xf_style_property AS style_property
			INNER JOIN xf_style_property_definition AS property_definition ON
				(property_definition.property_definition_id = style_property.property_definition_id)
			WHERE style_property.property_definition_id = ?
				AND style_property.style_id = ?
		', array($definitionId, $styleId));
	}

	/**
	 * Gets all style properties in the specified styles. This only
	 * includes properties that have been customized or initially defined
	 * in the specified styles. Includes definition info.
	 *
	 * @param array $styleIds
	 *
	 * @return array Format: [property id] => info
	 */
	public function getStylePropertiesInStyles(array $styleIds)
	{
		if (!$styleIds)
		{
			return array();
		}

		return $this->fetchAllKeyed('
			SELECT property_definition.*,
				style_property.*
			FROM xf_style_property AS style_property
			INNER JOIN xf_style_property_definition AS property_definition ON
				(property_definition.property_definition_id = style_property.property_definition_id)
			WHERE style_property.style_id IN (' . $this->_getDb()->quote($styleIds) . ')
			ORDER BY property_definition.display_order
		', 'property_id');
	}

	/**
	 * Gets all style properties in a style with the specified definition IDs
	 * that have been customized or defined directly in the style.
	 *
	 * @param integer $styleId
	 * @param array $definitionIds
	 *
	 * @return array Format: [definition id] => info
	 */
	public function getStylePropertiesInStyleByDefinitions($styleId, array $definitionIds)
	{
		if (!$definitionIds)
		{
			return array();
		}

		return $this->fetchAllKeyed('
			SELECT property_definition.*,
				style_property.*
			FROM xf_style_property AS style_property
			INNER JOIN xf_style_property_definition AS property_definition ON
				(property_definition.property_definition_id = style_property.property_definition_id)
			WHERE style_property.style_id = ?
				AND style_property.definition_id IN (' . $this->_getDb()->quote($definitionIds) . ')
			ORDER BY property_definition.display_order
		', 'property_definition_id', $styleId);
	}

	/**
	 * Gets the effective style properties in a style. This includes properties
	 * that have been customized/created in a parent style.
	 *
	 * Includes effectiveState key in each property (customized, inherited, default).
	 *
	 * @param integer $styleId
	 * @param array|null $path Path from style to root (earlier positions closer to style); if null, determined automatically
	 * @param array|null $properties List of properties in this style and all parent styles; if null, determined automatically
	 *
	 * @return array Format: [definition id] => info
	 */
	public function getEffectiveStylePropertiesInStyle($styleId, array $path = null, array $properties = null)
	{
		if ($path === null)
		{
			$path = $this->getParentPathFromStyle($styleId);
		}
		if ($properties === null)
		{
			$properties = $this->getStylePropertiesInStyles($path);
		}

		$effective = array();
		$propertyPriorities = array();
		foreach ($properties AS $property)
		{
			$definitionId = $property['property_definition_id'];
			$propertyPriority = array_search($property['style_id'], $path);

			if (!isset($propertyPriorities[$definitionId]) || $propertyPriority < $propertyPriorities[$definitionId])
			{
				switch ($property['style_id'])
				{
					case $property['definition_style_id']:
						$property['effectiveState'] = 'default';
						break;

					case $styleId:
						$property['effectiveState'] = 'customized';
						break;

					default:
						$property['effectiveState'] = 'inherited';
						break;
				}

				$effective[$definitionId] = $property;
				$propertyPriorities[$definitionId] = $propertyPriority;
			}
		}

		return $effective;
	}

	/**
	 * Gets the effective properties and groups in a style. Properties are organized
	 * within the groups (in properties key).
	 *
	 * @param integer $styleId
	 * @param array|null $path Path from style to root (earlier positions closer to style); if null, determined automatically
	 * @param array|null $properties List of properties in this style and all parent styles; if null, determined automatically
	 *
	 * @return array Format: [group name] => group info, with [properties][definition id] => property info
	 */
	public function getEffectiveStylePropertiesByGroup($styleId, array $path = null, array $properties = null)
	{
		if ($path === null)
		{
			$path = $this->getParentPathFromStyle($styleId);
		}

		if ($properties === null)
		{
			$properties = $this->getEffectiveStylePropertiesInStyle($styleId, $path, $properties);
		}

		$groups = $this->getEffectiveStylePropertyGroupsInStyle($styleId, $path);

		$invalidGroupings = array();
		foreach ($properties AS $definitionId => $property)
		{
			if (isset($groups[$property['group_name']]))
			{
				$groups[$property['group_name']]['properties'][$definitionId] = $property;
			}
			else
			{
				$invalidGroupings[$definitionId] = $property;
			}
		}

		if ($invalidGroupings)
		{
			$groups[''] = array(
				'property_group_id' => 0,
				'group_name' => '',
				'group_style_id' => $styleId,
				'title' => '(ungrouped)',
				'description' => '',
				'addon_id' => '',
				'properties' => $invalidGroupings
			);
		}

		return $groups;
	}

	/**
	 * Fetches all color palette properties for the specified style.
	 *
	 * @param integer $styleId
	 * @param array|null $path Path from style to root (earlier positions closer to style); if null, determined automatically
	 * @param array|null $properties List of properties in this style and all parent styles; if null, determined automatically
	 *
	 * @return array
	 */
	public function getColorPalettePropertiesInStyle($styleId, array $path = null, array $properties = null)
	{
		$groups = $this->getEffectiveStylePropertiesByGroup($styleId, $path, $properties);

		return $groups['color']['properties'];
	}

	/**
	 * Reorganizes a property list to key properties by name. This is only safe
	 * to do when getting properties (effective or not) for a single style.
	 *
	 * @param array $properties
	 *
	 * @return array
	 */
	public function keyPropertiesByName(array $properties)
	{
		$output = array();
		foreach ($properties AS $property)
		{
			$output[$property['property_name']] = $property;
		}

		return $output;
	}

	/**
	 * Filters a list of properties into 2 groups: scalar properties and css properties.
	 *
	 * @param array [scalar props, css props]
	 */
	public function filterPropertiesByType(array $properties)
	{
		$scalar = array();
		$css = array();

		foreach ($properties AS $key => $property)
		{
			if ($property['property_type'] == 'scalar')
			{
				$scalar[$key] = $property;
			}
			else
			{
				$css[$key] = $property;
			}
		}

		return array($scalar, $css);
	}

	/**
	 * Gets the specified style property group.
	 *
	 * @param integer $groupId
	 *
	 * @return array|false
	 */
	public function getStylePropertyGroupById($groupId)
	{
		return $this->_getDb()->fetchRow('
			SELECT *
			FROM xf_style_property_group
			WHERE property_group_id = ?
		', $groupId);
	}

	/**
	 * Gets all style property groups defined in the specified styles.
	 *
	 * @param array $styleIds
	 *
	 * @return array Format: [property group id] => info
	 */
	public function getStylePropertyGroupsInStyles(array $styleIds)
	{
		if (!$styleIds)
		{
			return array();
		}

		return $this->fetchAllKeyed('
			SELECT *
			FROM xf_style_property_group
			WHERE group_style_id IN (' . $this->_getDb()->quote($styleIds) . ')
			ORDER BY display_order
		', 'property_group_id');
	}

	/**
	 * Gets the effective list of groups that apply to a style.
	 *
	 * @param integer $styleId
	 * @param array|null $path Path from style to root (earlier positions closer to style); if null, determined automatically
	 *
	 * @return array Format: [group name] => info
	 */
	public function getEffectiveStylePropertyGroupsInStyle($styleId, array $path = null)
	{
		if ($path === null)
		{
			$path = $this->getParentPathFromStyle($styleId);
		}

		$groups = $this->getStylePropertyGroupsInStyles($path);
		$output = array();
		foreach ($groups AS $group)
		{
			$output[$group['group_name']] = $group;
		}

		return $output;
	}

	/**
	 * Gets name-value pairs of style property groups in a style.
	 *
	 * @param integer $styleId
	 *
	 * @return array Format: [name] => title
	 */
	public function getStylePropertyGroupOptions($styleId)
	{
		$groups = $this->prepareStylePropertyGroups($this->getEffectiveStylePropertyGroupsInStyle($styleId));
		$output = array();
		foreach ($groups AS $group)
		{
			$output[$group['group_name']] = $group['title'];
		}

		return $output;
	}

	/**
	 * Prepares a style property group for display. If properties are found
	 * within, they will be automatically prepared.
	 *
	 * @param array $property
	 * @param integer|null $displayStyleId The ID of the style the groups/properties are being edited in
	 *
	 * @return array Prepared version
	 */
	public function prepareStylePropertyGroup(array $group, $displayStyleId = null)
	{
		$group['masterTitle'] = $group['title'];
		$group['masterDescription'] = $group['description'];
		if ($group['addon_id'])
		{
			$group['title'] = new XenForo_Phrase($this->getStylePropertyGroupTitlePhraseName($group));
			$group['description'] = new XenForo_Phrase($this->getStylePropertyGroupDescriptionPhraseName($group));
		}

		if (!$group['group_name'])
		{
			$group['canEdit'] = false;
		}
		else if ($displayStyleId === null)
		{
			$group['canEdit'] = $this->canEditStylePropertyDefinition($group['group_style_id']);
		}
		else
		{
			$group['canEdit'] = (
				$this->canEditStylePropertyDefinition($group['group_style_id'])
				&& $group['group_style_id'] == $displayStyleId
			);
		}

		if (!empty($group['properties']))
		{
			$group['properties'] = $this->prepareStyleProperties($group['properties'], $displayStyleId);
		}

		return $group;
	}

	/**
	 * Prepares a list of style property groups. If properties are found within,
	 * they will be automatically prepared.
	 *
	 * @param array $groups
	 * @param integer|null $displayStyleId The ID of the style the groups/properties are being edited in
	 *
	 * @return array
	 */
	public function prepareStylePropertyGroups(array $groups, $displayStyleId = null)
	{
		foreach ($groups AS &$group)
		{
			$group = $this->prepareStylePropertyGroup($group, $displayStyleId);
		}

		return $groups;
	}

	/**
	 * Gets the default style property group record.
	 *
	 * @param integer $styleId
	 *
	 * @return array
	 */
	public function getDefaultStylePropertyGroup($styleId)
	{
		return array(
			'group_name' => '',
			'group_style_id' => $styleId,
			'title' => '',
			'display_order' => 1,
			'sub_group' => '',
			'addon_id' => null,

			'masterTitle' => '',
			'masterDescription' => '',
		);
	}

	/**
	 * Gets the name of the style property group title phrase.
	 *
	 * @param array $group
	 *
	 * @return string
	 */
	public function getStylePropertyGroupTitlePhraseName(array $group)
	{
		switch ($group['group_style_id'])
		{
			case -1: $suffix = 'admin'; break;
			case 0:  $suffix = 'master'; break;
			default: return '';
		}

		return "style_property_group_$group[group_name]_$suffix";
	}

	/**
	 * Gets the name of the style property group description phrase.
	 *
	 * @param array $group
	 *
	 * @return string
	 */
	public function getStylePropertyGroupDescriptionPhraseName(array $group)
	{
		switch ($group['group_style_id'])
		{
			case -1: $suffix = 'admin'; break;
			case 0:  $suffix = 'master'; break;
			default: return '';
		}

		return "style_property_group_$group[group_name]_{$suffix}_desc";
	}

	/**
	 * Gets the parent path from the specified style. For real styles,
	 * this is the parent list. However, this function can handle styles
	 * 0 (master) and -1 (ACP).
	 *
	 * @param $styleId
	 * @return array Parent list; earlier positions are more specific
	 */
	public function getParentPathFromStyle($styleId)
	{
		switch (intval($styleId))
		{
			case 0: return array(0);
			case -1: return array(-1, 0);

			default:
				$style = $this->_getStyleModel()->getStyleById($styleId);
				if ($style)
				{
					return explode(',', $style['parent_list']);
				}
				else
				{
					return array();
				}
		}
	}

	/**
	 * Gets style info in the style property-specific way.
	 *
	 * @param integer $styleId
	 *
	 * @return array|false
	 */
	public function getStyle($styleId)
	{
		if ($styleId >= 0)
		{
			return $this->getModelFromCache('XenForo_Model_Style')->getStyleById($styleId, true);
		}
		else
		{
			return array(
				'style_id' => -1,
				'parent_list' => '-1,0',
				'title' => new XenForo_Phrase('admin_control_panel')
			);
		}
	}

	/**
	 * Group a list of style properties by the style they belong to.
	 * This uses the customization style (not definition style) for grouping.
	 *
	 * @param array $properties
	 *
	 * @return array Format: [style id][definition id] => info
	 */
	public function groupStylePropertiesByStyle(array $properties)
	{
		$newProperties = array();
		foreach ($properties AS $property)
		{
			$newProperties[$property['style_id']][$property['property_definition_id']] = $property;
		}

		return $newProperties;
	}

	/**
	 * Rebuilds the property cache for all styles.
	 */
	public function rebuildPropertyCacheForAllStyles()
	{
		$this->rebuildPropertyCacheInStyleAndChildren(0);
	}

	/**
	 * Rebuild the style property cache in the specified style and all
	 * child/dependent styles.
	 *
	 * @param integer $styleId
	 *
	 * @return array The property cache for the requested style
	 */
	public function rebuildPropertyCacheInStyleAndChildren($styleId)
	{
		if ($styleId == -1)
		{
			$rebuildStyleIds = array(-1);
			$dataStyleIds = array(-1, 0);
			$styles = array();
		}
		else
		{
			$styleModel = $this->_getStyleModel();

			$styles = $styleModel->getAllStyles();
			$styleTree = $styleModel->getStyleTreeAssociations($styles);

			$rebuildStyleIds = $styleModel->getAllChildStyleIdsFromTree($styleId, $styleTree);
			$rebuildStyleIds[] = $styleId;

			$dataStyleIds = array_keys($styles);
			$dataStyleIds[] = 0;

			if ($styleId == 0)
			{
				$rebuildStyleIds[] = -1;
				$dataStyleIds[] = -1;
			}
		}

		$properties = $this->groupStylePropertiesByStyle(
			$this->getStylePropertiesInStyles($dataStyleIds)
		);

		$styleOutput = false;

		foreach ($rebuildStyleIds AS $rebuildStyleId)
		{
			$sourceStyle = (isset($styles[$rebuildStyleId]) ? $styles[$rebuildStyleId] : array());

			switch ($rebuildStyleId)
			{
				case 0:
					continue 2;

				case -1:
					$sourceStyleIds = array(-1, 0);
					break;

				default:
					$sourceStyleIds = explode(',', $sourceStyle['parent_list']);
			}

			$styleProperties = array();
			foreach ($sourceStyleIds AS $sourceStyleId)
			{
				if (isset($properties[$sourceStyleId]))
				{
					$styleProperties = array_merge($styleProperties, $properties[$sourceStyleId]);
				}
			}

			$effectiveProperties = $this->getEffectiveStylePropertiesInStyle(
				$rebuildStyleId, $sourceStyleIds, $styleProperties
			);

			$cacheOutput = $this->updatePropertyCacheInStyle($rebuildStyleId, $effectiveProperties, $sourceStyle);
			if ($rebuildStyleId == $styleId)
			{
				$styleOutput = $cacheOutput;
			}
		}

		return $styleOutput;
	}

	/**
	 * Updates the property cache in the specified style.
	 *
	 * @param integer $styleId
	 * @param array $effectiveProperties List of effective properties in style.
	 * @param array|null $style Style information; queried if needed
	 *
	 * @return array|false Compiled property cache
	 */
	public function updatePropertyCacheInStyle($styleId, array $effectiveProperties, array $style = null)
	{
		if ($styleId == 0)
		{
			return false;
		}

		$propertyCache = array();
		foreach ($effectiveProperties AS $property)
		{
			if ($property['property_type'] == 'scalar')
			{
				$propertyCache[$property['property_name']] = $property['property_value'];
			}
			else
			{
				$propertyCache[$property['property_name']] = unserialize($property['property_value']);
			}
		}
		foreach ($propertyCache AS &$propertyValue)
		{
			if (is_array($propertyValue))
			{
				$propertyValue = $this->compileCssPropertyForCache($propertyValue, $propertyCache);
			}
			else
			{
				$propertyValue = $this->compileScalarPropertyForCache($propertyValue, $propertyCache);
			}
		}

		if ($styleId == -1)
		{
			$this->_getDataRegistryModel()->set('adminStyleModifiedDate', XenForo_Application::$time);
			$this->_getDataRegistryModel()->set('adminStyleProperties', $propertyCache);
		}
		else if ($styleId > 0)
		{
			$dw = XenForo_DataWriter::create('XenForo_DataWriter_Style');
			if ($style)
			{
				$dw->setExistingData($style, true);
			}
			else
			{
				$dw->setExistingData($styleId);
			}
			$dw->set('properties', $propertyCache);
			$dw->save();
		}

		return $propertyCache;
	}

	/**
	 * Compiles a CSS property from it's user-input-based version to the property cache version.
	 *
	 * @param array $original Original, input-based CSS rule
	 * @param array $properties A list of all properties, for resolving variable style references
	 *
	 * @return array CSS rule for cache
	 */
	public function compileCssPropertyForCache(array $original, array $properties = array())
	{
		$output = $original;
		$output = $this->compileCssProperty_sanitize($output, $original);

		foreach ($output AS &$outputValue)
		{
			$outputValue = $this->replaceVariablesInStylePropertyValue($outputValue, $properties);
		}

		$output = $this->compileCssProperty_compileRules($output, $original);
		$output = $this->compileCssProperty_cleanUp($output, $original);

		return $output;
	}

	/**
	 * Sanitizes the values in the CSS property output array.
	 *
	 * @param array $output Output CSS property
	 * @param array $original Original format of CSS property
	 *
	 * @return array Updated output CSS property
	 */
	public function compileCssProperty_sanitize(array $output, array $original)
	{
		// remove empty properties so isset can be used (0 is a valid value in many places)
		foreach ($output AS $key => &$value)
		{
			if (is_array($value))
			{
				if (count($value) == 0)
				{
					unset($output[$key]);
				}
			}
			else if (trim($value) === '')
			{
				unset($output[$key]);
			}
			else if ($value !== '0' && strval(intval($value)) === $value)
			{
				// not 0 and looks like an int, add "px" unit
				$value = $value . 'px';
			}
		}

		// translate array-based text decoration to css style
		if (!empty($output['text-decoration']) && is_array($output['text-decoration']))
		{
			if (isset($output['text-decoration']['none']))
			{
				$output['text-decoration'] = 'none';
			}
			else
			{
				$output['text-decoration'] = implode(' ', $output['text-decoration']);
			}
		}

		return $output;
	}

	/**
	 * Compiles all the rules of a CSS property.
	 *
	 * @param array $output Output CSS property
	 * @param array $original Original format of CSS property
	 *
	 * @return array Updated output CSS property
	 */
	public function compileCssProperty_compileRules(array $output, array $original)
	{
		// handle the font short cut (includes all text-related rules)
		if (false && isset($output['font-size'], $output['font-family']))
		{
			// font shortcut now disabled on account of the line-height issue
			$output['font'] = 'font: ' . $this->_getCssValue(
				$output, array('font-style', 'font-variant', 'font-weight', 'font-size', 'font-family')
			) . ';';
		}
		else
		{
			$output['font'] = $this->_getCssValueRule(
				$output, array('font-style', 'font-variant', 'font-weight', 'font-size', 'font-family')
			);
		}

		$output['font'] .= "\n" . $this->_getCssValueRule(
			$output, array('color', 'text-decoration')
		);

		// background shortcut
		if (isset($output['background-image']) && $output['background-image'] != 'none')
		{
			$output['background-image'] = trim($output['background-image']);
			if (!preg_match('#^url\s*\(#', $output['background-image']))
			{
				$output['background-image'] = preg_replace('/^("|\')(.*)\\1$/', '\\2', $output['background-image']);
				$output['background-image'] = 'url(\'' . $output['background-image'] . '\')';
			}
		}

		if (!empty($output['background-none']))
		{
			$output['background'] = 'background: none;';
			$output['background-color'] = 'none';
			$output['background-image'] = 'none';
		}
		else if ( // force the background shortcut if a color + image is specified, OR if color = rgba
			isset($output['background-color'], $output['background-image'])
			||
			(isset($output['background-color']) && substr(strtolower($output['background-color']), 0, 4) == 'rgba')
		)
		{
			$output['background'] = 'background: ' . $this->_getCssValue(
				$output, array('background-color', 'background-image', 'background-repeat', 'background-position')
			) . ';';
		}
		else
		{
			$output['background'] = $this->_getCssValueRule($output,
				array('background-color', 'background-image', 'background-repeat', 'background-position')
			);
		}

		// padding, margin shortcuts
		$this->_getPaddingMarginShortCuts('padding', $output);
		$this->_getPaddingMarginShortCuts('margin', $output);

		// border shortcut
		if (isset($output['border-width'], $output['border-style'], $output['border-color']))
		{
			$output['border'] = 'border: ' . $this->_getCssValue(
				$output, array('border-width', 'border-style', 'border-color')
			) . ';';
		}
		else
		{
			$output['border'] = $this->_getCssValueRule(
				$output, array('border-width', 'border-style', 'border-color')
			);
		}

		foreach (array('top', 'right', 'bottom', 'left') AS $borderSide)
		{
			$borderSideName = "border-$borderSide";

			if (isset($output["$borderSideName-width"], $output["$borderSideName-style"], $output["$borderSideName-color"]))
			{
				$borderSideCss = $borderSideName . ': ' . $this->_getCssValue(
					$output, array("$borderSideName-width", "$borderSideName-style", "$borderSideName-color")
				) . ';';
			}
			else
			{
				$borderSideCss = $this->_getCssValueRule(
					$output, array("$borderSideName-width", "$borderSideName-style", "$borderSideName-color")
				);
			}

			if ($borderSideCss)
			{
				$output['border'] .= "\n" . $borderSideCss;
			}
		}

		// border radius shortcut, ties into border
		if (isset($output['border-radius']))
		{
			$output['border'] .= "\nborder-radius: " . $output['border-radius'] . ';';
		}

		foreach (array('top-left', 'top-right', 'bottom-right', 'bottom-left') AS $radiusCorner)
		{
			$radiusCornerName = "border-$radiusCorner-radius";
			if (isset($output[$radiusCornerName]))
			{
				$output['border'] .= "\n$radiusCornerName: " . $output[$radiusCornerName] . ';';
			}
		}

		return $output;
	}

	protected function _getPaddingMarginShortCuts($type, array &$output)
	{
		$test = $output;

		// push all the values into the test array for purposes of determining how to build the short cut
		if (isset($output[$type . '-all']))
		{
			foreach (array('top', 'left', 'bottom', 'right') AS $side)
			{
				if (!isset($output["{$type}-{$side}"]))
				{
					$test["{$type}-{$side}"] = $output[$type . '-all'];
				}
			}
		}

		if (isset($test[$type . '-top'], $test[$type . '-right'], $test[$type . '-bottom'], $test[$type . '-left']))
		{
			if ($test[$type . '-top'] == $test[$type . '-right']
				&& $test[$type . '-top'] == $test[$type . '-bottom']
				&& $test[$type . '-top'] == $test[$type . '-left'])
			{
				$output[$type] = $type . ': ' . $test[$type . '-top'] . ';';
			}
			else if ($test[$type . '-top'] == $test[$type . '-bottom'] && $test[$type . '-right'] == $test[$type . '-left'])
			{
				$output[$type] = $type . ': ' . $this->_getCssValue(
					$test, array($type . '-top', $type . '-right')
				) . ';';
			}
			else if ($test[$type . '-right'] == $test[$type . '-left'])
			{
				$output[$type] = $type . ': ' . $this->_getCssValue(
					$test, array($type . '-top', $type . '-right', $type . '-bottom')
				) . ';';
			}
			else
			{
				$output[$type] = $type . ': ' . $this->_getCssValue(
					$test, array($type . '-top', $type . '-right', $type . '-bottom', $type . '-left')
				) . ';';
			}
		}
		else
		{
			$output[$type] = $this->_getCssValueRule(
				$test, array($type . '-top', $type . '-right', $type . '-bottom', $type . '-left')
			);
		}
	}

	/**
	 * Cleans up the CSS property output after compilation.
	 *
	 * @param array $output Output CSS property
	 * @param array $original Original format of CSS property
	 *
	 * @return array Updated output CSS property
	 */
	public function compileCssProperty_cleanUp(array $output, array $original)
	{
		foreach ($output AS $key => &$value)
		{
			if (preg_match('/^(
				background-(none|image|position|repeat)
				|font-(variant|weight|style)
				|text-decoration
				|border-style
				|border-(left|right|top|bottom)-style
			)/x', $key))
			{
				unset($output[$key]);
				continue;
			}

			$value = trim($value);
			if ($value === '')
			{
				unset($output[$key]);
				continue;
			}
		}

		return $output;
	}

	/**
	 * Compiles a scalar property value for the cache.
	 *
	 * @param string $original Original property value
	 * @param array $properties A list of all properties, for resolving variable style references
	 *
	 * @return string
	 */
	public function compileScalarPropertyForCache($original, array $properties = array())
	{
		return $this->replaceVariablesInStylePropertyValue($original, $properties);
	}

	/**
	 * Replaces variable references in a style property value.
	 *
	 * @param string $value Property value. This is an individual string value.
	 * @param array $properties List of properites to read from.
	 *
	 * @return string
	 */
	public function replaceVariablesInStylePropertyValue($value, array $properties, array $seenProperties = array())
	{
		if (!$properties)
		{
			return $value;
		}

		$outputValue = $this->convertAtPropertiesToTemplateSyntax($value, $properties);

		$varProperties = array();

		preg_match_all('#\{xen:property\s+("|\'|)([a-z0-9_-]+)(\.([a-z0-9_-]+))?\\1\s*\}#i', $outputValue, $matches, PREG_SET_ORDER);
		foreach ($matches AS $match)
		{
			$varProperties[$match[0]] = (isset($match[4]) ? array($match[2], $match[4]) : array($match[2]));
		}

		foreach ($varProperties AS $matchSearch => $match)
		{
			$matchReplace = '';

			$matchName = implode('.', $match);
			if (!isset($seenProperties[$matchName]))
			{
				$matchProperty = $match[0];
				if (isset($match[1]))
				{
					$matchSubProperty = $match[1];
					if (isset($properties[$matchProperty][$matchSubProperty]) && is_array($properties[$matchProperty]))
					{
						$matchReplace = $properties[$matchProperty][$matchSubProperty];
					}
				}
				else if (isset($properties[$matchProperty]) && !is_array($properties[$matchProperty]))
				{
					$matchReplace = $properties[$matchProperty];
				}
			}

			if ($matchReplace)
			{
				$newSeenProperties = $seenProperties;
				$newSeenProperties[$matchName] = true;

				$matchReplace = $this->replaceVariablesInStylePropertyValue($matchReplace, $properties, $newSeenProperties);
			}

			$outputValue = str_replace($matchSearch, $matchReplace, $outputValue);
		}

		return $outputValue;
	}

	/**
	 * Helper for CSS property cache compilation. Gets the value(s) for one or more
	 * CSS rule keys. Multiple keys will be separated by a space.
	 *
	 * @param array $search Array to search for keys in
	 * @param string|array $key One or more keys to search for
	 *
	 * @return string Values for matching keys; space separated
	 */
	protected function _getCssValue(array $search, $key)
	{
		if (is_array($key))
		{
			$parts = array();
			foreach ($key AS $searchKey)
			{
				if (isset($search[$searchKey]))
				{
					$parts[] = $search[$searchKey];
				}
			}
			return implode(' ', $parts);
		}
		else
		{
			return (isset($search[$key]) ? $search[$key] : '');
		}
	}

	/**
	 * Helper for CSS property cache compilation. Gets the full rule(s) for one or more
	 * CSS rule keys.
	 *
	 * @param array $search Array to search for keys in
	 * @param string|array $key One or more keys to search for
	 *
	 * @return string Full CSS rules
	 */
	protected function _getCssValueRule(array $search, $key)
	{
		if (is_array($key))
		{
			$parts = array();
			foreach ($key AS $searchKey)
			{
				if (isset($search[$searchKey]))
				{
					$parts[] = "$searchKey: " . $search[$searchKey] . ";";
				}
			}
			return implode("\n", $parts);
		}
		else if (isset($search[$key]))
		{
			return "$key: " . $search[$key] . ";";
		}
	}

	/**
	 * Updates the specified style property value.
	 *
	 * @param array $definition Style property definition
	 * @param integer $styleId Style property is being changed in
	 * @param mixed $newValue New value (string for scalar; array for css)
	 * @param boolean $extraOptions Extra options to pass to the data writer
	 * @param mixed $existingProperty If array/false, considered to be the property to be updated; otherwise, determined automatically
	 * @param string $existingValue The existing value in the place of this property. This may
	 * 		come from the parent style (unlike $existingProperty). This prevents customization from
	 * 		occurring when a value isn't changed.
	 *
	 * @param string Returns the property value as it looks going into the DB
	 */
	public function updateStylePropertyValue(array $definition, $styleId, $newValue,
		array $extraOptions = array(), $existingProperty = null, $existingValue = null
	)
	{
		$styleId = intval($styleId);

		if ($existingProperty !== false && !is_array($existingProperty))
		{
			$existingProperty = $this->getStylePropertyByDefinitionAndStyle(
				$definition['property_definition_id'], $styleId
			);
		}

		if ($definition['property_type'] == 'scalar')
		{
			$newValue = strval($newValue);
		}
		else if (!is_array($newValue))
		{
			$newValue = array();
		}

		$dw = XenForo_DataWriter::create('XenForo_DataWriter_StyleProperty');
		$dw->setOption(XenForo_DataWriter_StyleProperty::OPTION_VALUE_FORMAT, $definition['property_type']);
		$dw->setOption(XenForo_DataWriter_StyleProperty::OPTION_VALUE_COMPONENTS, unserialize($definition['css_components']));
		$dw->setOption(XenForo_DataWriter_StyleProperty::OPTION_REBUILD_CACHE, true);
		foreach ($extraOptions AS $option => $optionValue)
		{
			$dw->setOption($option, $optionValue);
		}
		$dw->setExtraData(XenForo_DataWriter_StyleProperty::DATA_DEFINITION, $definition);

		if ($existingProperty)
		{
			$dw->setExistingData($existingProperty, true);
		}
		else
		{
			$dw->set('property_definition_id', $definition['property_definition_id']);
			$dw->set('style_id', $styleId);
		}

		$dw->set('property_value', $newValue);
		$dw->preSave();

		if ($dw->get('property_value') === $existingValue)
		{
			return $dw->get('property_value');
		}

		$dw->save();

		return $dw->get('property_value');
	}

	/**
	 * Saves a set of style property changes from the input format.
	 *
	 * @param integer $styleId Style to change properties in
	 * @param array $properties Properties from input; keyed by definition ID
	 * @param array $reset List of properties to reset if customized; keyed by definition ID
	 */
	public function saveStylePropertiesInStyleFromInput($styleId, array $properties, array $reset = array())
	{
		$existingProperties = $this->getEffectiveStylePropertiesInStyle($styleId);

		XenForo_Db::beginTransaction($this->_getDb());

		foreach ($properties AS $definitionId => $propertyValue)
		{
			if (!isset($existingProperties[$definitionId]))
			{
				continue;
			}

			$propertyDefinition = $existingProperties[$definitionId];
			if ($propertyDefinition['style_id'] == $styleId)
			{
				$existingProperty = $propertyDefinition;
				if (!empty($reset[$definitionId]) && $propertyDefinition['definition_style_id'] != $styleId)
				{
					$dw = XenForo_DataWriter::create('XenForo_DataWriter_StyleProperty');
					$dw->setOption(XenForo_DataWriter_StyleProperty::OPTION_REBUILD_CACHE, false);
					$dw->setExtraData(XenForo_DataWriter_StyleProperty::DATA_DEFINITION, $propertyDefinition);
					$dw->setExistingData($existingProperty, true);
					$dw->delete();
					continue;
				}
			}
			else
			{
				$existingProperty = false;
			}

			$this->updateStylePropertyValue(
				$propertyDefinition, $styleId, $propertyValue,
				array(XenForo_DataWriter_StyleProperty::OPTION_REBUILD_CACHE => false),
				$existingProperty, $propertyDefinition['property_value'] // this is the effective value
			);
		}

		$this->rebuildPropertyCacheInStyleAndChildren($styleId);

		XenForo_Db::commit($this->_getDb());
	}

	/**
	 * Get the specified style property definition by ID. Includes default
	 * property value.
	 *
	 * @param integer $propertyDefinitionId
	 *
	 * @return array|false
	 */
	public function getStylePropertyDefinitionById($propertyDefinitionId)
	{
		return $this->_getDb()->fetchRow('
			SELECT property_definition.*,
				property.property_value
			FROM xf_style_property_definition AS property_definition
			LEFT JOIN xf_style_property AS property ON
				(property.property_definition_id = property_definition.property_definition_id
				AND property.style_id = property_definition.definition_style_id)
			WHERE property_definition.property_definition_id = ?
		', $propertyDefinitionId);
	}

	/**
	 * Gets the specified style property definition by its name and definition
	 * style ID. Includes default property value.
	 *
	 * @param string $name
	 * @param integer $styleId
	 *
	 * @return array|false
	 */
	public function getStylePropertyDefinitionByNameAndStyle($name, $styleId)
	{
		return $this->_getDb()->fetchRow('
			SELECT property_definition.*,
				property.property_value
			FROM xf_style_property_definition AS property_definition
			LEFT JOIN xf_style_property AS property ON
				(property.property_definition_id = property_definition.property_definition_id
				AND property.style_id = property_definition.definition_style_id)
			WHERE property_definition.property_name = ?
				AND property_definition.definition_style_id = ?
		', array($name, $styleId));
	}

	/**
	 * Get the specified style property definitions by their IDs.
	 * Includes default property value.
	 *
	 * @param array $propertyDefinitionIds
	 *
	 * @return array Format: [property definition id] => info
	 */
	public function getStylePropertyDefinitionsByIds(array $propertyDefinitionIds)
	{
		if (!$propertyDefinitionIds)
		{
			return array();
		}

		return $this->fetchAllKeyed('
			SELECT property_definition.*,
				property.property_value
			FROM xf_style_property_definition AS property_definition
			LEFT JOIN xf_style_property AS property ON
				(property.property_definition_id = property_definition.property_definition_id
				AND property.style_id = property_definition.definition_style_id)
			WHERE property_definition.property_definition_id IN (' . $this->_getDb()->quote($propertyDefinitionIds) . ')
		', 'property_definition_id');
	}

	/**
	 * Returns an array of all style property definitions in the specified group
	 *
	 * @param string $groupId
	 * @param integer|null $styleId If specified, limits to definitions in a specified style
	 *
	 * @return array
	 */
	public function getStylePropertyDefinitionsByGroup($groupName, $styleId = null)
	{
		$properties = $this->fetchAllKeyed('
			SELECT *
			FROM xf_style_property_definition
			WHERE group_name = ?
				' . ($styleId !== null ? 'AND definition_style_id = ' . $this->_getDb()->quote($styleId) : '') . '
			ORDER BY display_order
		', 'property_name', $groupName);

		return $properties;
	}

	/**
	 * Create of update a style property definition. Input data
	 * is named after fields in the style property definition, as well as
	 * property_value_scalar and property_value_css.
	 *
	 * @param integer $definitionId Definition to update; if 0, creates a new one
	 * @param array $input List of data from input to change in definition
	 *
	 * @return array Definition info after saving
	 */
	public function createOrUpdateStylePropertyDefinition($definitionId, array $input)
	{
		XenForo_Db::beginTransaction($this->_getDb());

		$dw = XenForo_DataWriter::create('XenForo_DataWriter_StylePropertyDefinition');
		if ($definitionId)
		{
			$dw->setExistingData($definitionId);
		}
		else
		{
			$dw->set('definition_style_id', $input['definition_style_id']);
		}
		$dw->set('group_name', $input['group_name']);
		$dw->set('property_name', $input['property_name']);
		$dw->set('title', $input['title']);
		$dw->set('description', $input['description']);
		$dw->set('property_type', $input['property_type']);
		$dw->set('css_components', $input['css_components']);
		$dw->set('scalar_type', $input['scalar_type']);
		$dw->set('scalar_parameters', $input['scalar_parameters']);
		$dw->set('display_order', $input['display_order']);
		$dw->set('sub_group', $input['sub_group']);
		$dw->set('addon_id', $input['addon_id']);
		$dw->save();

		$definition = $dw->getMergedData();
		if ($input['property_type'] == 'scalar')
		{
			$propertyValue = $input['property_value_scalar'];

			$newPropertyValue = $this->updateStylePropertyValue(
				$definition, $definition['definition_style_id'], $propertyValue
			);
		}
		else
		{
			$propertyValue = $input['property_value_css'];
			if ($definitionId && !$dw->isChanged('property_type'))
			{
				// TODO: update value when possible
			}
			else
			{
				$newPropertyValue = $this->updateStylePropertyValue(
					$definition, $definition['definition_style_id'], array()
				);
			}
		}

		XenForo_Db::commit($this->_getDb());

		return $definition;
	}

	/**
	 * Prepares a style property (or definition) for display.
	 *
	 * @param array $property
	 * @param integer|null $displayStyleId The ID of the style the properties are being edited in
	 *
	 * @return array Prepared version
	 */
	public function prepareStyleProperty(array $property, $displayStyleId = null)
	{
		$property['cssComponents'] = unserialize($property['css_components']);

		if ($property['property_type'] == 'scalar')
		{
			$property['propertyValueScalar'] = $property['property_value'];
			$property['propertyValueCss'] = array();
		}
		else
		{
			$property['propertyValueScalar'] = '';
			$property['propertyValueCss'] = unserialize($property['property_value']);
		}

		$property['masterTitle'] = $property['title'];
		if ($property['addon_id'])
		{
			$property['title'] = new XenForo_Phrase($this->getStylePropertyTitlePhraseName($property));
		}

		$property['masterDescription'] = $property['description'];
		if ($property['addon_id'])
		{
			$property['description'] = new XenForo_Phrase($this->getStylePropertyDescriptionPhraseName($property));
		}

		if ($displayStyleId === null)
		{
			$property['canEditDefinition'] = $this->canEditStylePropertyDefinition($property['definition_style_id']);
		}
		else
		{
			$property['canEditDefinition'] = (
				$this->canEditStylePropertyDefinition($property['definition_style_id'])
				&& $property['definition_style_id'] == $displayStyleId
			);
		}

		$property['canReset'] = (
			isset($property['effectiveState'])
			&& $property['effectiveState'] == 'customized'
		);

		return $property;
	}

	/**
	 * Prepares a list of style properties.
	 *
	 * @param array $properties
	 * @param integer|null $displayStyleId The ID of the style the properties are being edited in
	 *
	 * @return array
	 */
	public function prepareStyleProperties(array $properties, $displayStyleId = null)
	{
		foreach ($properties AS &$property)
		{
			$property = $this->prepareStyleProperty($property, $displayStyleId);
		}

		return $properties;
	}

	/**
	 * Gets the name of the style property title phrase.
	 *
	 * @param array $property
	 *
	 * @return string
	 */
	public function getStylePropertyTitlePhraseName(array $property)
	{
		switch ($property['definition_style_id'])
		{
			case -1: $suffix = 'admin'; break;
			case 0:  $suffix = 'master'; break;
			default: return '';
		}

		return "style_property_$property[property_name]_$suffix";
	}

	/**
	 * Gets the name of the style property description phrase.
	 *
	 * @param array $property
	 *
	 * @return string
	 */
	public function getStylePropertyDescriptionPhraseName(array $property)
	{
		switch ($property['definition_style_id'])
		{
			case -1: $suffix = 'admin'; break;
			case 0:  $suffix = 'master'; break;
			default: return '';
		}

		return "style_property_$property[property_name]_description_$suffix";
	}

	/**
	 * Gets the default style property definition.
	 *
	 * @param integer $styleId
	 * @param string $groupName
	 *
	 * @return array
	 */
	public function getDefaultStylePropertyDefinition($styleId, $groupName = '')
	{
		$components = array(
			'text' => true,
			'background' => true,
			'border' => true,
			'layout' => true,
			'extra' => true
		);

		return array(
			'definition_style_id' => $styleId,
			'group_name' => $groupName,
			'title' => '',
			'property_name' => '',
			'property_type' => 'css',
			'property_value' => '',
			'css_components' => serialize($components),
			'display_order' => 1,
			'sub_group' => '',
			'addon_id' => null,

			'cssComponents' => $components,
			'propertyValueScalar' => '',
			'propertyValueCss' => array(),
			'masterTitle' => ''
		);
	}

	/**
	 * Determines if a style property definition can be edited in
	 * the specified style.
	 *
	 * @param integer $styleId
	 *
	 * @return boolean
	 */
	public function canEditStylePropertyDefinition($styleId)
	{
		return XenForo_Application::debugMode();
	}

	/**
	 * Determines if a style property can be edited in the specified style.
	 *
	 * @param integer $styleId
	 *
	 * @return boolean
	 */
	public function canEditStyleProperty($styleId)
	{
		if ($styleId > 0)
		{
			return true;
		}
		else
		{
			return XenForo_Application::debugMode();
		}
	}

	/**
	 * Get the style property development directory based on the style a property/definition
	 * is defined in. Returns an empty string for non-master properties.
	 *
	 * @param integer $styleId
	 *
	 * @return string
	 */
	public function getStylePropertyDevelopmentDirectory($styleId)
	{
		if ($styleId > 0)
		{
			return '';
		}

		$config = XenForo_Application::get('config');
		if (!$config->debug || !$config->development->directory)
		{
			return '';
		}

		if ($styleId == 0)
		{
			return XenForo_Application::getInstance()->getRootDir()
				. '/' . $config->development->directory . '/file_output/style_properties';
		}
		else
		{
			return XenForo_Application::getInstance()->getRootDir()
				. '/' . $config->development->directory . '/file_output/admin_style_properties';
		}
	}

	/**
	 * Gets the full path to a specific style property development file.
	 * Ensures directory is writable.
	 *
	 * @param string $propertyName
	 * @param integer $styleId
	 *
	 * @return string
	 */
	public function getStylePropertyDevelopmentFileName($propertyName, $styleId)
	{
		$dir = $this->getStylePropertyDevelopmentDirectory($styleId);
		if (!$dir)
		{
			throw new XenForo_Exception('Tried to write non-master/admin style property value to development directory, or debug mode is not enabled');
		}
		if (!is_dir($dir) || !is_writable($dir))
		{
			throw new XenForo_Exception("Style property development directory $dir is not writable");
		}

		return ($dir . '/' . $propertyName . '.xml');
	}

	public function writeAllStylePropertyDevelopmentFiles()
	{
		$definitions = $this->getDefaultStylePropertyDefinition(0);

		Zend_Debug::dump($definitions);
	}

	/**
	 * Writes out a style property development file.
	 *
	 * @param array $definition Property definition
	 * @param array $property Property value
	 */
	public function writeStylePropertyDevelopmentFile(array $definition, array $property)
	{
		if ($definition['addon_id'] != 'XenForo' && $property['style_id'] > 0)
		{
			return;
		}

		$fileName = $this->getStylePropertyDevelopmentFileName($definition['property_name'], $property['style_id']);

		// TODO: in the future, the writing system could be split into writing out definition and values in separate functions.
		// 		it would make a clearer code path.

		$document = new DOMDocument('1.0', 'utf-8');
		$document->formatOutput = true;

		$node = $document->createElement('property');
		$document->appendChild($node);

		$includeDefinition = ($definition['definition_style_id'] == $property['style_id']);
		if ($includeDefinition)
		{
			$node->setAttribute('definition', 1);
			$node->setAttribute('group_name', $definition['group_name']);
			$node->setAttribute('property_type', $definition['property_type']);
			$node->setAttribute('scalar_type', $definition['scalar_type']);
			$node->setAttribute('scalar_parameters', $definition['scalar_parameters']);

			$components = unserialize($definition['css_components']);
			$node->setAttribute('css_components', implode(',', array_keys($components)));

			$node->setAttribute('display_order', $definition['display_order']);
			$node->setAttribute('sub_group', $definition['sub_group']);

			XenForo_Helper_DevelopmentXml::createDomElements($node, array(
				'title' => isset($definition['masterTitle']) ? $definition['masterTitle'] : strval($definition['title']),
				'description' => isset($definition['masterDescription']) ? $definition['masterDescription'] : strval($definition['description'])
			));
		}
		else
		{
			$node->setAttribute('property_type', $definition['property_type']);
		}

		$valueNode = $document->createElement('value');
		if ($definition['property_type'] == 'scalar')
		{
			$valueNode->appendChild($document->createCDATASection($property['property_value']));
		}
		else
		{
			$jsonValue = json_encode(unserialize($property['property_value']));
			// format one value per line
			$jsonValue = preg_replace('/(?<!:|\\\\)","/', '",' . "\n" . '"', $jsonValue);

			$valueNode->appendChild($document->createCDATASection($jsonValue));
		}
		$node->appendChild($valueNode);

		$document->save($fileName);
	}

	/**
	 * Moves a style property development file, for renames.
	 *
	 * @param array $oldDefinition
	 * @param array $newDefinition
	 */
	public function moveStylePropertyDevelopmentFile(array $oldDefinition, array $newDefinition)
	{
		if ($oldDefinition['addon_id'] != 'XenForo' || $oldDefinition['definition_style_id'] > 0)
		{
			return;
		}

		if ($newDefinition['definition_style_id'] > 0)
		{
			$this->deleteStylePropertyDevelopmentFile($oldDefinition['property_name'], $oldDefinition['definition_style_id']);
			return;
		}

		$oldFile = $this->getStylePropertyDevelopmentFileName($oldDefinition['property_name'], $oldDefinition['definition_style_id']);
		$newFile = $this->getStylePropertyDevelopmentFileName($newDefinition['property_name'], $newDefinition['definition_style_id']);

		if (file_exists($oldFile))
		{
			rename($oldFile, $newFile);
		}
	}

	/**
	 * Updates the definition-related elements of the style property development file
	 * without touching the property value.
	 *
	 * @param array $definition
	 */
	public function updateStylePropertyDevelopmentFile(array $definition)
	{
		if ($definition['addon_id'] != 'XenForo' || $definition['definition_style_id'] > 0)
		{
			return;
		}

		$fileName = $this->getStylePropertyDevelopmentFileName($definition['property_name'], $definition['definition_style_id']);

		if (file_exists($fileName))
		{
			$document = new SimpleXMLElement($fileName, 0, true);

			if ((string)$document['definition'])
			{
				$value = (string)$document->value;
				if ((string)$document['property_type'] == 'css')
				{
					$value = serialize(json_decode($value, true));
				}

				$property = $definition + array(
					'style_id' => $definition['definition_style_id'],
					'property_value' => $value
				);

				$this->writeStylePropertyDevelopmentFile($definition, $property);
			}
		}
	}

	/**
	 * Deletes a style property development file.
	 *
	 * @param string $name
	 * @param integer $styleId Definition style ID.
	 */
	public function deleteStylePropertyDevelopmentFile($name, $styleId)
	{
		if ($styleId > 0)
		{
			return;
		}

		$fileName = $this->getStylePropertyDevelopmentFileName($name, $styleId);
		if (file_exists($fileName))
		{
			unlink($fileName);
		}
	}

	/**
	 * Deletes the style property file if needed. This is used when reverting a
	 * customized property; the file is only deleted if we're deleting the property
	 * from a style other than the one it was created in.
	 *
	 * @param array $definition
	 * @param array $property
	 */
	public function deleteStylePropertyDevelopmentFileIfNeeded(array $definition, array $property)
	{
		if ($property['style_id'] > 0)
		{
			return;
		}
		if ($property['style_id'] == $definition['definition_style_id'])
		{
			// assume this will be deleted by the definition
			return;
		}

		$this->deleteStylePropertyDevelopmentFile($definition['property_name'], $property['style_id']);
	}

	/**
	 * Imports style properties/groups from the development location. This only imports
	 * one style's worth of properties at a time.
	 *
	 * @param integer $styleId Style to import for
	 */
	public function importStylePropertiesFromDevelopment($styleId)
	{
		if ($styleId > 0)
		{
			return;
		}

		$dir = $this->getStylePropertyDevelopmentDirectory($styleId);
		if (!$dir)
		{
			return;
		}

		if (!is_dir($dir))
		{
			throw new XenForo_Exception("Style property development directory doesn't exist");
		}

		$files = glob("$dir/*.xml");

		$newProperties = array();
		$newGroups = array();
		foreach ($files AS $fileName)
		{
			$name = basename($fileName, '.xml');
			$document = new SimpleXMLElement($fileName, 0, true);

			if (substr($name, 0, 6) == 'group.')
			{
				$newGroups[] = array(
					'group_name'    => substr($name, 6),
					'title'         => (string)$document->title,
					'description'   => (string)$document->description,
					'display_order' => (string)$document['display_order']
				);
			}
			else
			{
				if ((string)$document['definition'])
				{
					$property = array(
						'title'             => (string)$document->title,
						'description'       => (string)$document->description,
						'definition'        => (string)$document['definition'],
						'group_name'        => (string)$document['group_name'],
						'property_type'     => (string)$document['property_type'],
						'scalar_type'       => (string)$document['scalar_type'],
						'scalar_parameters' => (string)$document['scalar_parameters'],
						'display_order'     => (string)$document['display_order'],
						'sub_group'         => (string)$document['sub_group']
					);

					$components = (string)$document['css_components'];
					if ($components)
					{
						$property['css_components'] = array_fill_keys(explode(',', $components), true);
					}
					else
					{
						$property['css_components'] = array();
					}
				}
				else
				{
					$property = array(
						'property_type' => (string)$document['property_type']
					);
				}

				$property['property_name'] = $name;

				if ($property['property_type'] == 'scalar')
				{
					$property['property_value'] = (string)$document->value;
				}
				else
				{
					$property['property_value'] = json_decode((string)$document->value, true);
				}

				$newProperties[] = $property;
			}
		}

		$this->importStylePropertiesFromArray($newProperties, $newGroups, $styleId, 'XenForo');
	}

	/**
	 * Deletes the style properties and definitions in a style (and possibly limited to an add-on).
	 *
	 * @param integer $styleId Style to delete from. 0 for master, -1 for admin
	 * @param string|null $addOnId If not null, limits deletions to an ad-on
	 * @param boolean $leaveChildCustomizations If true, child customizations of a deleted definition will be left
	 */
	public function deleteStylePropertiesAndDefinitionsInStyle($styleId, $addOnId = null, $leaveChildCustomizations = false)
	{
		$properties = $this->getStylePropertiesInStyles(array($styleId));

		$delPropertyIds = array();
		$delPropertyDefinitionIds = array();
		foreach ($properties AS $property)
		{
			if ($addOnId === null || $property['addon_id'] == $addOnId)
			{
				if ($property['definition_style_id'] == $styleId)
				{
					$delPropertyDefinitionIds[] = $property['property_definition_id'];
				}
				else if ($property['style_id'] == $styleId)
				{
					$delPropertyIds[] = $property['property_id'];
				}
			}
		}

		if ($delPropertyIds)
		{
			$this->_db->delete('xf_style_property',
				'property_id IN (' . $this->_db->quote($delPropertyIds) . ')'
			);
		}

		if ($delPropertyDefinitionIds)
		{
			$this->_db->delete('xf_style_property_definition',
				'property_definition_id IN (' . $this->_db->quote($delPropertyDefinitionIds) . ')'
			);

			if ($leaveChildCustomizations)
			{
				$this->_db->delete('xf_style_property',
					'property_definition_id IN (' . $this->_db->quote($delPropertyDefinitionIds) . ')
					AND style_id = ' . $this->_db->quote($styleId)
				);
			}
			else
			{
				$this->_db->delete('xf_style_property',
					'property_definition_id IN (' . $this->_db->quote($delPropertyDefinitionIds) . ')'
				);
			}
		}
	}

	/**
	 * Delete the style property groups in the specified style, matching the add-on if provided.
	 *
	 * @param integer $styleId Style to delete from. 0 for master, -1 for admin
	 * @param string|null $addOnId If not null, limits deletions to an ad-on
	 */
	public function deleteStylePropertyGroupsInStyle($styleId, $addOnId = null)
	{
		$db = $this->_getDb();
		if ($addOnId === null)
		{
			$db->delete('xf_style_property_group', 'group_style_id = ' . $db->quote($styleId));
		}
		else
		{
			$db->delete('xf_style_property_group',
				'group_style_id = ' . $db->quote($styleId) . ' AND addon_id = ' . $db->quote($addOnId)
			);
		}
	}

	/**
	 * Appends the style property list to an XML document.
	 *
	 * @param DOMElement $rootNode Node to append to
	 * @param integer $styleId Style to read values/definitions from
	 * @param string|null $addOnId If not null, limits to values/definitions in the specified add-on
	 */
	public function appendStylePropertyXml(DOMElement $rootNode, $styleId, $addOnId = null)
	{
		$document = $rootNode->ownerDocument;

		$properties = $this->getStylePropertiesInStyles(array($styleId));
		ksort($properties);

		foreach ($properties AS $property)
		{
			if ($addOnId === null || $property['addon_id'] == $addOnId)
			{
				$node = $document->createElement('property');
				$node->setAttribute('property_name', $property['property_name']);
				$node->setAttribute('property_type', $property['property_type']);

				if ($property['definition_style_id'] == $styleId)
				{
					$node->setAttribute('definition', 1);
					$node->setAttribute('group_name', $property['group_name']);
					$node->setAttribute('title', isset($property['masterTitle']) ? $property['masterTitle'] : strval($property['title']));
					$node->setAttribute('description', isset($property['masterDescription']) ? $property['masterDescription'] : strval($property['description']));

					$components = unserialize($property['css_components']);
					$node->setAttribute('css_components', implode(',', array_keys($components)));
					$node->setAttribute('scalar_type', $property['scalar_type']);
					$node->setAttribute('scalar_parameters', $property['scalar_parameters']);
					$node->setAttribute('display_order', $property['display_order']);
					$node->setAttribute('sub_group', $property['sub_group']);
				}

				if ($property['property_type'] == 'scalar')
				{
					$node->appendChild($document->createCDATASection($property['property_value']));
				}
				else
				{
					$node->appendChild($document->createCDATASection(json_encode(unserialize($property['property_value']))));
				}

				$rootNode->appendChild($node);
			}
		}

		$groups = $this->getStylePropertyGroupsInStyles(array($styleId));
		ksort($groups);
		foreach ($groups AS $group)
		{
			if ($addOnId === null || $group['addon_id'] == $addOnId)
			{
				$node = $document->createElement('group');
				$rootNode->appendChild($node);

				$node->setAttribute('group_name', $group['group_name']);
				$node->setAttribute('display_order', $group['display_order']);
				XenForo_Helper_DevelopmentXml::createDomElements($node, array(
					'title' => $group['title'],
					'description' => $group['description']
				));
			}
		}
	}

	/**
	 * Gets the style property development XML.
	 *
	 * @param integer $styleId
	 *
	 * @return DOMDocument
	 */
	public function getStylePropertyDevelopmentXml($styleId)
	{
		$rootTag = ($styleId == -1 ? 'admin_style_properties' : 'style_properties');

		$document = new DOMDocument('1.0', 'utf-8');
		$document->formatOutput = true;
		$rootNode = $document->createElement($rootTag);
		$document->appendChild($rootNode);

		$this->appendStylePropertyXml($rootNode, $styleId, 'XenForo');

		return $document;
	}

	/**
	 * Imports the development admin navigation XML data.
	 *
	 * @param string $fileName File to read the XML from
	 */
	public function importStylePropertyDevelopmentXml($fileName, $styleId)
	{
		$document = new SimpleXMLElement($fileName, 0, true);
		$this->importStylePropertyXml($document, $styleId, 'XenForo');
	}

	/**
	 * Imports style properties and definitions from XML.
	 *
	 * @param SimpleXMLElement $xml XML node to search within
	 * @param integer $styleId Target style ID
	 * @param string|null $addOnId If not null, target add-on for definitions; if null, add-on is ''
	 */
	public function importStylePropertyXml(SimpleXMLElement $xml, $styleId, $addOnId = null)
	{
		if ($xml->property === null)
		{
			return;
		}

		$newProperties = array();
		foreach ($xml->property AS $xmlProperty)
		{
			$property = array(
				'property_name' => (string)$xmlProperty['property_name'],
				'group_name' => (string)$xmlProperty['group_name'],
				'title' => (string)$xmlProperty['title'],
				'description' => (string)$xmlProperty['description'],
				'definition' => (string)$xmlProperty['definition'],
				'property_type' => (string)$xmlProperty['property_type'],
				'scalar_type' => (string)$xmlProperty['scalar_type'],
				'scalar_parameters' => (string)$xmlProperty['scalar_parameters'],
				'display_order' => (string)$xmlProperty['display_order'],
				'sub_group' => (string)$xmlProperty['sub_group']
			);

			$components = (string)$xmlProperty['css_components'];
			if ($components)
			{
				$components = array_fill_keys(explode(',', $components), true);
				$property['css_components'] = $components;
			}
			else
			{
				$property['css_components'] = array();
			}

			if ($property['property_type'] == 'scalar')
			{
				$property['property_value'] = (string)$xmlProperty;
			}
			else
			{
				$property['property_value'] = json_decode((string)$xmlProperty, true);
			}

			$newProperties[] = $property;
		}

		$newGroups = array();
		foreach ($xml->group AS $xmlGroup)
		{
			$newGroups[] = array(
				'group_name' => (string)$xmlGroup['group_name'],
				'title' => (string)$xmlGroup->title,
				'description' => (string)$xmlGroup->description,
				'display_order' => (string)$xmlGroup['display_order']
			);
		}

		$this->importStylePropertiesFromArray($newProperties, $newGroups, $styleId, $addOnId);
	}

	/**
	 * Imports style properties and definitions from an array.
	 *
	 * @param array $newProperties List of properties and definitions to import
	 * @param array $newGroups List of groups to import
	 * @param integer $styleId Target style ID
	 * @param string|null $addOnId If not null, only replaces properties with this add-on; otherwise, all in style
	 */
	public function importStylePropertiesFromArray(array $newProperties, array $newGroups, $styleId, $addOnId = null)
	{
		// must be run before delete to keep values accessible
		$existingProperties = $this->keyPropertiesByName($this->getEffectiveStylePropertiesInStyle($styleId));

		$addOnIdString = ($addOnId !== null ? $addOnId : '');
		$db = $this->_getDb();

		XenForo_Db::beginTransaction($db);
		$this->deleteStylePropertiesAndDefinitionsInStyle($styleId, $addOnId, true);
		$this->deleteStylePropertyGroupsInStyle($styleId, $addOnId);

		// run after the delete to not include removed data
		$existingGroups = $this->getEffectiveStylePropertyGroupsInStyle($styleId);

		foreach ($newGroups AS $group)
		{
			if (isset($existingGroups[$group['group_name']]))
			{
				continue;
			}

			$dw = XenForo_DataWriter::create('XenForo_DataWriter_StylePropertyGroup');
			$dw->setOption(XenForo_DataWriter_StylePropertyGroup::OPTION_UPDATE_MASTER_PHRASE, false);
			$dw->setOption(XenForo_DataWriter_StylePropertyGroup::OPTION_UPDATE_DEVELOPMENT, false);
			$dw->bulkSet(array(
				'group_name' => $group['group_name'],
				'group_style_id' => $styleId,
				'title' => $group['title'],
				'description' => $group['description'],
				'display_order' => $group['display_order'],
				'addon_id' => $addOnIdString
			));
			$dw->save();
		}

		foreach ($newProperties AS $property)
		{
			$propertyName = $property['property_name'];
			$propertyValue = $property['property_value'];

			$definition = null;
			$deletedDefinitionId = 0;
			$existingProperty = null;

			if (isset($existingProperties[$propertyName]))
			{
				$definition = $existingProperties[$propertyName];
				if ($definition['definition_style_id'] == $styleId && ($addOnId === null || $definition['addon_id'] == $addOnId))
				{
					$deletedDefinitionId = $definition['property_definition_id'];
					$definition = null;
				}
			}

			if (!empty($property['definition']))
			{
				if (!$definition)
				{
					$dw = XenForo_DataWriter::create('XenForo_DataWriter_StylePropertyDefinition');
					$dw->setOption(XenForo_DataWriter_StylePropertyDefinition::OPTION_UPDATE_MASTER_PHRASE, false);
					$dw->setOption(XenForo_DataWriter_StylePropertyDefinition::OPTION_UPDATE_DEVELOPMENT, false);
					$dw->setOption(XenForo_DataWriter_StylePropertyDefinition::OPTION_CHECK_DUPLICATE, false);
					$dw->bulkSet(array(
						'property_name' => $propertyName,
						'group_name' => $property['group_name'],
						'title' => $property['title'],
						'description' => $property['description'],
						'definition_style_id' => $styleId,
						'property_type' => $property['property_type'],
						'css_components' => $property['css_components'],
						'scalar_type' => $property['scalar_type'],
						'scalar_parameters' => $property['scalar_parameters'],
						'display_order' => $property['display_order'],
						'sub_group' => $property['sub_group'],
						'addon_id' => $addOnIdString
					));
					$dw->save();

					$definition = $dw->getMergedData();

					if ($deletedDefinitionId)
					{
						$db->update('xf_style_property', array(
							'property_definition_id' => $definition['property_definition_id']
						), 'property_definition_id = ' . $db->quote($deletedDefinitionId));
					}

					$existingProperty = false;
				}
			}
			else if ($definition)
			{
				if ($definition['style_id'] == $styleId && $addOnId !== null && $definition['addon_id'] !== $addOnId)
				{
					$existingProperty = $definition;
				}
				else
				{
					$existingProperty = false;
				}
			}
			else
			{
				// non-definition and no matching definition
				continue;
			}

			$this->updateStylePropertyValue(
				$definition, $styleId, $propertyValue,
				array(
					XenForo_DataWriter_StyleProperty::OPTION_REBUILD_CACHE => false,
					XenForo_DataWriter_StyleProperty::OPTION_UPDATE_DEVELOPMENT => false
				),
				$existingProperty
			);
		}

		$this->rebuildPropertyCacheInStyleAndChildren($styleId);

		XenForo_Db::commit($db);
	}

	/**
	 * Replaces {xen:property} references in a template with the @ property version.
	 * This allows for easier editing and viewing of properties.
	 *
	 * @param string $templateText Template with {xen:property} references
	 * @param integer $editStyleId The style the template is being edited in
	 * @param array|null $properties A list of valid style properties; if null, grabbed automatically ([name] => property)
	 *
	 * @return string Replaced template text
	 */
	public function replacePropertiesInTemplateForEditor($templateText, $editStyleId, array $properties = null)
	{
		if ($properties === null)
		{
			$properties = $this->keyPropertiesByName($this->getEffectiveStylePropertiesInStyle($editStyleId));
		}

		$validComponents = array('font', 'background', 'padding', 'margin', 'border', 'extra');

		preg_match_all('#(?P<leading_space>[ \t]*)\{xen:property\s+("|\'|)(?P<property>[a-z0-9._-]+)\\2\s*\}#si', $templateText, $matches, PREG_SET_ORDER);
		foreach ($matches AS $match)
		{
			$propertyReference = $match['property'];
			$parts = explode('.', $propertyReference, 2);

			$propertyName = $parts[0];
			$propertyComponent = (count($parts) == 2 ? $parts[1] : false);

			if (!isset($properties[$propertyName]) || $properties[$propertyName]['property_type'] == 'scalar')
			{
				continue;
			}

			if ($propertyComponent && !in_array($propertyComponent, $validComponents))
			{
				continue;
			}

			$propertyValue = unserialize($properties[$propertyName]['property_value']);
			$outputValue = $propertyValue;

			$outputValue = $this->compileCssProperty_sanitize($outputValue, $propertyValue);
			$outputValue = $this->compileCssProperty_compileRules($outputValue, $propertyValue);
			$outputValue = $this->compileCssProperty_cleanUp($outputValue, $propertyValue);

			$replacementRules = '';
			if ($propertyComponent)
			{
				if (isset($outputValue[$propertyComponent]))
				{
					$replacementRules = $outputValue[$propertyComponent];
				}
			}
			else
			{
				foreach ($validComponents AS $validComponent)
				{
					if (isset($outputValue[$validComponent]))
					{
						$replacementRules .= "\n" . $outputValue[$validComponent];
					}
				}
				if (isset($outputValue['width']))
				{
					$replacementRules .= "\nwidth: $outputValue[width];";
				}
				if (isset($outputValue['height']))
				{
					$replacementRules .= "\nheight: $outputValue[height];";
				}
			}

			$leadingSpace = $match['leading_space'];

			$replacementRules = preg_replace('#(^|;(\r?\n)+)[ ]*([*a-z0-9_/\-]+):\s*#i', "\\1{$leadingSpace}\\3: ", trim($replacementRules));

			$replacement = $leadingSpace . '@property "' . $propertyReference . '";'
				. "\n" . $replacementRules
				. "\n" . $leadingSpace . '@property "/' . $propertyReference . '";';

			$templateText = str_replace($match[0], $replacement, $templateText);
		}

		preg_match_all('/\{xen:property\s+("|\'|)(?P<propertyName>[a-z0-9_]+)(?P<propertyComponent>\.[a-z0-9._-]+)?\\1\s*\}/si', $templateText, $matches, PREG_SET_ORDER);
		foreach ($matches AS $match)
		{
			if (!in_array(strtolower($match['propertyName']), XenForo_DataWriter_StylePropertyDefinition::$reservedNames))
			{
				$replacement = '@' . $match['propertyName'] . (empty($match['propertyComponent']) ? '' : $match['propertyComponent']);

				$templateText = str_replace($match[0], $replacement, $templateText);
			}
		}

		return $templateText;
	}

	protected static function _atToPropertyCallback($property)
	{
		$name = $property[1];

		if (!isset(self::$_tempProperties[$name]))
		{
			return $property[0];
		}

		if (!in_array(strtolower($name), XenForo_DataWriter_StylePropertyDefinition::$reservedNames))
		{
			return '{xen:property ' . $name . (empty($property[2]) ? '' : $property[2]) . '}';
		}

		return $property[0];
	}

	/**
	 * Converts @propertyName to {xen:property propertyName}
	 *
	 * @param string $text
	 * @param array $properties
	 *
	 * @return string
	 */
	public function convertAtPropertiesToTemplateSyntax($text, array $properties)
	{
		self::$_tempProperties = $properties;
		$text = preg_replace_callback(
			'/(?<=[^a-z0-9_]|^)@([a-z0-9_]+)(\.[a-z0-9._-]+)?/si',
			array('self', '_atToPropertyCallback'),
			$text
		);
		self::$_tempProperties = null;

		return $text;
	}

	/**
	 * Translates @ property style references from the template editor into a structured array,
	 * and rewrites the template text to the standard {xen:property} format.
	 *
	 * @param string $templateText Template with @ property references
	 * @param string $outputText By reference, the template with {xen:property} values instead
	 * @param array $properties A list of valid style properties in the correct style; keyed by named
	 *
	 * @return array Property values from the template text. Change detection still needs to be run.
	 */
	public function translateEditorPropertiesToArray($templateText, &$outputText, array $properties)
	{
		// replace @property 'foo'; .... @property '/foo'; with {xen:property foo}
		$outputText = $templateText;
		$outputProperties = array();

		preg_match_all('/
			@property\s+("|\')(?P<name>[a-z0-9._-]+)\\1;
			(?P<rules>([^@]*?|@(?!property))*)
			@property\s+("|\')\/(?P=name)\\5;
			/siUx', $templateText, $matches, PREG_SET_ORDER
		);
		foreach ($matches AS $match)
		{
			$parts = explode('.', $match['name'], 2);
			$propertyName = $parts[0];
			$propertyComponent = (count($parts) == 2 ? $parts[1] : false);
			if ($propertyComponent == 'font')
			{
				$propertyComponent = 'text';
			}
			else if ($propertyComponent == 'margin' || $propertyComponent == 'padding')
			{
				$propertyComponent = 'layout';
			}

			if (!isset($properties[$propertyName]) || $properties[$propertyName]['property_type'] != 'css')
			{
				continue;
			}

			$validComponents = unserialize($properties[$propertyName]['css_components']);
			if ($propertyComponent && !isset($validComponents[$propertyComponent]))
			{
				continue;
			}

			$set = array(
				'name' => $propertyName,
				'component' => $propertyComponent,
				'rules' => array()
			);
			$extra = array();
			$nonPropertyRules = array();
			$paddingValues = array();
			$marginValues = array();

			$comments = array();
			preg_match_all('#/\*(.+)(\*/|$)#siU', $match['rules'], $commentMatches, PREG_SET_ORDER);
			foreach ($commentMatches AS $commentMatch)
			{
				$comments[] = $commentMatch[1];
				$match['rules'] = str_replace($commentMatch[0], '', $match['rules']);
			}

			preg_match_all('/
				(?<=^|\s|;)(?P<name>[a-z0-9-_*]+)
				\s*:\s*
				(?P<value>[^;]*)
				(;|$)
				/siUx', $match['rules'], $ruleMatches, PREG_SET_ORDER
			);
			foreach ($ruleMatches AS $ruleMatch)
			{
				$value = trim($ruleMatch['value']);
				if ($value === '')
				{
					continue;
				}

				$name = strtolower($ruleMatch['name']);

				switch ($name)
				{
					case 'color':
					case 'text-decoration':
						$group = 'text';
						break;

					case 'width':
					case 'height':
						$group = 'layout';
						break;

					default:
						$regex = '/^('
							. 'font|font-(family|size|style|variant|weight)'
							. '|background|background-(color|image|position|repeat)'
							. '|padding|padding-.*|margin|margin-.*'
							. '|border|border-.*-radius|border(-(top|right|bottom|left))?(-(color|style|width|radius))?'
							. ')$/';
						if (preg_match($regex, $name, $nameMatch))
						{
							$ruleParts = explode('-', $nameMatch[1], 2);
							$group = $ruleParts[0];
						}
						else
						{
							$group = 'extra';
						}
				}

				// css references font, but the css components list references text
				if ($group == 'font')
				{
					$group = 'text';
				}
				else if ($group == 'padding' || $group == 'margin')
				{
					$group = 'layout';
				}

				if (($propertyComponent && $group != $propertyComponent) || !isset($validComponents[$group]))
				{
					if (isset($validComponents['extra']))
					{
						$extra[$name] = $value;
					}
					else
					{
						$nonPropertyRules[] = $ruleMatch[0];
					}
				}
				else
				{
					$isValidRule = false;

					if ($group == 'extra')
					{
						$isValidRule = true;
						$extra[$name] = $value;
					}
					else if ($value == 'inherit')
					{
						$isValidRule = false; // can't put inherit rules in properties
						$nonPropertyRules[] = $ruleMatch[0];
					}
					else
					{
						$isValidRule = false;

						switch ($name)
						{
							case 'font':
								$ruleOutput = $this->parseFontCss($value);
								if (is_array($ruleOutput))
								{
									$isValidRule = true;
									$set['rules'] = array_merge($set['rules'], $ruleOutput);
								}
								break;

							case 'text-decoration':
								$isValidRule = true;

								if ($value == 'none')
								{
									$set['rules']['text-decoration'] = array('none' => 'none');
								}
								else
								{
									$decorations = preg_split('/\s+/', strtolower($value), -1, PREG_SPLIT_NO_EMPTY);
									$set['rules']['text-decoration'] = array_combine($decorations, $decorations);
								}
								break;

							case 'background':
								$ruleOutput = $this->parseBackgroundCss($value);
								if (is_array($ruleOutput))
								{
									$isValidRule = true;
									$set['rules'] = array_merge($set['rules'], $ruleOutput);
								}
								break;

							case 'background-image':
								$isValidRule = true;

								if (preg_match('/^url\(("|\'|)([^)]+)\\1\)$/iU', $value, $imageMatch))
								{
									$set['rules']['background-image'] = $imageMatch[2];
								}
								else
								{
									$set['rules']['background-image'] = $value;
								}
								break;

							case 'padding':
								if ($this->parsePaddingMarginCss($value, $paddingValues))
								{
									$isValidRule = true;
								}
								break;

							case 'padding-top':
							case 'padding-right':
							case 'padding-bottom':
							case 'padding-left':
								$paddingValues[substr($name, 8)] = $value;
								unset($paddingValues['all']);
								$isValidRule = true;
								break;

							case 'margin':
								if ($this->parsePaddingMarginCss($value, $marginValues))
								{
									$isValidRule = true;
								}
								break;

							case 'margin-top':
							case 'margin-right':
							case 'margin-bottom':
							case 'margin-left':
								$marginValues[substr($name, 7)] = $value;
								unset($marginValues['all']);
								$isValidRule = true;
								break;

							case 'border':
							case 'border-top':
							case 'border-right':
							case 'border-bottom':
							case 'border-left':
								$ruleOutput = $this->parseBorderCss($value, $name);
								if (is_array($ruleOutput))
								{
									$isValidRule = true;
									$set['rules'] = array_merge($set['rules'], $ruleOutput);
								}
								break;

							default:
								$isValidRule = true;
								$set['rules'][$name] = $value;
						}

						if (!$isValidRule)
						{
							$nonPropertyRules[] = "-xenforo-nomatch-" . $ruleMatch[0];
						}
					}
				}
			}

			if ($paddingValues)
			{
				if (isset($paddingValues['all']))
				{
					$set['rules']['padding-all'] = $paddingValues['all'];
				}
				else
				{
					foreach (array('top', 'right', 'bottom', 'left') AS $paddingSide)
					{
						if (isset($paddingValues[$paddingSide]))
						{
							$set['rules']["padding-$paddingSide"] = $paddingValues[$paddingSide];
						}
					}
				}
			}
			if ($marginValues)
			{
				if (isset($marginValues['all']))
				{
					$set['rules']['margin-all'] = $marginValues['all'];
				}
				else
				{
					foreach (array('top', 'right', 'bottom', 'left') AS $marginSide)
					{
						if (isset($marginValues[$marginSide]))
						{
							$set['rules']["margin-$marginSide"] = $marginValues[$marginSide];
						}
					}
				}
			}

			if ($extra || $comments)
			{
				$set['rules']['extra'] = '';

				if ($extra)
				{
					foreach ($extra AS $extraRule => $extraValue)
					{
						$set['rules']['extra'] .= "\n$extraRule: $extraValue;";
					}
				}
				if ($comments)
				{
					foreach ($comments AS $comment)
					{
						$set['rules']['extra'] .= "\n/*$comment*/";
					}
				}

				$set['rules']['extra'] = trim($set['rules']['extra']);
			}

			$outputProperties[] = $set;

			$replacement = '{xen:property ' . $match['name'] . '}';
			foreach ($nonPropertyRules AS $nonPropertyRule)
			{
				$replacement .= "\n\t$nonPropertyRule";
			}

			$outputText = str_replace($match[0], $replacement, $outputText);
		}

		$outputText = $this->convertAtPropertiesToTemplateSyntax($outputText, $properties);

		return $outputProperties;
	}

	/**
	 * Parses font shortcut CSS.
	 *
	 * @param string $value
	 *
	 * @return array|false List of property rules to apply or false if shortcut could not be parsed
	 */
	public function parseFontCss($value)
	{
		preg_match('/
			^
			((?P<font_style>italic|oblique|normal)\s+)?
			((?P<font_variant>small-caps|normal)\s+)?
			((?P<font_weight>bold(?:er)?|lighter|[1-9]00|normal)\s+)?
			(?P<font_size>
				xx-small|x-small|small|medium|large|x-large|xx-large|smaller|larger
				|0|-?\d+(\.\d+)?(%|[a-z]+)
				|\{xen:property\s+("|\'|)([a-z0-9._-]+)("|\'|)\s*\}
				|@[a-z0-9._-]+
			)
			\s+
			(?P<font_family>\S.*)
			$
			/siUx', $value, $fontMatch
		);
		if (!$fontMatch)
		{
			return false;
		}

		$output = array();
		if (!empty($fontMatch['font_style']) && strtolower($fontMatch['font_style']) != 'normal')
		{
			$output['font-style'] = 'italic';
		}
		else
		{
			$output['font-style'] = '';
		}

		if (!empty($fontMatch['font_variant']) && strtolower($fontMatch['font_variant']) != 'normal')
		{
			$output['font-variant'] = 'small-caps';
		}
		else
		{
			$output['font-variant'] = '';
		}

		if (!empty($fontMatch['font_weight']) && strtolower($fontMatch['font_weight']) != 'normal')
		{
			$output['font-weight'] = 'bold';
		}
		else
		{
			$output['font-weight'] = '';
		}

		$output['font-size'] = $fontMatch['font_size'];
		$output['font-family'] = $fontMatch['font_family'];

		return $output;
	}

	/**
	 * Parses background shortcut CSS.
	 *
	 * @param string $value
	 *
	 * @return array|false List of property rules to apply or false if shortcut could not be parsed
	 */
	public function parseBackgroundCss($value)
	{
		if (strtolower($value) == 'none')
		{
			return array(
				'background-none' => '1',
				'background-color' => '',
				'background-image' => '',
				'background-repeat' => '',
				'background-position' => ''
			);
		}

		$output = array();

		do
		{
			if (preg_match('/^(repeat-x|repeat-y|no-repeat|repeat)/i', $value, $match))
			{
				if (isset($output['background-repeat']))
				{
					return false;
				}
				$output['background-repeat'] = $match[0];
			}
			else if (preg_match('/^(none|url\(("|\'|)(?P<background_image_url>[^)]+)\\2\))/i', $value, $match))
			{
				if (isset($output['background-image']))
				{
					return false;
				}

				if ($match[0] == 'none')
				{
					$output['background-image'] = 'none';
				}
				else
				{
					$output['background-image'] = $match['background_image_url'];
				}
			}
			else if (preg_match('/^(
					(
						(left|center|right|0|-?\d+(\.\d+)?(%|[a-z]+))
						(
							\s+(top|center|bottom|0|-?\d+(\.\d+)?(%|[a-z]+))
						)?
					)|top|center|bottom
				)/ix', $value, $match))
			{
				if (isset($output['background-position']))
				{
					return false;
				}
				$output['background-position'] = $match[0];
			}
			else if (preg_match('/^(
					rgb\(\s*\d+%?\s*,\s*\d+%?\s*,\s*\d+%?\s*\)
					|rgba\(\s*\d+%?\s*,\s*\d+%?\s*,\s*\d+%?\s*,\s*[0-9.]+\s*\)
					|\#[a-f0-9]{6}|\#[a-f0-9]{3}
					|[a-z]+
				)/ix', $value, $match)
			)
			{
				if (isset($output['background-color']))
				{
					return false;
				}
				$output['background-color'] = $match[0];
			}
			else if (preg_match('/^(
					(\{xen:property\s+("|\'|)([a-z0-9._-]+)("|\'|)\s*\})
					|@[a-z0-9._-]+
				)/ix', $value, $match))
			{
				$handled = false;
				foreach (array('background-color', 'background-image', 'background-position') AS $ruleName)
				{
					if (!isset($output[$ruleName]))
					{
						$output[$ruleName] = $match[0];
						$handled = true;
						break;
					}
				}
				if (!$handled)
				{
					return false;
				}
			}
			else
			{
				return false;
			}

			$value = strval(substr($value, strlen($match[0])));

			if (preg_match('/^(\s+|$)/', $value))
			{
				$value = ltrim($value);
			}
			else
			{
				return false;
			}
		}
		while ($value !== '');

		if (!$output)
		{
			return false;
		}

		return array_merge(
			array(
				'background-color' => '',
				'background-image' => '',
				'background-repeat' => '',
				'background-position' => ''
			),
			$output
		);

	}

	/**
	 * Parses padding/margin shortcut CSS.
	 *
	 * @param string $value
	 * @param array $values By reference. Pushes out the effective padding/margin values to later be pulled together.
	 *
	 * @return boolean
	 */
	public function parsePaddingMarginCss($value, array &$values)
	{
		$value = preg_replace('#\{xen:property\s+("|\'|)([a-z0-9_-]+(\.[a-z0-9_-]+)?)\\1\s*\}#i', '@\\2', $value);

		$paddingParts = preg_split('/\s+/', $value, -1, PREG_SPLIT_NO_EMPTY);
		if (count($paddingParts) > 4)
		{
			return false;
		}

		foreach ($paddingParts AS $paddingPart)
		{
			if ($paddingPart[0] !== '@' && !preg_match('#^0|auto|-?\d+(\.\d+)?(%|[a-z]+)$#i', $paddingPart))
			{
				return false;
			}
		}

		switch (count($paddingParts))
		{
			case 1:
				$values = array(
					'all' => $paddingParts[0],
					'top' => $paddingParts[0],
					'right' => $paddingParts[0],
					'bottom' => $paddingParts[0],
					'left' => $paddingParts[0],
				);
				break;

			case 2:
				$values = array(
					'top' => $paddingParts[0],
					'right' => $paddingParts[1],
					'bottom' => $paddingParts[0],
					'left' => $paddingParts[1],
				);
				break;

			case 3:
				$values = array(
					'top' => $paddingParts[0],
					'right' => $paddingParts[1],
					'bottom' => $paddingParts[2],
					'left' => $paddingParts[1],
				);
				break;

			case 4:
				$values = array(
					'top' => $paddingParts[0],
					'right' => $paddingParts[1],
					'bottom' => $paddingParts[2],
					'left' => $paddingParts[3],
				);
				break;
		}

		return true;
	}

	/**
	 * Parses border shortcut CSS.
	 *
	 * @param string $value
	 * @param string $name The name of the shortcut (border, border-top, etc)
	 *
	 * @return array|false List of property rules to apply or false if shortcut could not be parsed
	 */
	public function parseBorderCss($value, $name)
	{
		$output = array();

		do
		{
			if (preg_match('/^(thin|medium|thick|0|-?\d+(\.\d+)?[a-z]+)/i', $value, $match))
			{
				if (isset($output["$name-width"]))
				{
					return false;
				}
				$output["$name-width"] = $match[0];
			}
			else if (preg_match('/^(none|hidden|dashed|dotted|double|groove|inset|outset|ridge|solid)/i', $value, $match))
			{
				if (isset($output["$name-style"]))
				{
					return false;
				}
				$output["$name-style"] = $match[0];
			}
			else if (preg_match('/^(
					rgb\(\s*\d+%?\s*,\s*\d+%?\s*,\s*\d+%?\s*\)
					|rgba\(\s*\d+%?\s*,\s*\d+%?\s*,\s*\d+%?\s*,\s*[0-9.]+\s*\)
					|\#[a-f0-9]{6}|\#[a-f0-9]{3}
					|[a-z]+
				)/ix', $value, $match)
			)
			{
				if (isset($output["$name-color"]))
				{
					return false;
				}
				$output["$name-color"] = $match[0];
			}
			else if (preg_match('/^(
					(\{xen:property\s+("|\'|)([a-z0-9._-]+)\\2\s*\})
					|@[a-z0-9._-]+
				)/ix', $value, $match))
			{
				$handled = false;
				foreach (array("$name-width", "$name-color") AS $ruleName)
				{
					if (!isset($output[$ruleName]))
					{
						$output[$ruleName] = $match[0];
						$handled = true;
						break;
					}
				}
				if (!$handled)
				{
					return false;
				}
			}
			else
			{
				return false;
			}

			$value = strval(substr($value, strlen($match[0])));

			if (preg_match('/^(\s+|$)/', $value))
			{
				$value = ltrim($value);
			}
			else
			{
				return false;
			}
		}
		while ($value !== '');

		if (!$output)
		{
			return false;
		}

		return array_merge(
			array(
				"$name-width" => '1px',
				"$name-style" => 'none',
				"$name-color" => 'black'
			),
			$output
		);
	}

	/**
	 * Saves style properties in the specified style based on the @ property
	 * references that have been parsed out of the template(s).
	 *
	 * @param integer $styleId Style to save properties into
	 * @param array $updates List of property data to update (return from translateEditorPropertiesToArray)
	 * @param array $properties List of style properties available in this style. Keyed by name!
	 */
	public function saveStylePropertiesInStyleFromTemplate($styleId, array $updates, array $properties)
	{
		$input = array();

		foreach ($updates AS $update)
		{
			if (!isset($properties[$update['name']]))
			{
				continue;
			}

			$property = $properties[$update['name']];

			$definitionId = $property['property_definition_id'];

			if ($update['component'])
			{
				if (isset($input[$definitionId]))
				{
					$base = $input[$definitionId];
				}
				else
				{
					$base = unserialize($property['property_value']);
				}

				$input[$definitionId] = array_merge($base, $update['rules']);
			}
			else
			{
				$input[$definitionId] = $update['rules'];
			}
		}

		if ($input)
		{
			$this->saveStylePropertiesInStyleFromInput($styleId, $input);
		}
	}

	/**
	 * Updates the group_name of all style property definitions in group $sourceGroup to $destinationGroup.
	 *
	 * @param string $sourceGroup
	 * @param string $destinationGroup
	 */
	public function moveStylePropertiesBetweenGroups($sourceGroup, $destinationGroup)
	{
		XenForo_Db::beginTransaction($this->_getDb());

		foreach ($this->getStylePropertyDefinitionsByGroup($sourceGroup, 0) AS $property)
		{
			$dw = XenForo_DataWriter::create('XenForo_DataWriter_StylePropertyDefinition', XenForo_DataWriter::ERROR_EXCEPTION);
			$dw->setExistingData($property['property_definition_id']);
			$dw->set('group_name', $destinationGroup);
			$dw->save();
		}

		XenForo_Db::commit($this->_getDb());
	}

	/**
	 * @return XenForo_Model_Style
	 */
	protected function _getStyleModel()
	{
		return $this->getModelFromCache('XenForo_Model_Style');
	}
}
Back to Top