RELATEDX

{RELATEDX:Listenzähler : Schlüsselwörter : Kategorie-ID}

Erweitert von KH (flip-flop) 15.03.09

Docu: http://www.phpwcms-docu.de/link_tags_.phtml

Forum: –

Tag: {RELATEDX:Listenzähler : Schlüsselwörter : Kategorie-ID} ist ein “ausgeschnittener Programmteil aus dem System frontend” erweitert um die Möglichkeit unterhalb einer vorgegebenen Ebene mit der Suche zu beginnen. Dies ist sehr vorteilhaft z.B. für mehrsprachige Seiten. <note> Bis zur Version 1.21 dürfen die zu durchsuchenden Kategorien in denen die Artikel liegen nicht versteckt sein, ab der Version 1.3 kann dies im Script eingestellt werden. </note>

Die Formatierung und Sortierung wird im Bereich // related articles based on keywords in der Datei config/phpwcms/conf.template_default.inc.php eingestellt.


Dateiname: rt_relatedx.php

Ort: /template/inc_script/frontend_render/rt_relatedx.php

Bedingung: $phpwcms['allow_ext_render'] = 1;/config/phpwcms/conf.inc.php




Bitte immer alle Felder ausfüllen: Z.B.

  • {RELATEDX:0:tip,top,flip,flop:0}die Suche nach den Schlüsselwörtern in Artikeln beginnt unterhalb der Kategorie-ID=0 = home
  • {RELATEDX:5:tip,top,flip,flop:2}Ausgabe der ersten fünf Einträge, mit der Suche nach den Schlüsselwörtern in Artikeln beginnend untehalb der Kategorie-ID=2

Z.B.:

Gegebene Struktur:

------------------------------------
L  E  V  E  L
.:  :  :  :
.0  1  2  3   <- LEVEL-Nr.
.:  :  :  :
-+ home   :                    ID=0
-+--+ category_01              ID=01
-+--+ category_02              ID=02
-+--+--+ category_02_01        ID=04
-+--+--+--+ category_02_01_01  ID=06
-+--+--+--+ category_02_01_02  ID=07
-+--+--+ category_02_01        ID=05
-+--+--+ category_02_02        ID=08
-+--+ category_03              ID=03
-+--+ category_04              ID=09
.:  :  :  :
.0  1  2  3   <- LEVEL Nr.
------------------------------------

{RELATEDX:5:tip,top,flip,flop:2} → Ausgabe: Der Title-Link der ersten fünf sichtbaren Artikel, unterhalb der nicht versteckten category_02 (ID=2), in denen die Schlüsselwörter vorkommen. (“nicht versteckten” siehe Hinweis oben und V1.3).


V 1.0

20.01.11 KH Update: Fehler korrigiert

Bitte spätestens ab r400 die Version 1.1 verwenden!!

{RELATEDX:count:key words:category-ID}

<?php
/*************************************************************************************
   Copyright notice
 
   (c) 2002-2008 Oliver Georgi (oliver@phpwcms.de) // All rights reserved.
 
   This script is part of PHPWCMS. The PHPWCMS web content management system is
   free software; you can redistribute it and/or modify it under the terms of
   the GNU General Public License as published by the Free Software Foundation;
   either version 2 of the License, or (at your option) any later version.
 
   The GNU General Public License can be found at http://www.gnu.org/copyleft/gpl.html
   A copy is found in the textfile GPL.txt and important notices to the license
   from the author is found in LICENSE.txt distributed with these scripts.
 
   This script is distributed in the hope that it will be useful, but WITHOUT ANY
   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
   PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 
   This copyright notice MUST APPEAR in all copies of the script!
 
-------------------------------------------------------------------------------------
15.03.09 KH (flip-flop) RT RELATED cut out (frontend snippet)
   {RELATEDX:count:key word´s:category-ID}
...Please always fill out all fields: e.g.
   {RELATEDX:0:tip,top,flip,flop:0}  -> Search beginning behind cad-ID=0 = home
   {RELATEDX:5:tip,top,flip,flop:2}  -> Output first 5 entries, search beginning behind cad-ID=2
 
*************************************************************************************/
// ----------------------------------------------------------------------------------
// obligate check for phpwcms constants
if (!defined('PHPWCMS_ROOT')) {die("You Cannot Access This Script Directly, Have a Nice Day.");}
// ----------------------------------------------------------------------------------
 
 
 
// related articles based on keywords, inspired by Magnar Stav Johanssen
if(strpos($content["all"],'{RELATEDX:') !== false) {
 
    if (!$no_content_for_this_page && !empty($content["articles"][$aktion[1]]["article_keyword"])) {
        $related_keywords = $content["articles"][$aktion[1]]["article_keyword"];
    } else {
        $related_keywords = '';
    }
 
// -------------------------------------------------------------
 
 
// $content["all"] = preg_replace('/\{RELATEDX:(\d+):(.*?):(\d+)\}/e','my_get_related_articles("$2",$GLOBALS["aktion"][1],$GLOBALS["template_default"]["related"],"$1",$GLOBALS["db"], "$3");',$content["all"]);
 
 
 
function buildStruct_TopDown($start=0) {
 
    static $my_cat_id = '';
 
    $struct = getStructureChildData($start);
 
    foreach($struct as $value) {
 
        buildStruct_TopDown($value['acat_id']);
        $my_cat_id .= $my_cat_id != '' ? ','.$value['acat_id'] : $value['acat_id'];
 
//        echo  '||'.$value['acat_id'].' ,';
 
    }
 
return $my_cat_id;
 
}
 
// =================================================================
 
function my_get_related_articles($keywords, $current_article_id, $template_default, $max_cnt_links=0, $dbcon ,$cat_id) {
    // find keyword for current article used for RELATED replacementtag
    // prepared and inspired by Magnar Stav Johanssen
 
    $cat_id = buildStruct_TopDown($cat_id);  // find all permitted IDs behind $cat_id
 
 
    $keyword_links = "";
    $max_cnt_links = intval($max_cnt_links);
 
    $keywords = str_replace("ALLKEYWORDS", $GLOBALS['content']['all_keywords'].',', $keywords);
 
    // replace unwanted chars and convert to wanted
    $keywords = str_replace(";", ",", $keywords);
    $keywords = str_replace("'", "", $keywords);
    $keywords = str_replace(" ", ",", $keywords);
    $keywords = str_replace(",,", ",", $keywords);
 
    // choose comma separated keywords
    $keywordarray = explode (",", $keywords);
    $keywordarray = array_map('trim', $keywordarray);
    $keywordarray = array_diff($keywordarray, array(''));
    $keywordarray = array_unique($keywordarray);
    $keywordarray = array_map('strtoupper', $keywordarray);
    // check for empty keywords or keywords smaller than 3 chars
    if(is_array($keywordarray) && count($keywordarray)) {
        foreach($keywordarray as $key => $value) {
 
            if(substr($keywordarray[$key], 0, 1) == '-') {
                $doNotUse = substr($keywordarray[$key], 1);
                foreach($keywordarray as $key2 => $value2) {
                    if($doNotUse == $value2) {
                        unset($keywordarray[$key2]);
                        unset($keywordarray[$key]);
                    }
                }
            }
 
            if(isset($keywordarray[$key]) && (strlen($keywordarray[$key]) < 3 || empty($keywordarray[$key]))) {
                unset($keywordarray[$key]);
            }
        }
    }
 
    if(is_array($keywordarray) && count($keywordarray)) {
        $where = "";
        foreach($keywordarray as $value) {
                //build where keyword = blabla
                $where .= ($where) ? " OR " : "";
                //replace every "'" to "''" for security reasons with aporeplace()
                $where .= "article_keyword LIKE '%".aporeplace($value)."%'";
        }
        $limit = ($max_cnt_links) ? " LIMIT ".$max_cnt_links : "";
        $sql  =    "SELECT article_id, article_title, article_cid, article_subtitle, article_summary, article_alias ";
        $sql .=    "FROM ".DB_PREPEND."phpwcms_article WHERE article_deleted=0 AND ";
        $sql .=    "article_id<>".intval($current_article_id)." AND ";
        $sql .=    "article_cid IN (".$cat_id.") AND ";
        // VISIBLE_MODE: 0 = frontend (all) mode, 1 = article user mode, 2 = admin user mode
        switch(VISIBLE_MODE) {
            case 0: $sql .=    "article_public=1 AND article_aktiv=1 AND ";
                    break;
            case 1: $sql .= "article_uid=".$_SESSION["wcs_user_id"]." AND ";
                    break;
            //case 2: admin mode no additional neccessary
        }
        $sql .=    "article_begin < NOW() AND article_end > NOW() AND (".$where.") ";
 
        if(empty($template_default['sort_by'])) $template_default['sort_by'] = '';
 
        switch($template_default['sort_by']) {
 
            case 'title_asc':
                        $sql .=    "ORDER BY article_title";
                        break;
 
            case 'title_desc':
                        $sql .=    "ORDER BY article_title DESC";
                        break;
 
            case 'ldate_asc':
                        $sql .=    "ORDER BY article_begin";
                        break;
 
            case 'ldate_desc':
                        $sql .=    "ORDER BY article_begin DESC";
                        break;
 
            case 'kdate_asc':
                        $sql .=    "ORDER BY article_end";
                        break;
 
            case 'kdate_desc':
                        $sql .=    "ORDER BY article_end DESC";
                        break;
 
            case 'cdate_asc':
                        $sql .=    "ORDER BY article_created";
                        break;
 
            case 'cdate_desc':
                        $sql .=    "ORDER BY article_created DESC";
                        break;
 
            default:
                        $sql .=    "ORDER BY article_tstamp DESC";
        }
 
        $sql .= $limit;
 
        // related things
        $target = ($template_default["link_target"]) ? ' target="'.$template_default["link_target"].'"' : "";
        if($result = _dbQuery($sql)) {
            $count_results = count($result);
            $count = 0;
 
 
            foreach($result as $value) {
                $count++;
                if($template_default["link_length"] && strlen($value['article_title']) > $template_default["link_length"]) {
                    $article_title = substr($value['article_title'], 0, $template_default["link_length"]).$template_default["cut_title_add"];
                } else {
                    $article_title = $value['article_title'];
                }
 
                $keyword_links .= $template_default["link_before"].$template_default["link_symbol"];
                $keyword_links .= '<a href="index.php?id='.$value['article_cid'].','.$value['article_id'].',0,0,1,0"';
                $keyword_links .= $target.">".html_specialchars($article_title)."</a>";
 
                //try to remove possible unwanted after - if not enclosed before.link.after
                if($keyword_links && !$template_default["link_before"] && $count < $count_results) {
                    $keyword_links .= $template_default["link_after"];
                }
            }
        }
    }
 
    //enclose whole
    if($keyword_links) $keyword_links = $template_default["before"].$keyword_links.$template_default["after"];
 
    return $keyword_links;
}
 
 
    $content["all"] = preg_replace('/\{RELATEDX:(\d+)\}/e','get_related_articles($related_keywords,$aktion[1],$template_default["related"],"$1",$db);',$content["all"]);
    $content["all"] = preg_replace('/\{RELATEDX:(\d+):(.*?):(\d+)\}/e','my_get_related_articles("$2",$aktion[1],$template_default["related"],"$1",$db, "$3");',$content["all"]);
 
 
}
 
 
 
