/** * Creates or updates a character * @param array $data Array of fields. * @return null|integer Returns the character id on success or null. */ public static function CreateOrUpdate($data) { $char = ['name' => $data['name'], 'class' => $data['class'], 'diablo_id' => $data['id'], 'level' => $data['level'], 'hardcore' => $data['hardcore'] == "true", 'season' => $data['seasonal'] == "true", 'owner_id' => $data['owner_id']]; /* Check if character already exists */ try { $character = Character::where('diablo_id', $char['diablo_id'])->firstOrFail(); /* Character Exists - Update */ foreach ($char as $key => $val) { $character->{$key} = $val; } $character->save(); return $character->id; } catch (ModelNotFoundException $e) { /* Does not exist - Add */ Character::create($char); /* Return the created characters id */ try { $character = Character::where('diablo_id', $char['diablo_id'])->firstOrFail(); return $character->id; } catch (Exception $e) { return null; } } catch (Exception $e) { \Log::error($e->getMessage()); } }
/** * Show the application dashboard to the user. * * @return Response */ public function index() { $userCount = User::where('id', '>=', 1)->count(); $memberCount = Member::where('id', '>=', 1)->count(); $charCount = Character::where('id', '>=', 1)->count(); $data = ['user' => Auth::user(), 'sitename' => \Config::get('diablo.sitename'), 'dashboard' => ['counters' => ['users' => ['title' => 'Users', 'value' => $userCount, 'url' => ''], 'members' => ['title' => 'Members', 'value' => $memberCount, 'url' => ''], 'characters' => ['title' => 'Characters', 'value' => $charCount, 'url' => '']]]]; return view('admin/index', $data); }
public function deleteCharacter($id) { try { $character = Character::where('id', $id)->firstOrFail(); } catch (\Exception $e) { return response('Bad Request.', 400); } /* Is Admin? or Character Owner */ $user = Auth::user(); if ($user->isAdmin() || $user->id == $character->member->id) { $character->delete(); } }