htmlbulkexport.class.inc.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. return $this->GetValue($oObj, $sAttCode);
  49. }
  50. protected function GetValue($oObj, $sAttCode)
  51. {
  52. switch($sAttCode)
  53. {
  54. case 'id':
  55. $sRet = $oObj->GetHyperlink();
  56. break;
  57. default:
  58. $value = $oObj->Get($sAttCode);
  59. if ($value instanceof ormCaseLog)
  60. {
  61. $sRet = $value->GetAsSimpleHtml();
  62. }
  63. elseif ($value instanceof ormStopWatch)
  64. {
  65. $sRet = $value->GetTimeSpent();
  66. }
  67. else
  68. {
  69. $sRet = $oObj->GetAsHtml($sAttCode);
  70. }
  71. }
  72. return $sRet;
  73. }
  74. public function GetHeader()
  75. {
  76. $sData = '';
  77. $oSet = new DBObjectSet($this->oSearch);
  78. $this->aStatusInfo['status'] = 'running';
  79. $this->aStatusInfo['position'] = 0;
  80. $this->aStatusInfo['total'] = $oSet->Count();
  81. $sData .= "<table class=\"listResults\">\n";
  82. $sData .= "<thead>\n";
  83. $sData .= "<tr>\n";
  84. foreach($this->aStatusInfo['fields'] as $iCol => $aFieldSpec)
  85. {
  86. $sData .= "<th>".$aFieldSpec['sColLabel']."</th>\n";
  87. }
  88. $sData .= "</tr>\n";
  89. $sData .= "</thead>\n";
  90. $sData .= "<tbody>\n";
  91. return $sData;
  92. }
  93. public function GetNextChunk(&$aStatus)
  94. {
  95. $sRetCode = 'run';
  96. $iPercentage = 0;
  97. $oSet = new DBObjectSet($this->oSearch);
  98. $oSet->SetLimit($this->iChunkSize, $this->aStatusInfo['position']);
  99. $this->OptimizeColumnLoad($oSet);
  100. $sFirstAlias = $this->oSearch->GetClassAlias();
  101. $iCount = 0;
  102. $sData = '';
  103. $iPreviousTimeLimit = ini_get('max_execution_time');
  104. $iLoopTimeLimit = MetaModel::GetConfig()->Get('max_execution_time_per_loop');
  105. while($aRow = $oSet->FetchAssoc())
  106. {
  107. set_time_limit($iLoopTimeLimit);
  108. $oMainObj = $aRow[$sFirstAlias];
  109. $sHilightClass = '';
  110. if ($oMainObj)
  111. {
  112. $sHilightClass = $aRow[$sFirstAlias]->GetHilightClass();
  113. }
  114. if ($sHilightClass != '')
  115. {
  116. $sData .= "<tr class=\"$sHilightClass\">";
  117. }
  118. else
  119. {
  120. $sData .= "<tr>";
  121. }
  122. foreach($this->aStatusInfo['fields'] as $iCol => $aFieldSpec)
  123. {
  124. $sAlias = $aFieldSpec['sAlias'];
  125. $sAttCode = $aFieldSpec['sAttCode'];
  126. $oObj = $aRow[$sAlias];
  127. $sField = '';
  128. if ($oObj)
  129. {
  130. $sField = $this->GetValue($oObj, $sAttCode);
  131. }
  132. $sValue = ($sField === '') ? '&nbsp;' : $sField;
  133. $sData .= "<td>$sValue</td>";
  134. }
  135. $sData .= "</tr>";
  136. $iCount++;
  137. }
  138. set_time_limit($iPreviousTimeLimit);
  139. $this->aStatusInfo['position'] += $this->iChunkSize;
  140. if ($this->aStatusInfo['total'] == 0)
  141. {
  142. $iPercentage = 100;
  143. }
  144. else
  145. {
  146. $iPercentage = floor(min(100.0, 100.0*$this->aStatusInfo['position']/$this->aStatusInfo['total']));
  147. }
  148. if ($iCount < $this->iChunkSize)
  149. {
  150. $sRetCode = 'done';
  151. }
  152. $aStatus = array('code' => $sRetCode, 'message' => Dict::S('Core:BulkExport:RetrievingData'), 'percentage' => $iPercentage);
  153. return $sData;
  154. }
  155. public function GetFooter()
  156. {
  157. $sData = "</tbody>\n";
  158. $sData .= "</table>\n";
  159. return $sData;
  160. }
  161. public function GetSupportedFormats()
  162. {
  163. return array('html' => Dict::S('Core:BulkExport:HTMLFormat'));
  164. }
  165. public function GetMimeType()
  166. {
  167. return 'text/html';
  168. }
  169. public function GetFileExtension()
  170. {
  171. return 'html';
  172. }
  173. }