main.itop-portal.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. // Copyright (C) 2016 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. /**
  19. * main.itop-portal.php
  20. *
  21. * @author Guillaume Lajarige <guillaume.lajarige@combodo.com>
  22. */
  23. class iTopPortalEditUrlMaker implements iDBObjectURLMaker
  24. {
  25. /**
  26. * Generate an (absolute) URL to an object, either in view or edit mode
  27. * @param string $sClass The class of the object
  28. * @param int $iId The identifier of the object
  29. * @param string $sMode edit|view
  30. * @return string
  31. */
  32. public static function PrepareObjectURL($sClass, $iId, $sMode)
  33. {
  34. require_once APPROOT . '/lib/silex/vendor/autoload.php';
  35. require_once APPROOT . '/env-' . utils::GetCurrentEnvironment() . '/itop-portal-base/portal/src/providers/urlgeneratorserviceprovider.class.inc.php';
  36. require_once APPROOT . '/env-' . utils::GetCurrentEnvironment() . '/itop-portal-base/portal/src/helpers/urlgeneratorhelper.class.inc.php';
  37. require_once APPROOT . '/env-' . utils::GetCurrentEnvironment() . '/itop-portal-base/portal/src/helpers/applicationhelper.class.inc.php';
  38. // Using a static var allows to preserve the object through function calls
  39. static $oApp = null;
  40. static $sPortalId = null;
  41. // Initializing Silex app
  42. if ($oApp === null)
  43. {
  44. // Initializing Silex framework
  45. $oApp = new Silex\Application();
  46. // Registering optional silex components
  47. $oApp->register(new Combodo\iTop\Portal\Provider\UrlGeneratorServiceProvider());
  48. // Registering routes
  49. Combodo\iTop\Portal\Helper\ApplicationHelper::LoadRouters();
  50. Combodo\iTop\Portal\Helper\ApplicationHelper::RegisterRoutes($oApp);
  51. // Retrieving portal id
  52. $sPortalId = basename(__DIR__);
  53. }
  54. // The object is reachable in the specified mode (edit/view)
  55. switch($sMode)
  56. {
  57. case 'view':
  58. $sObjectQueryString = $oApp['url_generator']->generate('p_object_view', array('sObjectClass' => $sClass, 'sObjectId' => $iId));
  59. break;
  60. case 'edit':
  61. default:
  62. $sObjectQueryString = $oApp['url_generator']->generate('p_object_edit', array('sObjectClass' => $sClass, 'sObjectId' => $iId));
  63. }
  64. $sPortalAbsoluteUrl = utils::GetAbsoluteUrlModulePage($sPortalId, 'index.php');
  65. if (strpos($sPortalAbsoluteUrl, '?') !== false)
  66. {
  67. $sUrl = substr($sPortalAbsoluteUrl, 0, strpos($sPortalAbsoluteUrl, '?')).$sObjectQueryString;
  68. }
  69. else
  70. {
  71. $sUrl = $sPortalAbsoluteUrl.$sObjectQueryString;
  72. }
  73. return $sUrl;
  74. }
  75. public static function MakeObjectURL($sClass, $iId)
  76. {
  77. return static::PrepareObjectURL($sClass, $iId, 'edit');
  78. }
  79. }
  80. /**
  81. * Hyperlinks to the "view" of the object (vs edition)
  82. * @author denis
  83. *
  84. */
  85. class iTopPortalViewUrlMaker extends iTopPortalEditUrlMaker
  86. {
  87. public static function MakeObjectURL($sClass, $iId)
  88. {
  89. return static::PrepareObjectURL($sClass, $iId, 'view');
  90. }
  91. }
  92. // Default portal hyperlink (for notifications) is the edit hyperlink
  93. DBObject::RegisterURLMakerClass('portal', 'iTopPortalEditUrlMaker');