?>


V 1.1

20.01.11 KH geändert: (r400: Solve redirect problem with {NEW:..} and {RELATED:...})
22.01.11 KH Update: Die Startkategorie wird nun in das Ergebnis einbezogen. Ist das nicht erwünscht, bitte die Zeile nach // PATCH01 entkommentieren.
25.01.11 KH Update: Fehlerkorrektur in der Funktion buildStruct_TopDown. Bei mehrmaligem Aufruf blieben die alten Daten vorhanden.

{RELATEDX:count:key words:category-ID}

<?php
/*************************************************************************************
   Copyright notice
 
   (c) 2002-2011 Oliver Georgi (oliver@phpwcms.de) // All rights reserved.
 
   This script is part of PHPWCMS. The PHPWCMS web content management system is
   free software; you can redistribute it and/or modify it under the terms of
   the GNU General Public License as published by the Free Software Foundation;
   either version 2 of the License, or (at your option) any later version.
 
   The GNU General Public License can be found at http://www.gnu.org/copyleft/gpl.html
   A copy is found in the textfile GPL.txt and important notices to the license
   from the author is found in LICENSE.txt distributed with these scripts.
 
   This script is distributed in the hope that it will be useful, but WITHOUT ANY
   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
   PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 
   This copyright notice MUST APPEAR in all copies of the script!
 
-------------------------------------------------------------------------------------
V 1.0
15.03.09 KH (flip-flop) RT RELATED cut out (frontend snippet)
   {RELATEDX:count:key word´s:category-ID}
...Please always fill out all fields: e.g.
   {RELATEDX:0:tip,top,flip,flop:0}  -> Search beginning behind cad-ID=0 = home
   {RELATEDX:5:tip,top,flip,flop:2}  -> Output first 5 entries, search beginning behind cad-ID=2
 
V 1.1
20.01.11 KH - Update: Last version don´t work right in newer CMS vrsions (since r400).
 http://code.google.com/p/phpwcms/source/detail?r=400&path=/trunk/include/inc_front/front.func.inc.php
22.01.11 KH : Start category in result
25.01.11 KH : Error correction in function buildStruct_TopDown
 
*************************************************************************************/
// ----------------------------------------------------------------------------------
// obligate check for phpwcms constants
if (!defined('PHPWCMS_ROOT')) {die("You Cannot Access This Script Directly, Have a Nice Day.");}
// ----------------------------------------------------------------------------------
 
 
 
// related articles based on keywords, inspired by Magnar Stav Johanssen
if(strpos($content["all"],'{RELATEDX:') !== false) {
 
    if (!$no_content_for_this_page && !empty($content["articles"][$aktion[1]]["article_keyword"])) {
        $related_keywords = $content["articles"][$aktion[1]]["article_keyword"];
    } else {
        $related_keywords = '';
    }
 
// -------------------------------------------------------------
 
 
 
function buildStruct_TopDown($start=0, &$my_cat_id='') { // KH: V1.1 25.01.2011
 
    $struct = getStructureChildData($start);
 
    foreach($struct as $value) {
 
//        buildStruct_TopDown($value['acat_id'],$my_cat_id);
        $my_cat_id .= ($my_cat_id != '') ? ','.$value['acat_id'] : $value['acat_id'];
        buildStruct_TopDown($value['acat_id'],$my_cat_id);
 
//        echo  '||'.$value['acat_id'].' ,';
 
    }
 
return $my_cat_id;
 
}
 
// =================================================================
 
function my_get_related_articles($keywords, $current_article_id, $template_default, $max_cnt_links=0, $dbcon ,$cat_id) {
    // find keyword for current article used for RELATED replacementtag
    // prepared and inspired by Magnar Stav Johanssen
 
    $temp = buildStruct_TopDown($cat_id);           // find all permitted IDs behind $cat_id
    if (!empty($temp)) $cat_id = $cat_id.','.$temp;  // +KH: 20.01.11 add start category
// PATCH01
//    $cat_id = $temp;                               // If you don´t need the start cat in result
 
 
    $keyword_links = "";
    $max_cnt_links = intval($max_cnt_links);
 
    $keywords = str_replace("ALLKEYWORDS", $GLOBALS['content']['all_keywords'].',', $keywords);
 
    // replace unwanted chars and convert to wanted
    $keywords = str_replace(";", ",", $keywords);
    $keywords = str_replace("'", "", $keywords);
    $keywords = str_replace(" ", ",", $keywords);
    $keywords = str_replace(",,", ",", $keywords);
 
    // choose comma separated keywords
    $keywordarray = explode (",", $keywords);
    $keywordarray = array_map('trim', $keywordarray);
    $keywordarray = array_diff($keywordarray, array(''));
    $keywordarray = array_unique($keywordarray);
    $keywordarray = array_map('strtoupper', $keywordarray);
    // check for empty keywords or keywords smaller than 3 chars
    if(is_array($keywordarray) && count($keywordarray)) {
        foreach($keywordarray as $key => $value) {
 
            if(substr($keywordarray[$key], 0, 1) == '-') {
                $doNotUse = substr($keywordarray[$key], 1);
                foreach($keywordarray as $key2 => $value2) {
                    if($doNotUse == $value2) {
                        unset($keywordarray[$key2]);
                        unset($keywordarray[$key]);
                    }
                }
            }
 
            if(isset($keywordarray[$key]) && (strlen($keywordarray[$key]) < 3 || empty($keywordarray[$key]))) {
                unset($keywordarray[$key]);
            }
        }
    }
 
    if(is_array($keywordarray) && count($keywordarray)) {
        $where = "";
        foreach($keywordarray as $value) {
                //build where keyword = blabla
                $where .= ($where) ? " OR " : "";
                //replace every "'" to "''" for security reasons with aporeplace()
                $where .= "article_keyword LIKE '%".aporeplace($value)."%'";
        }
        $limit = ($max_cnt_links) ? " LIMIT ".$max_cnt_links : "";
// -KH:200111
//        $sql  =    "SELECT article_id, article_title, article_cid, article_subtitle, article_summary, article_alias ";
// +KH:200111
        $sql  =    "SELECT article_id, article_title, article_cid, article_subtitle, article_summary, article_alias, article_redirect, article_morelink ";
        $sql .=    "FROM ".DB_PREPEND."phpwcms_article WHERE article_deleted=0 AND ";
        $sql .=    "article_id<>".intval($current_article_id)." AND ";
        $sql .=    "article_cid IN (".$cat_id.") AND ";
        // VISIBLE_MODE: 0 = frontend (all) mode, 1 = article user mode, 2 = admin user mode
        switch(VISIBLE_MODE) {
            case 0: $sql .=    "article_public=1 AND article_aktiv=1 AND ";
                    break;
            case 1: $sql .= "article_uid=".$_SESSION["wcs_user_id"]." AND ";
                    break;
            //case 2: admin mode no additional neccessary
        }
        $sql .=    "article_begin < NOW() AND article_end > NOW() AND (".$where.") ";
 
        if(empty($template_default['sort_by'])) $template_default['sort_by'] = '';
 
        switch($template_default['sort_by']) {
 
            case 'title_asc':
                        $sql .=    "ORDER BY article_title";
                        break;
 
            case 'title_desc':
                        $sql .=    "ORDER BY article_title DESC";
                        break;
 
            case 'ldate_asc':
                        $sql .=    "ORDER BY article_begin";
                        break;
 
            case 'ldate_desc':
                        $sql .=    "ORDER BY article_begin DESC";
                        break;
 
            case 'kdate_asc':
                        $sql .=    "ORDER BY article_end";
                        break;
 
            case 'kdate_desc':
                        $sql .=    "ORDER BY article_end DESC";
                        break;
 
            case 'cdate_asc':
                        $sql .=    "ORDER BY article_created";
                        break;
 
            case 'cdate_desc':
                        $sql .=    "ORDER BY article_created DESC";
                        break;
 
            default:
                        $sql .=    "ORDER BY article_tstamp DESC";
        }
 
        $sql .= $limit;
 
        // related things
        $target = $template_default["link_target"] ? ' target="'.$template_default["link_target"].'"' : '';
        $result = _dbQuery($sql);
        if(isset($result[0])) {
            foreach($result as $row) {
                if(empty($row['article_redirect'])) {
                    if(empty($row['article_morelink'])) {
                        continue;
                    }
                    $article_link = 'index.php?'.setGetArticleAid($row).'"'.$target;
                } else {
                    $redirect = get_redirect_link($row['article_redirect'], ' ', '');
                    $article_link = $redirect['link'].'"'.$redirect['target'];
                }
 
                if($template_default["link_length"] && strlen($row['article_title']) > $template_default["link_length"]) {
                    $article_title = substr($row['article_title'], 0, $template_default["link_length"]).$template_default["cut_title_add"];
                } else {
                    $article_title = $row['article_title'];
                }
                $keyword_links .= $template_default["link_before"];
                $keyword_links .= $template_default["link_symbol"];
                $keyword_links .= '<a href="' . $article_link . '>';
                $keyword_links .= html_specialchars($article_title);
                $keyword_links .= '</a>' . $template_default["link_after"];
            }
        }
    }
 
    // parses all system RTs  +KH 25.01.2011
    $keyword_links = html_parser($keyword_links);
 
    //enclose whole
    return empty($keyword_links) ? '' : $template_default["before"].$keyword_links.$template_default["after"];
}
 
 
//    $content["all"] = preg_replace('/\{RELATEDX:(\d+):(.*?):(\d+)\}/e','my_get_related_articles("$2",$GLOBALS["aktion"][1],$GLOBALS["template_default"]["related"],"$1",$GLOBALS["db"], "$3");',$content["all"]);
 
    $content["all"] = preg_replace('/\{RELATEDX:(\d+)\}/e','get_related_articles($related_keywords,$aktion[1],$template_default["related"],"$1",$db);',$content["all"]);
    $content["all"] = preg_replace('/\{RELATEDX:(\d+):(.*?):(\d+)\}/e','my_get_related_articles("$2",$aktion[1],$template_default["related"],"$1",$db, "$3");',$content["all"]);
 
 
}
 
 
?>


