File: D:/HostingSpaces/SBogers10/rentman.komma.pro/app/Komma/Updates/UpdateService.php
<?php
namespace Komma\Updates;
use Carbon\Carbon;
use Komma\LanguageService;
use Komma\Updates\Models\Update;
use Komma\Updates\Models\UpdateTranslation;
class UpdateService
{
public function getUpdate($id, $lang = 40)
{
$update = UpdateTranslation::where('update_id', '=', $id)
->where('language_id', '=', $lang)
->where('date', '<=', Carbon::now())
->with('rentmanUpdate', 'rentmanUpdate.images', 'rentmanUpdate.socialMediaImages')
->first();
$update->date = Carbon::createFromFormat('Y-m-d H:i:s', $update->date);
$update->description = json_decode($update->description);
// Check if there are social media images are defined
if(isset($update->rentmanUpdate->socialMediaImages) && $update->rentmanUpdate->socialMediaImages->count() != 0){
$languageArray = [];
$languageService = new LanguageService();
foreach ($update->rentmanUpdate->socialMediaImages as $socialMediaImage){
$explodedAttributeKey = explode('_', $socialMediaImage->attribute_key);
$langIso = end($explodedAttributeKey);
$language = $languageService->getIsoById($langIso);
$languageArray[$language->iso_2] = $socialMediaImage;
}
$update->socialMediaImageByLanguage = (object)$languageArray;
}
$this->getNextAndPrevious($update);
return $update;
}
public function getLatestUpdates($amount = 10, $lang = 40)
{
$updates = UpdateTranslation::where('language_id', '=', $lang)
->orderBy('date', 'DESC')
->where('active', 1)
->where('date', '<=', Carbon::now())
->with('rentmanUpdate', 'rentmanUpdate.images')
->take($amount)
->get();
$this->makeCarbonDate($updates);
return $updates;
}
public function getNextAndPrevious(&$update)
{
$update->previous = UpdateTranslation::where('language_id', '=', $update->language_id)
->orderBy('date', 'ASC')
->where('active', 1)
->where('date', '<=', Carbon::now())
->where('date', '>=', $update->date)
->where('id', '!=', $update->id)
->with('rentmanUpdate', 'rentmanUpdate.images')
->first();
$update->next = UpdateTranslation::where('language_id', '=', $update->language_id)
->orderBy('date', 'DESC')
->where('active', 1)
->where('date', '<=', Carbon::now())
->where('date', '<=', $update->date)
->where('id', '!=', $update->id)
->with('rentmanUpdate', 'rentmanUpdate.images')
->first();
}
/**
* Get all translations of an page
* based upon this page id
*
* @param $page_id
* @return mixed
*/
public function getOtherLanguageRoutes($update_id)
{
return Update::where('id', '=', $update_id)
->with('allTranslations')
->with('allTranslations.route')
->first();
}
public function makeCarbonDate(&$updates)
{
foreach ($updates as $update) {
$date = $update->date;
$update->date = Carbon::createFromFormat('Y-m-d H:i:s', $date);
}
}
public function getMoreUpdates($start, $amount, $lang = 40)
{
return UpdateTranslation::where('language_id', '=', $lang)
->orderBy('date', 'DESC')
->where('active', 1)
->where('date', '<=', Carbon::now())
->with('rentmanUpdate', 'rentmanUpdate.images')
->skip($start)
->take($amount)
->get();
}
public function getUpdatesCount($lang = 40)
{
return UpdateTranslation::where('language_id', '=', $lang)
->orderBy('date', 'DESC')
->where('active', 1)
->where('date', '<=', Carbon::now())
->count();
}
}