From 36bd3eb846e829b45313e10f7568dc4e95841143 Mon Sep 17 00:00:00 2001 From: slawkens <slawkens@gmail.com> Date: Tue, 12 Nov 2024 22:01:00 +0100 Subject: [PATCH] New hooks for news management Can be used for example as discord hooks --- system/src/News.php | 107 ++++++++++++++++++++++++++++-------------- system/src/global.php | 7 +++ 2 files changed, 80 insertions(+), 34 deletions(-) diff --git a/system/src/News.php b/system/src/News.php index 83eedb38a..5ef7a0865 100644 --- a/system/src/News.php +++ b/system/src/News.php @@ -37,26 +37,27 @@ static public function add($title, $body, $type, $category, $player_id, $comment if(!self::verify($title, $body, $article_text, $article_image, $errors)) return false; - ModelsNews::create([ - 'title' => $title, - 'body' => $body, - 'type' => $type, - 'date' => time(), - 'category' => $category, - 'player_id' => isset($player_id) ? $player_id : 0, + $currentTime = time(); + + $params = [ + 'title' => $title, 'body' => $body, + 'type' => $type, 'category' => $category, + 'date' => $currentTime, + 'player_id' => $player_id ?? 0, 'comments' => $comments, 'article_text' => ($type == 3 ? $article_text : ''), 'article_image' => ($type == 3 ? $article_image : '') - ]); + ]; global $hooks; + if (!$hooks->trigger(HOOK_ADMIN_NEWS_ADD_PRE, $params)) { + return false; + } + + $newsModel = ModelsNews::create($params); + $hooks->trigger(HOOK_ADMIN_NEWS_ADD, - [ - 'title' => $title, 'body' => $body, - 'type' => $type, 'category' => $category, - 'player_id' => $player_id, 'comments' => $comments, - 'article_text' => $article_text, 'article_image' => $article_image, - ] + $params + ['id' => $newsModel->id], ); self::clearCache(); @@ -69,30 +70,55 @@ static public function get($id) { static public function update($id, $title, $body, $type, $category, $player_id, $comments, $article_text, $article_image, &$errors) { - if(!self::verify($title, $body, $article_text, $article_image, $errors)) + if(!self::verify($title, $body, $article_text, $article_image, $errors)) { return false; + } + + $currentTime = time(); - ModelsNews::where('id', $id)->update([ - 'title' => $title, - 'body' => $body, - 'type' => $type, - 'category' => $category, - 'last_modified_by' => isset($player_id) ? $player_id : 0, - 'last_modified_date' => time(), + $params = [ + 'id' => $id, + 'title' => $title, 'body' => $body, + 'type' => $type, 'category' => $category, + 'last_modified_by' => $player_id ?? 0, 'last_modified_date' => $currentTime, 'comments' => $comments, - 'article_text' => $article_text, - 'article_image' => $article_image - ]); + 'article_text' => ($type == 3 ? $article_text : ''), + 'article_image' => ($type == 3 ? $article_image : ''), + ]; + + global $hooks; + if (!$hooks->trigger(HOOK_ADMIN_NEWS_UPDATE_PRE, $params)) { + return false; + } + + unset($params['id']); + + ModelsNews::where('id', $id)->update($params); + + $hooks->trigger(HOOK_ADMIN_NEWS_UPDATE, + $params + ['id' => $id] + ); + self::clearCache(); return true; } static public function delete($id, &$errors) { + global $hooks; + if(isset($id)) { $row = ModelsNews::find($id); if($row) { - if (!$row->delete()) { + $params = ['id' => $id]; + + if (!$hooks->trigger(HOOK_ADMIN_NEWS_DELETE_PRE, $params)) { + return false; + } + + if ($row->delete()) { + $hooks->trigger(HOOK_ADMIN_NEWS_DELETE, $params); + } else { $errors[] = 'Fail during delete News.'; } } @@ -114,22 +140,35 @@ static public function delete($id, &$errors) static public function toggleHide($id, &$errors, &$status) { - if(isset($id)) - { + global $hooks; + + if(isset($id)) { $row = ModelsNews::find($id); - if($row) - { - $row->hide = $row->hide == 1 ? 0 : 1; - if (!$row->save()) { + if($row) { + $row->hide = ($row->hide == 1 ? 0 : 1); + + $params = ['hide' => $row->hide]; + + if (!$hooks->trigger(HOOK_ADMIN_NEWS_TOGGLE_HIDE_PRE, $params)) { + return false; + } + + if ($row->save()) { + $hooks->trigger(HOOK_ADMIN_NEWS_TOGGLE_HIDE, $params); + } + else { $errors[] = 'Fail during toggle hide News.'; } + $status = $row->hide; } - else + else { $errors[] = 'News with id ' . $id . ' does not exists.'; + } } - else + else { $errors[] = 'News id not set.'; + } if(count($errors)) { return false; diff --git a/system/src/global.php b/system/src/global.php index 8ab06c812..d5c54fd89 100644 --- a/system/src/global.php +++ b/system/src/global.php @@ -71,7 +71,14 @@ define('HOOK_ADMIN_BODY_END', ++$i); define('HOOK_ADMIN_BEFORE_PAGE', ++$i); define('HOOK_ADMIN_MENU', ++$i); +define('HOOK_ADMIN_NEWS_ADD_PRE', ++$i); define('HOOK_ADMIN_NEWS_ADD', ++$i); +define('HOOK_ADMIN_NEWS_UPDATE_PRE', ++$i); +define('HOOK_ADMIN_NEWS_UPDATE', ++$i); +define('HOOK_ADMIN_NEWS_DELETE_PRE', ++$i); +define('HOOK_ADMIN_NEWS_DELETE', ++$i); +define('HOOK_ADMIN_NEWS_TOGGLE_HIDE_PRE', ++$i); +define('HOOK_ADMIN_NEWS_TOGGLE_HIDE', ++$i); define('HOOK_ADMIN_LOGIN_AFTER_ACCOUNT', ++$i); define('HOOK_ADMIN_LOGIN_AFTER_PASSWORD', ++$i); define('HOOK_ADMIN_LOGIN_AFTER_SIGN_IN', ++$i);