PageRenderTime 57ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/concreteOLD/libraries/content/importer.php

https://bitbucket.org/selfeky/xclusivescardwebsite
PHP | 461 lines | 395 code | 35 blank | 31 comment | 82 complexity | a46870619ed6d51404b5acafa9d1bcca MD5 | raw file
  1. <?php
  2. /**
  3. * @package Core
  4. * @author Andrew Embler <andrew@concrete5.org>
  5. * @copyright Copyright (c) 2003-2011 Concrete5. (http://www.concrete5.org)
  6. * @license http://www.concrete5.org/license/ MIT License
  7. *
  8. */
  9. /**
  10. * A way to import concrete5 content.
  11. * @package Core
  12. * @author Andrew Embler <andrew@concrete5.org>
  13. * @copyright Copyright (c) 2003-2011 Concrete5. (http://www.concrete5.org)
  14. * @license http://www.concrete5.org/license/ MIT License
  15. *
  16. */
  17. defined('C5_EXECUTE') or die("Access Denied.");
  18. class ContentImporter {
  19. protected static $mcBlockIDs = array();
  20. public function importContentFile($file) {
  21. $sx = simplexml_load_file($file);
  22. $this->importSinglePageStructure($sx);
  23. $this->importStacksStructure($sx);
  24. $this->importBlockTypes($sx);
  25. $this->importAttributeCategories($sx);
  26. $this->importAttributeTypes($sx);
  27. $this->importAttributes($sx);
  28. $this->importAttributeSets($sx);
  29. $this->importThemes($sx);
  30. $this->importTaskPermissions($sx);
  31. $this->importJobs($sx);
  32. // import bare page types first, then import structure, then page types blocks, attributes and composer settings, then page content, because we need the structure for certain attributes and stuff set in master collections (like composer)
  33. $this->importPageTypesBase($sx);
  34. $this->importPageStructure($sx);
  35. $this->importPageTypeDefaults($sx);
  36. $this->importSinglePageContent($sx);
  37. $this->importStacksContent($sx);
  38. $this->importPageContent($sx);
  39. $this->importPackages($sx);
  40. $this->importConfigValues($sx);
  41. $this->importSystemCaptchaLibraries($sx);
  42. }
  43. protected static function getPackageObject($pkgHandle) {
  44. $pkg = false;
  45. if ($pkgHandle) {
  46. $pkg = Package::getByHandle($pkgHandle);
  47. }
  48. return $pkg;
  49. }
  50. protected function importStacksStructure(SimpleXMLElement $sx) {
  51. if (isset($sx->stacks)) {
  52. foreach($sx->stacks->stack as $p) {
  53. if (isset($p['type'])) {
  54. $type = Stack::mapImportTextToType($p['type']);
  55. Stack::addStack($p['name'], $type);
  56. } else {
  57. Stack::addStack($p['name']);
  58. }
  59. }
  60. }
  61. }
  62. protected function importStacksContent(SimpleXMLElement $sx) {
  63. if (isset($sx->stacks)) {
  64. foreach($sx->stacks->stack as $p) {
  65. $stack = Stack::getByName($p['name']);
  66. if (isset($p->area)) {
  67. $this->importPageAreas($stack, $p);
  68. }
  69. }
  70. }
  71. }
  72. protected function importSinglePageStructure(SimpleXMLElement $sx) {
  73. Loader::model('single_page');
  74. if (isset($sx->singlepages)) {
  75. foreach($sx->singlepages->page as $p) {
  76. $pkg = ContentImporter::getPackageObject($p['package']);
  77. $spl = SinglePage::add($p['path'], $pkg);
  78. if (is_object($spl)) {
  79. if (isset($p['root']) && $p['root'] == true) {
  80. $spl->moveToRoot();
  81. }
  82. if ($p['name']) {
  83. $spl->update(array('cName' => $p['name'], 'cDescription' => $p['description']));
  84. }
  85. }
  86. }
  87. }
  88. }
  89. protected function importSinglePageContent(SimpleXMLElement $sx) {
  90. Loader::model('single_page');
  91. if (isset($sx->singlepages)) {
  92. foreach($sx->singlepages->page as $px) {
  93. $page = Page::getByPath($px['path']);
  94. if (isset($px->area)) {
  95. $this->importPageAreas($page, $px);
  96. }
  97. if (isset($px->attributes)) {
  98. foreach($px->attributes->children() as $attr) {
  99. $ak = CollectionAttributeKey::getByHandle($attr['handle']);
  100. $page->setAttribute((string) $attr['handle'], $ak->getController()->importValue($attr));
  101. }
  102. }
  103. }
  104. }
  105. }
  106. protected function setupPageNodeOrder($pageNodeA, $pageNodeB) {
  107. $pathA = (string) $pageNodeA['path'];
  108. $pathB = (string) $pageNodeB['path'];
  109. $numA = explode('/', $pathA);
  110. $numB = explode('/', $pathB);
  111. if ($numA == $numB) {
  112. return 0;
  113. } else {
  114. return ($numA < $numB) ? -1 : 1;
  115. }
  116. }
  117. protected function importPageContent(SimpleXMLElement $sx) {
  118. if (isset($sx->pages)) {
  119. foreach($sx->pages->page as $px) {
  120. if ($px['path'] != '') {
  121. $page = Page::getByPath($px['path']);
  122. } else {
  123. $page = Page::getByID(HOME_CID, 'RECENT');
  124. }
  125. if (isset($px->area)) {
  126. $this->importPageAreas($page, $px);
  127. }
  128. if (isset($px->attributes)) {
  129. foreach($px->attributes->children() as $attr) {
  130. $ak = CollectionAttributeKey::getByHandle($attr['handle']);
  131. $page->setAttribute((string) $attr['handle'], $ak->getController()->importValue($attr));
  132. }
  133. }
  134. $page->reindex();
  135. }
  136. }
  137. }
  138. protected function importPageStructure(SimpleXMLElement $sx) {
  139. if (isset($sx->pages)) {
  140. $nodes = array();
  141. foreach($sx->pages->page as $p) {
  142. $nodes[] = $p;
  143. }
  144. usort($nodes, array('ContentImporter', 'setupPageNodeOrder'));
  145. $home = Page::getByID(HOME_CID, 'RECENT');
  146. foreach($nodes as $px) {
  147. $pkg = ContentImporter::getPackageObject($px['package']);
  148. $data = array();
  149. $data['pkgID'] = 0;
  150. if (is_object($pkg)) {
  151. $data['pkgID'] = $pkg->getPackageID();
  152. }
  153. $args = array();
  154. if ($px['path'] == '') {
  155. // home page
  156. $page = $home;
  157. $ct = CollectionType::getByHandle($px['pagetype']);
  158. $args['ctID'] = $ct->getCollectionTypeID();
  159. } else {
  160. $page = Page::getByPath($px['path']);
  161. if (!is_object($page) || ($page->isError())) {
  162. $ct = CollectionType::getByHandle($px['pagetype']);
  163. $lastSlash = strrpos((string) $px['path'], '/');
  164. $parentPath = substr((string) $px['path'], 0, $lastSlash);
  165. $data['cHandle'] = substr((string) $px['path'], $lastSlash + 1);
  166. if (!$parentPath) {
  167. $parent = $home;
  168. } else {
  169. $parent = Page::getByPath($parentPath);
  170. }
  171. $page = $parent->add($ct, $data);
  172. }
  173. }
  174. $args['cName'] = $px['name'];
  175. $args['cDescription'] = $px['description'];
  176. $page->update($args);
  177. }
  178. }
  179. }
  180. protected function importPageAreas(Page $page, SimpleXMLElement $px) {
  181. foreach($px->area as $ax) {
  182. if (isset($ax->block)) {
  183. foreach($ax->block as $bx) {
  184. if ($bx['type'] != '') {
  185. // we check this because you might just get a block node with only an mc-block-id, if it's an alias
  186. $bt = BlockType::getByHandle($bx['type']);
  187. $btc = $bt->getController();
  188. $btc->import($page, (string) $ax['name'], $bx);
  189. } else if ($bx['mc-block-id'] != '') {
  190. // we find that block in the master collection block pool and alias it out
  191. $bID = array_search((string) $bx['mc-block-id'], self::$mcBlockIDs);
  192. if ($bID) {
  193. $mc = Page::getByID($page->getMasterCollectionID());
  194. $block = Block::getByID($bID, $mc, (string) $ax['name']);
  195. $block->alias($page);
  196. }
  197. }
  198. }
  199. }
  200. }
  201. }
  202. public static function addMasterCollectionBlockID($b, $id) {
  203. self::$mcBlockIDs[$b->getBlockID()] = $id;
  204. }
  205. public static function getMasterCollectionTemporaryBlockID($b) {
  206. if (isset(self::$mcBlockIDs[$b->getBlockID()])) {
  207. return self::$mcBlockIDs[$b->getBlockID()];
  208. }
  209. }
  210. protected function importPageTypesBase(SimpleXMLElement $sx) {
  211. if (isset($sx->pagetypes)) {
  212. foreach($sx->pagetypes->pagetype as $ct) {
  213. $pkg = ContentImporter::getPackageObject($ct['package']);
  214. $ctt = CollectionType::getByHandle($ct['handle']);
  215. if (!is_object($ctt)) {
  216. $ctr = CollectionType::add(array(
  217. 'ctHandle' => $ct['handle'],
  218. 'ctName' => $ct['name'],
  219. 'ctIcon' => $ct['icon'],
  220. 'ctIsInternal' => (string) $ct['internal']
  221. ), $pkg);
  222. }
  223. }
  224. }
  225. }
  226. protected function importPageTypeDefaults(SimpleXMLElement $sx) {
  227. $db = Loader::db();
  228. if (isset($sx->pagetypes)) {
  229. foreach($sx->pagetypes->pagetype as $ct) {
  230. $ctr = CollectionType::getByHandle((string) $ct['handle']);
  231. $mc = Page::getByID($ctr->getMasterCollectionID());
  232. if (isset($ct->page)) {
  233. $this->importPageAreas($mc, $ct->page);
  234. }
  235. if (isset($ct->composer)) {
  236. $ctr = CollectionType::getByHandle((string) $ct['handle']);
  237. $ctr->importComposerSettings($ct->composer);
  238. }
  239. // now, we copy all the content from these defaults out to the page that they're on.
  240. /*
  241. $r = $db->Execute('select arHandle, bID from CollectionVersionBlocks where cID = ?', array($ctr->getMasterCollectionID()));
  242. $cs = $db->GetCol('select cID from Pages where ctID = ?', array($ctr->getCollectionTypeID()));
  243. while ($row = $r->FetchRow()) {
  244. $block = Block::getByID($row['bID'], $mc, $row['arHandle']);
  245. foreach($cs as $cID) {
  246. $newC = Page::getByID($cID, 'RECENT');
  247. $block->alias($newC);
  248. }
  249. }
  250. */
  251. }
  252. }
  253. }
  254. protected function importBlockTypes(SimpleXMLElement $sx) {
  255. if (isset($sx->blocktypes)) {
  256. foreach($sx->blocktypes->blocktype as $bt) {
  257. $pkg = ContentImporter::getPackageObject($bt['package']);
  258. if (is_object($pkg)) {
  259. BlockType::installBlockTypeFromPackage($bt['handle'], $pkg);
  260. } else {
  261. BlockType::installBlockType($bt['handle']);
  262. }
  263. }
  264. }
  265. }
  266. protected function importAttributeTypes(SimpleXMLElement $sx) {
  267. if (isset($sx->attributetypes)) {
  268. foreach($sx->attributetypes->attributetype as $at) {
  269. $pkg = ContentImporter::getPackageObject($at['package']);
  270. $name = $at['name'];
  271. if (!$name) {
  272. $name = Loader::helper('text')->unhandle($at['handle']);
  273. }
  274. $type = AttributeType::add($at['handle'], $name, $pkg);
  275. if (isset($at->categories)) {
  276. foreach($at->categories->children() as $cat) {
  277. $catobj = AttributeKeyCategory::getByHandle((string) $cat['handle']);
  278. $catobj->associateAttributeKeyType($type);
  279. }
  280. }
  281. }
  282. }
  283. }
  284. protected function importPackages(SimpleXMLElement $sx) {
  285. if (isset($sx->packages)) {
  286. foreach($sx->packages->package as $p) {
  287. $pkg = Loader::package($p['handle']);
  288. $pkg->install();
  289. }
  290. }
  291. }
  292. protected function importThemes(SimpleXMLElement $sx) {
  293. if (isset($sx->themes)) {
  294. foreach($sx->themes->theme as $th) {
  295. $pkg = ContentImporter::getPackageObject($th['package']);
  296. $pt = PageTheme::add($th['handle'], $pkg);
  297. if ($th['activated'] == '1') {
  298. $pt->applyToSite();
  299. }
  300. }
  301. }
  302. }
  303. protected function importSystemCaptchaLibraries(SimpleXMLElement $sx) {
  304. if (isset($sx->systemcaptcha)) {
  305. Loader::model('system/captcha/library');
  306. foreach($sx->systemcaptcha->library as $th) {
  307. $pkg = ContentImporter::getPackageObject($th['package']);
  308. $scl = SystemCaptchaLibrary::add($th['handle'], $th['name'], $pkg);
  309. if ($th['activated'] == '1') {
  310. $scl->activate();
  311. }
  312. }
  313. }
  314. }
  315. protected function importJobs(SimpleXMLElement $sx) {
  316. Loader::model('job');
  317. if (isset($sx->jobs)) {
  318. foreach($sx->jobs->job as $jx) {
  319. $pkg = ContentImporter::getPackageObject($jx['package']);
  320. if (is_object($pkg)) {
  321. Job::installByPackage($jx['handle'], $pkg);
  322. } else {
  323. Job::installByHandle($jx['handle']);
  324. }
  325. }
  326. }
  327. }
  328. protected function importConfigValues(SimpleXMLElement $sx) {
  329. if (isset($sx->config)) {
  330. $db = Loader::db();
  331. $configstore = new ConfigStore($db);
  332. foreach($sx->config->children() as $key) {
  333. $pkg = ContentImporter::getPackageObject($key['package']);
  334. if (is_object($pkg)) {
  335. $configstore->set($key->getName(), (string) $key, $pkg->getPackageID());
  336. } else {
  337. $configstore->set($key->getName(), (string) $key);
  338. }
  339. }
  340. }
  341. }
  342. protected function importTaskPermissions(SimpleXMLElement $sx) {
  343. if (isset($sx->taskpermissions)) {
  344. foreach($sx->taskpermissions->taskpermission as $tp) {
  345. $pkg = ContentImporter::getPackageObject($tp['package']);
  346. $tpa = TaskPermission::addTask($tp['handle'], $tp['name'], $tp['description'], $pkg);
  347. if (isset($tp->access)) {
  348. foreach($tp->access->children() as $ch) {
  349. if ($ch->getName() == 'group') {
  350. $g = Group::getByName($ch['name']);
  351. if (!is_object($g)) {
  352. $g = Group::add($g['name'], $g['description']);
  353. }
  354. $tpa->addAccess($g);
  355. }
  356. }
  357. }
  358. }
  359. }
  360. }
  361. protected function importAttributeCategories(SimpleXMLElement $sx) {
  362. if (isset($sx->attributecategories)) {
  363. foreach($sx->attributecategories->category as $akc) {
  364. $pkg = ContentImporter::getPackageObject($akc['package']);
  365. $akx = AttributeKeyCategory::add($akc['handle'], $akc['allow-sets'], $pkg);
  366. }
  367. }
  368. }
  369. protected function importAttributes(SimpleXMLElement $sx) {
  370. if (isset($sx->attributekeys)) {
  371. foreach($sx->attributekeys->attributekey as $ak) {
  372. $akc = AttributeKeyCategory::getByHandle($ak['category']);
  373. $pkg = ContentImporter::getPackageObject($ak['package']);
  374. $type = AttributeType::getByHandle($ak['type']);
  375. if (is_object($pkg)) {
  376. Loader::model('attribute/categories/' . $akc->getAttributeKeyCategoryHandle(), $pkg->getPackageHandle());
  377. } else {
  378. Loader::model('attribute/categories/' . $akc->getAttributeKeyCategoryHandle());
  379. }
  380. $txt = Loader::helper('text');
  381. $className = $txt->camelcase($akc->getAttributeKeyCategoryHandle());
  382. $c1 = $className . 'AttributeKey';
  383. $ak = call_user_func(array($c1, 'import'), $ak);
  384. }
  385. }
  386. }
  387. protected function importAttributeSets(SimpleXMLElement $sx) {
  388. if (isset($sx->attributesets)) {
  389. foreach($sx->attributesets->attributeset as $as) {
  390. $akc = AttributeKeyCategory::getByHandle($as['category']);
  391. $pkg = ContentImporter::getPackageObject($as['package']);
  392. $set = $akc->addSet((string) $as['handle'], (string) $as['name'], $pkg, $as['locked']);
  393. foreach($as->children() as $ask) {
  394. $ak = $akc->getAttributeKeyByHandle((string) $ask['handle']);
  395. if (is_object($ak)) {
  396. $set->addKey($ak);
  397. }
  398. }
  399. }
  400. }
  401. }
  402. public static function getValue($value) {
  403. if (preg_match('/\{ccm:export:page:(.*)\}|\{ccm:export:file:(.*)\}|\{ccm:export:image:(.*)\}|\{ccm:export:pagetype:(.*)\}/i', $value, $matches)) {
  404. if ($matches[1]) {
  405. $c = Page::getByPath($matches[1]);
  406. return $c->getCollectionID();
  407. }
  408. if ($matches[2]) {
  409. $db = Loader::db();
  410. $fID = $db->GetOne('select fID from FileVersions where fvFilename = ?', array($matches[2]));
  411. return $fID;
  412. }
  413. if ($matches[3]) {
  414. $db = Loader::db();
  415. $fID = $db->GetOne('select fID from FileVersions where fvFilename = ?', array($matches[3]));
  416. return $fID;
  417. }
  418. if ($matches[4]) {
  419. $ct = CollectionType::getByHandle($matches[4]);
  420. return $ct->getCollectionTypeID();
  421. }
  422. } else {
  423. return $value;
  424. }
  425. }
  426. }