V 1.2

27.01.11 KH Update: Erweitert um das Erstellungsdatum des jeweiligen Artikels auszugeben.

Um diese Funktion zu aktivieren sind zwei Möglichkeiten vorhanden:

  • Im Script innerhalb des Bereichs “CUSTOM VAR” die Variable $date_form mit entsprechender php-Datumsnotation füllen.
  • In der Datei conf_template_default.inc.php eine Variable $template_default['related']['date_format'] anlegen und mit entsprechender php-Datumsnotation füllen.

// CUSTOM VAR ========================== +KH: 27.01.11
// Article date created
// Set in conf_template_default.inc.php e.g: $template_default['related']['date_format'] = 'd.m.Y';
// or set up the var $date_form
    
    $date_form = 'd.m.Y'; // '';
    $date_form_before = '<span style="font-size:70%; color: grey;"> [';
    $date_form_after  = ']</span>';
    
// ===================================== 

// CUSTOM VAR ========================== +KH: 27.01.11
// Article date created
// Set in conf_template_default.inc.php e.g: $template_default['related']['date_format'] = 'd.m.Y';
// or set up the var $date_form
    
    $date_form = ''; // 'd.m.Y';
    $date_form_before = ' [';
    $date_form_after  = ']';
    
// ===================================== 


{RELATEDX:count:key words:category-ID}

<?php
/*************************************************************************************
   Copyright notice
 
   (c) 2002-2011 Oliver Georgi (oliver@phpwcms.de) // All rights reserved.
 
   This script is part of PHPWCMS. The PHPWCMS web content management system is
   free software; you can redistribute it and/or modify it under the terms of
   the GNU General Public License as published by the Free Software Foundation;
   either version 2 of the License, or (at your option) any later version.
 
   The GNU General Public License can be found at http://www.gnu.org/copyleft/gpl.html
   A copy is found in the textfile GPL.txt and important notices to the license
   from the author is found in LICENSE.txt distributed with these scripts.
 
   This script is distributed in the hope that it will be useful, but WITHOUT ANY
   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
   PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 
   This copyright notice MUST APPEAR in all copies of the script!
 
-------------------------------------------------------------------------------------
V 1.0
15.03.09 KH (flip-flop) RT RELATED cut out (frontend snippet)
   {RELATEDX:count:key word´s:category-ID}
...Please always fill out all fields: e.g.
   {RELATEDX:0:tip,top,flip,flop:0}  -> Search beginning behind cad-ID=0 = home
   {RELATEDX:5:tip,top,flip,flop:2}  -> Output first 5 entries, search beginning behind cad-ID=2
 
V 1.1
20.01.11 KH - Update: Last version don´t work right in newer CMS vrsions (since r400).
 http://code.google.com/p/phpwcms/source/detail?r=400&path=/trunk/include/inc_front/front.func.inc.php
22.01.11 KH : Start category in result
 
V 1.2
27.01.11 KH - Update: Added "article creaded date" -> see CUSTOM VAR
 
*************************************************************************************/
// ----------------------------------------------------------------------------------
// obligate check for phpwcms constants
if (!defined('PHPWCMS_ROOT')) {die("You Cannot Access This Script Directly, Have a Nice Day.");}
// ----------------------------------------------------------------------------------
 
 
 
