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

/modules/website/services/URLService.class.php

http://pagizer-cms.googlecode.com/
PHP | 142 lines | 87 code | 28 blank | 27 comment | 15 complexity | 58989fb4a058f14416a0fadcafe9cdd9 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * This file is part of the Pagizer package.
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. *
  8. * @copyright Copyright (c) 2010 Advisa (http://www.advisa.fr)
  9. * @author Pagizer Core Team <team@pagizer.org>
  10. * @package pagizer
  11. * @subpackage website
  12. */
  13. class m_website_services_URLService extends f_core_Service
  14. {
  15. /**
  16. * Retrieve the correct page from the given URL
  17. *
  18. * @param string $url
  19. * @return m_website_documents_Page
  20. */
  21. public static function getPageFromUrl($url)
  22. {
  23. $url = str_replace(URL_ABSOLUTE_WO_HTTP, '', $url);
  24. $urlParams = explode('/', $url);
  25. // 1 - We get the website ID
  26. $documentProvider = f_document_Provider::getInstance();
  27. $website = $documentProvider->loadDocuments('website/Website')->whereIsEqual('url', URL_ABSOLUTE_WO_HTTP)->retrieveFirstDocument();
  28. if(is_null($website))
  29. {
  30. throw new Exception('No website with this URL');
  31. }
  32. f_core_Context::getInstance()->setContext('currentWebsite', $website);
  33. // 2 - Check if we are in a translated website
  34. $langs = $website->getTranslationLangs();
  35. if($urlParams[1] != "" && in_array(strtoupper($urlParams[1]), $langs))
  36. {
  37. $website = $documentProvider->getByModelAndUniqueId('website/Website', $website->getUniqueId(), $urlParams[1]);
  38. $urlWoHost = str_replace('/'.$urlParams[1],'', $url);
  39. }
  40. else
  41. {
  42. $urlWoHost = $url;
  43. }
  44. // 3 - we retrieve the page
  45. if($urlWoHost == '') $urlWoHost = '/';
  46. $page = $documentProvider ->loadDocuments('website/Page')
  47. ->whereIsEqual('url', $urlWoHost)
  48. ->byPublicationStatus('PUBLISHED')
  49. ->whereIsEqual('websiteId', $website->getUniqueId())
  50. ->retrieveFirstDocument($website->getLang());
  51. if(is_null($page))
  52. {
  53. $page = self::getExtendedPage($urlWoHost, $website->getUniqueId(), $website->getLang());
  54. }
  55. if(!is_null($page))
  56. {
  57. // $cache->putInCache($cacheKey, array("docId" => $documents[0]->getId(), "docLang" => $documents[0]->getLang()) , 75000);
  58. return $page;
  59. }
  60. return null;
  61. }
  62. private static function getExtendedPage($url, $websiteId, $lang)
  63. {
  64. $documentProvider = f_document_Provider::getInstance();
  65. $pos = strrpos($url, '/');
  66. if($pos == 0) $pos = strlen($url);
  67. $regexp = substr($url, 0, $pos).'/<.*>';
  68. $page = $documentProvider ->loadDocuments('website/Page')
  69. ->whereRegexp('url', '^'.$regexp.'$')
  70. ->byPublicationStatus('PUBLISHED')
  71. ->whereIsEqual('websiteId', $websiteId)
  72. ->retrieveFirstDocument($lang);
  73. if(is_null($page))
  74. {
  75. $page = $documentProvider ->loadDocuments('website/Page')
  76. ->whereRegexp('url', '^/<.*>$')
  77. ->whereIsEqual('document_publicationstatus', 'PUBLISHED')
  78. ->whereIsEqual('websiteId', $websiteId)
  79. ->retrieveFirstDocument($lang);
  80. $pos = 0;
  81. }
  82. if(!is_null($page))
  83. {
  84. preg_match('/<(.*?)>/', $page->getUrl(true), $matches);
  85. if(isset($matches[1]))
  86. {
  87. $model = $matches[1];
  88. $url = substr($url, $pos+1);
  89. if($url !== false)
  90. {
  91. $suffix[0]['model'] = $model;
  92. $suffix[0]['url'] = $url;
  93. f_core_Request::getInstance()->setParameter('urlSuffix', $suffix);
  94. }
  95. }
  96. }
  97. return $page;
  98. }
  99. /**
  100. * Check if a given url exists into db
  101. *
  102. * @param string $url
  103. * @return boolean
  104. */
  105. public static function urlExists($url, $websiteId, $lang, $documentModel = 'modules_website/Page')
  106. {
  107. $requete = f_document_Provider::getInstance()->loadDocuments($documentModel)->byPublicationStatus('PUBLISHED')->whereIsEqual('url', $url);
  108. if(!is_null($websiteId))
  109. {
  110. $requete = $requete->whereIsEqual('websiteId', $websiteId);
  111. }
  112. return $requete->count($lang) >= 1;
  113. }
  114. }