PATH:
home
/
rwabteecom
/
project_11
/
app
/
Helpers
/
Editing: helper.php
<?php use App\Enums\OnboardingEnum; use App\Http\Controllers\Dashboard\Analytics\AnalyticsController; use App\Models\Analytics; use App\Models\Domain; use App\Models\DomainSocial; use App\Models\GeneralSetting; use App\Models\Link; use App\Models\Theme; use App\Models\User; use Illuminate\Support\Arr; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Response; use Illuminate\Support\Facades\Storage; use Illuminate\Support\Str; use Symfony\Component\DomCrawler\Crawler; function gs($key) { $gs = Cache::rememberForever("gs", function () { return GeneralSetting::query()->first(); }); return $gs->$key; } function getGeneral() { return Cache::rememberForever("gs", function () { return GeneralSetting::query()->first(); }); } function currentDomain() { // return Cache::rememberForever("domain", function () { if (auth()->user()) { return auth()->user()->domain; } // }); } function currentUserFromUrl() { // return Cache::rememberForever("domain", function () { return User::whereHas('domain',function($q){ $q->where('domain',request()->route('domain')); })->first(); // }); } function currentDomainFromUrl() { // return Cache::rememberForever("domain", function () { return Domain::where('domain',request()->route('domain'))->first(); // }); } function slug($string, $separator = '-'): array|string|null { if (is_null($string)) { return ""; } $string = trim(htmlspecialchars($string)); $string = strtolower(str_replace([ ',', '?', '"', '.', '/', '|', '\\', '~', '+', '>', '<', '؟', '#', '/', '\\', ' ', '\'', '"', '`', '!', '@', '$', '%', '^', '&', '*', '(', ')', '=' ], '-', $string)); $string = mb_strtolower($string, "UTF-8");; $string = preg_replace("/[^a-z0-9_\sءاأإآؤئبتثجحخدذرزسشصضطظعغفقكلمنهويةى]#u/", "", $string); $string = preg_replace("/[\s-]+/", " ", $string); return preg_replace("/[\s_]/", $separator, $string); } function uuid() { return (string) Str::uuid(); } function uploadImgBase64($name, $collection, $model): void { if (request()->$name) { if (is_array(request()->$name)) { foreach (request()->$name as $file) { $extantion = explode(';', $file); $extantion = str_replace('data:image/', '', $extantion[0]); $filename = Str::random(10) . "." . $extantion; $model->addMediaFromBase64($file)->usingFileName($filename)->toMediaCollection($collection); } } else { $extantion = explode(';', request()->$name); $extantion = str_replace('data:image/', '', $extantion[0]); $filename = Str::random(10) . "." . $extantion; $model->addMediaFromBase64(request()->$name)->usingFileName($filename)->toMediaCollection($collection); } } } function analytics($type,$uuid): void { if($type == "link"){ $link=Link::where('uuid',$uuid)->first(); Analytics::create( [ 'domain_id'=>$link->domain_id, 'link_id'=>$link->id, 'ip'=>request()->ip(), ]); } if($type== "social"){ $social=DomainSocial::where('id',$uuid)->first(); Analytics::create( [ 'domain_id'=>$social->domain_id, 'domain_socials_id'=>$social->id, 'ip'=>request()->ip(), ]); } if($type== "view"){ $domain=Domain::where('domain',$uuid)->first(); Analytics::create( [ 'domain_id'=>$domain->id, 'ip'=>request()->ip(), ]); } } function getCTR($click,$view) { if($view > 0 && $click > 0){ $number= number_format($click / $view *100,2); }else{ $number= 0; } return $number; } function scrapeTitleFromUrl($url,$before_link) { // Make a GET request to the URL $response = Http::get($before_link.$url); // Check if request was successful if ($response->successful()) { // Get the HTML content of the response $html = $response->body(); // Create a new Crawler instance and load HTML content $crawler = new Crawler($html); // Extract the title tag text $title = $crawler->filter('title')->text(); return $title; } else { // If request was unsuccessful, handle the error return 'Error: Unable to fetch URL'; } } function getUserLink() { return env('APP_URL') . '/' . auth()->user()->domain->domain; } function extractJsonFile($domain) { $realDomain = Domain::query()->find($domain); $domain = $realDomain->currentTheme; $id = $domain->id; $fileName = "themes/$realDomain->domain" . "_" . "$id.json"; $attributes = [ "site_title"=>$realDomain->site_title, "intro_text"=>$realDomain->intro_text, "hide_branding"=>$realDomain->hide_branding, "font"=>$domain->font, "text_color"=>$domain->text_color, "layout"=>$domain->layout, "background_style"=>$domain->background_style, "background_color"=>$domain->background_color, "background_first_color"=>$domain->background_first_color, "background_second_color"=>$domain->background_second_color, "background_direction"=>$domain->background_direction, "button_font"=>$domain->button_font, "button_text_weight"=>$domain->button_text_weight, "button_background"=>$domain->button_background, "button_color"=>$domain->button_color, "button_transparency"=>$domain->button_transparency, "button_border_radius"=>$domain->button_border_radius, "button_border_width"=>$domain->button_border_width, "button_border_color"=>$domain->button_border_color, "button_shadow_xoffset"=>$domain->button_shadow_xoffset, "button_shadow_yoffset"=>$domain->button_shadow_yoffset, "button_shadow_blur"=>$domain->button_shadow_blur, "button_shadow_spread"=>$domain->button_shadow_spread, "button_shadow_color"=>$domain->button_shadow_color, "button_shadow_transparency"=>$domain->button_shadow_transparency, "button_image_radius"=>$domain->button_image_radius, "header_layout"=>$domain->header_layout, "hide_profile_image"=>$domain->hide_profile_image, "hide_profile_title"=>$domain->hide_profile_title, "hide_community"=>$domain->hide_community, "enable_banner_image"=>$domain->enable_banner_image, "social_icons_style"=>$domain->social_icons_style, "social_icons_color"=>$domain->social_icons_color, "social_icons_position"=>$domain->social_icons_position, "status"=>$domain->status, "logo" => ($realDomain->hasMedia('logo')) ? $realDomain->getFirstMediaUrl('logo') : false, "background" => $realDomain->backgroundImg, "banner_image"=>$realDomain->banner? asset($realDomain->banner):false, ]; Storage::disk('local')->put($fileName, json_encode($attributes)); $file_path= storage_path("app/".$fileName); if ( file_exists( $file_path ) ) { // Send Download return Storage::download($fileName); } else { // Error exit( 'Requested file does not exist on our server!' ); } } function installTheme($theme_id, $fileName): void { $attributes = json_decode(file_get_contents($fileName), true); $theme = Theme::query()->find($theme_id); $data= Arr::except($attributes, ["banner_image", 'background' , 'logo']); $theme->update($data); if(config('app.env') != 'local'){ if($attributes['banner_image']){ $theme->addMediaFromUrl($attributes['banner_image'])->preservingOriginal()->toMediaCollection('banner_image'); } if($attributes['logo']){ $theme->addMediaFromUrl($attributes['logo'])->preservingOriginal()->toMediaCollection('logo'); } if($attributes['background']){ $theme->addMediaFromUrl($attributes['background'])->preservingOriginal()->toMediaCollection('background_image'); } } } function skipBoardingStep($skipTo = null) { $boardingStep = null; if($skipTo != null){ Domain::query()->where('id' , auth()->user()->domain->id)->update([ 'onboarding' => $skipTo ]); return $skipTo; } // move to next step if there $currentStep = false; foreach(OnboardingEnum::steps() as $index => $val) { if($val == $boardingStep){ $currentStep = $index; } } if($currentStep !== false){ $boardingStep = OnboardingEnum::steps()[$currentStep+1]; Domain::query()->where('id' , auth()->user()->domain->id)->update([ 'onboarding' => $boardingStep ]); }else{ // finished onboarding $boardingStep = null; Domain::query()->where('id' , auth()->user()->domain->id)->update([ 'onboarding' => $boardingStep ]); } return $boardingStep; }
SAVE
CANCEL