665 lines
		
	
	
		
			30 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			665 lines
		
	
	
		
			30 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
|     defined('BASEPATH') OR exit('No direct script access allowed');
 | |
| 
 | |
|     require "vendor/autoload.php";
 | |
| 
 | |
|     use PHPHtmlParser\Dom;
 | |
|     use PHPHtmlParser\Dom\HtmlNode;
 | |
|     use PHPHtmlParser\Dom\TextNode;
 | |
| 
 | |
|     class BlogModel extends CI_Model
 | |
|     {
 | |
| 
 | |
|         function __construct()
 | |
|         {
 | |
|             parent::__construct();
 | |
|             $this->load->model('UserModel', '', TRUE);
 | |
|         }
 | |
| 
 | |
|         function getAllPosts($search, $amount, $offset = 0)
 | |
|         {
 | |
|             $offset *= $amount;
 | |
|             if ($search !== '') {
 | |
|                 $posts = $this->db->query('
 | |
| SELECT
 | |
|   p.*,
 | |
|   ca.name                    categoryName,
 | |
|   ca.display_name            categoryDisplayName,
 | |
|   (SELECT count(*)
 | |
|    FROM blog_comments
 | |
|    WHERE post_id = p.postID) commentCount,
 | |
|   (SELECT count(*)
 | |
|    FROM blog_post_likes
 | |
|    WHERE post_id = p.postID) likeCount,
 | |
|    (SELECT wordCount FROM blog_content c WHERE c.postID = p.postID AND isActive = TRUE AND c.language = ? ORDER BY c.contentDate DESC LIMIT 1) wordCount
 | |
| FROM blog_posts p, blog_categories ca
 | |
| WHERE ca.id = p.postCategoryID
 | |
|       AND (CONCAT((SELECT t.postTitle FROM blog_translations t WHERE t.postID = p.postID AND language = ?), postUrl, (SELECT t.postDesc FROM blog_translations t WHERE t.postID = p.postID AND language = ?)) RLIKE ?
 | |
|            OR (SELECT content
 | |
|                FROM blog_content
 | |
|                WHERE blog_content.postID = p.postID AND isActive = TRUE
 | |
|                LIMIT 1) RLIKE ?)
 | |
|       AND postState = 1
 | |
| ORDER BY postPublishDate DESC
 | |
| LIMIT ? OFFSET ?', [$_SESSION['site_lang'], $_SESSION['site_lang'], $_SESSION['site_lang'], $search, $search, $amount, $offset])->result_array();
 | |
|             } else {
 | |
|                 $posts = $this->db->query('
 | |
|             SELECT p.*,
 | |
|               ca.name categoryName,
 | |
|               ca.display_name categoryDisplayName,
 | |
|               (SELECT count(*) FROM blog_comments WHERE post_id = p.postID) commentCount,
 | |
|               (SELECT count(*) FROM blog_post_likes WHERE post_id = p.postID) likeCount,
 | |
|               (SELECT wordCount FROM blog_content c WHERE c.postID = p.postID AND isActive = TRUE AND c.language = ? ORDER BY c.contentDate DESC LIMIT 1) wordCount
 | |
|             FROM blog_posts p, blog_categories ca
 | |
|             WHERE ca.id = p.postCategoryID
 | |
|             AND postState = 1 
 | |
|             ORDER BY postPublishDate DESC
 | |
|             LIMIT ? OFFSET ?', [$_SESSION['site_lang'], $amount, $offset])->result_array();
 | |
|             }
 | |
|             $posts = $this->mergePostTitleDesc($posts);
 | |
|             $posts = $this->mergePostAuthorData($posts);
 | |
|             return $posts;
 | |
|         }
 | |
| 
 | |
|         public function getPostPageCount($search, $postsPerPage)
 | |
|         {
 | |
|             if ($search !== '') {
 | |
|                 $data = $this->db->query('
 | |
| SELECT COUNT(*) pageCount
 | |
| FROM blog_posts p, blog_categories ca
 | |
| WHERE ca.id = p.postCategoryID
 | |
| AND (CONCAT((SELECT t.postTitle FROM blog_translations t WHERE t.postID = p.postID AND language = ?), postUrl, (SELECT t.postDesc FROM blog_translations t WHERE t.postID = p.postID AND language = ?)) RLIKE ? 
 | |
| OR (SELECT content FROM blog_content WHERE blog_content.postID = p.postID AND isActive = TRUE LIMIT 1) RLIKE ?)
 | |
| AND postState = 1', [$_SESSION['site_lang'], $_SESSION['site_lang'], $search, $search])->result_array();
 | |
|             } else {
 | |
|                 $data = $this->db->query('SELECT COUNT(*) pageCount FROM blog_posts')->result_array();
 | |
|             }
 | |
|             return ($data[0]['pageCount']) / $postsPerPage;
 | |
|         }
 | |
| 
 | |
|         function getCategoryPosts($category, $amount, $offset = 0)
 | |
|         {
 | |
|             $offset *= $amount;
 | |
|             $posts = $this->db->query('
 | |
|         SELECT p.*, ca.name categoryName, ca.display_name categoryDisplayName, (SELECT count(*) FROM blog_comments WHERE post_id = p.postID) commentCount, (SELECT count(*) FROM blog_post_likes WHERE post_id = p.postID) likeCount
 | |
|         FROM blog_posts p, blog_categories ca
 | |
|         WHERE ca.id = p.postCategoryID
 | |
|         AND postState = 1 
 | |
|         AND ca.name = ? 
 | |
|         ORDER BY postPublishDate DESC
 | |
|         LIMIT ? OFFSET ?', [$category, $amount, $offset])->result_array();
 | |
|             $posts = $this->mergePostTitleDesc($posts);
 | |
|             $posts = $this->mergePostAuthorData($posts);
 | |
|             return $posts;
 | |
|         }
 | |
| 
 | |
|         public function getCategoryPostsByID($categoryID, $amount = 3, $postID = NULL)
 | |
|         {
 | |
|             $posts = $this->db->query('SELECT * FROM blog_posts WHERE postCategoryID = ? AND postID != ? AND blog_posts.postState = 1 ORDER BY postPublishDate DESC LIMIT ?', [$categoryID, $postID, $amount])->result_array();
 | |
|             return $this->mergePostTitleDesc($posts);
 | |
|         }
 | |
| 
 | |
|         public function getTagPosts($tag, $amount, $offset = 0) {
 | |
|             $offset *= $amount;
 | |
|             $posts = $this->db->query('SELECT p.*, ca.name categoryName, ca.display_name categoryDisplayName, (SELECT count(*) FROM blog_comments WHERE post_id = p.postID) commentCount, (SELECT count(*) FROM blog_post_likes WHERE post_id = p.postID) likeCount FROM blog_posts p LEFT JOIN blog_categories ca ON ca.ID = p.postCategoryID WHERE postState = 1 AND EXISTS(SELECT post_id FROM blog_post_tags WHERE tag_id = (SELECT ID FROM blog_tags t WHERE t.name = ?) AND post_id = p.postID) ORDER BY postPublishDate DESC LIMIT ? OFFSET ?', [$tag, $amount, $offset])->result_array();
 | |
|             $posts = $this->mergePostTitleDesc($posts);
 | |
|             $posts = $this->mergePostAuthorData($posts);
 | |
|             return $posts;
 | |
|         }
 | |
| 
 | |
|         function getCategoryIDAfterInsert($name, $display_name)
 | |
|         {
 | |
|             if (!$this->db->simple_query('INSERT INTO blog_categories (name, display_name) VALUES (?, ?)', [$name, $display_name])) {
 | |
|                 $return = $this->db->query('SELECT ID FROM blog_categories WHERE name = ? AND display_name = ? LIMIT 1', [$name, $display_name])->result_array()[0];
 | |
|                 $return = $return['ID'];
 | |
|             } else {
 | |
|                 $return = $this->db->query('SELECT LAST_INSERT_ID() ID')->result_array()[0]['ID'];
 | |
|             }
 | |
|             return $return;
 | |
|         }
 | |
| 
 | |
|         function incrementViews($id)
 | |
|         {
 | |
|             $this->db->query('UPDATE blog_posts SET postViews = postViews+1 WHERE postID = ?', [$id]);
 | |
|             $this->db->cache_delete('admin', 'blog');
 | |
|         }
 | |
| 
 | |
|         public function getCategories()
 | |
|         {
 | |
|             $this->db->cache_off();
 | |
|             $categories = $this->db->query('SELECT * FROM blog_categories')->result_array();
 | |
|             $this->db->cache_on();
 | |
|             return $categories;
 | |
|         }
 | |
| 
 | |
|         public function getPost($postUrl)
 | |
|         {
 | |
|             $content = $this->db->query('
 | |
|         SELECT p.*, 
 | |
|             ca.name categoryName, 
 | |
|             ca.display_name categoryDisplayName, 
 | |
|             COUNT(co.comment) commentCount, 
 | |
|             (SELECT count(*) FROM blog_post_likes l WHERE l.post_id = p.postID) likeCount
 | |
|         FROM blog_posts p
 | |
|             LEFT JOIN blog_categories ca ON p.postCategoryID = ca.ID 
 | |
|             LEFT JOIN blog_comments co ON p.postID = co.post_id 
 | |
|             LEFT JOIN blog_post_likes l ON p.postID = l.post_id 
 | |
|         WHERE postUrl = ? AND postState = 1
 | |
|         GROUP BY p.postID', [$postUrl])->result_array(); //TODO: language integration
 | |
| 
 | |
|             $content = $this->mergePostTitleDesc($content);
 | |
|             $content = $this->mergePostAuthorData($content);
 | |
|             $content = $this->mergePostContent($content, $_SESSION['site_lang']);
 | |
|             return $content[0];
 | |
|         }
 | |
| 
 | |
|         public function getPostDataByID($postID)
 | |
|         {
 | |
|             $data = $this->db->query('
 | |
|         SELECT p.*, 
 | |
|             ca.name AS categoryName,
 | |
|             ca.display_name AS categoryDisplayName, 
 | |
|             COUNT(co.comment) AS commentCount, 
 | |
|             COUNT(l.post_id) AS likeCount 
 | |
|         FROM blog_posts p
 | |
|             LEFT JOIN blog_categories ca ON p.postCategoryID = ca.ID 
 | |
|             LEFT JOIN blog_comments co ON p.postID = co.post_id 
 | |
|             LEFT JOIN blog_post_likes l ON p.postID = l.post_id 
 | |
|         WHERE postID = ?
 | |
|         GROUP BY p.postID', [$postID])->result_array();
 | |
| 
 | |
|             return $data;
 | |
|         }
 | |
| 
 | |
|         public function getContentDataByID($postID, $contentID, $lang)
 | |
|         {
 | |
|             $data = $this->db->query('SELECT * FROM blog_content WHERE postID = ? AND contentID = ? AND language = ?', [$postID, $contentID, $lang])->result_array();
 | |
|             return $data;
 | |
|         }
 | |
| 
 | |
|         public function getTranslationDataByID($postID, $translationID, $lang)
 | |
|         {
 | |
|             $data = $this->db->query('SELECT * FROM blog_translations WHERE postID = ? AND postTranslationID = ? AND language = ?', [$postID, $translationID, $lang])->result_array();
 | |
|             return $data;
 | |
|         }
 | |
| 
 | |
|         private function getPostContentByID($postID, $language)
 | |
|         {
 | |
|             $content = $this->db->query('SELECT * FROM blog_content WHERE postID = ? AND isActive = TRUE AND language = ? ORDER BY contentDate DESC LIMIT 1', [$postID, $language])->result_array();
 | |
|             return $content;
 | |
|         }
 | |
| 
 | |
|         public function getRandomPosts($postID)
 | |
|         {
 | |
|             $posts = $this->db->query('SELECT * FROM blog_posts WHERE postID <> ? AND postState = 1 ORDER BY RAND() LIMIT 3', [$postID])->result_array();
 | |
|             $posts = $this->mergePostTitleDesc($posts);
 | |
|             $posts = $this->mergePostAuthorData($posts);
 | |
|             return $posts;
 | |
|         }
 | |
| 
 | |
|         public function mergePostTitleDesc($posts, $language = NULL)
 | |
|         {
 | |
|             $language = ($language == NULL ? $_SESSION['site_lang'] : $language);
 | |
|             foreach ($posts as $i => $post) {
 | |
|                 $titleDesc = $this->getPostTitleDesc($post['postID'], $language);
 | |
|                 $posts[$i]['postTitle'] = $titleDesc['title'];
 | |
|                 $posts[$i]['postDesc'] = $titleDesc['desc'];
 | |
|             }
 | |
|             return $posts;
 | |
|         }
 | |
| 
 | |
|         private function mergePostAuthorData($posts)
 | |
|         {
 | |
|             foreach ($posts as $i => $post) {
 | |
|                 $authorData = $this->getAuthorData($post['postAuthorID']);
 | |
|                 $posts[$i]['postAuthorDisplayname'] = $authorData['displayname'];
 | |
|                 $posts[$i]['postAuthorUsername'] = $authorData['username'];
 | |
|                 $posts[$i]['postAuthorProfilePicture'] = $authorData['profile_picture'];
 | |
|                 $posts[$i]['postAuthorHeaderImage'] = $authorData['header_image'];
 | |
|                 $posts[$i]['postAuthorRank'] = $authorData['rank'];
 | |
|                 $posts[$i]['postAuthorAbout'] = $authorData['about'];
 | |
|             }
 | |
|             return $posts;
 | |
|         }
 | |
| 
 | |
|         private function mergePostContent($posts, $language)
 | |
|         {
 | |
|             $language = ($language == NULL ? $_SESSION['site_lang'] : $language);
 | |
|             foreach ($posts as $i => $post) {
 | |
|                 $content = $this->getPostContentByID($post['postID'], $language);
 | |
|                 if (empty($content) && $_SESSION['site_lang'] !== 'de') {
 | |
|                     $content = $this->getPostContentByID($post['postID'], 'de');
 | |
|                 }
 | |
|                 if (!empty($content)) {
 | |
|                     $content = $content[0];
 | |
|                     $posts[$i]['postContent'] = $content['content'];
 | |
|                     $posts[$i]['postWordCount'] = $content['wordCount'];
 | |
|                 }
 | |
|             }
 | |
|             return $posts;
 | |
|         }
 | |
| 
 | |
|         public function getPostTitleDesc($postID, $language)
 | |
|         {
 | |
|             $data = $this->db->query('SELECT * FROM blog_translations WHERE postID = ? ORDER BY blog_translations.language ASC', [$postID])->result_array();
 | |
|             if (empty($data)) {
 | |
|                 return ['title' => 'Nicht vorhandener Post', 'desc' => 'Aus unbekannten Gründen ist dieser Blog-Post nicht vorhanden. Bitte versuche es später erneut oder kontaktiere das Support-Team'];
 | |
|             }
 | |
|             $postTitle = $data[0]['postTitle'];
 | |
|             $postDesc = $data[0]['postDesc'];
 | |
|             foreach ($data as $row) {
 | |
|                 if ($row['language'] == $language) {
 | |
|                     $postTitle = $row['postTitle'];
 | |
|                     $postDesc = $row['postDesc'];
 | |
|                     break;
 | |
|                 }
 | |
|             }
 | |
|             return ['title' => $postTitle, 'desc' => $postDesc];
 | |
|         }
 | |
| 
 | |
|         public function getAuthorData($authorID)
 | |
|         {
 | |
|             $author = $this->db->query('SELECT ID, username, displayname, rank, profile_picture, header_image, about FROM users WHERE ID = ?', [$authorID])->result_array();
 | |
| 
 | |
|             $author = $this->UserModel->setDefaultImages($author);
 | |
| 
 | |
|             if(empty($author)) {
 | |
|                 return null;
 | |
|             }
 | |
|             return $author[0];
 | |
|         }
 | |
| 
 | |
|         public function getComments($postID)
 | |
|         {
 | |
|             $comments = $this->db->query('SELECT * FROM blog_comments WHERE post_id = ? ORDER BY date_created DESC', [$postID])->result_array();
 | |
|             return $comments;
 | |
|         }
 | |
| 
 | |
|         public function getCommentsByUrl($postUrl)
 | |
|         {
 | |
|             return $this->db->query('SELECT * FROM blog_comments WHERE post_id = (SELECT postID FROM blog_posts WHERE postUrl = ?) ORDER BY date_created DESC', [$postUrl])->result_array();
 | |
|         }
 | |
| 
 | |
|         public function addComment($postID, $userID, $comment, $reply, $replyTo)
 | |
|         {
 | |
|             $this->db->query('INSERT INTO blog_comments (post_id, user_id, comment, reply, replyTo_id) VALUES (?, ?, ?, ?, ?)', [$postID, $userID, $comment, $reply, $replyTo]);
 | |
| 
 | |
|             $this->db->cache_delete('blog', 'index');
 | |
|             $this->db->cache_delete('blog', 'post');
 | |
|             $this->db->cache_delete('blog', 'comment');
 | |
|             $this->db->cache_delete('blog', 'getComments');
 | |
|             $this->db->cache_delete('admin', 'blog');
 | |
|         }
 | |
| 
 | |
|         public function addCommentByUrl($postUrl, $userID, $comment, $reply, $replyTo)
 | |
|         {
 | |
|             $postID = $this->db->query('SELECT postID FROM blog_posts WHERE postUrl = ?', [$postUrl])->result_array();
 | |
|             if(empty($postID)) {
 | |
|                 return null;
 | |
|             }
 | |
| 
 | |
|             $this->addComment($postID[0]['postID'], $userID, $comment, $reply, $replyTo);
 | |
| 
 | |
|             return $this->db->query('SELECT * FROM blog_comments WHERE post_id = ? AND user_id = ? ORDER BY ID DESC LIMIT 1', [$postID[0]['postID'], $userID])->result_array()[0];
 | |
|         }
 | |
| 
 | |
|         public function getAllTags()
 | |
|         {
 | |
|             return $this->db->query('SELECT * FROM blog_tags')->result_array();
 | |
|         }
 | |
| 
 | |
|         public function getTags($postID)
 | |
|         {
 | |
|             $tags = $this->db->query('SELECT t.* FROM blog_tags t WHERE ID IN (SELECT tag_id FROM blog_post_tags WHERE post_id = ?)', [$postID])->result_array();
 | |
|             return $tags;
 | |
|         }
 | |
| 
 | |
|         public function mergeTagInfo($tags) {
 | |
|             foreach ($tags as $i => $tag) {
 | |
|                 $data = $this->db->query('SELECT count(*) countUsed, SUM(postViews) totalViews FROM blog_posts WHERE postID = (SELECT post_id FROM blog_post_tags WHERE tag_id = ? AND post_id = postID)', [$tag['ID']])->result_array();
 | |
|                 $data = $data[0];
 | |
|                 $tags[$i]['countUsed'] = $data['countUsed'];
 | |
|                 $tags[$i]['totalViews'] = $data['totalViews'] != '' ? $data['totalViews'] : 0;
 | |
|             }
 | |
|             return $tags;
 | |
|         }
 | |
| 
 | |
|         public function hasAlreadyLiked($postID, $userID)
 | |
|         {
 | |
|             $getLikes = $this->db->query('SELECT * FROM blog_post_likes WHERE post_id = ? AND user_id = ?', [$postID, $userID])->result_array();
 | |
|             if (empty($getLikes)) {
 | |
|                 return false;
 | |
|             } else {
 | |
|                 return true;
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         public function addLike($postID, $userID)
 | |
|         {
 | |
|             $this->db->query('INSERT INTO blog_post_likes (post_id, user_id) VALUES (?, ?)', [$postID, $userID]);
 | |
| 
 | |
|             $this->db->cache_delete('blog', 'index');
 | |
|             $this->db->cache_delete('blog', 'post');
 | |
|             $this->db->cache_delete('blog', 'like');
 | |
|             $this->db->cache_delete('admin', 'blog');
 | |
| 
 | |
|             return $this->db->query('SELECT count(*) likeCount FROM blog_post_likes WHERE post_id = ?', [$postID])->result_array()[0];
 | |
|         }
 | |
| 
 | |
|         public function removeLike($postID, $userID)
 | |
|         {
 | |
|             $this->db->query('DELETE FROM blog_post_likes WHERE post_id = ? AND user_id = ?', [$postID, $userID]);
 | |
| 
 | |
|             $this->db->cache_delete('blog', 'index');
 | |
|             $this->db->cache_delete('blog', 'post');
 | |
|             $this->db->cache_delete('blog', 'like');
 | |
|             $this->db->cache_delete('admin', 'blog');
 | |
| 
 | |
|             return $this->db->query('SELECT count(*) likeCount FROM blog_post_likes WHERE post_id = ?', [$postID])->result_array()[0];
 | |
|         }
 | |
| 
 | |
|         public function getPostList($onlyTrash)
 | |
|         {
 | |
|             $posts = $this->db->query('SELECT p.*,
 | |
|           ca.name                    categoryName,
 | |
|           ca.display_name            categoryDisplayName,
 | |
|           (SELECT count(*)
 | |
|            FROM blog_comments
 | |
|            WHERE post_id = p.postID) commentCount,
 | |
|           (SELECT count(*)
 | |
|            FROM blog_post_likes
 | |
|            WHERE post_id = p.postID) likeCount,
 | |
|           (SELECT display_name
 | |
|           FROM blog_states
 | |
|           WHERE ID = p.postState) postStateDisplay
 | |
|         FROM blog_posts p
 | |
|         LEFT JOIN blog_categories ca ON ca.ID = p.postCategoryID
 | |
|         WHERE postIsDeleted = ?', [$onlyTrash])->result_array();
 | |
|             $posts = $this->mergePostTitleDesc($posts);
 | |
|             $posts = $this->mergePostAuthorData($posts);
 | |
|             return $posts;
 | |
|         }
 | |
| 
 | |
|         public function getAllContentVersions($postID, $lang)
 | |
|         {
 | |
|             $content = $this->db->query('SELECT * FROM blog_content WHERE postID = ? AND language = ? ORDER BY contentDate DESC', [$postID, $lang])->result_array();
 | |
|             return $content;
 | |
|         }
 | |
| 
 | |
|         public function createTagIfNotExists($tag)
 | |
|         {
 | |
|             $this->db->cache_off();
 | |
|             $tagData = $this->db->query('SELECT ID FROM blog_tags WHERE name = ?', [strtolower($tag)])->result_array();
 | |
|             if (empty($tagData)) {
 | |
|                 $this->db->query('INSERT INTO blog_tags (name, display_name) VALUES (?, ?)', [strtolower($tag), $tag]);
 | |
|                 $tagData = $this->db->query('SELECT ID FROM blog_tags WHERE name = ?', [strtolower($tag)])->result_array();
 | |
|             }
 | |
|             $this->db->cache_on();
 | |
|             $tagID = $tagData[0]['ID'];
 | |
|             return $tagID;
 | |
|         }
 | |
| 
 | |
|         public function addPostTagByID($postID, $tagID)
 | |
|         {
 | |
|             $this->db->query('INSERT INTO blog_post_tags (post_id, tag_id) VALUES (?, ?) ON DUPLICATE KEY UPDATE post_id = post_id', [$postID, $tagID]);
 | |
| 
 | |
|             $this->db->cache_delete('blog', 'index');
 | |
|             $this->db->cache_delete('blog', 'post');
 | |
|             $this->db->cache_delete('blog', 'like');
 | |
|             $this->db->cache_delete('admin', 'blog');
 | |
|         }
 | |
| 
 | |
|         public function deleteAllPostTags($postID)
 | |
|         {
 | |
|             $this->db->query('DELETE FROM blog_post_tags WHERE post_id = ?', [$postID]);
 | |
| 
 | |
|             $this->db->cache_delete('blog', 'index');
 | |
|             $this->db->cache_delete('blog', 'post');
 | |
|             $this->db->cache_delete('blog', 'tag');
 | |
|             $this->db->cache_delete('admin', 'blog');
 | |
|         }
 | |
| 
 | |
|         // TODO: Update
 | |
|         public function prepareContentForRelease($content)
 | |
|         {
 | |
|             $dom = new Dom;
 | |
|             $dom->load($content);
 | |
|             $images = $dom->find('img');
 | |
|             foreach ($images as $image) {
 | |
|                 $image_src = $image->getAttribute('src');
 | |
|                 $image_caption = $image->getAttribute('alt');
 | |
|                 $image->removeAllAttributes();
 | |
|                 $image->setAttribute('class', 'img-fluid');
 | |
|                 $image->setAttribute('src', $image_src);
 | |
|                 $image->setAttribute('alt', $image_caption);
 | |
| 
 | |
|                 $container = new HtmlNode('div');
 | |
|                 $container->setAttribute('class', 'img');
 | |
| 
 | |
|                 $caption_node = new HtmlNode('p');
 | |
|                 $caption = new TextNode($image_caption);
 | |
|                 $caption_node->addChild($caption);
 | |
| 
 | |
|                 $parent = $image->getParent();
 | |
|                 $parent->addChild($container);
 | |
|                 $container->addChild($image);
 | |
|                 $container->addChild($caption_node);
 | |
|             }
 | |
|             $iframes = $dom->find('iframe');
 | |
|             foreach ($iframes as $iframe) {
 | |
|                 $container = new HtmlNode('div');
 | |
|                 $container->setAttribute('class', 'responsive-video');
 | |
|                 $parent = $iframe->getParent();
 | |
|                 $parent->addChild($container);
 | |
|                 $container->addChild($iframe);
 | |
|             }
 | |
|             return $dom;
 | |
|         }
 | |
| 
 | |
|         public function getFirstImage($content)
 | |
|         {
 | |
|             $dom = new Dom;
 | |
|             $dom->load($content);
 | |
|             $images = $dom->find('img');
 | |
|             if ($images[0] == null)
 | |
|                 return '';
 | |
|             $imageSrc = $images[0]->getAttribute('src');
 | |
|             return $imageSrc;
 | |
|         }
 | |
| 
 | |
|         public function deletePost($id)
 | |
|         {
 | |
|             $this->db->query('UPDATE blog_posts SET postIsDeleted = TRUE, postDeletedDate = NOW(), postState = 4 WHERE postID = ? LIMIT 1', [$id]);
 | |
| 
 | |
|             $this->db->cache_delete('blog', 'index');
 | |
|             $this->db->cache_delete('blog', 'post');
 | |
|             $this->db->cache_delete('blog', 'like');
 | |
|             $this->db->cache_delete('blog', 'comment');
 | |
|             $this->db->cache_delete('blog', 'getComments');
 | |
|             $this->db->cache_delete('admin', 'blog');
 | |
|         }
 | |
| 
 | |
|         public function restorePost($id)
 | |
|         {
 | |
|             $this->db->query('UPDATE blog_posts SET postIsDeleted = FALSE, postDeletedDate = NULL, postState = 1 WHERE postID = ? LIMIT 1', [$id]);
 | |
| 
 | |
|             $this->db->cache_delete('blog', 'index');
 | |
|             $this->db->cache_delete('blog', 'post');
 | |
|             $this->db->cache_delete('blog', 'like');
 | |
|             $this->db->cache_delete('blog', 'comment');
 | |
|             $this->db->cache_delete('blog', 'getComments');
 | |
|             $this->db->cache_delete('admin', 'blog');
 | |
|         }
 | |
| 
 | |
|         public function deletePostFinally($id)
 | |
|         {
 | |
|             $this->db->query('DELETE FROM blog_content WHERE postID = ? AND (SELECT postIsDeleted FROM blog_posts WHERE postID = ?) = TRUE', [$id, $id]);
 | |
|             $this->db->query('DELETE FROM blog_post_likes WHERE post_id = ? AND (SELECT postIsDeleted FROM blog_posts WHERE postID = ?) = TRUE', [$id, $id]);
 | |
|             $this->db->query('DELETE FROM blog_post_tags WHERE post_id = ? AND (SELECT postIsDeleted FROM blog_posts WHERE postID = ?) = TRUE', [$id, $id]);
 | |
|             $this->db->query('DELETE FROM blog_comments WHERE post_id = ? AND (SELECT postIsDeleted FROM blog_posts WHERE postID = ?) = TRUE', [$id, $id]);
 | |
|             $this->db->query('DELETE FROM blog_posts WHERE postID = ? AND postIsDeleted = TRUE LIMIT 1', [$id]);
 | |
| 
 | |
|             $this->db->cache_delete('blog', 'index');
 | |
|             $this->db->cache_delete('blog', 'post');
 | |
|             $this->db->cache_delete('blog', 'like');
 | |
|             $this->db->cache_delete('blog', 'comment');
 | |
|             $this->db->cache_delete('blog', 'getComments');
 | |
|             $this->db->cache_delete('admin', 'blog');
 | |
|         }
 | |
| 
 | |
|         public function getPrevPost($id)
 | |
|         {
 | |
|             $posts = $this->db->query('SELECT postID, postUrl, postImage FROM blog_posts WHERE postPublishDate < (SELECT postPublishDate FROM blog_posts WHERE postID = ?) AND postState = 1 ORDER BY postPublishDate DESC LIMIT 1', [$id])->result_array();
 | |
|             return $this->mergePostTitleDesc($posts);
 | |
|         }
 | |
| 
 | |
|         public function getNextPost($id)
 | |
|         {
 | |
|             $posts = $this->db->query('SELECT postID, postUrl, postImage FROM blog_posts WHERE postPublishDate > (SELECT postPublishDate FROM blog_posts WHERE postID = ?) AND postState = 1 ORDER BY postPublishDate ASC LIMIT 1', [$id])->result_array();
 | |
|             return $this->mergePostTitleDesc($posts);
 | |
|         }
 | |
| 
 | |
|         public function getMostRecentPosts($count)
 | |
|         {
 | |
|             $posts = $this->db->query('SELECT postID, postImage, postUrl, postPublishDate FROM blog_posts WHERE postState = 1 ORDER BY postPublishDate DESC LIMIT ?', [$count])->result_array();
 | |
|             return $this->mergePostTitleDesc($posts);
 | |
|         }
 | |
| 
 | |
|         private function countWords($text)
 | |
|         {
 | |
|             $text = preg_replace("/<[a-zA-Z0-9\/ .,:;\-_+!?&%=\"]+>/", '', $text);
 | |
|             return str_word_count($text);
 | |
|         }
 | |
| 
 | |
|         public function getReadingTime($postID)
 | |
|         {
 | |
|             $data = $this->db->query('SELECT wordCount, language FROM blog_content c WHERE postID = ? AND isActive = TRUE AND (c.language = ? OR c.language = "de") ORDER BY contentDate DESC', [$postID, $_SESSION['site_lang']])->result_array();
 | |
| 
 | |
|             if (empty($data)) {
 | |
|                 return 0;
 | |
|             }
 | |
| 
 | |
|             $wordCount = $data[0]['wordCount'];
 | |
| 
 | |
|             foreach ($data as $entry) {
 | |
|                 if ($entry['language'] == $_SESSION['site_lang']) {
 | |
|                     $wordCount = $entry['wordCount'];
 | |
|                     break;
 | |
|                 }
 | |
|             }
 | |
| 
 | |
|             $wordsPerSecond = 3;
 | |
|             $duration = $wordCount / $wordsPerSecond;
 | |
|             $duration /= 60;
 | |
|             $duration = round($duration);
 | |
|             return $duration;
 | |
|         }
 | |
| 
 | |
|         public function createNewPostDraft($authorID)
 | |
|         {
 | |
|             $this->db->query('INSERT INTO blog_posts (postAuthorID, postState) VALUE (?, 2)', [$authorID]);
 | |
|             $data = $this->db->query('SELECT postID FROM blog_posts WHERE postState = 2 ORDER BY postID DESC LIMIT 1')->result_array();
 | |
|             return intval($data[0]['postID']);
 | |
|         }
 | |
| 
 | |
|         public function createNewContentDraft($postID)
 | |
|         {
 | |
|             $this->db->query('INSERT INTO blog_content (postID, isActive) VALUES (?, 0)', [$postID]);
 | |
|             $this->db->cache_delete('admin', 'blog');
 | |
|             $data = $this->db->query('SELECT contentID FROM blog_content WHERE postID = ? ORDER BY contentID DESC LIMIT 1', [$postID])->result_array();
 | |
|             return intval($data[0]['contentID']);
 | |
|         }
 | |
| 
 | |
|         public function createNewTranslation($postID, $language)
 | |
|         {
 | |
|             $data = $this->db->query('SELECT postTranslationID FROM blog_translations WHERE postID = ? AND language = ?', [$postID, $language])->result_array();
 | |
| 
 | |
|             if (empty($data)) {
 | |
|                 $this->db->query('INSERT INTO blog_translations (postID, language) VALUES (?, ?)', [$postID, $language]);
 | |
|                 $data = $this->db->query('SELECT postTranslationID FROM blog_translations WHERE postID = ? AND language = ? ORDER BY postTranslationID DESC', [$postID, $language])->result_array();
 | |
|             }
 | |
| 
 | |
|             return intval($data[0]['postTranslationID']);
 | |
|         }
 | |
| 
 | |
|         public function updatePostDraft($postID, $postUrl, $postCategory, $postPublishDate, $postImage)
 | |
|         {
 | |
|             $this->db->query('UPDATE blog_posts SET postUrl = ?, postCategoryID = ?, postPublishDate = ?, postImage = ? WHERE postID = ?', [$postUrl, $postCategory, $postPublishDate, $postImage, $postID]);
 | |
|             $this->db->cache_delete('admin', 'edit');
 | |
|         }
 | |
| 
 | |
|         public function updateContentDraft($contentID, $postContent, $postLang)
 | |
|         {
 | |
|             $wordCount = $this->countWords($postContent);
 | |
|             $this->db->query('UPDATE blog_content SET content = ?, language = ?, contentDate = NOW(), wordCount = ? WHERE contentID = ?', [$postContent, $postLang, $wordCount, $contentID]);
 | |
|             $this->db->cache_delete('admin', 'blog');
 | |
|         }
 | |
| 
 | |
|         public function updateTranslation($translationID, $postTitle, $postDesc)
 | |
|         {
 | |
|             $this->db->query('UPDATE blog_translations SET postTitle = ?, postDesc = ? WHERE postTranslationID = ?', [$postTitle, $postDesc, $translationID]);
 | |
|             $this->db->cache_delete('admin', 'blog');
 | |
|         }
 | |
| 
 | |
|         public function publishPostDraft($postID)
 | |
|         {
 | |
|             $this->db->query('UPDATE blog_posts SET postPublishDate = NOW(), postLastEdit = NOW(), postState = 1 WHERE postID = ?', [$postID]);
 | |
|             $this->db->cache_delete('blog', 'index');
 | |
|             $this->db->cache_delete('blog', 'post');
 | |
|             $this->db->cache_delete('blog', 'tag');
 | |
|             $this->db->cache_delete('blog', 'category');
 | |
|             $this->db->cache_delete('blog', 'search');
 | |
|             $this->db->cache_delete('admin', 'blog');
 | |
|         }
 | |
| 
 | |
|         public function publishContentDraft($authorID, $contentID, $postID, $lang)
 | |
|         {
 | |
|             $firstContent = $this->db->query('SELECT contentID FROM blog_content WHERE postID = ? AND contentID != ? AND language = ?', [$postID, $contentID, $lang])->result_array();
 | |
|             $isNative = TRUE;
 | |
|             if(!empty($firstContent)) {
 | |
|                 $isNative = FALSE;
 | |
|             }
 | |
| 
 | |
|             $this->db->query('UPDATE blog_content SET isActive = FALSE WHERE postID = ?', [$postID]);
 | |
|             $this->db->query('UPDATE blog_content SET isActive = TRUE, isNativePost = ?, contentDate = NOW(), contentAuthorID = ? WHERE contentID = ?', [$isNative, $authorID, $contentID]);
 | |
|         }
 | |
| 
 | |
|         public function getPostTranslationIDs($postID) {
 | |
|             $data = $this->db->query('SELECT postTranslationID, language FROM blog_translations WHERE postID = ?', [$postID])->result_array();
 | |
|             return $data;
 | |
|         }
 | |
| 
 | |
|         public function getPostContentIDs($postID) {
 | |
|             $data = $this->db->query('SELECT (SELECT contentID FROM blog_content WHERE language = c.language ORDER BY contentID DESC LIMIT 1) contentID, language FROM blog_content c WHERE postID = ? AND language IS NOT NULL GROUP BY language ORDER BY contentID DESC', [$postID])->result_array();
 | |
|             return $data;
 | |
|         }
 | |
| 
 | |
|         public function getPostTranslations($postID)
 | |
|         {
 | |
|             $data = $this->db->query('SELECT * FROM blog_translations t LEFT JOIN blog_content c ON c.postID = t.postID AND c.language = t.language WHERE t.postID = ?', [$postID])->result_array();
 | |
|             return $data;
 | |
|         }
 | |
| 
 | |
|         public function postIDExisting($postID)
 | |
|         {
 | |
|             $data = $this->db->query('SELECT postID FROM blog_posts WHERE postID = ?', [$postID])->result_array();
 | |
|             return !empty($data);
 | |
|         }
 | |
| 
 | |
|         public function contentIDExisting($postID, $contentID)
 | |
|         {
 | |
|             $data = $this->db->query('SELECT contentID FROM blog_content WHERE postID = ? AND contentID = ?', [$postID, $contentID])->result_array();
 | |
|             return !empty($data);
 | |
|         }
 | |
| 
 | |
|         public function translationIDExisting($postID, $translationID)
 | |
|         {
 | |
|             $data = $this->db->query('SELECT postTranslationID FROM blog_translations WHERE postID = ? AND postTranslationID = ?', [$postID, $translationID])->result_array();
 | |
|             return !empty($data);
 | |
|         }
 | |
| 
 | |
|         public function postUrlExisting($postUrl) {
 | |
|             $data = $this->db->query('SELECT postUrl FROM blog_posts WHERE postUrl = ?', [$postUrl])->result_array();
 | |
|             return !empty($data);
 | |
|         }
 | |
|     } |