• Apr
  • 2012
  • 09
  • Categoria: Joomla
  • Scritto da Andrea Rossi

Come visualizzare a fondo articolo, i link per altri articoli della medesima categoria. Ovvero, come sostituire a quei anonimi "successivo" e "precedente" il titolo degli articoli linkati.

Override template: Articolo precedente e successivo

Funziona sujoomla 2.5

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.

Grazie alle prossime istruzioni, potremo visualizzare a fondo articolo, i titoli degli articoli "vicini". Per capirci, quando leggiamo un articolo in joomla, abbiamo due link <prec succ> per visualizzare l'articolo precedente/successivo; due nomi estremamente anonimi. Sarebbe più interessante/utile fornire i titoli degli articoli linkati.

Queste modifiche migliorano l'utilizzo da parte degli utenti del vostro portale, in quanto la loro scelta di cliccare o meno, si baserà sul titolo dell'articolo.

File da Salvare

Scaricate questa cartella root/plugins/content/pagenavigation. Fate due copie: una di backup ed una seconda in cui lavoreremo.

Quale effetto vogliamo ottenere

Stampare i link all'articolo successivo/precedente, visualizzandone il titolo.

File da modificare

Lavoriamo all'interno della cartella pagination, sul file pagination.php.

1 Recupero informazioni

Cercate questa stringa (attorno alla linea numero 119)

 $query->select('a.id,'.$case_when.','.$case_when1);
aggiungete a.title qui
$query->select('a.id,'.$case_when.','.$case_when1);
In questo modo, avete aggiunto alla query, la colonna a.title. Senza questo passaggio è inutile cercare di reperire dalla query, una informazione mai cercata.

2 Recupero informazioni

Scorrete il file sino alla riga 140, circa

            $location = array_search($uid, array_keys($list));

            $rows = array_values($list);

            $row->prev = null;
            $row->next = null;

            if ($location -1 >= 0)    {
                // The previous content item cannot be in the array position -1.
                $row->prev = $rows[$location -1];
            }
            if (($location +1) < count($rows)) {
                // The next content item cannot be in an array position greater than the number of array postions.
                $row->next = $rows[$location +1];
            }
ed aggiungete queste righe
$row->prev_title = null;
$row->next_title = null;
...
$row->prev_title=$row->prev->title;
...
$row->next_title=$row->prev->title;
In modo da ottenere complessivamente queste modifiche
            $location = array_search($uid, array_keys($list));

            $rows = array_values($list);

            $row->prev = null;
            $row->next = null;
            $row->prev_title = null;
            $row->next_title = null;

            if ($location -1 >= 0)    {
                // The previous content item cannot be in the array position -1.
                $row->prev = $rows[$location -1];
                $row->prev_title=$row->prev->title;
            }

            if (($location +1) < count($rows)) {
                // The next content item cannot be in an array position greater than the number of array postions.
                $row->next = $rows[$location +1];
                $row->next_title=$row->next->title;
            }

 
Abbiamo caricato la struttura $row, di nuovi dettagli, pronti per essere visualizzati.

3 Visualizzazione

