File: D:/HostingSpaces/SBogers10/csb.komma.pro/app/Vacancies/Kms/VacancyController.php
<?php
namespace App\Vacancies\Kms;
/**
* @author Komma <info@komma.pro>
* @copyright (c) 2012-2016, Komma
*/
use App\Attributes\Attribute;
use App\Attributes\ComponentArea;
use App\Components\ComponentArea\ComponentAreaService;
use App\Vacancies\Models\Vacancy;
use App\Vacancies\Models\VacancyTranslation;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Komma\KMS\Core\AbstractTranslatableModel;
use Komma\KMS\Core\AbstractTranslationModel;
use Komma\KMS\Core\SectionController;
final class VacancyController extends SectionController
{
/**
* @var ComponentAreaService
*/
private $componentAreaService;
protected $slug = "vacancies";
protected $classModelName = Vacancy::class;
protected $forTranslationModelName = VacancyTranslation::class;
function __construct()
{
$vacancySection = new VacancySection($this->slug);
parent::__construct($vacancySection);
$this->modelService = new VacancyModelService();
$this->componentAreaService = new ComponentAreaService();
}
/**
* @param Model $model
* @param Collection|null $attributesByValueFrom
* @return Model
*/
protected function save(Model $model, Collection $attributesByValueFrom = null): Model
{
$model = parent::save($model, $attributesByValueFrom);
$this->forEachComponentArea($model, $attributesByValueFrom->get(Attribute::ValueFromComponentArea), function(AbstractTranslationModel $translation, ComponentArea $attribute) use ($model) {
//If the translation model does not exist, and the component area is not empty, we need to save the translation.
if(!$translation->exists && $attribute->getValue() !== '[]') {
$translation->translatable()->associate($model);
$translation->save();
}
$this->componentAreaService->saveAttribute($translation, $attribute);
});
return $model;
}
/**
* A function that MUST delegate to services to accomplish loading.
* It MUST NOT act like a service.
*
* @param Model $model
* @param Collection $attributesByValueFrom Keys must be ValueFrom integers. Values must be Attributes that have the ValueFrom integers
* @return Collection The same collection as you've passed in. Only "filled".
*/
protected function load(Model $model, Collection $attributesByValueFrom = null): Collection
{
$attributesByValueFrom = parent::load($model, $attributesByValueFrom);
$this->forEachComponentArea($model, $attributesByValueFrom->get(Attribute::ValueFromComponentArea), function(AbstractTranslationModel $translation, ComponentArea $attribute) {
$this->componentAreaService->loadAttribute($translation, $attribute);
});
return $attributesByValueFrom;
}
/**
* @param AbstractTranslatableModel $model
* @param Collection $componentAreaAttributes
* @param string $do
*/
protected function forEachComponentArea(AbstractTranslatableModel $model, Collection $componentAreaAttributes, \Closure $do)
{
if($this->forTranslationModelName && $componentAreaAttributes->count() > 0) {
if($componentAreaAttributes) {
$componentAreaAttributes->each(function (Attribute $attribute) use ($model, $do) {
if (($language = $attribute->getAssociatedLanguage()) && $model) {
$translationModel = $this->translationService->getTranslationModelForModelByLanguage($model, $language);
if ($translationModel) $do->call($this, $translationModel, $attribute);
}
});
}
}
}
}