File: D:/HostingSpaces/SBogers10/kemi.komma.pro/app/KommaApp/Kms/Core/Attributes/KmsImages.php
<?php
/**
*
*
* @author Komma <info@komma.pro>
* @copyright (c) 2012-2016, Komma
*/
namespace App\KommaApp\Kms\Core\Attributes;
use Illuminate\Database\Eloquent\Collection;
use App\KommaApp\Images\ImageService;
use App\KommaApp\Images\Models\Image;
use View;
use File;
class KmsImages extends KmsAttribute
{
public $label;
public $placeholder;
public $subfolder;
public $maxImages;
public $modelId;
public $forModel;
/**
* @var array $uploadSizes an array containing one or more of these keys: small, medium, large.
* each of these keys again has an array containing keys: width, height, and method. These
* hold the width and height as a number and the method as a string containing one of these values:
* crop, fit.
*/
public $uploadSizes;
protected $fullPath;
protected $path;
function __construct($key, $value, array $options = [], array $errors = [], $siteId = null, $languageId = null, $section = null)
{
parent::__construct($key, $value, $options, $errors, $siteId, $languageId, $section);
$this->label = $this->getOption('label', $key);
$this->placeholder = $this->getOption('placeholder', $key);
$this->subfolder = $this->getOption('subfolder', null);
$this->maxImages = $this->getOption('maxImages', null);
$this->modelId = ($section) ? $section->getModelId() : null;
$this->forModel = ($section) ? $section->getModelName() : null;
$this->uploadSizes = $this->getAndValidateUploadSizesFromOptions();
$this->fullPath = $this->subfolder == null ? \Config::get('kms.paths.full_path_images') : \Config::get('kms.paths.full_path_images') . '/' . $this->subfolder;
$this->path = $this->subfolder == null ? \Config::get('kms.paths.path_images') : \Config::get('kms.paths.path_images') . '/' . $this->subfolder;
}
public function process()
{
$this->uploadAndDeleteImages();
}
public function render()
{
$view = View::make('kms/attributes.images', [
'attribute' => $this
]);
return $view;
}
public function getValue($post = false)
{
//Check if valueKey is set
if ($post == false && $valueKey = $this->getOption('valueKey', null)) {
if (!$value = $this->arr->get($this->value, $valueKey)) $this->value;
return $value;
}
return $this->value;
}
public function getValueAsJson()
{
return $this->getValue() ? json_encode($this->getValue()) : '[]';
}
public function delete()
{
$this->deleteImages();
}
protected function deleteImages()
{
$images = (is_array($this->value)) ? $this->value : json_decode($this->value);
foreach ($images as $image) {
if (File::exists(public_path() . $image->original))
File::delete(public_path() . $image->original);
if (File::exists(public_path() . $image->large))
File::delete(public_path() . $image->large);
if (File::exists(public_path() . $image->medium))
File::delete(public_path() . $image->medium);
if (File::exists(public_path() . $image->small))
File::delete(public_path() . $image->small);
if (File::exists(public_path() . $image->thumb))
File::delete(public_path() . $image->thumb);
}
}
protected function uploadAndDeleteImages()
{
//value is set in the kms section. See setValue calls over there. When you post a form with file inputs you'll hit this flow UserController: SectionController->store, kmsSection->save, kmsSection->renderInput
if ($this->value) {
$images = (is_array($this->value)) ? $this->value : json_decode($this->value);
if (!$images) {
$this->value = '';
return;
}
//Put images to upload in the $filesToUpload array. And images to delete in the $filesToDelete array. And images that not changed in the notChanged array. This latter one isn't used but added for completion
$notChangedFiles = new Collection();
$filesToUpload = new Collection();
$filesToDelete = new Collection();
foreach($images as $imageObject) {
if(property_exists($imageObject, "delete")) {
//The image is a modified one.
if ($imageObject->delete == false) {
$currentImage = \Input::file($imageObject->name);
$filesToUpload->add($currentImage);
} else {
$currentImage = Image::find($imageObject->id);
$filesToDelete->add($currentImage);
}
} else {
//The image did not change so we should not do anything with it.
$currentImage = Image::find($imageObject->id);
$notChangedFiles->add($currentImage);
}
}
$imageService = new ImageService();
$imageService->setSubFolder($this->subfolder);
$uploadedImages = $imageService->uploadImages($filesToUpload, $this->uploadSizes); //returns an numeric indexed array containing arrays representing images. the keys of the image arrays can be: original large medium small thumb
$deletedImages = $imageService->deleteImages($filesToDelete); //returns an numeric indexed array containing arrays representing images. the keys of the image arrays can be: original large medium small thumb
$this->value = [
'uploaded' => $uploadedImages,
'deleted' => $deletedImages,
'notChanged' => $notChangedFiles
];
}
}
/**
* Does look at the options small, medium and large in a section that uses this KmsImages class,
* and if they exist, validate that they are correctly formatted. After validation passes they
* wil be put in the $uploadSizes variable in this class.
*
* @throws \InvalidArgumentException if the small, medium, or large options are not correctly formatted.
* @return array $presentSizes Containing one of these keys matching the keys with the values from the section this
* KmsImages instance is used in: thumb, small, medium, large, original. The values from those keys contain an array
* with keys: width, height and method. Width and height are numbers, and method is as string containing crop or fit.
*/
private function getAndValidateUploadSizesFromOptions() {
$presentSizes = [];
$size = $this->getOption('thumb', false);
if($size) $presentSizes['thumb'] = $size;
$size = $this->getOption('small', false);
if($size) $presentSizes['small'] = $size;
$size = $this->getOption('medium', false);
if($size) $presentSizes['medium'] = $size;
$size = $this->getOption('large', false);
if($size) $presentSizes['large'] = $size;
$size = $this->getOption('original', false);
if($size) $presentSizes['original'] = $size;
//Validate each size and throw exception if not valid
foreach($presentSizes as $sizeName => $size) {
if(!is_array($size)) throw new \InvalidArgumentException('The option '.$sizeName.' of a KmsImages attribute (defined in the $modelAttributesData array of a section) should be an array containing keys method, with and height. But was a: '.typeOf($size));
if(!isset($size['width']) || !isset($size['height']) || !isset($size['method'])) throw new \InvalidArgumentException('The option '.$sizeName.' of a KmsImages attribute (defined in the $modelAttributesData array of a section) should be an array containing keys method, with and height. But did not have one or more of them: ');
}
//We are still alive at this point so we can safely take the options from the section and return them
return $presentSizes;
}
}