api.ponifdesc.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. * Allows to attach some description on PON OLT interfaces
  4. */
  5. class PONIfDesc {
  6. /**
  7. * Database mapping abstraction layer
  8. *
  9. * @var object
  10. */
  11. protected $dataSource = '';
  12. /**
  13. * Contains available descriptions for PON interfaces
  14. *
  15. * @var array
  16. */
  17. protected $allDescriptions = array();
  18. /**
  19. * Default datsource table name
  20. */
  21. const TABLE_IFDESC = 'ponifdesc';
  22. /**
  23. * Creates new descriptor instance
  24. */
  25. public function __construct() {
  26. $this->initDataSource();
  27. $this->loadDescriptions();
  28. }
  29. /**
  30. * Inits database abstraction layer
  31. *
  32. * @return void
  33. */
  34. protected function initDataSource() {
  35. $this->dataSource = new NyanORM(self::TABLE_IFDESC);
  36. }
  37. /**
  38. * Loads available descriptions from database
  39. *
  40. * @return void
  41. */
  42. protected function loadDescriptions() {
  43. $this->allDescriptions = $this->dataSource->getAll();
  44. }
  45. /**
  46. * Returns description of PON interface if it exists
  47. *
  48. * @param int $oltId
  49. * @param string $interface
  50. *
  51. * @return string
  52. */
  53. public function getDescription($oltId, $interface) {
  54. $result = '';
  55. if (!empty($this->allDescriptions)) {
  56. foreach ($this->allDescriptions as $io => $each) {
  57. if ($each['oltid'] == $oltId) {
  58. if ($each['iface'] == $interface) {
  59. $result = $each['desc'];
  60. }
  61. }
  62. }
  63. }
  64. return($result);
  65. }
  66. /**
  67. * Renders interface description form
  68. *
  69. * @param int $oltId
  70. * @param string $interface
  71. *
  72. * @return string
  73. */
  74. public function renderIfForm($oltId, $interface) {
  75. $oltId = ubRouting::filters($oltId, 'int');
  76. $result = '';
  77. $result .= wf_BackLink(PONizer::URL_ME . '&oltstats=true');
  78. $result .= wf_CleanDiv() . wf_delimiter(0);
  79. if (!empty($oltId)) {
  80. $currentDesc = $this->getDescription($oltId, $interface);
  81. $inputs = wf_HiddenInput('newoltiddesc', $oltId);
  82. $interface .= wf_HiddenInput('newoltif', $interface);
  83. $inputs .= wf_TextInput('newoltifdesc', __('Description') . ' ' . $interface, $currentDesc, false, 20) . ' ';
  84. $inputs .= wf_Submit(__('Save'));
  85. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  86. }
  87. return($result);
  88. }
  89. /**
  90. * Saves changed interface description in database
  91. *
  92. * @return void
  93. */
  94. public function save() {
  95. if (ubRouting::checkPost(array('newoltiddesc', 'newoltif'))) {
  96. $oltId = ubRouting::post('newoltiddesc', 'int');
  97. $interface = ubRouting::post('newoltif');
  98. $interfaceF = ubRouting::post('newoltif', 'mres');
  99. $newDescF = ubRouting::post('newoltifdesc', 'mres');
  100. $newDesc = ubRouting::post('newoltifdesc');
  101. $currentDesc = $this->getDescription($oltId, $interface);
  102. //something changed
  103. if ($currentDesc != $newDescF) {
  104. //clean old description
  105. $this->dataSource->where('oltid', '=', $oltId);
  106. $this->dataSource->where('iface', '=', $interfaceF);
  107. $this->dataSource->delete();
  108. //create new
  109. $this->dataSource->data('oltid', $oltId);
  110. $this->dataSource->data('iface', $interfaceF);
  111. $this->dataSource->data('desc', $newDescF);
  112. $this->dataSource->create();
  113. log_register('PON OLT [' . $oltId . '] IFACE `' . $interface . '` DESC CHANGE ON `' . $newDesc . '`');
  114. }
  115. }
  116. }
  117. }