File: D:/HostingSpaces/EUmans/umansradepo.be/workbench/komma/kms/src/Komma/Kms/Blocks/BlockRepository.php
<?php
/**
* @author Komma <support@komma.pro>
* @copyright (c) 2012-2016, Komma Mediadesign
*/
namespace Komma\Kms\Blocks;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Route;
use Komma\Kms\Core\Kms;
use Komma\Kms\Core\KmsRepository;
use Komma\Kms\Blocks\Models\Block;
use Komma\Kms\Blocks\Models\BlockTranslation;
use Komma\Kms\Images\ImageService;
use Komma\Kms\Images\Models\Image;
class BlockRepository extends KmsRepository
{
private $imageService;
public $type;
/**
* Call the parent __construct
* inject the dependencies.
*
* BlockRepository constructor.
* @param Kms $kms
* @param ImageService $imageService
*/
function __construct(Kms $kms, ImageService $imageService)
{
parent::__construct($kms);
$this->imageService = $imageService;
}
public function newEntity()
{
$block = new Block();
$entity = new BlockEntity($block->toArray());
foreach ($this->kms->getCurrentLanguages() as $language) {
$entity->addTranslationEntity(new BlockTranslationEntity($language->id));
}
return $entity;
}
public function getEntity($id)
{
if ($id == null) return $this->newEntity();
$model = Block::find($id);
// Transform for page_id
if (!$model) return false;
$entity = new BlockEntity($model->toArray());
$entity->images = $this->getImages($id, 'Komma\\Kms\\Blocks\\Models\\Block');
foreach ($model->translations as $translation) {
$translation->images_lang = $this->getImages($id, 'Komma\\Kms\\Blocks\\Models\\Block', 'images_lang_'.$translation->language_id);
$entity->addTranslationEntity(
new BlockTranslationEntity(
$translation->language_id,
$translation->attributesToArray()
)
);
}
return $entity;
}
public function getEntities($type = null)
{
if($type != null){
$records = Block::where('type', '=', $type)->get();
}
else{
$records = Block::all();
}
$entities = [];
foreach ($records as $record) {
$blockEntity = new BlockEntity($record->toArray());
if ($images = $this->getImages($record['id'], 'Komma\\Kms\\Blocks\\Models\\Block')) {
$blockEntity->thumbnail = $images['0']['thumb_image_url'];
}
$entities[] = $blockEntity;
}
return $entities;
}
/**
* @param BlockEntity $entity
* @return BlockEntity
*/
public function saveEntity(BlockEntity $entity)
{
$record = Block::firstOrNew(['id' => $entity->id]);
if (\Input::has('code_name')) {
$record->fill([
'code_name' => helper_underscoreAll(\Input::get('code_name')),
'navigation_label' => \Input::get('navigation_label'),
]);
}
$record->view = \Input::get('view');
if($this->type != null){
$record->type = $this->type;
}
else{
$record->type = \Input::get('type');
}
$record->save();
//save the translations
foreach ($this->kms->getCurrentLanguages() as $language) {
$translation = BlockTranslation::firstOrNew(['block_id' => $record->id, 'language_id' => $language->id]);
$translation->name = \Input::get('name_' . $language->id);
$translation->description = \Input::get('description_' . $language->id);
$translation->save();
//Link the images to this model
$this->imageService->linkImagesToModel('images_lang_'.$language->id, $record);
}
//Link the images to this model
$this->imageService->linkImagesToModel('images', $record);
return $entity;
}
public function destroyEntity($id)
{
$this->deleteImages($id, 'Komma\\Kms\\Blocks\\Models\\Block');
$entity = Block::find($id);
$entity->delete();
}
public function getForSelect($languageId = null, $excludeId = null)
{
$blocks = $this->getEntities();
$entities = [];
foreach ($blocks as $block) {
$entity = [];
$entity['value'] = $block->id;
$entity['fullValue'] = json_encode([$block->code_name]);
// End QnD !!!
$entity['content'] = $block->code_name;
$entity['htmlContent'] = $block->code_name;;
if (!$entity['htmlContent']) $entity['htmlContent'] = '\\';
$entities[] = $entity;
}
return $entities;
}
public function deleteImages($id, $type){
Image::where('imageble_id', $id)
->where('imageble_type', $type)
->update(['imageble_id' => 0]);
}
}