File: D:/HostingSpaces/fire-tech/fire-tech.nl/app/KommaApp/Course/CourseService.php
<?php
namespace App\KommaApp\Courses;
use App\KommaApp\Courses\Models\Course;
use Carbon\Carbon;
use Illuminate\Pagination\LengthAwarePaginator;
class CourseService
{
public function getAllCourses()
{
$courses = Course::with('translation')
->where('active', 1)
->get()
->sortBy('translation.name');
$pagination = new LengthAwarePaginator(
$courses->forPage(1, 8),
$courses->count(),
8,
1
);
return $pagination;
}
/**
* Fetch translated course routes
*
* @return object | bool
*/
public function getAllTranslatedCourseRoutes()
{
if (!\App::getSite()) {
return false;
}
// Find all courses
if (!$courses = \App::getSite()
->courses()
->where('active', 1)
->where('lft', '!=', 1)
->orderBy('lft', 'asc')
->with('translation')
->with('translation.route')
->get()
) {
return false;
}
$routes = [];
// Loop through courses
foreach ($courses as $key => $course) {
if (isset($course->translation) && isset($course->translation->route)) {
$routes[$course->code_name] = (object)[
'name' => $course->translation->name,
'route' => $course->translation->route->alias,
'node' => $course,
];
}
}
return (object)$routes;
}
public function getDatesForCourse($courseId): array
{
if (!$course = $this->getCourseById($courseId)) {
\App::abort(404);
}
$dates = [];
Carbon::setLocale('id');
for ($i = 1; $i <= 20; $i++) {
if ($course['date_' . $i . '_active']) {
$courseStartData = Carbon::createFromFormat('Y-m-d H:i:s', $course['date_' . $i]);
if($courseStartData < Carbon::today()) continue;
// dd($courseStartData, Carbon::today(), ($courseStartData < Carbon::today()));
$dates[] = [
'dateA' => $courseStartData->format('d-m-Y H:i'),
'dateB' => $course['date_' . $i . 'a_active'] == 1 ? Carbon::createFromFormat('Y-m-d H:i:s', $course['date_' . $i])->format('d-m-Y H:i') : null,
'full' => $course['date_' . $i . '_full'],
'remark' => $course['remarks_' . $i],
'remarkB' => $course['remarks_' . $i.'a'],
'dayOrNight' => $course['date_' . $i . '_dayOrNight'],
];
}
}
usort($dates, function ($a, $b) {
$aC = Carbon::parse($a['dateA']);
$bC = Carbon::parse($b['dateA']);
return $aC->greaterThanOrEqualTo($bC);
});
return $dates;
}
public function getCourseById($courseId): Course
{
return Course::with('translation')
->where('id', $courseId)
->where('active', 1)
->first();
}
/**
* Get course by specific code name
*
* @param $codeName
* @return Course
*/
public function getCourseByCodeName($codeName): Course
{
if (!$course = \App::getSite()
->courses()
->where('code_name', '=', $codeName)
->with('translation')
->where('active', 1)
->first()) {
\App::abort(404);
}
return $course;
}
/**
* Generate breadcrumb tree of the course
*
* @param Course $course
* @param $links
*/
public function generateBreadcrumbTree(Course &$course, $links)
{
// Generate the right language root path
$urlString = '/';
if (\App::getSite()->default_language_id !== \App::getLanguage()->id) {
$urlString .= \App::getLanguage()->iso_2;
}
// Make the root breadcrumb
$tree = [
(object)[
'name' => \Config::get('site.' . \App::getSite()->slug . '.smallName'),
'url' => $urlString,
'node' => $links->home,
],
];
// If we are on the home course the tree is already done so we can return
if ($course->code_name === 'home') {
$course->breadcrumbTree = $tree;
return;
}
// Else make the tree through the course models parents
$treeArray = $this->generateTreeArray($course);
//Append the tree in reversed order to the breadcrumb
$treeArray = array_reverse($treeArray);
foreach ($treeArray as $leaf) {
// This should generally work, but for fallback we also check it
if (isset($links->{$leaf->code_name})) {
$tree[] = (object)[
'name' => $links->{$leaf->code_name}->name,
'url' => '/' . $links->{$leaf->code_name}->route,
'node' => $links->{$leaf->code_name},
];
} // Just in case it shouldn't work, load the relation
else {
$leaf = $leaf->load(['translation', 'translation.route']);
$tree[] = (object)[
'name' => $leaf->translation->name,
'url' => $leaf->translation->route->alias,
'node' => $leaf,
];
}
}
// Bind the generated tree to the course
$course->breadcrumbTree = $tree;
}
/**
* Generate tree array by getting the parent till it is the root Course
*
* @param Course $course
* @param array $tree
* @param int $emergencyBreak
* @return array
*/
private function generateTreeArray(Course $course, $tree = [], $emergencyBreak = 0)
{
if ($emergencyBreak >= 10) {
throw new \RuntimeException('Tree building failed because of emergency break because the index is too deep');
} else {
$emergencyBreak++;
}
if ($course->lft === 1) {
return $tree;
} else {
$tree[] = $course;
return $this->generateTreeArray($course->getParent(), $tree, $emergencyBreak);
}
}
}