/autoloads/eztagstemplatefunctions.php

https://github.com/alafon/eztags · PHP · 164 lines · 90 code · 16 blank · 58 comment · 5 complexity · ed66a98bc495a168ccf3c4c55d46616c MD5 · raw file

  1. <?php
  2. /**
  3. * eZTagsTemplateFunctions class implements eztags tpl operator methods
  4. *
  5. */
  6. class eZTagsTemplateFunctions
  7. {
  8. /**
  9. * Return an array with the template operator name.
  10. *
  11. * @return array
  12. */
  13. function operatorList()
  14. {
  15. return array( 'eztags_parent_string', 'latest_tags', 'user_limitations' );
  16. }
  17. /**
  18. * Return true to tell the template engine that the parameter list exists per operator type,
  19. * this is needed for operator classes that have multiple operators.
  20. *
  21. * @return bool
  22. */
  23. function namedParameterPerOperator()
  24. {
  25. return true;
  26. }
  27. /**
  28. * Returns an array of named parameters, this allows for easier retrieval
  29. * of operator parameters. This also requires the function modify() has an extra
  30. * parameter called $namedParameters.
  31. *
  32. * @return array
  33. */
  34. function namedParameterList()
  35. {
  36. return array( 'eztags_parent_string' => array( 'tag_id' => array( 'type' => 'integer',
  37. 'required' => true,
  38. 'default' => 0 ) ),
  39. 'latest_tags' => array( 'limit' => array( 'type' => 'integer',
  40. 'required' => false,
  41. 'default' => 10 ) ),
  42. 'user_limitations' => array( 'module' => array( 'type' => 'string',
  43. 'required' => true,
  44. 'default' => '' ),
  45. 'function' => array( 'type' => 'string',
  46. 'required' => true,
  47. 'default' => '' ) ) );
  48. }
  49. /**
  50. * Executes the PHP function for the operator cleanup and modifies $operatorValue.
  51. *
  52. * @param eZTemplate $tpl
  53. * @param string $operatorName
  54. * @param array $operatorParameters
  55. * @param string $rootNamespace
  56. * @param string $currentNamespace
  57. * @param mixed $operatorValue
  58. * @param array $namedParameters
  59. */
  60. function modify( $tpl, $operatorName, $operatorParameters, $rootNamespace, $currentNamespace, &$operatorValue, $namedParameters )
  61. {
  62. switch ( $operatorName )
  63. {
  64. case 'eztags_parent_string':
  65. {
  66. $operatorValue = self::generateParentString( $namedParameters['tag_id'] );
  67. } break;
  68. case 'latest_tags':
  69. {
  70. $operatorValue = self::fetchLatestTags( $namedParameters['limit'] );
  71. } break;
  72. case 'user_limitations':
  73. {
  74. $operatorValue = self::getSimplifiedUserAccess( $namedParameters['module'], $namedParameters['function'] );
  75. } break;
  76. }
  77. }
  78. /**
  79. * Generates tag heirarchy string for given tag ID
  80. *
  81. * @static
  82. * @param integer $tagID
  83. * @return string
  84. */
  85. static function generateParentString( $tagID )
  86. {
  87. $tag = eZTagsObject::fetch( $tagID );
  88. if ( !$tag instanceof eZTagsObject )
  89. return '(' . ezpI18n::tr( 'extension/eztags/tags/edit', 'no parent' ) . ')';
  90. $synonymsCount = $tag->getSynonymsCount();
  91. $keywordsArray = array();
  92. while ( $tag->hasParent() )
  93. {
  94. $keywordsArray[] = ( $synonymsCount > 0 ) ? $tag->attribute( 'keyword' ) . ' (+' . $synonymsCount . ')' : $tag->attribute( 'keyword' );
  95. $tag = $tag->getParent();
  96. $synonymsCount = $tag->getSynonymsCount();
  97. }
  98. $keywordsArray[] = ( $synonymsCount > 0 ) ? $tag->attribute( 'keyword' ) . ' (+' . $synonymsCount . ')' : $tag->attribute( 'keyword' );
  99. return implode( ' / ', array_reverse( $keywordsArray ) );
  100. }
  101. /**
  102. * Returns $limit latest tags
  103. *
  104. * @static
  105. * @param integer $limit
  106. * @return array
  107. */
  108. static function fetchLatestTags( $limit )
  109. {
  110. return eZPersistentObject::fetchObjectList( eZTagsObject::definition(), null,
  111. array( 'main_tag_id' => 0 ),
  112. array( 'modified' => 'desc' ),
  113. array( 'limit' => $limit ) );
  114. }
  115. /**
  116. * Shorthand method to check user access policy limitations for a given module/policy function.
  117. * Returns the same array as eZUser::hasAccessTo(), with "simplifiedLimitations".
  118. * 'simplifiedLimitations' array holds all the limitations names as defined in module.php.
  119. * If your limitation name is not defined as a key, then your user has full access to this limitation
  120. *
  121. * @static
  122. * @param string $module Name of the module
  123. * @param string $function Name of the policy function ($FunctionList element in module.php)
  124. * @return array
  125. */
  126. static function getSimplifiedUserAccess( $module, $function )
  127. {
  128. $user = eZUser::currentUser();
  129. $userAccess = $user->hasAccessTo( $module, $function );
  130. $userAccess['simplifiedLimitations'] = array();
  131. if ( $userAccess['accessWord'] == 'limited' )
  132. {
  133. foreach ( $userAccess['policies'] as $policy )
  134. {
  135. foreach ( $policy as $limitationName => $limitationList )
  136. {
  137. foreach ( $limitationList as $limitationValue )
  138. {
  139. $userAccess['simplifiedLimitations'][$limitationName][] = $limitationValue;
  140. }
  141. $userAccess['simplifiedLimitations'][$limitationName] = array_unique( $userAccess['simplifiedLimitations'][$limitationName] );
  142. }
  143. }
  144. }
  145. return $userAccess;
  146. }
  147. }
  148. ?>