export.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. try
  32. {
  33. // Do this before loging, in order to allow setting user credentials from within the file
  34. utils::UseParamFile();
  35. }
  36. catch(Exception $e)
  37. {
  38. echo "Error: ".$e->GetMessage()."<br/>\n";
  39. exit -2;
  40. }
  41. require_once(APPROOT.'/application/loginwebpage.class.inc.php');
  42. LoginWebPage::DoLogin(); // Check user rights and prompt if needed
  43. ApplicationContext::SetUrlMakerClass('iTopStandardURLMaker');
  44. $sOperation = utils::ReadParam('operation', 'menu');
  45. $oAppContext = new ApplicationContext();
  46. $iActiveNodeId = utils::ReadParam('menu', -1);
  47. $currentOrganization = utils::ReadParam('org_id', '');
  48. // Main program
  49. $sExpression = utils::ReadParam('expression', '', true /* Allow CLI */, 'raw_data');
  50. if (strlen($sExpression) == 0)
  51. {
  52. $sQueryId = trim(utils::ReadParam('query', '', true /* Allow CLI */, 'raw_data'));
  53. if (strlen($sQueryId) > 0)
  54. {
  55. $oSearch = DBObjectSearch::FromOQL('SELECT QueryOQL WHERE id = :query_id', array('query_id' => $sQueryId));
  56. $oQueries = new DBObjectSet($oSearch);
  57. if ($oQueries->Count() > 0)
  58. {
  59. $oQuery = $oQueries->Fetch();
  60. $sExpression = $oQuery->Get('oql');
  61. }
  62. }
  63. }
  64. $sFormat = strtolower(utils::ReadParam('format', 'html'));
  65. $sFields = utils::ReadParam('fields', '', true, 'raw_data'); // CSV field list (allows to specify link set attributes, still not taken into account for XML export)
  66. $aFields = explode(',', $sFields);
  67. // Clean the list of columns (empty it if every string is empty)
  68. foreach($aFields as $index => $sField)
  69. {
  70. $aFields[$index] = trim($sField);
  71. if(strlen($aFields[$index]) == 0)
  72. {
  73. unset($aFields[$index]);
  74. }
  75. }
  76. $oP = null;
  77. if (!empty($sExpression))
  78. {
  79. try
  80. {
  81. $oFilter = DBObjectSearch::FromOQL($sExpression);
  82. $aArgs = array();
  83. foreach($oFilter->GetQueryParams() as $sParam => $foo)
  84. {
  85. $value = utils::ReadParam('arg_'.$sParam, null, true, 'raw_data');
  86. if (!is_null($value))
  87. {
  88. $aArgs[$sParam] = $value;
  89. }
  90. }
  91. $oFilter->SetInternalParams($aArgs);
  92. if ($oFilter)
  93. {
  94. $oSet = new CMDBObjectSet($oFilter, array(), $aArgs);
  95. switch($sFormat)
  96. {
  97. case 'html':
  98. $oP = new NiceWebPage("iTop - Export");
  99. // Integration within MS-Excel web queries + HTTPS + IIS:
  100. // MS-IIS set these header values with no-cache... while Excel fails to do the job if using HTTPS
  101. // Then the fix is to force the reset of header values Pragma and Cache-control
  102. header("Pragma:", true);
  103. header("Cache-control:", true);
  104. // The HTML output is made for pages located in the /pages/ folder
  105. // since this page is in a different folder, let's adjust the HTML 'base' attribute
  106. // to make the relative hyperlinks in the page work
  107. $sUrl = utils::GetAbsoluteUrlAppRoot();
  108. $oP->set_base($sUrl.'pages/');
  109. if(count($aFields) > 0)
  110. {
  111. $iSearch = array_search('id', $aFields);
  112. if ($iSearch !== false)
  113. {
  114. $bViewLink = true;
  115. unset($aFields[$iSearch]);
  116. }
  117. else
  118. {
  119. $bViewLink = false;
  120. }
  121. $sFields = implode(',', $aFields);
  122. $aExtraParams = array('menu' => false, 'display_limit' => false, 'zlist' => false, 'extra_fields' => $sFields, 'view_link' => $bViewLink);
  123. }
  124. else
  125. {
  126. $aExtraParams = array('menu' => false, 'display_limit' => false, 'zlist' => 'details');
  127. }
  128. $oResultBlock = new DisplayBlock($oFilter, 'list', false, $aExtraParams);
  129. $oResultBlock->Display($oP, 'expresult');
  130. break;
  131. case 'csv':
  132. $oP = new CSVPage("iTop - Export");
  133. $sFields = implode(',', $aFields);
  134. cmdbAbstractObject::DisplaySetAsCSV($oP, $oSet, array('fields' => $sFields));
  135. break;
  136. case 'spreadsheet':
  137. $oP = new WebPage("iTop - Export for spreadsheet");
  138. // Integration within MS-Excel web queries + HTTPS + IIS:
  139. // MS-IIS set these header values with no-cache... while Excel fails to do the job if using HTTPS
  140. // Then the fix is to force the reset of header values Pragma and Cache-control
  141. header("Pragma:", true);
  142. header("Cache-control:", true);
  143. $sFields = implode(',', $aFields);
  144. cmdbAbstractObject::DisplaySetAsHTMLSpreadsheet($oP, $oSet, array('fields' => $sFields));
  145. break;
  146. case 'xml':
  147. $oP = new XMLPage("iTop - Export", true /* passthrough */);
  148. cmdbAbstractObject::DisplaySetAsXML($oP, $oSet);
  149. break;
  150. default:
  151. $oP = new WebPage("iTop - Export");
  152. $oP->add("Unsupported format '$sFormat'. Possible values are: html, csv or xml.");
  153. }
  154. }
  155. }
  156. catch(Exception $e)
  157. {
  158. $oP = new WebPage("iTop - Export");
  159. $oP->p("Error the query can not be executed.");
  160. $oP->p($e->GetHtmlDesc());
  161. }
  162. }
  163. if (!$oP)
  164. {
  165. // Display a short message about how to use this page
  166. $oP = new WebPage("iTop - Export");
  167. $oP->p("<strong>General purpose export page.</strong>");
  168. $oP->p("<strong>Parameters:</strong>");
  169. $oP->p("<ul><li>expression: an OQL expression (URL encoded if needed)</li>
  170. <li>query: (alternative to 'expression') the id of an entry from the query phrasebook</li>
  171. <li>arg_xxx: (needed if the query has parameters) the value of the parameter 'xxx'</li>
  172. <li>format: (optional, default is html) the desired output format. Can be one of 'html', 'spreadsheet', 'csv' or 'xml'</li>
  173. <li>fields: (optional, no effect on XML format) list of fields (attribute codes) separated by a coma</li>
  174. </ul>");
  175. }
  176. $oP->output();
  177. ?>