2024-02-09 21:53:08 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Repositories;
|
|
|
|
|
|
|
|
use App\Models\Location;
|
2024-02-20 23:33:49 +01:00
|
|
|
use Illuminate\Http\Request;
|
2024-02-09 21:53:08 +01:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2024-02-15 22:00:03 +01:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
2024-02-09 21:53:08 +01:00
|
|
|
|
|
|
|
class LocationRepository
|
|
|
|
{
|
|
|
|
protected $model;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-02-16 22:34:09 +01:00
|
|
|
public function getLocation($type)
|
2024-02-09 21:53:08 +01:00
|
|
|
{
|
2024-02-16 22:34:09 +01:00
|
|
|
if (!is_string($type) || (preg_match('/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/', $type) !== 1)) {
|
|
|
|
return $this->model::where("url", $type)->first();
|
|
|
|
} else {
|
|
|
|
return $this->model::where("uuid", $type)->first();
|
|
|
|
}
|
2024-02-09 21:53:08 +01:00
|
|
|
}
|
|
|
|
|
2024-02-10 18:48:09 +01:00
|
|
|
public function getActiveLocations()
|
|
|
|
{
|
|
|
|
return $this->model::where("active", true)->where('actions_count', '>=', 2)->get();
|
|
|
|
}
|
|
|
|
|
2024-02-09 21:53:08 +01:00
|
|
|
public function getRecent()
|
|
|
|
{
|
2024-02-10 18:48:09 +01:00
|
|
|
return $locations = $this->model::where("active", true)->where('actions_count', '>=', 2)
|
|
|
|
->orderByDesc('updated_at')->limit(10)->get();
|
2024-02-09 21:53:08 +01:00
|
|
|
}
|
|
|
|
|
2024-02-20 23:33:49 +01:00
|
|
|
public function editLocation($request)
|
|
|
|
{
|
2024-09-13 23:12:43 +02:00
|
|
|
$location = $this->getLocation($request->id);
|
|
|
|
$publicPath = '../public/';
|
|
|
|
$refPath = 'assets/images/references/' . $location->uuid;
|
|
|
|
$images = [];
|
2024-02-20 23:33:49 +01:00
|
|
|
if ($request->hasfile("references")) {
|
|
|
|
foreach ($request->references as $file) {
|
2024-09-13 23:12:43 +02:00
|
|
|
if (!is_dir($publicPath . $refPath)) {
|
|
|
|
mkdir($publicPath . $refPath, 0755, true);
|
|
|
|
}
|
|
|
|
$filename = urlencode($file->getClientOriginalName());
|
|
|
|
$file->move($publicPath . $refPath, $filename);
|
|
|
|
//$path = $file->store('reference');
|
|
|
|
array_push($images, ["path" => '/' . $refPath . '/' . $filename]);
|
2024-02-20 23:33:49 +01:00
|
|
|
}
|
|
|
|
}
|
2024-09-13 23:12:43 +02:00
|
|
|
if (!empty($images)) {
|
|
|
|
$request->merge(['images' => json_encode($images)]);
|
|
|
|
$location->images = json_encode($images);
|
|
|
|
}
|
|
|
|
|
2024-10-01 19:39:55 +02:00
|
|
|
$location->name = $request->name;
|
|
|
|
//$location->description = $request->description;
|
2024-02-20 23:33:49 +01:00
|
|
|
$location->archive_links = $request->archive_links;
|
|
|
|
|
|
|
|
$result = [];
|
|
|
|
|
|
|
|
if ($location->save()) {
|
2024-09-13 23:12:43 +02:00
|
|
|
return ['status' => true, 'message' => "Location Editited" . $request->hasfile("references")];
|
2024-02-20 23:33:49 +01:00
|
|
|
} else {
|
|
|
|
return ['status' => false, 'message' => "Location Not Editited"];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-15 22:00:03 +01:00
|
|
|
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',
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
2024-02-09 21:53:08 +01:00
|
|
|
}
|