File: D:/HostingSpaces/vanderkampen/kwaliteitsbouw.com/app/KommaApp/Kms/Core/Attributes/Dynamic.php
<?php
namespace App\KommaApp\Kms\Core\Attributes;
use App\KommaApp\Images\Models\Image;
use App\KommaApp\Kms\Core\Attributes\Models\AbstractBlockSettings;
use App\KommaApp\Kms\Core\Attributes\Traits\LabelTrait;
use App\KommaApp\Kms\Core\Sections\KmsSiteSection;
use App\KommaApp\Kms\Core\Sections\Section;
use Illuminate\Database\Eloquent\Model;
/**
* Class Dynamic
* @package App\KommaApp\Kms\Core\Attributes
*/
class Dynamic extends Attribute
{
use LabelTrait;
/** @var string $subFolder This is the subfolder of the path public/uploads/images where the images for this attribute wil be placed in*/
private $subFolder;
/** @var AbstractBlockSettings[] $blockSettings */
private $blockSettings;
public $dynamicLock = false;
/**
* Dynamic constructor.
*/
public function __construct()
{
parent::__construct();
}
/**
* Returns a view that visually represents this attribute
*
* @return \Illuminate\View\View
*/
public function render()
{
//retrieve model and model id.
$model = null;
$request = request();
$route = $request->route();
if(method_exists($route->controller, 'getSection') == false) throw new \RuntimeException("The controller: '".get_class($route->controller)."' did not have a public 'getSection' method while it should have one.");
/** @var Section $section */
$section = $route->controller->getSection();
$model = $section->getSectionService()->getModel();
// dd($model);
return \View::make('kms/attributes.dynamic', [
'attribute' => $this,
'forModel' => $model
]);
}
/**
* @return string
*/
public function getSubFolder(): string
{
return $this->subFolder;
}
/**
* @param string $subFolder
* @return Dynamic
*/
public function setSubFolder(string $subFolder): Dynamic
{
$this->subFolder = $subFolder;
return $this;
}
/**
* @param bool $asJson
* @return AbstractBlockSettings[]|string
*/
public function getBlockSettings($asJson = true)
{
if($asJson == false) return $this->blockSettings;
$toEncode = [];
foreach($this->blockSettings as $blockSetting) {
$toEncode[$blockSetting->getBlockType()] = $blockSetting->jsonSerialize();
}
$blockSettings = json_encode($toEncode);
return $blockSettings;
}
/**
* @param AbstractBlockSettings[] $blockSettings
* @return Dynamic
*/
public function setBlockSettings(array $blockSettings): Dynamic
{
$this->blockSettings = $blockSettings;
return $this;
}
public function setValue(string $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 isDynamicLock(): bool
{
return $this->dynamicLock;
}
public function setDynamicLock()
{
$this->dynamicLock = true;
return $this;
}
}