forked from projects/thebadspace
ro
573054e7d8
The Location Controller was getting too heavy so an Update and Maintenance service class was created to offload most of it's functionality. Location upating was moved to LocationRepository There was also a small issue with responsive list links not adapting properly in Safari that was fixed
113 lines
3.4 KiB
PHP
113 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace App\Repositories;
|
|
|
|
use App\Models\Location;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
class LocationRepository
|
|
{
|
|
protected $model;
|
|
private $limit = 15;
|
|
|
|
public function __construct(Location $model)
|
|
{
|
|
$this->model = $model;
|
|
}
|
|
|
|
public function search($request)
|
|
{
|
|
// this grabs the search results from the db
|
|
$terms = $request->index_search;
|
|
$rawSearch = $terms;
|
|
$terms = str_replace(",", "", $terms);
|
|
$terms = str_replace(" ", "|", $terms);
|
|
$raw = DB::select("SELECT * FROM searchlocations(?)", [$terms]);
|
|
$results = [];
|
|
foreach ($raw as $item) {
|
|
if (($item->block_count + $item->silence_count) >= 2) {
|
|
array_push($results, $item);
|
|
}
|
|
}
|
|
|
|
return $results;
|
|
}
|
|
|
|
public function getLocation($uuid)
|
|
{
|
|
return $this->model::where("uuid", $uuid)->first();
|
|
}
|
|
|
|
public function getActiveLocations()
|
|
{
|
|
return $this->model::where("active", true)->where('actions_count', '>=', 2)->get();
|
|
}
|
|
|
|
public function getRecent()
|
|
{
|
|
return $locations = $this->model::where("active", true)->where('actions_count', '>=', 2)
|
|
->orderByDesc('updated_at')->limit(10)->get();
|
|
}
|
|
|
|
public function getPage($pageNum)
|
|
{
|
|
$range = $pageNum * $this->limit - $this->limit;
|
|
$active = $this->model::where("active", true)->where('actions_count', '>=', 2)->get();
|
|
$locations = $this->model::where("active", true)->where('actions_count', '>=', 2)
|
|
->limit($this->limit)->offset($range)->orderBy('id', 'asc')->get();
|
|
$pageCount = ceil(count($active) / $this->limit);
|
|
|
|
$next = $pageNum + 1;
|
|
if ($next > $pageCount) {
|
|
$next = 1;
|
|
}
|
|
|
|
$prev = $pageNum - 1;
|
|
|
|
if ($prev <= 0) {
|
|
$prev = $pageCount;
|
|
}
|
|
|
|
return $result = [$locations, $pageCount, $prev, $next];
|
|
}
|
|
|
|
public function addLocation(Request $request)
|
|
{
|
|
$fields = $request->validate([
|
|
'name' => ['required'],
|
|
'url' => ['required'],
|
|
'description' => ['required'],
|
|
'tags' => ['required'],
|
|
]);
|
|
|
|
if ($fields) {
|
|
$examples = [];
|
|
$files = $request->files->get("loc_examples");
|
|
if ($request->hasfile('loc_examples')) {
|
|
foreach ($request->file('loc_examples') as $file) {
|
|
$path = $file->store('reference');
|
|
array_push($examples, ["path" => $path]);
|
|
}
|
|
}
|
|
$request->merge(['active' => true]);
|
|
$request->merge(['uuid' => Uuid::uuid4()]);
|
|
$request->merge(['images' => json_encode($examples)]);
|
|
$request->merge(['added_by' => Auth::user()->id]);
|
|
//NOTE: Laravel gets funky if sequencing isn't explicitly set
|
|
$new = Location::create($request->all());
|
|
if ($new) {
|
|
return back()->with('message', 'New Location Added. Take a break!');
|
|
} else {
|
|
return back()->withErrors([
|
|
'error' => 'Uh oh. There was an inssue',
|
|
]);
|
|
}
|
|
} else {
|
|
return back()->withErrors([
|
|
'error' => 'All fields are required',
|
|
]);
|
|
}
|
|
}
|
|
}
|