-
-
Save Rachind/e5afac92b5c8621c53449019b8cb2741 to your computer and use it in GitHub Desktop.
Копирование товара в miniShop2 с галереей. Плагин на скорую руку.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Сколько ресурсов обрабатывать за раз | |
$step = 1; | |
// Если процесс уже остановлен, сбрасываем OFFSET | |
if (!isset($_SESSION['Console']['completed'])) { | |
$_SESSION['console_offset'] = 0; | |
} | |
$offset = isset($_SESSION['console_offset']) && $_SESSION['console_offset'] ? $_SESSION['console_offset'] : 0; | |
// Формируем запрос | |
$q = $modx->newQuery('msProduct'); | |
$q->where(array('parent' => 11574)); | |
$total = $modx->getCount('msProduct', $q); | |
// Пропускаем все уже обработанные объекты | |
$q->limit($step, $offset); | |
$ress = $modx->getCollection('msProduct', $q); | |
foreach($ress as $res){ | |
$newObj = $modx->runProcessor('resource/duplicate', array( | |
'id'=>$res->id, | |
'duplicate_children'=>false, | |
'name'=>$res->get('pagetitle'))); | |
if ($newObj->isError()) { | |
//$updateerror = true; | |
//$errormsg = $newObj->getMessage(); | |
}else{ | |
$r = $newObj->getObject(); | |
if (isset($r['id']) && $resource = $modx->getObject('modResource',$r['id'])){ | |
$resource->set('parent', 6855); | |
$resource->save(); | |
} | |
} | |
} | |
$_SESSION['console_offset'] = $offset + $step; | |
if ($_SESSION['console_offset'] >= $total) { | |
$sucsess = 100; | |
$_SESSION['Console']['completed'] = true; | |
} else { | |
$sucsess = round($_SESSION['console_offset'] / $total, 2) * 100; | |
$_SESSION['Console']['completed'] = false; | |
} | |
for ($i=0; $i<=100; $i++) { | |
if ($i <= $sucsess) { | |
print '='; | |
} else { | |
print '_'; | |
} | |
} | |
print "\n"; | |
print $sucsess.'% ('.$_SESSION['console_offset'].')'."\n\n"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* вешаем плагин на событие OnResourceDuplicate | |
*/ | |
/* | |
* author i.modx@ya.ru aka (https://gist.github.com/andronex) | |
*/ | |
if($modx->event->name == 'OnResourceDuplicate'){ | |
$newResource = $modx->getOption('newResource', $scriptProperties, null, true); | |
$oldResource = $modx->getOption('oldResource', $scriptProperties, null, true); | |
//функция добавления к товару картинок | |
if(!function_exists(addImages)){ | |
function addImages($image, $res, $pagetitle){ | |
if ($image) { | |
global $modx; | |
$response_img = $modx->runProcessor('gallery/upload', | |
array('id' => $res->get('id'), 'name' => $pagetitle, 'file' => $image), | |
array('processors_path' => MODX_CORE_PATH.'components/minishop2/processors/mgr/') | |
); | |
if ($response_img->isError()) { | |
$modx->log(modX::LOG_LEVEL_ERROR, "Ошибка привязки картинки \"{$image}\" к товару id = {$res->get('id')}: \n". print_r($response_img->getAllErrors(), 1)); | |
} | |
else { | |
$sql = "UPDATE {$modx->getTableName('msProductFile')} SET `name` = \"{$pagetitle}\" WHERE `product_id` = {$res->get('id')};"; | |
$stmt = $modx->prepare($sql); | |
$stmt->execute(); | |
$stmt->closeCursor(); | |
unset($sql, $stmt); | |
$modx->log(modX::LOG_LEVEL_INFO, "Удачно загружена картинка \"$image\": \n". print_r($response_img->getObject(), 1)); | |
return true; | |
} | |
} | |
return false; | |
} | |
} | |
$class = 'msProductFile'; | |
$q = $modx->newQuery($class); | |
//выбираем все картинки, привязанные к старой карточке товара в оригинальных размерах (parent => 0) | |
$q->where(array('product_id' => $oldResource->get('id'), 'parent' => 0)); | |
$q->sortby('rank','ASC'); | |
foreach ($modx->getIterator($class, $q) as $files) { | |
$array = $files->toArray(); | |
$img = MODX_BASE_PATH . ltrim($array['url'], '/'); | |
addImages($img, $newResource, $array['name']); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment