<?php
if (!isset($config)) require_once('config.php');

define("DEBUG_SYNC_PALYAZAT", "No");

$targetDate = 27;
$targetDir  = $folder . '/public/img/rendering_palyazat_2023/' . date('Ym');
$sourceDir  = $folder . '/public/userupload/rendering_palyazat/' . date('Ym');

if ((int)date('j') == $targetDate || DEBUG_SYNC_PALYAZAT == "Yes") {
    // palyazat
    if (!is_dir($targetDir))
        mkdir($targetDir);

    $dirs = array();
    foreach (scandir($sourceDir) as $dir)
        if (!in_array($dir, array(".", ".."))) $dirs[] = $dir;

    if (count($dirs)) {
        foreach ($dirs as $dir) {
            $files = array();

            if (is_dir($sourceDir . '/' . $dir)) {
                foreach (scandir($sourceDir . '/' . $dir) as $file)
                    if (!in_array($file, array(".", ".."))) $files[] = $file;
            }
            if (count($files)) {
                foreach ($files as $file) {
                    if (is_file($sourceDir . "/" . $dir . "/" . $file))
                        var_dump(copy($sourceDir . '/' . $dir . '/' . $file, $targetDir . "/" . $dir . "-" . $file));
                }
            }
        }
    }
}