// related articles based on keywords, inspired by Magnar Stav Johanssen
if(strpos($content["all"],'{RELATEDX:') !== false) {
 
// CUSTOM VAR ========================== +KH: 27.01.11
// Article date created
// Set in conf_template_default.inc.php e.g: $template_default['related']['date_format'] = 'd.m.Y';
// or set up in var $date_form
 
    $date_form = ''; // 'd.m.Y';
    $date_form_before = ' [';
    $date_form_after  = ']';
 
// =====================================
 
    $date_form = (!isset($template_default['related']['date_format'])) ? $date_form : $template_default['related']['date_format'];  // +KH: 27.01.11
 
    if (!$no_content_for_this_page && !empty($content["articles"][$aktion[1]]["article_keyword"])) {
        $related_keywords = $content["articles"][$aktion[1]]["article_keyword"];
    } else {
        $related_keywords = '';
    }
 
// -------------------------------------------------------------
 
 
 
function buildStruct_TopDown($start=0, &$my_cat_id='') {  // KH: V1.1 25.01.2011
 
 
    $struct = getStructureChildData($start);
 
    foreach($struct as $value) {
 
//        buildStruct_TopDown($value['acat_id'],$my_cat_id);
        $my_cat_id .= ($my_cat_id != '') ? ','.$value['acat_id'] : $value['acat_id'];
        buildStruct_TopDown($value['acat_id'],$my_cat_id);
 
//        echo  '||'.$value['acat_id'].' ,';
 
    }
 
return $my_cat_id;
 
}
 
// =================================================================
 
function my_get_related_articles($keywords='', $current_article_id=0, $template_default='', $max_cnt_links=10, $dbcon ,$cat_id='',$date_form, $date_form_before, $date_form_after) {
    // find keyword for current article used for RELATED replacementtag
    // prepared and inspired by Magnar Stav Johanssen
 
    $temp = buildStruct_TopDown($cat_id);           // find all permitted IDs behind $cat_id
 
    if (!empty($temp)) $cat_id = $cat_id.','.$temp;  // +KH: 20.01.11 add start category
// PATCH01
//    $cat_id = $temp;                               // If you don´t need the start cat in result
 
 
    $keyword_links = "";
    $max_cnt_links = intval($max_cnt_links);
 
    $keywords = str_replace("ALLKEYWORDS", $GLOBALS['content']['all_keywords'].',', $keywords);
 
    // replace unwanted chars and convert to wanted
    $keywords = str_replace(";", ",", $keywords);
    $keywords = str_replace("'", "", $keywords);
    $keywords = str_replace(" ", ",", $keywords);
    $keywords = str_replace(",,", ",", $keywords);
 
    // choose comma separated keywords
    $keywordarray = explode (",", $keywords);
    $keywordarray = array_map('trim', $keywordarray);
    $keywordarray = array_diff($keywordarray, array(''));
    $keywordarray = array_unique($keywordarray);
    $keywordarray = array_map('strtoupper', $keywordarray);
    // check for empty keywords or keywords smaller than 3 chars
    if(is_array($keywordarray) && count($keywordarray)) {
        foreach($keywordarray as $key => $value) {
 
            if(substr($keywordarray[$key], 0, 1) == '-') {
                $doNotUse = substr($keywordarray[$key], 1);
                foreach($keywordarray as $key2 => $value2) {
                    if($doNotUse == $value2) {
                        unset($keywordarray[$key2]);
                        unset($keywordarray[$key]);
                    }
                }
            }
 
            if(isset($keywordarray[$key]) && (strlen($keywordarray[$key]) < 3 || empty($keywordarray[$key]))) {
                unset($keywordarray[$key]);
            }
        }
    }
 
    if(is_array($keywordarray) && count($keywordarray)) {
        $where = "";
        foreach($keywordarray as $value) {
                //build where keyword = blabla
                $where .= ($where) ? " OR " : "";
                //replace every "'" to "''" for security reasons with aporeplace()
                $where .= "article_keyword LIKE '%".aporeplace($value)."%'";
        }
        $limit = ($max_cnt_links) ? " LIMIT ".$max_cnt_links : "";
// -KH:200111
//        $sql  =    "SELECT article_id, article_title, article_cid, article_subtitle, article_summary, article_alias ";
// +KH:200111
        $sql  =    "SELECT article_id, article_title, article_cid, article_subtitle, article_summary, article_alias, article_redirect, article_morelink, article_created ";
        $sql .=    "FROM ".DB_PREPEND."phpwcms_article WHERE article_deleted=0 AND ";
        $sql .=    "article_id<>".intval($current_article_id)." AND ";
        $sql .=    "article_cid IN (".$cat_id.") AND ";
        // VISIBLE_MODE: 0 = frontend (all) mode, 1 = article user mode, 2 = admin user mode
        switch(VISIBLE_MODE) {
            case 0: $sql .=    "article_public=1 AND article_aktiv=1 AND ";
                    break;
            case 1: $sql .= "article_uid=".$_SESSION["wcs_user_id"]." AND ";
                    break;
            //case 2: admin mode no additional neccessary
        }
        $sql .=    "article_begin < NOW() AND article_end > NOW() AND (".$where.") ";
 
        if(empty($template_default['sort_by'])) $template_default['sort_by'] = '';
 
        switch($template_default['sort_by']) {
 
            case 'title_asc':
                        $sql .=    "ORDER BY article_title";
                        break;
 
            case 'title_desc':
                        $sql .=    "ORDER BY article_title DESC";
                        break;
 
            case 'ldate_asc':
                        $sql .=    "ORDER BY article_begin";
                        break;
 
            case 'ldate_desc':
                        $sql .=    "ORDER BY article_begin DESC";
                        break;
 
            case 'kdate_asc':
                        $sql .=    "ORDER BY article_end";
                        break;
 
            case 'kdate_desc':
                        $sql .=    "ORDER BY article_end DESC";
                        break;
 
            case 'cdate_asc':
                        $sql .=    "ORDER BY article_created";
                        break;
 
            case 'cdate_desc':
                        $sql .=    "ORDER BY article_created DESC";
                        break;
 
            default:
                        $sql .=    "ORDER BY article_tstamp DESC";
        }
 
        $sql .= $limit;
 
        // related things
        $target = $template_default["link_target"] ? ' target="'.$template_default["link_target"].'"' : '';
        $result = _dbQuery($sql);
        if(isset($result[0])) {
            foreach($result as $row) {
                if(empty($row['article_redirect'])) {
                    if(empty($row['article_morelink'])) {
                        continue;
                    }
                    $article_link = 'index.php?'.setGetArticleAid($row).'"'.$target;
                } else {
                    $redirect = get_redirect_link($row['article_redirect'], ' ', '');
                    $article_link = $redirect['link'].'"'.$redirect['target'];
                }
 
                if($template_default["link_length"] && strlen($row['article_title']) > $template_default["link_length"]) {
                    $article_title = substr($row['article_title'], 0, $template_default["link_length"]).$template_default["cut_title_add"];
                } else {
                    $article_title = $row['article_title'];
                }
                $keyword_links .= $template_default["link_before"];
                $keyword_links .= $template_default["link_symbol"];
                $keyword_links .= '<a href="' . $article_link . '>';
                $keyword_links .= html_specialchars($article_title);
                if (!empty($date_form)) $keyword_links .= $date_form_before.date($date_form, $row['article_created']).$date_form_after;  // +KH: 27.01.11
                $keyword_links .= '</a>' . $template_default["link_after"];
            }
        }
    }
 
    // parses all system RTs  +KH 25.01.2011
    $keyword_links = html_parser($keyword_links);
 
    //enclose whole
    return (empty($keyword_links)) ? '' : $template_default["before"].$keyword_links.$template_default["after"];
}
 
 
//    $content["all"] = preg_replace('/\{RELATEDX:(\d+):(.*?):(\d+)\}/e','my_get_related_articles("$2",$GLOBALS["aktion"][1],$GLOBALS["template_default"]["related"],"$1",$GLOBALS["db"], "$3");',$content["all"]);
 
    $content["all"] = preg_replace('/\{RELATEDX:(\d+)\}/e','get_related_articles($related_keywords,$aktion[1],$template_default["related"],"$1",$db);',$content["all"]);
    $content["all"] = preg_replace('/\{RELATEDX:(\d+):(.*?):(\d+)\}/e','my_get_related_articles("$2",$aktion[1],$template_default["related"],"$1",$db, "$3",$date_form,$date_form_before, $date_form_after);',$content["all"]);
 
}
 
?>


V 1.21

25.04.11 KH Update: Auswahl ob kein Artikeldatum oder das Artikel-Erstellungsdatum oder das Artikel-Editierdatum ausgegeben wird.

Um diese Funktion zu aktivieren sind zwei Möglichkeiten vorhanden:

  • Im Script innerhalb des Bereichs “CUSTOM VAR” die Variable $date_form mit entsprechender php-Datumsnotation füllen.
  • In der Datei conf_template_default.inc.php eine Variable $template_default['related']['date_format'] anlegen und mit entsprechender php-Datumsnotation füllen.

Zusätzlich kann, bei eingetragener php-Datumsnotation, die Ausgabe des Datums mit $date_output = 0; unterdrückt werden.


// CUSTOM VAR ========================== +KH: 27.01.11
// Article date created or arcticle date changed (+KH 25.04.2011)
// Set in conf_template_default.inc.php e.g: $template_default['related']['date_format'] = 'd.m.Y';
// or set up the var $date_form
    
    $date_output = 2;     // [0|1|2] No date | date created | date changed  // +KH: 25.04.11
    $date_form = 'd.m.Y'; // 'Y/m/d';
    $date_form_before = '<span style="font-size:70%; color: grey;"> [';
    $date_form_after  = ']</span>';
    
// ===================================== 

// CUSTOM VAR ========================== +KH: 27.01.11
// Article date created or arcticle date changed (+KH 25.04.2011)
// Set in conf_template_default.inc.php e.g: $template_default['related']['date_format'] = 'd.m.Y';
// or set up the var $date_form
    
    $date_output = 0;     // [0|1|2] No date | date created | date changed  // +KH: 25.04.11
    $date_form = 'd.m.Y'; // 'Y/m/d';
    $date_form_before = ' [';
    $date_form_after  = ']';
    
// ===================================== 


{RELATEDX:count:key words:category-ID}

<?php
/*************************************************************************************
   Copyright notice
 
   (c) 2002-2011 Oliver Georgi (oliver@phpwcms.de) // All rights reserved.
 
   This script is part of PHPWCMS. The PHPWCMS web content management system is
   free software; you can redistribute it and/or modify it under the terms of
   the GNU General Public License as published by the Free Software Foundation;
   either version 2 of the License, or (at your option) any later version.
 
   The GNU General Public License can be found at http://www.gnu.org/copyleft/gpl.html
   A copy is found in the textfile GPL.txt and important notices to the license
   from the author is found in LICENSE.txt distributed with these scripts.
 
   This script is distributed in the hope that it will be useful, but WITHOUT ANY
   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
   PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 
   This copyright notice MUST APPEAR in all copies of the script!
 
-------------------------------------------------------------------------------------
V 1.0
15.03.09 KH (flip-flop) RT RELATED cut out (frontend snippet)
   {RELATEDX:count:key word´s:category-ID}
...Please always fill out all fields: e.g.
   {RELATEDX:0:tip,top,flip,flop:0}  -> Search beginning behind cad-ID=0 = home
   {RELATEDX:5:tip,top,flip,flop:2}  -> Output first 5 entries, search beginning behind cad-ID=2
 
V 1.1
20.01.11 KH - Update: Last version don´t work right in newer CMS vrsions (since r400).
 http://code.google.com/p/phpwcms/source/detail?r=400&path=/trunk/include/inc_front/front.func.inc.php
22.01.11 KH : Start category in result
 
V 1.2
27.01.11 KH - Update: Added "article creaded date" -> see CUSTOM VAR
 
V 1.21
25.04.11 KH - Update: Added "article changed date" -> see CUSTOM VAR
                      Added: Output selection switch for article [date creaded | date changed]
 
*************************************************************************************/
// ----------------------------------------------------------------------------------
// obligate check for phpwcms constants
if (!defined('PHPWCMS_ROOT')) {die("You Cannot Access This Script Directly, Have a Nice Day.");}
// ----------------------------------------------------------------------------------
 
 
 
