ArticleRecursiveCollection

Gesperrt
v_r
Beiträge: 43
Registriert: Sa 23. Jul 2005, 15:48
Kontaktdaten:

ArticleRecursiveCollection

Beitrag von v_r » Mo 13. Mär 2006, 10:32

Guten Morgen Leute,

ich habe am Wochenende mal die ArticleCollection abgeleitet. Dabei ist eine Klasse mit dem Namen "ArticleRecursiveCollection" herrausgekommen. Diese verhält sich wie ArticleCollection, jedoch mit der Möglichkeit auch Unterkategorien in die Suche einzubeziehen. Das hat den Vorteil, dass man direkt auch die Sortierung, etc. auf alle Artikel beziehen kann.

Da ich das Teil nicht eichfach so hier kommetarlos reinballern will, bei Interess bitte bei mir mit PM melden.

V_R
Zuletzt geändert von v_r am Mo 13. Mär 2006, 11:50, insgesamt 1-mal geändert.
die neusten contenido-Projekte

http://www.koenig-pilsener-arena.de - Integration von Contenido und Tomcat
http://www.loreley-open-air.de - einfach nur Contenido

Halchteranerin
Beiträge: 5478
Registriert: Di 2. Mär 2004, 21:11
Wohnort: Halchter, wo sonst? ;-)
Kontaktdaten:

Beitrag von Halchteranerin » Mo 13. Mär 2006, 10:43

Irgendwie ist die Haelfte von dem einen Satz verschuett gegangen. ;-)
Aber kannst du das nicht hier veroeffentlichen und entsprechend kommentieren?
Bitte keine unaufgeforderten Privatnachrichten mit Hilfegesuchen schicken. WENN ich helfen kann, dann mache ich das im Forum, da ich auch alle Postings lese. PN werden nicht beantwortet!

v_r
Beiträge: 43
Registriert: Sa 23. Jul 2005, 15:48
Kontaktdaten:

Beitrag von v_r » Mo 13. Mär 2006, 11:52

Ok, da ist was daneben gegangen. Ich habe den Text vervollständing.

Soll ich das Teil wirklich hier pasten? Attachements gehen nicht, oder?

V_R
die neusten contenido-Projekte

http://www.koenig-pilsener-arena.de - Integration von Contenido und Tomcat
http://www.loreley-open-air.de - einfach nur Contenido

Halchteranerin
Beiträge: 5478
Registriert: Di 2. Mär 2004, 21:11
Wohnort: Halchter, wo sonst? ;-)
Kontaktdaten:

Beitrag von Halchteranerin » Mo 13. Mär 2006, 12:26

Attachments gehen leider nicht. Und ich persoenlich finde es sinnvoller, das heir zu posten, als einen Link auf eine Datei irgendwo auf einem Server einzubauen, denn erfahrungsgemaess verschwinden solche Dateien irgendwann wieder, und dann aergert man sich nur, weil es genau das ist, was man schon seit langer Zeit gesucht hat. :wink:
Bitte keine unaufgeforderten Privatnachrichten mit Hilfegesuchen schicken. WENN ich helfen kann, dann mache ich das im Forum, da ich auch alle Postings lese. PN werden nicht beantwortet!

v_r
Beiträge: 43
Registriert: Sa 23. Jul 2005, 15:48
Kontaktdaten:

Beitrag von v_r » Do 16. Mär 2006, 13:48

Ok, dann hier mal der Code. es gibt noch 2 Unzulänglichkeiten.

a) die Klasse "Article" beinhaltet leider keine Info über die zugehörige Kategorie.
b) es werden auch geschützte Inhalte angezeigt.

Frage: Wie kommt man von der Klasse Article an die Kategorie-ID?

Code: Alles auswählen

<?php

