HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/vanderkampen.komma.pro/app/KommaApp/Kms/Core/Attributes/KmsDynamic.php
<?php

/**
 * Short description for the file.
 *
 * @author      Komma <support@komma.pro>
 * @copyright   (c) 2012-2015, Komma Mediadesign
 */

namespace App\KommaApp\Kms\Core\Attributes;



use App\KommaApp\Images\Models\Image;

class KmsDynamic extends KmsAttribute
{
    public $label;
    public $subFolder;
    public $blockSettings;
    public $show;
    public $translations = [];
    public $currentEntityType;
    public $currentEntityId;
    public $routeParameters;
    public $forModel;
    public $modelId;
    public $section;

//    function __construct($key, $value, array $options = [], array $errors = [], $languageId = null, $section = null)
    function __construct($key, $value, array $options = [], array $errors = [], $siteId = null, $languageId = null, $section = null)
    {
        parent::__construct($key, $value, $options, $errors, $languageId, $section);
        $this->section = $section;
        $this->subFolder = $this->getOption('subFolder', null);
        $this->label = $this->getOption('label', $key);
        $this->blockSettings = json_encode($this->getOption('blockSettings', $key));
        $this->show = true;
        $this->translations = \Lang::get("kms/dynamic");
        $this->forModel = $this->section->getModelName();
        $this->modelId = $this->section->getModelId();
    }

    public function setValue($value)
    {
        //Delete old images if needed and remove the fileIdsToDelete array from the blocks as they aren't needed anymore
        $regularArray = json_decode($value,true);
        $someFilesCouldNotBeDeleted = false;
        foreach($regularArray as $blockIndex => $blockData)
        {
            if(isset($blockData['fileIdsToDelete'])) {

                foreach($blockData['fileIdsToDelete'] as $key => $fileId) {
                    $file = Image::find($fileId);

                    if(!$file) {
                        //There was no image in the database with that id. Let's remove it for that reason. Could occur because of debugging.
                        unset($regularArray[$blockIndex]['fileIdsToDelete'][$key]);
                        continue;
                    }

                    $fileUrlsToDelete = [];
                    if($file->large_image_url) $fileUrlsToDelete[] = $file->large_image_url;
                    if($file->medium_image_url) $fileUrlsToDelete[] = $file->medium_image_url;
                    if($file->small_image_url) $fileUrlsToDelete[] = $file->small_image_url;
                    if($file->thumb_image_url) $fileUrlsToDelete[] = $file->thumb_image_url;

                    foreach($fileUrlsToDelete as $fileUrl) {
                        $path = public_path($fileUrl);

                        if(file_exists($path) == false) continue;
                        $deleted = unlink($path);
                        if($deleted == false) $someFilesCouldNotBeDeleted = true;
                    }

                    Image::destroy($fileId);
                    unset($regularArray[$blockIndex]['fileIdsToDelete'][$key]);
                }

                if(count($blockData['fileIdsToDelete']) == 0) {
                    unset($regularArray[$blockIndex]['fileIdsToDelete']);
                }
            }
        }
        $value = json_encode($regularArray);

        parent::setValue($value);

        if($someFilesCouldNotBeDeleted) throw new \InvalidArgumentException("KmsDynamic could not remove some files while it was needed.");
        return;
    }


    public function render()
    {
        return \View::make('kms.attributes.dynamic', [
            'attribute' => $this,
            'translations' => $this->translations,
        ])->render();}
}