// related articles based on keywords, inspired by Magnar Stav Johanssen
if(strpos($content["all"],'{RELATEDX:') !== false) {
 
// CUSTOM VAR ========================== +KH: 27.01.11
// Article date created or arcticle date changed (+KH 25.04.2011)
// Set in conf_template_default.inc.php e.g: $template_default['related']['date_format'] = 'd.m.Y';
// or set up in var $date_form
 
    $date_output = 2;      // [0|1|2] No date | date created | date changed  // +KH: 25.04.11
    $date_form = 'd.m.Y'; // 'd.m.Y';
    $date_form_before = ' [';
    $date_form_after  = ']';
 
// =====================================
 
    $date_form = (!isset($template_default['related']['date_format'])) ? $date_form : $template_default['related']['date_format'];  // +KH: 27.01.11
 
    if (!$no_content_for_this_page && !empty($content["articles"][$aktion[1]]["article_keyword"])) {
        $related_keywords = $content["articles"][$aktion[1]]["article_keyword"];
    } else {
        $related_keywords = '';
    }
 
// -------------------------------------------------------------
 
 
 
function buildStruct_TopDown($start=0, &$my_cat_id='') {  // KH: V1.1 25.01.2011
 
 
    $struct = getStructureChildData($start);
 
    foreach($struct as $value) {
 
//        buildStruct_TopDown($value['acat_id'],$my_cat_id);
        $my_cat_id .= ($my_cat_id != '') ? ','.$value['acat_id'] : $value['acat_id'];
        buildStruct_TopDown($value['acat_id'],$my_cat_id);
 
//        echo  '||'.$value['acat_id'].' ,';
 
    }
 
return $my_cat_id;
 
}
 
// =================================================================
 
function my_get_related_articles($keywords='', $current_article_id=0, $template_default='', $max_cnt_links=10, $dbcon ,$cat_id='', $date_output, $date_form, $date_form_before, $date_form_after) {
    // find keyword for current article used for RELATED replacementtag
    // prepared and inspired by Magnar Stav Johanssen
 
    $temp = buildStruct_TopDown($cat_id);           // find all permitted IDs behind $cat_id
 
    if (!empty($temp)) $cat_id = $cat_id.','.$temp;  // +KH: 20.01.11 add start category
// PATCH01
//    $cat_id = $temp;                               // If you don´t need the start cat in result
 
 
    $keyword_links = "";
    $max_cnt_links = intval($max_cnt_links);
 
    $keywords = str_replace("ALLKEYWORDS", $GLOBALS['content']['all_keywords'].',', $keywords);
 
    // replace unwanted chars and convert to wanted
    $keywords = str_replace(";", ",", $keywords);
    $keywords = str_replace("'", "", $keywords);
    $keywords = str_replace(" ", ",", $keywords);
    $keywords = str_replace(",,", ",", $keywords);
 
    // choose comma separated keywords
    $keywordarray = explode (",", $keywords);
    $keywordarray = array_map('trim', $keywordarray);
    $keywordarray = array_diff($keywordarray, array(''));
    $keywordarray = array_unique($keywordarray);
    $keywordarray = array_map('strtoupper', $keywordarray);
    // check for empty keywords or keywords smaller than 3 chars
    if(is_array($keywordarray) && count($keywordarray)) {
        foreach($keywordarray as $key => $value) {
 
            if(substr($keywordarray[$key], 0, 1) == '-') {
                $doNotUse = substr($keywordarray[$key], 1);
                foreach($keywordarray as $key2 => $value2) {
                    if($doNotUse == $value2) {
                        unset($keywordarray[$key2]);
                        unset($keywordarray[$key]);
                    }
                }
            }
 
            if(isset($keywordarray[$key]) && (strlen($keywordarray[$key]) < 3 || empty($keywordarray[$key]))) {
                unset($keywordarray[$key]);
            }
        }
    }
 
    if(is_array($keywordarray) && count($keywordarray)) {
        $where = "";
        foreach($keywordarray as $value) {
                //build where keyword = blabla
                $where .= ($where) ? " OR " : "";
                //replace every "'" to "''" for security reasons with aporeplace()
                $where .= "article_keyword LIKE '%".aporeplace($value)."%'";
        }
        $limit = ($max_cnt_links) ? " LIMIT ".$max_cnt_links : "";
// -KH:200111
//        $sql  =    "SELECT article_id, article_title, article_cid, article_subtitle, article_summary, article_alias ";
// +KH:200111
        $sql  =    "SELECT article_id, article_title, article_cid, article_subtitle, article_summary, article_alias, article_redirect, article_morelink, article_created, UNIX_TIMESTAMP(article_tstamp) as article_changed ";
        $sql .=    "FROM ".DB_PREPEND."phpwcms_article WHERE article_deleted=0 AND ";
        $sql .=    "article_id<>".intval($current_article_id)." AND ";
        $sql .=    "article_cid IN (".$cat_id.") AND ";
        // VISIBLE_MODE: 0 = frontend (all) mode, 1 = article user mode, 2 = admin user mode
        switch(VISIBLE_MODE) {
            case 0: $sql .=    "article_public=1 AND article_aktiv=1 AND ";
                    break;
            case 1: $sql .= "article_uid=".$_SESSION["wcs_user_id"]." AND ";
                    break;
            //case 2: admin mode no additional neccessary
        }
        $sql .=    "article_begin < NOW() AND article_end > NOW() AND (".$where.") ";
 
        if(empty($template_default['sort_by'])) $template_default['sort_by'] = '';
 
        switch($template_default['sort_by']) {
 
            case 'title_asc':
                        $sql .=    "ORDER BY article_title";
                        break;
 
            case 'title_desc':
                        $sql .=    "ORDER BY article_title DESC";
                        break;
 
            case 'ldate_asc':
                        $sql .=    "ORDER BY article_begin";
                        break;
 
            case 'ldate_desc':
                        $sql .=    "ORDER BY article_begin DESC";
                        break;
 
            case 'kdate_asc':
                        $sql .=    "ORDER BY article_end";
                        break;
 
            case 'kdate_desc':
                        $sql .=    "ORDER BY article_end DESC";
                        break;
 
            case 'cdate_asc':
                        $sql .=    "ORDER BY article_created";
                        break;
 
            case 'cdate_desc':
                        $sql .=    "ORDER BY article_created DESC";
                        break;
 
            default:
                        $sql .=    "ORDER BY article_tstamp DESC";
        }
 
        $sql .= $limit;
 
        // related things
        $target = $template_default["link_target"] ? ' target="'.$template_default["link_target"].'"' : '';
        $result = _dbQuery($sql);
        if(isset($result[0])) {
            foreach($result as $row) {
                if(empty($row['article_redirect'])) {
                    if(empty($row['article_morelink'])) {
                        continue;
                    }
                    $article_link = 'index.php?'.setGetArticleAid($row).'"'.$target;
                } else {
                    $redirect = get_redirect_link($row['article_redirect'], ' ', '');
                    $article_link = $redirect['link'].'"'.$redirect['target'];
                }
 
                if($template_default["link_length"] && strlen($row['article_title']) > $template_default["link_length"]) {
                    $article_title = substr($row['article_title'], 0, $template_default["link_length"]).$template_default["cut_title_add"];
                } else {
                    $article_title = $row['article_title'];
                }
                $keyword_links .= $template_default["link_before"];
                $keyword_links .= $template_default["link_symbol"];
                $keyword_links .= '<a href="' . $article_link . '>';
                $keyword_links .= html_specialchars($article_title);
 
                if (!empty($date_form)) {
                    switch($date_output) { // [0|1|2] No date | date created | date last change  // +KH: 25.04.11
                        case 1: $keyword_links .= $date_form_before.date($date_form, $row['article_created']).$date_form_after;
                        break;
 
                        case 2: $keyword_links .= $date_form_before.date($date_form, $row['article_changed']).$date_form_after;
                        break;
 
                        default: $keyword_links .= '';
                    }
//                    $keyword_links .= $date_form_before.date($date_form, $row['article_created']).$date_form_after; // +KH: 27.01.11
                }
 
                $keyword_links .= '</a>' . $template_default["link_after"];
            }
        }
    }
 
    // parses all system RTs  +KH 25.01.2011
    $keyword_links = html_parser($keyword_links);
 
    //enclose whole
    return (empty($keyword_links)) ? '' : $template_default["before"].$keyword_links.$template_default["after"];
}
 
 
//    $content["all"] = preg_replace('/\{RELATEDX:(\d+):(.*?):(\d+)\}/e','my_get_related_articles("$2",$GLOBALS["aktion"][1],$GLOBALS["template_default"]["related"],"$1",$GLOBALS["db"], "$3");',$content["all"]);
 
    $content["all"] = preg_replace('/\{RELATEDX:(\d+)\}/e','get_related_articles($related_keywords,$aktion[1],$template_default["related"],"$1",$db);',$content["all"]);
    $content["all"] = preg_replace('/\{RELATEDX:(\d+):(.*?):(\d+)\}/e','my_get_related_articles("$2",$aktion[1],$template_default["related"],"$1",$db, "$3",$date_output,$date_form,$date_form_before, $date_form_after);',$content["all"]);
 
}
 
