File: D:/HostingSpaces/SBogers33/bbec.nl/workbench/komma/kms/src/Komma/Kms/Projects/ProjectRepository.php
<?php
/**
* Short description for the file.
*
* @author Komma <info@komma.pro>
* @copyright (c) 2012-2015, Komma Mediadesign
*/
namespace Komma\Kms\Projects;
use Carbon\Carbon;
use Illuminate\Support\Collection;
use Komma\Kms\Core\Entities\KmsHierarchicalListItemEntity;
use Komma\Kms\Core\Kms;
use Komma\Kms\Core\KmsRepository;
use Komma\Kms\Core\Routes\KmsRoute;
use Komma\Kms\Core\Routes\RoutableInterface;
use Komma\Kms\Core\Tree\Tree;
use Komma\Kms\Images\ImageRepository;
// Change these into new entity-type
use Komma\Kms\Images\ImageService;
use Komma\Kms\Languages\Language;
use Komma\Kms\Projects\Models\Project;
use Komma\Kms\Projects\Models\ProjectTranslation;
class ProjectRepository extends KmsRepository
{
// Here are some variables repeated in this repository
// These change for every type of entity
// Namespace
protected $namespace = 'Projects';
// Tables
protected $table = 'projects';
protected $tableTranslation = 'project_translations';
protected $translationId = 'project_translation_id';
protected $foreignKey = 'project_id';
// Entities
protected $entity = 'ProjectEntity';
protected $entityTranslation = 'ProjectTranslationEntity';
// Model-string
protected $modelString = 'Project';
protected $modelTranslationString = 'ProjectTranslation';
/**
* @var Tree
*/
protected $tree;
/**
* @var ImageRepository
*/
private $imageRepository;
/**
* @var
*/
private $model;
/**
* @var
*/
private $modelTranslation;
private $imageService;
private $languages;
/**
* ProjectRepository constructor.
* @param Kms $kms
* @param Tree $tree
* @param ImageService $imageService
*/
function __construct(
Kms $kms,
Tree $tree,
ImageService $imageService,
Language $language
) {
parent::__construct($kms);
$this->tree = $tree;
$this->imageService = $imageService;
$this->languages = $language;
}
public function newEntity()
{
$entity = new ProjectEntity();
foreach ($this->kms->getCurrentLanguages() as $language) {
$entity->addTranslationEntity(new ProjectTranslationEntity($language->id));
}
return $entity;
}
public function getEntity($id)
{
if ($id == null) {
return $this->newEntity();
}
$model = Project::find($id);
if ( ! $model) {
return false;
}
$entity = new ProjectEntity($model->attributesToArray());
$entity->parent_id = $model->getParentId();
$entity->name = $model->name;
// Get the images for an model (default is attribute_key 'images')
$entity->images = $this->getImages($id, get_class($model));
if ($entity->images) {
$entity->thumbnail = $entity->images[0]['thumb_image_url'];
}
foreach ($this->kms->getCurrentLanguages() as $language) {
$languageTrans = ProjectTranslation::where('language_id', '=', $language->id)
->where('project_id', '=', $id)
->first();
$languageTrans->images_lang = $this->getImages($id, get_class($model), 'images_lang_'.$language->id);
if ( ! $languageTrans) {
$entity->addTranslationEntity(
new ProjectTranslationEntity($language->id)
);
continue;
};
$entityTranslationString = 'Komma\Kms\\' . $this->namespace . '\\' . $this->entityTranslation;
$route = $this->getRouteByTranslationId($languageTrans->id);
// check if route starts with language string, if true remove it
$languageString = $this->languages->find($languageTrans->language_id)->iso_2;
if(substr($route->route, 0, 3) == $languageString.'/'){
$languageTrans->route = substr($route->route, 3);
}
else{
$languageTrans->route = $route->route;
}
$entity->addTranslationEntity(
new $entityTranslationString(
$languageTrans->language_id,
$languageTrans->attributesToArray()
)
);
}
return $entity;
}
public function getEntities()
{
$tree = $this->getEntitiesAsTree();
$this->makeRootIfNotExists();
return $tree;
}
public function getEntitiesAsTree($langId = null)
{
if ($langId == null) {
$langId = $this->kms->getDefaultLanguageId();
}
$records = \DB::table($this->table)
->select(
$this->table . '.lft',
$this->table . '.rgt',
$this->table . '.id as id',
$this->tableTranslation . '.id as ' . $this->translationId,
$this->tableTranslation . '.name',
'images.thumb_image_url as thumbnail'
)
->leftJoin(
$this->tableTranslation,
$this->table . '.id', '=', $this->tableTranslation . '.' . $this->foreignKey
)
->leftJoin('images', function ($join) {
$join->on($this->table . '.id', '=', 'images.imageble_id')
->where('images.imageble_type', '=',
'Komma\Kms\\' . $this->namespace . '\Models\\' . $this->modelString)
->where('images.sort_order', '=', '1');
})
->where(function ($query) use ($langId) {
$query->whereNull($this->tableTranslation . '.language_id')
->orWhere($this->tableTranslation . '.language_id', '=', $langId);
})
->orderBy($this->table . '.lft', 'asc')
->get();
$categories = [];
foreach ($records as $record) {
$categories[] = new KmsHierarchicalListItemEntity((array)$record);
}
$tree = new Tree();
$tree->make($categories);
return $tree;
}
/**
* @param $entity
* @return mixed
*/
public function saveEntity($entity)
{
$project = Project::firstOrNew(['id' => $entity->id]);
if ($project->id == null) {
$project->makeLastChildOf(Project::where('lft', '=', 1)->first());
}
//Set the fields
$project->active = $entity->active;
$project->code_name = \Str::slug($entity->code_name);
$project->save();
foreach ($entity->getTranslations() as $translation) {
if ( ! isset($translation->name) || $translation->name == '') {
continue;
}
$projectTranslation = ProjectTranslation::firstOrNew(['project_id' => $project->id,
'language_id' => $translation->getLanguageId()
]);
$projectTranslation->name = $translation->name;
$projectTranslation->name_short = $translation->name_short;
$projectTranslation->projectDescription = $translation->projectDescription;
$dps = \App::make('Komma\Kms\Core\Services\DynamicPageService');
$projectTranslation->projectDescription = $dps->prepareDynamicDescription(json_decode($projectTranslation->projectDescription), $project, true);
$projectTranslation->save();
$this->imageService->linkImagesToModel('images_lang_'.$translation->getLanguageId(), $project);
$route = $this->getBaseUrl($translation->getLanguageId()) . '/' . \Str::slug($projectTranslation->name);
$this->saveRoute($projectTranslation, $route, 'projects/' . $project->id);
}
//Link the images to this model
$this->imageService->linkImagesToModel('images', $project);
//$this->imageService->linkImagesToModel('images', $record);
return $project;
}
public function getForSelect($languageId = null, $excludeId = null)
{
return [];
$tree = $this->getEntitiesAsTree($languageId);
if ($excludeId) {
$tree->removeFromIndex((int)$excludeId);
}
$entities = [];
// QnD !!! Indexing trees per language (multiple queries)
$treePerLanguage = [];
foreach ($this->kms->getCurrentLanguages() as $language) {
$treePerLanguage[$language->id] = $this->getEntitiesAsTree($language->id);
}
// End QnD !!!
foreach ($tree->getIndex() as $record) {
$entity = [];
$entity['value'] = $record->node->id;
// QnD !!! Getting the path from the indexed trees
$paths = [];
foreach ($this->kms->getCurrentLanguages() as $language) {
$path = implode('/', $treePerLanguage[$language->id]->getEntityById($entity['value'])->getSlugPath());
$paths[$language->id] = $path ? $path . '/' : '';
}
$entity['fullValue'] = json_encode($paths);
// End QnD !!!
$entity['content'] = $record->node->getName();
if ( ! $entity['content']) {
$entity['content'] = '\\';
}
$indent = str_repeat(' ', $record->getDepth());
$entity['htmlContent'] = $indent . $record->node->getName();
if ( ! $entity['htmlContent']) {
$entity['htmlContent'] = '\\';
}
$entities[] = $entity;
}
return $entities;
}
public function destroyEntity($id)
{
$entity = Project::find($id);
foreach ($entity->translations()->get() as $translation) {
$translation->routes()->delete();
}
$entity->delete();
}
public function getRouteByTranslationId($productId)
{
if ($route = ProjectTranslation::find($productId)) {
return $route->routes()->first();
}
return null;
}
protected function saveRoute(RoutableInterface $routable, $routeString, $restRoute = null)
{
$routeString = $this->makeRouteUnique($routable, $routeString);
if ($route = $this->getRouteByTranslationId($routable->id)) {
$route->route = $routeString;
if ($restRoute != null) $route->rest_route = $restRoute;
$route->save();
} else {
$route = new KmsRoute([
'route' => $routeString,
]);
if ($restRoute != null) $route->rest_route = $restRoute;
$routable->routes()->save($route);
}
}
protected function makeRootIfNotExists()
{
$model = \DB::table($this->table)
->where('lft', 1)
->first();
if ( ! $model) {
$modelString = 'Komma\Kms\\' . $this->namespace . '\Models\\' . $this->modelString;
$model = new $modelString;
$model->makeRoot();
}
}
public function makeRouteUnique($routeable, $route)
{
$routes = KmsRoute::where('route', '=', $route)
->Join('project_translations', 'routes.routable_id', '=', 'project_translations.id')
->where('project_translations.language_id', '=', $routeable->language_id)
->where(function ($query) use ($routeable) {
$query->where('routable_id', '!=', $routeable->id);
$query->where('routable_type', '==', get_class($routeable));
})
->get();
if ($routes->count() == 0) return $route;
return $this->makeRouteUnique($routeable, $route . '-1');
}
public function getBaseUrl($language)
{
//Lang toevoegen
if (!$route = KmsRoute::select('routes.*')
->where('routes.rest_route', '=', 'projects')
->Join('page_translations', 'routes.routable_id', '=', 'page_translations.id')
->where('page_translations.language_id', '=', $language)
->first()
) return '';
return $route->route;
}
}