PageRenderTime 47ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/extensions/DataCenter/UI/Widgets/Export.php

https://github.com/ChuguluGames/mediawiki-svn
PHP | 350 lines | 281 code | 9 blank | 60 comment | 15 complexity | 5e6133e0c6abc16c952291d93c2b47c3 MD5 | raw file
  1. <?php
  2. /**
  3. * UI Class for DataCenter extension
  4. *
  5. * @file
  6. * @ingroup Extensions
  7. */
  8. class DataCenterWidgetExport extends DataCenterWidget {
  9. /* Private Static Members */
  10. private static $defaultParameters = array(
  11. /**
  12. * XML ID attribute of widget
  13. * @datatype string
  14. */
  15. 'id' => 'export',
  16. /**
  17. * CSS class of widget
  18. * @datatype string
  19. */
  20. 'class' => 'widget-export',
  21. /**
  22. * Name of category of table to export
  23. * @datatype string
  24. */
  25. 'category' => null,
  26. /**
  27. * Name of type of table to export
  28. * @datatype string
  29. */
  30. 'type' => null,
  31. );
  32. private static $defaultAttributes = array(
  33. /**
  34. * Default XML attributes for table
  35. */
  36. 'table' => array(
  37. 'width' => '100%',
  38. 'cellpadding' => 5,
  39. 'cellspacing' => 0,
  40. 'border' => 0,
  41. ),
  42. /**
  43. * Default XML attributes for heading cell
  44. */
  45. 'heading' => array(
  46. 'align' => 'left',
  47. 'colspan' => 3
  48. ),
  49. /**
  50. * Default XML attributes for label cell
  51. */
  52. 'label' => array(
  53. 'class' => 'label',
  54. 'align' => 'left',
  55. 'nowrap' => 'nowrap',
  56. ),
  57. /**
  58. * Default XML attributes for label cell
  59. */
  60. 'input' => array(
  61. 'class' => 'input',
  62. 'align' => 'left',
  63. 'width' => '200',
  64. ),
  65. /**
  66. * Default XML attributes for buttons cell
  67. */
  68. 'buttons' => array(
  69. 'class' => 'buttons',
  70. 'align' => 'right',
  71. 'colspan' => 3
  72. ),
  73. );
  74. /* Static Functions */
  75. public static function render( array $parameters ) {
  76. global $wgUser;
  77. // Gets current path
  78. $path = DataCenterPage::getPath();
  79. // Sets Defaults
  80. $parameters = array_merge( self::$defaultParameters, $parameters );
  81. // Begins widget
  82. $xmlOutput = parent::begin( $parameters['class'] );
  83. // Builds form attributes
  84. $formAttributes = array(
  85. 'id' => 'form_export',
  86. 'name' => 'form_export',
  87. 'method' => 'post',
  88. 'action' => DataCenterXml::url( $path ),
  89. );
  90. // Begins form
  91. $xmlOutput .= DataCenterXml::open( 'form', $formAttributes );
  92. // Begins table
  93. $xmlOutput .= DataCenterXml::open(
  94. 'table', self::$defaultAttributes['table']
  95. );
  96. // Adds ...
  97. $xmlOutput .= DataCenterXml::row(
  98. DataCenterXml::cell(
  99. self::$defaultAttributes['label'],
  100. DataCenterUI::message( 'field', 'format' )
  101. ),
  102. DataCenterXml::cell(
  103. self::$defaultAttributes['input'],
  104. DataCenterUI::renderInput(
  105. 'list',
  106. array(
  107. 'name' => 'meta[format]',
  108. 'options' => array( 'csv' => 'csv' )
  109. )
  110. )
  111. )
  112. );
  113. $xmlOutput .= DataCenterXml::row(
  114. DataCenterXml::cell(
  115. self::$defaultAttributes['label'],
  116. DataCenterUI::message( 'field', 'include-meta' )
  117. ),
  118. DataCenterXml::cell(
  119. self::$defaultAttributes['input'],
  120. DataCenterUI::renderInput(
  121. 'boolean',
  122. array(
  123. 'name' => 'meta[include-meta]',
  124. 'value' => true,
  125. )
  126. )
  127. )
  128. );
  129. if ( DataCenterPage::userCan( 'export' ) ) {
  130. // Adds reset and submit button
  131. $xmlOutput .= DataCenterXML::row(
  132. DataCenterXml::cell(
  133. self::$defaultAttributes['buttons'],
  134. DataCenterXml::tag(
  135. 'input',
  136. array(
  137. 'type' => 'reset',
  138. 'name' => 'reset',
  139. 'class' => 'reset',
  140. 'value' => DataCenterUI::message(
  141. 'label', 'reset'
  142. ),
  143. )
  144. ) .
  145. DataCenterXml::tag(
  146. 'input',
  147. array(
  148. 'type' => 'submit',
  149. 'name' => 'submit',
  150. 'class' => 'submit',
  151. 'value' => DataCenterUI::message(
  152. 'label', 'export'
  153. ),
  154. )
  155. )
  156. )
  157. );
  158. }
  159. $xmlOutput .= DataCenterXml::close( 'table' );
  160. // Adds category field
  161. $xmlOutput .= DataCenterXml::tag(
  162. 'input', array(
  163. 'type' => 'hidden',
  164. 'name' => 'meta[category]',
  165. 'value' => $parameters['category'],
  166. )
  167. );
  168. // Adds type field
  169. $xmlOutput .= DataCenterXml::tag(
  170. 'input', array(
  171. 'type' => 'hidden',
  172. 'name' => 'meta[type]',
  173. 'value' => $parameters['type'],
  174. )
  175. );
  176. // Adds do field
  177. $xmlOutput .= DataCenterXml::tag(
  178. 'input', array(
  179. 'type' => 'hidden',
  180. 'name' => 'do',
  181. 'value' => 'export'
  182. )
  183. );
  184. // Adds token field
  185. $xmlOutput .= DataCenterXml::tag(
  186. 'input', array(
  187. 'type' => 'hidden',
  188. 'name' => 'token',
  189. 'value' => $wgUser->editToken()
  190. )
  191. );
  192. // Adds success field
  193. $xmlOutput .= DataCenterXml::tag(
  194. 'input',
  195. array(
  196. 'type' => 'hidden',
  197. 'name' => 'success',
  198. 'value' => DataCenterXml::url( $path )
  199. )
  200. );
  201. // Adds failure field
  202. $xmlOutput .= DataCenterXml::tag(
  203. 'input',
  204. array(
  205. 'type' => 'hidden',
  206. 'name' => 'failure',
  207. 'value' => DataCenterXml::url( $path )
  208. )
  209. );
  210. // Adds canellation field
  211. $xmlOutput .= DataCenterXml::tag(
  212. 'input',
  213. array(
  214. 'type' => 'hidden',
  215. 'name' => 'cancellation',
  216. 'value' => DataCenterXml::url( $path )
  217. )
  218. );
  219. $xmlOutput .= DataCenterXml::close( 'form' );
  220. // Ends widget
  221. $xmlOutput .= parent::end();
  222. // Returns results
  223. return $xmlOutput;
  224. }
  225. public static function export( array $data ) {
  226. global $wgOut;
  227. // Disables MediaWiki output
  228. $wgOut->disable();
  229. // Gets current path
  230. $path = DataCenterPage::getPath();
  231. // Gets time in a nice format
  232. $date = date( 'Y-m-d' );
  233. $fileName = DataCenterUI::message( 'datacenter' ) .
  234. ' - ' .
  235. DataCenterUI::message(
  236. 'label',
  237. 'export-type',
  238. DataCenterUI::message( 'type', $data['meta']['type'] ) .
  239. ' ' .
  240. DataCenterUI::message( 'category', $data['meta']['category'] )
  241. ) .
  242. ' - ' . $date . '.' . $data['meta']['format'];
  243. // Sets headers for downloading CSV file
  244. header( 'Content-type: application/octet-stream' );
  245. header( "Content-Disposition: attachment; filename=\"{$fileName}\"" );
  246. $rows = DataCenterDB::getRows(
  247. 'DataCenterDBRow',
  248. $data['meta']['category'],
  249. $data['meta']['type']
  250. );
  251. $useMeta = (
  252. $data['meta']['include-meta'] == 1 &&
  253. (
  254. (
  255. $data['meta']['category'] == 'facility' &&
  256. DataCenterDB::isFacilityType( $data['meta']['type'] )
  257. ) ||
  258. (
  259. $data['meta']['category'] == 'asset' &&
  260. DataCenterDB::isAssetType( $data['meta']['type'] )
  261. ) ||
  262. (
  263. $data['meta']['category'] == 'model' &&
  264. DataCenterDB::isModelType( $data['meta']['type'] )
  265. )
  266. )
  267. );
  268. if ( $data['meta']['format'] == 'csv' ) {
  269. $metaFieldsTable = null;
  270. $lines = array();
  271. $fieldNames = '';
  272. $first = true;
  273. foreach ( $rows as $row ) {
  274. $line = '';
  275. $fields = $row->get();
  276. foreach ( $fields as $field => $value ) {
  277. $line .= self::exportValue( $value );
  278. }
  279. if ( $first ) {
  280. foreach ( $fields as $field => $value ) {
  281. $fieldNames .= self::exportValue(
  282. DataCenterUI::message( 'field', $field )
  283. );
  284. }
  285. }
  286. if ( $useMeta ) {
  287. $component = DataCenterDBComponent::newFromClass(
  288. 'DataCenterDBComponent',
  289. $data['meta']['category'],
  290. $data['meta']['type'],
  291. $fields
  292. );
  293. if ( !$metaFieldsTable ) {
  294. $metaFields = $component->getMetaFields();
  295. $metaFieldsTable = array();
  296. foreach ( $metaFields as $metaField ) {
  297. $metaFieldsTable[] = $metaField->get( 'field' );
  298. }
  299. if ( $first ) {
  300. foreach ( $metaFields as $metaField ) {
  301. $fieldNames .= self::exportValue(
  302. $metaField->get( 'name' )
  303. );
  304. }
  305. }
  306. }
  307. $metaValues = $component->getMetaValues();
  308. $metaValuesTable = array();
  309. foreach ( $metaValues as $metaValue ) {
  310. $metaValuesTable[$metaValue->get( 'field' )] =
  311. $metaValue->get( 'value' );
  312. }
  313. foreach ( $metaFieldsTable as $metaField ) {
  314. if ( isset( $metaValuesTable[$metaField] ) ) {
  315. $line .= self::exportValue(
  316. $metaValuesTable[$metaField]
  317. );
  318. } else {
  319. $line .= ',';
  320. }
  321. }
  322. }
  323. $lines[] = rtrim( $line, ',' ) . "\r\n";
  324. $first = false;
  325. }
  326. echo rtrim( $fieldNames, ',' ) . "\r\n";
  327. echo implode( $lines );
  328. }
  329. }
  330. private static function exportValue( $value ) {
  331. if (
  332. strpos( $value, ',' ) !== false ||
  333. strpos( $value, '"' ) !== false ||
  334. strpos( $value, "\n" ) !== false
  335. ) {
  336. return '"' . str_replace( '"', '""', $value ) . '",';
  337. } else {
  338. return $value . ',';
  339. }
  340. }
  341. }