<?php
/**
  * Ahead WebSoft Technologies Pvt Ltd.
  * @copyright  Copyright 2006-2026 Ahead WebSoft Technologies Pvt Ltd.
  * @license    https://www.socialengine.com/policies
*/

class Activity_View_Helper_ViewMoreActivity extends Zend_View_Helper_Abstract
{
  protected $_moreLength = 255;
  protected $_lessLength = 511;
  protected $_maxLength = 20000;
  protected $_fudgesicles = 10;
  protected $_maxLineBreaks = 4;
  protected $_tag = 'span';

  public function ViewMoreActivity($string, $moreLength = null, $maxLength = null, 
      $lessLength = null, $nl2br = true)
  {
    // Ensure UTF-8
    if (!mb_detect_encoding($string, 'UTF-8', true)) {
      $string = mb_convert_encoding($string, 'UTF-8');
    }

    $moreLength = 255;

    if( !is_numeric($moreLength) || $moreLength <= 0 ) {
      $moreLength = $this->_moreLength;
    }
    if( !is_numeric($maxLength) || $maxLength <= 0 ) {
      $maxLength = $this->_maxLength;
    }
    if( !is_numeric($lessLength) || $lessLength <= 0 ) {
      $lessLength = $this->_lessLength;
    }

    $strLen = mb_strlen($string, 'UTF-8');

    if( $strLen <= $moreLength + $this->_fudgesicles ) {
      return $nl2br ? nl2br($string) : $string;
    }

    if( $strLen >= $maxLength ) {
      $stringMore = $this->am_truncate($string, $maxLength);
      $string = $stringMore['content'];
    }

    $shortTextMore = $this->am_truncate($string, $moreLength);
    $shortText = $shortTextMore['content'];

    if(!$shortTextMore['more']) {
      return $shortText;
    }

    $fullText = $string;

    if( $nl2br ) {
      $shortText = nl2br($shortText);
      $fullText = nl2br($fullText);
    }

    $onclick = "var me = scriptJquery(this).parent(), other = scriptJquery(this).parent().next(); me.hide(); other.show();";

    $content = '<'.$this->_tag.' class="view_more">'
      . $shortText
      . $this->view->translate('... &nbsp;')
      . '<a class="view_more_link" href="javascript:void(0);" onclick="' . htmlspecialchars($onclick) . '">'
      . $this->view->translate('more')
      . '</a>'
      . '</'.$this->_tag.'>';

    $onclickLess = "var me = scriptJquery(this).parent(), other = scriptJquery(this).parent().prev(); me.hide(); other.show();";

    $content .= '<'.$this->_tag.' class="view_more" style="display:none;">'
      . $fullText
      . ' &nbsp;'
      . '<a class="view_less_link" href="javascript:void(0);" onclick="' . htmlspecialchars($onclickLess) . '">'
      . $this->view->translate('less')
      . '</a>'
      . '</'.$this->_tag.'>';

    return $content;
  }

  function am_truncate($text, $length = 100, $ending = '', $exact = true, $considerHtml = true) {

    if ($considerHtml) {

      if (mb_strlen(preg_replace('/<.*?>/', '', $text), 'UTF-8') <= $length) {
        return array('content'=>$text,'length'=>$length,'more'=>false);
      }

      preg_match_all('/(<.+?>)?([^<>]*)/su', $text, $lines, PREG_SET_ORDER);

      $total_length = mb_strlen($ending, 'UTF-8');
      $open_tags = array();
      $truncate = '';

      foreach ($lines as $line_matchings) {

        if (!empty($line_matchings[1])) {

          if (preg_match('/^<(\s*.+?\/\s*|\s*(img|br|input|hr|area|base|col|link|meta|param)(\s.+?)?)>$/is', $line_matchings[1])) {
            // self closing
          } else if (preg_match('/^<\s*\/([^\s]+?)\s*>$/s', $line_matchings[1], $tag_matchings)) {
            $pos = array_search($tag_matchings[1], $open_tags);
            if ($pos !== false) unset($open_tags[$pos]);
          } else if (preg_match('/^<\s*([^\s>!]+).*?>$/s', $line_matchings[1], $tag_matchings)) {
            array_unshift($open_tags, strtolower($tag_matchings[1]));
          }

          $truncate .= $line_matchings[1];
        }

        $content_length = mb_strlen(
          preg_replace('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|&#x[0-9a-f]{1,6};/i', ' ', $line_matchings[2]),
          'UTF-8'
        );

        if ($total_length + $content_length > $length) {

          $left = $length - $total_length;
          $truncate .= mb_substr($line_matchings[2], 0, $left, 'UTF-8');
          break;

        } else {
          $truncate .= $line_matchings[2];
          $total_length += $content_length;
        }

        if ($total_length >= $length) break;
      }

    } else {

      if (mb_strlen($text, 'UTF-8') <= $length) {
        return array('content'=>$text,'length'=>$length,'more'=>false);
      }

      $truncate = mb_substr($text, 0, $length - mb_strlen($ending, 'UTF-8'), 'UTF-8');
    }

    if (!$exact) {
      $spacepos = mb_strrpos($truncate, ' ', 0, 'UTF-8');
      if ($spacepos !== false) {
        $truncate = mb_substr($truncate, 0, $spacepos, 'UTF-8');
      }
    }

    $truncate .= $ending;

    if($considerHtml) {
      foreach ($open_tags as $tag) {
        $truncate .= '</' . $tag . '>';
      }
    }

    return array('content'=>$truncate,'length'=>$length,'more'=>true);
  }
}