Leere Kategorie -> zeige neue Artikel aller Unterkategorien

Hallo

Mein Kunde möchte von mir die Parent-Category-Seiten umgebaut haben. Er möchte dort im Prinzip keine Listung der Unterkategorien haben, sondern soll eine Liste der neuesten Artikel aller Unterkategorien Hauptbestandteil der Seite werden.

Also schreibe ich ein Modul und eine neue list.tpl, doch komme ich jetzt an zwei Stellen nicht wirklich weiter:
1.) Wie kann ich “schön” die Artikel der Unterkategorien erfragen?
enzarticlenewestfromcat.php


class enzarticlenewestfromcat extends oxUBase{

  protected $_sThisTemplate = 'page/list/list_w-out_subcats.tpl';

  public function getNewestArticleListFromCat(){
      $aArticleList = oxNew('oxarticlelist');
      $sQuery = "SELECT * FROM oxv_oxarticles 
                          INNER JOIN oxobject2category ON oxobject2category.OXOBJECTID = oxarticles.OXID
                          WHERE oxv_articles.OXACTIVE = 1 
                            AND oxv_articles.OXPARENTID = '' 
                            AND  ( oxv_articles.OXSTOCK + oxv_articles.OXVARSTOCK ) > 0 
                            
                          ORDER BY oxv_articles.OXINSERT DESC 
                          LIMIT 20 ";
      $aArticleList->selectString($sQuery);
      return $aArticleList->getArray();
  }
 
  public function getTitle(){
      return "Latest 100";
  }
  
  public function render(){
    return parent::render();
  }



}

2.) Rufe ich die Seite auf, so gibts ne Umleitung und im Log steht, daß die Funktion getNewestArticleListFromCat nicht gefunden würde. Bin ich total verbimmelt?

list_w-out_subcats.tpl:


[{oxscript add="$('a.js-external').attr('target', '_blank');"}]
[{oxscript include="js/widgets/oxarticlebox.js" priority=10 }]
[{oxscript add="$( '#content' ).oxArticleBox();"}]

[{assign var="actCategory" value=$oView->getActiveCategory()}]


[{capture append="oxidBlock_sidebar"}]
...

    [{if $oView->hasVisibleSubCats()}]
...
    [{/if}]

    [{if $oView->getArticleList()|@count > 0}]
...
    [{else}]
        <h2 class="sectionHead clear">Neuestes aus [{$actCategory->getTitle()}]</h2>
        [{assign var=parentCategory value=$actCategory->getParentCategory()}]
        [{include file="widget/product/list.tpl" type=grid listId="productList" products=$oView->getNewestArticleListFromCat()}] 
    [{/if}]
    [{insert name="oxid_tracker"}]
[{/capture}]
[{include file="layout/page.tpl" sidebar="Left" tree_path=$oView->getTreePath()}]

Hat da jemand eine Idee dazu? Kann mir hier jemand weiterhelfen?

Grüße
Johannes

