ACC SHELL
<?php
class Config extends AppModel
{
var $name = 'Config';
var $primaryKey = 'conf_name';
/**
*
*/
function updateImages($base = NULL)
{
static $files = array();
if ($base == NULL) {
$files = array();
$dir = dirname(__FILE__) . '/../webroot/img/products';
}
else {
$dir = $base;
}
$dh = opendir($dir);
while ($fname = readdir($dh)) {
if ($fname[0] == '.') {
continue;
}
if (is_dir($dir . '/' . $fname)) {
$this->updateImages($dir . '/' . $fname);
}
else {
$files[] = $dir . '/' . $fname;
}
}
closedir($dh);
if (! $base) {
$map = array();
sort($files);
foreach ($files as $fname) {
/*
* Images are matched by the index map: dir1/dir2, so get the
* first parent directory from the img name for the index and
* remove initial numbers "02_", eg. create:
*
* Termokamery
* from:
* ..products/02/02_Termokamery/05_Fluke Ti45/Fluke Ti45.jpg
*/
$parent = basename(dirname(dirname($fname)));
$parent = preg_replace('/^\d+(-\d+)?_?/', '', $parent);
/*
* Now get the second parent:
* Fluke Ti45
*/
$idx = basename(dirname($fname));
$idx = preg_replace('/^\d+(-\d+)?_/', '', $idx);
/*
* Final index for the map is: termokamery/fluke-ti45
*/
$map[indexFromName($parent) . '/'. indexFromName($idx)][] = array(
'filename' => preg_replace('/.*products\\//', 'products/', $fname),
'alt' => preg_replace('/(\d+_)(.*?)\\.\S+$/', '\\2', basename($fname)),
);
}
foreach ($this->query("
SELECT t1.id, t1.name AS name, t2.name AS parent FROM products t1
LEFT JOIN products t2 ON (t1.parent = t2.id)
") as $row) {
$parent = indexFromName(__latin2ascii($row['t2']['parent']));
$name = indexFromName(__latin2ascii($row['t1']['name']));
$idx = "$parent/$name";
$this->query("UPDATE products SET img_name = '$idx' WHERE id = '{$row['t1']['id']}'");
}
$this->del('images');
$this->save(array(
'conf_name' => 'images',
'conf_value' => serialize($map),
'conf_type' => 'A',
));
}
}
/**
*
*/
function updateDownloads($base = NULL)
{
static $files = array();
if ($base == NULL) {
$files = array();
$dir = dirname(__FILE__) . '/../data/products';
}
else {
$dir = $base;
}
$dh = opendir($dir);
while ($fname = readdir($dh)) {
if ($fname[0] == '.') {
continue;
}
if (is_dir($dir . '/' . $fname)) {
$this->updateDownloads($dir . '/' . $fname);
}
else {
$files[] = $dir . '/' . $fname;
}
}
closedir($dh);
if (! $base) {
$map = array();
sort($files);
foreach ($files as $fname) {
if (strpos($fname, '/products/sw/')) {
$idx = 'software';
}
else {
$idx = basename(dirname($fname));
$idx = preg_replace('/^\d+(-\d+)?_/', '', $idx);
}
preg_match('/\.([^.]+)$/', $fname, $m);
$ext = low($m[1]);
$map[indexFromName($idx)][] = array(
'filename' => $fname,
'name' => preg_replace('/^(\d+_)?(.*?)\\.[^.]+$/', '\\2', basename($fname)),
'href' => indexFromName($idx),
'ext' => $ext,
// because precision when serializing
'size' => (string) round(filesize($fname) / 1024, 1),
);
}
$map = serialize($map);
$this->del('downloads');
$this->save(array(
'conf_name' => 'downloads',
'conf_value' => $map,
'conf_type' => 'A',
));
}
}
}
?>
ACC SHELL 2018