PageRenderTime 46ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/backend/modules/pages/actions/copy.php

http://github.com/forkcms/forkcms
PHP | 183 lines | 103 code | 30 blank | 50 comment | 12 complexity | e636ec873dfa3bd2eaa2605b6e7fa687 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, MIT, AGPL-3.0, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /*
  3. * This file is part of Fork CMS.
  4. *
  5. * For the full copyright and license information, please view the license
  6. * file that was distributed with this source code.
  7. */
  8. /**
  9. * BackendPagesCopy
  10. * This is the copy-action, it will copy pages from one language to another
  11. * @remark: IMPORTANT existing data will be removed, this feature is also expiremental!
  12. *
  13. * @author Tijs Verkoyen <tijs@sumocoders.be>
  14. */
  15. class BackendPagesCopy extends BackendBaseActionDelete
  16. {
  17. /**
  18. * Execute the action
  19. */
  20. public function execute()
  21. {
  22. // call parent, this will probably add some general CSS/JS or other required files
  23. parent::execute();
  24. // get parameters
  25. $from = $this->getParameter('from');
  26. $to = $this->getParameter('to');
  27. // validate
  28. if($from == '') throw new BackendException('Specify a from-parameter.');
  29. if($to == '') throw new BackendException('Specify a to-parameter.');
  30. // get db
  31. $db = BackendModel::getDB(true);
  32. // get all old pages
  33. $ids = $db->getColumn(
  34. 'SELECT id
  35. FROM pages AS i
  36. WHERE i.language = ? AND i.status = ?',
  37. array($to, 'active')
  38. );
  39. // any old pages
  40. if(!empty($ids))
  41. {
  42. // delete existing pages
  43. foreach($ids as $id)
  44. {
  45. // redefine
  46. $id = (int) $id;
  47. // get revision ids
  48. $revisionIDs = (array) $db->getColumn(
  49. 'SELECT i.revision_id
  50. FROM pages AS i
  51. WHERE i.id = ? AND i.language = ?',
  52. array($id, $to)
  53. );
  54. // get meta ids
  55. $metaIDs = (array) $db->getColumn(
  56. 'SELECT i.meta_id
  57. FROM pages AS i
  58. WHERE i.id = ? AND i.language = ?',
  59. array($id, $to)
  60. );
  61. // delete meta records
  62. if(!empty($metaIDs)) $db->delete('meta', 'id IN (' . implode(',', $metaIDs) . ')');
  63. // delete blocks and their revisions
  64. if(!empty($revisionIDs)) $db->delete('pages_blocks', 'revision_id IN (' . implode(',', $revisionIDs) . ')');
  65. // delete page and the revisions
  66. if(!empty($revisionIDs)) $db->delete('pages', 'revision_id IN (' . implode(',', $revisionIDs) . ')');
  67. }
  68. }
  69. // delete search indexes
  70. $db->delete('search_index', 'module = ? AND language = ?', array('pages', $to));
  71. // get all active pages
  72. $ids = BackendModel::getDB()->getColumn(
  73. 'SELECT id
  74. FROM pages AS i
  75. WHERE i.language = ? AND i.status = ?',
  76. array($from, 'active')
  77. );
  78. // loop
  79. foreach($ids as $id)
  80. {
  81. // get data
  82. $sourceData = BackendPagesModel::get($id, null, $from);
  83. // get and build meta
  84. $meta = $db->getRecord(
  85. 'SELECT *
  86. FROM meta
  87. WHERE id = ?',
  88. array($sourceData['meta_id'])
  89. );
  90. // remove id
  91. unset($meta['id']);
  92. // build page record
  93. $page = array();
  94. $page['id'] = $sourceData['id'];
  95. $page['user_id'] = BackendAuthentication::getUser()->getUserId();
  96. $page['parent_id'] = $sourceData['parent_id'];
  97. $page['template_id'] = $sourceData['template_id'];
  98. $page['meta_id'] = (int) $db->insert('meta', $meta);
  99. $page['language'] = $to;
  100. $page['type'] = $sourceData['type'];
  101. $page['title'] = $sourceData['title'];
  102. $page['navigation_title'] = $sourceData['navigation_title'];
  103. $page['navigation_title_overwrite'] = $sourceData['navigation_title_overwrite'];
  104. $page['hidden'] = $sourceData['hidden'];
  105. $page['status'] = 'active';
  106. $page['publish_on'] = BackendModel::getUTCDate();
  107. $page['created_on'] = BackendModel::getUTCDate();
  108. $page['edited_on'] = BackendModel::getUTCDate();
  109. $page['allow_move'] = $sourceData['allow_move'];
  110. $page['allow_children'] = $sourceData['allow_children'];
  111. $page['allow_edit'] = $sourceData['allow_edit'];
  112. $page['allow_delete'] = $sourceData['allow_delete'];
  113. $page['sequence'] = $sourceData['sequence'];
  114. $page['data'] = ($sourceData['data'] !== null) ? serialize($sourceData['data']) : null;
  115. // insert page, store the id, we need it when building the blocks
  116. $revisionId = BackendPagesModel::insert($page);
  117. // init var
  118. $blocks = array();
  119. $hasBlock = ($sourceData['has_extra'] == 'Y');
  120. // get the blocks
  121. $sourceBlocks = BackendPagesModel::getBlocks($id, null, $from);
  122. // loop blocks
  123. foreach($sourceBlocks as $sourceBlock)
  124. {
  125. // build block
  126. $block = $sourceBlock;
  127. $block['revision_id'] = $revisionId;
  128. $block['created_on'] = BackendModel::getUTCDate();
  129. $block['edited_on'] = BackendModel::getUTCDate();
  130. // add block
  131. $blocks[] = $block;
  132. }
  133. // insert the blocks
  134. BackendPagesModel::insertBlocks($blocks, $hasBlock);
  135. // check if the method exists
  136. if(method_exists('BackendSearchModel', 'saveIndex'))
  137. {
  138. // init var
  139. $text = '';
  140. // build search-text
  141. foreach($blocks as $block) $text .= ' ' . $block['html'];
  142. // add
  143. BackendSearchModel::saveIndex('pages', (int) $page['id'], array('title' => $page['title'], 'text' => $text), $to);
  144. }
  145. // get tags
  146. $tags = BackendTagsModel::getTags('pages', $id, 'string', $from);
  147. // save tags
  148. if($tags != '') BackendTagsModel::saveTags($page['id'], $tags, 'pages');
  149. }
  150. // build cache
  151. BackendPagesModel::buildCache($to);
  152. }
  153. }