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
<?php
class DataTransfer extends BEAppModel
{
public $useTable = false;
private $maxRelationLevels = 2;
protected $objDefaults = array(
'status' => 'on',
'user_created' => '1',
'user_modified' => '1',
'lang' => 'ita',
'ip_created' => '127.0.0.1',
'syndicate' => 'off',
);
protected $objMinimalSet = array(
'id',
'objectType'
);
protected $relMinimalSet = array(
'idLeft',
'idRight',
'switch'
);
protected $import = array(
'source' => array(
'string' => null,
'data' => array()
),
'objects' => array(
'ids' => array(),
'types' => array()
),
'tree' => array(
'ids' => array(),
'parents' => array()
),
'relations' => array(
'ids' => array(),
'switches' => array()
),
'saveMap' => array(
),
'saveMode' => 1,
'logLevel' => 2
);
protected $export = array(
'destination' => array(
'byType' => array(
'ARRAY' => array(
'config' => array(),
'tree' => array(),
'objects' => array(),
'relations' => array()
),
'JSON' => ''
)
),
'logLevel' => 2,
'returnType' => 'JSON',
'filename' => null,
'all' => true,
'types' => null,
'relations' => null,
'objectUnsetFields' => array(
'user_created',
'user_modified',
'valid',
'ip_created',
'object_type_id',
'ObjectType',
'UserCreated',
'UserModified',
'User',
'Version',
'Permission',
'Annotation',
'area_id',
'object_path',
'parent_id',
'parent_path',
'priority',
'priority_order',
'syndicate'
),
'contain' => array(
'BEObject' => array(
'RelatedObject',
'ObjectProperty',
'LangText',
'Annotation',
'Category',
'GeoTag'
)
),
'contain-stream' => array(
'BEObject' => array(
'RelatedObject',
'ObjectProperty',
'LangText',
'Annotation',
'Category',
'GeoTag'
),
'Stream'
),
'media' => array(),
'customProperties' => array()
);
protected $streamModels = array(
'Image',
'Video',
'Application',
);
protected $result = array(
);
private $logFile;
private $logLevel;
protected $logLevels = array(
'ERROR' => 0,
'WARN' => 1,
'INFO' => 2,
'DEBUG' => 3
);
protected $saveModes = array(
'MERGE' => 0,
'NEW' => 1,
'OVERRIDE' => 2,
'IGNORE' => 3,
'UPDATE' => 4
);
protected $customPropertyDataTypes = array(
'number',
'date',
'text',
'options'
);
public function import(&$data, $options = array()) {
$this->result = array();
$this->logFile = 'import';
$this->logLevel = (!empty($options['logLevel'])) ? $options['logLevel'] : $this->logLevels['INFO'];
$this->import['saveMode'] = (!empty($options['saveMode'])) ? $options['saveMode'] : $this->saveModes['NEW'];
if (!empty($options['sourceMediaRoot'])) {
$this->import['sourceMediaRoot'] = $options['sourceMediaRoot'];
} else if (!empty($options['sourceMediaUri'])) {
$this->import['sourceMediaUri'] = $options['sourceMediaUri'];
} else {
$this->import['sourceMediaRoot'] = TMP . 'media-import';
}
$this->trackInfo('START');
try {
$this->trackInfo('1 validate start');
$this->validate($data, $options);
$this->trackInfo('1 validate OK');
$this->trackInfo('2 import start');
$this->trackInfo('2.1 import config');
if (!empty($this->import['source']['data']['config'])) {
$this->trackInfo('2.1.1 import custom properties');
if (!empty($this->import['source']['data']['config']['customProperties'])) {
foreach ($this->import['source']['data']['config']['customProperties'] as $customProperty) {
$this->trackDebug('2.1.1 import custom properties - save property with policy \'NEW\'');
$this->saveProperty($customProperty);
}
}
}
if (!empty($this->import['tree']['roots'])) {
$this->trackInfo('2.2 save areas/sections');
$this->trackInfo('2.2.1 save roots (areas/sections)');
$rootIds = $this->import['tree']['roots'];
foreach ($rootIds as $rootId) {
$rootData = $this->import['source']['data']['objects'][$rootId];
$rootObjType = $this->import['source']['data']['objects'][$rootId]['objectType'];
$this->trackDebug('2.2.1.1 save area/section(s) with policy (old id ' . $rootId . ') \'NEW\'');
$this->trackDebug('2.2.1.2 save area/section(s) with other policies (old id ' . $rootId . ') [TODO]');
if ($rootObjType == 'area') {
$this->saveArea($rootData);
} else if ($rootObjType == 'section') {
$parentId = $options['parentId'];
$this->saveSection($rootData, $parentId);
}
}
} else if (!empty($options['parentId']) && !empty($this->import['source']['data']['tree']['roots'])) {
foreach ($this->import['source']['data']['tree']['roots'] as $rootId) {
$parentId = $options['parentId'];
$rootData = $this->import['source']['data']['objects'][$rootId];
$this->saveSection($rootData, $parentId);
}
}
if (!empty($this->import['source']['data']['tree']['sections'])) {
$this->trackDebug('2.2.2 save other section(s)');
foreach ($this->import['source']['data']['tree']['sections'] as $section) {
$newParentId = null;
if (empty($this->import['saveMap'][$section['parent']]) && !empty($genParentId)) {
$newParentId = $genParentId;
} else {
$newParentId = $this->import['saveMap'][$section['parent']];
}
$this->trackDebug('2.2.2.1 save section(s) (old section id ' . $section['id'] . ' | old parent_id ' . $section['parent'] . ' | new parent id ' . $newParentId . ') with policy \'NEW\'');
$this->trackDebug('2.2.2.2 save section(s) (old section id ' . $section['id'] . ' | old parent_id ' . $section['parent'] . ' | new parent id ' . $newParentId . ') with other policies [TODO]');
$this->saveSection($section, $newParentId);
}
}
$this->trackInfo('2.3 save objects');
if (!empty($this->import['media'])) {
$this->trackInfo('2.3.1 copy media');
$streamModel = ClassRegistry::init('Stream');
foreach ($this->import['media'] as $id => &$media) {
try {
if (!empty($media['full'])) {
$beUri = $streamModel->copyFileToMediaFolder($media['full'], $this->import['destination']['media']['root']);
$beFull = $this->import['destination']['media']['root'] . $beUri;
$this->import['source']['data']['objects'][$id]['uri'] = $beUri;
} else {
if (!filter_var($media['uri'], FILTER_VALIDATE_URL)) {
$this->trackWarn('missing media file for object ' . $id . ' - uri: ' . $media['uri']);
}
}
} catch(Exception $e) {
$this->trackError($e->getMessage());
}
}
}
$orderedObjects = array();
$counter = 1;
foreach ($this->import['source']['data']['objects'] as &$object) {
if (!empty($object['parents'])) {
foreach ($object['parents'] as $parent) {
if (!empty($parent['priority'])) {
$orderedObjects[$object['id']] = str_pad($parent['id'], 20, '0', STR_PAD_LEFT) . '-' . str_pad($parent['priority'], 20, '0', STR_PAD_LEFT);
} else {
$orderedObjects[$object['id']] = str_pad($parent['id'], 20, '0', STR_PAD_LEFT) . '-' . str_pad(0, 20, '0', STR_PAD_LEFT);
}
}
} else {
$orderedObjects[$object['id']] = str_pad($counter++, 25, '0', STR_PAD_LEFT) . '-' . str_pad('0', 25, '0', STR_PAD_LEFT);
}
}
asort($orderedObjects);
$sortObjects = array();
$orderedIds = array_keys($orderedObjects);
foreach ($orderedIds as $objectId) {
$sortObjects[$objectId] = $this->import['source']['data']['objects'][$objectId];
}
$this->import['source']['data']['objects'] = $sortObjects;
foreach ($this->import['source']['data']['objects'] as &$object) {
$this->saveObject($object);
}
if (!empty($this->import['source']['data']['relations'])) {
$this->trackInfo('2.4 save relations');
$relationKeys = array_keys($this->import['source']['data']['relations']);
$relationStructureOne = is_int($relationKeys[0]);
$counter = 1;
if ($relationStructureOne) {
foreach ($this->import['source']['data']['relations'] as &$relation) {
$this->saveRelation($relation, $counter++);
}
} else {
foreach ($this->import['source']['data']['relations'] as $switch => &$relations) {
foreach ($relations as &$relation) {
$this->saveRelation($relation, $counter++);
}
}
}
}
$this->trackInfo('2 import OK');
} catch(Exception $e) {
$this->trackError('ERROR: ' . $e->getMessage());
}
$this->trackInfo('3 result');
$this->trackDebug('3.2 return result');
$this->trackInfo('END');
$this->importInfo();
return $this->result;
}
public function export(array &$objects, $options = array()) {
$this->result = array();
$this->logFile = 'export';
$this->export['destMediaRoot'] = TMP . 'media-export';
$this->export = array_merge($this->export, $options);
if (empty($this->export['status'])) {
$this->export['status'] = array('on', 'off', 'draft');
}
$this->logLevel = $this->export['logLevel'];
$this->trackInfo('START');
try {
$this->export['objectTypeIds'] = array();
$this->export['objectTypes'] = array();
if ($this->export['types'] != NULL) {
$types = explode(',', $this->export['types']);
foreach ($types as $type) {
$ot = Configure::read('objectTypes.' . $type . '.id');
if (!$ot) {
throw new BeditaException('Object type "' . $type . '" not found');
}
$this->export['objectTypeIds'][] = $ot;
$this->export['objectTypes'][] = $type;
}
if (!empty($this->export['related-types'])) {
$rtypes = explode(',', $this->export['related-types']);
foreach ($rtypes as $rtype) {
$rot = Configure::read('objectTypes.' . $rtype . '.id');
if (!$rot) {
throw new BeditaException('Object type "' . $rtype . '" not found');
}
$this->export['relatedObjectTypeIds'][] = $rot;
$this->export['relatedObjectTypes'][] = $rtype;
}
}
}
if ($this->export['relations'] != NULL) {
$this->export['relations'] = explode(',', $this->export['relations']);
}
$objModel = ClassRegistry::init('BEObject');
if (empty($objects) && ($this->export['all'] === true) ) {
$objModel->create();
if (empty($this->export['objectTypeIds'])) {
$this->export['objectTypeIds'][] = Configure::read('objectTypes.area.id');
}
$objIds = $objModel->find('list', array(
'fields' => array('id'),
'conditions' => array(
'object_type_id' => $this->export['objectTypeIds'],
'status' => $this->export['status']
)
));
$objects = array_keys($objIds);
}
$typeIds = array();
foreach ($objects as $objectId) {
$o = $this->objectTypeId($objectId);
if (empty($o)) {
throw new BeditaException('Object with id "' . $objectId . '" not found');
} else {
if (!empty($this->export['id'])) {
$ot = Configure::read('objectTypes.' . $o . '.name');
if (empty($this->export['relatedObjectTypeIds'])) {
$this->export['relatedObjectTypeIds'] = array();
$this->export['relatedObjectTypes'] = array();
}
$this->export['relatedObjectTypeIds'][] = $o;
$this->export['relatedObjectTypes'][] = $ot;
if ($ot == 'area') {
$ot = Configure::read('objectTypes.section.id');
$this->export['relatedObjectTypeIds'][] = $ot;
$this->export['relatedObjectTypes'][] = 'section';
}
}
if (!in_array($o, $typeIds)) {
$typeIds[] = $o;
$p = ClassRegistry::init('Property')->find(
'all', array(
'conditions' => array(
'object_type_id' => $o
),
'contain' => array('PropertyOption')
)
);
if (!empty($p)) {
foreach ($p as $cproperty) {
$this->export['customProperties'][$cproperty['id']] = $cproperty;
unset($this->export['customProperties'][$cproperty['id']]['id']);
}
}
}
}
}
$this->trackDebug('1 area/section/other objects data');
$treeModels = array('Area', 'Section');
$extractTreeData = true;
$conf = Configure::getInstance();
foreach ($objects as $objectId) {
$objectTypeId = ClassRegistry::init('BEObject')->findObjectTypeId($objectId);
if (isset($conf->objectTypes[$objectTypeId])) {
$model = $conf->objectTypes[$objectTypeId]['model'];
} else if (isset($conf->objectTypesExt[$objectTypeId])) {
$model = $conf->objectTypesExt[$objectTypeId]['model'];
} else {
throw new BeditaException('Model not found per objecttypeId "' . $objectTypeId . '"');
}
$objModel = ClassRegistry::init($model);
$objModel->contain(
$this->modelBinding($model, $objModel)
);
$obj = $objModel->findById($objectId);
$this->prepareObjectForExport($obj);
if (!in_array($model, $treeModels)) {
$extractTreeData = false;
}
}
$this->trackDebug('2 tree:');
if ($extractTreeData) {
$this->trackDebug('2.1 roots:');
$this->export['destination']['byType']['ARRAY']['tree']['roots'] = $objects;
$this->trackDebug('2.2 sections:');
foreach ($objects as $parent) {
$filter = array(
'Section.*' => '',
'object_type_id' => $conf->objectTypes['section']['id']
);
$sections = $this->findObjects($parent, null, $this->export['status'], $filter, null, true, 1, null, true, array());
if (!empty($sections['items'])) {
foreach ($sections['items'] as $section) {
$sectionItem = array(
'id' => $section['id'],
'parent' => $section['parent_id'],
'priority_order' => $section['priority_order'],
'syndicate' => $section['syndicate']
);
if (!empty($section['priority'])) {
$sectionItem['priority'] = $section['priority'];
}
if ($section['menu'] == 0) {
$sectionItem['menu'] = $section['menu'];
}
$this->export['destination']['byType']['ARRAY']['tree']['sections'][] = $sectionItem;
$objModel = ClassRegistry::init('Section');
$objModel->contain(
$this->modelBinding('Section', $objModel)
);
$obj = $objModel->findById($section['id']);
$this->prepareObjectForExport($obj);
}
}
}
} else {
$this->trackDebug('... skip roots and sections');
}
$this->trackDebug('3 objects + 4 relations');
if (!empty($this->export['destination']['byType']['ARRAY']['tree']['roots'])) {
$this->prepareObjectsForExportByParents($this->export['destination']['byType']['ARRAY']['tree']['roots']);
}
if (!empty($this->export['destination']['byType']['ARRAY']['tree']['sections'])) {
$parents = Set::extract('/id',$this->export['destination']['byType']['ARRAY']['tree']['sections']);
$this->prepareObjectsForExportByParents($parents);
}
if ($this->export['all'] === true) {
$this->trackDebug('... extracting orphans (objects not in tree)');
$orphanIds = $this->orphans(array_keys($this->export['destination']['byType']['ARRAY']['objects']));
foreach ($orphanIds as $objId) {
$model = ClassRegistry::init('BEObject')->getType($objId);
$objModel = ClassRegistry::init($model);
$objModel->contain(
$this->modelBinding($model, $objModel)
);
$obj = $objModel->findById($objId);
if (!empty($obj)) {
$this->prepareObjectForExport($obj);
} else {
$this->trackDebug('... object ' . $objId . 'not found');
}
}
}
$uniqueRelationsMap = array();
$uniqueRelations = array();
$inverseRelations = array();
$allRelations = BeLib::getObject('BeConfigure')->mergeAllRelations();
foreach ($this->export['destination']['byType']['ARRAY']['relations'] as $switch => $relations) {
$inverse = (!empty($allRelations[$switch]['inverse'])) ? $allRelations[$switch]['inverse'] : NULL;
if (($inverse != $switch) && !in_array($inverse, $inverseRelations)) {
$inverseRelations[$inverse] = $inverse;
}
foreach ($relations as $relation) {
$key = $relation['idLeft'] . '-' . $relation['idRight'] . '-' . $switch;
if (empty($uniqueRelationsMap[$key])) {
$keyInv = $relation['idRight'] . '-' . $relation['idLeft'] . '-' . $switch;
if (empty($uniqueRelationsMap[$keyInv])) {
$uniqueRelationsMap[$key] = $key;
$uniqueRelationsMap[$keyInv] = $keyInv;
$uniqueRelations[$switch][] = $relation;
} else {
$this->trackDebug('... relation duplication ' . $keyInv . ' - removed');
}
} else {
$this->trackDebug('... relation duplication ' . $key . ' - removed');
}
}
}
foreach ($uniqueRelations as $switch => $r) {
if (in_array($switch, $inverseRelations)) {
unset($uniqueRelations[$switch]);
}
}
$this->export['destination']['byType']['ARRAY']['relations'] = $uniqueRelations;
$treeTypes = array('area', 'section');
if (!empty($this->export['destination']['byType']['ARRAY']['tree']['roots'])) {
foreach ($this->export['destination']['byType']['ARRAY']['objects'] as &$object) {
if (!in_array($object['objectType'], $treeTypes)) {
$object['parents'] = $this->parentsForObjId($object['id'], $this->export['destination']['byType']['ARRAY']['tree']['roots']);
}
}
}
$this->trackDebug('4 config');
$this->trackDebug('4.1 config.customProperties:');
if ($this->export['all'] === true) {
$this->trackDebug('... extracting all custom properties');
$p = array();
if (empty($this->export['types'])) {
$p = ClassRegistry::init('Property')->find(
'all', array(
'contain' => array('PropertyOption')
)
);
} else {
$p = ClassRegistry::init('Property')->find(
'all', array(
'conditions' => array('object_type_id' => $this->export['objectTypeIds']),
'contain' => array('PropertyOption')
)
);
}
if (!empty($p)) {
foreach ($p as $cproperty) {
$this->export['customProperties'][$cproperty['id']] = $cproperty;
unset($this->export['customProperties'][$cproperty['id']]['id']);
}
}
}
if (!empty($this->export['customProperties'])) {
$propertiesNew = array();
foreach ($this->export['customProperties'] as $property) {
$objectType = Configure::read('objectTypes.' . $property['object_type_id'] . '.name');
if (!$this->objectTypeAllowed($property['object_type_id'])) {
continue;
}
$propertyNew = array();
$propertyNew['name'] = $property['name'];
$propertyNew['objectType'] = $objectType;
if(!empty($property['property_type'])) {
$propertyNew['dataType'] = $property['property_type'];
}
if (!empty($property['multiple_choice'])) {
$propertyNew['multipleChoice'] = $property['multiple_choice'];
}
if (!empty($property['PropertyOption'])) {
$propertyNew['options'] = array();
foreach ($property['PropertyOption'] as $propertyOption) {
$propertyNew['options'][] = $propertyOption['property_option'];
}
}
$propertiesNew[] = $propertyNew;
}
foreach ($propertiesNew as &$cp) {
if (!empty($cp['id'])) {
unset($cp['id']);
}
}
$this->export['destination']['byType']['ARRAY']['config']['customProperties'] = $propertiesNew;
}
$this->trackDebug('5. media');
if (empty($this->export['no-media']) && !empty($this->export['media'])) {
$this->export['srcMediaRoot'] = Configure::read('mediaRoot');
if (!file_exists($this->export['srcMediaRoot'])) {
throw new BeditaException('srcMediaRoot folder "' . $this->export['srcMediaRoot'] . '" not found');
}
if (!file_exists($this->export['destMediaRoot'])) {
throw new BeditaException('destMediaRoot folder "' . $this->export['destMediaRoot'] . '" not found');
}
foreach ($this->export['media'] as $objectId => $uri) {
if (!empty($uri) && $uri[0] == '/') {
$this->copyFileToFolder($this->export['srcMediaRoot'], $this->export['destMediaRoot'], $uri);
$this->trackDebug('... saving ' . $this->export['destMediaRoot'] . $uri);
} else {
$this->trackDebug('remote uri: ' . $uri . ' not saved to filesystem');
}
}
}
if ($this->export['returnType'] === 'JSON') {
if (phpversion() >= '5.4') {
$this->export['destination']['byType']['JSON'] = json_encode($this->export['destination']['byType']['ARRAY'], JSON_PRETTY_PRINT | JSON_NUMERIC_CHECK);
} else {
$this->export['destination']['byType']['JSON'] = json_encode($this->export['destination']['byType']['ARRAY']);
}
if (!empty($this->export['filename'])) {
if (!file_put_contents($this->export['filename'], $this->export['destination']['byType']['JSON'])) {
throw new BeditaException('error saving data to file "' . $this->export['filename'] . '"');
}
}
} elseif ($this->export['returnType'] === 'XML') {
$this->export['destination']['byType']['XML'] = BeLib::getObject('XmlJsonConverter')->toXmlString($this->export['destination']['byType']['ARRAY']);
if (!empty($this->export['filename'])) {
if (!file_put_contents($this->export['filename'], $this->export['destination']['byType']['XML'])) {
throw new BeditaException('error saving data to file "' . $this->export['filename'] . '"');
}
}
}
$this->trackInfo('export OK');
} catch(Exception $e) {
$this->trackError('ERROR: ' . $e->getMessage());
}
$this->trackInfo('END');
$this->exportInfo();
return $this->export['destination']['byType'][$this->export['returnType']];
}
public function getResult() {
return $this->result;
}
public function validate(&$data, $options = array()) {
if (!is_array($data)) {
$this->import['source']['string'] = $data;
if (empty($this->import['source']['string'])) {
throw new BeditaException('empty string');
}
$this->import['source']['string'] = trim($this->import['source']['string']);
$type = (!empty($options['type'])) ? $options['type'] : 'JSON';
switch (strtoupper($type)) {
case 'XML':
$this->import['source']['data'] = BeLib::getObject('XmlJsonConverter')->toArray($this->import['source']['string']);
if (empty($this->import['source']['data'])) {
throw new BeditaException('xml string not valid: xml error ' . $this->xmlLastErrorMsg());
}
$this->import['source']['data'] = $this->import['source']['data']['bedita'];
break;
case 'JSON':
default:
$this->import['source']['data'] = json_decode($this->import['source']['string'], true);
if (empty($this->import['source']['data'])) {
throw new BeditaException('json string not valid: json_last_error error code ' . $this->jsonLastErrorMsg());
}
}
} else {
$this->import['source']['data'] = $data;
if (empty($this->import['source']['data'])) {
throw new BeditaException('empty input data');
}
}
$this->import['source']['data']['objects'] = Set::combine($this->import['source']['data'], 'objects.{n}.id', 'objects.{n}');
$this->import['objects']['ids'] = array_keys($this->import['source']['data']['objects']);
$this->import['media'] = array();
$this->import['config'] = array();
if (!empty($this->import['source']['data']['config'])) {
if (!empty($this->import['source']['data']['config']['customProperties'])) {
$this->import['config']['properties'] = array();
foreach ($this->import['source']['data']['config']['customProperties'] as $customProperty) {
if (empty($customProperty['name'])) {
throw new BeditaException('config.customProperties: missing name field');
}
if (empty($customProperty['objectType'])) {
throw new BeditaException('config.customProperties: missing objectType field');
}
if (empty($customProperty['dataType'])) {
throw new BeditaException('config.customProperties: missing dataType field');
}
$objTypeId = Configure::read('objectTypes.' . $customProperty['objectType'] . '.id');
if (empty($objTypeId)) {
throw new BeditaException('config.customProperties: objectType ' . $customProperty['objectType'] . ' not found');
}
if (!in_array($customProperty['dataType'],$this->customPropertyDataTypes)) {
throw new BeditaException('config.customProperties: dataType ' . $customProperty['dataType'] . ' not allowed | dataType should be number, date, text or options');
}
$propertyModel = ClassRegistry::init('Property');
$propertyModel->create();
$property = $propertyModel->find('first',
array(
'conditions' => array(
'name' => $customProperty['name'],
'object_type_id' => $objTypeId
)
)
);
if (!empty($property)) {
$this->import['config']['properties'][$customProperty['name']] = $property;
if ($property['object_type_id'] != $objTypeId) {
throw new BeditaException('config.customProperties.object_type_id "' . $objTypeId . '"" different from system custom property object_type_id "' . $property['object_type_id'] . '" for property ' . $customProperty['name']);
}
if ($property['property_type'] != $customProperty['dataType']) {
throw new BeditaException('config.customProperties.dataType "' . $customProperty['dataType'] . '" different from system custom property property_type "' . $property['property_type'] . '" for property ' . $customProperty['name']);
}
$multipleChoice = (!empty($property['multiple_choice']) && ($property['multiple_choice'] == true));
$multipleChoiceImport = (!empty($customProperty['multipleChoice']) && ($customProperty['multipleChoice'] == true));
if ($multipleChoice != $multipleChoiceImport) {
throw new BeditaException('config.customProperties.multipleChoice "' . $customProperty['multipleChoice'] . '"" different from system custom property multiple_choice "' . $property['multiple_choice'] . '" for property ' . $customProperty['name']);
} else if ($multipleChoice) {
if (empty($customProperty['options'])) {
throw new BeditaException('config.customProperties.options: empty options for multiple_choice property for property ' . $customProperty['name']);
}
$options = $customProperty['options'];
if (sizeof($options) != sizeof($property['PropertyOption'])) {
throw new BeditaException('config.customProperties.options: options differ from set of PropertyOption in the system property for property ' . $customProperty['name']);
}
$propertyOptions = array();
foreach ($property['PropertyOption'] as $propertyOption) {
if (!in_array($propertyOption['property_option'], $options)) {
throw new BeditaException('config.customProperties.options: options does not contain the system property PropertyOption "' . $propertyOption['property_option'] . '"" for property ' . $customProperty['name']);
}
$propertyOptions[] = $propertyOption['property_option'];
}
foreach ($options as $option) {
if (!in_array($option,$propertyOptions)) {
throw new BeditaException('config.customProperties.options: option "' . $option . '" not found in the system property set of PropertyOption for property ' . $customProperty['name']);
}
}
}
}
if (!in_array($customProperty['name'], array_keys($this->import['config']['properties']))) {
$this->import['config']['properties'][$customProperty['name']] = array();
}
}
}
}
if ( !empty($this->import['source']['data']) && !empty($this->import['source']['data']['objects'])) {
foreach ($this->import['source']['data']['objects'] as $object) {
if (!empty($object['customProperties'])) {
if (empty($this->import['config']['properties'])) {
throw new BeditaException('object.customProperties defined, but config.customProperties not defined');
}
foreach ($object['customProperties'] as $customProperty) {
if (empty($customProperty['name'])) {
throw new BeditaException('object.customProperties.name not found for object ' . $object['id']);
}
if (!in_array($customProperty['name'], array_keys($this->import['config']['properties']))) {
throw new BeditaException('object.customProperties.name "' . $customProperty['name'] . '" not defined in config.customProperties');
}
if (!isset($customProperty['value'])) {
throw new BeditaException('object.customProperties.value not found for object ' . $object['id']);
}
}
}
}
}
if (empty($this->import['source']['data']) || empty($this->import['source']['data']['objects'])) {
throw new BeditaException('empty objects set');
}
foreach ($this->objMinimalSet as $field) {
foreach ($this->import['source']['data']['objects'] as $object) {
if (empty($object[$field])) {
$objDesc = (!empty($object['id'])) ? $object['id'] : '';
throw new BeditaException('missing field ' . $field . ' for object ' . $objDesc);
}
}
}
$noPublicationAllowed = (!empty($options['parentId']));
$this->import['expectedParentIds'] = array();
$treeObjectTypes = array('area', 'section');
foreach ($this->import['source']['data']['objects'] as $object) {
if (!empty($object['id']) && !empty($object['objectType']) && in_array($object['objectType'], $treeObjectTypes)) {
$this->import['expectedParentIds'][] = $object['id'];
}
if (!in_array($object['objectType'], $this->import['objects']['types'])) {
$ot = Configure::read('objectTypes.' . $object['objectType'] . '.id');
if (empty($ot)) {
throw new BeditaException('missing objectType ' . $object['objectType']);
}
$this->import['objects']['types'][] = $object['objectType'];
}
$this->import['objects']['typeById'][$object['id']] = $object['objectType'];
if (!empty($object['id']) && !empty($object['uri'])) {
$this->import['media'][$object['id']]['uri'] = $object['uri'];
}
if ($noPublicationAllowed && !empty($object['objectType']) && ($object['objectType'] === 'area') ) {
throw new BeditaException('object area (id: ' . $object['id'] . ') not allowed when importing to destination');
}
if (!empty($object['categories'])) {
foreach ($object['categories'] as $category) {
if (empty($category['name'])) {
throw new BeditaException('missing category name for object id "' . $object['id'] . '"');
}
}
}
}
$noParents = true;
if (!empty($this->import['source']['data']['objects'])) {
foreach ($this->import['source']['data']['objects'] as $o) {
if (!empty($o['parents']) || !empty($o['parent_id'])) {
$noParents = false;
}
}
}
if (!$noParents) {
if (empty($this->import['source']['data']['tree'])) {
throw new BeditaException('missing tree in source json data');
}
if (empty($this->import['source']['data']['tree']['roots'])) {
throw new BeditaException('missing tree roots in source json data');
}
$rootIds = $this->import['source']['data']['tree']['roots'];
$this->import['tree']['roots'] = $rootIds;
$this->import['tree']['ids'] = $rootIds;
$this->import['tree']['parents'] = $rootIds;
$rootObjTypes = array();
foreach ($rootIds as $rootId) {
if (empty($this->import['source']['data']['objects'][$rootId])) {
throw new BeditaException('root id ' . $rootId . ' not referenced in objects');
}
$rootObjType = $this->import['source']['data']['objects'][$rootId]['objectType'];
if (empty($rootObjType)) {
throw new BeditaException('missing root object type for root id ' . $rootId);
}
if (empty($rootObjTypes)) {
$rootObjTypes[] = $rootObjType;
} else if(!in_array($rootObjType, $rootObjTypes)) {
$rootObjTypes[] = $rootObjType;
}
if (sizeof($rootObjTypes) > 1) {
throw new BeditaException('all tree roots elements must be of the same type (area|section)');
}
if ($rootObjType != 'area' && $rootObjType != 'section') {
throw new BeditaException('root object type [' . $rootObjType . '] not valid: must be area or section');
} else if ($rootObjType == 'section') {
if (empty($options['parentId'])) {
throw new BeditaException('missing $options[parentId] for root section');
}
}
}
$this->updateTreeForImport();
$orderedSections = array();
foreach ($this->import['treeLevels'] as $levelName => $sections) {
if ($levelName != 'level-0') {
foreach ($sections as $sectionId => $section) {
$orderedSections[$sectionId] = $section;
}
}
}
$this->import['source']['data']['tree']['sections'] = $orderedSections;
foreach ($this->import['source']['data']['tree']['sections'] as $section) {
$this->import['tree']['ids'][] = $section['id'];
if (!empty($section['parent']) && !in_array($section['parent'], $this->import['tree']['parents'])) {
$this->import['tree']['parents'][] = $section['parent'];
}
}
foreach ($this->import['tree']['parents'] as $parentId) {
if (!in_array($parentId, $this->import['tree']['ids'])) {
throw new BeditaException('parent id ' . $parentId . ' not found in tree');
}
}
foreach ($this->import['expectedParentIds'] as $elemId) {
if (!in_array($elemId, $this->import['tree']['ids'])) {
throw new BeditaException('element ' . $elemId . ' not found in specified tree source');
}
}
foreach ($this->import['tree']['ids'] as $treeId) {
if (!in_array($treeId, $this->import['objects']['ids'])) {
throw new BeditaException('tree id ' . $treeId . ' not found in objects');
}
}
}
if (!empty($this->import['source']['data']['relations'])) {
$relationKeys = array_keys($this->import['source']['data']['relations']);
$relationStructureOne = is_int($relationKeys[0]);
foreach ($this->relMinimalSet as $field) {
if ($relationStructureOne) {
foreach ($this->import['source']['data']['relations'] as $relation) {
if (empty($relation[$field])) {
throw new BeditaException('missing field ' . $field . ' for relation');
}
if ($field == 'idLeft' && !in_array($relation['idLeft'], $this->import['relations']['ids'])) {
$this->import['relations']['ids'][] = $relation['idLeft'];
}
if ($field == 'idRight' && !in_array($relation['idRight'], $this->import['relations']['ids'])) {
$this->import['relations']['ids'][] = $relation['idRight'];
}
if ($field == 'switch' && !in_array($relation['switch'], $this->import['relations']['switches'])) {
$this->import['relations']['switches'][] = $relation['switch'];
}
}
} else {
foreach ($this->import['source']['data']['relations'] as $relationName => &$relations) {
foreach ($relations as &$relation) {
if (empty($relation[$field])) {
$relation[$field] = $relationName;
}
if ($field == 'idLeft' && !in_array($relation['idLeft'], $this->import['relations']['ids'])) {
$this->import['relations']['ids'][] = $relation['idLeft'];
}
if ($field == 'idRight' && !in_array($relation['idRight'], $this->import['relations']['ids'])) {
$this->import['relations']['ids'][] = $relation['idRight'];
}
if ($field == 'switch' && !in_array($relation['switch'], $this->import['relations']['switches'])) {
$this->import['relations']['switches'][] = $relation['switch'];
}
}
}
}
}
foreach ($this->import['relations']['ids'] as $relId) {
if (!in_array($relId, $this->import['objects']['ids'])) {
throw new BeditaException('relation id (left/right) ' . $relId . ' not found in objects');
}
}
$allRelations = BeLib::getObject('BeConfigure')->mergeAllRelations();
foreach ($allRelations as $switch => $a) {
$this->import['allRelations'][$switch] = array(
'left' => $a['left'],
'right' => $a['right'],
'symmetric' => ( !array_key_exists('inverse', $a) || ($a['inverse'] == $switch) ),
'inverse' => array_key_exists('inverse', $a) ? $a['inverse'] : $switch,
);
if (!$this->import['allRelations'][$switch]['symmetric']) {
$this->import['allRelations'][$a['inverse']] = array(
'left' => $a['right'],
'right' => $a['left'],
'symmetric' => false,
'inverse' => $switch
);
}
}
$this->import['allRelationsKeys'] = array_keys($this->import['allRelations']);
foreach ($this->import['relations']['switches'] as $switch) {
if (!in_array($switch, $this->import['allRelationsKeys'])) {
$this->trackWarn('relation switch ' . $switch . ' not found in bedita relations');
}
}
if ($relationStructureOne) {
foreach ($this->import['source']['data']['relations'] as $relation) {
$objTypeLeft = $this->import['objects']['typeById'][$relation['idLeft']];
$objTypeRight = $this->import['objects']['typeById'][$relation['idRight']];
$switch = $relation['switch'];
if (!empty($this->import['allRelations'][$switch])) {
$symmetric = $this->import['allRelations'][$switch]['symmetric'];
$cdl = $this->relationAllowed($objTypeLeft, $this->import['allRelations'][$switch]['left']);
$cdr = $this->relationAllowed($objTypeRight, $this->import['allRelations'][$switch]['right']);
$cil = $this->relationAllowed($objTypeLeft, $this->import['allRelations'][$switch]['right']);
$cir = $this->relationAllowed($objTypeRight, $this->import['allRelations'][$switch]['left']);
if ( !($cdl && $cdr) && !($symmetric && ($cil && $cir)) ) {
throw new BeditaException('relation switch ' . $switch . ' object not allowed (idLeft: ' . $relation['idLeft'] . ', idRight: ' . $relation['idRight'] . ')');
}
}
}
} else {
foreach ($this->import['source']['data']['relations'] as $relationName => $rr) {
foreach ($rr as $r) {
$objTypeLeft = $this->import['objects']['typeById'][$r['idLeft']];
$objTypeRight = $this->import['objects']['typeById'][$r['idRight']];
if (!empty($this->import['allRelations'][$relationName])) {
$symmetric = $this->import['allRelations'][$relationName]['symmetric'];
$relationLeftEmpty = empty($this->import['allRelations'][$relationName]['left']);
$cdl = $this->relationAllowed($objTypeLeft, $this->import['allRelations'][$relationName]['left']);
$cdr = $this->relationAllowed($objTypeRight, $this->import['allRelations'][$relationName]['right']);
$cil = $this->relationAllowed($objTypeLeft, $this->import['allRelations'][$relationName]['right']);
$cir = $this->relationAllowed($objTypeRight, $this->import['allRelations'][$relationName]['left']);
if (!$relationLeftEmpty) {
if ( !($cdl && $cdr) && !($symmetric && ($cil && $cir)) ) {
$this->trackWarn('relation switch ' . $relationName . ' object not allowed (idLeft: ' . $r['idLeft'] . ', idRight: ' . $r['idRight'] . ')');
}
}
}
}
}
}
}
if (!empty($this->import['media'])) {
if (!empty($this->import['sourceMediaUri'])) {
if (!$this->urlExists($this->import['sourceMediaUri'])) {
throw new BeditaException('sourceMediaUri url "' . $this->import['sourceMediaUri'] . '" not found');
}
$this->import['source']['media']['root'] = $this->import['sourceMediaUri'];
$this->import['source']['media']['isUrl'] = true;
} else {
if (!empty($this->import['sourceMediaRoot']) && !file_exists($this->import['sourceMediaRoot'])) {
throw new BeditaException('sourceMediaRoot folder "' . $this->import['sourceMediaRoot'] . '" not found');
}
$this->import['source']['media']['root'] = $this->import['sourceMediaRoot'];
$this->import['source']['media']['isUrl'] = false;
}
if (!$this->import['source']['media']['isUrl']) {
$folder =& new Folder($this->import['source']['media']['root'], true);
$this->import['source']['media']['size'] = $folder->dirSize();
}
$this->import['destination']['media']['root'] = Configure::read('mediaRoot');
if (!file_exists($this->import['destination']['media']['root'])) {
if (!mkdir($this->import['destination']['media']['root'])) {
throw new BeditaException('destination folder "' . $this->import['destination']['media']['root'] . '" not found: failure on creating it');
}
}
$this->import['destination']['media']['space'] = disk_free_space($this->import['destination']['media']['root']);
if (!$this->import['source']['media']['isUrl']) {
foreach ($this->import['media'] as $id => &$media) {
if (!empty($media['uri']) && $media['uri'][0] == '/') {
$filePath = $this->import['sourceMediaRoot'] . $media['uri'];
if (!file_exists($filePath)) {
$this->trackWarn('file "' . $filePath . '" not found (object id "' . $id . '")');
} else {
$media['base'] = $this->import['sourceMediaRoot'];
$media['full'] = $filePath;
}
}
}
if ($this->import['source']['media']['size'] >= $this->import['destination']['media']['space']) {
throw new BeditaException('not enought space on destination folder "' . $this->import['destination']['media']['root'] . '" - space required: ' . $this->import['source']['media']['size'] . ' / space available: ' . $this->import['destination']['media']['space']);
}
} else {
foreach ($this->import['media'] as $id => &$media) {
if (!empty($media['uri']) && $media['uri'][0] == '/') {
$fileUri = $this->import['sourceMediaUri'] . $media['uri'];
if (!$this->urlExists($fileUri)) {
$this->trackWarn('file "' . $fileUri . '" not found (object id "' . $id . '")');
} else {
$media['base'] = $this->import['sourceMediaUri'];
$media['full'] = $fileUri;
}
}
}
if (!empty($this->import['source']['media']['size']) && $this->import['source']['media']['size'] >= $this->import['destination']['media']['space']) {
throw new BeditaException('not enought space on destination folder "' . $this->import['destination']['media']['root'] . '" - space required: ' . $this->import['source']['media']['size'] . ' / space available: ' . $this->import['destination']['media']['space']);
}
}
}
}
private function relationAllowed($objType, array $relType) {
return empty($relType) || in_array($objType, $relType);
}
private function saveProperty($customProperty) {
$mode = $this->import['saveMode'];
$this->trackDebug('- saving property ' . $customProperty['name'] . ' with mode ' . $mode);
$model = ClassRegistry::init('Property');
$model->create();
$objTypeId = Configure::read('objectTypes.' . $customProperty['objectType'] . '.id');
$property = $model->find('first',
array(
'conditions' => array(
'name' => $customProperty['name'],
'object_type_id' => $objTypeId
)
)
);
if (empty($property)) {
$propertyData = array(
'name' => $customProperty['name'],
'property_type' => $customProperty['dataType'],
'object_type_id' => $objTypeId
);
if (!empty($customProperty['multipleChoice'])) {
$propertyData['multiple_choice'] = $customProperty['multipleChoice'];
}
if(!$model->save($propertyData)) {
throw new BeditaException('error saving property (import name ' . $customProperty['name'] . ')');
}
$propertyData['id'] = $model->id;
if (!empty($customProperty['options'])) {
$propertyOptions = array();
$this->trackDebug('- saving property options for property "' . $customProperty['name'] . '"');
$model->PropertyOption->deleteAll("property_id='" . $propertyData['id'] . "'");
$options = $customProperty['options'];
foreach ($options as $option) {
$propertyOptions[] = array(
'property_id' => $propertyData['id'],
'property_option' => trim($option)
);
}
if (!$model->PropertyOption->saveAll($propertyOptions)) {
throw new BeditaException('error saving property options (property import name ' . $customProperty['name'] . ')');
}
}
$this->import['config']['properties'][$customProperty['name']] = $propertyData;
$this->import['saveMap']['property'][$customProperty['name']][] = $propertyData['id'];
$this->trackDebug('- saving property ' . $customProperty['name'] . ' with BEdita Property id ' . $propertyData['id'] . ' ... END');
} else {
$this->trackDebug('- property "' . $customProperty['name'] . '" found: skip saving [TODO: update?]');
}
}
private function saveArea($area) {
$mode = $this->import['saveMode'];
$this->trackDebug('- saving area ' . $area['id'] . ' with mode ' . $mode . ' ... START');
$newArea = array_merge($this->objDefaults, $this->import['source']['data']['objects'][$area['id']]);
if (!isset($area['menu'])) {
$newArea['menu'] = '1';
} else {
$newArea['menu'] = $area['menu'];
}
unset($newArea['id']);
$model = ClassRegistry::init('Area');
$model->create();
if (!$model->save($newArea)) {
throw new BeditaException('error saving area (import id ' . $area['id'] . ')');
}
$this->import['saveMap'][$area['id']] = $model->id;
$this->trackDebug('- saving area ' . $area['id'] . ' with BEdita Area id ' . $model->id . ' ... END');
}
private function saveSection($section, $parendId = null) {
if (!empty($this->import['saveMap'][$section['id']])) {
$this->trackDebug('-- section ' . $section['id'] . ' already saved with BEdita id ' . $this->import['saveMap'][$section['id']]);
} else {
$mode = $this->import['saveMode'];
$this->trackDebug('-- saving section ' . $section['id'] . ' with mode ' . $mode . ' ... START');
$newSection = array_merge($this->objDefaults, $this->import['source']['data']['objects'][$section['id']]);
if (!isset($section['menu'])) {
$newSection['menu'] = '1';
} else {
$newSection['menu'] = $section['menu'];
}
unset($newSection['id']);
$newSection['parent_id'] = ($parendId != null) ? $parendId : $this->import['saveMap'][$section['parent']];
$model = ClassRegistry::init('Section');
$model->create();
if (!$model->save($newSection)) {
throw new BeditaException('error saving section (import id ' . $section['id'] . ')');
}
if (!empty($section['priority'])) {
$tree = ClassRegistry::init('Tree');
$tree->setPriority($model->id, $section['priority'], $newSection['parent_id']);
}
$this->import['saveMap'][$section['id']] = $model->id;
$this->trackDebug('-- saving section ' . $section['id'] . ' with BEdita Section id ' . $model->id . ' ... END');
}
}
private function saveObject($object) {
if (!empty($this->import['saveMap'][$object['id']])) {
$this->trackDebug($object['objectType'] . ' ' . $object['id'] . ' already saved with BEdita id ' . $this->import['saveMap'][$object['id']]);
} else {
$this->trackDebug('2.3.2 save object with policy \'NEW\'');
$this->trackDebug('2.3.3 save object with other policies [TODO]');
$mode = $this->import['saveMode'];
$this->trackDebug('- saving object ' . $object['id'] . ' with mode ' . $mode . ' ... START');
$object['Category'] = array();
if (!empty($object['categories'])) {
$this->trackDebug('2.3.4 save object.categories');
foreach ($object['categories'] as $category) {
$object['Category'] = $this->saveCategory($category['name'], $object['objectType']);
}
}
if (!empty($object['tags'])) {
$this->trackDebug('2.3.5 save object.tags');
$tagListString = '';
foreach ($object['tags'] as $tag) {
$tagListString.= $tag . ',';
}
if (!empty($tagListString)) {
$tagListString = substr($tagListString, 0, strlen($tagListString)-1);
$tags = $this->saveTags($tagListString);
$object['Category'] = array_merge($object['Category'], $tags);
}
}
$newObject = array_merge($this->objDefaults, $object);
unset($newObject['id']);
$model = ClassRegistry::init(Inflector::camelize($object['objectType']));
$model->create();
if (!$model->save($newObject)) {
throw new BeditaException('error saving ' . $object['objectType'] . ' (import id ' . $object['id'] . ')');
}
if (!empty($object['customProperties'])) {
$this->trackDebug('2.3.6 save object.customProperties');
$this->trackDebug('- saving custom properties for ' . $object['objectType'] . ' ' . $object['id'] . ' with BEdita id ' . $model->id);
$object['ObjectProperty'] = array();
foreach ($object['customProperties'] as $customProperty) {
$property = array(
'object_id' => $model->id,
'property_id' => $this->import['config']['properties'][$customProperty['name']]['id'],
'property_value' => $customProperty['value']
);
$object['ObjectProperty'][] = $property;
}
foreach ($object['ObjectProperty'] as $objectProperty) {
$objectPropertyModel = ClassRegistry::init('ObjectProperty');
$objectPropertyModel->create();
if (!$objectPropertyModel->save($objectProperty)) {
throw new BeditaException('error saving ObjectProperty for ' . $object['objectType'] . ' (import id ' . $object['id'] . ')');
}
}
}
if (!empty($object['LangText'])) {
$this->trackDebug('2.3.7 save object.langTexts');
$this->trackDebug('- saving lang texts for ' . $object['objectType'] . ' ' . $object['id'] . ' with BEdita id ' . $model->id);
$langTexts = $object['LangText'];
$object['LangText'] = array();
$langTextModel = ClassRegistry::init('LangText');
foreach ($langTexts as $lang => $fields) {
foreach ($fields as $name => $text) {
$langTxt = array(
'object_id' => $model->id,
'lang' => $lang,
'name' => $name,
'text' => $text,
);
$object['LangText'] = $langTxt;
$langTextModel->create();
if (!$langTextModel->save($langTxt)) {
throw new BeditaException('error saving LangText for ' . $object['objectType'] . ' (import id ' . $object['id'] . ')');
}
}
}
}
$this->import['saveMap'][$object['id']] = $model->id;
$this->trackDebug('- saving ' . $object['objectType'] . ' ' . $object['id'] . ' with BEdita id ' . $model->id . ' ... object saved');
if (!empty($object['parents'])) {
$tree = ClassRegistry::init('Tree');
foreach ($object['parents'] as $parent) {
$parentId = $parent['id'];
if (!empty($this->import['saveMap'][$parentId])) {
$beParentId = $this->import['saveMap'][$parentId];
$this->trackDebug('-- saving tree record for ' . $object['objectType'] . ' ' . $object['id'] . ' (BEdita id ' . $model->id . ') - (position - import parent id ' . $parentId . ' / BEdita parent id ' . $beParentId . ') ... START');
$tree->appendChild($model->id, $beParentId);
if (!empty($parent['priority'])) {
$tree->setPriority($model->id, $parent['priority'], $beParentId);
}
$this->trackDebug('-- saving tree record for ' . $object['objectType'] . ' ' . $object['id'] . ' (BEdita id ' . $model->id . ') - (position - import parent id ' . $parentId . ' / BEdita parent id ' . $beParentId . ') ... END');
} else {
$this->trackDebug('-- bedita object not found in import saveMap for id ' . $parentId);
}
}
} else {
$this->trackDebug('-- empty tree record for ' . $object['objectType'] . ' ' . $object['id'] . ' with BEdita id ' . $model->id . ' ...');
}
$this->trackDebug('- saving object ' . $object['id'] . ' with BEdita id ' . $model->id . ' ... END');
}
}
private function saveCategory($categoryName, $objectType) {
$categoryModel = ClassRegistry::init('Category');
$objectTypeId = Configure::read("objectTypes.$objectType.id");
$categories = array( $categoryName );
return $categoryModel->findCreateCategories($categories, $objectTypeId);
}
private function saveTags($tagListString) {
$categoryModel = ClassRegistry::init('Category');
return $categoryModel->saveTagList($tagListString);
}
private function saveRelation($relation, $counter) {
$this->trackDebug('- saving relation ' . $counter . ': ' . $relation['switch'] . ' ... START');
$relationData = array(
'id' => $this->import['saveMap'][$relation['idLeft']],
'objectId' => $this->import['saveMap'][$relation['idRight']],
'switch' => $relation['switch'],
'inverse' => NULL,
'priority' => NULL,
'params' => array()
);
if (!empty($this->import['allRelations'][$relation['switch']]['inverse'])) {
$relation['inverse'] = $this->import['allRelations'][$relation['switch']]['inverse'];
}
if (!empty($relation['priority'])) {
$relationData['priority'] = $relation['priority'];
}
if (!empty($relation['params'])) {
$relationData['params'] = $relation['params'];
}
$objRelModel = ClassRegistry::init('ObjectRelation');
if ($objRelModel->relationExists($relationData['id'], $relationData['objectId'], $relationData['switch'])) {
$this->trackDebug('- relation exists - SKIP');
} else {
if ($relationData['inverse'] === $relationData['switch']) {
if ($objRelModel->relationExists($relationData['objectId'], $relationData['id'], $relationData['switch'])) {
$this->trackDebug('- inverse relation exists - SKIP');
} else {
if (!@$objRelModel->createRelation(
$relationData['id'],
$relationData['objectId'],
$relationData['switch'],
$relationData['priority'],
true,
$relationData['params']) ) {
throw new BeditaException('Error saving relation ' . $relation['switch'] . ' idLeft ' . $relation['idLeft'] . ' idRight ' . $relation['idRight'] );
}
}
} else {
if (!@$objRelModel->createRelationAndInverse(
$relationData['id'],
$relationData['objectId'],
$relationData['switch'],
$relationData['inverse'],
$relationData['priority'],
$relationData['params']) ) {
throw new BeditaException('Error saving relation ' . $relation['switch'] . ' idLeft ' . $relation['idLeft'] . ' idRight ' . $relation['idRight'] );
}
}
}
$this->import['saveMap']['relations'][] = $relationData;
$this->trackDebug('- saving relation ' . $counter . ': ' . $relation['switch'] . ' ... DONE');
}
private function updateTreeForImport($level = 0) {
if ($level === 0) {
$this->import['treeLevels'] = array();
$this->import['treeLevels']['level-0'] = array();
foreach ($this->import['source']['data']['tree']['roots'] as $rootId) {
$this->import['treeLevels']['level-0'][$rootId] = array('id' => $rootId);
};
}
if (!empty($this->import['treeLevels']['level-' . $level])) {
$nextLevel = $level + 1;
foreach ($this->import['treeLevels']['level-' . $level] as $parent => $data) {
$orderByPriority = false;
if (!empty($this->import['source']['data']['tree']['sections'])) {
foreach ($this->import['source']['data']['tree']['sections'] as $section) {
if ($section['parent'] == $parent) {
$this->import['treeLevels']['level-' . $nextLevel][$section['id']] = $section;
if (!empty($section['priority'])) {
$orderByPriority = true;
}
}
}
}
if ($orderByPriority) {
usort($this->import['treeLevels']['level-' . $level], function ($item1, $item2) {
if (empty($item1['priority'])) {
$item1['priority'] = 0;
}
if (empty($item2['priority'])) {
$item2['priority'] = 0;
}
return ($item1['priority'] === $item2['priority']) ? 0 : ($item1['priority'] > $item2['priority']);
});
$this->import['treeLevels']['level-' . $level] = Set::combine($this->import['treeLevels']['level-' . $level], '{n}.id', '{n}');
}
}
$this->updateTreeForImport($level+1);
}
}
private function cleanObjectFields(array &$object) {
foreach ($this->export['objectUnsetFields'] as $unsetKey) {
if (isset($object[$unsetKey])) {
unset($object[$unsetKey]);
}
}
foreach ($object as $key => $value) {
if (empty($value)) {
unset($object[$key]);
}
}
if (!empty($this->export['xmlEntity']) && ($this->export['xmlEntity'] === true) && (!empty($object['body']))) {
$object['body'] = $this->convertXmlEntity($object['body']);
}
}
private function rearrangeObjectFields(array &$object, $level) {
if (isset($object['RelatedObject']) && $level < $this->maxRelationLevels) {
foreach ($object['RelatedObject'] as $relation) {
$relationObjectTypeId = $this->objectTypeId($relation['object_id']);
if (!$this->objectTypeAllowed($relationObjectTypeId)) {
continue;
}
if ($this->export['relations'] == NULL || in_array($relation['switch'], $this->export['relations'])) {
if (empty($this->export['destination']['byType']['ARRAY']['objects'][$relation['object_id']])) {
$object['relatedObjectIds'][] = $relation['object_id'];
}
if (!in_array($relation['switch'], array_keys($this->export['destination']['byType']['ARRAY']['relations']))) {
$this->export['destination']['byType']['ARRAY']['relations'][$relation['switch']] = array();
}
$r = array(
'idLeft' => $relation['id'],
'idRight' => $relation['object_id'],
'priority' => $relation['priority']
);
if (!empty($relation['params'])) {
$r['params'] = $relation['params'];
}
$this->export['destination']['byType']['ARRAY']['relations'][$relation['switch']][] = $r;
}
}
}
unset($object['RelatedObject']);
if (isset($object['LangText'])) {
$langTexts = array();
foreach ($object['LangText'] as $name => $langTxt) {
if (is_numeric($name) || in_array($name, array('created_by', 'modified_by'))) {
continue;
}
foreach ($langTxt as $lang => $text) {
if (!array_key_exists($lang, $langTexts)) {
$langTexts[$lang] = array();
}
if (is_numeric($text) && in_array($name, array('created_on', 'modified_on'))) {
$text = date('Y-m-d H:i:s', $text);
}
$langTexts[$lang][$name] = $text;
}
}
$object['LangText'] = $langTexts;
}
if (isset($object['GeoTag'])) {
foreach ($object['GeoTag'] as &$geoTag) {
if (isset($geoTag['id'])) {
unset($geoTag['id']);
}
if (isset($geoTag['object_id'])) {
unset($geoTag['object_id']);
}
}
}
if (isset($object['DateItem'])) {
foreach ($object['DateItem'] as &$dateItem) {
if (isset($dateItem['id'])) {
unset($dateItem['id']);
}
if (isset($dateItem['object_id'])) {
unset($dateItem['object_id']);
}
}
}
if (isset($object['ObjectProperty'])) {
foreach ($object['ObjectProperty'] as $cproperty) {
$this->export['customProperties'][$cproperty['id']] = $cproperty;
}
unset($object['ObjectProperty']);
}
if (isset($object['customProperties'])) {
$cproperties = array();
foreach ($object['customProperties'] as $cplabel => $cpvalue) {
$cproperty = array(
'name' => $cplabel
);
if (!is_array($cpvalue)) {
$cproperty['value'] = $cpvalue;
} else {
if (sizeof($cpvalue) == 1) {
$cproperty['value'] = $cpvalue[0];
} else {
$cproperty['value'] = $cpvalue;
}
}
$cproperties[] = $cproperty;
}
$object['customProperties'] = $cproperties;
}
if (isset($object['Category'])) {
$categories = array();
foreach ($object['Category'] as $category) {
$c = array(
'name' => $category['name']
);
if (!empty($category['label'])) {
$c['label'] = $category['label'];
}
$categories[] = $c;
}
$object['categories'] = $categories;
unset($object['Category']);
}
if (isset($object['Tag'])) {
$tags = array();
foreach ($object['Tag'] as $tag) {
$tags[] = (!empty($tag['label'])) ? $tag['label'] : $tag['name'];
}
$object['tags'] = $tags;
unset($object['Tag']);
}
}
private function prepareObjectForExport(array &$object, $level = 0) {
$this->trackDebug('... prepareObjectForExport for object id ' . $object['id']);
if (!empty($object['object_type_id'])) {
$object['objectType'] = Configure::read('objectTypes.' . $object['object_type_id'] . '.name');
if (!$this->objectTypeAllowed($object['object_type_id'])) {
return;
}
}
$this->trackDebug('... cleanObjectFields for object id ' . $object['id']);
$this->cleanObjectFields($object);
$this->trackDebug('... rearrangeObjectFields for object id ' . $object['id']);
$this->rearrangeObjectFields($object, $level);
$relatedObjectIds = array();
if (!empty($object['relatedObjectIds'])) {
$relatedObjectIds = $object['relatedObjectIds'];
unset($object['relatedObjectIds']);
}
$this->export['destination']['byType']['ARRAY']['objects'][$object['id']] = $object;
if ($level < $this->maxRelationLevels) {
$nextLevel = $level + 1;
$this->trackDebug('... load related objects');
if (!empty($relatedObjectIds)) {
$conf = Configure::getInstance();
foreach ($relatedObjectIds as $relatedObjectId) {
if (!empty($this->export['destination']['byType']['ARRAY']['objects'][$relatedObjectId])) {
$this->trackResult('WARN', 'object id: ' . $relatedObjectId . ' already exported');
continue;
}
$objectTypeId = $this->objectTypeId($relatedObjectId);
if (!$this->objectTypeAllowed($objectTypeId)) {
continue;
}
if (isset($conf->objectTypes[$objectTypeId])) {
$model = $conf->objectTypes[$objectTypeId]['model'];
} else if (isset($conf->objectTypesExt[$objectTypeId])) {
$model = $conf->objectTypesExt[$objectTypeId]['model'];
} else {
throw new BeditaException('Model not found for object type Id "' . $objectTypeId . '"');
}
if ($model === 'Section' || $model === 'Area') {
$this->trackResult('WARN', 'unable to export related type: ' . $model . ' tree info may be missing');
continue;
}
$relatedObjModel = ClassRegistry::init($model);
$relatedObjModel->contain(
$this->modelBinding($model, $relatedObjModel)
);
$relatedObj = $relatedObjModel->findById($relatedObjectId);
$this->prepareObjectForExport($relatedObj, $nextLevel);
}
}
} else {
$this->trackDebug('... related objects not loaded - recursion level ' . $level);
}
if (!empty($object['uri'])) {
$this->export['media'][$object['id']] = $object['uri'];
}
}
private function parentsForObjId($objId, $rootIds) {
$tree = ClassRegistry::init('Tree');
$parents = $tree->find('all',
array(
'fields' => array(
'parent_id',
'priority',
'menu'
),
'conditions' => array(
'Tree.id' => $objId,
'Tree.parent_id' => $rootIds
)
)
);
$result = array();
if (!empty($parents)) {
foreach ($parents as $k => $v) {
$r = array(
'id' => $v['Tree']['parent_id'],
'priority' => $v['Tree']['priority']
);
if ($v['Tree']['menu'] != 0) {
$r['menu'] = $v['Tree']['menu'];
}
$result[] = $r;
}
}
$parents = $tree->find('all',
array(
'fields' => array(
'parent_id',
'priority',
'menu'
),
'conditions' => array(
'Tree.id' => $objId,
'Tree.area_id' => $rootIds
)
)
);
if (!empty($parents)) {
foreach ($parents as $k => $v) {
$r = array(
'id' => $v['Tree']['parent_id'],
'priority' => $v['Tree']['priority']
);
if ($v['Tree']['menu'] != 0) {
$r['menu'] = $v['Tree']['menu'];
}
$result[] = $r;
}
}
return $result;
}
private function prepareObjectsForExportByParents($parents) {
$tree = ClassRegistry::init('Tree');
$tree->bindModel(
array('belongsTo' => array('BEObject' => array('foreignKey' => 'id'))),
false
);
$conf = Configure::getInstance();
foreach ($parents as $parentId) {
$this->trackDebug('... extracting objects inside rootId ' . $parentId);
$conditions = array(
'parent_id' => $parentId,
'status' => $this->export['status'],
'NOT' => array(
'object_type_id' => array(
Configure::read('objectTypes.area.id'),
Configure::read('objectTypes.section.id')
)
)
);
if (!empty($this->export['objectTypeIds'])) {
$conditions['object_type_id'] = $this->export['objectTypeIds'];
}
$children = $tree->find('all',
array(
'fields' => array('Tree.id', 'BEObject.object_type_id'),
'conditions' => $conditions
)
);
if (!empty($children)) {
foreach ($children as $child) {
$objectId = $child['Tree']['id'];
$objectTypeId = $child['BEObject']['object_type_id'];
if (isset($conf->objectTypes[$objectTypeId])) {
$model = $conf->objectTypes[$objectTypeId]['model'];
} else {
$model = $conf->objectTypesExt[$objectTypeId]['model'];
}
$objModel = ClassRegistry::init($model);
$objModel->contain(
$this->modelBinding($model, $objModel)
);
$obj = $objModel->findById($objectId);
$this->prepareObjectForExport($obj);
}
}
}
$tree->unbindModel(array('belongsTo' => array('BEObject')));
}
private function objectTypeAllowed($objectTypeId) {
if ($this->export['types'] != NULL && !empty($this->export['exclude-other-types'])) {
if (!in_array($objectTypeId, $this->export['objectTypeIds'])) {
if (!empty($this->export['relatedObjectTypeIds'])) {
if (in_array($objectTypeId, $this->export['relatedObjectTypeIds'])) {
return true;
}
}
$ot = Configure::read('objectTypes.' . $objectTypeId . '.name');;
$this->trackInfo('Object type "' . $ot . '" not allowed "');
return false;
}
}
return true;
}
private function objectTypeId($objId) {
$objModel = ClassRegistry::init('BEObject');
return $objModel->findObjectTypeId($objId);
}
private function orphans($objsToSkip = array()) {
$treeModel = ClassRegistry::init('Tree');
$objsInTree = $treeModel->find('list', array(
'fields' => array('id')
));
$objsInTree = array_values($objsInTree);
$objsInTree = array_merge($objsToSkip);
$conditions = array(
'status' => $this->export['status'],
'NOT' => array('BEObject.id' => $objsInTree)
);
if (!empty($this->export['types'])) {
$conditions['object_type_id'] = $this->export['objectTypeIds'];
}
$objModel = ClassRegistry::init('BEObject');
$result = $objModel->find('list', array(
'fields' => array('id'),
'conditions' => $conditions
));
return $result;
}
private function modelBinding($model, BEAppModel $objModel) {
try {
return $objModel->containLevel('detailed');
} catch (Exception $e) {
return $this->export[in_array($model, $this->streamModels) ? 'contain-stream' : 'contain'];
}
}
private function copyFileToFolder($sourceBasePath, $destBasePath, $source) {
$tmp = explode(DS, $source);
$dirs = array();
foreach($tmp as $dir) {
if (!empty($dir)) {
$dirs[] = $dir;
}
}
$name = array_pop($dirs);
$dirsString = "";
foreach ($dirs as $dir) {
$dirsString.= DS . $dir;
}
$pointPosition = strrpos($name,".");
$filename = $tmpname = substr($name, 0, $pointPosition);
$ext = substr($name, $pointPosition);
$counter = 1;
$d = $destBasePath;
$dirs = array_reverse($dirs);
while (($current = array_pop($dirs))) {
$d.= DS . $current;
if (!file_exists($d) && !is_dir($d)) {
if (!mkdir($d)) {
throw new BeditaException('Error creating dir "' . $current . '"');
}
}
}
$name = $filename . $ext;
$destination = $destBasePath . $dirsString . DS . $name;
if (!@copy($sourceBasePath . $source, $destination)) {
$this->trackError('Error copying file "' . $sourceBasePath . DS . $source . '" to "' . $destination);
}
}
private function urlExists($url) {
$headers = @get_headers($url);
return !strpos($headers[0], '404');
}
private function trackError($message) {
$this->trackResult('ERROR', $message);
}
private function trackWarn($message) {
$this->trackResult('WARN', $message);
}
private function trackInfo($message) {
$this->trackResult('INFO', $message);
}
private function trackDebug($message) {
$this->trackResult('DEBUG', $message);
}
private function trackResult($level = 'INFO', $message) {
$this->result['log'][$level][] = $message;
$this->result['log']['ALL'][] = $level . ': ' . $message;
if ($this->logLevels[$level] <= $this->logLevel) {
$this->result['log']['filtered'][] = $message;
$this->log($level . ': ' . $message, $this->logFile);
if ($level == 'ERROR') {
$this->log('DataTransfer: ' . $message, 'error');
}
}
}
private function jsonLastErrorMsg() {
$msg = '';
switch (json_last_error()) {
case JSON_ERROR_NONE:
$msg = ' - No errors';
break;
case JSON_ERROR_DEPTH:
$msg = ' - Maximum stack depth exceeded';
break;
case JSON_ERROR_STATE_MISMATCH:
$msg = ' - Underflow or the modes mismatch';
break;
case JSON_ERROR_CTRL_CHAR:
$msg = ' - Unexpected control character found';
break;
case JSON_ERROR_SYNTAX:
$msg = ' - Syntax error, malformed JSON';
break;
case JSON_ERROR_UTF8:
$msg = ' - Malformed UTF-8 characters, possibly incorrectly encoded';
break;
default:
$msg = ' - Unknown error';
break;
}
return $msg;
}
private function xmlLastErrorMsg() {
$msg = '';
$err = libxml_get_last_error();
switch ($err->level) {
case LIBXML_ERR_WARNING:
$msg .= " - Warning {$err->code}";
break;
case LIBXML_ERR_ERROR:
$msg .= " - Error {$err->code}";
break;
case LIBXML_ERR_FATAL:
$msg .= " - Fatal Error {$err->code}";
break;
}
$msg .= " (line {$err->line}; column {$err->column}): " . trim($err->message);
libxml_clear_errors();
return implode(PHP_EOL, $msg);
}
private function exportInfo() {
if (!empty($this->export['filename'])) {
$this->trackInfo('file created: ' . $this->export['filename']);
$this->result['filename'] = $this->export['filename'];
}
$objects = $this->export['destination']['byType']['ARRAY']['objects'];
$this->result['objects'] = sizeof($objects);
$this->trackInfo('objects exported: ' . $this->result['objects']);
$objTypeCounter = array();
foreach ($objects as $o) {
if (empty($objTypeCounter[$o['objectType']])) {
$objTypeCounter[$o['objectType']] = 0;
}
$objTypeCounter[$o['objectType']]++;
}
foreach ($objTypeCounter as $objType => $count) {
$this->result['type'][$objType] = $count;
$this->trackInfo($objType . ': ' . $count);
}
$relations = $this->export['destination']['byType']['ARRAY']['relations'];
if (!empty($relations)) {
$this->trackInfo('relations exported ...');
foreach ($relations as $switch => $r) {
$this->trackInfo($switch . ': ' . sizeof($r));
$this->result['relations'][$switch] = sizeof($r);
}
} else {
$this->trackInfo('relations exported: none');
}
}
private function importInfo() {
$this->result['objects'] = sizeof($this->import['saveMap']);
$this->trackInfo('objects imported: ' . $this->result['objects']);
$objTypeCounter = array();
$objects = $this->import['source']['data']['objects'];
foreach($objects as $object) {
if (in_array($object['id'], array_keys($this->import['saveMap']))) {
if (empty($objTypeCounter[$object['objectType']])) {
$objTypeCounter[$object['objectType']] = 0;
}
$objTypeCounter[$object['objectType']]++;
}
}
foreach ($objTypeCounter as $objType => $count) {
$this->result['type'][$objType] = $count;
$this->trackInfo($objType . ': ' . $count);
}
$relationCounter = array();
if (!empty($this->import['saveMap']['relations'])) {
$relations = $this->import['saveMap']['relations'];
$this->trackInfo('relations imported ...');
foreach ($relations as $r) {
if (empty($relationCounter[$r['switch']])) {
$relationCounter[$r['switch']] = 0;
}
$relationCounter[$r['switch']]++;
}
foreach ($relationCounter as $relName => $count) {
$this->trackInfo($relName . ': ' . $count);
$this->result['relations'][$relName] = $count;
}
} else {
$this->trackInfo('relations imported: none');
}
}
private function convertXmlEntity($text) {
static $table = array('"' => '"','&' => '&','<' => '<','>' => '>','Œ' => 'Œ','œ' => 'œ','Š' => 'Š',
'š' => 'š','Ÿ' => 'Ÿ','ˆ' => 'ˆ','˜' => '˜',' ' => ' ',' ' => ' ',
' ' => ' ','‌' => '‌','‍' => '‍','‎' => '‎','‏' => '‏','–' => '–',
'—' => '—','‘' => '‘','’' => '’','‚' => '‚','“' => '“','”' => '”',
'„' => '„','†' => '†','‡' => '‡','‰' => '‰','‹' => '‹','›' => '›',
'€' => '€','ƒ' => 'ƒ','Α' => 'Α','Β' => 'Β','Γ' => 'Γ','Δ' => 'Δ','Ε' => 'Ε',
'Ζ' => 'Ζ','Η' => 'Η','Θ' => 'Θ','Ι' => 'Ι','Κ' => 'Κ','Λ' => 'Λ','Μ' => 'Μ',
'Ν' => 'Ν','Ξ' => 'Ξ','Ο' => 'Ο','Π' => 'Π','Ρ' => 'Ρ','Σ' => 'Σ','Τ' => 'Τ','Υ' => 'Υ',
'Φ' => 'Φ','Χ' => 'Χ','Ψ' => 'Ψ','Ω' => 'Ω','α' => 'α','β' => 'β','γ' => 'γ','δ' => 'δ',
'ε' => 'ε','ζ' => 'ζ','η' => 'η','θ' => 'θ','ι' => 'ι','κ' => 'κ','λ' => 'λ','μ' => 'μ',
'ν' => 'ν','ξ' => 'ξ','ο' => 'ο','π' => 'π','ρ' => 'ρ','ς' => 'ς','σ' => 'σ','τ' => 'τ',
'υ' => 'υ','φ' => 'φ','χ' => 'χ','ψ' => 'ψ','ω' => 'ω','ϑ' => 'ϑ','ϒ' => 'ϒ','ϖ' => 'ϖ',
'•' => '•','…' => '…','′' => '′','″' => '″','‾' => '‾','⁄' => '⁄','℘' => '℘',
'ℑ' => 'ℑ','ℜ' => 'ℜ','™' => '™','ℵ' => 'ℵ','←' => '←','↑' => '↑','→' => '→','↓' => '↓',
'↔' => '↔','↵' => '↵','⇐' => '⇐','⇑' => '⇑','⇒' => '⇒','⇓' => '⇓','⇔' => '⇔','∀' => '∀',
'∂' => '∂','∃' => '∃','∅' => '∅','∇' => '∇','∈' => '∈','∉' => '∉','∋' => '∋','∏' => '∏',
'∑' => '∑','−' => '−','∗' => '∗','√' => '√','∝' => '∝','∞' => '∞','∠' => '∠','∧' => '∧',
'∨' => '∨','∩' => '∩','∪' => '∪','∫' => '∫','∴' => '∴','∼' => '∼','≅' => '≅','≈' => '≈',
'≠' => '≠','≡' => '≡','≤' => '≤','≥' => '≥','⊂' => '⊂','⊃' => '⊃','⊄' => '⊄','⊆' => '⊆',
'⊇' => '⊇','⊕' => '⊕','⊗' => '⊗','⊥' => '⊥','⋅' => '⋅','⌈' => '⌈','⌉' => '⌉','⌊' => '⌊',
'&rfloo(r;' => '⌋','⟨' => '〈','⟩' => '〉','◊' => '◊','♠' => '♠','♣' => '♣','♥' => '♥','♦' => '♦',
' ' => ' ','¡' => '¡','¢' => '¢','£' => '£','¤' => '¤','¥' => '¥','¦' => '¦','§' => '§','¨' => '¨',
'©' => '©','ª' => 'ª','«' => '«','¬' => '¬','­' => '­','®' => '®','¯' => '¯','°' => '°','±' => '±',
'²' => '²','³' => '³','´' => '´','µ' => 'µ','¶' => '¶','·' => '·','¸' => '¸','¹' => '¹',
'º' => 'º','»' => '»','¼' => '¼','½' => '½','¾' => '¾','¿' => '¿','À' => 'À','Á' => 'Á',
'Â' => 'Â','Ã' => 'Ã','Ä' => 'Ä','Å' => 'Å','Æ' => 'Æ','Ç' => 'Ç','È' => 'È','É' => 'É',
'Ê' => 'Ê','Ë' => 'Ë','Ì' => 'Ì','Í' => 'Í','Î' => 'Î','Ï' => 'Ï','Ð' => 'Ð','Ñ' => 'Ñ',
'Ò' => 'Ò','Ó' => 'Ó','Ô' => 'Ô','Õ' => 'Õ','Ö' => 'Ö','×' => '×','Ø' => 'Ø','Ù' => 'Ù',
'Ú' => 'Ú','Û' => 'Û','Ü' => 'Ü','Ý' => 'Ý','Þ' => 'Þ','ß' => 'ß','à' => 'à','á' => 'á',
'â' => 'â','ã' => 'ã','ä' => 'ä','å' => 'å','æ' => 'æ','ç' => 'ç','è' => 'è','é' => 'é',
'ê' => 'ê','ë' => 'ë','ì' => 'ì','í' => 'í','î' => 'î','ï' => 'ï','ð' => 'ð','ñ' => 'ñ',
'ò' => 'ò','ó' => 'ó','ô' => 'ô','õ' => 'õ','ö' => 'ö','÷' => '÷','ø' => 'ø','ù' => 'ù',
'ú' => 'ú','û' => 'û','ü' => 'ü','ý' => 'ý','þ' => 'þ','ÿ' => 'ÿ'
);
$a1 = array_keys($table);
$a2 = array_values($table);
return str_replace($a1,$a2,$text);
}
}
?>