Skip to content

Instantly share code, notes, and snippets.

@webnitros
Created February 26, 2020 10:17
Show Gist options
  • Select an option

  • Save webnitros/6129a171608aa278f6bed09fa8b86fe0 to your computer and use it in GitHub Desktop.

Select an option

Save webnitros/6129a171608aa278f6bed09fa8b86fe0 to your computer and use it in GitHub Desktop.
Генерация файла со всеми изображениями
<?php
/*header('Access-Control-Allow-Origin: *');
header("Content-type: application/json; charset=utf-8");*/
ini_set('display_errors', 1);
ini_set("max_execution_time", 1000);
define('MODX_API_MODE', true);
require 'index.php';
if (!$modx->getAuthenticatedUser('mgr')) {
die('Access is denied');
}
$path = "assets/data/images.csv";
$path_save = MODX_BASE_PATH . $path;
if (file_exists($path_save)) {
unlink($path_save);
}
if (!file_exists("images.csv")) {
$csv = chr(239) . chr(187) . chr(191);
$csv .= 'ариткул;vendor;category;sub_category;MAIN_фото;MAIN_width;MAIN_height;фото;width;height;фото;width;height;фото;width;height;фото;width;height;фото;width;height;фото;width;height;фото;width;height;фото;width;height;фото;width;height;фото;width;height;фото;width;height;фото;width;height;фото;width;height;фото;width;height;фото;width;height;фото;width;height;фото;width;height;фото;width;height;фото;width;height;фото;width;height;фото;width;height;фото;width;height;фото;width;height;фото;width;height;фото;width;height;фото;width;height;фото;width;height;фото;width;height;фото;width;height;фото;width;height;фото;width;height;' . PHP_EOL;
file_put_contents("images.csv", $csv);
$csv = "";
}
$images = [];
$q = $modx->newQuery('msProductFile');
$q->select('product_id,properties,url');
if ($q->prepare() && $q->stmt->execute()) {
while ($row = $q->stmt->fetch(PDO::FETCH_ASSOC)) {
$properties = $modx->fromJSON($row['properties']);
$product_id = $row['product_id'];
$images[$product_id][] = [
'url' => $row['url'],
'width' => $properties['width'],
'height' => $properties['height']
];
}
}
$ids = [];
$q = $modx->newQuery('msProduct');
$q->select('id');
$q->where(array(
'published' => true,
));
if ($q->prepare() && $q->stmt->execute()) {
while ($row = $q->stmt->fetch(PDO::FETCH_ASSOC)) {
$ids[] = $row['id'];
}
}
$output = '';
$q = $modx->newQuery('msProductData');
$q->select("id,image,show_artikul,vendor_code,category,sub_category");
$q->where(array(
'id:IN' => $ids,
));
if ($q->prepare() && $q->stmt->execute()) {
while ($row = $q->stmt->fetch(PDO::FETCH_ASSOC)) {
$rcsv = null;
$i = $row['id'];
$M = $row['image'];
$a = $row['show_artikul'];
$v = $row['vendor_code'];
$c = $row['category'];
$s = $row['sub_category'];
$rcsv .= "$a;$v;$c;$s";
if (array_key_exists($i, $images)) {
$files = $images[$i];
foreach ($files as $image) {
$file = $image['url'];
$w = $image['height'];
$h = $image['height'];
$rcsv .= ";$file;$w;$h";
}
}
file_put_contents($path_save, $rcsv . PHP_EOL, FILE_APPEND);
}
}
echo '<a href="/' . $path . '">Скачать</a>';
function getDirContents($dir, &$results = array())
{
$files = scandir($dir);
foreach ($files as $key => $value) {
$path = realpath($dir . DIRECTORY_SEPARATOR . $value);
if (!is_dir($path)) {
$results[] = $path;
} else if ($value != "." && $value != "..") {
getDirContents($path, $results);
$results[] = $path;
}
}
return $results;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment