File: D:/HostingSpaces/centrum8a/centrum8a.com/app/KommaApp/Images/ImageController.php
<?php
/**
* Short description for the file.
*
* @author Komma <support@komma.pro>
* @copyright (c) 2012-2015, Komma Mediadesign\
* Example usage: See fileupload.ng.js uploadFiles function
*/
namespace App\KommaApp\Images;
use App\Http\Controllers\Controller;
use App\KommaApp\Documents\Kms\DocumentService;
use App\KommaApp\Images\Models\Image;
class ImageController extends Controller
{
private $imageService;
private $fileService;
public function __construct(ImageService $imageService, DocumentService $fileService)
{
$this->imageService = $imageService;
$this->fileService = $fileService;
}
public function upload()
{
if(\Input::has('forModelName')) $forModelName = \Input::get("forModelName");
if(\Input::has('forModelId')) $forModelId = \Input::get("forModelId");
if(\Input::has('forBlockId')) $forBlockId = \Input::get("forBlockId");
if(\Input::has('forTabId')) $forTabId = \Input::get("forTabId");
//Set subFolder
if(\Input::has('subFolder')) $this->imageService->setUp('subfolder', \Input::get('subFolder'));
//Set attributeKey
if(\Input::has('attribute_key')) $this->imageService->setUp('attribute_key',\Input::get('attribute_key'));
$uploadSizes = [];
//Get the uploadSizes
if(\Input::has('uploadSizes') && is_array(\Input::get('uploadSizes')))$uploadSizes = \Input::get('uploadSizes');
// $dynamic = false;
//Get if is dynamic
// if(\Input::has('dynamic'))$dynamic =(\Input::get('dynamic') == 'true'?true:false);
//upload the files to the server
if (!$result = $this->imageService->uploadImages([\Input::file('file')],$uploadSizes)) return null;
//And link them to the database
if(isset($forModelName) && isset($forModelId) && isset($forBlockId)) {
//Add the model name and id for each image.
$imageDataAmount = count($result);
for($index = 0; $index < $imageDataAmount; $index++) {
$result[$index]['forModelname'] = $forModelName;
$result[$index]['forModelId'] = $forModelId;
$result[$index]['forBlockId'] = $forBlockId;
if(isset($forTabId)) $result[$index]['forTabId'] = $forTabId;
}
//Make a updateImageReferencesInDatabase compatible 'uploaded', 'deleted' array
$uploadedAndDeletedImages = ['uploaded' => $result];
$result = $this->imageService->updateImageReferencesInDatabase($uploadedAndDeletedImages);
}
return $result;
}
public function delete($id) {
$image = Image::find($id);
if(!$image) throw new \InvalidArgumentException("There isn't an image with id that could be deleted: ".$id);
$collection = collect($image);
$this->imageService->deleteImages($collection);
if(\Input::has('forBlockId')) $forBlockId = \Input::get("forBlockId");
return [
'id' => $id,
'forBlockId' => (isset($forBlockId)) ? $forBlockId : null,
];
}
public function getImageData($fileIds = null)
{
if(isset($fileIds)) $fileIdsToRetrieve = $fileIds;
else $fileIdsToRetrieve = \Input::get('fileIds');
if(!$fileIdsToRetrieve) return [];
return $this->imageService->getDynamicBlockImages($fileIdsToRetrieve);
}
}