api.switchportreport.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. /**
  3. * Users to switch port binding report
  4. */
  5. class SwitchPortReport {
  6. /**
  7. * Contains full array data for port switch assign as key=>value
  8. *
  9. * @var array
  10. */
  11. protected $allPortSwitchData = array();
  12. /**
  13. * Contains array with login to port switch assign as key=>value
  14. *
  15. * @var array [login]=> 'data'
  16. */
  17. protected $data = array();
  18. /**
  19. * Contains available user address data as login=>address
  20. *
  21. * @var array
  22. */
  23. protected $allAddress = array();
  24. /**
  25. * Contains available user FIO
  26. *
  27. * @var array
  28. */
  29. protected $allrealnames = array();
  30. protected $allusers = array();
  31. protected $diff = array();
  32. /**
  33. * Contains system alter config as key=>value
  34. *
  35. * @var array
  36. */
  37. protected $altCfg = array();
  38. const USERS_TABLE = 'users';
  39. const PONONU_TABLE = 'pononu';
  40. const SWITCHPORTASSIGN_TABLE = 'switchportassign';
  41. const URL_ME_SWA = '?module=report_switchportassign';
  42. const URL_ME_NOSWA = '?module=report_switchportassign';
  43. /**
  44. * Contains user navigation URL
  45. */
  46. const URL_USERPROFILE = '?module=userprofile&username=';
  47. /**
  48. * SwitchId
  49. *
  50. * @var int
  51. */
  52. protected $switchID = '';
  53. public function __construct() {
  54. global $ubillingConfig;
  55. $this->altCfg = $ubillingConfig->getAlter();
  56. //set switchId
  57. $this->setSwitchId();
  58. //load actual data by switch port assing
  59. $this->loadData();
  60. //loads full user list
  61. $this->loadAllUsers();
  62. }
  63. /**
  64. * get all users with switch port assing and push it into data prop
  65. *
  66. * @return void
  67. */
  68. protected function setSwitchId() {
  69. if (ubRouting::checkGet('switchid')) {
  70. $switchId = ubRouting::get('switchid');
  71. $this->switchID = ubRouting::filters($switchId, 'int');
  72. }
  73. }
  74. /**
  75. * get all users with switch port assing and push it into data prop
  76. *
  77. * @return void
  78. */
  79. protected function loadData() {
  80. $switchPortAssign = new NyanORM(self::SWITCHPORTASSIGN_TABLE);
  81. $switchPortAssign->selectable('`switchportassign`.`id`,`port`,`login`,`ip`,`location`,`switchid`,`sw`.`id` swid');
  82. $switchPortAssign->joinOn('LEFT', '(SELECT * FROM `switches`) as sw', '`switchportassign`.`switchid` = `sw`.`id`', true);
  83. if (!empty($this->switchID)) {
  84. $switchPortAssign->where('switchid', '=', $this->switchID);
  85. }
  86. $this->allPortSwitchData = $switchPortAssign->getAll();
  87. if (!empty($this->allPortSwitchData)) {
  88. foreach ($this->allPortSwitchData as $io => $rawData) {
  89. $this->data[$rawData['login']] = $rawData;
  90. }
  91. }
  92. }
  93. /**
  94. * get all users logins and push it into allusers prop
  95. *
  96. * @return void
  97. */
  98. protected function loadAllUsers() {
  99. $users = new NyanORM(self::USERS_TABLE);
  100. $users->selectable('login');
  101. if (isset($this->altCfg['SWITCHPORT_REPORT_IGNORE_PON'])) {
  102. if ($this->altCfg['SWITCHPORT_REPORT_IGNORE_PON']) {
  103. $users->join('LEFT', self::PONONU_TABLE, 'login');
  104. $users->where('pononu.login', 'IS', 'NULL');
  105. }
  106. }
  107. $allDataUsers = $users->getAll();
  108. if (!empty($allDataUsers)) {
  109. foreach ($allDataUsers as $io => $each) {
  110. $this->allusers[$each['login']] = $each['login'];
  111. }
  112. }
  113. }
  114. /**
  115. * Loads users data
  116. *
  117. * @return void
  118. */
  119. public function loadUsersData() {
  120. $this->loadAllRealnames();
  121. $this->loadAddressData();
  122. }
  123. /**
  124. * Loads address data required for user telepathy into protected property
  125. *
  126. * @return void
  127. */
  128. protected function loadAllRealnames() {
  129. $this->allrealnames = zb_UserGetAllRealnames();
  130. }
  131. /**
  132. * Loads address data required for user telepathy into protected property
  133. *
  134. * @return void
  135. */
  136. protected function loadAddressData() {
  137. $this->allAddress = zb_AddressGetFulladdresslistCached();
  138. }
  139. /**
  140. * returns protected propert data
  141. *
  142. * @return array
  143. */
  144. public function getData() {
  145. $result = $this->data;
  146. return ($result);
  147. }
  148. /**
  149. * renders report by existing protected data prop
  150. *
  151. * @return string
  152. */
  153. public function renderNoSwitchPort() {
  154. if (!empty($this->allusers)) {
  155. foreach ($this->allusers as $io => $each) {
  156. if (!isset($this->data[$each]) OR empty($this->data[$each]['ip'])) {
  157. $this->diff[$each] = $each;
  158. }
  159. }
  160. }
  161. $result = web_UserArrayShower($this->diff);
  162. return ($result);
  163. }
  164. /**
  165. * renders report by existing protected data prop
  166. *
  167. * @return string
  168. */
  169. public function renderSwitchPortAssign() {
  170. if (empty($this->switchID)) {
  171. $result = '';
  172. $dopUrl = '';
  173. } else {
  174. $result = show_window('', wf_BackLink("?module=switches&edit=" . $this->switchID));
  175. $dopUrl = '&switchid=' . $this->switchID;
  176. }
  177. $columns = array('ID', 'IP', 'Port', 'Location', 'Switch', 'User', 'Full address');
  178. $opts = '"order": [[ 0, "desc" ]], "dom": \'<"F"lfB>rti<"F"ps>\', buttons: [\'csv\', \'excel\', \'pdf\']';
  179. $result .= wf_JqDtLoader($columns, self::URL_ME_SWA . '&ajaxswitchassign=true' . $dopUrl, false, 'Switch port assign', 100, $opts);
  180. return ($result);
  181. }
  182. /**
  183. * Renders json formatted data about switch ports assign
  184. *
  185. * @return void
  186. */
  187. public function ajaxAvaibleSwitchPortAssign() {
  188. $json = new wf_JqDtHelper();
  189. if (!empty($this->allPortSwitchData)) {
  190. foreach ($this->allPortSwitchData as $io => $raw) {
  191. $data[] = $raw['id'];
  192. $data[] = $raw['ip'];
  193. $data[] = $raw['port'];
  194. $data[] = $raw['location'];
  195. $data[] = $raw['ip'] . ' - ' . $raw['location'] . ' ' . __('Port') . ' ' . $raw['port'];
  196. $data[] = wf_Link(self::URL_USERPROFILE . $raw['login'], web_profile_icon() . ' ' . @$this->allrealnames[$raw['login']]) . '(' . $raw['login'] . ')';
  197. $data[] = @$this->allAddress[$raw['login']];
  198. $json->addRow($data);
  199. unset($data);
  200. }
  201. }
  202. $json->getJson();
  203. }
  204. }