<?PHP

class ImagesAdmin extends Buyers
{
    public function fetch()
    {
        $images = array();
        $allowed_extentions = array('png', 'gif', 'jpg', 'jpeg', 'ico', 'webp', 'svg');
        $images_dir = "design/{$this->settings->theme}/images/";

        if($dir = $this->request->get('dir') ?? null)
        {
            $dir = str_replace(array('../', '..\\'), '', $dir).'/';
            $images_dir .= $dir;
        }

        // Сохраняем
        if($this->request->method('post'))
        {
            $old_names = $this->request->post('old_name');
            $new_names = $this->request->post('new_name');

            if(is_array($old_names))
            foreach($old_names as $i=>$old_name)
            {
                $new_name = $new_names[$i];
                $new_name = trim(pathinfo($new_name, PATHINFO_FILENAME).'.'.pathinfo($old_name, PATHINFO_EXTENSION), '.');

                if(is_writable($images_dir) && is_file($images_dir.$old_name) && !is_file($images_dir.$new_name))
                    rename($images_dir.$old_name, $images_dir.$new_name);
                elseif(is_file($images_dir.$new_name) && $new_name!=$old_name)
                    $message_error = 'name_exists';
            }

            $delete_image = trim($this->request->post('delete_image'), '.');

            if(!empty($delete_image))
                @unlink($images_dir.$delete_image);

            // Загрузка изображений
            if($images = $this->request->files('upload_images'))
            {
                for($i=0; $i<count($images['name']); $i++)
                {
                    $name = trim($images['name'][$i], '.');
                    if(in_array(strtolower(pathinfo($name, PATHINFO_EXTENSION)), $allowed_extentions))
                        move_uploaded_file($images['tmp_name'][$i], $images_dir.$name);
                }
            }

            if(!isset($message_error))
                $this->request->redirect($_SERVER['REQUEST_URI']);
            else
                $this->design->assign('message_error', $message_error);
        }

        // Чтаем все файлы
        $content = $this->custom->scan_dir($images_dir, $allowed_extentions);
        foreach($content->files AS $file)
        {
            $size = filesize($images_dir.$file);
            list($width, $height) = @getimagesize($images_dir.$file);
            $image = (object) array('name'=>$file, 'size'=>$size, 'width'=>$width, 'height'=>$height);
            $images[$file] = $image;
        }

        // Если нет прав на запись - передаем в дизайн предупреждение
        if(!is_writable($images_dir))
            $this->design->assign('message_error', 'permissions');

        $this->design->assign('current_dir', $dir);
        $this->design->assign('dirs', $content->dirs);
        $this->design->assign('theme', $this->settings->theme);
        $this->design->assign('images', $images);
        $this->design->assign('images_dir', $images_dir);
        return $this->design->fetch('images.tpl');
    }
}
