api.dshaper.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. /**
  3. * User dynamic shaper with speeds self-changing depends on time of day
  4. */
  5. class DynamicShaper {
  6. /**
  7. * Contains all tariffs names array
  8. *
  9. * @var array
  10. */
  11. protected $allTariffs = array();
  12. /**
  13. * Contains all tariffs speeds as tariffname=>speeddown/speedup
  14. *
  15. * @var array
  16. */
  17. protected $allSpeeds = array();
  18. /**
  19. * Contains prepeared data for tariff speed selector
  20. *
  21. * @var array
  22. */
  23. protected $selectorParams = array();
  24. public function __construct() {
  25. $this->loadTariffs();
  26. $this->loadSpeeds();
  27. $this->preprocessTariffs();
  28. }
  29. /**
  30. * Loads existing tariffs from database
  31. *
  32. * @return void
  33. */
  34. protected function loadTariffs() {
  35. $raw = zb_TariffsGetAll();
  36. if (!empty($raw)) {
  37. foreach ($raw as $io => $each) {
  38. $this->allTariffs[$each['name']] = $each['name'];
  39. }
  40. }
  41. }
  42. /**
  43. * Loads available tariff speeds from database
  44. *
  45. * @return void
  46. */
  47. protected function loadSpeeds() {
  48. $this->allSpeeds = zb_TariffGetAllSpeeds();
  49. }
  50. /**
  51. * Preprocess tariffs for selector boxes
  52. *
  53. * @return void
  54. */
  55. protected function preprocessTariffs() {
  56. if (!empty($this->allTariffs)) {
  57. foreach ($this->allTariffs as $io => $eachTariff) {
  58. $this->selectorParams[$eachTariff] = $eachTariff . $this->getSpeeds($eachTariff);
  59. }
  60. }
  61. }
  62. /**
  63. * Returns current tariff natural speeds
  64. *
  65. * @param string $tariff
  66. * @return string
  67. */
  68. protected function getSpeeds($tariff) {
  69. $result = '';
  70. if (isset($this->allSpeeds[$tariff])) {
  71. $result = ' (' . $this->allSpeeds[$tariff]['speeddown'] . '/' . $this->allSpeeds[$tariff]['speedup'] . ')';
  72. }
  73. return ($result);
  74. }
  75. /**
  76. * Returns available time rules grid
  77. *
  78. * @return string
  79. */
  80. public function renderList() {
  81. $messages = new UbillingMessageHelper();
  82. $allTariffs = zb_TariffGetPricesAll();
  83. $query = "SELECT * from `dshape_time` ORDER BY `id` ASC";
  84. $allrules = simple_queryall($query);
  85. $cells = wf_TableCell(__('ID'));
  86. $cells.= wf_TableCell(__('Tariff'));
  87. $cells.= wf_TableCell(__('Time from'));
  88. $cells.= wf_TableCell(__('Time to'));
  89. $cells.= wf_TableCell(__('Speed'));
  90. $cells.= wf_TableCell(__('Actions'));
  91. $rows = wf_TableRow($cells, 'row1');
  92. if (!empty($allrules)) {
  93. foreach ($allrules as $io => $eachrule) {
  94. $rowClass = (isset($allTariffs[$eachrule['tariff']])) ? 'row3' : 'sigdeleteduser';
  95. $tariffControl = (cfr('TARIFFSPEED')) ? wf_Link('?module=tariffspeeds&tariff=' . $eachrule['tariff'], $eachrule['tariff'] . ' ', false) : $eachrule['tariff'];
  96. $cells = wf_TableCell($eachrule['id']);
  97. $cells.= wf_TableCell($tariffControl);
  98. $cells.= wf_TableCell($eachrule['threshold1']);
  99. $cells.= wf_TableCell($eachrule['threshold2']);
  100. $cells.= wf_TableCell($eachrule['speed']);
  101. $actions = wf_JSAlert('?module=dshaper&delete=' . $eachrule['id'], web_delete_icon(), $messages->getDeleteAlert());
  102. $actions.= wf_JSAlert('?module=dshaper&edit=' . $eachrule['id'], web_edit_icon(), $messages->getEditAlert());
  103. $cells.= wf_TableCell($actions);
  104. $rows.= wf_TableRow($cells, $rowClass);
  105. }
  106. }
  107. $result = wf_TableBody($rows, '100%', '0', 'sortable');
  108. return ($result);
  109. }
  110. /**
  111. * Deletes existing time rule from database
  112. *
  113. * @param int $ruleid
  114. */
  115. public function delete($ruleid) {
  116. $ruleid = vf($ruleid, 3);
  117. $query = "DELETE from `dshape_time` where `id`='" . $ruleid . "'";
  118. nr_query($query);
  119. log_register("DSHAPE DELETE [" . $ruleid . ']');
  120. }
  121. /**
  122. * Deletes shaper rules from database by tariff name
  123. *
  124. * @param string $tariff
  125. */
  126. public function flushTariff($tariff) {
  127. $tariff = mysql_real_escape_string($tariff);
  128. $query = "DELETE from `dshape_time` WHERE `tariff`='" . $tariff . "';";
  129. nr_query($query);
  130. log_register("DSHAPE FLUSH TARIFF `" . $tariff . '`');
  131. }
  132. /**
  133. * Returns time rule adding form
  134. *
  135. * @return string
  136. */
  137. public function renderAddForm() {
  138. $sup = wf_tag('sup') . '*' . wf_tag('sup', true);
  139. $inputs = wf_Selector('newdshapetariff', $this->selectorParams, __('Tariff'), '', true);
  140. $inputs.= wf_TimePickerPresetSeconds('newthreshold1', '', __('Time from') . $sup . ' ', true);
  141. $inputs.= wf_TimePickerPresetSeconds('newthreshold2', '', __('Time to') . $sup . ' ', true);
  142. $inputs.= wf_TextInput('newspeed', __('Speed') . $sup, '', true, 8);
  143. $inputs.= wf_Submit(__('Create'));
  144. $result = wf_Form('', 'POST', $inputs, 'glamour');
  145. return ($result);
  146. }
  147. /**
  148. * Returns time rule editing form
  149. *
  150. * @param int $timeruleid existing time rule database ID
  151. * @return string
  152. */
  153. public function renderEditForm($timeruleid) {
  154. $timeruleid = vf($timeruleid, 3);
  155. $query = "SELECT * from `dshape_time` WHERE `id`='" . $timeruleid . "'";
  156. $timerule_data = simple_query($query);
  157. $sup = wf_tag('sup') . '*' . wf_tag('sup', true);
  158. $inputs = wf_tag('select', false, '', 'DISABLED');
  159. $inputs.= wf_tag('option') . $timerule_data['tariff'] . $this->getSpeeds($timerule_data['tariff']) . wf_tag('option', true);
  160. $inputs.= wf_tag('select', true);
  161. $inputs.= wf_tag('br');
  162. $inputs.= wf_HiddenInput('editdshapetariff', $timerule_data['tariff']);
  163. $inputs.= wf_TimePickerPresetSeconds('editthreshold1', $timerule_data['threshold1'], __('Time from') . $sup, true);
  164. $inputs.= wf_TimePickerPresetSeconds('editthreshold2', $timerule_data['threshold2'], __('Time to') . $sup, true);
  165. $inputs.= wf_TextInput('editspeed', __('Speed') . $sup, $timerule_data['speed'], true, 8);
  166. $inputs.= wf_Submit(__('Save'));
  167. $form = wf_Form('', 'POST', $inputs, 'glamour');
  168. $form.= wf_CleanDiv();
  169. $form.=wf_tag('br');
  170. $form.= wf_BackLink('?module=dshaper');
  171. return ($form);
  172. }
  173. /**
  174. * Creates new time rule in database
  175. *
  176. * @param string $tariff existing tariff name
  177. * @param string $threshold1 event start time
  178. * @param string $threshold2 event stop time
  179. * @param integer $speed
  180. */
  181. public function create($tariff, $threshold1, $threshold2, $speed) {
  182. $tariff = mysql_real_escape_string($tariff);
  183. $threshold1 = mysql_real_escape_string($threshold1);
  184. $threshold2 = mysql_real_escape_string($threshold2);
  185. $speed = vf($speed);
  186. $query = "INSERT INTO `dshape_time` (`id` , `tariff` , `threshold1` , `threshold2` , `speed` ) "
  187. . "VALUES (NULL , '" . $tariff . "', '" . $threshold1 . "', '" . $threshold2 . "', '" . $speed . "');";
  188. nr_query($query);
  189. log_register("DSHAPE ADD `" . $tariff . '`');
  190. }
  191. /**
  192. * Edits existing timerule in database
  193. *
  194. * @param type $timeruleid
  195. * @param type $threshold1 event start time
  196. * @param type $threshold2 event stop time
  197. * @param type $speed
  198. */
  199. public function edit($timeruleid, $threshold1, $threshold2, $speed) {
  200. $timeruleid = vf($timeruleid);
  201. $threshold1 = mysql_real_escape_string($threshold1);
  202. $threshold2 = mysql_real_escape_string($threshold2);
  203. $speed = vf($speed);
  204. $query = "UPDATE `dshape_time` SET
  205. `threshold1` = '" . $threshold1 . "',
  206. `threshold2` = '" . $threshold2 . "',
  207. `speed` = '" . $speed . "' WHERE `id` ='" . $timeruleid . "' LIMIT 1;
  208. ";
  209. nr_query($query);
  210. log_register("DSHAPE CHANGE [" . $timeruleid . '] ON `' . $speed . '`');
  211. }
  212. }
  213. ?>