PATH:
home
/
rwabteecom
/
project_11
/
app
/
Services
/
Link
/
Editing: LinkServices.php
<?php namespace App\Services\Link; use App\Interfaces\ServiceInterface; use App\Models\Domain; use App\Models\Link; use Illuminate\Database\Eloquent\Builder; use Illuminate\Support\Facades\DB; use Str; class LinkServices implements ServiceInterface { public function index($active = false) { return Link::query() ->where('domain_id', auth()->user()->domain->id) ->when($active, function (Builder $query) { return $query->active(); }) ->orderBy('order', 'ASC') ->get(); } public function findOrFail($id, $active = false, $withTrashed = false) { return Link::query() ->where('domain_id', auth()->user()->domain->id) ->when($active, function (Builder $query) { return $query->active(); }) ->when($withTrashed, function (Builder $query) { return $query->withTrashed(); }) ->findOrFail($id); } public function findWithoutFail($id, $active = false, $withTrashed = false) { return Link::query() ->where('domain_id', auth()->user()->domain->id) ->when($active, function (Builder $query) { return $query->active(); }) ->when($withTrashed, function (Builder $query) { return $query->withTrashed(); }) ->where('uuid', $id)->first(); } private function linkBeforHttp($link){ $url = $link['link']; $prefix = isset($link['before_link']) ? $link['before_link'] : null; if($prefix == 'http://' || $prefix == 'https://'){ $url = str_replace($prefix, '', $url); } if($prefix == null){ $prefix = 'https://'; $url = preg_replace('/^https?:\/\//', '', $url); } return [ 'url' => $url, 'prefix' => $prefix ]; } public function store($request): void { DB::transaction(function () use ($request) { foreach ($request->link as $link) { if(strlen($link['link']) == 0){ continue; } $urlSanitizer = self::linkBeforHttp($link); $link['link'] = $urlSanitizer['url']; $link['before_link'] = $urlSanitizer['prefix']; $links_db = null; if(isset($link['uuid'])){ $links_db = Link::where('domain_id', auth()->user()->domain->id)->where('uuid', $link['uuid'])?->first(); } if ($links_db) { if (isset($link['pen']) && $link['pen'] == 1) { Link::where('domain_id', auth()->user()->domain->id)->update(['pen' => 0]); $order_link= 0; }else{ $order_link= (isset($link['order'])) ? $link['order'] ?? null : null; } $links_db->update( [ 'title' => (isset($link['title'])) ? $link['title'] ?? null : null, 'link' => (isset($link['link'])) ? $link['link'] ?? null : null, 'link_type' => (isset($link['link_type'])) ? $link['link_type'] ?? null : null, 'link_visibility' => (isset($link['link_visibility'])) ? $link['link_visibility'] ?? 0 : 0, 'pen' => (isset($link['pen'])) ? $link['pen'] ?? 0 : 0, 'lock_link' => (isset($link['lock_link'])) ? $link['lock_link'] ?? 0 : 0, 'link_animation' => (isset($link['link_animation'])) ? $link['link_animation'] ?? null : null, 'order' => $order_link, //'uuid'=>(isset($link['uuid']))? $link['uuid']?? null: null , 'before_link' => (isset($link['before_link'])) ? $link['before_link'] ?? null : null, 'divider_height' => (isset($link['divider_height'])) ? $link['divider_height'] ?? null : null, 'password' => (isset($link['password'])) ? $link['password'] ?? null : null, 'divider_color' => (isset($link['divider_color'])) ? $link['divider_color'] ?? null : null, ] ); if (env('APP_URL') != 'http://localhost:8000') { if (isset($link['image']) && $link['image']) { $links_db->addMediaFromUrl($link['image'])->toMediaCollection('image'); } if ($request->removeImage) { $mediaItem = $links_db->getMedia('image')->first(); if ($mediaItem) { $mediaItem->delete(); } } } } else { if (isset($link['pen']) && $link['pen'] == 1) { Link::where('domain_id', auth()->user()->domain->id)->update(['pen' => 0]); } $l = Link::create( [ 'domain_id' => auth()->user()->domain->id, 'title' => (isset($link['title'])) ? $link['title'] ?? 'Website' : 'Website', 'link' => (isset($link['link'])) ? $link['link'] ?? null : null, 'link_type' => (isset($link['link_type'])) ? $link['link_type'] ?? 'link' : 'link', 'link_visibility' => (isset($link['link_visibility'])) ? $link['link_visibility'] ?? 0 : 1, 'pen' => (isset($link['pen'])) ? $link['pen'] ?? 0 : 0, 'lock_link' => (isset($link['lock_link'])) ? $link['lock_link'] ?? 0 : 0, 'link_animation' => (isset($link['link_animation'])) ? $link['link_animation'] ?? null : null, 'order' => (isset($link['order'])) ? $link['order'] ?? null : null, 'uuid' => (isset($link['uuid'])) ? $link['uuid'] ?? null : Str::uuid(), 'before_link' => (isset($link['before_link'])) ? $link['before_link'] ?? null : null, 'divider_height' => (isset($link['divider_height'])) ? $link['divider_height'] ?? null : null, 'password' => (isset($link['password'])) ? $link['password'] ?? null : null, 'divider_color' => (isset($link['divider_color'])) ? $link['divider_color'] ?? null : null, ] ); if (env('APP_URL') != 'http://localhost:8000') { if (isset($link['image']) && $link['image']) { $l->addMediaFromUrl($link['image'])->toMediaCollection('image'); //'https://epicwall.net/logo/logo-welcome.png' } if ($request->removeImage) { $mediaItem = $l->getMedia('image')->first(); if ($mediaItem) { $mediaItem->delete(); } } } } } }); } public function update($id, $request) { DB::transaction(function () use ($id, $request) { $link = $this->findOrFail($id); $link->update([ "title" => $request->title, "description" => $request->description ]); }); } public function destroy($id): void { DB::transaction(function () use ($id) { $link = $this->findOrFail($id); $link->delete(); }); } public function restore($id): void { DB::transaction(function () use ($id) { $link = $this->findOrFail(id: $id, withTrashed: true); $link->restore(); }); } public function updateImage($request) { DB::transaction(function () use ($request) { $domains = Domain::where('id', auth()->user()->domain->id)->first(); uploadImgBase64('images', 'media_library', $domains); }); } }
SAVE
CANCEL