?>



V 1.3

27.04.11 KH Update: Auswahl ob Schlüsselwörter in Artikeln auch unterhalb von versteckten Kategorien gesucht werden sollen oder nicht.

 $show_hidden      = 1;       // Suche [0] = nicht in versteckten Kategorien  [1] = in allen Kategorien


03.05.11 KH Update: Fehlerkorrektur im Funktionsaufruf zu “buildStruct_TopDown”


// CUSTOM VAR ========================== +KH: 27.01.11
// Article date created or arcticle date changed (+KH 25.04.2011)
// Set in conf_template_default.inc.php e.g: $template_default['related']['date_format'] = 'd.m.Y';
// or set up the var $date_form
    
    $date_output      = 2;       // [0|1|2] No date | date created | date changed  // +KH: 25.04.11
    $date_form        = 'd.m.Y'; // '';
    $date_form_before = '<span style="font-size:70%; color: grey;"> [';
    $date_form_after  = ']</span>';
    $show_hidden      = 1;       // search in [0] = no hidden categories  [1] = all categories  
    
// ===================================== 


{RELATEDX:count:key words:category-ID}

<?php
/*************************************************************************************
   Copyright notice
 
   (c) 2002-2011 Oliver Georgi (oliver@phpwcms.de) // All rights reserved.
 
   This script is part of PHPWCMS. The PHPWCMS web content management system is
   free software; you can redistribute it and/or modify it under the terms of
   the GNU General Public License as published by the Free Software Foundation;
   either version 2 of the License, or (at your option) any later version.
 
   The GNU General Public License can be found at http://www.gnu.org/copyleft/gpl.html
   A copy is found in the textfile GPL.txt and important notices to the license
   from the author is found in LICENSE.txt distributed with these scripts.
 
   This script is distributed in the hope that it will be useful, but WITHOUT ANY
   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
   PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 
   This copyright notice MUST APPEAR in all copies of the script!
 
-------------------------------------------------------------------------------------
V 1.0
15.03.09 KH (flip-flop) RT RELATED cut out (frontend snippet)
   {RELATEDX:count:key word´s:category-ID}
...Please always fill out all fields: e.g.
   {RELATEDX:0:tip,top,flip,flop:0}  -> Search beginning behind cad-ID=0 = home
   {RELATEDX:5:tip,top,flip,flop:2}  -> Output first 5 entries, search beginning behind cad-ID=2
 
V 1.1
20.01.11 KH - Update: Last version don´t work right in newer CMS vrsions (since r400).
 http://code.google.com/p/phpwcms/source/detail?r=400&path=/trunk/include/inc_front/front.func.inc.php
22.01.11 KH : Start category in result
 
V 1.2
27.01.11 KH - Update: Added "article creaded date" -> see CUSTOM VAR
 
V 1.21
25.04.11 KH - Update: Added "article changed date" -> see CUSTOM VAR
                      Added: Output selection switch for article [date creaded | date changed]
V 1.3
27.04.11 KH - Update: Added "show article in hidden categories" -> see CUSTOM VAR
                      Added: Output selection switch for article in [no hidden categories | all categories]
03.05.11 KH - Update: Error correction in the function call "buildStruct_TopDown"
 
 
*************************************************************************************/
// ----------------------------------------------------------------------------------
// obligate check for phpwcms constants
if (!defined('PHPWCMS_ROOT')) {die("You Cannot Access This Script Directly, Have a Nice Day.");}
// ----------------------------------------------------------------------------------
 
 
 
// related articles based on keywords, inspired by Magnar Stav Johanssen
if(strpos($content["all"],'{RELATEDX:') !== false) {
 
// CUSTOM VAR ========================== +KH: 27.01.11
// Article date created or arcticle date changed (+KH 25.04.2011)
// Set in conf_template_default.inc.php e.g: $template_default['related']['date_format'] = 'd.m.Y';
// or set up in var $date_form
 
    $date_output      = 2;       // [0|1|2] No date | date created | date changed  // +KH: 25.04.11
    $date_form        = 'd.m.Y'; // '';
    $date_form_before = '<span style="font-size:70%; color: grey;"> [';
    $date_form_after  = ']</span>';
    $show_hidden      = 1;       // search in [0] = no hidden categories  [1] = all categories
 
// =====================================
 
 
    $date_form = (!isset($template_default['related']['date_format'])) ? $date_form : $template_default['related']['date_format'];  // +KH: 27.01.11
 
    if (!$no_content_for_this_page && !empty($content["articles"][$aktion[1]]["article_keyword"])) {
        $related_keywords = $content["articles"][$aktion[1]]["article_keyword"];
    } else {
        $related_keywords = '';
    }
 
// -------------------------------------------------------------
 
// Function imported from   /include/inc_front/front.func.inc.php ->  line 3305
// Added KH 27.04.2011: Var $show_hidden
function my_getStructureLevelDisplayStatus(&$level_ID, &$current_ID, $show_hidden=1) {
 
    if($GLOBALS['content']['struct'][$level_ID]['acat_struct'] == $current_ID && $level_ID) {
        if($GLOBALS['content']['struct'][$level_ID]['acat_regonly'] && !FEUSER_LOGIN_STATUS) {
            return false;
        }
        // Added KH 27.04.2011: OR xxxx = Show_hidden
        if(empty($GLOBALS['content']['struct'][$level_ID]['acat_hidden']) OR $GLOBALS['content']['struct'][$level_ID]['acat_hidden']==$show_hidden) {
            return true;
        } elseif($GLOBALS['content']['struct'][$level_ID]["acat_hidden"] == 2 && isset($GLOBALS['LEVEL_KEY'][$level_ID])) {
            return true;
        }
        return false;
    }
    return false;
}
 
function buildStruct_TopDown($start=0, &$my_cat_id='',$show_hidden=1) {  // KH: V1.1 25.01.2011
 
        // Original from: function my_getStructureChildData($level_id=0) +KH 27.04.2011
        $struct_data = array();
        if( isset($GLOBALS['content']['struct'][$start]) ) {
            foreach($GLOBALS['content']['struct'] as $key => $value) {
                if( my_getStructureLevelDisplayStatus($key, $start,$show_hidden) ) {
                    $struct_data[$key]    = $value;
                }
            }
        }
        $struct = $struct_data;
 
 
    foreach($struct as $value) {
 
        $my_cat_id .= ($my_cat_id != '') ? ','.$value['acat_id'] : $value['acat_id'];
        buildStruct_TopDown($value['acat_id'],$my_cat_id,$show_hidden);
 
//        echo  '||'.$value['acat_id'].' ,';
 
    }
 
return $my_cat_id;
 
}
 
// =================================================================
 
 
 
function my_get_related_articles($keywords='', $current_article_id=0, $template_default='', $max_cnt_links=10, $dbcon ,$cat_id='', $date_output, $date_form, $date_form_before, $date_form_after,$show_hidden) {
    // find keyword for current article used for RELATED replacementtag
    // prepared and inspired by Magnar Stav Johanssen
 
    $dummy = '';
    $temp = buildStruct_TopDown($cat_id,$dummy,$show_hidden);    // find all permitted IDs behind $cat_id
 
    if (!empty($temp)) $cat_id = $cat_id.','.$temp;  // +KH: 20.01.11 add start category
// PATCH01
//    $cat_id = $temp;                               // If you don´t need the start cat in result
 
 
    $keyword_links = "";
    $max_cnt_links = intval($max_cnt_links);
 
    $keywords = str_replace("ALLKEYWORDS", $GLOBALS['content']['all_keywords'].',', $keywords);
 
    // replace unwanted chars and convert to wanted
    $keywords = str_replace(";", ",", $keywords);
    $keywords = str_replace("'", "", $keywords);
    $keywords = str_replace(" ", ",", $keywords);
    $keywords = str_replace(",,", ",", $keywords);
 
    // choose comma separated keywords
    $keywordarray = explode (",", $keywords);
    $keywordarray = array_map('trim', $keywordarray);
    $keywordarray = array_diff($keywordarray, array(''));
    $keywordarray = array_unique($keywordarray);
    $keywordarray = array_map('strtoupper', $keywordarray);
    // check for empty keywords or keywords smaller than 3 chars
    if(is_array($keywordarray) && count($keywordarray)) {
        foreach($keywordarray as $key => $value) {
 
            if(substr($keywordarray[$key], 0, 1) == '-') {
                $doNotUse = substr($keywordarray[$key], 1);
                foreach($keywordarray as $key2 => $value2) {
                    if($doNotUse == $value2) {
                        unset($keywordarray[$key2]);
                         unset($keywordarray[$key]);
                    }
                }
            }
 
            if(isset($keywordarray[$key]) && (strlen($keywordarray[$key]) < 3 || empty($keywordarray[$key]))) {
                unset($keywordarray[$key]);
            }
        }
    }
 
    if(is_array($keywordarray) && count($keywordarray)) {
        $where = "";
         foreach($keywordarray as $value) {
            //build where keyword = blabla
            $where .= ($where) ? " OR " : "";
            //replace every "'" to "''" for security reasons with aporeplace()
            $where .= "article_keyword LIKE '%".aporeplace($value)."%'";
        }
        $limit = ($max_cnt_links) ? " LIMIT ".$max_cnt_links : "";
// -KH:200111
//         $sql  =    "SELECT article_id, article_title, article_cid, article_subtitle, article_summary, article_alias ";
// +KH:200111
        $sql  =    "SELECT article_id, article_title, article_cid, article_subtitle, article_summary, article_alias, article_redirect, article_morelink, article_created, UNIX_TIMESTAMP(article_tstamp) AS article_changed ";
        $sql .=    "FROM ".DB_PREPEND."phpwcms_article WHERE article_deleted=0 AND ";
        $sql .=    "article_id<>".intval($current_article_id)." AND ";
         $sql .=    "article_cid IN (".$cat_id.") AND ";
        // VISIBLE_MODE: 0 = frontend (all) mode, 1 = article user mode, 2 = admin user mode
        switch(VISIBLE_MODE) {
            case 0: $sql .=    "article_public=1 AND article_aktiv=1 AND ";
                    break;
                case 1: $sql .= "article_uid=".$_SESSION["wcs_user_id"]." AND ";
                    break;
            //case 2: admin mode no additional neccessary
 
        }
        $sql .=    "article_begin < NOW() AND article_end > NOW() AND (".$where.") ";
 
        if(empty($template_default['sort_by'])) $template_default['sort_by'] = '';
 
        switch($template_default['sort_by']) {
 
            case 'title_asc':
                    $sql .=    "ORDER BY article_title";
                    break;
 
            case 'title_desc':
                    $sql .=    "ORDER BY article_title DESC";
                    break;
 
            case 'ldate_asc':
                    $sql .=    "ORDER BY article_begin";
                    break;
 
            case 'ldate_desc':
                    $sql .=    "ORDER BY article_begin DESC";
                    break;
 
            case 'kdate_asc':
                    $sql .=    "ORDER BY article_end";
                    break;
 
            case 'kdate_desc':
                    $sql .=    "ORDER BY article_end DESC";
                    break;
 
            case 'cdate_asc':
                    $sql .=    "ORDER BY article_created";
                    break;
 
                case 'cdate_desc':
                    $sql .=    "ORDER BY article_created DESC";
                    break;
 
            default:
                    $sql .=    "ORDER BY article_tstamp DESC";
        }
 
        $sql .= $limit;
 
        // related things
        $target = $template_default["link_target"] ? ' target="'.$template_default["link_target"].'"' : '';
        $result = _dbQuery($sql);
        if(isset($result[0])) {
            foreach($result as $row) {
// echo 'Article ===========';
 
                if(empty($row['article_redirect'])) {
                    if(empty($row['article_morelink'])) {
                        continue;
                    }
                    $article_link = 'index.php?'.setGetArticleAid($row).'"'.$target;
                } else {
                    $redirect = get_redirect_link($row['article_redirect'], ' ', '');
                    $article_link = $redirect['link'].'"'.$redirect['target'];
                }
 
                if($template_default["link_length"] && strlen($row['article_title']) > $template_default["link_length"]) {
                    $article_title = substr($row['article_title'], 0, $template_default["link_length"]).$template_default["cut_title_add"];
                } else {
                    $article_title = $row['article_title'];
                }
                $keyword_links .= $template_default["link_before"];
                $keyword_links .= $template_default["link_symbol"];
                $keyword_links .= '<a href="' . $article_link . '>';
                $keyword_links .= html_specialchars($article_title);
                if (!empty($date_form)) {
                    switch($date_output) { // [0|1|2] No date | date created | date last change  // +KH: 25.04.11
                        case 1: $keyword_links .= $date_form_before.date($date_form, $row['article_created']).$date_form_after;
                        break;
 
                        case 2: $keyword_links .= $date_form_before.date($date_form, $row['article_changed']).$date_form_after;
                        break;
 
                        default: $keyword_links .= '';
                    }
//                    $keyword_links .= $date_form_before.date($date_form, $row['article_created']).$date_form_after; // +KH: 27.01.11
                }
 
                $keyword_links .= '</a>' . $template_default["link_after"];
            }
        }
    }
 
    // parses all system RTs  +KH 25.01.2011
    $keyword_links = html_parser($keyword_links);
 
    //enclose whole
    return (empty($keyword_links)) ? '' : $template_default["before"].$keyword_links.$template_default["after"];
}
 
 
 
    $content["all"] = preg_replace('/\{RELATEDX:(\d+)\}/e','get_related_articles($related_keywords,$aktion[1],$template_default["related"],"$1",$db);',$content["all"]);
    $content["all"] = preg_replace('/\{RELATEDX:(\d+):(.*?):(\d+)\}/e','my_get_related_articles("$2",$aktion[1],$template_default["related"],"$1",$db, "$3",$date_output,$date_form,$date_form_before, $date_form_after, $show_hidden);',$content["all"]);
 
 
}
 
