spreadsheetbulkexport.class.inc.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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: "spreadsheet" export: a simplified HTML export in which the date/time columns are split in two column: date AND time
  20. *
  21. * @copyright Copyright (C) 2015 Combodo SARL
  22. * @license http://opensource.org/licenses/AGPL-3.0
  23. */
  24. class SpreadsheetBulkExport extends TabularBulkExport
  25. {
  26. public function DisplayUsage(Page $oP)
  27. {
  28. $oP->p(" * spreadsheet format options:");
  29. $oP->p(" *\tfields: (mandatory) the comma separated list of field codes to export (e.g: name,org_id,service_name...).");
  30. $oP->p(" *\tno_localize: (optional) pass 1 to retrieve the raw (untranslated) values for enumerated fields. Default: 0.");
  31. }
  32. public function EnumFormParts()
  33. {
  34. return array_merge(parent::EnumFormParts(), array('spreadsheet_options' => array('no-localize') ,'interactive_fields_spreadsheet' => array('interactive_fields_spreadsheet')));
  35. }
  36. public function DisplayFormPart(WebPage $oP, $sPartId)
  37. {
  38. switch($sPartId)
  39. {
  40. case 'interactive_fields_spreadsheet':
  41. $this->GetInteractiveFieldsWidget($oP, 'interactive_fields_spreadsheet');
  42. break;
  43. case 'spreadsheet_options':
  44. $sChecked = (utils::ReadParam('no_localize', 0) == 1) ? ' checked ' : '';
  45. $oP->add('<fieldset><legend>'.Dict::S('Core:BulkExport:SpreadsheetOptions').'</legend>');
  46. $oP->add('<table>');
  47. $oP->add('<tr>');
  48. $oP->add('<td><input type="checkbox" id="spreadsheet_no_localize" name="no_localize" value="1"'.$sChecked.'><label for="spreadsheet_no_localize"> '.Dict::S('Core:BulkExport:OptionNoLocalize').'</label></td>');
  49. $oP->add('</tr>');
  50. $oP->add('</table>');
  51. $oP->add('</fieldset>');
  52. break;
  53. default:
  54. return parent:: DisplayFormPart($oP, $sPartId);
  55. }
  56. }
  57. protected function GetSampleData($oObj, $sAttCode)
  58. {
  59. return $this->GetValue($oObj, $sAttCode);
  60. }
  61. protected function GetValue($oObj, $sAttCode)
  62. {
  63. switch($sAttCode)
  64. {
  65. case 'id':
  66. $sRet = $oObj->GetKey();
  67. break;
  68. default:
  69. $value = $oObj->Get($sAttCode);
  70. $oAttDef = MetaModel::GetAttributeDef(get_class($oObj), $sAttCode);
  71. if ($value instanceof ormCaseLog)
  72. {
  73. $sRet = str_replace("\n", "<br/>", htmlentities($value->__toString(), ENT_QUOTES, 'UTF-8'));
  74. }
  75. elseif ($value instanceof ormStopWatch)
  76. {
  77. $sRet = $value->GetTimeSpent();
  78. }
  79. elseif ($oAttDef instanceof AttributeString)
  80. {
  81. $sRet = $oObj->GetAsHTML($sAttCode);
  82. }
  83. else
  84. {
  85. if ($this->bLocalizeOutput)
  86. {
  87. $sRet = htmlentities($oObj->GetEditValue(), ENT_QUOTES, 'UTF-8');
  88. }
  89. else
  90. {
  91. $sRet = htmlentities($value, ENT_QUOTES, 'UTF-8');
  92. }
  93. }
  94. }
  95. return $sRet;
  96. }
  97. public function SetHttpHeaders(WebPage $oPage)
  98. {
  99. // Integration within MS-Excel web queries + HTTPS + IIS:
  100. // MS-IIS set these header values with no-cache... while Excel fails to do the job if using HTTPS
  101. // Then the fix is to force the reset of header values Pragma and Cache-control
  102. $oPage->add_header("Pragma:", true);
  103. $oPage->add_header("Cache-control:", true);
  104. }
  105. public function GetHeader()
  106. {
  107. $oSet = new DBObjectSet($this->oSearch);
  108. $this->aStatusInfo['status'] = 'running';
  109. $this->aStatusInfo['position'] = 0;
  110. $this->aStatusInfo['total'] = $oSet->Count();
  111. $aData = array();
  112. foreach($this->aStatusInfo['fields'] as $iCol => $aFieldSpec)
  113. {
  114. $sColLabel = $aFieldSpec['sColLabel'];
  115. if ($aFieldSpec['sAttCode'] != 'id')
  116. {
  117. $oAttDef = MetaModel::GetAttributeDef($aFieldSpec['sClass'], $aFieldSpec['sAttCode']);
  118. $oFinalAttDef = $oAttDef->GetFinalAttDef();
  119. if (get_class($oFinalAttDef) == 'AttributeDateTime')
  120. {
  121. $aData[] = $sColLabel.' ('.Dict::S('UI:SplitDateTime-Date').')';
  122. $aData[] = $sColLabel.' ('.Dict::S('UI:SplitDateTime-Time').')';
  123. }
  124. else
  125. {
  126. $aData[] = $sColLabel;
  127. }
  128. }
  129. else
  130. {
  131. $aData[] = $sColLabel;
  132. }
  133. }
  134. $sData = '';
  135. $sData .= '<style>table br {mso-data-placement:same-cell;}</style>'; // Trick for Excel: keep line breaks inside the same cell !
  136. $sData .= "<table border=\"1\">\n";
  137. $sData .= "<tr>\n";
  138. foreach($aData as $sLabel)
  139. {
  140. $sData .= "<td>".$sLabel."</td>\n";
  141. }
  142. $sData .= "</tr>\n";
  143. return $sData;
  144. }
  145. public function GetNextChunk(&$aStatus)
  146. {
  147. $sRetCode = 'run';
  148. $iPercentage = 0;
  149. $oSet = new DBObjectSet($this->oSearch);
  150. $oSet->SetLimit($this->iChunkSize, $this->aStatusInfo['position']);
  151. $this->OptimizeColumnLoad($oSet);
  152. $iCount = 0;
  153. $sData = '';
  154. $iPreviousTimeLimit = ini_get('max_execution_time');
  155. $iLoopTimeLimit = MetaModel::GetConfig()->Get('max_execution_time_per_loop');
  156. while($aRow = $oSet->FetchAssoc())
  157. {
  158. set_time_limit($iLoopTimeLimit);
  159. $sData .= "<tr>";
  160. foreach($this->aStatusInfo['fields'] as $iCol => $aFieldSpec)
  161. {
  162. $sAlias = $aFieldSpec['sAlias'];
  163. $sAttCode = $aFieldSpec['sAttCode'];
  164. $sField = '';
  165. $oObj = $aRow[$sAlias];
  166. if ($oObj == null)
  167. {
  168. $sData .= "<td x:str></td>";
  169. continue;
  170. }
  171. switch($sAttCode)
  172. {
  173. case 'id':
  174. $sField = $oObj->GetKey();
  175. $sData .= "<td>$sField</td>";
  176. break;
  177. default:
  178. $oAttDef = MetaModel::GetAttributeDef(get_class($oObj), $sAttCode);
  179. $oFinalAttDef = $oAttDef->GetFinalAttDef();
  180. if (get_class($oFinalAttDef) == 'AttributeDateTime')
  181. {
  182. $iDate = AttributeDateTime::GetAsUnixSeconds($oObj->Get($sAttCode));
  183. $sData .= '<td>'.date('Y-m-d', $iDate).'</td>'; // Add the first column directly
  184. $sField = date('H:i:s', $iDate); // Will add the second column below
  185. $sData .= "<td>$sField</td>";
  186. }
  187. else if($oAttDef instanceof AttributeCaseLog)
  188. {
  189. $rawValue = $oObj->Get($sAttCode);
  190. $sField = str_replace("\n", "<br/>", htmlentities($rawValue->__toString(), ENT_QUOTES, 'UTF-8'));
  191. // Trick for Excel: treat the content as text even if it begins with an equal sign
  192. $sData .= "<td x:str>$sField</td>";
  193. }
  194. else if($oAttDef instanceof AttributeString)
  195. {
  196. $sField = $oObj->GetAsHTML($sAttCode, $this->bLocalizeOutput);
  197. $sData .= "<td x:str>$sField</td>";
  198. }
  199. else
  200. {
  201. $rawValue = $oObj->Get($sAttCode);
  202. if ($this->bLocalizeOutput)
  203. {
  204. $sField = htmlentities($oFinalAttDef->GetEditValue($rawValue), ENT_QUOTES, 'UTF-8');
  205. }
  206. else
  207. {
  208. $sField = htmlentities($rawValue, ENT_QUOTES, 'UTF-8');
  209. }
  210. $sData .= "<td>$sField</td>";
  211. }
  212. }
  213. }
  214. $sData .= "</tr>";
  215. $iCount++;
  216. }
  217. set_time_limit($iPreviousTimeLimit);
  218. $this->aStatusInfo['position'] += $this->iChunkSize;
  219. if ($this->aStatusInfo['total'] == 0)
  220. {
  221. $iPercentage = 100;
  222. }
  223. else
  224. {
  225. $iPercentage = floor(min(100.0, 100.0*$this->aStatusInfo['position']/$this->aStatusInfo['total']));
  226. }
  227. if ($iCount < $this->iChunkSize)
  228. {
  229. $sRetCode = 'done';
  230. }
  231. $aStatus = array('code' => $sRetCode, 'message' => Dict::S('Core:BulkExport:RetrievingData'), 'percentage' => $iPercentage);
  232. return $sData;
  233. }
  234. public function GetFooter()
  235. {
  236. $sData = "</table>\n";
  237. return $sData;
  238. }
  239. public function GetSupportedFormats()
  240. {
  241. return array('spreadsheet' => Dict::S('Core:BulkExport:SpreadsheetFormat'));
  242. }
  243. public function GetMimeType()
  244. {
  245. return 'text/html';
  246. }
  247. public function GetFileExtension()
  248. {
  249. return 'html';
  250. }
  251. }