Clone theme with config 4-5

<?php
/**
 * $Id$
 *
 * Bemerkenswert ist, dass die oxid in oxconfig und oxconfigdisplay nicht
 * korresponieren.
 * Wenn ein configWert gepeichert wird, bekommt der eine neue ID
 * siehe: core/oxconfig ~ 1680
 *
 * # im Frontend muß man als Admin angemeldet sein, oder in Zeile exit(Anmeldung erforderlich) auskommentieren
 * # Vor dem ersten Lauf die beiden Werte für Quelle und Ziel anpassen
 * # Datei ins root-Verzeichnis
 * # Vorher den Theme-Ordner kopieren und umbenennen
 */

// Hier anpassen.
$scriptConfig = (object) array(
    'from' => 'azure',           // Quelle
    'to'   => 'mytheme'          // Ziel
);

exit('angepasst? kommentier mich aus in Zeile:' . __LINE__);

function getShopBasePath() {
    return realpath( dirname(__FILE__) ) . DIRECTORY_SEPARATOR;
}

require_once getShopBasePath() . 'core/oxfunctions.php';
$shopConfig = oxConfig::getInstance();

if( oxSession::getVar('usr') === null || oxSession::getVar('usr') !== 'oxdefaultadmin' ) {
    exit( 'Anmeldung erforderlich.' );
}

$oDb      = oxDb::getDb(true);
$sShopId  = $shopConfig->getBaseShopId();

$sSql = "
    SELECT
        `cfg`.`oxid`                                                   AS `id`,
        `cfg`.`oxmodule`                                               AS `theme`,
        `cfg`.`oxvarname`                                              AS `name`,
        `cfg`.`oxvartype`                                              AS `type`,
        DECODE( `cfg`.`oxvarvalue`, '" . $shopConfig->sConfigKey . "') AS `value`,
        `cfgd`.`oxgrouping`                                            AS `group`,
        `cfgd`.`oxvarconstraint`                                       AS `constraint`,
        `cfgd`.`oxpos`                                                 AS `pos`
    FROM
        `oxconfig`        AS `cfg`,
        `oxconfigdisplay` AS `cfgd`
    WHERE
        `cfg`.`oxshopid` = '" . $sShopId . "'
    AND
        `cfg`.`oxmodule` = 'theme:" . $scriptConfig->from ."'
    AND(
        `cfgd`.`oxcfgmodule` = `cfg`.`oxmodule`
        AND
        `cfgd`.`oxcfgvarname` = `cfg`.`oxvarname`
    )
";

$aThemeValues = $oDb->getAll( $sSql );

if( is_array( $aThemeValues )
    && !empty( $aThemeValues ) )
{
    // lösche vorhande Einträge für das theme
    $oDb->Execute("
        DELETE FROM
            `oxconfig`
        WHERE
            `oxshopid`  = '" . $sShopId . "'
        AND
            `oxmodule` = 'theme:" . $scriptConfig->to . "'
    ");

    // lösche vorhande Einträge für das theme
    $oDb->Execute("
        DELETE FROM
            `oxconfigdisplay`
        WHERE
            `oxcfgmodule` = 'theme:" . $scriptConfig->to . "'
    ");

    foreach( $aThemeValues as $aConfig )
    {
        $cfg = (object) $aConfig;
        $sSql = "
            REPLACE INTO
                `oxconfig`
            SET
                `OXID`       = '" . $scriptConfig->to . "." . $cfg->name . "',
                `OXSHOPID`   = '" . $sShopId . "',
                `OXMODULE`   = 'theme:" . $scriptConfig->to . "',
                `OXVARNAME`  = '" . $cfg->name . "',
                `OXVARTYPE`  = '" . $cfg->type . "',
                `OXVARVALUE` = ENCODE( " . $oDb->quote( $cfg->value ) . ", " . $oDb->quote( $shopConfig->sConfigKey ) . " )
        ";
        $oDb->Execute($sSql);

        // display
        $sSql = "
            REPLACE INTO
                `oxconfigdisplay`
            SET
                `oxid`            = '" . $scriptConfig->to . "." . $cfg->name . "',
                `oxcfgmodule`     = 'theme:" . $scriptConfig->to . "',
                `oxcfgvarname`    = '" . $cfg->name . "',
                `oxgrouping`      = '" . $cfg->group . "',
                `oxvarconstraint` = '" . $cfg->constraint . "',
                `oxpos`           = '" . $cfg->pos . "'
      ";
      $oDb->Execute($sSql);
    }
} else {
    exit('Fehler. Keine Einträge für "'. $scriptConfig->from . '" gefunden' );
}

Thanks to stefan2 for providing this script; forum thread: http://www.oxid-esales.com/forum/showthread.php?t=9393&page=2#post55919