Exemple #1
0
 /**
  * Get text from string with or without stripped html tags
  * Strips out Joomla plugin tags
  * Joomla 3.1+ only
  * 
  */
 static function getText($text, $type = 'html', $max_letter_count = 0, $strip_tags = true, $tags_to_keep = '')
 {
     $temp = '';
     if ($max_letter_count == 0) {
         return $temp;
     }
     if ($max_letter_count > 0) {
         if ($type == 'html') {
             $temp = self::stripPluginTags($text);
             if ($strip_tags) {
                 if ($tags_to_keep == '') {
                     $temp = strip_tags($temp);
                     return JHtmlString::truncate($temp, $max_letter_count, false, false);
                     // splits words and no html allowed
                 } else {
                     $temp = strip_tags($temp, $tags_to_keep);
                     if (method_exists('JHtmlString', 'truncateComplex')) {
                         return JHtmlString::truncateComplex($temp, $max_letter_count, true);
                         // since Joomla v3.1
                     } else {
                         //JFactory::getApplication()->enqueueMessage(JText::_('LIB_SYW_WARNING_CANNOTUSETRUNCATE'), 'warning'); // requires Joomla 3.1 minimum
                         return JHtmlString::truncate($temp, $max_letter_count, false, false);
                     }
                 }
             } else {
                 if (method_exists('JHtmlString', 'truncateComplex')) {
                     return JHtmlString::truncateComplex($temp, $max_letter_count, true);
                     // since Joomla v3.1
                 } else {
                     //JFactory::getApplication()->enqueueMessage(JText::_('LIB_SYW_WARNING_CANNOTUSETRUNCATE'), 'warning'); // requires Joomla 3.1 minimum
                     JHtmlString::truncate($temp, $max_letter_count, false, false);
                 }
             }
         } else {
             // 'txt'
             return JHtmlString::truncate($text, $max_letter_count, false, false);
             // splits words and no html allowed
         }
     } else {
         // take everything
         if ($type == 'html') {
             $temp = self::stripPluginTags($text);
             if ($strip_tags) {
                 if ($tags_to_keep == '') {
                     return strip_tags($temp);
                 } else {
                     return strip_tags($temp, $tags_to_keep);
                 }
             } else {
                 return $temp;
             }
         } else {
             // 'txt'
             return $text;
         }
     }
     return $temp;
 }
 /**
  * Tests the JHtmlString::truncateComplex method.
  *
  * @param   string   $html       The text to truncate.
  * @param   integer  $maxLength  The maximum length of the text.
  * @param   boolean  $noSplit    Don't split a word if that is where the cutoff occurs (default: true)
  * @param   string   $expected   The expected result.
  *
  * @return  void
  *
  * @dataProvider  getTestTruncateComplexData
  * @since   12.2
  */
 public function testTruncateComplex($html, $maxLength, $noSplit, $expected)
 {
     $this->assertThat(JHtmlString::truncateComplex($html, $maxLength, $noSplit), $this->equalTo($expected));
 }
Exemple #3
0
 /**
  * This method is available first in joomla 3.1
  * We have this function here to polyfill for joomla 2.5
  *
  * Method to extend the truncate method to more complex situations
  *
  * The goal is to get the proper length plain text string with as much of
  * the html intact as possible with all tags properly closed.
  *
  * @param   string   $html       The content of the introtext to be truncated
  * @param   integer  $maxLength  The maximum number of characters to render
  * @param   boolean  $noSplit    Don't split a word if that is where the cutoff occurs (default: true).
  *
  * @return  string  The truncated string. If the string is truncated an ellipsis
  *                  (...) will be appended.
  *
  * @note    If a maximum length of 3 or less is selected and the text has more than
  *          that number of characters an ellipsis will be displayed.
  *          This method will not create valid HTML from malformed HTML.
  *
  * @since   3.1
  */
 public static function truncateComplex($html, $maxLength = 0, $noSplit = true)
 {
     if (JVERSION > 3.1) {
         return JHtmlString::truncateComplex($html, $maxLength, $noSplit);
     }
     // Start with some basic rules.
     $baseLength = strlen($html);
     // If the original HTML string is shorter than the $maxLength do nothing and return that.
     if ($baseLength <= $maxLength || $maxLength == 0) {
         return $html;
     }
     // Take care of short simple cases.
     if ($maxLength <= 3 && substr($html, 0, 1) != '<' && strpos(substr($html, 0, $maxLength - 1), '<') === false && $baseLength > $maxLength) {
         return '...';
     }
     // Deal with maximum length of 1 where the string starts with a tag.
     if ($maxLength == 1 && substr($html, 0, 1) == '<') {
         $endTagPos = strlen(strstr($html, '>', true));
         $tag = substr($html, 1, $endTagPos);
         $l = $endTagPos + 1;
         if ($noSplit) {
             return substr($html, 0, $l) . '</' . $tag . '...';
         }
         // TODO: $character doesn't seem to be used...
         $character = substr(strip_tags($html), 0, 1);
         return substr($html, 0, $l) . '</' . $tag . '...';
     }
     // First get the truncated plain text string. This is the rendered text we want to end up with.
     $ptString = JHtml::_('string.truncate', $html, $maxLength, $noSplit, $allowHtml = false);
     // It's all HTML, just return it.
     if (strlen($ptString) == 0) {
         return $html;
     }
     // If the plain text is shorter than the max length the variable will not end in ...
     // In that case we use the whole string.
     if (substr($ptString, -3) != '...') {
         return $html;
     }
     // Regular truncate gives us the ellipsis but we want to go back for text and tags.
     if ($ptString == '...') {
         $stripped = substr(strip_tags($html), 0, $maxLength);
         $ptString = JHtml::_('string.truncate', $stripped, $maxLength, $noSplit, $allowHtml = false);
     }
     // We need to trim the ellipsis that truncate adds.
     $ptString = rtrim($ptString, '.');
     // Now deal with more complex truncation.
     while ($maxLength <= $baseLength) {
         // Get the truncated string assuming HTML is allowed.
         $htmlString = JHtml::_('string.truncate', $html, $maxLength, $noSplit, $allowHtml = true);
         if ($htmlString == '...' && strlen($ptString) + 3 > $maxLength) {
             return $htmlString;
         }
         $htmlString = rtrim($htmlString, '.');
         // Now get the plain text from the HTML string and trim it.
         $htmlStringToPtString = JHtml::_('string.truncate', $htmlString, $maxLength, $noSplit, $allowHtml = false);
         $htmlStringToPtString = rtrim($htmlStringToPtString, '.');
         // If the new plain text string matches the original plain text string we are done.
         if ($ptString == $htmlStringToPtString) {
             return $htmlString . '...';
         }
         // Get the number of HTML tag characters in the first $maxLength characters
         $diffLength = strlen($ptString) - strlen($htmlStringToPtString);
         if ($diffLength <= 0) {
             return $htmlString . '...';
         }
         // Set new $maxlength that adjusts for the HTML tags
         $maxLength += $diffLength;
     }
 }
 /**
  * Tests the JHtmlString::truncateComplex method.
  *
  * @param   string   $html       The text to truncate.
  * @param   integer  $maxLength  The maximum length of the text.
  * @param   boolean  $noSplit    Don't split a word if that is where the cutoff occurs (default: true)
  * @param   string   $expected   The expected result.
  *
  * @return  void
  *
  * @since         3.1
  * @dataProvider  getTestTruncateComplexData
  */
 public function testTruncateComplex($html, $maxLength, $noSplit, $expected)
 {
     $this->assertEquals($expected, JHtmlString::truncateComplex($html, $maxLength, $noSplit));
 }