/**
 * Contenido API Extention - Article Object Recusive Collection
 *
 * This class is used to manage multiple Contenido
 * article objects in a collection.
 * 
 * The constructor awaits an associative array
 * as parameter with the following schema:
 * 
 * array( string paramname => mixed value ); 
 *
 * The parameter idcat is required: array('idcat'=>n)
 *
 * Legal parameter names are: 
 *
 *  idcat  	  - Contenido Category Id
 *  lang 	  - Language Id, active language if ommited
 *  client 	  - Client Id, active client if ommited
 *  start 	  - include start article in the collection, defaults to false
 *  artspecs  - Array of article specifications, which should be considered
 *  order 	  - articles will be orderered by this article property, defaults to 'created'
 *  direction - Order direcion, 'asc' or 'desc' for ascending/descending, defaults to 'asc'
 *  limit	  - Limit numbers of articles in collection
 *  level	  - 
 *
 * You can easy create article lists/teasers with this class.
 * 
 * To create an article list of category 4 (maybe a news category) use:
 * 
 * $myList = new ArticleRecursiveCollection(array("idcat"=>4);
 *
 * while ($article = $myList->nextArticle())
 * {
 *    // Fetch the first headline
 *    $headline = $article->getContent('htmlhead', 1);	
 *	  $idart    = $article->getField('idart');
 *
 *    // Create a link     
 *	  echo '<a href="front_content.php?idcat='.$myList->idcat.'&idart='.$idart.'">'.$headline.'</a><br/>';
 * }
 * 
 * Based on ArticleCollection
 *
 * @package Contenido API Extention
 * @version 0.1
 *
 * @author Volker Richert <richert@addmore.de>
 * @copyright addmore GbR <www.addmore.de>
 */
class ArticleRecursiveCollection extends ArticleCollection
{
    /**
     * Level of depths of recursion
     * @var int
     */
    var $iLevel;

    /**
     * Article Recursive Collection Constructor
     *
     * @param array Options array with schema array("option"=>"value");
     *  idcat (required) - Contenido Category Id
     *  lang - Language Id, active language if ommited
     *  client - Client Id, active client if ommited
	 *  artspecs  - Array of article specifications, which should be considered
     *  start - include start article in the collection, defaults to false
     *  order - articles will be orderered by this property, defaults to 'created'
     *  direction - Order direcion, 'asc' or 'desc' for ascending/descending
     *  limit - Limit numbers of articles in collection
     *  level - 
     *
     * @return void
     */
    function ArticleRecursiveCollection($options)
    {
        global $cfg;
        
        $this->tab = $cfg['tab'];
        $this->db = new DB_Contenido;
        
        if (!is_numeric($options["idcat"]))
        {
            return 'idcat has to be defined';
        }
        
        $this->_setObjectProperties($options);
        $this->_getArticlesByCatId($this->idcat);
    }
    
    /**
     * Set the Object properties
     *
     * @param array Options array with schema array("option"=>"value");
     *  idcat (required) - Contenido Category Id
     *  lang - Language Id, active language if ommited
     *  client - Client Id, active client if ommited
	 *	artspecs  - Array of article specifications, which should be considered
     *  start - include start article in the collection, defaults to false
     *  order - articles will be ordered by this property, defaults to 'created'
     *  direction - Order direcion, 'ASC' or 'DESC' for ascending/descending
	 *  limit - Limit numbers of articles in collection
     *
     * @access private
     * @return void
     */
    function _setObjectProperties($options)
    {
        global $client, $lang;
        
	parent::_setObjectProperties($options);
	$this->iLevel     = (array_key_exists('level',  $options))    ? $options['level']      : 0; // all subcategories
    }
    
    /**
     * Extracts all articles from a specified
     * category id incldung sub categories up to level "iLevel"
     * and stores them in the internal article array
     *
     * @param int Category Id
     *        int level
     *
     * @access private
     * @return array
     */
    function &_getSubCategorieIds($parent, $level)
    {
    	global $cfg;

        $db = new db_contenido;
        $ids = array();
        if ($level < $this->iLevel || $this->iLevel == 0) {
            $sql = "SELECT
                        A.idcat
                    FROM
                        ".$cfg["tab"]["cat_tree"]." AS A,
                        ".$cfg["tab"]["cat"]." AS B,
                        ".$cfg["tab"]["cat_lang"]." AS C
                    WHERE
                        A.idcat     = B.idcat   AND
                        B.idcat     = C.idcat   AND
                        B.idclient  = '$this->client' AND
                        C.idlang    = '$this->lang'   AND
                        C.visible   = '1'       AND
                        B.parentid  = '".$parent."'";

            $db->query($sql);

            while ($db->next_record()) {
                $id = $db->f('idcat');
                $ids[] = $id;
		$subids = $this->_getSubCategorieIds($id, $level+1);
                $ids = array_merge($ids, $subids);

            }
        }
        return $ids;
    }

