api.switchhistory.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. /**
  3. * Renders events which happens with some switch devices
  4. */
  5. class SwitchHistory {
  6. /**
  7. * Contains existing switch ID
  8. *
  9. * @var int
  10. */
  11. protected $switchId = '';
  12. /**
  13. * Weblogs data model placeholder
  14. *
  15. * @var object
  16. */
  17. protected $weblogs = '';
  18. /**
  19. * System message helper object placeholder
  20. *
  21. * @var object
  22. */
  23. protected $messages = '';
  24. /**
  25. * Contains current instance render type
  26. *
  27. * @var bool
  28. */
  29. protected $ajaxFlag = false;
  30. /**
  31. * Contains current instance report scope
  32. *
  33. * @var bool
  34. */
  35. protected $timeMachineFlag = false;
  36. /**
  37. * Switch profile back URL
  38. */
  39. const URL_SWPROFILE = '?module=switches&edit=';
  40. /**
  41. * Default controller module URL
  42. */
  43. const URL_ME = '?module=switchhistory';
  44. /**
  45. * Creates new report instance
  46. *
  47. * @param int $swithId
  48. *
  49. * @return void
  50. */
  51. public function __construct($switchId = '') {
  52. $this->setSwitchId($switchId);
  53. $this->setRenderType();
  54. $this->initMessages();
  55. $this->initWeblogs();
  56. }
  57. /**
  58. * Sets current instance render type
  59. *
  60. * @return void
  61. */
  62. protected function setRenderType() {
  63. if (ubRouting::checkGet('ajax')) {
  64. $this->ajaxFlag = true;
  65. }
  66. if (ubRouting::checkGet('timemachine')) {
  67. $this->timeMachineFlag = true;
  68. }
  69. }
  70. /**
  71. * Inits system messages object instance for further usage
  72. *
  73. * @return void
  74. */
  75. protected function initMessages() {
  76. $this->messages = new UbillingMessageHelper();
  77. }
  78. /**
  79. * Inits weblogs database model
  80. *
  81. * @return void
  82. */
  83. protected function initWeblogs() {
  84. $this->weblogs = new NyanORM('weblogs');
  85. }
  86. /**
  87. * Sets required switchId to protected prop
  88. *
  89. * @param int $switchId
  90. *
  91. * @return void
  92. */
  93. protected function setSwitchId($switchId) {
  94. $switchId = ubRouting::filters($switchId, 'int');
  95. if (!empty($switchId)) {
  96. $this->switchId = $switchId;
  97. }
  98. }
  99. /**
  100. * Renders switch timemachine data
  101. *
  102. * @return string
  103. */
  104. protected function renderTimeMachineData() {
  105. $result = '';
  106. if (!empty($this->switchId)) {
  107. $switchData = zb_SwitchGetData($this->switchId);
  108. if (!empty($switchData)) {
  109. $switchIp = $switchData['ip'];
  110. $tmData = ub_SwitchesTimeMachineGetByIp($switchIp);
  111. if (!empty($tmData)) {
  112. $cells = wf_TableCell(__('Switch dead'));
  113. $cells .= wf_TableCell(__('Location'));
  114. $rows = wf_TableRow($cells, 'row1');
  115. foreach ($tmData as $date => $location) {
  116. $cells = wf_TableCell($date);
  117. $cells .= wf_TableCell($location);
  118. $rows .= wf_TableRow($cells, 'row5');
  119. }
  120. $result .= wf_TableBody($rows, '100%', 0, 'sortable');
  121. } else {
  122. $result .= $this->messages->getStyledMessage(__('Nothing found'), 'success');
  123. }
  124. } else {
  125. $result .= $this->messages->getStyledMessage(__('Something went wrong') . ': EX_NO_SWITCH_DATA', 'error');
  126. }
  127. } else {
  128. $result .= $this->messages->getStyledMessage(__('Something went wrong') . ': EX_NO_SWITCHID', 'error');
  129. }
  130. return($result);
  131. }
  132. /**
  133. * Renders report about all events which happens with some switch
  134. *
  135. * @return string
  136. */
  137. public function renderReport() {
  138. $result = '';
  139. if ($this->ajaxFlag) {
  140. $result .= wf_tag('div', false, '', 'style="height:200px; overflow:auto;"');
  141. $result .= wf_tag('strong') . __('History of switch life') . wf_tag('strong', true) . ' ';
  142. $result .= wf_Link(self::URL_ME . '&switchid=' . $this->switchId, wf_img_sized('skins/log_icon_small.png', __('View full'), 12)) . ' ';
  143. $result .= wf_Link(self::URL_ME . '&timemachine=true&switchid=' . $this->switchId, wf_img_sized('skins/skull.png', __('Time machine'),12));
  144. }
  145. if (!$this->timeMachineFlag) {
  146. if (!empty($this->switchId)) {
  147. $eventMask = '%SWITCH%';
  148. $switchMask = '%[' . $this->switchId . ']%';
  149. $this->weblogs->where('event', 'LIKE', $eventMask);
  150. $this->weblogs->where('event', 'LIKE', $switchMask);
  151. $this->weblogs->orderBy('id', 'desc');
  152. $allEvents = $this->weblogs->getAll();
  153. if (!empty($allEvents)) {
  154. $cells = wf_TableCell(__('ID'));
  155. $cells .= wf_TableCell(__('Date'));
  156. $cells .= wf_TableCell(__('Admin'));
  157. $cells .= wf_TableCell(__('IP'));
  158. $cells .= wf_TableCell(__('Event'));
  159. $rows = wf_TableRow($cells, 'row1');
  160. foreach ($allEvents as $io => $each) {
  161. $cells = wf_TableCell($each['id']);
  162. $cells .= wf_TableCell($each['date']);
  163. $cells .= wf_TableCell($each['admin']);
  164. $cells .= wf_TableCell($each['ip']);
  165. $cells .= wf_TableCell($each['event']);
  166. $rows .= wf_TableRow($cells, 'row5');
  167. }
  168. $result .= wf_TableBody($rows, '100%', 0, 'sortable');
  169. } else {
  170. $result .= $this->messages->getStyledMessage(__('Nothing to show'), 'warning');
  171. }
  172. } else {
  173. $result .= $this->messages->getStyledMessage(__('Something went wrong') . ': EX_NO_SWITCHID', 'error');
  174. }
  175. } else {
  176. $result .= $this->renderTimeMachineData();
  177. }
  178. //no additional formatting required
  179. if ($this->ajaxFlag) {
  180. $result .= wf_tag('div', true);
  181. die($result);
  182. }
  183. $result .= wf_delimiter();
  184. $result .= wf_BackLink(self::URL_SWPROFILE . $this->switchId) . ' ';
  185. if (!$this->timeMachineFlag) {
  186. $timeMachineUrl = self::URL_ME . '&timemachine=true&switchid=' . $this->switchId;
  187. $result .= wf_Link($timeMachineUrl, wf_img('skins/skull.png') . ' ' . __('Time machine'), false, 'ubButton') . '';
  188. } else {
  189. $switchHistUrl = self::URL_ME . '&switchid=' . $this->switchId;
  190. $result .= wf_Link($switchHistUrl, wf_img('skins/log_icon_small.png') . ' ' . __('History'), false, 'ubButton') . '';
  191. }
  192. return($result);
  193. }
  194. }