HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/anvil.komma.pro/app/KommaApp/Segments/SegmentService.php
<?php

namespace App\KommaApp\Segments;

use App\KommaApp\Pages\Models\Page;
use App\KommaApp\Segments\Models\Segment;
use Illuminate\Database\Eloquent\Collection;

class SegmentService
{
    /**
     * Get all the segments through the site model
     *
     * @return mixed
     */
    public function getAllSegments()
    {
        return \App::getSite()
            ->segments()
            ->with('translation')
            ->with('images')
            ->where('active', 1)
            ->where('lft', '!=', 1)
            ->orderBy('lft')
            ->get();
    }

    /**
     * Get segment by specific code name
     *
     * @param $codeName
     * @return Segment
     */
    public function getSegmentByCodeName($codeName): Segment
    {
        if (! $segment = Segment::where('code_name', '=', $codeName)
            ->with('translation')
            ->where('active', 1)
            ->first()) {
            \App::abort(404);
        }

        return $segment;
    }

    public function expandBreadCrumbTree(Page &$page, Segment $segment, $links)
    {
        if (! isset($links->segments)) {
            return;
        }

        $tree = $page->breadcrumbTree;

        $tree[] = (object) [
            'name' => $segment->translation->name,
            'url' => '/'.$links->segments->route.'/'.$segment->translation->slug,
            'node' => $segment,
        ];

        $page->breadcrumbTree = $tree;
    }

    /**
     * Get the next amount of segments from the collection
     *
     * @param Segment $segment
     * @param Collection $segments
     * @param int $amount
     */
    public function makeNextSegments(Segment &$segment, Collection $segments, $amount = 4)
    {
        // Clone the segments because else it weirdly get edit
        $segments = clone $segments;

        // Boolean for when to start grabbing the segments for the next segments collection
        $startGetting = false;
        $nextSegments = new Collection();

        // Loop through the segments
        foreach ($segments as $key => $otherSegment) {
            // If the looped segment is the active segment enable the boolean and remove it from the cloned segment collection
            // Then continue with the loop
            if ($segment->id == $otherSegment->id) {
                $startGetting = true;
                $segments->forget($key);
                continue;
            }

            // If the boolean is through append the looped segment to the next segments collection
            // And also remove it from the cloned segment collection (in case we need to grab the first more because there are to few segments in the collection)
            if ($startGetting) {
                $nextSegments->push($otherSegment);
                $segments->forget($key);
            }
            // If we have enough segments in the collection break from the foreach loop
            if ($nextSegments->count() == $amount) {
                break;
            }
        }

        // If we have to few segments take from the front of resting segments in the cloned segment collection
        if ($nextSegments->count() != $amount && $segments->count() != 0) {
            // Determine the amount we need to take
            $take = $amount - $nextSegments->count();

            //Merge it with the next segment collection
            $nextSegments = $nextSegments->merge($segments->take($take));
        }

        // Bind the images relation to the collection segments
        $nextSegments->load('images');

        // Bind it collection to the segment model
        $segment->nextSegments = $nextSegments;
    }
}