File: D:/HostingSpaces/SBogers10/rentman2019.komma.pro/app/Komma/CustomerStories/CustomerStoryService.php
<?php
namespace App\Komma\CustomerStories;
use App\Komma\Base\Service;
use App\Komma\CustomerStories\Models\CustomerStory;
class CustomerStoryService extends Service
{
/**
* Generate a base query for getting customer stories
*
* @param bool $withRelation
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
private function baseCustomerStoriesQuery($withRelation = true)
{
$query = $this->site
->customerStories()
->where('lft', '!=', 1)
->orderBy('lft')
->where('name', '!=', '');
if ($withRelation) {
return $query->with('translation', 'images');
} else {
return $query;
}
}
/**
* We need to use a join to select the active customer story
* because they can disable them for language specific
*
* @param $query
* @return mixed
*/
private function filterForActiveTranslation(&$query)
{
return $query->join('customer_story_translations', 'customer_stories.id', '=', 'customer_story_translations.customer_story_id')
->select('customer_stories.*', 'customer_story_translations.active', 'customer_story_translations.language_id')
->where('active', 1)
->where('language_id', \App::getLanguage()->id);
}
/**
* Get all the active customer stories for this language
*
* @param bool $pagination
* @param int $itemsPerPage
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Relations\HasMany
*/
public function getAllCustomerStories($pagination = false, $itemsPerPage = 6)
{
$customerStories = $this->baseCustomerStoriesQuery();
$this->filterForActiveTranslation($customerStories);
// Use pagination, or else get them
if ($pagination) {
$customerStories = $customerStories->paginate($itemsPerPage);
} else {
$customerStories = $customerStories->get();
}
return $customerStories;
}
public function getMoreCustomerStories(CustomerStory $customerStory, $amountNeeded = 3)
{
$customerStories = $this->baseCustomerStoriesQuery();
$this->filterForActiveTranslation($customerStories);
$lowerCustomerStoriesQuery = clone $customerStories;
$lowerCustomerStories = $lowerCustomerStoriesQuery->where('lft', '>', $customerStory->lft)
->take($amountNeeded)
->get();
$foundStories = $lowerCustomerStories->count();
// If we have enough customer stories return them
if ($foundStories == $amountNeeded) {
return $lowerCustomerStories;
}
// Get the rest of the customer stories from the top till we have enough (like a carousel)
$higherCustomerStories = $customerStories->where('lft', '<', $customerStory->lft)
->take(($amountNeeded - $foundStories))
->get();
return $lowerCustomerStories->merge($higherCustomerStories);
}
/**
* Get the customer stories by the given ids
* Used for grabbing the component bind stories
*
* @param $ids
* @return CustomerStory[]|\Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
*/
public function getCustomerStoriesByIds($ids)
{
$customerStories = CustomerStory::whereIn('customer_stories.id', $ids)
->with('images', 'translation');
$this->filterForActiveTranslation($customerStories);
return $customerStories->get();
}
/**
* Expend the language menu with the found translation of this resource
*
* @param $languageMenu
* @param $resource
* @param $home
*/
public function extendLanguageMenuWithResource(&$languageMenu, $resource, $home)
{
if (count($languageMenu->withFallback) == 0) {
return;
}
$resourceTranslations = $resource->translations->keyBy('language_id');
$homeTranslations = $home->node->translations->keyBy('language_id');
foreach ($this->site->languages as $language) {
if (isset($languageMenu->withFallback[$language->iso_2]) && $resourceTranslation = $resourceTranslations->get($language->id)) {
// Append the found resource translation to given language menu item if active
if ($resourceTranslation->active) {
$languageMenu[$language->iso_2] .= '/'.$resource->slug;
}
continue;
}
if (! isset($languageMenu[$language->iso_2])) {
if ($homeTranslation = $homeTranslations->get($language->id)) {
if ($homeTranslation->route->alias != '/') {
$languageMenu[$language->iso_2] = '/'.$homeTranslation->route->alias;
} else {
$languageMenu[$language->iso_2] = $homeTranslation->route->alias;
}
}
continue;
}
}
}
}