?>



V 1.31

27.09.12 KH Update: Sortiert die Artikel innerhalb der Kategorie nach manueller Reihenfolge.

In der Datei config\phpwcms\conf.template_default.inc.php im Bereich // related articles based on keywords setze bitte

 $template_default['related']['sort_by']      = 'sort_asc|sort_desc';     


{RELATEDX:count:key words:category-ID}

<?php
/*************************************************************************************
   Copyright notice
 
   (c) 2002-2011 Oliver Georgi (oliver@phpwcms.de) // All rights reserved.
 
   This script is part of PHPWCMS. The PHPWCMS web content management system is
   free software; you can redistribute it and/or modify it under the terms of
   the GNU General Public License as published by the Free Software Foundation;
   either version 2 of the License, or (at your option) any later version.
 
   The GNU General Public License can be found at http://www.gnu.org/copyleft/gpl.html
   A copy is found in the textfile GPL.txt and important notices to the license
   from the author is found in LICENSE.txt distributed with these scripts.
 
   This script is distributed in the hope that it will be useful, but WITHOUT ANY
   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
   PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 
   This copyright notice MUST APPEAR in all copies of the script!
 
-------------------------------------------------------------------------------------
V 1.0
15.03.09 KH (flip-flop) RT RELATED cut out (frontend snippet)
   {RELATEDX:count:key word´s:category-ID}
...Please always fill out all fields: e.g.
   {RELATEDX:0:tip,top,flip,flop:0}  -> Search beginning behind cad-ID=0 = home
   {RELATEDX:5:tip,top,flip,flop:2}  -> Output first 5 entries, search beginning behind cad-ID=2
 
V 1.1
20.01.11 KH - Update: Last version don´t work right in newer CMS vrsions (since r400).
 http://code.google.com/p/phpwcms/source/detail?r=400&path=/trunk/include/inc_front/front.func.inc.php
22.01.11 KH : Start category in result
 
V 1.2
27.01.11 KH - Update: Added "article creaded date" -> see CUSTOM VAR
 
V 1.21
25.04.11 KH - Update: Added "article changed date" -> see CUSTOM VAR
                      Added: Output selection switch for article [date creaded | date changed]
V 1.3
27.04.11 KH - Update: Added "show article in hidden categories" -> see CUSTOM VAR
                      Added: Output selection switch for article in [no hidden categories | all categories]
03.05.11 KH - Update: Error correction in the function call "buildStruct_TopDown"
 
V 1.31
27.09.12 KH - Update: Added: Sorts the articles in the category for manual order.
 
 
*************************************************************************************/
// ----------------------------------------------------------------------------------
// obligate check for phpwcms constants
if (!defined('PHPWCMS_ROOT')) {die("You Cannot Access This Script Directly, Have a Nice Day.");}
// ----------------------------------------------------------------------------------
 
 
 
