Skip to content

Instantly share code, notes, and snippets.

@ExileofAranei
Last active October 11, 2019 11:55
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 ExileofAranei/24c2ec425b8c7f569a331ce9bd1727fc to your computer and use it in GitHub Desktop.
Save ExileofAranei/24c2ec425b8c7f569a331ce9bd1727fc to your computer and use it in GitHub Desktop.
Lamodul: update modules, phpthumb images and etc.
<?php
$pdoFetch = $modx->getService('pdoFetch');
$parents = '30641';
$counter = 0;
$origin_context = null;
if ($modx->context->key == 'mgr') {
$origin_context = $modx->context->key;
$modx->switchContext('web');
}
$tvId = 85;
$relatedCategories = Array(
'ratio' => Array(
0 => 30988,
1 => 30989,
),
'count' => Array(
2 => 30983,
3 => 30984,
4 => 30985,
5 => 30986
)
);
$config = array(
'class' => 'msProduct',
'parents' => $parents,
'sortby' => 'msProduct.id',
'sortdir' => 'ASC',
'return' => 'data',
'templates' => 4,
'where' => array(
'class_key' => 'msProduct'
),
'limit' => 0,
);
// Merge all properties and run!
$pdoFetch->setConfig($config, false);
$products = $pdoFetch->run();
if ($products) {
foreach ($products as $idx => $product) {
$id = $product['id'];
$resource = $modx->getObject('msProduct', $id, array('ctx' => 'web'));
$module = ($resource->get('module_id'));
if ($module == 0 || !isset($module)) {
$savedModule = json_decode($resource->getTVValue($tvId), 1);
$modx->log(1, print_r($savedModule, 1));
if ($savedModule) {
$oldCategories = Array();
$oldCategories['ratio'] = $relatedCategories['ratio'][$savedModule['ratio']];
$oldCategories['count'] = $relatedCategories['count'][$savedModule['count']];
foreach ($oldCategories as $k => $v) {
unsetCategory($id, $v);
}
}
$modules = $modx->runSnippet('apiGetModules', Array(
'action' => 'getCount'
));
$modulesCount = sizeof($modules);
$rndModule = rand(1, $modulesCount);
$moduleId = $modules[$rndModule - 1];
$module = $modx->runSnippet('apiGetModules', Array(
'action' => 'getModuleData',
'id' => $moduleId
));
setModule($resource, $module);
setModuleTV($resource, $module, $tvId);
if ($resource->get('image')) {
setModuleImages($resource, $module);
} else {
$modx->log(1, print_r($resource->get('id') . ': ' . 'image is not set', 1));
}
$newCategories = Array();
$newCategories['ratio'] = $relatedCategories['ratio'][$module['ratio']];
$newCategories['count'] = $relatedCategories['count'][$module['count']];
foreach ($newCategories as $k => $v) {
setCategory($id, $v);
}
$counter += 1;
}
}
print_r('Completed for ' . $counter . ' products.');
if ($origin_context) {
$modx->switchContext($origin_context);
}
}
function setModule($resource, $module) {
$resource->set('module_id', $module['module']);
$resource->set('module_ratio', $module['ratio']);
$resource->set('module_count', $module['count']);
$resource->set('module_sold', getRandomSaled());
$result = $resource->save();
return $result;
}
function setModuleTV($resource, $module, $tvId) {
$output = Array();
$output['module'] = $module['module'];
$output['ratio'] = $module['ratio'];
$output['count'] = $module['count'];
$result = $resource->setTVValue($tvId, json_encode($output), 1);
return $result;
}
function setModuleImages($resource, $module) {
global $modx;
$image = $resource->get('image');
$moduleId = $module['module'];
if ($image && $moduleId) {
$imageMain = $modx->runSnippet('phpthumbon', array('input' => $image, 'options' => "w=1500&h=1000&aoe=1&zc=C&api=1&modul={$moduleId}&f=png"));
$imageOg = $modx->runSnippet('phpthumbon', array('input' => $image, 'options' => "w=968&h=504&aoe=1&api=1&modul={$moduleId}&f=png"));
$imageVk = $modx->runSnippet('phpthumbon', array('input' => $image, 'options' => "w=537&h=240&zc=C&api=1&modul={$moduleId}&f=png"));
$imageThumb = $modx->runSnippet('phpthumbon', array('input' => $image, 'options' => "w=150&h=100&zc=C&api=1&modul={$moduleId}&f=png"));
$resource->set('module_image_main', $imageMain);
$resource->set('module_image_og', $imageOg);
$resource->set('module_image_vk', $imageVk);
$resource->set('module_image_thumb', $imageThumb);
$result = $resource->save();
return $result;
} else {
$modx->log(1, print_r('setting module images failed'));
return 0;
}
}
function getRandomSaled() {
$rnd = Array();
$prepareRnd = Array(
0 => 35,
1 => 25,
2 => 20,
3 => 15,
4 => 5,
5 => 3,
6 => 2,
7 => 1
);
foreach ($prepareRnd as $k => $v) {
for ($i = 0; $i < $v; $i++) {
$rnd[] = $k;
}
}
return $rnd[rand(0, sizeof($rnd) - 1)];
}
function setCategory($pid, $cid) {
global $modx;
$res = $modx->newObject('msCategoryMember');
$res->set('product_id', $pid);
$res->set('category_id', $cid);
$result = $res->save();
return $result;
}
function unsetCategory($pid, $cid) {
global $modx;
$table = $modx->getTableName('msCategoryMember');
$result = $modx->exec("DELETE FROM {$table} WHERE `product_id` = {$pid} AND `category_id` = {$cid};");
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment