htmlbulkexport.class.inc.php 5.1 KB

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