Giungete alla linea 178, si trova il commento //Output

            // Output.
            if ($row->prev || $row->next) {
                $html = '<ul class="pagenav">';
                if ($row->prev) {
                    $html .= '<li class="pagenav-prev"><a href="'. $row->prev .'" rel="prev">'. JText::_('JGLOBAL_LT') . $pnSpace . JText::_('JPREV') . '</a></li>';
                }
                if ($row->next) {
                    $html .= '<li class="pagenav-next"><a href="'. $row->next .'" rel="next">' . JText::_('JNEXT') . $pnSpace . JText::_('JGLOBAL_GT') .'</a></li>';
                }
                $html .= '</ul>';
Attuate queste semplici modifiche
<li class="pagenav-prev" style="width:40%;float:left;text-align:left;margin:-10px 0px;"><a href="'. $row->prev .'" rel="prev">'. JText::_('JGLOBAL_LT') . $pnSpace .$row->prev_title.'</a></li>
...
<li class="pagenav-next" style="width:40%;float:right;text-align:right;margin:-10px 0px;"><a href="'. $row->next .'" rel="next">'.$row->next_title. $pnSpace . JText::_('JGLOBAL_GT') .'</a></li>
per ottener questo risultato
            // Output.
            if ($row->prev || $row->next) {
                $html = '<ul class="pagenav" style="width:100%;clear:both;padding:0px; margin:50px 0px;padding:16px 0px;">';
                if ($row->prev) {
                    $html .= '
                    <li class="pagenav-prev" style="width:40%;float:left;text-align:left;margin:-10px 0px;"><a href="'
. $row->prev .'" rel="prev">'. JText::_('JGLOBAL_LT') . $pnSpace .$row->prev_title.'</a></li>';
                }
                if ($row->next) {
                    $html .= '
                    <li class="pagenav-next" style="width:40%;float:right;text-align:right;margin:-10px 0px;"><a href="'
. $row->next .'" rel="next">'.$row->next_title. $pnSpace . JText::_('JGLOBAL_GT') .'</a></li>';
                }
                $html .= '</ul>';
Caricate tutto il file, nella cartella online allo stesso indirizzo root/plugins/content/pagenavigation.

Risultato finale

Ecco il codice per intero

  1. <?php
  2. /**
  3.  * @copyright    Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
  4.  * @license        GNU General Public License version 2 or later; see LICENSE.txt
  5.  */
  6.  
  7. // No direct access
  8. defined('_JEXEC') or die;
  9.  
  10. /**
  11.  * Pagenavigation plugin class.
  12.  *
  13.  * @package        Joomla.Plugin
  14.  * @subpackage    Content.pagenavigation
  15.  */
  16. class plgContentPagenavigation extends JPlugin
  17. {
  18.     /**
  19.      * @since    1.6
  20.      */
  21.     public function onContentBeforeDisplay($context, &amp;$row, &amp;$params, $page=0)
  22.     {
  23.         $view = JRequest::getCmd('view');
  24.         $print = JRequest::getBool('print');
  25.  
  26.         if ($print) {
  27.             return false;
  28.         }
  29.  
  30.         if ($params->get('show_item_navigation') &amp;&amp; ($context == 'com_content.article') &amp;&amp; ($view == 'article')) {
  31.             $html = '';
  32.             $db        = JFactory::getDbo();
  33.             $user    = JFactory::getUser();
  34.             $app    = JFactory::getApplication();
  35.             $lang    = JFactory::getLanguage();
  36.             $nullDate = $db->getNullDate();
  37.  
  38.             $date    = JFactory::getDate();
  39.             $config    = JFactory::getConfig();
  40.             $now = $date->toSql();
  41.  
  42.             $uid    = $row->id;
  43.             $option    = 'com_content';
  44.             $canPublish = $user->authorise('core.edit.state', $option.'.article.'.$row->id);
  45.  
  46.             // The following is needed as different menu items types utilise a different param to control ordering.
  47.             // For Blogs the `orderby_sec` param is the order controlling param.
  48.             // For Table and List views it is the `orderby` param.
  49.             $params_list = $params->toArray();
  50.             if (array_key_exists('orderby_sec', $params_list)) {
  51.                 $order_method = $params->get('orderby_sec', '');
  52.             } else {
  53.                 $order_method = $params->get('orderby', '');
  54.             }
  55.             // Additional check for invalid sort ordering.
  56.             if ($order_method == 'front') {
  57.                 $order_method = '';
  58.             }
  59.  
  60.             // Determine sort order.
  61.             switch ($order_method) {
  62.                 case 'date' :
  63.                     $orderby = 'a.created';
  64.                     break;
  65.                 case 'rdate' :
  66.                     $orderby = 'a.created DESC';
  67.                     break;
  68.                 case 'alpha' :
  69.                     $orderby = 'a.title';
  70.                     break;
  71.                 case 'ralpha' :
  72.                     $orderby = 'a.title DESC';
  73.                     break;
  74.                 case 'hits' :
  75.                     $orderby = 'a.hits';
  76.                     break;
  77.                 case 'rhits' :
  78.                     $orderby = 'a.hits DESC';
  79.                     break;
  80.                 case 'order' :
  81.                     $orderby = 'a.ordering';
  82.                     break;
  83.                 case 'author' :
  84.                     $orderby = 'a.created_by_alias, u.name';
  85.                     break;
  86.                 case 'rauthor' :
  87.                     $orderby = 'a.created_by_alias DESC, u.name DESC';
  88.                     break;
  89.                 case 'front' :
  90.                     $orderby = 'f.ordering';
  91.                     break;
  92.                 default :
  93.                     $orderby = 'a.ordering';
  94.                     break;
  95.             }
  96.  
  97.             $xwhere = ' AND (a.state = 1 OR a.state = -1)' .
  98.             ' AND (publish_up = '.$db->Quote($nullDate).' OR publish_up <= '.$db->Quote($now).')' .
  99.             ' AND (publish_down = '.$db->Quote($nullDate).' OR publish_down >= '.$db->Quote($now).')';
  100.  
  101.             // Array of articles in same category correctly ordered.
  102.             $query    = $db->getQuery(true);
  103.            //sqlsrv changes
  104.             $case_when = ' CASE WHEN ';
  105.             $case_when .= $query->charLength('a.alias');
  106.             $case_when .= ' THEN ';
  107.             $a_id = $query->castAsChar('a.id');
  108.             $case_when .= $query->concatenate(array($a_id, 'a.alias'), ':');
  109.             $case_when .= ' ELSE ';
  110.             $case_when .= $a_id.' END as slug';
  111.  
  112.             $case_when1 = ' CASE WHEN ';
  113.             $case_when1 .= $query->charLength('cc.alias');
  114.             $case_when1 .= ' THEN ';
  115.             $c_id = $query->castAsChar('cc.id');
  116.             $case_when1 .= $query->concatenate(array($c_id, 'cc.alias'), ':');
  117.             $case_when1 .= ' ELSE ';
  118.             $case_when1 .= $c_id.' END as catslug';
  119.               $query->select('a.id,a.title,'.$case_when.','.$case_when1);
  120.             $query->from('#__content AS a');
  121.             $query->leftJoin('#__categories AS cc ON cc.id = a.catid');
  122.             $query->where('a.catid = '. (int)$row->catid .' AND a.state = '. (int)$row->state
  123.                         . ($canPublish ? '' : ' AND a.access = ' .(int)$row->access) . $xwhere);
  124.             $query->order($orderby);
  125.             if ($app->isSite() &amp;&amp; $app->getLanguageFilter()) {
  126.                 $query->where('a.language in ('.$db->quote($lang->getTag()).','.$db->quote('*').')');
  127.             }
  128.  
  129.             $db->setQuery($query);
  130.             $list = $db->loadObjectList('id');
  131.  
  132.             // This check needed if incorrect Itemid is given resulting in an incorrect result.
  133.             if (!is_array($list)) {
  134.                 $list = array();
  135.             }
  136.  
  137.             reset($list);
  138.  
  139.             // Location of current content item in array list.
  140.             $location = array_search($uid, array_keys($list));
  141.  
  142.             $rows = array_values($list);
  143.  
  144.             $row->prev = null;
  145.             $row->next = null;
  146.             $row->prev_title = null;
  147.             $row->next_title = null;
  148.  
  149.             if ($location -1 >= 0)    {
  150.                 // The previous content item cannot be in the array position -1.
  151.                 $row->prev = $rows[$location -1];
  152.                 $row->prev_title=$row->prev->title;
  153.             }
  154.  
  155.             if (($location +1) < count($rows)) {
  156.                 // The next content item cannot be in an array position greater than the number of array postions.
  157.                 $row->next = $rows[$location +1];
  158.                 $row->next_title=$row->next->title;
  159.             }
  160.  
  161.             $pnSpace = "";
  162.             if (JText::_('JGLOBAL_LT') || JText::_('JGLOBAL_GT')) {
  163.                 $pnSpace = " ";
  164.             }
  165.  
  166.             if ($row->prev) {
  167.                 $row->prev = JRoute::_(ContentHelperRoute::getArticleRoute($row->prev->slug, $row->prev->catslug));
  168.             } else {
  169.                 $row->prev = '';
  170.             }
  171.  
  172.             if ($row->next) {
  173.                 $row->next = JRoute::_(ContentHelperRoute::getArticleRoute($row->next->slug, $row->next->catslug));
  174.             } else {
  175.                 $row->next = '';
  176.             }
  177.  
  178.             // Output.
  179.             if ($row->prev || $row->next) {
  180.                 $html = '
  181.                 <ul class="pagenav">'
  182.                 ;
  183.                 if ($row->prev) {
  184.                     $html .= '
  185.                     <li class="pagenav-prev"><a href="'. $row->prev .'" rel="prev">'. JText::_('JGLOBAL_LT') . $pnSpace .$row->prev_title.'</a></li>'
  186.                     ;
  187.                 }
  188.  
  189.  
  190.  
  191.                 if ($row->next) {
  192.                     $html .= '
  193.                     <li class="pagenav-next"><a href="'. $row->next .'" rel="next">'.$row->next_title. $pnSpace . JText::_('JGLOBAL_GT') .'</a></li>'
  194.                     ;
  195.                 }
  196.                 $html .= '
  197.                 </ul>'
  198.                 ;
  199.  
  200.                 $row->pagination = $html;
  201.                 $row->paginationposition = $this->params->get('position', 1);
  202.                 // This will default to the 1.5 and 1.6-1.7 behavior.
  203.                 $row->paginationrelative = $this->params->get('relative',0);
  204.             }
  205.         }
  206.  
  207.         return ;
  208.     }
  209. }

  • Dettagli

  • Condividi