Skip to content

Instantly share code, notes, and snippets.

@vgrish

vgrish/transfer.php

Created Sep 22, 2017
Embed
What would you like to do?
<?php
ini_set('display_errors', 1);
ini_set('error_reporting', -1);
define('MODX_API_MODE', true);
$productionConfig = (dirname(dirname(__FILE__))) . '/index.php';
if (file_exists($productionConfig)) {
/** @noinspection PhpIncludeInspection */
require_once $productionConfig;
} else {
die;
}
// load services
$modx->setLogTarget('FILE');
$modx->setLogLevel(modX::LOG_LEVEL_ERROR);
$modx->getService('error', 'error.modError');
$modx->error->message = null;
// time limit
set_time_limit(600);
$tmp = 'Trying to set time limit = 600 sec: ';
$tmp .= ini_get('max_execution_time') == 600 ? 'done' : 'error';
$modx->log(modX::LOG_LEVEL_INFO, $tmp);
$path = MODX_BASE_PATH . 'assets/images/products/';
$q = $modx->newQuery('msProduct');
$q->innerJoin('msProductFile', 'msProductFile', 'msProduct.id = msProductFile.product_id');
$q->where(array(
'msProductFile.parent' => 0,
'msProductFile.url:LIKE' => '%/assets/images/products/%',
));
$q->sortby('msProduct.id', 'ASC');
$q->limit(1);
/** @var msProduct $product */
$product = $modx->getObject('msProduct', $q);
if ($product) {
$modx->log(modX::LOG_LEVEL_ERROR, "Process product " . $product->get('id'));
$files = scandir($path . $product->get('id'));
foreach ($files as $idx => $file) {
if ($file == '.' || $file == '..') {
unset($files[$idx]);
continue;
};
if (is_dir($path . $product->get('id') . '/' . $file)) {
unset($files[$idx]);
continue;
}
$files[$idx] = $path . $product->get('id') . '/' . $file;
}
$modx->log(modX::LOG_LEVEL_ERROR, print_r($files, 1));
// set TMP source
$product->set('source', 3);
$product->save();
/** @var msProductFile[] $images */
$images = $product->getMany('Files', array('parent' => 0, 'url:LIKE' => '%/assets/images/products/%',));
foreach ($images as $image) {
$image->remove();
}
// set MS2 Images source
$product->set('source', 2);
$product->save();
if (!empty($files)) {
foreach ($files as $image) {
$name = explode('/', $image);
$name = end($name);
if (!file_exists($image)) {
$modx->log(modX::LOG_LEVEL_ERROR, "Could not import image \"$v\" to gallery. File \"$image\" not found on server.");
} else {
$response = $modx->runProcessor('gallery/upload',
array('id' => $product->get('id'), 'name' => $name, 'file' => $image),
array('processors_path' => MODX_CORE_PATH . 'components/minishop2/processors/mgr/')
);
if ($response->isError()) {
$modx->log(modX::LOG_LEVEL_ERROR, "Error on upload \"$v\": \n" . print_r($response->getAllErrors(), 1));
} else {
$modx->log(modX::LOG_LEVEL_INFO, "Successful upload \"$v\": \n" . print_r($response->getObject(), 1));
}
}
}
}
}
$count = $modx->getCount('msProductFile', array(
'parent' => 0,
'url:LIKE' => '%/assets/images/products/%',
));
$modx->log(modX::LOG_LEVEL_ERROR, "Total " . $count);
if ($count AND $product) {
$uri = parse_url($_SERVER['REQUEST_URI']);
$siteUrl = $modx->getOption('site_url', null, MODX_SITE_URL);
$stepRedirect = trim($siteUrl, '/') . $uri['path'];
$refreshDelay = 2;
header("Refresh: {$refreshDelay}; url={$stepRedirect}");
} else {
$modx->log(modX::LOG_LEVEL_ERROR, "Process product END");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.