PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/application/libraries/Omeka/Navigation/Page/Uri.php

https://gitlab.com/eita/multiplique-consumo-responsavel
PHP | 130 lines | 87 code | 3 blank | 40 comment | 22 complexity | d610fa56aaca5b5e4000897ec31649bb MD5 | raw file
  1. <?php
  2. /**
  3. * Omeka
  4. *
  5. * @copyright Copyright 2007-2012 Roy Rosenzweig Center for History and New Media
  6. * @license http://www.gnu.org/licenses/gpl-3.0.txt GNU GPLv3
  7. */
  8. /**
  9. * Customized subclass of Zend Framework's Zend_Navigation_Page_Uri class.
  10. *
  11. * @package Omeka\Navigation
  12. */
  13. class Omeka_Navigation_Page_Uri extends Zend_Navigation_Page_Uri
  14. {
  15. /**
  16. * Returns whether page should be considered active or not
  17. *
  18. * @param bool $recursive [optional] whether page should be considered
  19. * active if any child pages are active. Default is
  20. * false.
  21. * @return bool whether page should be considered active
  22. */
  23. public function isActive($recursive = false)
  24. {
  25. $this->_active = is_current_url($this->getUri());
  26. return parent::isActive($recursive);
  27. }
  28. /**
  29. * Sets page href. It will parse the href and update the uri and fragment properties.
  30. *
  31. * @param string|null $href page href, must a string or null
  32. * @return Omeka_Navigation_Page_Uri fluent interface, returns self
  33. * @throws Omeka_Navigation_Page_Uri_Exception if $uri is invalid
  34. */
  35. public function setHref($href)
  36. {
  37. $hrefData = $this->_normalizeHref($href);
  38. $this->setUri($hrefData['uri']);
  39. $this->setFragment($hrefData['fragment']);
  40. }
  41. /**
  42. * Normalizes a string href for a navigation page and returns an array with the following keys:
  43. * 'uri' => the uri of the href.
  44. * 'fragment' => the fragment of the href
  45. * If the $href is a relative path, then it must be a root path.
  46. * If the $href is a relative path then the value for the 'uri' key will be a relative path.
  47. * If $href is an invalid uri, then return null.
  48. *
  49. * @param String $href
  50. * @return array
  51. * @throws Omeka_Navigation_Page_Uri_Exception if $uri is invalid
  52. */
  53. protected function _normalizeHref($href)
  54. {
  55. if ($href === null || trim($href) == '') {
  56. return array(
  57. 'uri' => '',
  58. 'fragment' => null
  59. );
  60. }
  61. $href = trim($href);
  62. $isPath = false;
  63. if (strlen($href) && $href[0] == '/') {
  64. // attempt to convert root path into a full path,
  65. // so that we can later extract the fragment using Zend_Uri_Http
  66. $webRoot = trim(WEB_ROOT);
  67. $webPath = trim(PUBLIC_BASE_URL);
  68. if ($webPath == '') {
  69. $href = $webRoot . $href;
  70. } else {
  71. $index = strrpos($webRoot, $webPath);
  72. if ($index !== false) {
  73. $href = substr($webRoot, 0, $index) . $href;
  74. } else {
  75. $href = $webRoot . $href;
  76. }
  77. }
  78. $isPath = true;
  79. }
  80. try {
  81. $uri = Zend_Uri::factory($href);
  82. if ($uri->valid()) {
  83. $fragment = $uri->getFragment();
  84. if (!$fragment) {
  85. $fragment = null;
  86. }
  87. $uri->setFragment('');
  88. if ($isPath) {
  89. $uriString = $uri->getPath();
  90. if ($query = $uri->getQuery()) {
  91. $uriString .= '?' . $query;
  92. }
  93. $uri = $uriString;
  94. } else {
  95. $uri = $uri->getUri();
  96. }
  97. if (strpos($href, '#')) {
  98. $uri .= '#'; // add the hash to the uri
  99. }
  100. return array(
  101. 'uri' => $uri,
  102. 'fragment' => $fragment,
  103. );
  104. }
  105. } catch (Zend_Uri_Exception $e) {
  106. if (filter_var($href, FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED)) {
  107. $fragmentPos = strrpos($href, '#');
  108. if ($fragmentPos !== FALSE) {
  109. if ($fragmentPos < strlen($href) - 1) {
  110. $fragment = substr($href, $fragmentPos + 1); // exclude the hash from the fragment
  111. } else {
  112. $fragment = null;
  113. }
  114. $uri = substr($href, 0, $fragmentPos + 1); // include the hash in the uri
  115. } else {
  116. $uri = $href;
  117. $fragment = null;
  118. }
  119. return array(
  120. 'uri' => $uri,
  121. 'fragment' => $fragment
  122. );
  123. }
  124. }
  125. throw new Omeka_Navigation_Page_Uri_Exception(__('Invalid URI for Omeka_Navigation_Page_Uri object: %s', $href));
  126. }
  127. }