Source of file api.switchuplinks.php
Size: 10,276 Bytes - Last Modified: 2024-05-04T16:22:09+03:00
/tmp/current_snapshot/api/libs/api.switchuplinks.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358 | <?php /** * Siwtches uplink parameters management class */ class SwitchUplinks { /** * Current instance switch ID * * @var int */ protected $switchId = 0; /** * Contains current switch uplink data * * @var array */ protected $uplinkData = array(); /** * Contains available media types markers and their names * * @var array */ protected $mediaTypes = array(); /** * Contains available media types icons * * @var array */ protected $mediaIcons = array(); /** * Contains typical uplink speed rates * * @var array */ protected $speedRates = array(); /** * System message helper placeholder * * @var object */ protected $messages = ''; /** * Switches uplink paramereds DB abstraction placeholder * * @var object */ protected $switchUplinks = ''; /** * Contains all switches uplinks detailed data as switch=>updata * * @var array */ protected $allUplinksData = array(); /** * Static routes, etc */ const TABLE_UPLINKS = 'switchuplinks'; const URL_SWPROFILE = '?module=switches&edit='; const ROUTE_SWID = 'swuplinkswitchid'; const ROUTE_MEDIA = 'swuplinksmedia'; const ROUTE_SPEED = 'swuplinksspeed'; const ROUTE_PORT = 'swuplinksport'; const ROUTE_EDITINTERFACE = 'editswuplinkparameters'; const PATH_ICONS = 'skins/'; /** * Creates new switch uplinks object instance * * @param int/void $switchId */ public function __construct($switchId = '') { $this->initMessages(); $this->initDatabase(); $this->setMediaTypes(); $this->setSpeedRates(); if (!empty($switchId)) { $this->setSwitchId($switchId); $this->loadUplinkData(); } } /** * Sets available uplink media types * * @return void */ protected function setMediaTypes() { //may be configurable in future.. or not.. $this->mediaTypes = array( 'F' => __('Fiber optics'), 'C' => __('Copper'), 'W' => __('Wireless'), ); $this->mediaIcons = array( 'F' => 'linkfiber.png', 'C' => 'linkcopper.png', 'W' => 'linkwireless.png', ); } /** * Sets typical speed rates for uplink ports * * @return void */ protected function setSpeedRates() { $this->speedRates = array( '1G' => '1 ' . __('Gbit/s'), '10G' => '10 ' . __('Gbit/s'), '40G' => '40 ' . __('Gbit/s'), '100M' => '100 ' . __('Mbit/s'), '10M' => '10 ' . __('Mbit/s'), ); } /** * Inits system message helper instance for further usage * * @return void */ protected function initMessages() { $this->messages = new UbillingMessageHelper(); } /** * Inits dabase abstraction * * @return void */ protected function initDatabase() { $this->switchUplinks = new NyanORM(self::TABLE_UPLINKS); } /** * Current instance switchId setter * * @param int/void $switchId * * @return void */ protected function setSwitchId($switchId = '') { $switchId = ubRouting::filters($switchId, 'int'); if (!empty($switchId)) { $this->switchId = $switchId; } } /** * Loads current switch uplink data * * @return void */ protected function loadUplinkData() { if (!empty($this->switchId)) { $this->switchUplinks->where('switchid', '=', $this->switchId); $tmpData = $this->switchUplinks->getAll(); if (!empty($tmpData)) { if (isset($tmpData[0])) { $this->uplinkData = $tmpData[0]; } } } } /** * Renders uplink parameters editing inputs * * @return string */ public function renderEditForm() { $result = ''; if (!empty($this->switchId)) { $mediaTmp = array('' => '-'); $mediaTmp += $this->mediaTypes; $speedTmp = array('' => '-'); $speedTmp += $this->speedRates; $inputs = wf_HiddenInput(self::ROUTE_SWID, $this->switchId); $inputs .= wf_Selector(self::ROUTE_MEDIA, $mediaTmp, __('Type'), @$this->uplinkData['media'], false) . ' '; $inputs .= wf_Selector(self::ROUTE_SPEED, $speedTmp, __('Speed'), @$this->uplinkData['speed'], false) . ' '; $inputs .= wf_TextInput(self::ROUTE_PORT, __('Port'), @$this->uplinkData['port'], false, 2, 'digits') . ' '; $result .= $inputs; //we need it for main edit form integration } else { $result .= $this->messages->getStyledMessage(__('Something went wrong') . ': ' . __('Switch') . ' ID ' . __('is empty'), 'error'); } return($result); } /** * Saves switch uplink data into database * * @return void */ public function save() { if (ubRouting::checkPost(array(self::ROUTE_SWID))) { $switchId = ubRouting::post(self::ROUTE_SWID, 'int'); $newMedia = ubRouting::post(self::ROUTE_MEDIA, 'mres'); $newSpeed = ubRouting::post(self::ROUTE_SPEED, 'mres'); $newPort = ubRouting::post(self::ROUTE_PORT, 'int'); $this->switchUplinks->data('media', $newMedia); $this->switchUplinks->data('speed', $newSpeed); $this->switchUplinks->data('port', $newPort); //updating existing record if (!empty($this->uplinkData)) { $this->switchUplinks->where('switchid', '=', $switchId); $this->switchUplinks->save(); } else { //creating new record $this->switchUplinks->data('switchid', $switchId); $this->switchUplinks->create(); } log_register('SWITCHUPLINK CHANGE [' . $switchId . '] MEDIA `' . $newMedia . '` SPEED `' . $newSpeed . '` PORT `' . $newPort . '`'); } } /** * Delete switch uplink data record from database * * @param int $switchId * * @return void */ public function delete($switchId) { $switchId = ubRouting::filters($switchId, 'int'); if ($switchId) { $this->switchUplinks->where('switchid', '=', $switchId); $this->switchUplinks->delete(); log_register('SWITCHUPLINK DELETE [' . $switchId . ']'); } } /** * Delete switch uplink data record from database on switch deletion * * @param int $switchId * * @return void */ public function flush($switchId) { $switchId = ubRouting::filters($switchId, 'int'); if ($switchId) { $this->switchUplinks->where('switchid', '=', $switchId); $this->switchUplinks->delete(); log_register('SWITCHUPLINK FLUSH [' . $switchId . ']'); } } /** * Renders current instance uplink data in compact format * * @return string */ public function renderSwitchUplinkData() { $result = ''; if (!empty($this->uplinkData)) { if (!empty($this->uplinkData['media'])) { if (isset($this->mediaIcons[$this->uplinkData['media']])) { $mediaIcon = wf_img_sized(self::PATH_ICONS . $this->mediaIcons[$this->uplinkData['media']], '', '10') . ' '; } else { $mediaIcon = ''; } $result .= $mediaIcon . $this->mediaTypes[$this->uplinkData['media']] . ' '; } if (!empty($this->uplinkData['speed'])) { $result .= $this->speedRates[$this->uplinkData['speed']] . ' '; } if (!empty($this->uplinkData['port'])) { $result .= $this->uplinkData['port'] . ' ' . __('Port'); } //empty existing record if (!$this->uplinkData['media'] AND !$this->uplinkData['speed'] AND !$this->uplinkData['port']) { $result .= __('Uplink parameters is not set'); } } else { $result .= __('Uplink parameters is not set'); } return($result); } /** * Loads all switches uplinks data * * @return void */ public function loadAllUplinksData() { $this->allUplinksData = $this->switchUplinks->getAll('switchid'); } /** * Returns count of available uplinks data records * * @return int */ public function getAllUplinksCount() { return(sizeof($this->allUplinksData)); } /** * Returns short uplink parameters text description * * @param int $swithchId * @param bool $includePort * * @return string */ public function getUplinkTinyDesc($swithchId, $includePort = false) { $result = ''; if (isset($this->allUplinksData[$swithchId])) { $media = $this->allUplinksData[$swithchId]['media']; $speed = $this->allUplinksData[$swithchId]['speed']; $icon = (isset($this->mediaIcons[$media])) ? wf_img(self::PATH_ICONS . $this->mediaIcons[$media], $this->mediaTypes[$media]) : ''; $result .= $icon . $media . $speed; //optional append port if ($includePort) { $port = $this->allUplinksData[$swithchId]['port']; if (!empty($port)) { $result .= 'p' . $port; } } } return($result); } /** * Returns short uplink port or void if not set * * @param int $swithchId * * @return int/void */ public function getUplinkPort($swithchId) { $result = ''; if (isset($this->allUplinksData[$swithchId])) { $port = $this->allUplinksData[$swithchId]['port']; if (!empty($port)) { $result .= $port; } } return($result); } } |