Skip to content

Instantly share code, notes, and snippets.

@dajoho
Last active July 30, 2020 14:00
Show Gist options
  • Save dajoho/5117053 to your computer and use it in GitHub Desktop.
Save dajoho/5117053 to your computer and use it in GitHub Desktop.
Copy Categories REDAXO 4.3.x - Duplicates an entire category in REDAXO (including metadata, modules, slices & actions)
<?php
error_reporting(0);
include 'redaxo/include/master.inc.php';
error_reporting(E_ALL ^E_NOTICE);
ini_set('display_errors', 1);
$CLONE_THIS_ID = 184;
$DUPLICATE_MODULES = true;
function duplicateModule($id) {
$sql = new rex_sql;
$sql->setQuery('SELECT * FROM rex_module WHERE name LIKE "__CLONE__'.$id.'__%"');
if ($sql->getRows() > 0) {
return $sql->getValue('id');
} else {
// DUPLICATE MODULUE
$sql = new rex_sql;
$sql->setQuery('SELECT * FROM rex_module WHERE id = '.$id);
if ($sql->getRows() > 0) {
$fields = $sql->getFieldNames();
$iSql = new rex_sql;
$iSql->setTable('rex_module');
foreach ($fields as $field) {
if ($field != 'id' && $field != 'name') {
$iSql->setValue($field, mysql_real_escape_string($sql->getValue($field)));
}
}
$iSql->setValue('name', "__CLONE__".$id."__".$sql->getValue('name'));
$iSql->insert();
$newId = $iSql->last_insert_id;
// DUPLICATE ACTIONS
$sql = new rex_sql;
$sql->setQuery('SELECT * FROM rex_module_action WHERE module_id = '.$id);
for($i=0;$i<$sql->getRows();$i++) {
$iSql = new rex_sql;
$iSql->setTable('rex_module_action');
$iSql->setValue('module_id', $newId);
$iSql->setValue('action_id', $sql->getValue('action_id'));
$iSql->insert();
}
return $newId;
}
}
}
$sql = new rex_sql();
$sql->setQuery('
SELECT
*, (SELECT (id+1) FROM rex_article ORDER BY id DESC LIMIT 1) as max
FROM
rex_article
WHERE
(path LIKE "%|'.$CLONE_THIS_ID.'|%" OR id = '.$CLONE_THIS_ID.')
ORDER BY
path, clang
');
$PROCESSED_ARTICLES = array();
$PROCESSED_SLICES = array();
$maxAdditional = 0;
for ($i=0;$i<$sql->getRows();$i++) {
$fields = $sql->getFieldnames();
// DUPLICATE ARTICLES
$iSql = new rex_sql;
$iSql->setTable('rex_article');
$clang = $sql->getValue('clang');
foreach($fields as $field) {
if ($field != 'max' && $field != 'pid') {
$iSql->setValue($field, $sql->getValue($field));
if ($field == 'id') {
if (!array_key_exists($sql->getValue('id'), $PROCESSED_ARTICLES)) {
echo "Found article is not in array:".$sql->getValue('id').' => '.$PROCESSED_ARTICLES[$sql->getValue('id')]."\n";
$iSql->setValue('id', $sql->getValue('max')+$maxAdditional);
$newId = $sql->getValue('max')+$maxAdditional;
} else {
echo "Found article:".$sql->getValue('id').' => '.$PROCESSED_ARTICLES[$sql->getValue('id')]."\n";
$iSql->setValue('id', $PROCESSED_ARTICLES[$sql->getValue('id')]);
$newId = $PROCESSED_ARTICLES[$sql->getValue('id')];
}
}
}
// Fix re_id
if ($field == "re_id") {
if (array_key_exists($sql->getValue('re_id'), $PROCESSED_ARTICLES)) {
$iSql->setValue('re_id', $PROCESSED_ARTICLES[$sql->getValue('re_id')]);
}
}
// Fix path
if ($field == 'path') {
$path = explode('|',$sql->getValue('path'));
foreach ($path as $k=>$v) {
if (array_key_exists($v, $PROCESSED_ARTICLES)) {
$path[$k] = $PROCESSED_ARTICLES[$v];
}
}
$iSql->setValue('path', implode('|', $path));
}
// Rename to 'cloned_'
if ($sql->getValue('id') == $CLONE_THIS_ID) {
$iSql->setValue('catname', 'CLONED_'.$sql->getValue('catname'));
}
}
// DUPLICATE ARTICLE SLICES
$sliceSql = new rex_sql;
$sliceSql->setQuery('SELECT * FROM rex_article_slice WHERE article_id = '.$sql->getValue('id').' ORDER BY id ASC');
$sliceIds = array();
for ($j=0;$j<$sliceSql->getRows();$j++) {
$sliceISql = new rex_sql;
$sliceISql->setTable('rex_article_slice');
$sliceFields = $sliceSql->getFieldnames();
foreach ($sliceFields as $sliceField) {
if ($sliceField != 'id') {
$sliceISql->setValue($sliceField, mysql_real_escape_string($sliceSql->getValue($sliceField)));
if ($sliceSql->getValue('article_id') == $sql->getValue('id')) {
$sliceISql->setValue('article_id', $newId);
}
}
}
if ($DUPLICATE_MODULES) {
$sliceISql->setValue('modultyp_id', duplicateModule($sliceSql->getValue('modultyp_id')));
}
$sliceISql->insert();
$newSliceId = $sliceISql->last_insert_id;
$sliceIds[] = $newSliceId;
$PROCESSED_SLICES[$clang][$sliceSql->getValue('id')] = $newSliceId;
$sliceSql->next();
}
// UPDATE ARTICLE SLICE RELATIONS
if (count($sliceIds) > 0) {
$sliceSql = new rex_sql;
$sliceSql->setQuery('SELECT * FROM rex_article_slice WHERE id IN ('.implode(',', $sliceIds).')');
for ($j=0;$j<$sliceSql->getRows();$j++) {
if (array_key_exists($sliceSql->getValue('re_article_slice_id'), $PROCESSED_SLICES[$clang])) {
$sliceUSql = new rex_sql;
$sliceUSql->setTable('rex_article_slice');
$sliceUSql->setValue('re_article_slice_id', $PROCESSED_SLICES[$clang][$sliceSql->getValue('re_article_slice_id')]);
$sliceUSql->wherevar = ' WHERE id = '.$sliceSql->getValue('id');
$sliceUSql->update();
}
$sliceSql->next();
}
}
$maxAdditional++;
$iSql->insert();
$PROCESSED_ARTICLES[$sql->getValue('id')] = $newId;
$sql->next();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment