<?php

/**
 * This file contains package_ecoyn_core_ajax_logoUpload
 */

/**
 * @return array
 */

use Ecoyn\Setup\PackageConfig;
use QUI\Projects\Media\Folder;

QUI::getAjax()->registerFunction(
    'package_ecoyn_core_ajax_logoUpload',
    function ($File) {
        $file = $File->getAttribute('filepath');

        if (!file_exists($file)) {
            return;
        }

        $Project = QUI::getProjectManager()->getStandard();

        if ($Project === null) {
            return;
        }

        $Media = $Project->getMedia();
        $Folder = $Media->firstChild();

        $File = $Folder->uploadFile(
            $file,
            Folder::FILE_OVERWRITE_TRUE
        );

        $File->activate(QUI::getUsers()->getSystemUser());

        $ErpConfig = PackageConfig::get('quiqqer/erp');
        $ErpConfig->setValue('general', 'logo', $File->getUrl());
        $ErpConfig->save();
    },
    ['File'],
    'Permission::checkAdminUser'
);
