CatalyzTextFilter.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. class CatalyzTextFilter {
  3. static function renderLinksToNodes($content) {
  4. if (preg_match_all('/#node-([0-9]+)(,([a-z]{2}))?#/', $content, $items)) {
  5. $patterns = array();
  6. $replacement = array();
  7. foreach($items[1] as $index => $nodeId) {
  8. if (empty($items[3][$index])) {
  9. $ContentTree = ContentTree::instance();
  10. } else {
  11. $ContentTree = ContentTree::instance($items[3][$index]);
  12. }
  13. $node = $ContentTree->getContentTreeNodeById($nodeId);
  14. if (!is_null($node)) {
  15. $patterns[] = $items[0][$index];
  16. $replacement[] = $node->getUrl();
  17. }
  18. }
  19. $content = str_replace($patterns, $replacement, $content);
  20. }
  21. return $content;
  22. }
  23. /**
  24. * CatalyzTextFilter::renderCollapseExpand()
  25. *
  26. * @param string $content
  27. * @return string
  28. */
  29. public static function renderCollapseExpand($content, $ContentTree = null, $group_element = 'h2', $sub_group_element = 'h3') {
  30. use_javascript('catalyz/jquery-1.3.2.min.js');
  31. $return = '<a name="top"></a>';
  32. if (false !== $group_element_start_tag_pos = strpos($content, sprintf('<%s>', $group_element))) {
  33. $return .= substr($content, 0, $group_element_start_tag_pos);
  34. $content = trim(substr($content, $group_element_start_tag_pos));
  35. }
  36. $groups = explode(sprintf('<%s>', $group_element), $content);
  37. if (0 < count($groups)) {
  38. /*$return .= '<ul>';
  39. foreach ($groups as $i => $group_html) {
  40. if ('' != $group_html && false !== $group_element_end_tag_pos = strpos($group_html, sprintf('</%s>', $group_element))) {
  41. $group_element_content = substr($group_html, 0, $group_element_end_tag_pos);
  42. $return .= sprintf('<li><a href="#%s">%s</a></li>', Catalyz::normalizeUrlIdentifier($group_element_content), $group_element_content);
  43. }
  44. }
  45. $return .= '</ul>';*/
  46. foreach ($groups as $i => $group_html) {
  47. if ('' != $group_html && false !== $group_element_end_tag_pos = strpos($group_html, sprintf('</%s>', $group_element))) {
  48. $group_element_content = substr($group_html, 0, $group_element_end_tag_pos);
  49. $return .= sprintf('<a name="%s"></a><%s style="clear: both"><span style="float: left">%s</span><a href="#top" class="top-link">Top</a></%s>', Catalyz::normalizeUrlIdentifier(utf8_encode(html_entity_decode($group_element_content))), $group_element, $group_element_content, $group_element);
  50. $group_html = trim(substr($group_html, $group_element_end_tag_pos + strlen($group_element) + 3));
  51. if ('' != $group_html && false !== $sub_group_element_start_tag_pos = strpos($group_html, sprintf('<%s>', $sub_group_element))) {
  52. $return .= substr($group_html, 0, $sub_group_element_start_tag_pos);
  53. $group_html = trim(substr($group_html, $sub_group_element_start_tag_pos));
  54. $sub_groups = explode(sprintf('<%s>', $sub_group_element), $group_html);
  55. if (0 < count($sub_groups)) {
  56. $return .= '<ul>';
  57. foreach ($sub_groups as $j => $sub_group_html) {
  58. if ('' != $sub_group_html && false !== $sub_group_element_end_tag_pos = strpos($sub_group_html, sprintf('</%s>', $sub_group_element))) {
  59. $li_html = substr($sub_group_html, 0, $sub_group_element_end_tag_pos);
  60. $sub_group_html = trim(substr($sub_group_html, $sub_group_element_end_tag_pos + strlen($sub_group_element) + 3));
  61. $return .= '<li';
  62. if ('' != $sub_group_html) {
  63. $return .= sprintf(' id="li-%d-%d" class="expand" onclick="$(\'#node-%d-%d\').toggle(); $(\'#li-%d-%d\').toggleClass(\'expand\', !$(\'#node-%d-%d\').is(\':visible\')); $(\'#li-%d-%d\').toggleClass(\'collapse\', $(\'#node-%d-%d\').is(\':visible\'));"', $i, $j, $i, $j, $i, $j, $i, $j, $i, $j, $i, $j);
  64. }
  65. $return .= '>';
  66. if ('' != $sub_group_html) {
  67. $return .= '<div onmouseover="this.style.cursor = \'pointer\';" onmouseout="this.style.cursor = \'default\';">' . $li_html . '</div>';
  68. } else {
  69. $return .= $li_html;
  70. }
  71. if ('' != $sub_group_html) {
  72. $return .= sprintf('<div id="node-%d-%d" style="display: none">%s</div>', $i, $j, $sub_group_html);
  73. }
  74. $return .= '</li>';
  75. }
  76. }
  77. $return .= '</ul>';
  78. }
  79. }
  80. }
  81. }
  82. }
  83. if (!is_null($ContentTree)) {
  84. $children = $ContentTree->getCurrentNode()->getVisibleChildren();
  85. if (0 < count($children)) {
  86. $return .= '<ul>';
  87. foreach ($children as $child) {
  88. $return .= sprintf('<li><a href="%s">%s</a></li>', $child->getUrl(), $child->getTitle());
  89. }
  90. $return .= '</ul>';
  91. }
  92. }
  93. return $return;
  94. }
  95. /**
  96. * CatalyzTextFilter::renderLightBoxImages()
  97. *
  98. * @param string $content
  99. * @param string $light_box_class
  100. * @return string
  101. */
  102. public static function renderLightBoxImages($content, $light_box_class = 'lightbox') {
  103. $return = $content;
  104. foreach (array(640=>$light_box_class,320=>'lightboxColumn') as $taille=>$class){
  105. if (preg_match_all('|(?P<tag><img class="' . $class . '"[^>]*src="(?P<src>[^"]*)"[^>]*>)|si', $return, $matches)) {
  106. use_helper('fancybox');
  107. use_helper('sfThumbnail');
  108. foreach ($matches['tag'] as $index => $tag) {
  109. if (!empty($matches['src'][$index])) {
  110. $return= self::renderlink($return, $tag, $matches['src'][$index], $matches['alt'][$index],$taille);
  111. }
  112. }
  113. }
  114. }
  115. return $return;
  116. }
  117. protected static function renderlink($content, $tag,$matches_src,$matches_alt,$taille){
  118. $return = $content;
  119. $image=sfConfig::get('sf_web_dir').$matches_src;
  120. if (is_file($image)) {
  121. $imageSize=getimagesize($image);
  122. $src = $matches_src;
  123. $infos=pathinfo($image);
  124. $extractWidth = $imageSize[0];
  125. $extractHeight = $imageSize[1];
  126. if (preg_match('/width="(?P<width>\d+)"/si', $tag, $tag_matches)) {
  127. $extractWidth=$tag_matches['width'];
  128. }
  129. if (preg_match('/height="(?P<height>\d+)"/si', $tag, $tag_matches)) {
  130. $extractHeight=$tag_matches['height'];
  131. }
  132. $src = !empty($matches_src)?$matches_src:'';
  133. $alt = !empty($matches_alt)?$matches_alt:'';
  134. if ($extractWidth > $taille || $extractHeight>$taille) {
  135. $thum = thumbnail_path($src, $taille, $taille);
  136. }else{
  137. $thum = $src;
  138. }
  139. $light_box_tag = sprintf('<a class="fancybox" rel="group" title="%s" href="%s"><img src="%s" alt="%s" /></a>', $alt, $src, $thum, $alt);
  140. $return = str_replace($tag, $light_box_tag, $return);
  141. }
  142. return $return;
  143. }
  144. }