File: D:/HostingSpaces/SBogers10/zuiderbos.komma.pro/app/Komma/Pages/models/Page.php
<?php
namespace Komma\Pages\Models;
use Carbon\Carbon;
use Illuminate\Support\Collection;
/**
* Short description for the file.
*
* @author Komma <support@komma.pro>
* @copyright (c) 2012-2015, Komma Mediadesign
*/
class Page extends \Komma\Kms\Pages\Models\Page
{
public $kmsClass = 'Komma\\Kms\\Pages\\Models\\Page';
/**
* Get blocks with are linked by the kms blocks in pages
*
* @return mixed
*/
public function blocks()
{
return $this->belongsToMany('Komma\Blocks\Models\Block', 'page_blocks')->with(['translation', 'images']);
}
/**
* Get the translation for the current language
*
* @return Collection
*/
public function translation()
{
/**
* On the translation model is an BelongsTo relation.
* We want to collect the current translation.
* Therefore we create a hasOne relation
* Where we will select the page_translation,
* Join on the languages table
* And set the current Locale as the languages.iso_2
*/
return $this->hasOne('Komma\Pages\Models\PageTranslation')
//We only need the translation
->select('page_translations.*')
//Join the languages
->join('languages', 'languages.id', '=', 'page_translations.language_id')
//Get only the language with the correct lang
->where('languages.iso_2', '=', \App::getLocale());
}
/**
* Get all translations
*
* @return Collection
*/
public function allTranslations()
{
return $this->hasMany('Komma\Pages\Models\PageTranslation')
//Join the languages
->join('languages', 'languages.id', '=', 'page_translations.language_id')
->whereIn('languages.iso_2', \Config::get('app.availableLanguages'))
//We only need the translation and language iso
->select('page_translations.*', 'languages.iso_2')
->orderBy('languages.id');
}
/**
* Get the images from the current page
*
* @return Collection
*/
public function images()
{
/**
* On the Image model is an MorphTo relation
* By using a hasMany relation:
* where the imageble_type is filled in with the KmsClass
* And the imageble_id is set as the foreign_id,
* we can collect the images of the given model directly.
*/
return $this->hasMany('Komma\Kms\Images\Models\Image', 'imageble_id')
->where('imageble_type', '=', $this->kmsClass)
->orderBy('sort_order');
}
/**
* @param int $amount
* @param bool $pagination
* @return array
*/
public function getNews($amount = null, $pagination = false)
{
$newsItems = [];
$newsletters = $this->school->newsletters()
->orderBy('newsletters.date', 'desc')
->where('date', '<=', \Carbon\Carbon::today())
->leftJoin('images', function ($join) {
$join->on('images.imageble_id', '=', 'newsletters.id')
->where('images.imageble_type', '=', 'Komma\Kms\Newsletters\Models\Newsletter')
->where('sort_order', '=', 1);
})
->get();
$news = $this->school->news()
->with('translations')
->with('translations.routes')
->orderBy('posts.date', 'desc')
->where('date', '<=', \Carbon\Carbon::today())
->get();
if ($amount) {
$newsletters = $newsletters->take($amount);
$news = $news->take($amount);
}
foreach ($newsletters as $newsletter) {
$newsletter->date = Carbon::createFromFormat(Carbon::DEFAULT_TO_STRING_FORMAT, $newsletter->date);
$timestamp = $newsletter->date->timestamp;
do {
$timestamp++;
} while (isset($newsItems[$timestamp]));
$newsItems[$timestamp] = $newsletter;
}
foreach ($news as $post) {
$post->date = Carbon::createFromFormat(Carbon::DEFAULT_TO_STRING_FORMAT, $post->date);
$timestamp = $post->date->timestamp;
do {
$timestamp++;
} while (isset($newsItems[$timestamp]));
$newsItems[$timestamp] = $post;
}
krsort($newsItems);
if ($amount) {
$newsItems = array_slice($newsItems, 0, $amount, true);
}
return $newsItems;
}
}