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/BVerhoeven/verhoevendak.nl/app/Komma/Projects/ProjectService.php
<?php

/**
 * Short description for the file.
 *
 * @author      Komma <support@komma.pro>
 * @copyright   (c) 2012-2015, Komma Mediadesign
 */

namespace Komma\Projects;

use Komma\Images\ImageService;
use Komma\Pages\PageService;
use Komma\Projects\Models\Project;
use Komma\Projects\Models\ProjectTranslation;
use Illuminate\Support\Collection;


class ProjectService
{
    protected $imageService;
    protected $pageService;

    public function __construct(ImageService $imageService, PageService $pageService)
    {
        $this->imageService = $imageService;
        $this->pageService = $pageService;
    }

    /**
     * Get Project by id
     *
     * @param $id
     * @return mixed
     */
    public function getProject($id)
    {
        if (!$project = Project::where('id', '=', $id)
            ->with('translation')
            ->with('translation.route')
            ->with('images')
            ->with('project_images')
            ->with('project_category')
            ->where('active', '=', 1)
            ->first()) return \App::abort(404, 'project not found');


        $this->fillProject($project);

        return $project;
    }

    /**
     * Fill additional content to project
     *
     * @param Project $project
     */
    protected function fillProject(Project &$project)
    {
        //debug($project);
        //Comment the decode when there aren't dynamic blocks
        $project->translation->description = json_decode($project->translation->description);


        $project->meta_description = strip_tags($project->meta_description);

        $metaParent = $this->pageService->getMetaData('projects');
        debug($metaParent);
        $project->translation->meta_title = $project->translation->name . ' | ' . $metaParent->meta_title;
    }


    /**
     * Get all projects
     *
     * @param bool $pagination
     * @param int $itemsPerPage
     * @return mixed
     */
    public function getAllProjects($pagination = false, $itemsPerPage = 9)
    {

        $projects = Project::where('lft', '!=', 1)
            ->with('translation')
            ->with('translation.route')
            ->with('images')
            ->with('project_images')
            ->where('active', '=', 1)
            ->orderBy('lft');

        if ($pagination) {
            $projects = $projects->paginate($itemsPerPage);
        } else {
            $projects = $projects->get();
            foreach ($projects as $key => $project) {
                if (!isset($project->translation)) $projects->forget($key);
            }
        }

        return $projects;

    }


    /**
     * This method gets the projects where
     *
     * @param $field | string, field for the where
     * @param $values | array, value for the where
     * @return mixed
     */
    public function getProjectsWhere($field, $values)
    {
        //Only get the active blocks
        $projects = Project::where('active', '=', 1)
            //Where field is value
            ->where('lft', '!=', 1)
            ->where($field, '=', $values)
            //Also load the translation
            ->with('translation')
            ->with('images')
            ->orderBy('lft')
            ->get();

        foreach ($projects as $key => $project) {
            if (!isset($project->translation)) $projects->forget($key);
        }

        return $projects;
    }

    public function getProjectsByCategory($category)
    {
        //get projects in categories
        $projects = Project::join('project_categories', 'projects.id', '=', 'project_categories.project_id')
            ->where('projects.active', '=', 1)
            ->where('project_categories.category_id', '=', $category)
            ->orderBy('lft')
            ->select('projects.id')
            ->paginate(9);

        return $projects;
    }

    public function getProjectsNearLocation($projectId, $location)
    {
        $project_location = $this->getLocationString($location);
        $lat = $project_location->latitude;
        $long = $project_location->longitude;

        $projects = Project::leftJoin('project_translations', 'projects.id', '=', 'project_translations.project_id')
            ->leftJoin('4pp', 'project_translations.location', '=', '4pp.id')
            ->where('project_translations.project_id', '!=', $projectId)
            ->where('project_translations.location', '!=', "")
            ->groupBy('projects.id')
            ->select('projects.id', '4pp.latitude', '4pp.longitude', '4pp.woonplaats')
            ->get();

        foreach ($projects as $item) {
            $item->{"distance"} = $this->distance($lat, $long, $item->latitude, $item->longitude);
        }
        $topProjects = $projects->sortBy('distance')->splice(0, 3);


        $topProjects->transform(function ($item) {
            return [$this->getProject($item->id), $item->woonplaats];
        });

        return $topProjects;

    }

    private function distance($lat1, $lon1, $lat2, $lon2)
    {

        $theta = $lon1 - $lon2;
        $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
        $dist = acos($dist);
        $dist = rad2deg($dist);
        $miles = $dist * 60 * 1.1515;

        return $miles;
    }

    private function getLocationString($locationID){
        return \DB::table('4pp')->where('id', $locationID)->first();
    }

}