From 2a7f7446341f3efa12397ca51bbfba648f62b268 Mon Sep 17 00:00:00 2001 From: Seph Date: Wed, 7 Aug 2024 22:34:54 +0200 Subject: [PATCH] Lists --- tailwind/custom/file-header.css | 2 +- theme/functions.php | 4 + theme/inc/config-functions.php | 4 + theme/inc/config-routing.php | 54 ++++++-- theme/pages/charlists.php | 57 +++++++++ theme/pages/raids.php | 2 +- theme/pages/single_charlist.php | 118 ++++++++++++++++++ theme/request.php | 73 ++++++++++- .../template-parts/layout/header-content.php | 6 +- .../Database/CreateCharListItemTable.php | 17 +++ .../Database/CreateCharListsTable.php | 16 +++ theme/wowpress/Models/Character.php | 14 +++ theme/wowpress/Models/CharacterList.php | 35 ++++++ theme/wowpress/Models/CharacterListItem.php | 35 ++++++ theme/wowpress/Models/Notification.php | 9 ++ theme/wowpress/Models/Raid.php | 45 +++---- 16 files changed, 451 insertions(+), 40 deletions(-) create mode 100644 theme/pages/charlists.php create mode 100644 theme/pages/single_charlist.php create mode 100644 theme/wowpress/Database/CreateCharListItemTable.php create mode 100644 theme/wowpress/Database/CreateCharListsTable.php create mode 100644 theme/wowpress/Models/CharacterList.php create mode 100644 theme/wowpress/Models/CharacterListItem.php diff --git a/tailwind/custom/file-header.css b/tailwind/custom/file-header.css index 38d470d..6c8557f 100644 --- a/tailwind/custom/file-header.css +++ b/tailwind/custom/file-header.css @@ -4,7 +4,7 @@ Theme URI: https://nebelkrieger.de Author: Yorndar Author URI: https://nebelkrieger.de Description: The next gen NK-Theme -Version: 0.4.0 +Version: 0.4.2 Tested up to: 6.2 Requires PHP: 8.0 License: GNU General Public License v2 or later diff --git a/theme/functions.php b/theme/functions.php index e120f54..aec5eb8 100644 --- a/theme/functions.php +++ b/theme/functions.php @@ -2,6 +2,8 @@ use WoWPress\Database\CreateCacheTable; use WoWPress\Database\CreateCharacterTable; +use WoWPress\Database\CreateCharListItemTable; +use WoWPress\Database\CreateCharListsTable; use WoWPress\Database\CreateComplaintTable; use WoWPress\Database\CreateLogTable; use WoWPress\Database\CreateNotificationTable; @@ -163,6 +165,8 @@ if (!function_exists('wowpress_database')) : CreateLogTable::sql(), CreateComplaintTable::sql(), CreateNotificationTable::sql(), + CreateCharListsTable::sql(), + CreateCharListItemTable::sql(), ]); } diff --git a/theme/inc/config-functions.php b/theme/inc/config-functions.php index e51b894..52888cf 100644 --- a/theme/inc/config-functions.php +++ b/theme/inc/config-functions.php @@ -84,6 +84,10 @@ function isAllowed($capability) endif; } +function checkNonce($nonce){ + return isset($_POST[$nonce.'_nonce']) && wp_verify_nonce($_POST[$nonce.'_nonce'], $nonce); +} + function setGlobalUser(){ global $user; diff --git a/theme/inc/config-routing.php b/theme/inc/config-routing.php index c41f2c2..cff15fd 100644 --- a/theme/inc/config-routing.php +++ b/theme/inc/config-routing.php @@ -1,5 +1,7 @@ 'index.php?wowpress_page=db'], ['kummerkasten/?$' => 'index.php?wowpress_page=complaints'], ['kummerkasten/(\d+)/?$' => 'index.php?wowpress_page=complaints&complaint_id=$matches[1]'], + ['lists/?$' => 'index.php?wowpress_page=lists'], + ['list/(\d+)/?$' => 'index.php?wowpress_page=lists&list_id=$matches[1]'], $wp_rewrite->rules ); @@ -29,6 +33,7 @@ add_filter('query_vars', function ($query_vars) { $query_vars[] = 'month'; $query_vars[] = 'raid_id'; $query_vars[] = 'list_name'; + $query_vars[] = 'list_id'; $query_vars[] = 'complaint_id'; @@ -48,6 +53,18 @@ add_action('template_redirect', function () { header('Location: /'); die; break; + case 'lists': + $list = get_query_var('list_id'); + $list = CharacterList::find($list); + if ($list) { + $GLOBALS['wowpress']['page_title'] = $list->list_name; + include plugin_dir_path(__FILE__) . '../pages/single_charlist.php'; + } else { + $GLOBALS['wowpress']['page_title'] = "Charakterlisten"; + include plugin_dir_path(__FILE__) . '../pages/charlists.php'; + } + die; + break; case 'complaints': isAllowed('wowpress_view_complaints'); $GLOBALS['wowpress']['page_title'] = "Kummerkasten"; @@ -68,20 +85,43 @@ add_action('template_redirect', function () { die; break; case 'logs': - + + # $cl = new CharacterList(); + # $cl->list_name = "Strike-Liste TWW Season 1"; + # $cl->border_color = "red"; + # $cl->max_count = 6; + # $cl->notify = true; + # $cl->save(); + + $cl = CharacterList::find(14); + + + + + $char = Character::first(); + + #$char = Character::find(8); + + + $cl->addItem($char,'Richtig Dumm','2024-09-26'); + + dd($cl->characters); + + + exit; isAllowed('wowpress_edit_raids'); $GLOBALS['wowpress']['page_title'] = "Logs"; echo "
"; echo '
'; - foreach(Log::all() as $log){ - ?> + foreach (Log::all() as $log) { +?>
-
Message: message?>
-
Action: action?>
-
Logged: created_at,"dd.MM.YY HH:mm")?>
+
Message: message ?>
+
Action: action ?>
+
Logged: created_at, "dd.MM.YY HH:mm") ?>

