htmlbulkexport.class.inc.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. // Copyright (C) 2015 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. * Bulk export: HTML export
  20. *
  21. * @copyright Copyright (C) 2015 Combodo SARL
  22. * @license http://opensource.org/licenses/AGPL-3.0
  23. */
  24. class HTMLBulkExport extends TabularBulkExport
  25. {
  26. public function DisplayUsage(Page $oP)
  27. {
  28. $oP->p(" * html format options:");
  29. $oP->p(" *\tfields: (mandatory) the comma separated list of field codes to export (e.g: name,org_id,service_name...).");
  30. }
  31. public function EnumFormParts()
  32. {
  33. return array_merge(parent::EnumFormParts(), array('interactive_fields_html' => array('interactive_fields_html')));
  34. }
  35. public function DisplayFormPart(WebPage $oP, $sPartId)
  36. {
  37. switch($sPartId)
  38. {
  39. case 'interactive_fields_html':
  40. $this->GetInteractiveFieldsWidget($oP, 'interactive_fields_html');
  41. break;
  42. default:
  43. return parent:: DisplayFormPart($oP, $sPartId);
  44. }
  45. }
  46. protected function GetSampleData($oObj, $sAttCode)
  47. {
  48. if ($oObj)
  49. {
  50. $sRet = $oObj->GetAsHTML($sAttCode);
  51. }
  52. else
  53. {
  54. $sRet = '';
  55. }
  56. return $sRet;
  57. }
  58. public function GetHeader()
  59. {
  60. $sData = '';
  61. $oSet = new DBObjectSet($this->oSearch);
  62. $this->aStatusInfo['status'] = 'running';
  63. $this->aStatusInfo['position'] = 0;
  64. $this->aStatusInfo['total'] = $oSet->Count();
  65. $sData .= "<table class=\"listResults\">\n";
  66. $sData .= "<thead>\n";
  67. $sData .= "<tr>\n";
  68. foreach($this->aStatusInfo['fields'] as $iCol => $aFieldSpec)
  69. {
  70. $sData .= "<th>".$aFieldSpec['sColLabel']."</th>\n";
  71. }
  72. $sData .= "</tr>\n";
  73. $sData .= "</thead>\n";
  74. $sData .= "<tbody>\n";
  75. return $sData;
  76. }
  77. public function GetNextChunk(&$aStatus)
  78. {
  79. $sRetCode = 'run';
  80. $iPercentage = 0;
  81. $oSet = new DBObjectSet($this->oSearch);
  82. $oSet->SetLimit($this->iChunkSize, $this->aStatusInfo['position']);
  83. $this->OptimizeColumnLoad($oSet);
  84. $sFirstAlias = $this->oSearch->GetClassAlias();
  85. $iCount = 0;
  86. $sData = '';
  87. $iPreviousTimeLimit = ini_get('max_execution_time');
  88. $iLoopTimeLimit = MetaModel::GetConfig()->Get('max_execution_time_per_loop');
  89. while($aRow = $oSet->FetchAssoc())
  90. {
  91. set_time_limit($iLoopTimeLimit);
  92. $oMainObj = $aRow[$sFirstAlias];
  93. $sHilightClass = '';
  94. if ($oMainObj)
  95. {
  96. $sHilightClass = $aRow[$sFirstAlias]->GetHilightClass();
  97. }
  98. if ($sHilightClass != '')
  99. {
  100. $sData .= "<tr class=\"$sHilightClass\">";
  101. }
  102. else
  103. {
  104. $sData .= "<tr>";
  105. }
  106. foreach($this->aStatusInfo['fields'] as $iCol => $aFieldSpec)
  107. {
  108. $sAlias = $aFieldSpec['sAlias'];
  109. $sAttCode = $aFieldSpec['sAttCode'];
  110. $oObj = $aRow[$sAlias];
  111. $sField = '';
  112. if ($oObj)
  113. {
  114. switch($sAttCode)
  115. {
  116. case 'id':
  117. $sField = $aRow[$sAlias]->GetHyperlink();
  118. break;
  119. default:
  120. $sField = $aRow[$sAlias]->GetAsHtml($sAttCode);
  121. }
  122. }
  123. $sValue = ($sField === '') ? '&nbsp;' : $sField;
  124. $sData .= "<td>$sValue</td>";
  125. }
  126. $sData .= "</tr>";
  127. $iCount++;
  128. }
  129. set_time_limit($iPreviousTimeLimit);
  130. $this->aStatusInfo['position'] += $this->iChunkSize;
  131. if ($this->aStatusInfo['total'] == 0)
  132. {
  133. $iPercentage = 100;
  134. }
  135. else
  136. {
  137. $iPercentage = floor(min(100.0, 100.0*$this->aStatusInfo['position']/$this->aStatusInfo['total']));
  138. }
  139. if ($iCount < $this->iChunkSize)
  140. {
  141. $sRetCode = 'done';
  142. }
  143. $aStatus = array('code' => $sRetCode, 'message' => Dict::S('Core:BulkExport:RetrievingData'), 'percentage' => $iPercentage);
  144. return $sData;
  145. }
  146. public function GetFooter()
  147. {
  148. $sData = "</tbody>\n";
  149. $sData .= "</table>\n";
  150. return $sData;
  151. }
  152. public function GetSupportedFormats()
  153. {
  154. return array('html' => Dict::S('Core:BulkExport:HTMLFormat'));
  155. }
  156. public function GetMimeType()
  157. {
  158. return 'text/html';
  159. }
  160. public function GetFileExtension()
  161. {
  162. return 'html';
  163. }
  164. }