api.switchuplinks.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. <?php
  2. /**
  3. * Siwtches uplink parameters management class
  4. */
  5. class SwitchUplinks {
  6. /**
  7. * Current instance switch ID
  8. *
  9. * @var int
  10. */
  11. protected $switchId = 0;
  12. /**
  13. * Contains current switch uplink data
  14. *
  15. * @var array
  16. */
  17. protected $uplinkData = array();
  18. /**
  19. * Contains available media types markers and their names
  20. *
  21. * @var array
  22. */
  23. protected $mediaTypes = array();
  24. /**
  25. * Contains available media types icons
  26. *
  27. * @var array
  28. */
  29. protected $mediaIcons = array();
  30. /**
  31. * Contains typical uplink speed rates
  32. *
  33. * @var array
  34. */
  35. protected $speedRates = array();
  36. /**
  37. * System message helper placeholder
  38. *
  39. * @var object
  40. */
  41. protected $messages = '';
  42. /**
  43. * Switches uplink paramereds DB abstraction placeholder
  44. *
  45. * @var object
  46. */
  47. protected $switchUplinks = '';
  48. /**
  49. * Contains all switches uplinks detailed data as switch=>updata
  50. *
  51. * @var array
  52. */
  53. protected $allUplinksData = array();
  54. /**
  55. * Static routes, etc
  56. */
  57. const TABLE_UPLINKS = 'switchuplinks';
  58. const URL_SWPROFILE = '?module=switches&edit=';
  59. const ROUTE_SWID = 'swuplinkswitchid';
  60. const ROUTE_MEDIA = 'swuplinksmedia';
  61. const ROUTE_SPEED = 'swuplinksspeed';
  62. const ROUTE_PORT = 'swuplinksport';
  63. const ROUTE_EDITINTERFACE = 'editswuplinkparameters';
  64. const PATH_ICONS = 'skins/';
  65. /**
  66. * Creates new switch uplinks object instance
  67. *
  68. * @param int/void $switchId
  69. */
  70. public function __construct($switchId = '') {
  71. $this->initMessages();
  72. $this->initDatabase();
  73. $this->setMediaTypes();
  74. $this->setSpeedRates();
  75. if (!empty($switchId)) {
  76. $this->setSwitchId($switchId);
  77. $this->loadUplinkData();
  78. }
  79. }
  80. /**
  81. * Sets available uplink media types
  82. *
  83. * @return void
  84. */
  85. protected function setMediaTypes() {
  86. //may be configurable in future.. or not..
  87. $this->mediaTypes = array(
  88. 'F' => __('Fiber optics'),
  89. 'C' => __('Copper'),
  90. 'W' => __('Wireless'),
  91. );
  92. $this->mediaIcons = array(
  93. 'F' => 'linkfiber.png',
  94. 'C' => 'linkcopper.png',
  95. 'W' => 'linkwireless.png',
  96. );
  97. }
  98. /**
  99. * Sets typical speed rates for uplink ports
  100. *
  101. * @return void
  102. */
  103. protected function setSpeedRates() {
  104. $this->speedRates = array(
  105. '1G' => '1 ' . __('Gbit/s'),
  106. '10G' => '10 ' . __('Gbit/s'),
  107. '40G' => '40 ' . __('Gbit/s'),
  108. '100M' => '100 ' . __('Mbit/s'),
  109. '10M' => '10 ' . __('Mbit/s'),
  110. );
  111. }
  112. /**
  113. * Inits system message helper instance for further usage
  114. *
  115. * @return void
  116. */
  117. protected function initMessages() {
  118. $this->messages = new UbillingMessageHelper();
  119. }
  120. /**
  121. * Inits dabase abstraction
  122. *
  123. * @return void
  124. */
  125. protected function initDatabase() {
  126. $this->switchUplinks = new NyanORM(self::TABLE_UPLINKS);
  127. }
  128. /**
  129. * Current instance switchId setter
  130. *
  131. * @param int/void $switchId
  132. *
  133. * @return void
  134. */
  135. protected function setSwitchId($switchId = '') {
  136. $switchId = ubRouting::filters($switchId, 'int');
  137. if (!empty($switchId)) {
  138. $this->switchId = $switchId;
  139. }
  140. }
  141. /**
  142. * Loads current switch uplink data
  143. *
  144. * @return void
  145. */
  146. protected function loadUplinkData() {
  147. if (!empty($this->switchId)) {
  148. $this->switchUplinks->where('switchid', '=', $this->switchId);
  149. $tmpData = $this->switchUplinks->getAll();
  150. if (!empty($tmpData)) {
  151. if (isset($tmpData[0])) {
  152. $this->uplinkData = $tmpData[0];
  153. }
  154. }
  155. }
  156. }
  157. /**
  158. * Renders uplink parameters editing inputs
  159. *
  160. * @return string
  161. */
  162. public function renderEditForm() {
  163. $result = '';
  164. if (!empty($this->switchId)) {
  165. $mediaTmp = array('' => '-');
  166. $mediaTmp += $this->mediaTypes;
  167. $speedTmp = array('' => '-');
  168. $speedTmp += $this->speedRates;
  169. $inputs = wf_HiddenInput(self::ROUTE_SWID, $this->switchId);
  170. $inputs .= wf_Selector(self::ROUTE_MEDIA, $mediaTmp, __('Type'), @$this->uplinkData['media'], false) . ' ';
  171. $inputs .= wf_Selector(self::ROUTE_SPEED, $speedTmp, __('Speed'), @$this->uplinkData['speed'], false) . ' ';
  172. $inputs .= wf_TextInput(self::ROUTE_PORT, __('Port'), @$this->uplinkData['port'], false, 2, 'digits') . ' ';
  173. $result .= $inputs; //we need it for main edit form integration
  174. } else {
  175. $result .= $this->messages->getStyledMessage(__('Something went wrong') . ': ' . __('Switch') . ' ID ' . __('is empty'), 'error');
  176. }
  177. return($result);
  178. }
  179. /**
  180. * Saves switch uplink data into database
  181. *
  182. * @return void
  183. */
  184. public function save() {
  185. if (ubRouting::checkPost(array(self::ROUTE_SWID))) {
  186. $switchId = ubRouting::post(self::ROUTE_SWID, 'int');
  187. $newMedia = ubRouting::post(self::ROUTE_MEDIA, 'mres');
  188. $newSpeed = ubRouting::post(self::ROUTE_SPEED, 'mres');
  189. $newPort = ubRouting::post(self::ROUTE_PORT, 'int');
  190. $this->switchUplinks->data('media', $newMedia);
  191. $this->switchUplinks->data('speed', $newSpeed);
  192. $this->switchUplinks->data('port', $newPort);
  193. //updating existing record
  194. if (!empty($this->uplinkData)) {
  195. $this->switchUplinks->where('switchid', '=', $switchId);
  196. $this->switchUplinks->save();
  197. } else {
  198. //creating new record
  199. $this->switchUplinks->data('switchid', $switchId);
  200. $this->switchUplinks->create();
  201. }
  202. log_register('SWITCHUPLINK CHANGE [' . $switchId . '] MEDIA `' . $newMedia . '` SPEED `' . $newSpeed . '` PORT `' . $newPort . '`');
  203. }
  204. }
  205. /**
  206. * Delete switch uplink data record from database
  207. *
  208. * @param int $switchId
  209. *
  210. * @return void
  211. */
  212. public function delete($switchId) {
  213. $switchId = ubRouting::filters($switchId, 'int');
  214. if ($switchId) {
  215. $this->switchUplinks->where('switchid', '=', $switchId);
  216. $this->switchUplinks->delete();
  217. log_register('SWITCHUPLINK DELETE [' . $switchId . ']');
  218. }
  219. }
  220. /**
  221. * Delete switch uplink data record from database on switch deletion
  222. *
  223. * @param int $switchId
  224. *
  225. * @return void
  226. */
  227. public function flush($switchId) {
  228. $switchId = ubRouting::filters($switchId, 'int');
  229. if ($switchId) {
  230. $this->switchUplinks->where('switchid', '=', $switchId);
  231. $this->switchUplinks->delete();
  232. log_register('SWITCHUPLINK FLUSH [' . $switchId . ']');
  233. }
  234. }
  235. /**
  236. * Renders current instance uplink data in compact format
  237. *
  238. * @return string
  239. */
  240. public function renderSwitchUplinkData() {
  241. $result = '';
  242. if (!empty($this->uplinkData)) {
  243. if (!empty($this->uplinkData['media'])) {
  244. if (isset($this->mediaIcons[$this->uplinkData['media']])) {
  245. $mediaIcon = wf_img_sized(self::PATH_ICONS . $this->mediaIcons[$this->uplinkData['media']], '', '10') . ' ';
  246. } else {
  247. $mediaIcon = '';
  248. }
  249. $result .= $mediaIcon . $this->mediaTypes[$this->uplinkData['media']] . ' ';
  250. }
  251. if (!empty($this->uplinkData['speed'])) {
  252. $result .= $this->speedRates[$this->uplinkData['speed']] . ' ';
  253. }
  254. if (!empty($this->uplinkData['port'])) {
  255. $result .= $this->uplinkData['port'] . ' ' . __('Port');
  256. }
  257. //empty existing record
  258. if (!$this->uplinkData['media'] AND !$this->uplinkData['speed'] AND !$this->uplinkData['port']) {
  259. $result .= __('Uplink parameters is not set');
  260. }
  261. } else {
  262. $result .= __('Uplink parameters is not set');
  263. }
  264. return($result);
  265. }
  266. /**
  267. * Loads all switches uplinks data
  268. *
  269. * @return void
  270. */
  271. public function loadAllUplinksData() {
  272. $this->allUplinksData = $this->switchUplinks->getAll('switchid');
  273. }
  274. /**
  275. * Returns count of available uplinks data records
  276. *
  277. * @return int
  278. */
  279. public function getAllUplinksCount() {
  280. return(sizeof($this->allUplinksData));
  281. }
  282. /**
  283. * Returns short uplink parameters text description
  284. *
  285. * @param int $swithchId
  286. * @param bool $includePort
  287. *
  288. * @return string
  289. */
  290. public function getUplinkTinyDesc($swithchId, $includePort = false) {
  291. $result = '';
  292. if (isset($this->allUplinksData[$swithchId])) {
  293. $media = $this->allUplinksData[$swithchId]['media'];
  294. $speed = $this->allUplinksData[$swithchId]['speed'];
  295. $icon = (isset($this->mediaIcons[$media])) ? wf_img(self::PATH_ICONS . $this->mediaIcons[$media], $this->mediaTypes[$media]) : '';
  296. $result .= $icon . $media . $speed;
  297. //optional append port
  298. if ($includePort) {
  299. $port = $this->allUplinksData[$swithchId]['port'];
  300. if (!empty($port)) {
  301. $result .= 'p' . $port;
  302. }
  303. }
  304. }
  305. return($result);
  306. }
  307. /**
  308. * Returns short uplink port or void if not set
  309. *
  310. * @param int $swithchId
  311. *
  312. * @return int/void
  313. */
  314. public function getUplinkPort($swithchId) {
  315. $result = '';
  316. if (isset($this->allUplinksData[$swithchId])) {
  317. $port = $this->allUplinksData[$swithchId]['port'];
  318. if (!empty($port)) {
  319. $result .= $port;
  320. }
  321. }
  322. return($result);
  323. }
  324. }