export.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * Export data specified by an OQL
  18. *
  19. * @author Erwan Taloc <erwan.taloc@combodo.com>
  20. * @author Romain Quetiez <romain.quetiez@combodo.com>
  21. * @author Denis Flaven <denis.flaven@combodo.com>
  22. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  23. */
  24. require_once('../application/application.inc.php');
  25. require_once('../application/nicewebpage.class.inc.php');
  26. require_once('../application/csvpage.class.inc.php');
  27. require_once('../application/xmlpage.class.inc.php');
  28. require_once('../application/startup.inc.php');
  29. require_once('../application/loginwebpage.class.inc.php');
  30. LoginWebPage::DoLogin(); // Check user rights and prompt if needed
  31. $sOperation = utils::ReadParam('operation', 'menu');
  32. $oAppContext = new ApplicationContext();
  33. $iActiveNodeId = utils::ReadParam('menu', -1);
  34. $currentOrganization = utils::ReadParam('org_id', '');
  35. // Main program
  36. $sExpression = utils::ReadParam('expression', '');
  37. $sFormat = strtolower(utils::ReadParam('format', 'html'));
  38. $oP = null;
  39. if (!empty($sExpression))
  40. {
  41. try
  42. {
  43. $oFilter = DBObjectSearch::FromOQL($sExpression);
  44. if ($oFilter)
  45. {
  46. $oSet = new CMDBObjectSet($oFilter);
  47. switch($sFormat)
  48. {
  49. case 'html':
  50. $oP = new NiceWebPage("iTop - Export");
  51. // The HTML output is made for pages located in the /pages/ folder
  52. // since this page is in a different folder, let's adjust the HTML 'base' attribute
  53. // to make the relative hyperlinks in the page work
  54. $sServerName = $_SERVER['SERVER_NAME'];
  55. $sProtocol = isset($_SERVER['HTTPS']) ? 'https' : 'http';
  56. if ($sProtocol == 'http')
  57. {
  58. $sPort = ($_SERVER['SERVER_PORT'] == 80) ? '' : ':'.$_SERVER['SERVER_PORT'];
  59. }
  60. else
  61. {
  62. $sPort = ($_SERVER['SERVER_PORT'] == 443) ? '' : ':'.$_SERVER['SERVER_PORT'];
  63. }
  64. $sUrl = "$sProtocol://{$sServerName}{$sPort}/pages/";
  65. $oP->set_base($sUrl);
  66. cmdbAbstractObject::DisplaySet($oP, $oSet, array('block_id' => 'expresult', 'menu' => false, 'display_limit' => false, 'zlist' => 'details')); // no menu, no truncated list, "details" zlist
  67. break;
  68. case 'csv':
  69. $oP = new CSVPage("iTop - Export");
  70. cmdbAbstractObject::DisplaySetAsCSV($oP, $oSet);
  71. break;
  72. case 'xml':
  73. $oP = new XMLPage("iTop - Export");
  74. cmdbAbstractObject::DisplaySetAsXML($oP, $oSet);
  75. break;
  76. default:
  77. $oP = new WebPage("iTop - Export");
  78. $oP->add("Unsupported format '$sFormat'. Possible values are: html, csv or xml.");
  79. }
  80. }
  81. }
  82. catch(Exception $e)
  83. {
  84. $oP = new WebPage("iTop - Export");
  85. $oP->p("Error the query can not be executed.");
  86. $oP->p($e->GetHtmlDesc());
  87. }
  88. }
  89. if (!$oP)
  90. {
  91. // Display a short message about how to use this page
  92. $oP = new WebPage("iTop - Export");
  93. $oP->p("<strong>General purpose export page.</strong>");
  94. $oP->p("<strong>Parameters:</strong>");
  95. $oP->p("<ul><li>expression: an OQL expression (URL encoded if needed)</li>
  96. <li>format: (optional, default is html) the desired output format. Can be one of 'html', 'csv' or 'xml'</li>
  97. </ul>");
  98. }
  99. $oP->output();
  100. ?>