File: D:/HostingSpaces/SBogers93/fitale.nl/workbench/komma/kms/src/Komma/Kms/Blocks/BlockRepository.php
<?php
/**
* @author Tim Van Samang <timvansamang@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;
class BlockRepository extends KmsRepository
{
private $imageService;
/**
* 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) {
$entity->addTranslationEntity(
new BlockTranslationEntity(
$translation->language_id,
$translation->attributesToArray()
)
);
}
return $entity;
}
public function getEntities()
{
$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')),
]);
}
$record->view = \Input::get('view');
$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', $record);
return $entity;
}
public function destroyEntity($id)
{
if ($images = $this->getImages($id, 'Komma\\Kms\\Blocks\\Models\\Block')) {
//Delete the images
$images = $this->imageSerivce->getImages();
$this->imageSerivce->deleteImages($images);
}
$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;
}
}