- "; die; diff --git a/theme/pages/charlists.php b/theme/pages/charlists.php new file mode 100644 index 0000000..9a9356d --- /dev/null +++ b/theme/pages/charlists.php @@ -0,0 +1,57 @@ +get(); +get_header(); + + +?> +
+
+ +
+
+ +
+
+ + +
list_name ?>
+
(characters()->groupBy('ID')->get()->count() ?> Charaktere)
+
+ +
+ + + + +
+ + -
title ?>
+
title ?>
difficulty) : ?>
(difficulty ?>)
get(); +get_header(); + +?> +
+
+ +
+
+ +
+
+
+ characters()->get()->groupBy('ID') as $character) : ?> +
+
+
first()->classButton ?>
+
+
+ count() ?>x +
+ +
+ +
+
+ + + + +
+ +complaint = $_POST['complaint']; $c->anonymous = !empty($_POST['hide']); $c->save(); + Notification::addCapabilityNotification("wowpress_edit_complaints", "Neuer Eintrag im Kummerkasten!"); } } break; @@ -334,24 +340,79 @@ switch ($_POST['action']) { case 'updatePageBG': isAllowed('wowpress_edit_site'); if (isset($_POST['updatePageBG_nonce']) && wp_verify_nonce($_POST['updatePageBG_nonce'], 'updatePageBG')) { - if (isset($_FILES['bg']) && $_FILES['bg']['type'] == "image/jpeg" ) { - $bg_path = ABSPATH."/wp-content/bg.jpg"; - rename($_FILES['bg']['tmp_name'],$bg_path); - chmod(ABSPATH."/wp-content/bg.jpg",0644); + if (isset($_FILES['bg']) && $_FILES['bg']['type'] == "image/jpeg") { + $bg_path = ABSPATH . "/wp-content/bg.jpg"; + rename($_FILES['bg']['tmp_name'], $bg_path); + chmod(ABSPATH . "/wp-content/bg.jpg", 0644); } } break; case 'updateRaidTitle': isAllowed('wowpress_edit_raids'); if (isset($_POST['updateRaidTitle_nonce']) && wp_verify_nonce($_POST['updateRaidTitle_nonce'], 'updateRaidTitle')) { - if(isset($_POST['raid_id']) && isset($_POST['name'])){ + if (isset($_POST['raid_id']) && isset($_POST['name'])) { $raid = Raid::find($_POST['raid_id']); - if($raid){ + if ($raid) { $raid->setTitle($_POST['name']); } } } + case 'addList': + isAllowed('wowpress_edit_raids'); + if(checkNonce('addList')){ + if(isset($_POST['list_name'],$_POST['border_color'])){ + $list = new CharacterList(); + $list->list_name = $_POST['list_name']; + $list->border_color = $_POST['border_color']; + $list->notify = !empty($_POST['notify']); + $list->max_count = 18; + $list->save(); + } + } + case 'addCharToList': + isAllowed('wowpress_edit_raids'); + if (isset($_POST['addCharToList_nonce']) && wp_verify_nonce($_POST['addCharToList_nonce'], 'addCharToList')) { + if(isset($_POST['character_ID']) && isset($_POST['list_id'])){ + $list = CharacterList::find($_POST['list_id']); + $char = Character::find($_POST['character_ID']); + if($char && $list){ + $comment = ""; + $date = date('Y-m-d'); + if(!empty($_POST['comment'])){ + $comment = $_POST['comment']; + } + if(!empty($_POST['date'])){ + $date = $_POST['date']; + } + $list->addItem($char,$comment,$date); + } + } + + } + break; + case 'removeListItem': + isAllowed('wowpress_edit_raids'); + if(checkNonce('removeListItem')){ + if(isset($_POST['item_id'])){ + $item_id = intval($_POST['item_id']); + $item = CharacterListItem::find($item_id); + if($item){ + $item->delete(); + #CharacterListItem::deleteOrphans(); + } + } + } + break; + case 'cron': + if(isset($_POST['cron'])){ + switch($_POST['cron']){ + case 'update_applications': + + break; + } + } + break; } header('Location: ' . $_SERVER['HTTP_REFERER']); diff --git a/theme/template-parts/layout/header-content.php b/theme/template-parts/layout/header-content.php index 32afed9..1437149 100644 --- a/theme/template-parts/layout/header-content.php +++ b/theme/template-parts/layout/header-content.php @@ -136,14 +136,14 @@ if (is_user_logged_in()) {
-
+
-
note ?>
- +
note ?>
+
diff --git a/theme/wowpress/Database/CreateCharListItemTable.php b/theme/wowpress/Database/CreateCharListItemTable.php new file mode 100644 index 0000000..4388849 --- /dev/null +++ b/theme/wowpress/Database/CreateCharListItemTable.php @@ -0,0 +1,17 @@ + 'mediumint NOT NULL', + 'list_id' => 'mediumint NOT NULL', + 'date' => 'date NOT NULL', + 'count_multiplier' => 'tinyint NOT NULL', + 'comment' => 'text', + ]; + +} diff --git a/theme/wowpress/Database/CreateCharListsTable.php b/theme/wowpress/Database/CreateCharListsTable.php new file mode 100644 index 0000000..e35462d --- /dev/null +++ b/theme/wowpress/Database/CreateCharListsTable.php @@ -0,0 +1,16 @@ + 'text NOT NULL', + 'border_color' => 'text NOT NULL', + 'max_count' => 'mediumint NOT NULL', + 'notify' => 'boolean' + ]; + +} diff --git a/theme/wowpress/Models/Character.php b/theme/wowpress/Models/Character.php index 643b285..21479f4 100644 --- a/theme/wowpress/Models/Character.php +++ b/theme/wowpress/Models/Character.php @@ -29,6 +29,10 @@ class Character extends Model $this->api = new BattleNet($id,$key); } + public function lists(){ + return $this->belongsToMany(CharacterList::class,'wowpress_charlists_item','list_id','character_id')->withPivot('comment','date','id'); + } + public function user(){ return $this->belongsTo(User::class); } @@ -53,15 +57,25 @@ class Character extends Model public function getRankAttribute($rank){ switch($rank){ case 0: return "Gildenmeister"; + break; case 1: return "Offizier"; + break; case 2: return "Offi-Twink"; + break; case 3: return "Raidlead"; + break; case 4: return "Ehrenmitglied"; + break; case 5: return "Raider"; + break; case 6: return "Raider-Twink"; + break; case 7: return "F&F"; + break; case 8: return "Novize"; + break; case 9: return "Sleeper"; + break; default: return "Gast"; } } diff --git a/theme/wowpress/Models/CharacterList.php b/theme/wowpress/Models/CharacterList.php new file mode 100644 index 0000000..87f22c9 --- /dev/null +++ b/theme/wowpress/Models/CharacterList.php @@ -0,0 +1,35 @@ +belongsToMany(Character::class,'wowpress_charlists_item','character_id','list_id')->orderBy('character_id')->orderByPivot('date')->withPivot('comment','date','ID'); + } + + public function addItem(Character $char, $comment = null, $date = null, $multiplier = 1){ + $this->characters()->attach($char->ID,[ + 'comment' => $comment, + 'date' => $date?:date('Y-m-d'), + 'count_multiplier' => $multiplier + ]); + if($this->notify){ + if($char->user){ + Notification::addNotification($char->user->ID,'Neuer Eintrag in Liste: '.$this->list_name); + } + } + } + + public function removeItem($id){ + $this->characters()->wherePivot('id',$id)->first()->detach(); + } + +} \ No newline at end of file diff --git a/theme/wowpress/Models/CharacterListItem.php b/theme/wowpress/Models/CharacterListItem.php new file mode 100644 index 0000000..6670327 --- /dev/null +++ b/theme/wowpress/Models/CharacterListItem.php @@ -0,0 +1,35 @@ +belongsTo(CharacterList::class,'list_id'); + } + + public function character(){ + $this->belongsTo(Character::class,'character_id'); + } + + public static function deleteOrphans(){ + $items = static::all(); + foreach($items as $item){ + try{ + $item->list; + }catch(LogicException $e){ + $item->delete(); + } + } + } + +} \ No newline at end of file diff --git a/theme/wowpress/Models/Notification.php b/theme/wowpress/Models/Notification.php index a7ec384..124011d 100644 --- a/theme/wowpress/Models/Notification.php +++ b/theme/wowpress/Models/Notification.php @@ -19,6 +19,15 @@ class Notification extends Model{ $note->save(); } + public static function addCapabilityNotification($capability,$note_text){ + $users = get_users(array( + 'capability' => $capability, + )); + foreach($users as $user){ + Notification::addNotification($user->ID, $note_text); + } + } + public static function getAll(){ return Notification::where('user_id',get_current_user_id())->get(); } diff --git a/theme/wowpress/Models/Raid.php b/theme/wowpress/Models/Raid.php index 358f6a5..3b3e1df 100644 --- a/theme/wowpress/Models/Raid.php +++ b/theme/wowpress/Models/Raid.php @@ -81,9 +81,9 @@ class Raid extends Model if (!empty($this->id_wowaudit)) { $raid = (object)$this->api->getRaid($this->id_wowaudit, $force); if (!empty($raid->id)) { - if($this->title && $raid->instance == "Custom"){ + if ($this->title && $raid->instance == "Custom") { //Titel nur sezten wenn kein Custom Raid - }else{ + } else { $this->title = $raid->instance; } $this->start = $raid->date . " " . $raid->start_time; @@ -103,7 +103,7 @@ class Raid extends Model } $encounters[$key]['selections'] = collect($encounter['selections']); } - + $this->partial = $partial; foreach ($raid->signups as $signup) { @@ -147,10 +147,10 @@ class Raid extends Model $s->role = $signup['status'] == "Unknown" ? "Unknown" : $signup['role']; $signups[] = $s; } - + $this->signups = collect($signups); - + foreach ($encounters as &$encounter) { foreach ($encounter['selections'] as $key => &$selection) { $character = Character::where('id_wowaudit', $selection['character_id'])->first(); @@ -166,9 +166,9 @@ class Raid extends Model $encounter['selections'][$key] = $s; } } - - - + + + array_unshift($encounters, [ 'name' => "Alle Bosse", @@ -183,16 +183,17 @@ class Raid extends Model } $this->encounters = collect($encounters); - }else{ + } else { return false; } } - + return $this; } - public function setTitle($title){ + public function setTitle($title) + { $this->title = $title; $this->save(); } @@ -222,27 +223,28 @@ class Raid extends Model public function getPreviousAttribute() { - $previous = Raid::where('start','<',$this->start)->orderBy('start','DESC')->first(); + $previous = Raid::where('start', '<', $this->start)->orderBy('start', 'DESC')->first(); return $previous; } - public function getThumbnailAttribute(){ + public function getThumbnailAttribute() + { - $clean_name = str_replace([" ","'"],"-",$this->title); + $clean_name = str_replace([" ", "'"], "-", $this->title); $clean_name = preg_replace('/[^a-zA-Z0-9_-]/', '', $clean_name); $clean_name = strtolower($clean_name); $url = "https://data.wowaudit.com/img/$clean_name-small.jpeg"; - if(file_exists($url)){ - return $url; + list($status) = get_headers($url); + if (strpos($status, '404') !== FALSE) { + return "https://data.wowaudit.com/img/custom-small.jpeg"; } - return "https://data.wowaudit.com/img/custom-small.jpeg"; - + return $url; } public function getNextAttribute() { - $next = Raid::where('start','>',$this->start)->orderBy('start','ASC')->first(); + $next = Raid::where('start', '>', $this->start)->orderBy('start', 'ASC')->first(); return $next; } @@ -254,7 +256,7 @@ class Raid extends Model } else { $chars = [$character]; } - if(!empty($this->id_wowaudit)) $this->sync(); + if (!empty($this->id_wowaudit)) $this->sync(); if (!empty($character->ID) && !empty($this->signups)) { foreach ($this->signups as $signup) { foreach ($chars as $character) { @@ -268,7 +270,6 @@ class Raid extends Model $s = new Signup(); $s->raid_id = $this->ID; $s->character_id = $character->ID; - return $s->showForm($showRole,$showStatus,$vertical); - + return $s->showForm($showRole, $showStatus, $vertical); } }