Top_Items in Content.tpl (Random-Gen Widget)

Hi Guys,
I’ve done this “widget” for the Top_Items. What it does is pick out randomly 3 of the articles in the Top Items list and displays them on the page in a sort of “widget”.

Here’s the code:
top_items.tpl:


[{assign var=topArticles value=$oView->getTop5ArticleList()}]
	[{assign var=topArticles_keys value=$topArticles->arrayKeys()}]
	[{php}]shuffle($this->_tpl_vars['topArticles_keys']);[{/php}]

	[{section name=counter loop=$topArticles_keys max=3}]
		[{assign var=_key value=$topArticles_keys[counter]}]
		[{assign var=_product value=$topArticles[$_key]}]

		<div class="listitem">
			[{ assign var="sTop5ArtTitle" value="`$_product-&gt;oxarticles__oxtitle-&gt;value` `$_product-&gt;oxarticles__oxvarselect-&gt;value`" }]
			<table>
				<tr>
					<td style="vertical-align:top; width:56px">
						<div class="top5picture">
							<a id="test_Top5Pic_[{$_product->oxarticles__oxid->value}]" href="[{$_product->getMainLink()}]" class="picture">
								<img src="[{$_product->getIconUrl()}]" alt="[{ $sTop5ArtTitle|strip_tags }]" class="float_left">
							</a>
						</div>
					</td>
					<td>
						<div class="top5details">
							<a id="test_Top5Title_[{$_product->oxarticles__oxid->value}]" href="[{$_product->getMainLink()}]" class="title">[{ $sTop5ArtTitle|strip_tags}]</a>
							<br>
							[{oxhasrights ident="SHOWARTICLEPRICE"}]
								[{if $_product->getFPrice()}]
									[{assign var="currency" value=$oView->getActCurrency() }]
									<b id="test_Top5Price_[{$_product->oxarticles__oxid->value}]">[{ $_product->getFPrice() }] [{ $currency->sign}]</b>
								[{/if}]
							[{/oxhasrights}]

							[{*oxhasrights ident="SHOWSHORTDESCRIPTION"}]
								<div class="desc"><i>[{ $_product->oxarticles__oxshortdesc->value }]</i></div>
							[{/oxhasrights*}]
							<div class="clear"></div>
						</div>
					</td>
				</tr>
			</table>
		</div>
	[{/section}]

start.tpl


...
	[{ if $oView->getTop5ArticleList() }]
		<div id="top5articlelist" class="forms float_right">
			<div class="framed-bg" style="width:181px;"><div class="h2">Kennen Sie schon...</div></div>
			<div class="startpage_products box framed" style="width:188px; padding:0px; margin-bottom:0;">
				[{include file="inc/top_items.tpl" }]
				<div class="clear"></div>
			</div>
		</div>
	[{ /if }]
...

Now this works fine on the start.tpl but for some reason, and that is why I’m posting this, it woun’t work when I want to insert it into the content.tpl in the exact same way. :confused:

content.tpl


[{assign var="oContent" value=$oView->getContent()}]
[{assign var="template_title" value=$oContent->oxcontents__oxtitle->value}]
[{include file="_header.tpl" title=$template_title location=$template_title}]

	[{*<h1 id="test_contentHeader" class="boxhead title">[{$template_title}]</h1>*}]
	
	<div id="top5articlelist" class="forms float_right contentview">
		<div class="framed-bg" style="width:181px;"><div class="h2">Bestseller</div></div>
		<div class="startpage_products box framed" style="width:188px; padding:0px; margin-bottom:0;">
			[{include file="inc/top_items.tpl"}]
			<div class="clear"></div>
		</div>
	</div>
	
	<div id="test_contentBody" class="box">[{ oxcontent oxid=$oView->getContentId() }]</div>

[{insert name="oxid_tracker" title=$template_title }]
[{include file="_footer.tpl" }]

Maybe I’m missing something. Any body able to help me out here?

Thx

Hi,
oxubase::getTop5ArticleList checks for $this->_blTop5Action flag, and views/start.php has it, but view/content.php does not. so my sugesstion is to write a module for geting these articles, by extending oxviewconfig


<?php
class top5articlelist extends top5articlelist_parent {
	protected $_aTop5ArticleList = null;
    /**
     * Template variable getter. Returns Top 5 article list.
     * @return array
     */
    public function getTop5ArticleList()
    {
		if ( $this->_aTop5ArticleList === null ) {
			$this->_aTop5ArticleList = false;
			$myConfig = $this->getConfig();
			if ( $myConfig->getConfigParam( 'bl_perfLoadAktion' ) ) {
				// top 5 articles
				$oArtList = oxNew( 'oxarticlelist' );
				$oArtList->loadTop5Articles();
				if ( $oArtList->count() ) {
					$this->_aTop5ArticleList = $oArtList;
				}
			}
		}
        return $this->_aTop5ArticleList;
    }
}

in modules add


oxviewconfig => top5articlelist

and in template use


[{assign var=topArticles value=$oViewConf->getTop5ArticleList()}]

Hm, I just tried that and it blew up the shop. Had to put the mysql backup over it.
I think the problem was that the module entry caused some sort of problem. I wasn’t able to re-enter the admin section to take the module entry out again.

Must have done something wrong.
I placed that code in oxviewconfig right at the bottom, declaring the new class.
That was ok.
Then I entered the admin section and entered the module line into the module text field. After saving it loaded blank :frowning:

What went wrong?

Ok, I solved it with a not so nice tweek.
I bet the way Wanis suggested is by far more correct, but I didn’t get it to work.
What I did now is to just add this in views/content.php

protected $_blTop5Action = true;

This way, as Wanis mentioned, it locates it in $this aswell.

@JeromeC

For something as simple as this, it doesn’t really matter, but the reason why it’s not a good idea to directly change any of the core or view files, like content.php is that you’ll need to check every update to see if OXID have modified that file. If they have, you’ll need to make the changes again.

Once you get the hang of using the OXID module system, it’s very simple to have your own classes extend core and view. In this example, you’d need a one line php file and a corresponding module entry.