diff --git a/system/functions.php b/system/functions.php index 350341e9..ea958b50 100644 --- a/system/functions.php +++ b/system/functions.php @@ -1079,7 +1079,9 @@ function configLua($key) { function clearCache() { - global $template_name; + require_once LIBS . 'news.php'; + News::clearCache(); + $cache = Cache::getInstance(); if($cache->enabled()) { @@ -1112,12 +1114,7 @@ function clearCache() if ($cache->fetch('failed_logins', $tmp)) $cache->delete('failed_logins'); - if ($cache->fetch('news' . $template_name . '_' . NEWS, $tmp)) - $cache->delete('news' . $template_name . '_' . NEWS); - - if ($cache->fetch('news' . $template_name . '_' . TICKER, $tmp)) - $cache->delete('news' . $template_name . '_' . TICKER); - + global $template_name; if ($cache->fetch('template_ini' . $template_name, $tmp)) $cache->delete('template_ini' . $template_name); } diff --git a/system/libs/news.php b/system/libs/news.php new file mode 100644 index 00000000..383609b2 --- /dev/null +++ b/system/libs/news.php @@ -0,0 +1,141 @@ + TITLE_LIMIT) { + $errors[] = 'News title cannot be longer than ' . TITLE_LIMIT . ' characters.'; + return false; + } + if(strlen($body) > BODY_LIMIT) { + $errors[] = 'News content cannot be longer than ' . BODY_LIMIT . ' characters.'; + return false; + } + if(strlen($article_text) > ARTICLE_TEXT_LIMIT) { + $errors[] = 'Article text cannot be longer than ' . ARTICLE_TEXT_LIMIT . ' characters.'; + return false; + } + if(strlen($article_image) > ARTICLE_IMAGE_LIMIT) { + $errors[] = 'Article image cannot be longer than ' . ARTICLE_IMAGE_LIMIT . ' characters.'; + return false; + } + return true; + } + + static public function add($title, $body, $type, $category, $player_id, $comments, $article_text, $article_image, &$errors) + { + global $db; + if(!self::verify($title, $body, $article_text, $article_image, $errors)) + return false; + + $db->insert(TABLE_PREFIX . 'news', array('title' => $title, 'body' => $body, 'type' => $type, 'date' => time(), 'category' => $category, 'player_id' => isset($player_id) ? $player_id : 0, 'comments' => $comments, 'article_text' => ($type == 3 ? $article_text : ''), 'article_image' => ($type == 3 ? $article_image : ''))); + self::clearCache(); + return true; + } + + static public function get($id) { + global $db; + return $db->select(TABLE_PREFIX . 'news', array('id' => $id)); + } + + static public function update($id, $title, $body, $type, $category, $player_id, $comments, $article_text, $article_image, &$errors) + { + global $db; + if(!self::verify($title, $body, $article_text, $article_image, $errors)) + return false; + + $db->update(TABLE_PREFIX . 'news', array('title' => $title, 'body' => $body, 'type' => $type, 'category' => $category, 'last_modified_by' => isset($player_id) ? $player_id : 0, 'last_modified_date' => time(), 'comments' => $comments, 'article_text' => $article_text, 'article_image' => $article_image), array('id' => $id)); + self::clearCache(); + return true; + } + + static public function delete($id, &$errors) + { + global $db; + if(isset($id)) + { + if($db->select(TABLE_PREFIX . 'news', array('id' => $id)) !== false) + $db->delete(TABLE_PREFIX . 'news', array('id' => $id)); + else + $errors[] = 'News with id ' . $id . ' does not exists.'; + } + else + $errors[] = 'News id not set.'; + + if(count($errors)) { + return false; + } + + self::clearCache(); + return true; + } + + static public function toggleHidden($id, &$errors, &$status) + { + global $db; + if(isset($id)) + { + $query = $db->select(TABLE_PREFIX . 'news', array('id' => $id)); + if($query !== false) + { + $db->update(TABLE_PREFIX . 'news', array('hidden' => ($query['hidden'] == 1 ? 0 : 1)), array('id' => $id)); + $status = $query['hidden']; + } + else + $errors[] = 'News with id ' . $id . ' does not exists.'; + } + else + $errors[] = 'News id not set.'; + + if(count($errors)) { + return false; + } + + self::clearCache(); + return true; + } + + static public function getCached($type) + { + global $template_name; + + $cache = Cache::getInstance(); + if ($cache->enabled()) + { + $tmp = ''; + if ($cache->fetch('news_' . $template_name . '_' . $type, $tmp) && isset($tmp[0])) { + return $tmp; + } + } + + return false; + } + + static public function clearCache() + { + $cache = Cache::getInstance(); + if (!$cache->enabled()) { + return; + } + + $tmp = ''; + foreach (get_templates() as $template) { + if ($cache->fetch('news' . $template . '_' . NEWS, $tmp)) { + $cache->delete('news' . $template . '_' . NEWS); + } + + if ($cache->fetch('news' . $template . '_' . TICKER, $tmp)) { + $cache->delete('news' . $template . '_' . TICKER); + } + + if ($cache->fetch('news' . $template . '_' . ARTICLE, $tmp)) { + $cache->delete('news' . $template . '_' . ARTICLE); + } + } + } +} \ No newline at end of file diff --git a/system/pages/admin/news.php b/system/pages/admin/news.php index d36067e3..8838780a 100644 --- a/system/pages/admin/news.php +++ b/system/pages/admin/news.php @@ -8,7 +8,9 @@ * @link https://my-aac.org */ defined('MYAAC') or die('Direct access not allowed!'); + require_once LIBS . 'forum.php'; +require_once LIBS . 'news.php'; $title = 'News Panel'; @@ -150,93 +152,3 @@ foreach ($query as $_news) { $twig->display('admin.news.html.twig', array( 'newses' => $newses )); - -class News -{ - static public function verify($title, $body, $article_text, $article_image, &$errors) - { - if(!isset($title[0]) || !isset($body[0])) { - $errors[] = 'Please fill all inputs.'; - return false; - } - if(strlen($title) > TITLE_LIMIT) { - $errors[] = 'News title cannot be longer than ' . TITLE_LIMIT . ' characters.'; - return false; - } - if(strlen($body) > BODY_LIMIT) { - $errors[] = 'News content cannot be longer than ' . BODY_LIMIT . ' characters.'; - return false; - } - if(strlen($article_text) > ARTICLE_TEXT_LIMIT) { - $errors[] = 'Article text cannot be longer than ' . ARTICLE_TEXT_LIMIT . ' characters.'; - return false; - } - if(strlen($article_image) > ARTICLE_IMAGE_LIMIT) { - $errors[] = 'Article image cannot be longer than ' . ARTICLE_IMAGE_LIMIT . ' characters.'; - return false; - } - return true; - } - - static public function add($title, $body, $type, $category, $player_id, $comments, $article_text, $article_image, &$errors) - { - global $db; - if(!self::verify($title, $body, $article_text, $article_image, $errors)) - return false; - - $db->insert(TABLE_PREFIX . 'news', array('title' => $title, 'body' => $body, 'type' => $type, 'date' => time(), 'category' => $category, 'player_id' => isset($player_id) ? $player_id : 0, 'comments' => $comments, 'article_text' => ($type == 3 ? $article_text : ''), 'article_image' => ($type == 3 ? $article_image : ''))); - return true; - } - - static public function get($id) { - global $db; - return $db->select(TABLE_PREFIX . 'news', array('id' => $id)); - } - - static public function update($id, $title, $body, $type, $category, $player_id, $comments, $article_text, $article_image, &$errors) - { - global $db; - if(!self::verify($title, $body, $article_text, $article_image, $errors)) - return false; - - $db->update(TABLE_PREFIX . 'news', array('title' => $title, 'body' => $body, 'type' => $type, 'category' => $category, 'last_modified_by' => isset($player_id) ? $player_id : 0, 'last_modified_date' => time(), 'comments' => $comments, 'article_text' => $article_text, 'article_image' => $article_image), array('id' => $id)); - return true; - } - - static public function delete($id, &$errors) - { - global $db; - if(isset($id)) - { - if($db->select(TABLE_PREFIX . 'news', array('id' => $id)) !== false) - $db->delete(TABLE_PREFIX . 'news', array('id' => $id)); - else - $errors[] = 'News with id ' . $id . ' does not exists.'; - } - else - $errors[] = 'News id not set.'; - - return !count($errors); - } - - static public function toggleHidden($id, &$errors, &$status) - { - global $db; - if(isset($id)) - { - $query = $db->select(TABLE_PREFIX . 'news', array('id' => $id)); - if($query !== false) - { - $db->update(TABLE_PREFIX . 'news', array('hidden' => ($query['hidden'] == 1 ? 0 : 1)), array('id' => $id)); - $status = $query['hidden']; - } - else - $errors[] = 'News with id ' . $id . ' does not exists.'; - } - else - $errors[] = 'News id not set.'; - - return !count($errors); - } -} -?> \ No newline at end of file diff --git a/system/pages/news.php b/system/pages/news.php index 193eb143..7f9f7552 100644 --- a/system/pages/news.php +++ b/system/pages/news.php @@ -11,6 +11,7 @@ defined('MYAAC') or die('Direct access not allowed!'); require_once LIBS . 'forum.php'; +require_once LIBS . 'news.php'; if(isset($_GET['archive'])) { @@ -131,7 +132,7 @@ if(!$news_cached) } if($cache->enabled() && !$canEdit) - $cache->set('news_' . $template_name . '_' . TICKER, $tickers_content, 120); + $cache->set('news_' . $template_name . '_' . TICKER, $tickers_content, 60 * 60); $featured_article_db =$db->query('SELECT `id`, `title`, `article_text`, `article_image`, `hidden` FROM `' . TABLE_PREFIX . 'news` WHERE `type` = ' . ARTICLE . ($canEdit ? '' : ' AND `hidden` != 1') .' ORDER BY `date` DESC LIMIT 1'); $article = ''; @@ -154,7 +155,7 @@ if(!$news_cached) } if($cache->enabled() && !$canEdit) - $cache->set('news_' . $template_name . '_' . ARTICLE, $featured_article, 120); + $cache->set('news_' . $template_name . '_' . ARTICLE, $featured_article, 60 * 60); } } else { @@ -221,29 +222,9 @@ if(!$news_cached) ob_end_clean(); if($cache->enabled() && !$canEdit) - $cache->set('news_' . $template_name . '_' . NEWS, $tmp_content, 120); + $cache->set('news_' . $template_name . '_' . NEWS, $tmp_content, 60 * 60); echo $tmp_content; } else - echo $news_cached; - -class News -{ - static public function getCached($type) - { - global $template_name; - - $cache = Cache::getInstance(); - if($cache->enabled()) - { - $tmp = ''; - if($cache->fetch('news_' . $template_name . '_' . $type, $tmp) && isset($tmp[0])) { - return $tmp; - } - } - - return false; - } -} -?> + echo $news_cached; \ No newline at end of file