htmlbulkexport.class.inc.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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(DBObject $oObj, $sAttCode)
  47. {
  48. return $oObj->GetAsHTML($sAttCode);
  49. }
  50. public function GetHeader()
  51. {
  52. $oSet = new DBObjectSet($this->oSearch);
  53. $this->aStatusInfo['status'] = 'running';
  54. $this->aStatusInfo['position'] = 0;
  55. $this->aStatusInfo['total'] = $oSet->Count();
  56. $aSelectedClasses = $this->oSearch->GetSelectedClasses();
  57. $aData = array();
  58. foreach($this->aStatusInfo['fields'] as $sExtendedAttCode)
  59. {
  60. if (preg_match('/^([^\.]+)\.(.+)$/', $sExtendedAttCode, $aMatches))
  61. {
  62. $sAlias = $aMatches[1];
  63. $sAttCode = $aMatches[2];
  64. }
  65. else
  66. {
  67. $sAlias = reset($aSelectedClasses);
  68. $sAttCode = $sExtendedAttCode;
  69. }
  70. if (!array_key_exists($sAlias, $aSelectedClasses))
  71. {
  72. throw new Exception("Invalid alias '$sAlias' for the column '$sExtendedAttCode'. Availables aliases: '".implode("', '", array_keys($aSelectedClasses))."'");
  73. }
  74. $sClass = $aSelectedClasses[$sAlias];
  75. switch($sAttCode)
  76. {
  77. case 'id':
  78. if (count($aSelectedClasses) > 1)
  79. {
  80. $aData[] = $sAlias.'.id'; //@@@
  81. }
  82. else
  83. {
  84. $aData[] = 'id'; //@@@
  85. }
  86. break;
  87. default:
  88. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  89. if (count($aSelectedClasses) > 1)
  90. {
  91. $aData[] = $sAlias.'.'.$oAttDef->GetLabel();
  92. }
  93. else
  94. {
  95. $aData[] = $oAttDef->GetLabel();
  96. }
  97. }
  98. }
  99. $sData = "<table class=\"listResults\">\n";
  100. $sData .= "<thead>\n";
  101. $sData .= "<tr>\n";
  102. foreach($aData as $sLabel)
  103. {
  104. $sData .= "<th>".$sLabel."</th>\n";
  105. }
  106. $sData .= "</tr>\n";
  107. $sData .= "</thead>\n";
  108. $sData .= "<tbody>\n";
  109. return $sData;
  110. }
  111. public function GetNextChunk(&$aStatus)
  112. {
  113. $sRetCode = 'run';
  114. $iPercentage = 0;
  115. $oSet = new DBObjectSet($this->oSearch);
  116. $aSelectedClasses = $this->oSearch->GetSelectedClasses();
  117. $oSet->SetLimit($this->iChunkSize, $this->aStatusInfo['position']);
  118. $aAliasByField = array();
  119. $aColumnsToLoad = array();
  120. // Prepare the list of aliases / columns to load
  121. foreach($this->aStatusInfo['fields'] as $sExtendedAttCode)
  122. {
  123. if (preg_match('/^([^\.]+)\.(.+)$/', $sExtendedAttCode, $aMatches))
  124. {
  125. $sAlias = $aMatches[1];
  126. $sAttCode = $aMatches[2];
  127. }
  128. else
  129. {
  130. $sAlias = reset($aSelectedClasses);
  131. $sAttCode = $sExtendedAttCode;
  132. }
  133. if (!array_key_exists($sAlias, $aSelectedClasses))
  134. {
  135. throw new Exception("Invalid alias '$sAlias' for the column '$sExtendedAttCode'. Availables aliases: '".implode("', '", array_keys($aSelectedClasses))."'");
  136. }
  137. if (!array_key_exists($sAlias, $aColumnsToLoad))
  138. {
  139. $aColumnsToLoad[$sAlias] = array();
  140. }
  141. if ($sAttCode != 'id')
  142. {
  143. // id is not a real attribute code and, moreover, is always loaded
  144. $aColumnsToLoad[$sAlias][] = $sAttCode;
  145. }
  146. $aAliasByField[$sExtendedAttCode] = array('alias' => $sAlias, 'attcode' => $sAttCode);
  147. }
  148. $iCount = 0;
  149. $sData = '';
  150. $oSet->OptimizeColumnLoad($aColumnsToLoad);
  151. $iPreviousTimeLimit = ini_get('max_execution_time');
  152. $iLoopTimeLimit = MetaModel::GetConfig()->Get('max_execution_time_per_loop');
  153. while($aRow = $oSet->FetchAssoc())
  154. {
  155. set_time_limit($iLoopTimeLimit);
  156. $sFirstAlias = reset($aSelectedClasses);
  157. $sHilightClass = $aRow[$sFirstAlias]->GetHilightClass();
  158. if ($sHilightClass != '')
  159. {
  160. $sData .= "<tr class=\"$sHilightClass\">";
  161. }
  162. else
  163. {
  164. $sData .= "<tr>";
  165. }
  166. foreach($aAliasByField as $aAttCode)
  167. {
  168. $sField = '';
  169. switch($aAttCode['attcode'])
  170. {
  171. case 'id':
  172. $sField = $aRow[$aAttCode['alias']]->GetHyperlink();
  173. break;
  174. default:
  175. $sField = $aRow[$aAttCode['alias']]->GetAsHtml($aAttCode['attcode']);
  176. }
  177. $sValue = ($sField === '') ? '&nbsp;' : $sField;
  178. $sData .= "<td>$sValue</td>";
  179. }
  180. $sData .= "</tr>";
  181. $iCount++;
  182. }
  183. set_time_limit($iPreviousTimeLimit);
  184. $this->aStatusInfo['position'] += $this->iChunkSize;
  185. if ($this->aStatusInfo['total'] == 0)
  186. {
  187. $iPercentage = 100;
  188. }
  189. else
  190. {
  191. $iPercentage = floor(min(100.0, 100.0*$this->aStatusInfo['position']/$this->aStatusInfo['total']));
  192. }
  193. if ($iCount < $this->iChunkSize)
  194. {
  195. $sRetCode = 'done';
  196. }
  197. $aStatus = array('code' => $sRetCode, 'message' => Dict::S('Core:BulkExport:RetrievingData'), 'percentage' => $iPercentage);
  198. return $sData;
  199. }
  200. public function GetFooter()
  201. {
  202. $sData = "</tbody>\n";
  203. $sData .= "</table>\n";
  204. return $sData;
  205. }
  206. public function GetSupportedFormats()
  207. {
  208. return array('html' => Dict::S('Core:BulkExport:HTMLFormat'));
  209. }
  210. public function GetMimeType()
  211. {
  212. return 'text/html';
  213. }
  214. public function GetFileExtension()
  215. {
  216. return 'html';
  217. }
  218. }