How to migrate a cs-cart layout programmatically?

324 views Asked by At

On a cs-cart project, I am looking for a clean way to automate the migration of the current theme in a fully automatic manner.

Migrating the theme files is easy since each theme is located in a dedicated directory. Nevertheless, some pages are created using the layouts WYSIWIG editor from the backend. This editor as a nice feature that allows to generate an XML file that describes all the layouts and can be imported back into another environment. This requires manual operation (logging into the backend, clicking the button...) and I would like to fully automate this.

So far, the only solution I was able to find is to migrate the data directly from the database, by generating a dump of the layout related-tables and importing it into the target environment. This solution seems to work but is a bit risky in my opinion.

Does cs-cart provide a command-line interface to export and import the current layouts?

2

There are 2 answers

1
ISTI On

I wrote a small script for this task:

<?php

use Tygh\BlockManager\Layout;
use Tygh\BlockManager\Exim;

if ($mode == "do") {
    $location_ids = array();
    $default_layout_data = Layout::instance()->getDefault();
    $layout_id = $default_layout_data['layout_id'];

    $content = Exim::instance()->export($layout_id, $location_ids);
    header("Content-Type: text/xml");
    header("Content-Length: " . strlen($content));
    header('Content-Disposition: attachment; filename=layouts.xml');
    header('Expires: 0');
    print $content;
    exit();
}

if you put this code to a controller file (for example into your /app/addons/my_changes/controllers/backend/layout_export.php) and call this via url (in my case: /youradmin.php?dispatch=layout_export.do), it will download the default theme's main layout structure.

Please note, that I'm working with version 4.3.3. However I think this code will work any type of CS-Cart from version 4.x

0
ISTI On

Full solution (with my_changes add-on)

/app/addons/my_changes/schemas/permissions/trusted_controllers.post.php

<?php

$schema['layout_exim']['allow']['export'] = true;
$schema['layout_exim']['allow']['import'] = true;

return $schema;

/app/addons/my_changes/controllers/backend/layout_exim.php

<?php

use Tygh\BlockManager\Layout;
use Tygh\BlockManager\Exim;

if ($mode == "export") {
    $location_ids = array();
    $default_layout_data = Layout::instance()->getDefault();
    $layout_id = $default_layout_data['layout_id'];

    $content = Exim::instance()->export($layout_id, $location_ids);
    header("Content-Type: text/xml");
    header("Content-Length: " . strlen($content));
    header('Content-Disposition: attachment; filename=layouts.xml');
    header('Expires: 0');
    print $content;
    exit();
}  elseif ($mode == "import") {
    $result = Exim::instance()->importFromFile($_REQUEST['filepath']);

    if ($result) {
        print "true";
    } else {
        print "false";
    }
    exit();
}

After it, please clear your cache. If you do everything right, you will be able to call these 2 controllers without authentication (however I suggest you to set passwords in the get parameters, because right now, this is very unsafe!)

To call the controllers via URL:

http://path-to-your-admin.com/admin.php?dispatch=layout_exim.import&filepath=path/to/file

http://path-to-your-admin.com/admin.php?dispatch=layout_exim.export