// related articles based on keywords, inspired by Magnar Stav Johanssen
if(strpos($content["all"],'{RELATEDX:') !== false) {
 
// CUSTOM VAR ========================== +KH: 27.01.11
// Article date created or arcticle date changed (+KH 25.04.2011)
// Set in conf_template_default.inc.php e.g: $template_default['related']['date_format'] = 'd.m.Y';
// or set up in var $date_form
 
    $date_output      = 2;       // [0|1|2] No date | date created | date changed  // +KH: 25.04.11
    $date_form        = 'd.m.Y'; // '';
    $date_form_before = '<span style="font-size:70%; color: grey;"> [';
    $date_form_after  = ']</span>';
    $show_hidden      = 1;       // search in [0] = no hidden categories  [1] = all categories
 
// =====================================
 
 
    $date_form = (!isset($template_default['related']['date_format'])) ? $date_form : $template_default['related']['date_format'];  // +KH: 27.01.11
 
    if (!$no_content_for_this_page && !empty($content["articles"][$aktion[1]]["article_keyword"])) {
        $related_keywords = $content["articles"][$aktion[1]]["article_keyword"];
    } else {
        $related_keywords = '';
    }
 
// -------------------------------------------------------------
 
// Function imported from   /include/inc_front/front.func.inc.php ->  line 3305
// Added KH 27.04.2011: Var $show_hidden
function my1_getStructureLevelDisplayStatus(&$level_ID, &$current_ID, $show_hidden=1) {
 
    if($GLOBALS['content']['struct'][$level_ID]['acat_struct'] == $current_ID && $level_ID) {
        if($GLOBALS['content']['struct'][$level_ID]['acat_regonly'] && !FEUSER_LOGIN_STATUS) {
            return false;
        }
        // Added KH 27.04.2011: OR xxxx = Show_hidden
        if(empty($GLOBALS['content']['struct'][$level_ID]['acat_hidden']) OR $GLOBALS['content']['struct'][$level_ID]['acat_hidden']==$show_hidden) {
            return true;
        } elseif($GLOBALS['content']['struct'][$level_ID]["acat_hidden"] == 2 && isset($GLOBALS['LEVEL_KEY'][$level_ID])) {
            return true;
        }
        return false;
    }
    return false;
}
 
function buildStruct_TopDown($start=0, &$my_cat_id='',$show_hidden=1) {  // KH: V1.1 25.01.2011
 
        // Original from: function my_getStructureChildData($level_id=0) +KH 27.04.2011
        $struct_data = array();
        if( isset($GLOBALS['content']['struct'][$start]) ) {
            foreach($GLOBALS['content']['struct'] as $key => $value) {
                if( my1_getStructureLevelDisplayStatus($key, $start,$show_hidden) ) {
                    $struct_data[$key]    = $value;
                }
            }
        }
        $struct = $struct_data;
 
 
    foreach($struct as $value) {
 
        $my_cat_id .= ($my_cat_id != '') ? ','.$value['acat_id'] : $value['acat_id'];
        buildStruct_TopDown($value['acat_id'],$my_cat_id,$show_hidden);
 
//        echo  '||'.$value['acat_id'].' ,';
 
    }
 
return $my_cat_id;
 
}
 
// =================================================================
 
 
 
function my_get_related_articles($keywords='', $current_article_id=0, $template_default='', $max_cnt_links=10, $dbcon ,$cat_id='', $date_output, $date_form, $date_form_before, $date_form_after,$show_hidden) {
    // find keyword for current article used for RELATED replacementtag
    // prepared and inspired by Magnar Stav Johanssen
 
    $dummy = '';
    $temp = buildStruct_TopDown($cat_id,$dummy,$show_hidden);    // find all permitted IDs behind $cat_id
 
    if (!empty($temp)) $cat_id = $cat_id.','.$temp;  // +KH: 20.01.11 add start category
// PATCH01
//    $cat_id = $temp;                               // If you don´t need the start cat in result
 
    $keyword_links = "";
    $max_cnt_links = intval($max_cnt_links);
 
    $keywords = str_replace("ALLKEYWORDS", $GLOBALS['content']['all_keywords'].',', $keywords);
 
    // replace unwanted chars and convert to wanted
    $keywords = str_replace(";", ",", $keywords);
    $keywords = str_replace("'", "", $keywords);
    $keywords = str_replace(" ", ",", $keywords);
    $keywords = str_replace(",,", ",", $keywords);
 
    // choose comma separated keywords
    $keywordarray = explode (",", $keywords);
    $keywordarray = array_map('trim', $keywordarray);
    $keywordarray = array_diff($keywordarray, array(''));
    $keywordarray = array_unique($keywordarray);
    $keywordarray = array_map('strtoupper', $keywordarray);
    // check for empty keywords or keywords smaller than 3 chars
    if(is_array($keywordarray) && count($keywordarray)) {
        foreach($keywordarray as $key => $value) {
 
            if(substr($keywordarray[$key], 0, 1) == '-') {
                $doNotUse = substr($keywordarray[$key], 1);
                foreach($keywordarray as $key2 => $value2) {
                    if($doNotUse == $value2) {
                        unset($keywordarray[$key2]);
                         unset($keywordarray[$key]);
                    }
                }
            }
 
            if(isset($keywordarray[$key]) && (strlen($keywordarray[$key]) < 3 || empty($keywordarray[$key]))) {
                unset($keywordarray[$key]);
            }
        }
    }
 
    if(is_array($keywordarray) && count($keywordarray)) {
        $where = "";
         foreach($keywordarray as $value) {
            //build where keyword = blabla
            $where .= ($where) ? " OR " : "";
            //replace every "'" to "''" for security reasons with aporeplace()
            $where .= "article_keyword LIKE '%".aporeplace($value)."%'";
        }
        $limit = ($max_cnt_links) ? " LIMIT ".$max_cnt_links : "";
// -KH:200111
//         $sql  =    "SELECT article_id, article_title, article_cid, article_subtitle, article_summary, article_alias ";
// +KH:200111
        $sql  =    "SELECT article_id, article_title, article_cid, article_subtitle, article_summary, article_alias, article_redirect, article_morelink, article_created, UNIX_TIMESTAMP(article_tstamp) AS article_changed ";
        $sql .=    "FROM ".DB_PREPEND."phpwcms_article WHERE article_deleted=0 AND ";
        $sql .=    "article_id<>".intval($current_article_id)." AND ";
        $sql .=    "article_cid IN (".$cat_id.") AND ";
        // VISIBLE_MODE: 0 = frontend (all) mode, 1 = article user mode, 2 = admin user mode
        switch(VISIBLE_MODE) {
            case 0: $sql .=    "article_public=1 AND article_aktiv=1 AND ";
                    break;
                case 1: $sql .= "article_uid=".$_SESSION["wcs_user_id"]." AND ";
                    break;
            //case 2: admin mode no additional neccessary
 
        }
        $sql .=    "article_begin < NOW() AND article_end > NOW() AND (".$where.") ";
 
        if(empty($template_default['sort_by'])) $template_default['sort_by'] = '';
 
        switch($template_default['sort_by']) {
 
            // +KH:27.09.12  Sorts the articles in the category for manual order.
            case 'sort_asc':
                    $sql .=    "ORDER BY article_cid, article_sort";
                    break;
            case 'sort_desc':
                    $sql .=    "ORDER BY article_cid, article_sort DESC";
                    break;
            // ----------
 
            case 'title_asc':
                    $sql .=    "ORDER BY article_title";
                    break;
 
            case 'title_desc':
                    $sql .=    "ORDER BY article_title DESC";
                    break;
 
            case 'ldate_asc':
                    $sql .=    "ORDER BY article_begin";
                    break;
 
            case 'ldate_desc':
                    $sql .=    "ORDER BY article_begin DESC";
                    break;
 
            case 'kdate_asc':
                    $sql .=    "ORDER BY article_end";
                    break;
 
            case 'kdate_desc':
                    $sql .=    "ORDER BY article_end DESC";
                    break;
 
            case 'cdate_asc':
                    $sql .=    "ORDER BY article_created";
                    break;
 
                case 'cdate_desc':
                    $sql .=    "ORDER BY article_created DESC";
                    break;
 
            default:
                    $sql .=    "ORDER BY article_tstamp DESC";
        }
 
        $sql .= $limit;
 
        // related things
        $target = $template_default["link_target"] ? ' target="'.$template_default["link_target"].'"' : '';
        $result = _dbQuery($sql);
        if(isset($result[0])) {
            foreach($result as $row) {
// echo 'Article ===========';
 
                if(empty($row['article_redirect'])) {
                    if(empty($row['article_morelink'])) {
                        continue;
                    }
                    $article_link = 'index.php?'.setGetArticleAid($row).'"'.$target;
                } else {
                    $redirect = get_redirect_link($row['article_redirect'], ' ', '');
                    $article_link = $redirect['link'].'"'.$redirect['target'];
                }
 
                if($template_default["link_length"] && strlen($row['article_title']) > $template_default["link_length"]) {
                    $article_title = substr($row['article_title'], 0, $template_default["link_length"]).$template_default["cut_title_add"];
                } else {
                    $article_title = $row['article_title'];
                }
                $keyword_links .= $template_default["link_before"];
                $keyword_links .= $template_default["link_symbol"];
                $keyword_links .= '<a href="' . $article_link . '>';
                $keyword_links .= html_specialchars($article_title);
                if (!empty($date_form)) {
                    switch($date_output) { // [0|1|2] No date | date created | date last change  // +KH: 25.04.11
                        case 1: $keyword_links .= $date_form_before.date($date_form, $row['article_created']).$date_form_after;
                        break;
 
                        case 2: $keyword_links .= $date_form_before.date($date_form, $row['article_changed']).$date_form_after;
                        break;
 
                        default: $keyword_links .= '';
                    }
//                    $keyword_links .= $date_form_before.date($date_form, $row['article_created']).$date_form_after; // +KH: 27.01.11
                }
 
                $keyword_links .= '</a>' . $template_default["link_after"];
            }
        }
    }
 
    // parses all system RTs  +KH 25.01.2011
    $keyword_links = html_parser($keyword_links);
 
    //enclose whole
    return (empty($keyword_links)) ? '' : $template_default["before"].$keyword_links.$template_default["after"];
}
 
 
 
    $content["all"] = preg_replace('/\{RELATEDX:(\d+)\}/e','get_related_articles($related_keywords,$aktion[1],$template_default["related"],"$1",$db);',$content["all"]);
    $content["all"] = preg_replace('/\{RELATEDX:(\d+):(.*?):(\d+)\}/e','my_get_related_articles("$2",$aktion[1],$template_default["related"],"$1",$db, "$3",$date_output,$date_form,$date_form_before, $date_form_after, $show_hidden);',$content["all"]);
 
 
}
 
?>



deutsch/ersetzer_rts/frontend_render/relatedx.txt · Last modified: 2018/06/03 18:09 (external edit)
www.planmatrix.de www.chimeric.de Creative Commons License Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0