spreadsheetbulkexport.class.inc.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. if ($oObj)
  60. {
  61. $sRet = $oObj->GetAsHTML($sAttCode);
  62. }
  63. else
  64. {
  65. $sRet = '';
  66. }
  67. return $sRet;
  68. }
  69. public function GetHeader()
  70. {
  71. $oSet = new DBObjectSet($this->oSearch);
  72. $this->aStatusInfo['status'] = 'running';
  73. $this->aStatusInfo['position'] = 0;
  74. $this->aStatusInfo['total'] = $oSet->Count();
  75. $aData = array();
  76. foreach($this->aStatusInfo['fields'] as $iCol => $aFieldSpec)
  77. {
  78. $sColLabel = $aFieldSpec['sColLabel'];
  79. if ($aFieldSpec['sAttCode'] != 'id')
  80. {
  81. $oAttDef = MetaModel::GetAttributeDef($aFieldSpec['sClass'], $aFieldSpec['sAttCode']);
  82. $oFinalAttDef = $oAttDef->GetFinalAttDef();
  83. if (get_class($oFinalAttDef) == 'AttributeDateTime')
  84. {
  85. $aData[] = $sColLabel.' ('.Dict::S('UI:SplitDateTime-Date').')';
  86. $aData[] = $sColLabel.' ('.Dict::S('UI:SplitDateTime-Time').')';
  87. }
  88. else
  89. {
  90. $aData[] = $sColLabel;
  91. }
  92. }
  93. }
  94. $sData = "<table border=\"1\">\n";
  95. $sData .= "<tr>\n";
  96. foreach($aData as $sLabel)
  97. {
  98. $sData .= "<td>".$sLabel."</td>\n";
  99. }
  100. $sData .= "</tr>\n";
  101. return $sData;
  102. }
  103. public function GetNextChunk(&$aStatus)
  104. {
  105. $sRetCode = 'run';
  106. $iPercentage = 0;
  107. $oSet = new DBObjectSet($this->oSearch);
  108. $oSet->SetLimit($this->iChunkSize, $this->aStatusInfo['position']);
  109. $this->OptimizeColumnLoad($oSet);
  110. $iCount = 0;
  111. $sData = '';
  112. $iPreviousTimeLimit = ini_get('max_execution_time');
  113. $iLoopTimeLimit = MetaModel::GetConfig()->Get('max_execution_time_per_loop');
  114. while($aRow = $oSet->FetchAssoc())
  115. {
  116. set_time_limit($iLoopTimeLimit);
  117. $sData .= "<tr>";
  118. foreach($this->aStatusInfo['fields'] as $iCol => $aFieldSpec)
  119. {
  120. $sAlias = $aFieldSpec['sAlias'];
  121. $sAttCode = $aFieldSpec['sAttCode'];
  122. $sField = '';
  123. $oObj = $aRow[$sAlias];
  124. if ($oObj == null)
  125. {
  126. $sData .= "<td x:str>$sField</td>";
  127. continue;
  128. }
  129. switch($sAttCode)
  130. {
  131. case 'id':
  132. $sField = $oObj->GetName();
  133. $sData .= "<td>$sField</td>";
  134. break;
  135. default:
  136. $oAttDef = MetaModel::GetAttributeDef(get_class($oObj), $sAttCode);
  137. $oFinalAttDef = $oAttDef->GetFinalAttDef();
  138. if (get_class($oFinalAttDef) == 'AttributeDateTime')
  139. {
  140. $iDate = AttributeDateTime::GetAsUnixSeconds($oObj->Get($sAttCode));
  141. $sData .= '<td>'.date('Y-m-d', $iDate).'</td>'; // Add the first column directly
  142. $sField = date('H:i:s', $iDate); // Will add the second column below
  143. $sData .= "<td>$sField</td>";
  144. }
  145. else if($oAttDef instanceof AttributeCaseLog)
  146. {
  147. $rawValue = $oObj->Get($sAttCode);
  148. $sField = str_replace("\n", "<br/>", htmlentities($rawValue->__toString(), ENT_QUOTES, 'UTF-8'));
  149. // Trick for Excel: treat the content as text even if it begins with an equal sign
  150. $sData .= "<td x:str>$sField</td>";
  151. }
  152. else
  153. {
  154. $rawValue = $oObj->Get($sAttCode);
  155. // Due to custom formatting rules, empty friendlynames may be rendered as non-empty strings
  156. // let's fix this and make sure we render an empty string if the key == 0
  157. if ($oAttDef instanceof AttributeFriendlyName)
  158. {
  159. $sKeyAttCode = $oAttDef->GetKeyAttCode();
  160. if ($sKeyAttCode != 'id')
  161. {
  162. if ($oObj->Get($sKeyAttCode) == 0)
  163. {
  164. $sValue = '';
  165. }
  166. }
  167. }
  168. if ($this->bLocalizeOutput)
  169. {
  170. $sField = htmlentities($oFinalAttDef->GetEditValue($rawValue), ENT_QUOTES, 'UTF-8');
  171. }
  172. else
  173. {
  174. $sField = htmlentities($rawValue, ENT_QUOTES, 'UTF-8');
  175. }
  176. $sData .= "<td>$sField</td>";
  177. }
  178. }
  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 = "</table>\n";
  203. return $sData;
  204. }
  205. public function GetSupportedFormats()
  206. {
  207. return array('spreadsheet' => Dict::S('Core:BulkExport:SpreadsheetFormat'));
  208. }
  209. public function GetMimeType()
  210. {
  211. return 'text/html';
  212. }
  213. public function GetFileExtension()
  214. {
  215. return 'html';
  216. }
  217. }