    /**
     * Extracts all articles from a specified
     * category id incldung sub categories up to level "iLevel"
     * and stores them in the internal article array
     *
     * @param int Category Id
     *
     * @access private
     * @return void
     */
    function _getArticlesByCatId($idcat)
    {   
        $subids = $this->_getSubCategorieIds($idcat, 1);

        $subids[] = $idcat; // add top level cat
	$this->_getArticlesByCatIds($subids);
    }

    /**
     * Extracts all articles from a specified
     * category id array and stores them in the
     * internal article array
     *
     * @param int Category Id
     *
     * @access private
     * @return void
     */
    function _getArticlesByCatIds($aIds)
    {
    	global $cfg;
    	
        if (count($aIds) == 0) {
             $this->count = 0;
             return;
        }
    	if ($cfg["is_start_compatible"] == true)
    	{
            $sql = 'SELECT
                        a.idart,
                        c.idcat,
                        c.is_start
                    FROM
                        '.$this->tab['art_lang'].' AS a,
                        '.$this->tab['art'].' AS b,
                        '.$this->tab['cat_art'].' AS c
                    WHERE
                        c.idcat in (\''.implode("','", $aIds).'\') AND
                        b.idclient = '.$this->client.' AND
                        b.idart = c.idart AND
                        a.idart = b.idart AND
                        a.idlang = '.$this->lang.'';
    	} else {
    		
			$sArtSpecs = (count($this->artspecs) > 0) ? " a.artspec IN ('".implode("','", $this->artspecs)."') AND " : '';
    		
		$sql = 'SELECT
                    a.idart,
                    c.idcat,
		    a.idartlang,
		    c.is_start
                FROM
                    '.$this->tab['art_lang'].' AS a,
                    '.$this->tab['art'].' AS b,
                    '.$this->tab['cat_art'].' AS c
                WHERE
                    c.idcat in (\''.implode("','", $aIds).'\') AND
                    b.idclient = '.$this->client.' AND
                    b.idart = c.idart AND
                    a.idart = b.idart AND
					'.$sArtSpecs.'
                    a.idlang = '.$this->lang.'';   		
    	}

        if (!$this->offline)
        {
            $sql .= ' AND a.online = 1 ';
        }

        $sql .= ' ORDER BY a.'.$this->order.' '.$this->direction.'';

        $this->db->query($sql);

	if ($cfg["is_start_compatible"] == false)
	{
    		$db2 = new DB_Contenido;
        	$sql = "SELECT startidartlang FROM ".$cfg["tab"]["cat_lang"]." WHERE idcat='".$this->idcat."' AND idlang='".$this->lang."'";
        	$db2->query($sql);
        	$db2->next_record();
        	
        	$startidartlang = $db2->f("startidartlang");   			
    		
    		if ($startidartlang != 0)
    		{
        		$sql = "SELECT idart FROM ".$cfg["tab"]["art_lang"]." WHERE idartlang='$startidartlang'";
        		$db2->query($sql);
        		$db2->next_record();
        		$this->startId = $db2->f("idart");
    		}
	}
		
        while ($this->db->next_record())
        {
            if ($cfg["is_start_compatible"] == true)
    	    {
                if ($this->db->f('is_start') == 1)
                {
                    if ($this->db->f("idcat") == $this->idcat)
                    {
                        $this->startId = $this->db->f('idart');
                    
                        if ($this->start)
                        {
                            $this->articles[] = $this->db->f('idart');
                        }
                    }
                }
                else
                {
                    $this->articles[] = $this->db->f('idart');
                }
                
    	    } else {
    			
    		if ($this->db->f("idart") == $this->startId && $this->db->f("idcat") == $this->idcat)
                {   
                    if ($this->start)
                    {
                        $this->articles[] = $this->db->f('idart');
                    }
                }
                else
                {
                    $this->articles[] = $this->db->f('idart');
                }
    	    }
        }

        $this->count = count($this->articles);
    }

} // ArticleRecusiveCollection

?>
die neusten contenido-Projekte

http://www.koenig-pilsener-arena.de - Integration von Contenido und Tomcat
http://www.loreley-open-air.de - einfach nur Contenido

emergence
Beiträge: 10641
Registriert: Mo 28. Jul 2003, 12:49
Wohnort: Austria
Kontaktdaten:

Beitrag von emergence » Do 16. Mär 2006, 15:05

v_r hat geschrieben:a) die Klasse "Article" beinhaltet leider keine Info über die zugehörige Kategorie.
das benötigt die klasse auch nicht...
die klasse article nimmt den idart wert um die inhalte auslesen zu können...

sieh dir die klasse ArticleCollection an... dort findet sich der idcat wert...
Frage: Wie kommt man von der Klasse Article an die Kategorie-ID?
ähm gar nicht...
da deine klasse auf der ArticleCollection beruht... eventuell mittel $this->idcat;
*** make your own tools (wishlist :: thx)

v_r
Beiträge: 43
Registriert: Sa 23. Jul 2005, 15:48
Kontaktdaten:

Beitrag von v_r » Fr 17. Mär 2006, 10:26

Tach auch,
emergence hat geschrieben:
v_r hat geschrieben:a) die Klasse "Article" beinhaltet leider keine Info über die zugehörige Kategorie.
das benötigt die klasse auch nicht...
die klasse article nimmt den idart wert um die inhalte auslesen zu können...

