File: D:/HostingSpaces/Lacom/lacom.nl/app/KommaApp/Specialisms/SpecialismService.php
<?php
namespace App\KommaApp\Specialisms;
use App\KommaApp\Pages\Models\Page;
use App\KommaApp\Specialisms\Models\Specialism;
use Illuminate\Database\Eloquent\Collection;
class SpecialismService
{
/**
* Get all the specialisms through the site model
*
* @return mixed
*/
public function getAllSpecialisms(){
return \App::getSite()
->specialisms()
->with('translation')
->with('images')
->where('active', 1)
->where('lft', '!=', 1)
->orderBy('lft')
->get();
}
/**
* Get specialism by specific code name
*
* @param $codeName
* @return Specialism
*/
public function getSpecialismByCodeName($codeName): Specialism
{
if(!$specialism = Specialism::where('code_name','=', $codeName)
->with('translation')
->where('active', 1)
->first()) \App::abort(404);
return $specialism;
}
public function expandBreadCrumbTree(Page &$page, Specialism $specialism, $links){
if(!isset($links->specialisms)) return;
$tree = $page->breadcrumbTree;
$tree[] = (object)[
'name' => $specialism->translation->name,
'url' => '/' . $links->specialisms->route . '/'.$specialism->translation->slug,
'node' => $specialism,
];
$page->breadcrumbTree = $tree;
}
/**
* Get the next amount of specialisms from the collection
*
* @param Specialism $specialism
* @param Collection $specialisms
* @param int $amount
*/
public function makeNextSpecialisms(Specialism &$specialism, Collection $specialisms, $amount = 4){
// Clone the specialisms because else it weirdly get edit
$specialisms = clone $specialisms;
// Boolean for when to start grabbing the specialisms for the next specialisms collection
$startGetting = false;
$nextSpecialisms = new Collection();
// Loop through the specialisms
foreach ($specialisms as $key => $otherSpecialism){
// If the looped specialism is the active specialism enable the boolean and remove it from the cloned specialism collection
// Then continue with the loop
if($specialism->id == $otherSpecialism->id){
$startGetting = true;
$specialisms->forget($key);
continue;
}
// If the boolean is through append the looped specialism to the next specialisms collection
// And also remove it from the cloned specialism collection (in case we need to grab the first more because there are to few specialisms in the collection)
if($startGetting){
$nextSpecialisms->push($otherSpecialism);
$specialisms->forget($key);
}
// If we have enough specialisms in the collection break from the foreach loop
if($nextSpecialisms->count() == $amount) break;
}
// If we have to few specialisms take from the front of resting specialisms in the cloned specialism collection
if($nextSpecialisms->count() != $amount && $specialisms->count() != 0){
// Determine the amount we need to take
$take = $amount - $nextSpecialisms->count();
//Merge it with the next specialism collection
$nextSpecialisms = $nextSpecialisms->merge($specialisms->take($take));
}
// Bind it collection to the specialism model
$specialism->nextSpecialisms = $nextSpecialisms;
}
}