Source for file helper.php

Documentation is available at helper.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Site
  4.  * @subpackage  mod_articles_category
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE.txt
  8.  */
  9.  
  10. defined('_JEXEC'or die;
  11.  
  12. $com_path JPATH_SITE.'/components/com_content/';
  13. require_once $com_path.'router.php';
  14. require_once $com_path.'helpers/route.php';
  15.  
  16. JModelLegacy::addIncludePath($com_path '/models''ContentModel');
  17.  
  18. /**
  19.  * Helper for mod_articles_category
  20.  *
  21.  * @package     Joomla.Site
  22.  * @subpackage  mod_articles_category
  23.  */
  24. abstract class ModArticlesCategoryHelper
  25. {
  26.     public static function getList(&$params)
  27.     {
  28.         // Get an instance of the generic articles model
  29.         $articles JModelLegacy::getInstance('Articles''ContentModel'array('ignore_request' => true));
  30.  
  31.         // Set application parameters in model
  32.         $app JFactory::getApplication();
  33.         $appParams $app->getParams();
  34.         $articles->setState('params'$appParams);
  35.  
  36.         // Set the filters based on the module params
  37.         $articles->setState('list.start'0);
  38.         $articles->setState('list.limit'(int) $params->get('count'0));
  39.         $articles->setState('filter.published'1);
  40.  
  41.         // Access filter
  42.         $access !JComponentHelper::getParams('com_content')->get('show_noauth');
  43.         $authorised JAccess::getAuthorisedViewLevels(JFactory::getUser()->get('id'));
  44.         $articles->setState('filter.access'$access);
  45.  
  46.         // Prep for Normal or Dynamic Modes
  47.         $mode $params->get('mode''normal');
  48.         switch ($mode)
  49.         {
  50.             case 'dynamic':
  51.                 $option $app->input->get('option');
  52.                 $view $app->input->get('view');
  53.                 if ($option === 'com_content')
  54.                 {
  55.                     switch($view)
  56.                     {
  57.                         case 'category':
  58.                             $catids array($app->input->getInt('id'));
  59.                             break;
  60.                         case 'categories':
  61.                             $catids array($app->input->getInt('id'));
  62.                             break;
  63.                         case 'article':
  64.                             if ($params->get('show_on_article_page'1))
  65.                             {
  66.                                 $article_id $app->input->getInt('id');
  67.                                 $catid $app->input->getInt('catid');
  68.  
  69.                                 if (!$catid)
  70.                                 {
  71.                                     // Get an instance of the generic article model
  72.                                     $article JModelLegacy::getInstance('Article''ContentModel'array('ignore_request' => true));
  73.  
  74.                                     $article->setState('params'$appParams);
  75.                                     $article->setState('filter.published'1);
  76.                                     $article->setState('article.id'(int) $article_id);
  77.                                     $item $article->getItem();
  78.  
  79.                                     $catids array($item->catid);
  80.                                 }
  81.                                 else
  82.                                 {
  83.                                     $catids array($catid);
  84.                                 }
  85.                             }
  86.                             else {
  87.                                 // Return right away if show_on_article_page option is off
  88.                                 return;
  89.                             }
  90.                             break;
  91.  
  92.                         case 'featured':
  93.                         default:
  94.                             // Return right away if not on the category or article views
  95.                             return;
  96.                     }
  97.                 }
  98.                 else {
  99.                     // Return right away if not on a com_content page
  100.                     return;
  101.                 }
  102.  
  103.                 break;
  104.  
  105.             case 'normal':
  106.             default:
  107.                 $catids $params->get('catid');
  108.                 $articles->setState('filter.category_id.include'(bool) $params->get('category_filtering_type'1));
  109.                 break;
  110.         }
  111.  
  112.         // Category filter
  113.         if ($catids)
  114.         {
  115.             if ($params->get('show_child_category_articles'0&& (int) $params->get('levels'00)
  116.             {
  117.                 // Get an instance of the generic categories model
  118.                 $categories JModelLegacy::getInstance('Categories''ContentModel'array('ignore_request' => true));
  119.                 $categories->setState('params'$appParams);
  120.                 $levels $params->get('levels'1$params->get('levels'19999;
  121.                 $categories->setState('filter.get_children'$levels);
  122.                 $categories->setState('filter.published'1);
  123.                 $categories->setState('filter.access'$access);
  124.                 $additional_catids array();
  125.  
  126.                 foreach ($catids as $catid)
  127.                 {
  128.                     $categories->setState('filter.parentId'$catid);
  129.                     $recursive true;
  130.                     $items $categories->getItems($recursive);
  131.  
  132.                     if ($items)
  133.                     {
  134.                         foreach ($items as $category)
  135.                         {
  136.                             $condition (($category->level $categories->getParent()->level<= $levels);
  137.                             if ($condition)
  138.                             {
  139.                                 $additional_catids[$category->id;
  140.                             }
  141.  
  142.                         }
  143.                     }
  144.                 }
  145.  
  146.                 $catids array_unique(array_merge($catids$additional_catids));
  147.             }
  148.  
  149.             $articles->setState('filter.category_id'$catids);
  150.         }
  151.  
  152.         // Ordering
  153.         $articles->setState('list.ordering'$params->get('article_ordering''a.ordering'));
  154.         $articles->setState('list.direction'$params->get('article_ordering_direction''ASC'));
  155.  
  156.         // New Parameters
  157.         $articles->setState('filter.featured'$params->get('show_front''show'));
  158.         $articles->setState('filter.author_id'$params->get('created_by'""));
  159.         $articles->setState('filter.author_id.include'$params->get('author_filtering_type'1));
  160.         $articles->setState('filter.author_alias'$params->get('created_by_alias'""));
  161.         $articles->setState('filter.author_alias.include'$params->get('author_alias_filtering_type'1));
  162.         $excluded_articles $params->get('excluded_articles''');
  163.  
  164.         if ($excluded_articles)
  165.         {
  166.             $excluded_articles explode("\r\n"$excluded_articles);
  167.             $articles->setState('filter.article_id'$excluded_articles);
  168.             $articles->setState('filter.article_id.include'false)// Exclude
  169.         }
  170.  
  171.         $date_filtering $params->get('date_filtering''off');
  172.         if ($date_filtering !== 'off')
  173.         {
  174.             $articles->setState('filter.date_filtering'$date_filtering);
  175.             $articles->setState('filter.date_field'$params->get('date_field''a.created'));
  176.             $articles->setState('filter.start_date_range'$params->get('start_date_range''1000-01-01 00:00:00'));
  177.             $articles->setState('filter.end_date_range'$params->get('end_date_range''9999-12-31 23:59:59'));
  178.             $articles->setState('filter.relative_date'$params->get('relative_date'30));
  179.         }
  180.  
  181.         // Filter by language
  182.         $articles->setState('filter.language'$app->getLanguageFilter());
  183.  
  184.         $items $articles->getItems();
  185.  
  186.         // Display options
  187.         $show_date $params->get('show_date'0);
  188.         $show_date_field $params->get('show_date_field''created');
  189.         $show_date_format $params->get('show_date_format''Y-m-d H:i:s');
  190.         $show_category $params->get('show_category'0);
  191.         $show_hits $params->get('show_hits'0);
  192.         $show_author $params->get('show_author'0);
  193.         $show_introtext $params->get('show_introtext'0);
  194.         $introtext_limit $params->get('introtext_limit'100);
  195.  
  196.         // Find current Article ID if on an article page
  197.         $option $app->input->get('option');
  198.         $view $app->input->get('view');
  199.  
  200.         if ($option === 'com_content' && $view === 'article')
  201.         {
  202.             $active_article_id $app->input->getInt('id');
  203.         }
  204.         else
  205.         {
  206.             $active_article_id 0;
  207.         }
  208.  
  209.         // Prepare data for display using display options
  210.         foreach ($items as &$item)
  211.         {
  212.             $item->slug $item->id.':'.$item->alias;
  213.             $item->catslug $item->catid $item->catid .':'.$item->category_alias $item->catid;
  214.  
  215.             if ($access || in_array($item->access$authorised))
  216.             {
  217.                 // We know that user has the privilege to view the article
  218.                 $item->link JRoute::_(ContentHelperRoute::getArticleRoute($item->slug$item->catslug));
  219.             }
  220.             else
  221.             {
  222.                 $app  JFactory::getApplication();
  223.                 $menu $app->getMenu();
  224.                 $menuitems $menu->getItems('link''index.php?option=com_users&view=login');
  225.                 if (isset($menuitems[0]))
  226.                 {
  227.                     $Itemid $menuitems[0]->id;
  228.                 }
  229.                 elseif ($app->input->getInt('Itemid'0)
  230.                 {
  231.                     // Use Itemid from requesting page only if there is no existing menu
  232.                     $Itemid $app->input->getInt('Itemid');
  233.                 }
  234.  
  235.                 $item->link JRoute::_('index.php?option=com_users&view=login&Itemid='.$Itemid);
  236.             }
  237.  
  238.             // Used for styling the active article
  239.             $item->active $item->id == $active_article_id 'active' '';
  240.  
  241.             $item->displayDate '';
  242.             if ($show_date)
  243.             {
  244.                 $item->displayDate JHTML::_('date'$item->$show_date_field$show_date_format);
  245.             }
  246.  
  247.             if ($item->catid)
  248.             {
  249.                 $item->displayCategoryLink JRoute::_(ContentHelperRoute::getCategoryRoute($item->catid));
  250.                 $item->displayCategoryTitle $show_category '<a href="'.$item->displayCategoryLink.'">'.$item->category_title.'</a>' '';
  251.             }
  252.             else {
  253.                 $item->displayCategoryTitle $show_category $item->category_title '';
  254.             }
  255.  
  256.             $item->displayHits $show_hits $item->hits '';
  257.             $item->displayAuthorName $show_author $item->author '';
  258.             if ($show_introtext)
  259.             {
  260.                 $item->introtext JHtml::_('content.prepare'$item->introtext'''mod_articles_category.content');
  261.                 $item->introtext self::_cleanIntrotext($item->introtext);
  262.             }
  263.             $item->displayIntrotext $show_introtext self::truncate($item->introtext$introtext_limit'';
  264.             $item->displayReadmore $item->alternative_readmore;
  265.  
  266.         }
  267.  
  268.         return $items;
  269.     }
  270.  
  271.     public static function _cleanIntrotext($introtext)
  272.     {
  273.         $introtext str_replace('<p>'' '$introtext);
  274.         $introtext str_replace('</p>'' '$introtext);
  275.         $introtext strip_tags($introtext'<a><em><strong>');
  276.  
  277.         $introtext trim($introtext);
  278.  
  279.         return $introtext;
  280.     }
  281.  
  282.     /**
  283.     * Method to truncate introtext
  284.     *
  285.     * The goal is to get the proper length plain text string with as much of
  286.     * the html intact as possible with all tags properly closed.
  287.     *
  288.     * @param string   $html       The content of the introtext to be truncated
  289.     * @param integer  $maxLength  The maximum number of charactes to render
  290.     *
  291.     * @return  string  The truncated string
  292.     */
  293.     public static function truncate($html$maxLength 0)
  294.     {
  295.         $baseLength strlen($html);
  296.  
  297.         // First get the plain text string. This is the rendered text we want to end up with.
  298.         $ptString JHtml::_('string.truncate'$html$maxLength$noSplit true$allowHtml false);
  299.  
  300.         for ($maxLength$maxLength $baseLength;)
  301.         {
  302.             // Now get the string if we allow html.
  303.             $htmlString JHtml::_('string.truncate'$html$maxLength$noSplit true$allowHtml true);
  304.  
  305.             // Now get the plain text from the html string.
  306.             $htmlStringToPtString JHtml::_('string.truncate'$htmlString$maxLength$noSplit true$allowHtml false);
  307.  
  308.             // If the new plain text string matches the original plain text string we are done.
  309.             if ($ptString == $htmlStringToPtString)
  310.             {
  311.                 return $htmlString;
  312.             }
  313.             // Get the number of html tag characters in the first $maxlength characters
  314.             $diffLength strlen($ptStringstrlen($htmlStringToPtString);
  315.  
  316.             // Set new $maxlength that adjusts for the html tags
  317.             $maxLength += $diffLength;
  318.             if ($baseLength <= $maxLength || $diffLength <= 0)
  319.             {
  320.                 return $htmlString;
  321.             }
  322.         }
  323.         return $html;
  324.     }
  325.  
  326.     public static function groupBy($list$fieldName$article_grouping_direction$fieldNameToKeep null)
  327.     {
  328.         $grouped array();
  329.  
  330.         if (!is_array($list))
  331.         {
  332.             if ($list == '')
  333.             {
  334.                 return $grouped;
  335.             }
  336.  
  337.             $list array($list);
  338.         }
  339.  
  340.         foreach ($list as $key => $item)
  341.         {
  342.             if (!isset($grouped[$item->$fieldName]))
  343.             {
  344.                 $grouped[$item->$fieldNamearray();
  345.             }
  346.  
  347.             if (is_null($fieldNameToKeep))
  348.             {
  349.                 $grouped[$item->$fieldName][$key$item;
  350.             }
  351.             else {
  352.                 $grouped[$item->$fieldName][$key$item->$fieldNameToKeep;
  353.             }
  354.  
  355.             unset($list[$key]);
  356.         }
  357.  
  358.         $article_grouping_direction($grouped);
  359.  
  360.         return $grouped;
  361.     }
  362.  
  363.     public static function groupByDate($list$type 'year'$article_grouping_direction$month_year_format 'F Y')
  364.     {
  365.         $grouped array();
  366.  
  367.         if (!is_array($list))
  368.         {
  369.             if ($list == '')
  370.             {
  371.                 return $grouped;
  372.             }
  373.  
  374.             $list array($list);
  375.         }
  376.  
  377.         foreach ($list as $key => $item)
  378.         {
  379.             switch($type)
  380.             {
  381.                 case 'month_year':
  382.                     $month_year JString::substr($item->created07);
  383.  
  384.                     if (!isset($grouped[$month_year]))
  385.                     {
  386.                         $grouped[$month_yeararray();
  387.                     }
  388.  
  389.                     $grouped[$month_year][$key$item;
  390.                     break;
  391.  
  392.                 case 'year':
  393.                 default:
  394.                     $year JString::substr($item->created04);
  395.  
  396.                     if (!isset($grouped[$year]))
  397.                     {
  398.                         $grouped[$yeararray();
  399.                     }
  400.  
  401.                     $grouped[$year][$key$item;
  402.                     break;
  403.             }
  404.  
  405.             unset($list[$key]);
  406.         }
  407.  
  408.         $article_grouping_direction($grouped);
  409.  
  410.         if ($type === 'month_year')
  411.         {
  412.             foreach ($grouped as $group => $items)
  413.             {
  414.                 $date new JDate($group);
  415.                 $formatted_group $date->format($month_year_format);
  416.                 $grouped[$formatted_group$items;
  417.                 unset($grouped[$group]);
  418.             }
  419.         }
  420.  
  421.         return $grouped;
  422.     }
  423. }

Documentation generated on Tue, 19 Nov 2013 15:04:33 +0100 by phpDocumentor 1.4.3