api.realms.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. <?php
  2. /**
  3. * VLAN management realms class
  4. */
  5. class Realms {
  6. const TABLE_NAME = 'realms';
  7. const MODULE = '?module=vlanmanagement&realms=true';
  8. /**
  9. * Placeholder for UbRouing object
  10. *
  11. * @var object
  12. */
  13. public $routing;
  14. /**
  15. * Contains errors
  16. *
  17. * @var array
  18. */
  19. protected $error = array();
  20. /**
  21. * Contains exceptions
  22. *
  23. * @var array
  24. */
  25. protected $exceptions = array();
  26. /**
  27. * Placeholder for nyan_orm object for `realms` table
  28. *
  29. * @var object
  30. */
  31. protected $db;
  32. /**
  33. * Placeholder for nyan_orm object for qinq_svlan table.
  34. *
  35. * @var object
  36. */
  37. protected $svlanDb;
  38. /**
  39. * Placeholder for nyan_orm object for qinq_bindings table.
  40. *
  41. * @var object
  42. */
  43. protected $qinqBindingsDb;
  44. /**
  45. * Contains all realms
  46. *
  47. * @var array
  48. */
  49. protected $allRealms = array();
  50. /**
  51. * Placeholder for UbillingMessageHelper instance.
  52. *
  53. * @var object
  54. */
  55. protected $messages;
  56. /**
  57. * Realms object
  58. *
  59. * @return void
  60. */
  61. public function __construct() {
  62. $this->db = new NyanORM(self::TABLE_NAME);
  63. $this->svlanDb = new NyanORM('qinq_svlan');
  64. $this->qinqBindingsDb = new NyanORM('qinq_bindings');
  65. $this->routing = new ubRouting();
  66. $this->messages = new UbillingMessageHelper();
  67. $this->allRealms = $this->db->getAll('id');
  68. }
  69. /**
  70. * Validator
  71. *
  72. * @return bool
  73. */
  74. protected function validate() {
  75. if (!$this->unique()) {
  76. $this->error[] = __('Realm exists') . ' :' . $this->routing->get('realm', 'mres');
  77. }
  78. if ($this->emptyVar()) {
  79. $this->error[] = __('Realm cannot be empty');
  80. }
  81. if ($this->protectDefault()) {
  82. $this->error[] = __('Default realm is protected and cannot be deleted or edited');
  83. }
  84. if (!empty($this->error)) {
  85. return(false);
  86. }
  87. return(true);
  88. }
  89. /**
  90. * Check if delete or edit not default entry
  91. *
  92. * @return bool
  93. */
  94. protected function protectDefault() {
  95. if (( $this->routing->get('action') == 'edit') or ( $this->routing->get('action') == 'delete')) {
  96. if ($this->routing->get('id') == 1) {
  97. return(true);
  98. }
  99. }
  100. return(false);
  101. }
  102. /**
  103. * Check if occasionally we do not receive empty vars like realm or id.
  104. *
  105. * @return bool
  106. */
  107. protected function emptyVar() {
  108. if (($this->routing->get('action') == 'add') or ( $this->routing->get('action') == 'edit')) {
  109. if (!$this->routing->get('realm', 'mres')) {
  110. return(true);
  111. }
  112. }
  113. if ($this->routing->get('action') == 'delete') {
  114. if (!$this->routing->get('id', 'int')) {
  115. return(true);
  116. }
  117. }
  118. return(false);
  119. }
  120. /**
  121. * Check if entry unique
  122. *
  123. * @return bool
  124. */
  125. protected function unique() {
  126. if (($this->routing->get('action') == 'edit') or ( $this->routing->get('action') == 'delete')) {
  127. //skip if edit
  128. return(true);
  129. } else {
  130. $allRealms = $this->db->getAll('realm');
  131. if (isset($allRealms[$this->routing->get('realm', 'mres')])) {
  132. return(false);
  133. } else {
  134. return(true);
  135. }
  136. }
  137. }
  138. /**
  139. * Adding new entry
  140. *
  141. * @return void
  142. */
  143. public function add() {
  144. try {
  145. if ($this->validate()) {
  146. $this->db->data('realm', $this->routing->get('realm', 'mres'));
  147. $this->db->data('description', $this->routing->get('description', 'mres'));
  148. $this->db->create();
  149. $this->logAdd();
  150. }
  151. $this->goToStartOrError();
  152. } catch (Exception $ex) {
  153. $this->exceptions[] = $ex;
  154. $this->goToStartOrError();
  155. }
  156. }
  157. /**
  158. * Edit entry
  159. *
  160. * @return void
  161. */
  162. public function edit() {
  163. try {
  164. if ($this->validate()) {
  165. $this->db->where('id', '=', $this->routing->get('id', 'int'));
  166. $this->db->data('realm', $this->routing->get('realm', 'mres'));
  167. $this->db->data('description', $this->routing->get('description', 'mres'));
  168. $this->db->save();
  169. $this->logEdit();
  170. }
  171. $this->goToStartOrError();
  172. } catch (Exception $ex) {
  173. $this->exceptions[] = $ex;
  174. $this->goToStartOrError();
  175. }
  176. }
  177. /**
  178. * Delete entry
  179. *
  180. * @return void
  181. */
  182. public function delete() {
  183. try {
  184. if ($this->validate()) {
  185. $this->db->where('id', '=', $this->routing->get('id', 'int'));
  186. $this->db->delete();
  187. //deleting all the c-vlan and s-vlan which belong to this realm
  188. $this->svlanDb->where('realm_id', '=', $this->routing->get('id', 'int'));
  189. $allVlanId = $this->svlanDb->getAll('id');
  190. foreach ($allVlanId as $id => $data) {
  191. $this->qinqBindingsDb->where("svlan_id", '=', $id);
  192. }
  193. $this->qinqBindingsDb->delete();
  194. $this->svlanDb->where('realm_id', '=', $this->routing->get('id', 'int'));
  195. $this->svlanDb->delete();
  196. $this->logDelete();
  197. }
  198. $this->goToStartOrError();
  199. } catch (Exception $ex) {
  200. $this->exceptions[] = $ex;
  201. $this->goToStartOrError();
  202. }
  203. }
  204. /**
  205. * Show all the entries
  206. *
  207. * @return string
  208. */
  209. public function showAll() {
  210. $modal = '<link rel="stylesheet" href="./skins/vlanmanagement.css" type="text/css" media="screen" />';
  211. $modal .= wf_tag('div', false, 'cvmodal', 'id="dialog-modal_cvmodal" title="' . __('Choose') . '" style="display:none; width:1px; height:1px;"');
  212. $modal .= wf_tag('p', false, '', 'id="content-cvmodal"');
  213. $modal .= wf_tag('p', true);
  214. $modal .= wf_tag('div', true);
  215. $modal .= '<script src="./modules/jsc/vlanmanagement.js" type="text/javascript"></script>';
  216. $columns = array('ID', 'Realm', 'Description', 'Actions');
  217. $opts = '"order": [[ 0, "desc" ]]';
  218. $result = '';
  219. $ajaxURL = '' . self::MODULE . '&action=ajax';
  220. $result .= show_window('', $modal . wf_JqDtLoader($columns, $ajaxURL, false, __('Realms'), 100, $opts));
  221. return ($result);
  222. }
  223. /**
  224. * Form to create new entry
  225. *
  226. * @return void
  227. */
  228. protected function addForm() {
  229. $addControls = wf_HiddenInput('module', 'vlanmanagement');
  230. $addControls .= wf_HiddenInput('realms', 'true');
  231. $addControls .= wf_HiddenInput('action', 'add');
  232. $addControls .= wf_TextInput('realm', __('Realm'), '', true, '', '');
  233. $addControls .= wf_TextInput('description', __('Description'), '', true, '', '');
  234. $addControls .= wf_Submit('Save');
  235. $form = wf_Form('', 'GET', $addControls, 'glamour');
  236. return(wf_modalAuto(web_icon_create() . ' ' . __('Create new entry'), __('Create new entry'), $form, 'ubButton'));
  237. }
  238. /**
  239. * Forming edit form
  240. *
  241. * @param string $encoded
  242. *
  243. * @return string
  244. */
  245. public function ajaxEdit($encoded) {
  246. $decoded = unserialize(base64_decode($encoded));
  247. $addControls = wf_HiddenInput('module', 'vlanmanagement');
  248. $addControls .= wf_HiddenInput('realms', 'true');
  249. $addControls .= wf_HiddenInput('action', 'edit');
  250. $addControls .= wf_HiddenInput('id', $decoded['id']);
  251. $addControls .= wf_TextInput('realm', __('Realm'), $decoded['realm'], true, '');
  252. $addControls .= wf_TextInput('description', __('Description'), $decoded['description'], true, '');
  253. $addControls .= wf_HiddenInput('old_realm', $decoded['realm']);
  254. $addControls .= wf_Submit('Save');
  255. $form = wf_Form('', 'GET', $addControls, 'glamour');
  256. return($form);
  257. }
  258. protected function back() {
  259. return(wf_BackLink(VlanManagement::MODULE, __('Back'), false, 'ubButton'));
  260. }
  261. public function links() {
  262. show_window('', '' .
  263. $this->back() .
  264. $this->addForm()
  265. );
  266. }
  267. /**
  268. * Form all the entries to ajax array
  269. *
  270. * @return void
  271. */
  272. public function ajaxData() {
  273. $json = new wf_JqDtHelper();
  274. if (!empty($this->allRealms)) {
  275. foreach ($this->allRealms as $io => $each) {
  276. $eachId = base64_encode(serialize(array(
  277. 'id' => $each['id'],
  278. 'realm' => $each['realm'],
  279. 'description' => $each['description']
  280. )));
  281. $actLinks = wf_tag('div', false, '', 'id="' . $eachId . '" onclick="realmEdit(this)" style="display:inline-block;"') . web_edit_icon() . wf_tag('div', true);
  282. $actLinks .= wf_JSAlert(self::MODULE . '&action=delete&id=' . $each['id'], web_delete_icon(), $this->messages->getDeleteAlert());
  283. $data[] = $each['id'];
  284. $data[] = $each['realm'];
  285. $data[] = $each['description'];
  286. $data[] = $actLinks;
  287. $json->addRow($data);
  288. unset($data);
  289. }
  290. }
  291. $json->getJson();
  292. }
  293. /**
  294. * Redirects user back and show error if any
  295. *
  296. * @return void
  297. */
  298. protected function goToStartOrError() {
  299. if (empty($this->error)) {
  300. rcms_redirect(self::MODULE);
  301. } else {
  302. $this->showError();
  303. if (!empty($this->exceptions)) {
  304. $this->showExceptions();
  305. }
  306. }
  307. }
  308. /**
  309. * If we have any errors show all of them
  310. *
  311. * @return void
  312. */
  313. protected function showError() {
  314. foreach ($this->error as $io => $each) {
  315. show_error($each);
  316. }
  317. }
  318. /**
  319. * Show all exceptions if any.
  320. *
  321. * @return void
  322. */
  323. protected function showExceptions() {
  324. foreach ($this->exceptions as $io => $each) {
  325. show_error($each);
  326. }
  327. }
  328. /**
  329. * Log add action
  330. *
  331. * @return void
  332. */
  333. protected function logAdd() {
  334. log_register('CREATE realm (' . trim($this->routing->get('realm', 'mres')) . ')');
  335. }
  336. /**
  337. * Log delete action
  338. *
  339. * @return void
  340. */
  341. protected function logDelete() {
  342. log_register('DELETE realm (' . trim($this->routing->get('realm', 'mres')) . ')');
  343. }
  344. /**
  345. * Log edit action
  346. *
  347. * @return void
  348. */
  349. protected function logEdit() {
  350. log_register('EDIT realm (' . trim($this->routing->get('old_realm', 'mres')) . ') ' . 'ON (' . trim($this->routing->get('realm', 'mres')) . ')');
  351. }
  352. }