export.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. if (!defined('__DIR__')) define('__DIR__', dirname(__FILE__));
  25. require_once(__DIR__.'/../approot.inc.php');
  26. require_once(APPROOT.'/application/application.inc.php');
  27. require_once(APPROOT.'/application/nicewebpage.class.inc.php');
  28. require_once(APPROOT.'/application/csvpage.class.inc.php');
  29. require_once(APPROOT.'/application/xmlpage.class.inc.php');
  30. require_once(APPROOT.'/application/startup.inc.php');
  31. require_once(APPROOT.'/application/loginwebpage.class.inc.php');
  32. LoginWebPage::DoLogin(); // Check user rights and prompt if needed
  33. ApplicationContext::SetUrlMakerClass('iTopStandardURLMaker');
  34. $sOperation = utils::ReadParam('operation', 'menu');
  35. $oAppContext = new ApplicationContext();
  36. $iActiveNodeId = utils::ReadParam('menu', -1);
  37. $currentOrganization = utils::ReadParam('org_id', '');
  38. // Main program
  39. $sExpression = utils::ReadParam('expression', '', true /* Allow CLI */, 'raw_data');
  40. $sFormat = strtolower(utils::ReadParam('format', 'html'));
  41. $sFields = utils::ReadParam('fields', '', true, 'raw_data'); // CSV field list (allows to specify link set attributes, still not taken into account for XML export)
  42. $aFields = explode(',', $sFields);
  43. foreach($aFields as &$sField)
  44. {
  45. $sField = trim($sField);
  46. }
  47. $oP = null;
  48. if (!empty($sExpression))
  49. {
  50. try
  51. {
  52. $oFilter = DBObjectSearch::FromOQL($sExpression);
  53. if ($oFilter)
  54. {
  55. $oSet = new CMDBObjectSet($oFilter);
  56. switch($sFormat)
  57. {
  58. case 'html':
  59. $oP = new NiceWebPage("iTop - Export");
  60. // The HTML output is made for pages located in the /pages/ folder
  61. // since this page is in a different folder, let's adjust the HTML 'base' attribute
  62. // to make the relative hyperlinks in the page work
  63. $sUrl = utils::GetAbsoluteUrlAppRoot();
  64. $oP->set_base($sUrl.'pages/');
  65. if(count($aFields) > 0)
  66. {
  67. $iSearch = array_search('id', $aFields);
  68. if ($iSearch !== false)
  69. {
  70. $bViewLink = true;
  71. unset($aFields[$iSearch]);
  72. }
  73. else
  74. {
  75. $bViewLink = false;
  76. }
  77. $sFields = implode(',', $aFields);
  78. $aExtraParams = array('menu' => false, 'display_limit' => false, 'zlist' => false, 'extra_fields' => $sFields, 'view_link' => $bViewLink);
  79. }
  80. else
  81. {
  82. $aExtraParams = array('menu' => false, 'display_limit' => false, 'zlist' => 'details');
  83. }
  84. $oResultBlock = new DisplayBlock($oFilter, 'list', false, $aExtraParams);
  85. $oResultBlock->Display($oP, 'expresult');
  86. break;
  87. case 'csv':
  88. $oP = new CSVPage("iTop - Export");
  89. $sFields = implode(',', $aFields);
  90. cmdbAbstractObject::DisplaySetAsCSV($oP, $oSet, array('fields' => $sFields));
  91. break;
  92. case 'xml':
  93. $oP = new XMLPage("iTop - Export", true /* passthrough */);
  94. cmdbAbstractObject::DisplaySetAsXML($oP, $oSet);
  95. break;
  96. default:
  97. $oP = new WebPage("iTop - Export");
  98. $oP->add("Unsupported format '$sFormat'. Possible values are: html, csv or xml.");
  99. }
  100. }
  101. }
  102. catch(Exception $e)
  103. {
  104. $oP = new WebPage("iTop - Export");
  105. $oP->p("Error the query can not be executed.");
  106. $oP->p($e->GetHtmlDesc());
  107. }
  108. }
  109. if (!$oP)
  110. {
  111. // Display a short message about how to use this page
  112. $oP = new WebPage("iTop - Export");
  113. $oP->p("<strong>General purpose export page.</strong>");
  114. $oP->p("<strong>Parameters:</strong>");
  115. $oP->p("<ul><li>expression: an OQL expression (URL encoded if needed)</li>
  116. <li>format: (optional, default is html) the desired output format. Can be one of 'html', 'csv' or 'xml'</li>
  117. <li>fields: (optional, no effect on XML format) list of fields (attribute codes) separated by a coma</li>
  118. </ul>");
  119. }
  120. $oP->output();
  121. ?>