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

/libraries/ManiaLivePlugins/MLEPP/Admin/Gui/Windows/AdminWindow.php

http://mlepp.googlecode.com/
PHP | 259 lines | 210 code | 38 blank | 11 comment | 20 complexity | 16896622d2c4e83cf3b9d9fa4e4eb980 MD5 | raw file
  1. <?php
  2. namespace ManiaLivePlugins\MLEPP\Admin\Gui\Windows;
  3. use ManiaLivePlugins\MLEPP\Admin\Gui\Controls\Header;
  4. use ManiaLivePlugins\MLEPP\Admin\Gui\Controls\Normal;
  5. use ManiaLivePlugins\MLEPP\Admin\Gui\Controls\Action;
  6. use ManiaLivePlugins\MLEPP\Admin\Gui\Controls\SpecIcon;
  7. use ManiaLive\Gui\Windowing\WindowHandler;
  8. use ManiaLive\PluginHandler\PluginHandler;
  9. use ManiaLib\Gui\Elements\Bgs1InRace;
  10. use ManiaLib\Gui\Tools;
  11. use ManiaLib\Gui\Elements\BgsPlayerCard;
  12. use ManiaLib\Gui\Elements\Label;
  13. use ManiaLib\Gui\Elements\Entry;
  14. use ManiaLib\Gui\Elements\Quad;
  15. use ManiaLib\Gui\Elements\Button;
  16. use ManiaLib\Gui\Layouts\Flow;
  17. use ManiaLive\Data\Storage;
  18. use ManiaLive\Gui\Windowing\Controls\ButtonResizeable;
  19. use ManiaLive\Gui\Windowing\Windows\Info;
  20. use ManiaLive\Gui\Windowing\Controls\PageNavigator;
  21. use ManiaLive\Gui\Windowing\Controls\Panel;
  22. use ManiaLive\Gui\Windowing\Controls\Frame;
  23. use ManiaLive\Utilities\Time;
  24. use ManiaLive\Utilities\Console;
  25. class AdminWindow extends \ManiaLive\Gui\Windowing\ManagedWindow
  26. {
  27. //components ...
  28. private $navigator;
  29. private $table;
  30. private $btn_player;
  31. private $btn_website;
  32. private $navigator_back;
  33. private $page;
  34. private $records;
  35. private $page_last;
  36. private $page_items;
  37. private $item_height;
  38. private $table_height;
  39. private $columns;
  40. //private $info;
  41. private $highlight;
  42. private $callback;
  43. function initializeComponents()
  44. {
  45. $this->page = 1;
  46. $this->page_last = 1;
  47. $this->item_height = 6;
  48. $this->table_height = 0;
  49. $this->records = array();
  50. $this->columns = array();
  51. $this->highlight = false;
  52. $this->panel = new Panel();
  53. $this->setTitle('Administer players');
  54. $this->setMaximizable();
  55. // add background for navigation elements ...
  56. $this->navigator_back = new BgsPlayerCard();
  57. $this->navigator_back->setSubStyle(BgsPlayerCard::BgCardSystem);
  58. $this->addComponent($this->navigator_back);
  59. // create records-table ...
  60. $this->table = new Frame($this->getSizeX() - 4, $this->getSizeY() - 16);
  61. $this->table->applyLayout(new Flow());
  62. $this->table->setPosition(2, 16);
  63. $this->addComponent($this->table);
  64. // create page navigator ...
  65. $this->navigator = new PageNavigator();
  66. $this->addComponent($this->navigator);
  67. }
  68. function onResize()
  69. {
  70. $this->table->setSize($this->getSizeX() - 4, $this->getSizeY() - 16);
  71. $this->calculatePages();
  72. }
  73. function onDraw()
  74. {
  75. // refresh table ...
  76. $this->table->clearComponents();
  77. // create table header ...
  78. foreach ($this->columns as $name => $percent)
  79. {
  80. $cell = new Header($percent * $this->table->getSizeX(), $this->item_height + 1);
  81. $cell->setText($name);
  82. $this->table->addComponent($cell);
  83. }
  84. // create table body ...
  85. $count = count($this->records);
  86. $max = $this->page_items * $this->page;
  87. for ($i = $this->page_items * ($this->page - 1); $i < $count && $i < $max; $i++)
  88. {
  89. $record = $this->records[$i];
  90. foreach ($this->columns as $name => $percent)
  91. {
  92. if ($name == "isSpec" ) {
  93. if ($record[$name][0] == "isSpec") $icon = "Spec";
  94. if ($record[$name][0] == "isRace") $icon = "Race";
  95. $cell = new SpecIcon($percent * $this->table->getSizeX(), $this->item_height, $icon);
  96. }
  97. else {
  98. if ($record[$name][2]) {
  99. $cell = new Action($percent * $this->table->getSizeX(), $this->item_height);
  100. }
  101. else {
  102. $cell = new Normal($percent * $this->table->getSizeX(), $this->item_height);
  103. }
  104. $cell->callback = $this->callback;
  105. $cell->action = $record[$name][0];
  106. $cell->target = $record[$name][1];
  107. if (isset($record[$name]))
  108. $cell->setText($record[$name][0]);
  109. else
  110. $cell->setText(' ');
  111. }
  112. $this->table->addComponent($cell);
  113. }
  114. }
  115. // add page navigator to the bottom ...
  116. $this->navigator->setPositionX($this->getSizeX() / 2);
  117. $this->navigator->setPositionY($this->getSizeY() - 4);
  118. // place navigation background ...
  119. $this->navigator_back->setValign('bottom');
  120. $this->navigator_back->setSize($this->getSizeX() - 0.6, 8);
  121. $this->navigator_back->setPosition(0.3, $this->getSizeY() - 0.3);
  122. // configure ...
  123. $this->navigator->setCurrentPage($this->page);
  124. $this->navigator->setPageNumber($this->page_last);
  125. $this->navigator->showText(true);
  126. $this->navigator->showLast(true);
  127. if ($this->page < $this->page_last)
  128. {
  129. $this->navigator->arrowNext->setAction($this->callback('showNextPage'));
  130. $this->navigator->arrowLast->setAction($this->callback('showLastPage'));
  131. }
  132. else
  133. {
  134. $this->navigator->arrowNext->setAction(null);
  135. $this->navigator->arrowLast->setAction(null);
  136. }
  137. if ($this->page > 1)
  138. {
  139. $this->navigator->arrowPrev->setAction($this->callback('showPrevPage'));
  140. $this->navigator->arrowFirst->setAction($this->callback('showFirstPage'));
  141. }
  142. else
  143. {
  144. $this->navigator->arrowPrev->setAction(null);
  145. $this->navigator->arrowFirst->setAction(null);
  146. }
  147. }
  148. function onHide()
  149. {
  150. $this->showFirstPage();
  151. $this->highlight = false;
  152. }
  153. function calculatePages()
  154. {
  155. $this->page_items = floor( ($this->table->getSizeY()-12) / $this->item_height);
  156. $this->page_last = ceil(count($this->records) * $this->item_height / max(1, $this->table->getSizeY()-12));
  157. }
  158. function addColumn($name, $percent)
  159. {
  160. $this->columns[$name] = $percent;
  161. }
  162. function clearAll()
  163. {
  164. $this->columns = array();
  165. $this->records = array();
  166. }
  167. function clearItems()
  168. {
  169. $this->records = array();
  170. }
  171. function addItem($record)
  172. {
  173. if (is_array($record))
  174. {
  175. $this->records[] = $record;
  176. $this->calculatePages();
  177. }
  178. }
  179. function addAdminItem($record,$callback)
  180. {
  181. $this->callback = $callback;
  182. if (is_array($record))
  183. {
  184. $this->records[] = $record;
  185. $this->calculatePages();
  186. }
  187. }
  188. function showPrevPage($login = null)
  189. {
  190. $this->page--;
  191. if ($login) $this->show();
  192. }
  193. function showNextPage($login = null)
  194. {
  195. $this->page++;
  196. if ($login) $this->show();
  197. }
  198. function showLastPage($login = null)
  199. {
  200. $this->page = $this->page_last;
  201. if ($login) $this->show();
  202. }
  203. function showFirstPage($login = null)
  204. {
  205. $this->page = 1;
  206. if ($login) $this->show();
  207. }
  208. function destroy()
  209. {
  210. unset($this->callback);
  211. parent::destroy();
  212. }
  213. function showInfo($login,$title,$text) {
  214. $infowindow = Info::Create($login, false);
  215. $infowindow->setSize(60, 40);
  216. $infowindow->setTitle($title);
  217. $infowindow->setText($text);
  218. $infowindow->centerOn($this);
  219. WindowHandler::showDialog($infowindow);
  220. }
  221. }