Exemple #5
0
 /**
  * Retrieve a list of article
  *
  * @param   JRegistry  &$params  module parameters
  *
  * @return  mixed
  */
 public static function getList(&$params)
 {
     // Get the dbo
     $db = JFactory::getDbo();
     // Get an instance of the generic articles model
     $model = JModelLegacy::getInstance('Articles', 'ContentModel', array('ignore_request' => true));
     // Set application parameters in model
     $app = JFactory::getApplication();
     $appParams = $app->getParams();
     $model->setState('params', $appParams);
     // Set the filters based on the module params
     $model->setState('list.start', 0);
     $model->setState('list.limit', (int) $params->get('count', 5));
     $model->setState('filter.published', 1);
     // Access filter
     $access = !JComponentHelper::getParams('com_content')->get('show_noauth');
     $authorised = JAccess::getAuthorisedViewLevels(JFactory::getUser()->get('id'));
     $model->setState('filter.access', $access);
     // Category filter
     $model->setState('filter.category_id', $params->get('catid', array()));
     // User filter
     $userId = JFactory::getUser()->get('id');
     switch ($params->get('user_id')) {
         case 'by_me':
             $model->setState('filter.author_id', (int) $userId);
             break;
         case 'not_me':
             $model->setState('filter.author_id', $userId);
             $model->setState('filter.author_id.include', false);
             break;
         case '0':
             break;
         default:
             $model->setState('filter.author_id', (int) $params->get('user_id'));
             break;
     }
     // Filter by language
     $model->setState('filter.language', $app->getLanguageFilter());
     //  Featured switch
     switch ($params->get('show_featured')) {
         case '1':
             $model->setState('filter.featured', 'only');
             break;
         case '0':
             $model->setState('filter.featured', 'hide');
             break;
         default:
             $model->setState('filter.featured', 'show');
             break;
     }
     // Set ordering
     $order_map = array('m_dsc' => 'a.modified DESC, a.created', 'mc_dsc' => 'CASE WHEN (a.modified = ' . $db->quote($db->getNullDate()) . ') THEN a.created ELSE a.modified END', 'c_dsc' => 'a.created', 'p_dsc' => 'a.publish_up', 'random' => 'RAND()');
     $ordering = JArrayHelper::getValue($order_map, $params->get('ordering'), 'a.publish_up');
     $dir = 'DESC';
     $model->setState('list.ordering', $ordering);
     $model->setState('list.direction', $dir);
     $items = $model->getItems();
     //var_dump($items);
     foreach ($items as &$item) {
         $item->introtext = JHtmlString::truncateComplex($item->introtext, $params->get('textlimit'));
         $item->slug = $item->id . ':' . $item->alias;
         $item->catslug = $item->catid . ':' . $item->category_alias;
         if ($access || in_array($item->access, $authorised)) {
             // We know that user has the privilege to view the article
             $item->link = JRoute::_(ContentHelperRoute::getArticleRoute($item->slug, $item->catslug));
         } else {
             $item->link = JRoute::_('index.php?option=com_users&view=login');
         }
     }
     return $items;
 }