366 lines
12 KiB
PHP
366 lines
12 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
/**
|
|
* App\Models\SidebarWidget
|
|
*
|
|
* @property int $id
|
|
* @property string $name
|
|
* @property string|null $component
|
|
* @property string|null $html
|
|
* @property array|null $show_at
|
|
* @property int|null $pos
|
|
* @property int $active
|
|
* @property \Illuminate\Support\Carbon|null $created_at
|
|
* @property \Illuminate\Support\Carbon|null $updated_at
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\SidebarWidget whereActive($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\SidebarWidget whereComponent($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\SidebarWidget whereCreatedAt($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\SidebarWidget whereHtml($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\SidebarWidget whereId($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\SidebarWidget whereName($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\SidebarWidget wherePos($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\SidebarWidget whereShowAt($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\SidebarWidget whereUpdatedAt($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\SidebarWidget newModelQuery()
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\SidebarWidget newQuery()
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\SidebarWidget query()
|
|
* @mixin \Eloquent
|
|
*/
|
|
class SidebarWidget extends Model
|
|
{
|
|
const HOMEPAGE_PLANNABLE_TRIPS = 'homepagePlannableTrips';
|
|
const HOMEPAGE_POPULAR_TRIPS = 'homepagePopularTrips';
|
|
const NEWS_SIDEBAR_WIDGET = 'newsSidebarWidget';
|
|
|
|
|
|
protected static $shows = [
|
|
'home' => 'Startseite',
|
|
'search' => 'Suche',
|
|
'default' => 'Standartseiten',
|
|
'overview' => 'Übersicht',
|
|
'program' => 'Programme',
|
|
'booking' => 'Buchungen',
|
|
'bookingconfirm' => 'Buchungsbestätigung',
|
|
|
|
|
|
];
|
|
|
|
protected static $components = [
|
|
'aboutSternToursWidget' => 'Wir: STERN TOURS',
|
|
'searchSidebarWidget' => 'Kulturreisen suchen',
|
|
'navSidebarWidget' => 'Reiseprogramme',
|
|
'topVotingWidget' => 'TOP bewertet',
|
|
'feedbacksSidebarWidget' => 'Kundenfeedback',
|
|
'travelGuideSidebarWidget' => 'Reiseführer',
|
|
'travelMagazineSidebarWidget' => 'Reisemagazin',
|
|
'offersSidebarWidget' => 'Angebote',
|
|
self::NEWS_SIDEBAR_WIDGET => 'News',
|
|
self::HOMEPAGE_PLANNABLE_TRIPS => 'Startseite: Aktuell planbare Reisen',
|
|
self::HOMEPAGE_POPULAR_TRIPS => 'Startseite: Beliebte Kulturreisen',
|
|
|
|
];
|
|
|
|
|
|
protected $connection = 'mysql_stern';
|
|
|
|
protected $table = 'sidebar_widgets';
|
|
|
|
protected $casts = ['show_at' => 'array'];
|
|
|
|
protected $fillable = [
|
|
'name', 'component', 'html', 'show_at', 'pos', 'active'
|
|
];
|
|
|
|
|
|
public static function getComponentsOptions($setKey = false){
|
|
$options = self::$components;
|
|
$ret = '<option value="">Keine Komponente laden (nur HTML)</option>\n';
|
|
foreach ($options as $key => $option){
|
|
$attr = ($key == $setKey) ? 'selected="selected"' : '';
|
|
$ret .= '<option value="'.$key.'" '.$attr.'>'.$option.'</option>\n';
|
|
}
|
|
return $ret;
|
|
}
|
|
|
|
public static function getShowsOptions($setKey){
|
|
if(!is_array($setKey))
|
|
$setKey = [];
|
|
|
|
$options = self::$shows;
|
|
$ret = "";
|
|
foreach ($options as $key => $option){
|
|
$attr = in_array($key, $setKey) ? 'selected="selected"' : '';
|
|
$ret .= '<option value="'.$key.'" '.$attr.'>'.$option.'</option>\n';
|
|
}
|
|
return $ret;
|
|
}
|
|
|
|
public function getShowsAtString(){
|
|
$ret = "";
|
|
if($this->show_at){
|
|
foreach($this->show_at as $show_at){
|
|
if(isset(self::$shows[$show_at])){
|
|
$ret .= self::$shows[$show_at].", ";
|
|
}
|
|
}
|
|
}
|
|
return rtrim($ret, ", ");
|
|
}
|
|
|
|
public function getComponentLabel()
|
|
{
|
|
if (!$this->component) {
|
|
return 'Nur HTML';
|
|
}
|
|
|
|
return isset(self::$components[$this->component]) ? self::$components[$this->component] : $this->component;
|
|
}
|
|
|
|
public function isHomepageTripsComponent()
|
|
{
|
|
return in_array($this->component, [self::HOMEPAGE_PLANNABLE_TRIPS, self::HOMEPAGE_POPULAR_TRIPS]);
|
|
}
|
|
|
|
public function isNewsComponent()
|
|
{
|
|
return $this->component === self::NEWS_SIDEBAR_WIDGET;
|
|
}
|
|
|
|
public function isStructuredConfigComponent()
|
|
{
|
|
return $this->isHomepageTripsComponent() || $this->isNewsComponent();
|
|
}
|
|
|
|
public function getConfig()
|
|
{
|
|
$config = json_decode($this->html, true);
|
|
|
|
if (!is_array($config)) {
|
|
$config = [];
|
|
}
|
|
|
|
$config['page_ids'] = isset($config['page_ids']) && is_array($config['page_ids'])
|
|
? array_values(array_filter($config['page_ids']))
|
|
: [];
|
|
$config['new_page_ids'] = isset($config['new_page_ids']) && is_array($config['new_page_ids'])
|
|
? array_values(array_filter($config['new_page_ids']))
|
|
: [];
|
|
$config['new_badge_active'] = !empty($config['new_badge_active']);
|
|
$config['news_limit'] = isset($config['news_limit']) ? max(1, min(12, (int) $config['news_limit'])) : 3;
|
|
|
|
return $config;
|
|
}
|
|
|
|
public function getHomepageConfig()
|
|
{
|
|
return $this->getConfig();
|
|
}
|
|
|
|
public function getHomepagePageIds()
|
|
{
|
|
return $this->getHomepageConfig()['page_ids'];
|
|
}
|
|
|
|
public function getHomepageNewPageIds()
|
|
{
|
|
return array_map('intval', $this->getHomepageConfig()['new_page_ids']);
|
|
}
|
|
|
|
public function getHomepageNewBadgeActive()
|
|
{
|
|
return $this->getHomepageConfig()['new_badge_active'];
|
|
}
|
|
|
|
public function getHomepageNewsLimit()
|
|
{
|
|
return $this->getConfig()['news_limit'];
|
|
}
|
|
|
|
public static function getHomepageTravelPageOptions($selectedIds = [])
|
|
{
|
|
$selectedIds = array_map('intval', (array) $selectedIds);
|
|
$items = self::getHomepageTravelPageItems($selectedIds);
|
|
$groupedItems = [];
|
|
|
|
foreach ($items as $item) {
|
|
$groupedItems[$item['country_group']][] = $item;
|
|
}
|
|
|
|
uksort($groupedItems, function ($a, $b) {
|
|
return strnatcasecmp(self::getHomepageSortValue($a), self::getHomepageSortValue($b));
|
|
});
|
|
|
|
$ret = '';
|
|
foreach ($groupedItems as $countryGroup => $groupItems) {
|
|
$ret .= '<optgroup label="'.e($countryGroup).'">'."\n";
|
|
|
|
foreach ($groupItems as $item) {
|
|
$attr = $item['selected'] ? 'selected="selected"' : '';
|
|
$label = e($item['label']);
|
|
$ret .= '<option value="'.$item['id'].'" data-label="'.$label.'" '.$attr.'>'.$label.'</option>'."\n";
|
|
}
|
|
|
|
$ret .= '</optgroup>'."\n";
|
|
}
|
|
|
|
return $ret;
|
|
}
|
|
|
|
public static function getHomepageTravelPageItems($selectedIds = [])
|
|
{
|
|
$selectedIds = array_map('intval', (array) $selectedIds);
|
|
$pages = Page::with([
|
|
'travel_program_content.travel_program_countries.travel_country',
|
|
'travel_program_content.travel_country_content',
|
|
])
|
|
->where('status', 1)
|
|
->whereNotNull('travel_program')
|
|
->where('travel_program', '>', 0)
|
|
->whereHas('travel_program_content', function ($query) {
|
|
$query->where('status', 1);
|
|
})
|
|
->orderBy('title')
|
|
->get()
|
|
->unique('travel_program');
|
|
|
|
$items = [];
|
|
foreach ($pages as $page) {
|
|
$countryGroup = self::getHomepageTravelPageCountryLabel($page);
|
|
$items[] = [
|
|
'id' => (int) $page->id,
|
|
'label' => self::getHomepageTravelPageLabel($page),
|
|
'country_group' => $countryGroup,
|
|
'sort_title' => $page->title,
|
|
'selected' => in_array((int) $page->id, $selectedIds),
|
|
];
|
|
}
|
|
|
|
usort($items, function ($a, $b) {
|
|
$countryCompare = strnatcasecmp(
|
|
self::getHomepageSortValue($a['country_group']),
|
|
self::getHomepageSortValue($b['country_group'])
|
|
);
|
|
if ($countryCompare !== 0) {
|
|
return $countryCompare;
|
|
}
|
|
|
|
return strnatcasecmp(self::getHomepageSortValue($a['sort_title']), self::getHomepageSortValue($b['sort_title']));
|
|
});
|
|
|
|
return $items;
|
|
}
|
|
|
|
public function getHomepageSelectedTravelPageItems()
|
|
{
|
|
$selectedIds = array_map('intval', $this->getHomepagePageIds());
|
|
|
|
if (empty($selectedIds)) {
|
|
return [];
|
|
}
|
|
|
|
$newPageIds = $this->getHomepageNewPageIds();
|
|
$pages = Page::with([
|
|
'travel_program_content.travel_program_countries.travel_country',
|
|
'travel_program_content.travel_country_content',
|
|
])
|
|
->whereIn('id', $selectedIds)
|
|
->where('status', 1)
|
|
->whereHas('travel_program_content', function ($query) {
|
|
$query->where('status', 1);
|
|
})
|
|
->get()
|
|
->keyBy('id');
|
|
|
|
$items = [];
|
|
$usedTravelPrograms = [];
|
|
foreach ($selectedIds as $pageId) {
|
|
if (!$pages->has($pageId)) {
|
|
continue;
|
|
}
|
|
|
|
$page = $pages->get($pageId);
|
|
$travelProgramId = (int) $page->travel_program;
|
|
if (in_array($travelProgramId, $usedTravelPrograms)) {
|
|
continue;
|
|
}
|
|
$usedTravelPrograms[] = $travelProgramId;
|
|
|
|
$items[] = [
|
|
'id' => (int) $page->id,
|
|
'label' => self::getHomepageTravelPageLabel($page),
|
|
'is_new' => in_array((int) $page->id, $newPageIds),
|
|
];
|
|
}
|
|
|
|
return $items;
|
|
}
|
|
|
|
protected static function getHomepageTravelPageLabel(Page $page)
|
|
{
|
|
$country = self::getHomepageTravelPageCountryLabel($page);
|
|
$travelProgram = $page->travel_program_content;
|
|
$programId = $travelProgram ? $travelProgram->id : $page->travel_program;
|
|
|
|
return $country.' | Reise, '.$page->title.' (#'.$programId.')';
|
|
}
|
|
|
|
protected static function getHomepageTravelPageCountryLabel(Page $page)
|
|
{
|
|
$countryNames = self::getHomepageTravelPageCountryNames($page);
|
|
|
|
return !empty($countryNames) ? implode(', ', $countryNames) : 'Ohne Reiseland';
|
|
}
|
|
|
|
protected static function getHomepageTravelPageCountryNames(Page $page)
|
|
{
|
|
$travelProgram = $page->travel_program_content;
|
|
$countryNames = [];
|
|
|
|
if ($travelProgram && $travelProgram->travel_program_countries) {
|
|
foreach ($travelProgram->travel_program_countries as $programCountry) {
|
|
if ($programCountry->travel_country && $programCountry->travel_country->name) {
|
|
$countryNames[] = $programCountry->travel_country->name;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (empty($countryNames) && $travelProgram && $travelProgram->travel_country_content) {
|
|
$countryNames[] = $travelProgram->travel_country_content->name;
|
|
}
|
|
|
|
$countryNames = array_unique($countryNames);
|
|
usort($countryNames, function ($a, $b) {
|
|
return strnatcasecmp(self::getHomepageSortValue($a), self::getHomepageSortValue($b));
|
|
});
|
|
|
|
return $countryNames;
|
|
}
|
|
|
|
protected static function getHomepageSortValue($value)
|
|
{
|
|
return str_replace(
|
|
['Ä', 'Ö', 'Ü', 'ä', 'ö', 'ü', 'ß'],
|
|
['Ae', 'Oe', 'Ue', 'ae', 'oe', 'ue', 'ss'],
|
|
(string) $value
|
|
);
|
|
}
|
|
|
|
/*
|
|
* public function getVotesDetailAttribute($details)
|
|
{
|
|
* return json_decode($details, true);
|
|
}
|
|
* then when you will call $store->votes_detail you will get the expected result.
|
|
*
|
|
* After that you can use mutators to convert an array back to JSON when it is saved back in the DB. Define the method setVotesDetailAttribute($value) as follows:
|
|
*
|
|
* public function setVotesDetailsAttribute($value)
|
|
* {
|
|
* $this->attributes['votes_detail'] = json_encode($value);
|
|
* }
|
|
*/
|
|
}
|