123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391 |
- <?php
- /**
- * VLAN management realms class
- */
- class Realms {
- const TABLE_NAME = 'realms';
- const MODULE = '?module=vlanmanagement&realms=true';
- /**
- * Placeholder for UbRouing object
- *
- * @var object
- */
- public $routing;
- /**
- * Contains errors
- *
- * @var array
- */
- protected $error = array();
- /**
- * Contains exceptions
- *
- * @var array
- */
- protected $exceptions = array();
- /**
- * Placeholder for nyan_orm object for `realms` table
- *
- * @var object
- */
- protected $db;
- /**
- * Placeholder for nyan_orm object for qinq_svlan table.
- *
- * @var object
- */
- protected $svlanDb;
- /**
- * Placeholder for nyan_orm object for qinq_bindings table.
- *
- * @var object
- */
- protected $qinqBindingsDb;
- /**
- * Contains all realms
- *
- * @var array
- */
- protected $allRealms = array();
- /**
- * Placeholder for UbillingMessageHelper instance.
- *
- * @var object
- */
- protected $messages;
- /**
- * Realms object
- *
- * @return void
- */
- public function __construct() {
- $this->db = new NyanORM(self::TABLE_NAME);
- $this->svlanDb = new NyanORM('qinq_svlan');
- $this->qinqBindingsDb = new NyanORM('qinq_bindings');
- $this->routing = new ubRouting();
- $this->messages = new UbillingMessageHelper();
- $this->allRealms = $this->db->getAll('id');
- }
- /**
- * Validator
- *
- * @return bool
- */
- protected function validate() {
- if (!$this->unique()) {
- $this->error[] = __('Realm exists') . ' :' . $this->routing->get('realm', 'mres');
- }
- if ($this->emptyVar()) {
- $this->error[] = __('Realm cannot be empty');
- }
- if ($this->protectDefault()) {
- $this->error[] = __('Default realm is protected and cannot be deleted or edited');
- }
- if (!empty($this->error)) {
- return(false);
- }
- return(true);
- }
- /**
- * Check if delete or edit not default entry
- *
- * @return bool
- */
- protected function protectDefault() {
- if (( $this->routing->get('action') == 'edit') or ( $this->routing->get('action') == 'delete')) {
- if ($this->routing->get('id') == 1) {
- return(true);
- }
- }
- return(false);
- }
- /**
- * Check if occasionally we do not receive empty vars like realm or id.
- *
- * @return bool
- */
- protected function emptyVar() {
- if (($this->routing->get('action') == 'add') or ( $this->routing->get('action') == 'edit')) {
- if (!$this->routing->get('realm', 'mres')) {
- return(true);
- }
- }
- if ($this->routing->get('action') == 'delete') {
- if (!$this->routing->get('id', 'int')) {
- return(true);
- }
- }
- return(false);
- }
- /**
- * Check if entry unique
- *
- * @return bool
- */
- protected function unique() {
- if (($this->routing->get('action') == 'edit') or ( $this->routing->get('action') == 'delete')) {
- //skip if edit
- return(true);
- } else {
- $allRealms = $this->db->getAll('realm');
- if (isset($allRealms[$this->routing->get('realm', 'mres')])) {
- return(false);
- } else {
- return(true);
- }
- }
- }
- /**
- * Adding new entry
- *
- * @return void
- */
- public function add() {
- try {
- if ($this->validate()) {
- $this->db->data('realm', $this->routing->get('realm', 'mres'));
- $this->db->data('description', $this->routing->get('description', 'mres'));
- $this->db->create();
- $this->logAdd();
- }
- $this->goToStartOrError();
- } catch (Exception $ex) {
- $this->exceptions[] = $ex;
- $this->goToStartOrError();
- }
- }
- /**
- * Edit entry
- *
- * @return void
- */
- public function edit() {
- try {
- if ($this->validate()) {
- $this->db->where('id', '=', $this->routing->get('id', 'int'));
- $this->db->data('realm', $this->routing->get('realm', 'mres'));
- $this->db->data('description', $this->routing->get('description', 'mres'));
- $this->db->save();
- $this->logEdit();
- }
- $this->goToStartOrError();
- } catch (Exception $ex) {
- $this->exceptions[] = $ex;
- $this->goToStartOrError();
- }
- }
- /**
- * Delete entry
- *
- * @return void
- */
- public function delete() {
- try {
- if ($this->validate()) {
- $this->db->where('id', '=', $this->routing->get('id', 'int'));
- $this->db->delete();
- //deleting all the c-vlan and s-vlan which belong to this realm
- $this->svlanDb->where('realm_id', '=', $this->routing->get('id', 'int'));
- $allVlanId = $this->svlanDb->getAll('id');
- foreach ($allVlanId as $id => $data) {
- $this->qinqBindingsDb->where("svlan_id", '=', $id);
- }
- $this->qinqBindingsDb->delete();
- $this->svlanDb->where('realm_id', '=', $this->routing->get('id', 'int'));
- $this->svlanDb->delete();
- $this->logDelete();
- }
- $this->goToStartOrError();
- } catch (Exception $ex) {
- $this->exceptions[] = $ex;
- $this->goToStartOrError();
- }
- }
- /**
- * Show all the entries
- *
- * @return string
- */
- public function showAll() {
- $modal = '<link rel="stylesheet" href="./skins/vlanmanagement.css" type="text/css" media="screen" />';
- $modal .= wf_tag('div', false, 'cvmodal', 'id="dialog-modal_cvmodal" title="' . __('Choose') . '" style="display:none; width:1px; height:1px;"');
- $modal .= wf_tag('p', false, '', 'id="content-cvmodal"');
- $modal .= wf_tag('p', true);
- $modal .= wf_tag('div', true);
- $modal .= '<script src="./modules/jsc/vlanmanagement.js" type="text/javascript"></script>';
- $columns = array('ID', 'Realm', 'Description', 'Actions');
- $opts = '"order": [[ 0, "desc" ]]';
- $result = '';
- $ajaxURL = '' . self::MODULE . '&action=ajax';
- $result .= show_window('', $modal . wf_JqDtLoader($columns, $ajaxURL, false, __('Realms'), 100, $opts));
- return ($result);
- }
- /**
- * Form to create new entry
- *
- * @return void
- */
- protected function addForm() {
- $addControls = wf_HiddenInput('module', 'vlanmanagement');
- $addControls .= wf_HiddenInput('realms', 'true');
- $addControls .= wf_HiddenInput('action', 'add');
- $addControls .= wf_TextInput('realm', __('Realm'), '', true, '', '');
- $addControls .= wf_TextInput('description', __('Description'), '', true, '', '');
- $addControls .= wf_Submit('Save');
- $form = wf_Form('', 'GET', $addControls, 'glamour');
- return(wf_modalAuto(web_icon_create() . ' ' . __('Create new entry'), __('Create new entry'), $form, 'ubButton'));
- }
- /**
- * Forming edit form
- *
- * @param string $encoded
- *
- * @return string
- */
- public function ajaxEdit($encoded) {
- $decoded = unserialize(base64_decode($encoded));
- $addControls = wf_HiddenInput('module', 'vlanmanagement');
- $addControls .= wf_HiddenInput('realms', 'true');
- $addControls .= wf_HiddenInput('action', 'edit');
- $addControls .= wf_HiddenInput('id', $decoded['id']);
- $addControls .= wf_TextInput('realm', __('Realm'), $decoded['realm'], true, '');
- $addControls .= wf_TextInput('description', __('Description'), $decoded['description'], true, '');
- $addControls .= wf_HiddenInput('old_realm', $decoded['realm']);
- $addControls .= wf_Submit('Save');
- $form = wf_Form('', 'GET', $addControls, 'glamour');
- return($form);
- }
- protected function back() {
- return(wf_BackLink(VlanManagement::MODULE, __('Back'), false, 'ubButton'));
- }
- public function links() {
- show_window('', '' .
- $this->back() .
- $this->addForm()
- );
- }
- /**
- * Form all the entries to ajax array
- *
- * @return void
- */
- public function ajaxData() {
- $json = new wf_JqDtHelper();
- if (!empty($this->allRealms)) {
- foreach ($this->allRealms as $io => $each) {
- $eachId = base64_encode(serialize(array(
- 'id' => $each['id'],
- 'realm' => $each['realm'],
- 'description' => $each['description']
- )));
- $actLinks = wf_tag('div', false, '', 'id="' . $eachId . '" onclick="realmEdit(this)" style="display:inline-block;"') . web_edit_icon() . wf_tag('div', true);
- $actLinks .= wf_JSAlert(self::MODULE . '&action=delete&id=' . $each['id'], web_delete_icon(), $this->messages->getDeleteAlert());
- $data[] = $each['id'];
- $data[] = $each['realm'];
- $data[] = $each['description'];
- $data[] = $actLinks;
- $json->addRow($data);
- unset($data);
- }
- }
- $json->getJson();
- }
- /**
- * Redirects user back and show error if any
- *
- * @return void
- */
- protected function goToStartOrError() {
- if (empty($this->error)) {
- rcms_redirect(self::MODULE);
- } else {
- $this->showError();
- if (!empty($this->exceptions)) {
- $this->showExceptions();
- }
- }
- }
- /**
- * If we have any errors show all of them
- *
- * @return void
- */
- protected function showError() {
- foreach ($this->error as $io => $each) {
- show_error($each);
- }
- }
- /**
- * Show all exceptions if any.
- *
- * @return void
- */
- protected function showExceptions() {
- foreach ($this->exceptions as $io => $each) {
- show_error($each);
- }
- }
- /**
- * Log add action
- *
- * @return void
- */
- protected function logAdd() {
- log_register('CREATE realm (' . trim($this->routing->get('realm', 'mres')) . ')');
- }
- /**
- * Log delete action
- *
- * @return void
- */
- protected function logDelete() {
- log_register('DELETE realm (' . trim($this->routing->get('realm', 'mres')) . ')');
- }
- /**
- * Log edit action
- *
- * @return void
- */
- protected function logEdit() {
- log_register('EDIT realm (' . trim($this->routing->get('old_realm', 'mres')) . ') ' . 'ON (' . trim($this->routing->get('realm', 'mres')) . ')');
- }
- }
|