htmlbulkexport.class.inc.php 6.6 KB

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