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

/extensions/DataCenter/Views/Assets.php

https://github.com/ChuguluGames/mediawiki-svn
PHP | 414 lines | 373 code | 11 blank | 30 comment | 5 complexity | 471197256feee180cf4da2b28e5722e1 MD5 | raw file
  1. <?php
  2. /**
  3. * Racks UI Class for DataCenter extension
  4. *
  5. * @file
  6. * @ingroup Extensions
  7. */
  8. class DataCenterViewAssets extends DataCenterView {
  9. /* Static Members */
  10. public static $options = array(
  11. 'rack' => array(
  12. 'heading' => array(
  13. 'message' => 'racks',
  14. ),
  15. 'details' => array(
  16. 'fields' => array(
  17. 'manufacturer',
  18. 'model' => array( 'field' => 'name' ),
  19. 'units',
  20. ),
  21. ),
  22. ),
  23. 'object' => array(
  24. 'heading' => array(
  25. 'message' => 'objects',
  26. ),
  27. 'details' => array(
  28. 'fields' => array(
  29. 'manufacturer',
  30. 'model' => array( 'field' => 'name' ),
  31. 'units',
  32. 'depth',
  33. 'power',
  34. ),
  35. ),
  36. ),
  37. );
  38. /* Functions */
  39. public function main( $path ) {
  40. if ( !isset( self::$options[$path['type']] ) ) {
  41. return DataCenterUI::renderWidget(
  42. 'body', array(
  43. 'message' => 'invalid-request', 'type' => 'error'
  44. )
  45. );
  46. }
  47. // Gets all assets from database
  48. $assets = DataCenterDB::getAssets(
  49. $path['type'],
  50. array_merge_recursive(
  51. DataCenterDB::buildJoin(
  52. 'model', $path['type'], 'id',
  53. 'asset', $path['type'], 'model',
  54. array( 'name', 'manufacturer' )
  55. ),
  56. DataCenterDB::buildJoin(
  57. 'facility', 'location', 'id',
  58. 'asset', $path['type'], 'location',
  59. array( 'name' => 'location_name' )
  60. ),
  61. DataCenterDB::buildRange( $path )
  62. )
  63. );
  64. $numAssets = DataCenterDB::numAssets( $path['type'] );
  65. // Returns single columm layout with a table
  66. return DataCenterUI::renderLayout(
  67. 'columns',
  68. array(
  69. DataCenterUI::renderLayout(
  70. 'rows',
  71. array(
  72. DataCenterUI::renderWidget(
  73. 'heading',
  74. array(
  75. 'message' => 'assets-type',
  76. 'subject' => DataCenterUI::message(
  77. 'type', $path['type']
  78. )
  79. )
  80. ),
  81. DataCenterUI::renderWidget(
  82. 'table',
  83. array(
  84. 'rows' => $assets,
  85. 'num' => $numAssets,
  86. 'fields' => array(
  87. 'manufacturer',
  88. 'model' => array( 'field' => 'name' ),
  89. 'serial',
  90. 'asset',
  91. 'tense' => array( 'format' => 'option' ),
  92. 'location' => array(
  93. 'field' => 'location_name'
  94. )
  95. ),
  96. 'link' => array(
  97. 'page' => 'assets',
  98. 'type' => $path['type'],
  99. 'id' => '#id',
  100. 'action' => 'view',
  101. ),
  102. )
  103. ),
  104. DataCenterUI::renderWidget(
  105. 'actions',
  106. array(
  107. 'links' => array(
  108. array(
  109. 'page' => 'assets',
  110. 'type' => $path['type'],
  111. 'action' => 'design',
  112. ),
  113. ),
  114. 'rights' => array( 'change' ),
  115. )
  116. )
  117. )
  118. ),
  119. )
  120. );
  121. }
  122. public function history( $path ) {
  123. $asset = DataCenterDB::getAsset( $path['type'], $path['id'] );
  124. return DataCenterUI::renderLayout(
  125. 'columns',
  126. array(
  127. DataCenterUI::renderLayout(
  128. 'rows',
  129. array(
  130. DataCenterUI::renderWidget(
  131. 'heading',
  132. array(
  133. 'message' => 'history-type',
  134. 'subject' => DataCenterUI::message(
  135. 'type', $path['type']
  136. )
  137. )
  138. ),
  139. DataCenterUI::renderWidget(
  140. 'history',
  141. array( 'component' => $asset )
  142. ),
  143. )
  144. ),
  145. )
  146. );
  147. }
  148. public function export( $path ) {
  149. // Returns single columm layout with a table
  150. return DataCenterUI::renderLayout(
  151. 'columns',
  152. array(
  153. DataCenterUI::renderLayout(
  154. 'rows',
  155. array(
  156. DataCenterUI::renderWidget(
  157. 'heading', array( 'message' => 'export' )
  158. ),
  159. DataCenterUI::renderWidget(
  160. 'export',
  161. array(
  162. 'category' => 'asset',
  163. 'type' => $path['type']
  164. )
  165. ),
  166. )
  167. ),
  168. ' '
  169. )
  170. );
  171. }
  172. public function view( $path ) {
  173. // Checks if the user did not provide enough information
  174. if ( !$path['id'] ) {
  175. // Returns error message
  176. return DataCenterUI::message( 'error', 'insufficient-data' );
  177. }
  178. // Gets asset from database
  179. $asset = DataCenterDB::getAsset( $path['type'], $path['id'] );
  180. // Gets location asset is in from database
  181. $location = $asset->getLocation();
  182. // Sets location name to asset
  183. $asset->set( 'location_name', $location->get( 'name' ) );
  184. $asset->set( 'location_region', $location->get( 'region' ) );
  185. $model = $asset->getModel();
  186. // Returns single columm layout with a table
  187. return DataCenterUI::renderLayout(
  188. 'columns',
  189. array(
  190. DataCenterUI::renderLayout(
  191. 'rows',
  192. array(
  193. DataCenterUI::renderWidget(
  194. 'heading', array(
  195. 'message' => 'asset-type',
  196. 'type' => $path['type'],
  197. )
  198. ),
  199. DataCenterUI::renderWidget(
  200. 'details',
  201. array(
  202. 'heading' => array(
  203. 'message' => 'details-for',
  204. 'subject' => $model->get( 'name' ),
  205. ),
  206. 'row' => $asset,
  207. 'fields' => array(
  208. 'tense' => array( 'format' => 'option' ),
  209. 'serial',
  210. 'asset',
  211. 'location' => array(
  212. 'fields' => array(
  213. 'location_name', 'location_region'
  214. ),
  215. 'glue' => ' / ',
  216. )
  217. ),
  218. )
  219. ),
  220. )
  221. ),
  222. DataCenterUI::renderLayout(
  223. 'rows',
  224. array(
  225. DataCenterUI::renderWidget(
  226. 'heading', array(
  227. 'message' => 'model-type',
  228. 'type' => $path['type'],
  229. )
  230. ),
  231. DataCenterUI::renderWidget(
  232. 'details',
  233. array_merge(
  234. self::$options[$path['type']]['details'],
  235. array( 'row' => $model )
  236. )
  237. ),
  238. )
  239. ),
  240. )
  241. );
  242. }
  243. public function design( $path ) {
  244. $options = DataCenterViewModels::$options[$path['type']];
  245. if ( isset( $options['gallery'] ) ) {
  246. // Gets all components from database
  247. $models = DataCenterDB::getModels( $path['type'] );
  248. // Returns single columm layout with a table
  249. return DataCenterUI::renderLayout(
  250. 'columns',
  251. array(
  252. DataCenterUI::renderLayout(
  253. 'rows',
  254. array(
  255. DataCenterUI::renderWidget(
  256. 'heading',
  257. array(
  258. 'message' => 'select-deploy-type',
  259. 'type' => 'model'
  260. )
  261. ),
  262. DataCenterUI::renderWidget(
  263. 'gallery',
  264. array_merge(
  265. $options['gallery'],
  266. array(
  267. 'rows' => $models,
  268. 'link' => array(
  269. 'page' => 'assets',
  270. 'type' => $path['type'],
  271. 'action' => 'deploy',
  272. 'parameter' => '#id',
  273. ),
  274. )
  275. )
  276. ),
  277. )
  278. ),
  279. )
  280. );
  281. }
  282. }
  283. public function deploy( $path ) {
  284. return $this->manage( $path );
  285. }
  286. public function manage( $path ) {
  287. // Checks if...
  288. if (
  289. // No rack asset was specified
  290. !$path['id'] &&
  291. // A single parameter was given
  292. is_scalar( $path['parameter'] )
  293. ) {
  294. // Creates new asset with default parameters
  295. $asset = DataCenterDBAsset::newFromType(
  296. $path['type'],
  297. array('model' => $path['parameter'], 'tense' => 'present' )
  298. );
  299. // Sets action specific parameters
  300. $formParameters = array(
  301. 'label' => 'deploy',
  302. 'hidden' => array( 'model' ),
  303. 'success' => array(
  304. 'page' => 'assets',
  305. 'type' => $path['type'],
  306. ),
  307. 'type' => 'deploy',
  308. );
  309. $headingParameters = array(
  310. 'message' => 'deploying-asset-type',
  311. 'subject' => DataCenterUI::message(
  312. 'type', $path['type']
  313. )
  314. );
  315. } else {
  316. // Gets asset from database
  317. $asset = DataCenterDB::getAsset( $path['type'], $path['id'] );
  318. // Sets 'do' specific parameters
  319. $formParameters = array(
  320. 'label' => 'save',
  321. 'hidden' => array( 'id' ),
  322. 'success' => array(
  323. 'page' => 'assets',
  324. 'type' => $path['type'],
  325. 'action' => 'view',
  326. 'id' => $path['id'],
  327. ),
  328. 'type' => 'manage',
  329. );
  330. $headingParameters = array(
  331. 'message' => 'managing-asset-type',
  332. 'subject' => DataCenterUI::message(
  333. 'type', $path['type']
  334. )
  335. );
  336. }
  337. // Gets model from database
  338. $model = $asset->getModel();
  339. // Gets list of locations
  340. $locations = DataCenterDB::getLocations();
  341. // Completes form parameters
  342. $formParameters = array_merge(
  343. $formParameters,
  344. array(
  345. 'do' => 'save',
  346. 'failure' => $path,
  347. 'action' => array(
  348. 'page' => 'assets',
  349. 'type' => $path['type']
  350. ),
  351. 'row' => $asset,
  352. 'fields' => array(
  353. 'tense' => array(
  354. 'type' => 'tense',
  355. 'disable' => !$path['id'] ? array( 'past' ) : array(),
  356. ),
  357. 'location' => array(
  358. 'type' => 'list',
  359. 'rows' => $locations,
  360. 'labels' => array( 'name', 'region' ),
  361. 'glue' => ' / ',
  362. ),
  363. 'serial' => array( 'type' => 'string' ),
  364. 'asset' => array( 'type' => 'string' ),
  365. )
  366. )
  367. );
  368. // Returns 2 columm layout with a form and a scene
  369. return DataCenterUI::renderLayout(
  370. 'columns',
  371. array(
  372. DataCenterUI::renderLayout(
  373. 'rows',
  374. array(
  375. DataCenterUI::renderWidget(
  376. 'heading', $headingParameters
  377. ),
  378. DataCenterUI::renderWidget( 'form', $formParameters ),
  379. )
  380. ),
  381. DataCenterUI::renderLayout(
  382. 'rows',
  383. array(
  384. DataCenterUI::renderWidget(
  385. 'heading',
  386. array(
  387. 'message' => 'model-type',
  388. 'type' => $path['type'],
  389. )
  390. ),
  391. DataCenterUI::renderWidget(
  392. 'details',
  393. array_merge(
  394. self::$options[$path['type']]['details'],
  395. array( 'row' => $model )
  396. )
  397. )
  398. )
  399. ),
  400. )
  401. );
  402. }
  403. }