-
Categoria: Joomla
-
Scritto da Andrea Rossi
Come configurare il nostro joomla 2.5, per visualizzare nelle ricerche, le immagini degli articoli? 2 File da modificare, un paio di attenzioni da tenere conto et voilà, il gioco è fatto
Funziona su
Girovagando per il forum, mi sono imbattuto in questa discussione, Come visualizzare la thumb del contenuto nei risultati di ricerca. Con l'override ed una piccola modifica per il prelievo dati, il risultato è facilmente ottenibile e molto più gestibile!
Attenzione: questa non è propriamente una tecnica di override, in quanto si riscrive una parte di codice. Pertanto si invita a fare una copia ed a tenerla sul pc in tutta sicurezza.
Quanto segue è la procedura per prelevare i dati, mentre più in basso potrete vedere i codici per lo stile di visualizzazione. Entrambi gli aspetti sono fondamentali per il risultato finale!
Avvertenze
- Consideriamo Thumbnail del'articolo, solamente l'immagine inserita nel campo "immagine articolo intero". Pertanto, se vorrete fare delle modifiche od estendere questa funzionalità, possiamo parlarne sul forum alla discussione qui indicata, oppure nei commenti qui sotto.
- Le immagini che potrete inserire avranno dimensione diversa da quelle usate da me, pertanto fate qualche test ed adattate il codice css/html/php oppure caricate un determinato formato immagine.
Ti ricordo che tutto questo è per poter controllare meglio lo stile e l'integrazione di questa nuova funzionalità, nel tuo sito, senza l'ausilio di plugin potenzialmente dannosi.
Prelievo Dati
File da Salvare
Scaricate questa cartella root/plugins/search/content e fatene due copie: una per il backup ed una su cui andremo a lavorare.
File da Modificare
Cominciamo ritoccando il codice del file root/plugins/search/content/content.php. Ciò che facciamo, è di aggiungere un semplice campo, per il recupero delle informazioni "images".
1 Recupero Informazioni
Raggiungiamo questa riga (153 circa)
$query->select('a.title AS title, a.metadesc, a.metakey, a.created AS created,');
ed aggiungiamo a.images AS images
$query->select('a.title AS title, a.metadesc, a.metakey, a.created AS created,a.images AS images');
2 Recupero Informazioni
Raggiungiamo questa riga (163 circa)
$query->group('a.id, a.title, a.metadesc, a.metakey, a.created, a.introtext, a.fulltext, c.title, a.alias, c.alias, c.id, a.images');
ed aggiungiamo a.images
$query->group('a.id, a.title, a.metadesc, a.metakey, a.created, a.introtext, a.fulltext, c.title, a.alias, c.alias, c.id, a.images');
3 Recupero Informazioni
Raggiungiamo questa riga (209 circa)
$query->select('a.title AS title, a.metadesc, a.metakey, a.created AS created,'
ed aggiungiamo a.images AS images,
$query->select('a.title AS title, a.metadesc, a.metakey, a.created AS created,a.images AS images,'
Caricate tutto il file nella cartella
root/plugins/search/content/content.php.
Verifica Errori
Se dovesse apparire un errore del tipo
Warning: Invalid argument supplied for foreach() in root/plugins/search/content/content.php on line 258
La causa è dovuta alle virgole: ricontrollate bene il codice completo. Con queste modifiche stiamo lavorando con MySql, pertanto sbagliare una virgola tra gli elementi da visualizzare è abbastanza grave (anche se viene rilevato come un warning).
Risultato finale(1/2)
Ecco il codice per intero
<?php
/**
* @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/ // no direct access
defined('_JEXEC') or
die; require_once JPATH_SITE
.'/components/com_content/router.php'; /**
* Content Search plugin
*
* @package Joomla.Plugin
* @subpackage Search.content
* @since 1.6
*/
class plgSearchContent extends JPlugin
{
/**
* @return array An array of search areas
*/
function onContentSearchAreas()
{
'content' => 'JGLOBAL_ARTICLES'
);
return $areas;
} /**
* Content Search method
* The sql must return the following fields that are used in a common display
* routine: href, title, section, created, text, browsernav
* @param string Target search string
* @param string mathcing option, exact|any|all
* @param string ordering option, newest|oldest|popular|alpha|category
* @param mixed An array if the search it to be restricted to areas, null if search all
*/
function onContentSearch($text, $phrase='', $ordering='', $areas=null)
{
$db = JFactory::getDbo();
$app = JFactory::getApplication();
$user = JFactory::getUser();
$groups = implode(',', $user->getAuthorisedViewLevels());
$tag = JFactory::getLanguage()->getTag(); require_once JPATH_SITE.'/components/com_content/helpers/route.php';
require_once JPATH_SITE.'/administrator/components/com_search/helpers/search.php'; $searchText = $text;
}
} $sContent = $this->params->get('search_content', 1);
$sArchived = $this->params->get('search_archived', 1);
$limit = $this->params->def('search_limit', 50); $nullDate = $db->getNullDate();
$now = $date->toSql(); $text = trim($text);
if ($text == '') {
switch ($phrase) {
case 'exact':
$text = $db->Quote('%'.$db->escape($text, true).'%', false);
$wheres2[] = 'a.title LIKE '.$text;
$wheres2[] = 'a.introtext LIKE '.$text;
$wheres2[] = 'a.fulltext LIKE '.$text;
$wheres2[] = 'a.metakey LIKE '.$text;
$wheres2[] = 'a.metadesc LIKE '.$text;
$where = '(' . implode(') OR (', $wheres2) . ')';
break; case 'all':
case 'any':
default:
foreach ($words as $word) {
$word = $db->Quote('%'.$db->escape($word, true).'%', false);
$wheres2[] = 'a.title LIKE '.$word;
$wheres2[] = 'a.introtext LIKE '.$word;
$wheres2[] = 'a.fulltext LIKE '.$word;
$wheres2[] = 'a.metakey LIKE '.$word;
$wheres2[] = 'a.metadesc LIKE '.$word;
$wheres[] = implode(' OR ', $wheres2);
}
$where = '(' . implode(($phrase == 'all' ?
') AND (' : ') OR ('), $wheres) . ')';
break;
} $morder = '';
switch ($ordering) {
case 'oldest':
$order = 'a.created ASC';
break; case 'popular':
$order = 'a.hits DESC';
break; case 'alpha':
$order = 'a.title ASC';
break; case 'category':
$order = 'c.title ASC, a.title ASC';
$morder = 'a.title ASC';
break; case 'newest':
default:
$order = 'a.created DESC';
break;
$query = $db->getQuery(true); // search articles
if ($sContent && $limit > 0)
{
$query->clear();
//sqlsrv changes
$case_when = ' CASE WHEN ';
$case_when .= $query->charLength('a.alias');
$case_when .= ' THEN ';
$a_id = $query->castAsChar('a.id');
$case_when .= $query->concatenate(array($a_id, 'a.alias'), ':');
$case_when .= ' ELSE ';
$case_when .= $a_id.' END as slug'; $case_when1 = ' CASE WHEN ';
$case_when1 .= $query->charLength('c.alias');
$case_when1 .= ' THEN ';
$c_id = $query->castAsChar('c.id');
$case_when1 .= $query->concatenate(array($c_id, 'c.alias'), ':');
$case_when1 .= ' ELSE ';
$case_when1 .= $c_id.' END as catslug'; $query->select('a.title AS title, a.metadesc, a.metakey, a.created AS created,a.images AS images');
$query->select($query->concatenate(array('a.introtext', 'a.fulltext')).' AS text');
$query->select('c.title AS section, '.$case_when.','.$case_when1.', '.'\'2\' AS browsernav'); $query->from('#__content AS a');
$query->innerJoin('#__categories AS c ON c.id=a.catid');
$query->where('('. $where .')' . 'AND a.state=1 AND c.published = 1 AND a.access IN ('.$groups.') '
.'AND c.access IN ('.$groups.') '
.'AND (a.publish_up = '.$db->Quote($nullDate).' OR a.publish_up <= '.$db->Quote($now).') '
.'AND (a.publish_down = '.$db->Quote($nullDate).' OR a.publish_down >= '.$db->Quote($now).')' );
$query->group('a.id, a.title, a.metadesc, a.metakey, a.created, a.introtext, a.fulltext, c.title, a.alias, c.alias, c.id, a.images');
$query->order($order); // Filter by language
if ($app->isSite() && $app->getLanguageFilter()) {
$query->where('a.language in (' . $db->Quote($tag) . ',' . $db->Quote('*') . ')');
$query->where('c.language in (' . $db->Quote($tag) . ',' . $db->Quote('*') . ')');
} $db->setQuery($query, 0, $limit);
$list = $db->loadObjectList();
{
foreach($list as $key => $item)
{
$list[$key]->href = ContentHelperRoute::getArticleRoute($item->slug, $item->catslug);
}
}
$rows[] = $list;
} // search archived content
if ($sArchived && $limit > 0)
{
$searchArchived = JText::_('JARCHIVED'); $query->clear();
//sqlsrv changes
$case_when = ' CASE WHEN ';
$case_when .= $query->charLength('a.alias');
$case_when .= ' THEN ';
$a_id = $query->castAsChar('a.id');
$case_when .= $query->concatenate(array($a_id, 'a.alias'), ':');
$case_when .= ' ELSE ';
$case_when .= $a_id.' END as slug'; $case_when1 = ' CASE WHEN ';
$case_when1 .= $query->charLength('c.alias');
$case_when1 .= ' THEN ';
$c_id = $query->castAsChar('c.id');
$case_when1 .= $query->concatenate(array($c_id, 'c.alias'), ':');
$case_when1 .= ' ELSE ';
$case_when1 .= $c_id.' END as catslug'; $query->select('a.title AS title, a.metadesc, a.metakey, a.created AS created,a.images AS images'
.$query->concatenate(array("a.introtext", "a.fulltext")).' AS text,'
.$case_when.','.$case_when1.', '
.'c.title AS section, \'2\' AS browsernav');
//.'CONCAT_WS("/", c.title) AS section, \'2\' AS browsernav' );
$query->from('#__content AS a');
$query->innerJoin('#__categories AS c ON c.id=a.catid AND c.access IN ('. $groups .')');
$query->where('('. $where .') AND a.state = 2 AND c.published = 1 AND a.access IN ('. $groups
.') AND c.access IN ('. $groups .') '
.'AND (a.publish_up = '.$db->Quote($nullDate).' OR a.publish_up <= '.$db->Quote($now).') '
.'AND (a.publish_down = '.$db->Quote($nullDate).' OR a.publish_down >= '.$db->Quote($now).')' );
$query->order($order);
// Filter by language
if ($app->isSite() && $app->getLanguageFilter()) {
$query->where('a.language in (' . $db->Quote($tag) . ',' . $db->Quote('*') . ')');
$query->where('c.language in (' . $db->Quote($tag) . ',' . $db->Quote('*') . ')');
} $db->setQuery($query, 0, $limit);
$list3 = $db->loadObjectList(); // find an itemid for archived to use if there isn't another one
$item = $app->getMenu()->getItems('link', 'index.php?option=com_content&view=archive', true);
$itemid = isset($item->id) ?
'&Itemid='.$item->id : ''; if (isset($list3))
{
foreach($list3 as $key => $item)
{
$date = JFactory
::getDate($item->created); $created_month = $date->format("n");
$created_year = $date->format("Y");
$list3[$key]->href = JRoute::_('index.php?option=com_content&view=archive&year='
.$created_year.'&month='.$created_month.$itemid);
}
} $rows[] = $list3;
{
foreach($rows as $row)
{
foreach($row as $key => $article) {
if (searchHelper
::checkNoHTML($article, $searchText, array('text', 'title', 'metadesc', 'metakey'))) {
$new_row[] = $article;
}
}
}
}return $results;
Visualizzazione
Ora che abbiamo ricavato i dati, possiamo dedicarci alla loro visualizzazione, pertanto fate il backup di questo file root/component/com_search/views/search/tmpl/default_results.php perchè dovremo lavorarci su.
Struttura HTML
Dobbiamo modificare il contenuto del foreach, pertanto, buttiamo via quello che c'è al suo interno ed inseriamo
<div style="clear:both">
<dt class="result-title">
<span style="font-size:20px">
<?php echo $this->pagination->limitstart + $result->count.'. ';?></span>
<?php
$result_url="";
if ($result->href) :
$result_url="<a href=\"".JRoute
::_
($result->href)."\" target=\"_blank\">".$this->escape($result->title)."</a>";
else:
$result_url=$this->escape($result->title);
endif; echo $result_url;?>
</dt>
<?php /*Is there some images?*/
$images = json_decode($result->images);
$emptyimage=0;
if (isset($images->image_fulltext) and
!empty($images->image_fulltext)) :?>
<div class="result-thumb">
<a href="
<?php echo JRoute
::_
($result->href);?>">
<img src="
<?php echo $images->image_fulltext; ?>" />
</a>
<span style="font-size:10px;">
<?php echo $images->image_fulltext_caption; ?></span>
</div>
<?php else : $emptyimage=1;?>
<?php endif; /*End images*/?>
<div class="result-detail"
<?php if ($emptyimage==1) echo "style=\"width:100%;\"";?>>
<dd class="result-misc">
<?php if ($this->params->get('show_date')) : ?>
&bull;
<?php echo JText
::sprintf('JGLOBAL_CREATED_DATE_ON', $result->created); ?>
<?php endif; ?>
<?php if ($result->section) : ?>
<span class="small
<?php echo $this->pageclass_sfx; ?>">
&bull;
<?php $title = $this->escape($result->section);
$url = '<a href="'.JRoute
::_
(ContentHelperRoute
::getCategoryRoute($result->catslug)).'">'.$title.'</a>';
echo $url;?>
</span>
<?php endif; ?>
</dd>
<dd class="result-text">
<?php echo $result->text; ?>
</dd>
</div>
</div>
<?php endforeach; ?>Per aiutarmi nella costruzione, ho utilizzato la versione base/default del file per stampare gli articoli, in quanto i dati provengono dalla stessa tabella. Il file in questione è
root/component/com_content/views/article/tmpl/default.php più o meno dalla riga 162; cercate la parte relativa alle immagini e vedrete come le gestice. Se confrontate quanto fatto sino ad ora, con quest'ultimo file, noterete delle belle somiglianze!
Effetto Css
Largo alla fantasia, ma io ho optato per qualcosa di semplice
dt.result-title{margin:5px 0px;
padding:5px 0px;
}
div.result-thumb{
float:left;
width:200px;
border-right:2px solid #DDD;
padding-right:10px;
}
div.result-thumb img{
float:left;
width:90%;
height:90%;
display:block;
}
div.result-detail{
float:left;
width:378px;
}
div.result-detail dd{
-webkit-margin-start:10px;
}
L'unica nota per questo semplicissimo script css sono i float, width e webkit: ho voluto posizionare l'immagine a sinistra del contenuto, tutto qui. Mentre per l'ultimo, ho notato un comportamento particolare con chrome, dove aggiunge 40px di margine. Io li ho impostati a 10px, tutto qui.
Risultato finale (2/2)
Ecco il quest'ultimo file per intero
<?php
/**
* @package Joomla.Site
* @subpackage com_search
* @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/ // no direct access
?>
<style>
dt.result-title{
margin:5px 0px;
padding:5px 0px;
}
div.result-thumb{
float:left;
width:200px;
border-right:2px solid #DDD;
padding-right:10px;
}
div.result-thumb img{
float:left;
width:90%;
height:90%;
display:block;
}
div.result-detail{
float:left;
width:378px;
}
div.result-detail dd{
-webkit-margin-start:10px;
}
</style>
<dl class="search-results<?php echo $this->pageclass_sfx; ?>">
<?php foreach($this->results as $result) : ?>
<div style="clear:both">
<dt class="result-title">
<span style="font-size:20px">
<?php echo $this->pagination->limitstart + $result->count.'. ';?></span>
<?php
$result_url="";
if ($result->href) :
$result_url="<a href=\"".JRoute::_($result->href)."\" target=\"_blank\">".$this->escape($result->title)."</a>";
else:
$result_url=$this->escape($result->title);
endif; echo $result_url;?>
</dt>
<?php /*Is there some images?*/
$emptyimage=0;
if (isset($images->image_fulltext) and
!empty($images->image_fulltext)) :?>
<div class="result-thumb">
<a href="<?php echo JRoute::_($result->href);?>">
<img src="<?php echo $images->image_fulltext; ?>" />
</a>
<span style="font-size:10px;"><?php echo $images->image_fulltext_caption; ?></span>
</div>
<?php else : $emptyimage=1;?>
<?php endif; /*End images*/?>
<div class="result-detail"<?php if ($emptyimage==1) echo "style=\"width:100%;\"";?>>
<dd class="result-misc">
<?php if ($this->params->get('show_date')) : ?>
&bull;
<?php echo JText
::sprintf('JGLOBAL_CREATED_DATE_ON', $result->created); ?>
<?php endif; ?>
<?php if ($result->section) : ?>
<span class="small<?php echo $this->pageclass_sfx; ?>">
&bull; <?php $title = $this->escape($result->section);
$url = '<a href="'.JRoute::_(ContentHelperRoute::getCategoryRoute($result->catslug)).'">'.$title.'</a>';
echo $url;?>
</span>
<?php endif; ?>
</dd>
<dd class="result-text">
<?php echo $result->text; ?>
</dd>
</div>
</div>
<?php endforeach; ?>
</dl><div class="pagination">
<?php echo $this->pagination->getPagesLinks(); ?>
</div>
Effetto Finale
Questa volta niente codice, ma una immagine dalla versione, in locale, del mio sito.

Che ne pensate???