htmlbulkexport.class.inc.php 6.4 KB

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