sieh dir die klasse ArticleCollection an... dort findet sich der idcat wert...
Frage: Wie kommt man von der Klasse Article an die Kategorie-ID?
ähm gar nicht...
da deine klasse auf der ArticleCollection beruht... eventuell mittel $this->idcat;
schon klar, aber das ist die falsche Logik. Wenn ich eine Klasse Article habe, ggf. ohne eine Collection also einfach mit "new", dann wäre es logisch, wenn ich die Kategorie abfragen kann. Das wäre aus meiner Sicht die Aufgabe einer Klasse. Und wo ist festgelegt, dass ich eine Collection nur aus einer Kategorie haben darf? Wie wäre es mit einer "LastestChangedArticleCollection"? Aus meiner Sicht gehört die Kategorie zu der ein Artikel gehört in die Article-Klasse.
die neusten contenido-Projekte

http://www.koenig-pilsener-arena.de - Integration von Contenido und Tomcat
http://www.loreley-open-air.de - einfach nur Contenido

lars.v
Beiträge: 5
Registriert: Fr 22. Jul 2005, 09:16
Wohnort: Oldenburg
Kontaktdaten:

Beitrag von lars.v » Fr 9. Mär 2007, 09:28

Vielen Dank,
genau diese Erweiterung habe ich gesucht.
Damit die Startartikel der Unterkategorien wahlweise ausgelesen werden können oder nicht ,habe ich ab Zeile 244 (getArticlesByCatIds($aIds) ) ein paar Veränderungen vorgenommen (funktioniert nur über startidartlang).
Meine Lösung läuft, vermutlich gibt es aber bessere Ansätze, deshalb hier

der Code:


Code: Alles auswählen

<?php

