1
0
mirror of https://koodu.h-i.works/projects/thebadspace synced 2025-05-06 14:41:02 -05:00
thebadspace/app/Repositories/LocationRepository.php
ro 07793a413a Cleaned up migrations, full text search in model
Migrations were pretty much useless for database set up, so that needed
some attention to make setting up the project easier. Now, all necessary
tables can be created by running the `artisan migrate` command assuming
one has the database parameters set in the .env file

also added full text search capabilites through the database set up,
which the model itself can use to find locations, so the search has been
updated in the appropriate locations as well.

still need to add initial account set up and a form for adding sources
through the UI, but this was a big step towards letting anyone set up
their own version of tbs
2025-04-15 13:58:12 -06:00

131 lines
4.3 KiB
PHP

<?php
namespace App\Repositories;
use App\Models\Location;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
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]);
$raw = Location::search($terms)->get();
$results = [];
foreach ($raw as $item) {
if (($item->block_count + $item->silence_count) >= 2) {
array_push($results, $item);
}
}
return $results;
}
public function getLocation($type)
{
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();
}
}
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 editLocation($request)
{
$location = $this->getLocation($request->id);
$publicPath = '../public/';
$refPath = 'assets/images/references/' . $location->uuid;
$images = [];
if ($request->hasfile("references")) {
foreach ($request->references as $file) {
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]);
}
}
if (!empty($images)) {
$request->merge(['images' => json_encode($images)]);
$location->images = json_encode($images);
}
$location->name = $request->name;
$location->notes = $request->notes;
$location->archive_links = $request->archive_links;
$result = [];
if ($location->save()) {
return ['status' => true, 'message' => "Location Editited" . $request->hasfile("references")];
} else {
return ['status' => false, 'message' => "Location Not Editited"];
}
}
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',
]);
}
}
}