/modules/tags/treemenu.php

https://github.com/alafon/eztags · PHP · 168 lines · 143 code · 25 blank · 0 comment · 20 complexity · c1729e2fd7d335e7feef93e066a099a7 MD5 · raw file

  1. <?php
  2. eZExpiryHandler::registerShutdownFunction();
  3. if ( !defined( 'MAX_AGE' ) )
  4. {
  5. define( 'MAX_AGE', 86400 );
  6. }
  7. function washJS( $string )
  8. {
  9. return str_replace( array( "\\", "/", "\n", "\t", "\r", "\b", "\f", '"' ), array( '\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"' ), $string );
  10. }
  11. function arrayToJSON( $array )
  12. {
  13. if ( $array )
  14. {
  15. $result = array();
  16. $resultDict = array();
  17. $isDict = false;
  18. $index = 0;
  19. foreach ( $array as $key => $value )
  20. {
  21. if ( $key != $index++ )
  22. {
  23. $isDict = true;
  24. }
  25. if ( is_array( $value ) )
  26. {
  27. $value = arrayToJSON( $value );
  28. }
  29. else if ( !is_numeric( $value ) or $key == 'name' )
  30. {
  31. $value = '"' . washJS( $value ) . '"';
  32. }
  33. $result[] = $value;
  34. $resultDict[] = '"' . washJS( $key ) . '":' . $value;
  35. }
  36. if ( $isDict )
  37. {
  38. return '{' . implode( $resultDict, ',' ) . '}';
  39. }
  40. else
  41. {
  42. return '[' . implode( $result, ',' ) . ']';
  43. }
  44. }
  45. else
  46. {
  47. return '[]';
  48. }
  49. }
  50. function lookupIcon( $ini, $tag )
  51. {
  52. $iconMap = $ini->variable( 'Icons', 'IconMap' );
  53. $returnValue = $ini->variable( 'Icons', 'Default' );
  54. if ( array_key_exists( $tag->attribute( 'id' ), $iconMap ) && !empty( $iconMap[$tag->attribute( 'id' )] ) )
  55. {
  56. $returnValue = $iconMap[$tag->attribute( 'id' )];
  57. }
  58. else
  59. {
  60. $tempTag = $tag;
  61. while ( $tempTag->attribute( 'parent_id' ) > 0 )
  62. {
  63. $tempTag = $tempTag->getParent();
  64. if ( array_key_exists( $tempTag->attribute( 'id' ), $iconMap ) && !empty( $iconMap[$tempTag->attribute( 'id' )] ) )
  65. {
  66. $returnValue = $iconMap[$tempTag->attribute( 'id' )];
  67. break;
  68. }
  69. }
  70. }
  71. return eZURLOperator::eZImage( eZTemplate::factory(), 'tag_icons/small/' . $returnValue, '' );
  72. }
  73. for ( $i = 0, $obLevel = ob_get_level(); $i < $obLevel; ++$i )
  74. {
  75. ob_end_clean();
  76. }
  77. if ( isset( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) )
  78. {
  79. header( $_SERVER['SERVER_PROTOCOL'] . ' 304 Not Modified' );
  80. header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', time() + MAX_AGE ) . ' GMT' );
  81. header( 'Cache-Control: max-age=' . MAX_AGE );
  82. header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s', strtotime( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) . ' GMT' );
  83. header( 'Pragma: ' );
  84. eZExecution::cleanExit();
  85. }
  86. $tagID = (int) $Params['TagID'];
  87. $siteINI = eZINI::instance();
  88. $eztagsINI = eZINI::instance( 'eztags.ini' );
  89. $tag = eZTagsObject::fetch( $tagID );
  90. if ( !( $tag instanceof eZTagsObject || $TagID == 0 ) )
  91. {
  92. header( $_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found' );
  93. }
  94. else
  95. {
  96. $children = eZTagsObject::fetchByParentID( $tagID );
  97. $response = array();
  98. $response['error_code'] = 0;
  99. $response['id'] = $tagID;
  100. $response['parent_id'] = ( $tag instanceof eZTagsObject ) ? (int) $tag->attribute( 'parent_id' ) : -1;
  101. $response['children_count'] = count( $children );
  102. $response['children'] = array();
  103. foreach ( $children as $child )
  104. {
  105. $childResponse = array();
  106. $childResponse['id'] = (int) $child->attribute( 'id' );
  107. $childResponse['parent_id'] = (int) $child->attribute( 'parent_id' );
  108. $childResponse['has_children'] = ( eZTagsObject::childrenCountByParentID( $child->attribute( 'id' ) ) ) ? 1 : 0;
  109. $childResponse['synonyms_count'] = eZTagsObject::synonymsCount( $child->attribute( 'id' ) );
  110. $childResponse['subtree_limitations_count'] = $child->getSubTreeLimitationsCount();
  111. $childResponse['keyword'] = $child->attribute( 'keyword' );
  112. $childResponse['url'] = 'tags/id/' . $child->attribute( 'id' );
  113. $childResponse['icon'] = lookupIcon( $eztagsINI, $child );
  114. eZURI::transformURI( $childResponse['url'] );
  115. $childResponse['modified'] = (int) $child->attribute( 'modified' );
  116. $response['children'][] = $childResponse;
  117. }
  118. $httpCharset = eZTextCodec::httpCharset();
  119. $jsonText = arrayToJSON( $response );
  120. $codec = eZTextCodec::instance( $httpCharset, 'unicode' );
  121. $jsonTextArray = $codec->convertString( $jsonText );
  122. $jsonText = '';
  123. foreach ( $jsonTextArray as $character )
  124. {
  125. if ( $character < 128 )
  126. {
  127. $jsonText .= chr( $character );
  128. }
  129. else
  130. {
  131. $jsonText .= '\u' . str_pad( dechex( $character ), 4, '0000', STR_PAD_LEFT );
  132. }
  133. }
  134. header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', time() + MAX_AGE ) . ' GMT' );
  135. header( 'Cache-Control: cache, max-age=' . MAX_AGE . ', post-check=' . MAX_AGE . ', pre-check=' . MAX_AGE );
  136. header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s', ( $tag instanceof eZTagsObject ) ? (int) $tag->attribute( 'modified' ) : time() ) . ' GMT' );
  137. header( 'Pragma: cache' );
  138. header( 'Content-Type: application/json' );
  139. header( 'Content-Length: '. strlen( $jsonText ) );
  140. echo $jsonText;
  141. }
  142. eZExecution::cleanExit();
  143. ?>