/**
 * Contenido API Extention - Article Object Recusive Collection
 *
 * This class is used to manage multiple Contenido
 * article objects in a collection.
 *
 * The constructor awaits an associative array
 * as parameter with the following schema:
 *
 * array( string paramname => mixed value );
 *
 * The parameter idcat is required: array('idcat'=>n)
 *
 * Legal parameter names are:
 *
 *  idcat       - Contenido Category Id
 *  lang      - Language Id, active language if ommited
 *  client      - Client Id, active client if ommited
 *  start      - include start article in the collection, defaults to false
 *  artspecs  - Array of article specifications, which should be considered
 *  order      - articles will be orderered by this article property, defaults to 'created'
 *  direction - Order direcion, 'asc' or 'desc' for ascending/descending, defaults to 'asc'
 *  limit     - Limit numbers of articles in collection
 *  level     -
 *
 * You can easy create article lists/teasers with this class.
 *
 * To create an article list of category 4 (maybe a news category) use:
 *
 * $myList = new ArticleRecursiveCollection(array("idcat"=>4);
 *
 * while ($article = $myList->nextArticle())
 * {
 *    // Fetch the first headline
 *    $headline = $article->getContent('htmlhead', 1);   
 *     $idart    = $article->getField('idart');
 *
 *    // Create a link     
 *     echo '<a href="front_content.php?idcat='.$myList->idcat.'&idart='.$idart.'">'.$headline.'</a><br/>';
 * }
 *
 * Based on ArticleCollection
 *
 * @package Contenido API Extention
 * @version 0.1
 *
 * @author Volker Richert <richert@addmore.de>
 * @copyright addmore GbR <www.addmore.de>
 */
class ArticleRecursiveCollection extends ArticleCollection
{
    /**
     * Level of depths of recursion
     * @var int
     */
    var $iLevel;

    /**
     * Article Recursive Collection Constructor
     *
     * @param array Options array with schema array("option"=>"value");
     *  idcat (required) - Contenido Category Id
     *  lang - Language Id, active language if ommited
     *  client - Client Id, active client if ommited
    *  artspecs  - Array of article specifications, which should be considered
     *  start - include start article in the collection, defaults to false
     *  order - articles will be orderered by this property, defaults to 'created'
     *  direction - Order direcion, 'asc' or 'desc' for ascending/descending
     *  limit - Limit numbers of articles in collection
     *  level -
     *
     * @return void
     */
    function ArticleRecursiveCollection($options)
    {
        global $cfg;
       
        $this->tab = $cfg['tab'];
        $this->db = new DB_Contenido;
       
        if (!is_numeric($options["idcat"]))
        {
            return 'idcat has to be defined';
        }
       
        $this->_setObjectProperties($options);
        $this->_getArticlesByCatId($this->idcat);
    }
   
    /**
     * Set the Object properties
     *
     * @param array Options array with schema array("option"=>"value");
     *  idcat (required) - Contenido Category Id
     *  lang - Language Id, active language if ommited
     *  client - Client Id, active client if ommited
    *   artspecs  - Array of article specifications, which should be considered
     *  start - include start article in the collection, defaults to false
     *  order - articles will be ordered by this property, defaults to 'created'
     *  direction - Order direcion, 'ASC' or 'DESC' for ascending/descending
    *  limit - Limit numbers of articles in collection
     *
     * @access private
     * @return void
     */
    function _setObjectProperties($options)
    {
        global $client, $lang;
       
   		parent::_setObjectProperties($options);
   		$this->iLevel     = (array_key_exists('level',  $options))    ? $options['level']      : 0; // all subcategories
    }
   
    /**
     * Extracts all articles from a specified
     * category id incldung sub categories up to level "iLevel"
     * and stores them in the internal article array
     *
     * @param int Category Id
     *        int level
     *
     * @access private
     * @return array
     */
    function &_getSubCategorieIds($parent, $level)
    {
       global $cfg;
		
        $db = new db_contenido;
        $ids = array();
        if ($level < $this->iLevel || $this->iLevel == 0) {
            $sql = "SELECT
                        A.idcat
                    FROM
                        (".$cfg["tab"]["cat_tree"]." AS A,
                        ".$cfg["tab"]["cat"]." AS B,
                        ".$cfg["tab"]["cat_lang"]." AS C)
                    WHERE
                        A.idcat     = B.idcat   AND
                        B.idcat     = C.idcat   AND
                        B.idclient  = '$this->client' AND
                        C.idlang    = '$this->lang'   AND
                        C.visible   = '1'       AND
                        B.parentid  = '".$parent."'";

            $db->query($sql);

            while ($db->next_record()) {
			
                $id = $db->f('idcat');
				$ids[] = $id;
      			$subids = $this->_getSubCategorieIds($id, $level+1);
                $ids = array_merge($ids, $subids);

            }
        }
        return $ids;
    }

