News : "Browse By Category" patch

La discussione sul CMS Made Simple in italiano.

Moderator: magallo

Post Reply
andrea

News : "Browse By Category" patch

Post by andrea »

Salve a tutti,
lavorando con CMSMS mi capita spesso di doverlo modificare per adattarlo meglio alle mie esigenze,
appena terminata questa modifica mi son detto, "beh, questa sembra davvero utile, ma quanto sei bravo!!!  ;D "
sfogliando qua e la il forum alla ricerca di un metodo che mi premetesse di navigare nelle news per categorie non ho trovato molto.
Allora ho creato questa patch che aggiunge al tag {news} il parametro browsecat che se impostato ad 1 genera una lista con tutte le categorie, il numero di news per ogni categoria e il link per visualizzare le news di quella categoria. Ovviamente il tutto basato su template.

Prima di postarla sullo spazio ufficiale del modulo volevo chiedervi cosa ne pensate (magari esiste già qualcosa di simile non l'ho trovato).

Patch da inserire nel file action.default.php (da iserire prima di if (isset($params["makerssbutton"])) riga 26 circa)

Code: Select all

if(isset($params['browsecat']) && $params['browsecat']==1){
		$depth=1;
		$query = "SELECT news_category_id, news_category_name, hierarchy, long_name FROM ".cms_db_prefix()."module_news_categories WHERE hierarchy not like ? ";
		if (isset($params["category"]) && $params["category"] != '')
		{
			$categories = explode(',', $params['category']);
            $query .= " AND (";
			$count = 0;
			foreach ($categories as $onecat)
			{
				if ($count > 0)
				{
					$query .= ' OR ';
				}
				if (strpos($onecat, '|') !== FALSE || strpos($onecat, '*') !== FALSE)
					$query .= "upper(long_name) like upper('" . trim(str_replace('*', '%', $onecat)) . "')";
				else
					$query .= "news_category_name = '" . trim($onecat) . "'";
				$count++;
			}
			$query .= ") ";
		}

		$query .=" ORDER by hierarchy";

		$dbresult = $db->Execute($query, array(''));
		$rowcounter=0;
		while ($dbresult && $row = $dbresult->FetchRow())
		{
		$q2 = "SELECT COUNT(news_id) as cnt FROM ".cms_db_prefix()."module_news WHERE news_category_id= ?";
		$dbres2 = $db->Execute($q2,array($row['news_category_id']));
		$count = $dbres2->FetchRow();
		$row['index']=$rowcounter++;
		$row['count']= $count['cnt'];
		$row['prevdepth'] = $depth;
		$depth= count(split('\.', $row['hierarchy']));
		$row['depth']=$depth;
		$row['url'] = $this->CreateLink($id,'default',$detailpage!=''?$detailpage:$returnid,$row['news_category_name'],array('category'=>$row['news_category_name'],'browsecat'=>'-1'),'',true);
		$items[] = $row;
		}
		#Display template
		//echo "<pre>";print_r($items);echo "</pre>";
		$this->smarty->assign('count', count($items));
		$this->smarty->assign('cats', $items);
		echo $this->ProcessTemplate('browsecategory.tpl');
return;
}
e questo è il file browsecategory.tpl da inserire nella cartella template

Code: Select all

{if $count > 0}
<ul class="list1">
{foreach from=$cats item=node}
{if $node.depth > $node.prevdepth}
	{repeat string="<ul>" times=$node.depth-$node.prevdepth}
{elseif $node.depth < $node.prevdepth}
	{repeat string="</li></ul>" times=$node.prevdepth-$node.depth}
</li>
{elseif $node.index > 0}</li>
{/if}

<li class="newscategory">

{if $node.count > 0}<a href="{$node.url}">{$node.news_category_name}({$node.count})</a>{else}<span>{$node.news_category_name} (nessun annuncio)</span>{/if}

{/foreach}
{repeat string="</li></ul>" times=$node.depth-1}</li>
</ul>
{/if}
Spero sia utile, fatemi sapere.
Post Reply

Return to “Italian - Italiano”