Skip to content

Instantly share code, notes, and snippets.

@vgrish
Created September 22, 2017 09:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vgrish/f6cf18f77f52058b4db2289c7795b78b to your computer and use it in GitHub Desktop.
Save vgrish/f6cf18f77f52058b4db2289c7795b78b to your computer and use it in GitHub Desktop.
<?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