Ich habe nun anhand dieses Threads (http://forum.oxid-esales.com/showthread.php?t=5639&highlight=top5+artikel+der+subkategorien) mal mein Konstrukt umgebaut. Der Code des list_w-out_subcats.tpl ändert sich somit wie folgt:


...
 [{else}]
        <h2 class="sectionHead clear">Neuestes aus [{$actCategory->getTitle()}]</h2>
        [{foreach from=$oView->getSubCatList() item=category name=MoreSubCat}]
          [{foreach from=$oView->getNewestArticleList() item=actionproduct name=test_articleList}]
            [{include file="inc/product.tpl" product=$actionproduct testid="action_"|cat:$actionproduct->oxarticles__oxid->value test_Cntr=$smarty.foreach.test_articleList.iteration}]
          [{/foreach}]
        [{/foreach}]  
    [{/if}]
...

Nun bekomme ich leider im Log die Meldung, daß oxSystemComponentException-oxException (time: 2013-09-26 12:47:40): [0]: Function ‘getNewestArticleList’ does not exist or is not accessible! (oeThemeSwitcherAList) bzw oxSystemComponentException-oxException (time: 2013-09-26 12:50:52): [0]: Function ‘getNewestArticleList’ does not exist or is not accessible! (aList) wäre.
Was muß ich tun?

Johannes

Hallo zusammen

Ich würde mich wirklich freuen, wenn jemand hierzu einen Hinweis hätte. Ich kann mir fast nicht vorstellen, daß niemand eine derartige Ansicht auf seinen Parent-Category-Seiten bisher umgesetzt hat!

Da das Log immer wieder ausspuckt, daß er meine Funktion nicht finden kann bin ich mir gerade unsicher, ob “mein Weg” wirklich korrekt ist. Hier mal die aktuelle Version:

enznewfromcat.php


<?php

class enznewfromcat extends alist{

  protected $_sThisTemplate = 'page/list/list_w-out_subcats.tpl';

  public function getNewestArticleListFromCat($cat){
      $aArticleList = oxNew('oxarticlelist');
      $cats = getSubCatsOfCat($cat);
      $sQuery = "SELECT * FROM oxobject2category
                          LEFT JOIN oxarticles ON oxarticles.OXID = oxobject2category.OXOBJECTID
                          WHERE oxarticles.OXACTIVE = 1 
                            AND oxarticles.OXPARENTID = '' 
                            AND  ( oxarticles.OXSTOCK + oxarticles.OXVARSTOCK ) > 0 
                            AND oxobject2category.OXCATNID IN '".$cats."'
                          ORDER BY oxarticles.OXINSERT DESC 
                          LIMIT 20 ";
      $aArticleList->selectString($sQuery);
      return $aArticleList->getArray();
  }
  
  public function getSubCatsOfCat($icat){
    $sQuery = "SELECT OXID FROM oxcategory WHERE OXPARENTID = '".$cat."'";
    $sRes = mysql_query($sQuery);
    $cats = array();
    while($sFe = mysql_fetch_array($sRes)){
      $cats[] = $sFe['OXID'];
    }
    return $cats;
  }
  
  public function render(){
    return parent::render();
  }
}

metadata.php


...
'extend'       => array('alist' => 'enznewfromcat/enznewfromcat'),
...

Bin ich richtig, daß ich alist extende???
Hat jemand eine schöneren Weg die neiuesten 20 Artikel aus allen SubKategorien per SQL abzufragen?

Wäre für Hilfe wirklich dankbar!

Johannes

Ich bin mir zwar nicht sicher, ob es eventuell einfacher geht, aber es funktioniert in der Version 4.7.10 :slight_smile:

Es wird also geprüft, ob eine Kategorie leer ist und wenn das der Fall ist, dann wird geprüft, ob es Unterkategorien gibt.

<?php

if (false) {
    class my_articlelist_parent extends oxArticleList
    {
    }
}

class my_articlelist extends my_articlelist_parent
{
    protected $subCategories;
    protected $selectSubCategories = false;

    protected function _getCategoryCountSelect($sCatId, $aSessionFilter)
    {
        if (!$this->selectSubCategories) {
            $parent = parent::_getCategoryCountSelect($sCatId, $aSessionFilter);
        } else {
            $sArticleTable = getViewName('oxarticles');
            $sO2CView = getViewName('oxobject2category');

            // ----------------------------------
            // filtering ?
            $sFilterSql = '';
            $iLang = oxRegistry::getLang()->getBaseLanguage();
            if ($aSessionFilter && isset($aSessionFilter[$sCatId][$iLang])) {
                $sFilterSql = $this->_getFilterSql($sCatId, $aSessionFilter[$sCatId][$iLang]);
            }

            $parent = "SELECT COUNT(*) FROM $sO2CView as oc left join $sArticleTable
                    ON $sArticleTable.oxid = oc.oxobjectid
                    WHERE " . $this->getBaseObject()->getSqlActiveSnippet() . " and $sArticleTable.oxparentid = ''
                    and oc.oxcatnid IN('" . implode("', '", $this->getSubCategoriesFromCategoryId($sCatId)) . "') $sFilterSql ";

        }

        return $parent;

    }

    protected function _getCategorySelect($sFields, $sCatId, $aSessionFilter)
    {
        $parent = parent::_getCategorySelect($sFields, $sCatId, $aSessionFilter);

        if (count(oxDb::getDb()->getAll($parent)) == 0) {
            if ($this->_sCustomSorting == 'oxv_oxarticles_de.rand asc'
                || $this->_sCustomSorting == 'oxv_oxarticles_de.rand desc'
            ) {
                $this->setCustomSorting($this->customSorting);
            }

            $sArticleTable = getViewName('oxarticles');
            $sO2CView = getViewName('oxobject2category');

            // ----------------------------------
            // sorting
            $sSorting = '';
            if ($this->_sCustomSorting) {
                $sSorting = " {$this->_sCustomSorting} , ";
            }

            // ----------------------------------
            // filtering ?
            $sFilterSql = '';
            $iLang = oxRegistry::getLang()->getBaseLanguage();
            if ($aSessionFilter && isset($aSessionFilter[$sCatId][$iLang])) {
                $sFilterSql = $this->_getFilterSql($sCatId, $aSessionFilter[$sCatId][$iLang]);
            }

            $oDb = oxDb::getDb();

            if (count($this->getSubCategoriesFromCategoryId($sCatId)) > 0) {
                $parent = "SELECT $sFields, $sArticleTable.oxtimestamp FROM $sO2CView as oc left join $sArticleTable
                        ON $sArticleTable.oxid = oc.oxobjectid
                        WHERE " . $this->getBaseObject()->getSqlActiveSnippet() . " and $sArticleTable.oxparentid = ''
                        and oc.oxcatnid IN('" . implode("','", $this->getSubCategoriesFromCategoryId($sCatId))
                    . "') $sFilterSql ORDER BY $sSorting oc.oxpos, oc.oxobjectid ";
                $this->selectSubCategories = true;
            }
        }
        return $parent;
    }


    public function getSubCategoriesFromCategoryId($categoryId)
    {
        if ($this->subCategories == null) {
            $subCategoriesArray = array();

            $sQuery
                = "SELECT OXID
                  FROM `oxcategories`
                  WHERE `OXPARENTID` = '" . $categoryId . "' AND OXACTIVE=1";

            $result = oxDb::getDb(false)->getAll($sQuery);

            foreach ($result as $subCategory) {
                array_push($subCategoriesArray, $subCategory['OXID']);
            }

            $this->subCategories = $subCategoriesArray;
        }

        return $this->subCategories;
    }
}

und dann eben nur noch die metadata.php anpassen

'oxarticlelist'             => 'my_module/application/model/my_articlelist',