    /**
     * Extracts all articles from a specified
     * category id incldung sub categories up to level "iLevel"
     * and stores them in the internal article array
     *
     * @param int Category Id
     *
     * @access private
     * @return void
     */
    function _getArticlesByCatId($idcat)
    {   
        $subids = $this->_getSubCategorieIds($idcat, 1);

        $subids[] = $idcat; // add top level cat
   		$this->_getArticlesByCatIds($subids);
    }

    /**
     * Extracts all articles from a specified
     * category id array and stores them in the
     * internal article array
     *
     * @param int Category Id
     *
     * @access private
     * @return void
     */
	 
    function _getArticlesByCatIds($aIds)
    {
       global $cfg;

        if (count($aIds) == 0) {
             $this->count = 0;
             return;
        }
       if ($cfg["is_start_compatible"] == true)
       {
            $sql = 'SELECT
                        a.idart,
                        c.idcat,
                        c.is_start
                    FROM
                        '.$this->tab['art_lang'].' AS a,
                        '.$this->tab['art'].' AS b,
                        '.$this->tab['cat_art'].' AS c
                    WHERE
                        c.idcat in (\''.implode("','", $aIds).'\') AND
                        b.idclient = '.$this->client.' AND
                        b.idart = c.idart AND
                        a.idart = b.idart AND
                        a.idlang = '.$this->lang.'';
       } else {
          
         $sArtSpecs = (count($this->artspecs) > 0) ? " a.artspec IN ('".implode("','", $this->artspecs)."') AND " : '';
          
      		$sql = 'SELECT
                    a.idart,
                    c.idcat,
          			a.idartlang,
          			c.is_start
                FROM
                    '.$this->tab['art_lang'].' AS a,
                    '.$this->tab['art'].' AS b,
                    '.$this->tab['cat_art'].' AS c
                WHERE
                    c.idcat in (\''.implode("','", $aIds).'\') AND
                    b.idclient = '.$this->client.' AND
                    b.idart = c.idart AND
                    a.idart = b.idart AND
               '.$sArtSpecs.'
                    a.idlang = '.$this->lang.'';         
       }

        if (!$this->offline)
        {
            $sql .= ' AND a.online = 1 ';
        }

        $sql .= ' ORDER BY a.'.$this->order.' '.$this->direction.'';

        $this->db->query($sql);
  
        while ($this->db->next_record())
        {
	
			if ($cfg["is_start_compatible"] == false)
		   {
				   $db2 = new DB_Contenido;
				   $sql = "SELECT startidartlang FROM ".$cfg["tab"]["cat_lang"]." WHERE idcat='".$this->db->f("idcat")."' AND idlang='".$this->lang."'";
				   $db2->query($sql);
				   $db2->next_record();
				   $startidartlang = $db2->f("startidartlang");
					  
				    if ($startidartlang != 0)
				  {
					  $sql = "SELECT idart FROM ".$cfg["tab"]["art_lang"]." WHERE idartlang='$startidartlang'";
					  $db2->query($sql);
					  $db2->next_record();
					  $this->startId = $db2->f("idart");
				  }
		   }

            if ($cfg["is_start_compatible"] == true)
           {
                if ($this->db->f('is_start') == 1)
                {
                    if ($this->db->f("idcat") == $this->idcat)
                    {
                        $this->startId = $this->db->f('idart');
                   
                        if ($this->start)
                        {
                            $this->articles[] = $this->db->f('idart');
                        }
                    }
                }
                else
                {
                    $this->articles[] = $this->db->f('idart');
                }
               
           } else {

				if ($this->start) 
                {   
                	$this->articles[] = $this->db->f('idart');
                }
                else
                {
					if ($this->startId != $this->db->f('idart')) {
						$this->articles[] = $this->db->f('idart');
					}
                    
                }
           }
        }

        $this->count = count($this->articles);
    }

} // ArticleRecusiveCollection

?>

Gesperrt