HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers33/bbec.nl/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)
    {
        if($type != null && $type != 'all'){
            $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[$record->navigation_label] = $blockEntity;
        }
        ksort($entities);
        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' => \Input::get('code_name'),
                'navigation_label' => \Input::get('navigation_label'),
            ]);
        }

        $record->view = \Input::get('view');

        if($entity->type != null && $entity->type != 'all'){
            $record->type = $entity->type;
        }
        elseif($this->type != null  && $this->type != 'all'){
            $record->type = $this->type;
        }
        else{
            $record->type = \Input::get('type');
        }

        $record->save();

        // Append id to entity for restfull routing
        $entity->id = $record->id;

        //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);
            $description = \Input::get('description_' . $language->id);

            //load the DynamicCaseService
            $dps = \App::make('Komma\Kms\Core\Services\DynamicPageService');
            $description = $dps->prepareDynamicDescription(json_decode($description), $record, true);

            $translation->description = $description;

            $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]);
    }

}