htmlbulkexport.class.inc.php 7.1 KB

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