Updated editIcons function

This commit is contained in:
Julian Prieber 2024-02-04 17:49:53 +01:00
parent 6aad9e2010
commit 75935a5264
1 changed files with 36 additions and 67 deletions

View File

@ -1201,28 +1201,46 @@ class UserController extends Controller
} }
//Edit/save page icons //Edit/save page icons
public function editIcons(request $request) public function editIcons(Request $request)
{ {
$request->validate([ $inputKeys = array_keys($request->except('_token'));
'link' => 'required|url',
]);
function searchIcon($icon) $validationRules = [];
foreach ($inputKeys as $platform) {
$validationRules[$platform] = 'nullable|url|max:255';
}
$request->validate($validationRules);
foreach ($inputKeys as $platform) {
$link = $request->input($platform);
if (!empty($link)) {
$iconId = $this->searchIcon($platform);
if (!is_null($iconId)) {
$this->updateIcon($platform, $link);
} else {
$this->addIcon($platform, $link);
}
}
}
return redirect('studio/links#icons');
}
private function searchIcon($icon)
{ {
$iconId = DB::table('links') return DB::table('links')
->where('user_id', Auth::id()) ->where('user_id', Auth::id())
->where('title', $icon) ->where('title', $icon)
->where('button_id', 94) ->where('button_id', 94)
->value('id'); ->value('id');
if (is_null($iconId)){
return false;
} else {
return $iconId;
}
} }
function addIcon($icon, $link){ private function addIcon($icon, $link)
{
$userId = Auth::user()->id; $userId = Auth::user()->id;
$links = new Link; $links = new Link;
$links->link = $link; $links->link = $link;
@ -1234,61 +1252,12 @@ class UserController extends Controller
$links->save(); $links->save();
} }
function updateIcon($icon, $link){ private function updateIcon($icon, $link)
Link::where('id', searchIcon($icon))->update([ {
Link::where('id', $this->searchIcon($icon))->update([
'button_id' => 94, 'button_id' => 94,
'link' => $link, 'link' => $link,
'title' => $icon 'title' => $icon
]); ]);
} }
function saveIcon($icon, $link){
if(isset($link)){
if(searchIcon($icon) != NULL){
updateIcon($icon, $link);
}else{
addIcon($icon, $link);}
}
}
saveIcon('mastodon', $request->mastodon);
saveIcon('instagram', $request->instagram);
saveIcon('twitter', $request->twitter);
saveIcon('facebook', $request->facebook);
saveIcon('github', $request->github);
saveIcon('linkedin', $request->linkedin);
saveIcon('tiktok', $request->tiktok);
saveIcon('discord', $request->discord);
saveIcon('youtube', $request->youtube);
saveIcon('snapchat', $request->snapchat);
saveIcon('reddit', $request->reddit);
saveIcon('pinterest', $request->pinterest);
saveIcon('telegram', $request->telegram);
saveIcon('whatsapp', $request->whatsapp);
saveIcon('twitch', $request->twitch);
return Redirect('studio/links#icons');
}
} }