Added links to other (language) versions of an article
authorMarcel Werk <burntime@woltlab.com>
Wed, 8 Jun 2016 13:18:20 +0000 (15:18 +0200)
committerMarcel Werk <burntime@woltlab.com>
Wed, 8 Jun 2016 13:51:26 +0000 (15:51 +0200)
com.woltlab.wcf/templates/article.tpl
wcfsetup/install/files/lib/data/article/Article.class.php
wcfsetup/install/files/lib/data/article/content/ArticleContent.class.php

index df85e5a4f65c27c209526b418beec10323c066c2..87a693b3568dc92e9ccc6e46dd35fecc0c4e89e3 100644 (file)
        </header>
 {/capture}
 
+{capture assign='headContent'}
+       {if $article->isMultilingual}
+               {foreach from=$article->getLanguageLinks() item='langArticleContent'}
+                       {if $langArticleContent->getLanguage()}
+                               <link rel="alternate" hreflang="{$langArticleContent->getLanguage()->languageCode}" href="{$langArticleContent->getLink()}" />
+                       {/if}
+               {/foreach}
+       {/if}
+{/capture}
+
 {include file='header'}
 
 {if $articleContent->getImage()}
        
        <ul class="articleLikeButtons buttonGroup"></ul>
        
+       {if $article->isMultilingual}
+               {foreach from=$article->getLanguageLinks() item='langArticleContent'}
+                       {if $langArticleContent->getLanguage() && $langArticleContent->languageID != $articleContent->languageID}
+                               <a href="{$langArticleContent->getLink()}">{$langArticleContent->getLanguage()->languageName}</a>
+                       {/if}
+               {/foreach}
+       {/if}
+       
        {if ENABLE_SHARE_BUTTONS}
                <section class="section jsOnly">
                        <h2 class="sectionTitle">{lang}wcf.message.share{/lang}</h2>
index f2a3f392c05b7ebeb4b61758e53f3da4d5c2c7dc..d3988d12821b5961e63a37e385d43512e472b7bf 100644 (file)
@@ -48,6 +48,12 @@ class Article extends DatabaseObject implements ILinkableObject {
         */
        public $articleContent;
        
+       /**
+        * language links
+        * @var ArticleContent[]
+        */
+       public $languageLinks;
+       
        /**
         * article's category
         * @var ArticleCategory
@@ -205,6 +211,27 @@ class Article extends DatabaseObject implements ILinkableObject {
                return $this->articleContent;
        }
        
+       /**
+        * Returns the article's language links.
+        *
+        * @return      ArticleContent[]
+        */
+       public function getLanguageLinks() {
+               if ($this->languageLinks === null) {
+                       $this->languageLinks = [];
+                       $sql = "SELECT  articleContentID, title, languageID
+                               FROM    wcf" . WCF_N . "_article_content
+                               WHERE   articleID = ?";
+                       $statement = WCF::getDB()->prepareStatement($sql);
+                       $statement->execute([$this->articleID]);
+                       while ($row = $statement->fetchArray()) {
+                               $this->languageLinks[($row['languageID'] ?: 0)] = new ArticleContent(null, $row);
+                       }
+               }
+               
+               return $this->languageLinks;
+       }
+       
        /**
         * Returns the category of the article.
         *
index 126d88ddc13d5abdcbf491f6e2e46a1b2e7673df..ac4d51457fce14fd8ecf3d7ebaa1db377488ff45 100644 (file)
@@ -3,7 +3,9 @@ namespace wcf\data\article\content;
 use wcf\data\article\Article;
 use wcf\data\DatabaseObject;
 use wcf\data\ILinkableObject;
+use wcf\data\language\Language;
 use wcf\system\html\output\HtmlOutputProcessor;
+use wcf\system\language\LanguageFactory;
 use wcf\system\message\embedded\object\MessageEmbeddedObjectManager;
 use wcf\system\request\IRouteController;
 use wcf\system\request\LinkHandler;
@@ -88,6 +90,19 @@ class ArticleContent extends DatabaseObject implements ILinkableObject, IRouteCo
                return $this->article;
        }
        
+       /**
+        * Returns the language of this article content as language object.
+        * 
+        * @return Language|null
+        */
+       public function getLanguage() {
+               if ($this->languageID) {
+                       return LanguageFactory::getInstance()->getLanguage($this->languageID);
+               }
+               
+               return null;
+       }
+       
        public static function getArticleContent($articleID, $languageID) {
                if ($languageID !== null) {
                        $sql = "SELECT  *