api.switchgroups.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. <?php
  2. /**
  3. * Allows to put some switches into groups
  4. */
  5. class SwitchGroups {
  6. /**
  7. * Contains default interface module URL
  8. */
  9. const URL_ME = '?module=switchgroups';
  10. /**
  11. * System message helper object placeholder
  12. *
  13. * @var object
  14. */
  15. protected $messages = null;
  16. /**
  17. * Placeholder for DEVICES_LISTS_SORT_BY_MODELNAME alter.ini option
  18. *
  19. * @var bool
  20. */
  21. protected $sortByModelName = false;
  22. public function __construct() {
  23. global $ubillingConfig;
  24. $this->initMessages();
  25. $this->sortByModelName = $ubillingConfig->getAlterParam('DEVICES_LISTS_SORT_BY_MODELNAME');
  26. }
  27. /**
  28. * Inits message helper object for further usage
  29. *
  30. * @return void
  31. */
  32. protected function initMessages() {
  33. $this->messages = new UbillingMessageHelper();
  34. }
  35. /**
  36. * Returns reference to UbillingMessageHelper object
  37. *
  38. * @return object
  39. */
  40. public function getUbMsgHelperInstance() {
  41. return $this->messages;
  42. }
  43. /**
  44. * Gets switches groups data from DB
  45. *
  46. * @param string $whereString - WHERE clause for SQL query
  47. * @param bool $addSwCountColumn
  48. *
  49. * @return array
  50. */
  51. public function getSwitchGroupsData($whereString = '', $addSwCountColumn = false) {
  52. if ($addSwCountColumn) {
  53. $query = "SELECT `switch_groups`.`id`, `switch_groups`.`groupname`, `switch_groups`.`groupdescr`, `tGrpRel`.`sw_cnt`
  54. FROM `switch_groups`
  55. LEFT JOIN (SELECT DISTINCT `switch_groups_relations`.`sw_group_id`, count(`switch_groups_relations`.`sw_group_id`) AS `sw_cnt`
  56. FROM `switch_groups_relations`
  57. GROUP BY `switch_groups_relations`.`sw_group_id`
  58. ORDER BY `switch_groups_relations`.`sw_group_id`) AS tGrpRel
  59. ON `tGrpRel`.`sw_group_id` = `switch_groups`.`id`" .
  60. $whereString;
  61. } else {
  62. $query = "SELECT * FROM `switch_groups` " . $whereString;
  63. }
  64. $result = simple_queryall($query);
  65. return ($result);
  66. }
  67. /**
  68. * Finds group Id by it's name
  69. *
  70. * @param $groupName
  71. *
  72. * @return int
  73. */
  74. public function getSwitchGroupIdByName($groupName) {
  75. $result = '';
  76. if (!empty($groupName)) {
  77. $query = "SELECT `id` FROM `switch_groups` WHERE `groupname`='" . $groupName . "'";
  78. $result = simple_queryall($query);
  79. }
  80. return ( empty($result) ) ? 0 : $result[0]['id'];
  81. }
  82. /**
  83. * Finds group name by it's Id
  84. *
  85. * @param $groupId
  86. *
  87. * @return int
  88. */
  89. public function getSwitchGroupNameById($groupId) {
  90. $result = '';
  91. if (!empty($groupId)) {
  92. $query = "SELECT `groupname` FROM `switch_groups` WHERE `id`= " . $groupId;
  93. $result = simple_queryall($query);
  94. }
  95. return ( empty($result) ) ? '' : $result[0]['groupname'];
  96. }
  97. /**
  98. * Returns array with all of the switches IDs and their group data side by side
  99. *
  100. * @return array
  101. */
  102. public function getSwitchesIdsWithGroupsData() {
  103. $result = array();
  104. $query = "SELECT `switch_groups_relations`.`switch_id`, `switch_groups`.`id` AS `groupid`, `switch_groups`.`groupname`, `switch_groups`.`groupdescr`
  105. FROM `switch_groups_relations`
  106. LEFT JOIN `switch_groups` ON `switch_groups_relations`.`sw_group_id` = `switch_groups`.`id`";
  107. $queryResult = simple_queryall($query);
  108. if (!empty($queryResult)) {
  109. foreach ($queryResult as $eachRec) {
  110. $result[$eachRec['switch_id']] = array( 'groupid' => $eachRec['groupid'],
  111. 'groupname' => $eachRec['groupname'],
  112. 'groupdescr' => $eachRec['groupdescr'] );
  113. }
  114. }
  115. return ($result);
  116. }
  117. /**
  118. * Returns switch group data by switch ID
  119. *
  120. * @param $switchId
  121. * @param string $returnVal - can be: 'all', 'name', 'id' - depending on what value you want to get
  122. *
  123. * @return array|string|int
  124. */
  125. public function getSwitchGroupBySwitchId($switchId, $returnVal = 'id') {
  126. $result = '';
  127. $query = "SELECT `switch_groups`.`id`, `switch_groups`.`groupname`, `switch_groups`.`groupdescr`
  128. FROM `switch_groups_relations`
  129. LEFT JOIN `switch_groups` ON `switch_groups_relations`.`sw_group_id` = `switch_groups`.`id`
  130. WHERE `switch_groups_relations`.`switch_id` = " . $switchId;
  131. $queryResult = simple_queryall($query);
  132. if (!empty($queryResult)) {
  133. switch ($returnVal) {
  134. case 'all':
  135. $result = $queryResult;
  136. break;
  137. case 'name':
  138. $result = $queryResult[0]['groupname'];
  139. break;
  140. default:
  141. $result = $queryResult[0]['id'];
  142. }
  143. }
  144. return ($result);
  145. }
  146. /**
  147. * Returns how many switches are there in a group
  148. *
  149. * @param $groupId
  150. *
  151. * @return int
  152. */
  153. public function countSwitchesInGroup($groupId) {
  154. $result = '';
  155. if (!empty($groupId)) {
  156. $query = "SELECT count(*) AS `sw_cnt` FROM `switch_groups_relations` WHERE `sw_group_id`=" . $groupId;
  157. $result = simple_queryall($query);
  158. }
  159. return ( empty($result) ) ? 0 : $result[0]['sw_cnt'];
  160. }
  161. /**
  162. * Returns array of switches in a group
  163. *
  164. * @param $swGroupId
  165. *
  166. * @return array
  167. */
  168. public function getSwithcesInGroup($swGroupId) {
  169. $queryOrderBy = ($this->sortByModelName) ? ' ORDER BY `switchmodels`.`modelname` ' : '';
  170. $query = "SELECT `swgrp`.`id`, `swgrp`.`ip`, `swgrp`.`location`, `swgrp`.`groupname`, `switchmodels`.`modelname`
  171. FROM ( SELECT `switches`.`id`, `switches`.`modelid`, `switches`.`ip`, `switches`.`location`, `switch_groups`.`groupname`
  172. FROM `switch_groups_relations`
  173. LEFT JOIN `switches` ON `switch_groups_relations`.`switch_id` = `switches`.`id`
  174. LEFT JOIN `switch_groups` ON `switch_groups_relations`.`sw_group_id` = `switch_groups`.`id`
  175. WHERE `switch_groups_relations`.`sw_group_id` = " . $swGroupId . " ) AS `swgrp`
  176. LEFT JOIN `switchmodels` ON `swgrp`.`modelid` = `switchmodels`.`id`" . $queryOrderBy;
  177. $result = simple_queryall($query);
  178. return ($result);
  179. }
  180. /**
  181. * Returns HTML table with switches list in a group
  182. *
  183. * @param $swGroupId
  184. *
  185. * @return string
  186. */
  187. public function renderSwitchesInGroupTable($swGroupId) {
  188. $tableBody = '';
  189. $totalCount = 0;
  190. $swGroupName = $this->getSwitchGroupNameById($swGroupId);
  191. $tableCells = wf_TableCell(__('ID'));
  192. $tableCells.= wf_TableCell(__('IP'));
  193. $tableCells.= wf_TableCell(__('Model'));
  194. $tableCells.= wf_TableCell(__('Location'));
  195. $tableCells.= wf_TableCell(__('Actions'));
  196. $tableRows = wf_TableRow($tableCells, 'row1');
  197. $switches = $this->getSwithcesInGroup($swGroupId);
  198. if (!empty($switches)) {
  199. foreach ($switches as $eachSwitch) {
  200. $tableCells = wf_TableCell($eachSwitch['id']);
  201. $tableCells.= wf_TableCell($eachSwitch['ip']);
  202. $tableCells.= wf_TableCell($eachSwitch['modelname']);
  203. $tableCells.= wf_TableCell($eachSwitch['location']);
  204. $tableCells.= wf_TableCell(wf_Link('?module=switches&edit=' . $eachSwitch['id'], web_edit_icon()) .
  205. wf_Link('http://' . $eachSwitch['ip'], wf_img('skins/ymaps/network.png'),
  206. false, '', 'target="_blank" title="' . __('Go to the web interface') . '"') );
  207. $tableRows.= wf_TableRow($tableCells, 'row3');
  208. $totalCount++;
  209. }
  210. }
  211. $tableBody = wf_tag('h3') . __('Switches of the group') . ':' . wf_nbsp(2) . $swGroupName . wf_tag('h3', true);
  212. $tableBody.= wf_delimiter(0);
  213. $tableBody.= wf_TableBody($tableRows, '100%', '0', 'sortable');
  214. $tableBody.= wf_tag('h4') . __('Total') . wf_nbsp(2) . $totalCount . wf_tag('h4', true);
  215. return ($tableBody);
  216. }
  217. public function renderSwitchGroupsSelector($selectorName = 'swgroup', $switchId = 0) {
  218. $result = '';
  219. $swGroupsArray = array('0' => '-');
  220. $swGroups = $this->getSwitchGroupsData();
  221. $swGroupId = $this->getSwitchGroupBySwitchId($switchId);
  222. if (!empty($swGroups)) {
  223. foreach ($swGroups as $eachRec => $eachGroup) {
  224. $swGroupsArray[$eachGroup['id']] = $eachGroup['groupname'];
  225. }
  226. $result = wf_Selector($selectorName, $swGroupsArray, __('Switch group'), $swGroupId, false, true);
  227. }
  228. return ($result);
  229. }
  230. /**
  231. * Renders JSON for JQDT
  232. *
  233. * @param $queryData
  234. */
  235. public function renderJSON($queryData) {
  236. $json = new wf_JqDtHelper();
  237. if (!empty($queryData)) {
  238. $data = array();
  239. $groupId = 0;
  240. foreach ($queryData as $eachRec) {
  241. foreach ($eachRec as $fieldName => $fieldVal) {
  242. if ($fieldName == 'id') {
  243. $groupId = $fieldVal;
  244. }
  245. $data[] = $fieldVal;
  246. }
  247. $linkId1 = wf_InputId();
  248. $linkId2 = wf_InputId();
  249. $actions = wf_JSAlert( '#', web_delete_icon(), 'Removing this may lead to irreparable results', 'deleteSWGroup(' . $eachRec['id'] . ', \'' . self::URL_ME . '\', \'delSWGroup\', \'' . wf_InputId() . '\')') . wf_nbsp();
  250. $actions.= wf_Link('#', web_edit_icon(), false, '', 'id="' . $linkId1 . '"') . wf_nbsp();
  251. $actions.= wf_Link('#', wf_img_sized('skins/ymaps/switchdir.png', __('Show switches in this group'), '16', '16'), false, '', 'id="' . $linkId2 . '"');
  252. $actions.= wf_tag('script', false, '', 'type="text/javascript"');
  253. $actions.= wf_JSAjaxModalOpener(self::URL_ME, array('swgroupedit' => 'true', 'swgroupid' => $groupId), $linkId1, false, 'POST');
  254. $actions.= wf_JSAjaxModalOpener(self::URL_ME, array('showswingroup' => 'true', 'swgroupid' => $groupId), $linkId2, false, 'POST');
  255. $actions.= wf_tag('script', true);
  256. $data[] = $actions;
  257. $json->addRow($data);
  258. unset($data);
  259. }
  260. }
  261. $json->getJson();
  262. }
  263. /**
  264. * Returns JQDT control and some JS bindings for dynamic forms
  265. *
  266. * @return string
  267. */
  268. public function renderJQDT() {
  269. $ajaxUrlStr = '' . self::URL_ME . '&ajax=true' . '';
  270. $jqdtId = 'jqdt_' . md5($ajaxUrlStr);
  271. $errorModalWindowId = wf_InputId();
  272. $columns = array();
  273. $opts = '"order": [[ 0, "asc" ]]';
  274. $columns[] = __('ID');
  275. $columns[] = __('Name');
  276. $columns[] = __('Description');
  277. $columns[] = __('Switch count');
  278. $columns[] = __('Actions');
  279. $result = wf_JqDtLoader($columns, $ajaxUrlStr, false, __('results'), 100, $opts);
  280. $result.= wf_tag('script', false, '', 'type="text/javascript"');
  281. $result.= wf_JSEmptyFunc();
  282. $result.= wf_JSElemInsertedCatcherFunc();
  283. $result.= ' function chekEmptyVal(ctrlCalssName) {
  284. $(document).on("focus keydown", ctrlCalssName, function(evt) {
  285. if ( $(ctrlCalssName).css("border-color") == "rgb(255, 0, 0)" ) {
  286. $(ctrlCalssName).val("");
  287. $(ctrlCalssName).css("border-color", "");
  288. $(ctrlCalssName).css("color", "");
  289. }
  290. });
  291. }
  292. onElementInserted(\'body\', \'.__SWGroupEmptyCheck\', function(element) {
  293. chekEmptyVal(\'.__SWGroupEmptyCheck\');
  294. });
  295. $(document).on("submit", ".__SWGroupForm", function(evt) {
  296. var FrmAction = $(".__SWGroupForm").attr("action");
  297. var FrmData = $(".__SWGroupForm").serialize() + \'&errfrmid=' . $errorModalWindowId . '\';
  298. //var modalWindowId = $(".__SWGroupForm").closest(\'div\').attr(\'id\');
  299. evt.preventDefault();
  300. var emptyCheckClass = \'.__SWGroupEmptyCheck\';
  301. if ( empty( $(emptyCheckClass).val() ) || $(emptyCheckClass).css("border-color") == "rgb(255, 0, 0)" ) {
  302. $(emptyCheckClass).css("border-color", "red");
  303. $(emptyCheckClass).css("color", "grey");
  304. $(emptyCheckClass).val("' . __('Mandatory field') . '");
  305. } else {
  306. $.ajax({
  307. type: "POST",
  308. url: FrmAction,
  309. data: FrmData,
  310. success: function(result) {
  311. if ( !empty(result) ) {
  312. $(document.body).append(result);
  313. $( \'#' . $errorModalWindowId . '\' ).dialog("open");
  314. } else {
  315. $(\'#' . $jqdtId . '\').DataTable().ajax.reload();
  316. $("[name=swgroupname]").val("");
  317. if ( $(".__CloseFrmOnSubmitChk").is(\':checked\') ) {
  318. $( \'#\'+$(".__SWGroupFormModalWindowId").val() ).dialog("close");
  319. }
  320. }
  321. }
  322. });
  323. }
  324. });
  325. function deleteSWGroup(swGroupId, ajaxURL, actionName, errFrmId) {
  326. var ajaxData = \'&\'+ actionName +\'=true&swgroupid=\' + swGroupId + \'&errfrmid=\' + errFrmId
  327. $.ajax({
  328. type: "POST",
  329. url: ajaxURL,
  330. data: ajaxData,
  331. success: function(result) {
  332. if ( !empty(result) ) {
  333. $(document.body).append(result);
  334. $(\'#\'+errFrmId).dialog("open");
  335. }
  336. $(\'#' . $jqdtId . '\').DataTable().ajax.reload();
  337. }
  338. });
  339. }
  340. ';
  341. $result.= wf_tag('script', true);
  342. return ($result);
  343. }
  344. /**
  345. * Returns switch group addition form
  346. *
  347. * @return string
  348. */
  349. public function renderAddForm($modalWindowId) {
  350. $formId = 'Form_' . wf_InputId();
  351. $closeFormChkId = 'CloseFrmChkID_' . wf_InputId();
  352. $inputs = wf_TextInput('swgroupname', __('Name'), '', true, '', '', '__SWGroupEmptyCheck');
  353. $inputs.= wf_TextInput('swgroupdescr', __('Description'), '', true);
  354. $inputs .= wf_CheckInput('formclose', __('Close form after operation'), false, true, $closeFormChkId, '__CloseFrmOnSubmitChk');
  355. $inputs .= wf_HiddenInput('', $modalWindowId, '', '__SWGroupFormModalWindowId');
  356. $inputs .= wf_HiddenInput('swgroupcreate', 'true');
  357. $inputs .= wf_delimiter();
  358. $inputs .= wf_Submit(__('Create'));
  359. $form = wf_Form(self::URL_ME, 'POST', $inputs, 'glamour __SWGroupForm', '', $formId);
  360. return ($form);
  361. }
  362. /**
  363. * Returns switch group editing form
  364. *
  365. * @return string
  366. */
  367. public function renderEditForm($swGroupId, $modalWindowId) {
  368. $formId = 'Form_' . wf_InputId();
  369. $closeFormChkId = 'CloseFrmChkID_' . wf_InputId();
  370. $swGroupData = $this->getSwitchGroupsData(" WHERE `id` = " . $swGroupId);
  371. $swGroupName = $swGroupData[0]['groupname'];
  372. $swGroupDescription = $swGroupData[0]['groupdescr'];
  373. $inputs = wf_TextInput('swgroupname', __('Name'), $swGroupName, true, '', '', '__SWGroupEmptyCheck');
  374. $inputs.= wf_TextInput('swgroupdescr', __('Description'), $swGroupDescription, true);
  375. $inputs .= wf_CheckInput('formclose', __('Close form after operation'), false, true, $closeFormChkId, '__CloseFrmOnSubmitChk');
  376. $inputs .= wf_HiddenInput('', $modalWindowId, '', '__SWGroupFormModalWindowId');
  377. $inputs .= wf_HiddenInput('swgroupedit', 'true');
  378. $inputs .= wf_HiddenInput('swgroupid', $swGroupId);
  379. $inputs .= wf_delimiter();
  380. $inputs .= wf_Submit(__('Edit'));
  381. $form = wf_Form(self::URL_ME, 'POST', $inputs, 'glamour __SWGroupForm', '', $formId);
  382. return ($form);
  383. }
  384. /**
  385. * Adds switch group to DB
  386. *
  387. * @param $swGroupName
  388. * @param $swGroupDescr
  389. */
  390. public function addSwitchGroup($swGroupName, $swGroupDescr) {
  391. $query = "INSERT INTO `switch_groups` (`id`, `groupname`, `groupdescr`) VALUES (NULL, '" . $swGroupName . "', '" . $swGroupDescr . "')";
  392. nr_query($query);
  393. log_register('CREATE switch group [' . $swGroupName . ']');
  394. }
  395. /**
  396. * Edits switch group
  397. *
  398. * @param $swGroupId
  399. * @param $swGroupName
  400. * @param $swGroupDescr
  401. */
  402. public function editSwitchGroup($swGroupId, $swGroupName, $swGroupDescr) {
  403. $query = "UPDATE `switch_groups`
  404. SET `groupname` = '" . $swGroupName . "',
  405. `groupdescr` = '" . $swGroupDescr . "'
  406. WHERE `id`= '" . $swGroupId . "'";
  407. nr_query($query);
  408. log_register('CHANGE switch group [' . $swGroupId . '] `' . $swGroupName);
  409. }
  410. /**
  411. * Deletes switch group
  412. *
  413. * @param $swGroupId
  414. * @param string $swGroupName
  415. * @param string $smsServiceAlphaName
  416. */
  417. public function deleteSwitchGroup($swGroupId, $swGroupName = '') {
  418. $query = "DELETE FROM `switch_groups` WHERE `id` = '" . $swGroupId . "';";
  419. nr_query($query);
  420. log_register('DELETE switch group [' . $swGroupId . '] ` ' . $swGroupName);
  421. }
  422. /**
  423. * Check if switch group is protected from deletion
  424. *
  425. * @param $swGroupId
  426. *
  427. * @return bool
  428. */
  429. public function checkSwitchGroupProtected($swGroupId) {
  430. $query = "SELECT `id` FROM `switch_groups_relations` WHERE `sw_group_id` = " . $swGroupId . ";";
  431. $result = simple_queryall($query);
  432. return (!empty($result));
  433. }
  434. /**
  435. * Returns true if switch group with such name already exists
  436. *
  437. * @param $swGroupName
  438. * @param int $excludeEditedGroupId
  439. *
  440. * @return string
  441. */
  442. public function checkSwitchGroupNameExists($swGroupName, $excludeEditedGroupId = 0) {
  443. $swGroupName = trim($swGroupName);
  444. if (empty($excludeEditedGroupId)) {
  445. $query = "SELECT `id` FROM `switch_groups` WHERE `groupname` = '" . $swGroupName . "';";
  446. } else {
  447. $query = "SELECT `id` FROM `switch_groups` WHERE `groupname` = '" . $swGroupName . "' AND `id` != '" . $excludeEditedGroupId . "';";
  448. }
  449. $result = simple_queryall($query);
  450. return ( empty($result) ) ? '' : $result[0]['id'];
  451. }
  452. public function removeSwitchFromGroup($switchId) {
  453. $query = "DELETE from `switch_groups_relations` WHERE `switch_id`='" . $switchId . "'";
  454. nr_query($query);
  455. }
  456. }
  457. ?>