PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/src/Varien/Convert/Parser/Xml/Excel.php

https://bitbucket.org/kdms/sh-magento
PHP | 230 lines | 155 code | 21 blank | 54 comment | 20 complexity | 1e7e9b1826f1db597c915afde78e7cd8 MD5 | raw file
  1. <?php
  2. /**
  3. * Magento Enterprise Edition
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Magento Enterprise Edition License
  8. * that is bundled with this package in the file LICENSE_EE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://www.magentocommerce.com/license/enterprise-edition
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Varien
  22. * @package Varien_Convert
  23. * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://www.magentocommerce.com/license/enterprise-edition
  25. */
  26. /**
  27. * Convert excel xml parser
  28. *
  29. * @category Varien
  30. * @package Varien_Convert
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Varien_Convert_Parser_Xml_Excel extends Varien_Convert_Parser_Abstract
  34. {
  35. /**
  36. * XML instance for a cell data
  37. *
  38. * @var SimpleXMLElement
  39. */
  40. protected $_xmlElement;
  41. public function parse()
  42. {
  43. $this->validateDataString();
  44. $dom = new DOMDocument();
  45. $dom->loadXML($this->getData());
  46. $worksheets = $dom->getElementsByTagName('Worksheet');
  47. foreach ($worksheets as $worksheet) {
  48. $wsName = $worksheet->getAttribute('ss:Name');
  49. $rows = $worksheet->getElementsByTagName('Row');
  50. $firstRow = true;
  51. $fieldNames = array();
  52. $wsData = array();
  53. foreach ($rows as $row) {
  54. $index = 1;
  55. $cells = $row->getElementsByTagName('Cell');
  56. $rowData = array();
  57. foreach ($cells as $cell) {
  58. $value = $cell->getElementsByTagName('Data')->item(0)->nodeValue;
  59. $ind = $cell->getAttribute('ss:Index');
  60. if (!is_null($ind) && $ind>0) {
  61. $index = $ind;
  62. }
  63. if ($firstRow && !$this->getVar('fieldnames')) {
  64. $fieldNames[$index] = 'column'.$index;
  65. }
  66. if ($firstRow && $this->getVar('fieldnames')) {
  67. $fieldNames[$index] = $value;
  68. } else {
  69. $rowData[$fieldNames[$index]] = $value;
  70. }
  71. $index++;
  72. }
  73. $firstRow = false;
  74. if (!empty($rowData)) {
  75. $wsData[] = $rowData;
  76. }
  77. }
  78. $data[$wsName] = $wsData;
  79. $this->addException('Found worksheet "'.$wsName.'" with '.sizeof($wsData).' row(s)');
  80. }
  81. if ($wsName = $this->getVar('single_sheet')) {
  82. if (isset($data[$wsName])) {
  83. $data = $data[$wsName];
  84. } else {
  85. reset($data);
  86. $data = current($data);
  87. }
  88. }
  89. $this->setData($data);
  90. return $this;
  91. }
  92. public function unparse()
  93. {
  94. if ($wsName = $this->getVar('single_sheet')) {
  95. $data = array($wsName => $this->getData());
  96. } else {
  97. $data = $this->getData();
  98. }
  99. $this->validateDataGrid();
  100. $xml = '<'.'?xml version="1.0"?'.'><'.'?mso-application progid="Excel.Sheet"?'.'>
  101. <Workbook xmlns:x="urn:schemas-microsoft-com:office:excel"
  102. xmlns="urn:schemas-microsoft-com:office:spreadsheet"
  103. xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">';
  104. if (is_array($data)) {
  105. foreach ($data as $wsName=>$wsData) {
  106. if (!is_array($wsData)) {
  107. continue;
  108. }
  109. $fields = $this->getGridFields($wsData);
  110. $xml .= '<Worksheet ss:Name="'.$wsName.'"><ss:Table>';
  111. if ($this->getVar('fieldnames')) {
  112. $xml .= '<ss:Row>';
  113. foreach ($fields as $fieldName) {
  114. $xml .= '<ss:Cell><Data ss:Type="String">'.$fieldName.'</Data></ss:Cell>';
  115. }
  116. $xml .= '</ss:Row>';
  117. }
  118. foreach ($wsData as $i=>$row) {
  119. if (!is_array($row)) {
  120. continue;
  121. }
  122. $xml .= '<ss:Row>';
  123. foreach ($fields as $fieldName) {
  124. $data = isset($row[$fieldName]) ? $row[$fieldName] : '';
  125. $xml .= '<ss:Cell><Data ss:Type="String">'.$data.'</Data></ss:Cell>';
  126. }
  127. $xml .= '</ss:Row>';
  128. }
  129. $xml .= '</ss:Table></Worksheet>';
  130. }
  131. }
  132. $xml .= '</Workbook>';
  133. $this->setData($xml);
  134. return $this;
  135. }
  136. /**
  137. * Retrieve Excel 2003 XML Document header XML fragment
  138. *
  139. * @param string $sheetName the Worksheet name
  140. * @return string
  141. */
  142. public function getHeaderXml($sheetName = '')
  143. {
  144. if (empty($sheetName)) {
  145. $sheetName = 'Sheet 1';
  146. }
  147. $sheetName = htmlspecialchars($sheetName);
  148. $xml = '<'.'?xml version="1.0"?'.'><'.'?mso-application progid="Excel.Sheet"?'
  149. . '><Workbook'
  150. . ' xmlns="urn:schemas-microsoft-com:office:spreadsheet"'
  151. . ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'
  152. . ' xmlns:x="urn:schemas-microsoft-com:office:excel"'
  153. . ' xmlns:x2="http://schemas.microsoft.com/office/excel/2003/xml"'
  154. . ' xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"'
  155. . ' xmlns:o="urn:schemas-microsoft-com:office:office"'
  156. . ' xmlns:html="http://www.w3.org/TR/REC-html40"'
  157. . ' xmlns:c="urn:schemas-microsoft-com:office:component:spreadsheet">'
  158. . '<OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office">'
  159. . '</OfficeDocumentSettings>'
  160. . '<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">'
  161. . '</ExcelWorkbook>'
  162. . '<Worksheet ss:Name="' . $sheetName . '">'
  163. . '<Table>';
  164. return $xml;
  165. }
  166. /**
  167. * Retrieve Excel 2003 XML Document footer XML fragment
  168. *
  169. * @return string
  170. */
  171. public function getFooterXml()
  172. {
  173. return '</Table></Worksheet></Workbook>';
  174. }
  175. /**
  176. * Convert an array to Excel 2003 XML Document a Row XML fragment
  177. *
  178. * @param array $row
  179. * @return string
  180. */
  181. public function getRowXml(array $row)
  182. {
  183. $xmlHeader = '<'.'?xml version="1.0"?'.'>' . "\n";
  184. $xmlRegexp = '/^<cell><row>(.*)?<\/row><\/cell>\s?$/ms';
  185. if (is_null($this->_xmlElement)) {
  186. $xmlString = $xmlHeader . '<cell><row></row></cell>';
  187. $this->_xmlElement = new SimpleXMLElement($xmlString, LIBXML_NOBLANKS);
  188. }
  189. $xmlData = array();
  190. $xmlData[] = '<Row>';
  191. foreach ($row as $value) {
  192. $this->_xmlElement->row = htmlspecialchars($value);
  193. $value = str_replace($xmlHeader, '', $this->_xmlElement->asXML());
  194. $value = preg_replace($xmlRegexp, '\\1', $value);
  195. $dataType = "String";
  196. if (is_numeric($value)) {
  197. $dataType = "Number";
  198. // is_numeric(' 96000') returns true, but Excel argues about space
  199. $value = trim($value);
  200. }
  201. $value = str_replace("\r\n", '&#10;', $value);
  202. $value = str_replace("\r", '&#10;', $value);
  203. $value = str_replace("\n", '&#10;', $value);
  204. $xmlData[] = '<Cell><Data ss:Type="'.$dataType.'">'.$value.'</Data></Cell>';
  205. }
  206. $xmlData[] = '</Row>';
  207. return join('', $xmlData);
  208. }
  209. }