api.condet.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. <?php
  2. /**
  3. * Connection (signup) details base class
  4. */
  5. class ConnectionDetails {
  6. /**
  7. * Contains all signup details data
  8. *
  9. * @var array
  10. */
  11. protected $allDetails = array();
  12. /**
  13. * Connection/signup details database abstraction layer
  14. *
  15. * @var object
  16. */
  17. protected $condetDb = '';
  18. const TABLE_CONDER = 'condet';
  19. const URL_ME = '?module=condetedit&username=';
  20. /**
  21. * Creates new condet instance
  22. *
  23. * @return void
  24. */
  25. public function __construct() {
  26. $this->initDb();
  27. $this->loadAllData();
  28. }
  29. /**
  30. * Inits database abstraction layer for further usage
  31. *
  32. * @return void
  33. */
  34. protected function initDb() {
  35. $this->condetDb = new NyanORM(self::TABLE_CONDER);
  36. }
  37. /**
  38. * Loads all connection details from database and
  39. * stores into private prop as login=>dataarray
  40. *
  41. * @return void
  42. */
  43. protected function loadAllData() {
  44. $this->allDetails = $this->condetDb->getAll('login');
  45. }
  46. /**
  47. * Returns array of connection details by user login
  48. *
  49. * @param string $login
  50. * @return array
  51. */
  52. public function getByLogin($login) {
  53. if (isset($this->allDetails[$login])) {
  54. $result = $this->allDetails[$login];
  55. } else {
  56. $result = array();
  57. }
  58. return ($result);
  59. }
  60. /**
  61. * Creates new DB entry for some login
  62. *
  63. * @param string $login
  64. * @param string $seal
  65. * @param int $length
  66. * @param string $price
  67. * @param int $term
  68. *
  69. * @return void
  70. */
  71. protected function create($login, $seal, $length, $price, $term = 0) {
  72. $login = ubRouting::filters($login, 'mres');
  73. $seal = ubRouting::filters($seal, 'mres');
  74. $length = ubRouting::filters($length, 'int');
  75. $price = ubRouting::filters($price, 'mres');
  76. $term = ubRouting::filters($term, 'int');
  77. $this->condetDb->data('login', $login);
  78. $this->condetDb->data('seal', $seal);
  79. $this->condetDb->data('length', $length);
  80. $this->condetDb->data('price', $price);
  81. $this->condetDb->data('term', $term);
  82. $this->condetDb->create();
  83. }
  84. /**
  85. * Deletes signup details from database
  86. *
  87. * @param string $login
  88. *
  89. * @return void
  90. */
  91. public function delete($login) {
  92. $login = ubRouting::filters($login, 'mres');
  93. $this->condetDb->where('login', '=', $login);
  94. $this->condetDb->delete();
  95. }
  96. /**
  97. * Updates existing DB entry for some login
  98. *
  99. * @param string $login
  100. * @param string $seal
  101. * @param int $length
  102. * @param string $price
  103. * @param int $term
  104. *
  105. * @return void
  106. */
  107. protected function update($login, $seal, $length, $price, $term = 0) {
  108. $login = ubRouting::filters($login, 'mres');
  109. $length = ubRouting::filters($length, 'int');
  110. $this->condetDb->data('seal', $seal);
  111. $this->condetDb->data('length', $length);
  112. $this->condetDb->data('price', $price);
  113. $this->condetDb->data('term', $term);
  114. $this->condetDb->where('login', '=', $login);
  115. $this->condetDb->save();
  116. }
  117. /**
  118. * Sets login connection data into database in needed way
  119. *
  120. * @param string $login
  121. * @param string $seal
  122. * @param string $length
  123. * @param string $price
  124. * @param int $term
  125. *
  126. * @return void
  127. */
  128. public function set($login, $seal, $length, $price, $term = 0) {
  129. if (!zb_checkMoney($price)) {
  130. $price = 0;
  131. }
  132. if (isset($this->allDetails[$login])) {
  133. $this->update($login, $seal, $length, $price, $term);
  134. } else {
  135. $this->create($login, $seal, $length, $price, $term);
  136. }
  137. log_register('CONDET SET (' . $login . ') SEAL `' . $seal . '` LENGTH `' . $length . '` PRICE `' . $price . '`');
  138. }
  139. /**
  140. * Retuns connection details edit form
  141. *
  142. * @param string $login
  143. *
  144. * @return string
  145. */
  146. public function editForm($login) {
  147. $login = ubRouting::filters($login, 'mres');
  148. $currentData = $this->getByLogin($login);
  149. $inputs = wf_TextInput('newseal', __('Cable seal'), @$currentData['seal'], true, '40');
  150. $inputs .= wf_TextInput('newlength', __('Cable length') . ', ' . __('m'), @$currentData['length'], true, 5, 'digits');
  151. $inputs .= wf_TextInput('newprice', __('Signup price'), @$currentData['price'], true, 5, 'finance');
  152. $inputs .= wf_TextInput('newterm', __('Signup term'), @$currentData['term'], true, 5, 'digits');
  153. $inputs .= wf_HiddenInput('editcondet', 'true');
  154. $inputs .= wf_delimiter(0);
  155. $inputs .= wf_Submit(__('Save'));
  156. $result = wf_Form("", 'POST', $inputs, 'glamour');
  157. return ($result);
  158. }
  159. /**
  160. * Renders connection details data for profile and edit form
  161. *
  162. * @param string $login
  163. *
  164. * @return string
  165. */
  166. public function renderData($login) {
  167. $currentData = $this->getByLogin($login);
  168. $result = '';
  169. if (!empty($currentData)) {
  170. if (!empty($currentData['seal'])) {
  171. $result .= __('Seal') . ': ' . $currentData['seal'] . ' ';
  172. }
  173. if (!empty($currentData['price'])) {
  174. $result .= __('Cost') . ': ' . $currentData['price'] . ' ';
  175. }
  176. if (!empty($currentData['length'])) {
  177. $result .= __('Cable') . ': ' . $currentData['length'] . __('m');
  178. }
  179. }
  180. return ($result);
  181. }
  182. /*
  183. Now it's too late, too late to live
  184. and my conscience killing me
  185. so am I alive
  186. but I'm not free
  187. and for all of you that can relate to this too
  188. and for all of you that can relate to this too
  189. */
  190. /**
  191. * Returns array of all existing cable seals as login=>seal
  192. *
  193. * @return array
  194. */
  195. public function getAllSeals() {
  196. $result = array();
  197. if (!empty($this->allDetails)) {
  198. foreach ($this->allDetails as $io => $each) {
  199. if (!empty($each['seal'])) {
  200. $result[$each['login']] = $each['seal'];
  201. }
  202. }
  203. }
  204. return ($result);
  205. }
  206. /**
  207. * Returns array of all existing details data as login=>condetData[seal,length,price,term]
  208. *
  209. * @return array
  210. */
  211. public function getAllData() {
  212. return ($this->allDetails);
  213. }
  214. /**
  215. * Returns display container of available connection details
  216. *
  217. * @return string
  218. */
  219. public function renderReportBody() {
  220. $columns = array('ID', 'Address', 'Real Name', 'IP', 'Tariff', 'Active', 'Cash', 'Credit', 'Seal', 'Cost', 'Cable');
  221. $result = wf_JqDtLoader($columns, '?module=report_condet&ajax=true', true, 'users');
  222. return ($result);
  223. }
  224. /**
  225. * Returns display container of available connection details
  226. *
  227. * @return string
  228. */
  229. public function renderReportBodyUkv() {
  230. $columns = array('ID', 'Address', 'Real Name', 'Tariff', 'Connected', 'Cash', 'Seal');
  231. $result = wf_JqDtLoader($columns, '?module=report_condet&ajaxukv=true', true, 'users');
  232. return ($result);
  233. }
  234. /**
  235. * Returns JSON reply for jquery datatables with full list of available connection details
  236. *
  237. * @return void
  238. */
  239. public function ajaxGetData() {
  240. $all = $this->condetDb->getAll();
  241. $allUserData = zb_UserGetAllDataCache();
  242. $rowData = array();
  243. $jsonData = new wf_JqDtHelper();
  244. if (!empty($all)) {
  245. foreach ($all as $io => $each) {
  246. if (isset($allUserData[$each['login']])) {
  247. $userData = $allUserData[$each['login']];
  248. $profileLink = wf_Link(UserProfile::URL_PROFILE . $each['login'], web_profile_icon() . ' ', false);
  249. $cash = $userData['Cash'];
  250. $credit = $userData['Credit'];
  251. $act = wf_img('skins/icon_active.gif') . __('Yes');
  252. //finance check
  253. if ($cash < '-' . $credit) {
  254. $act = wf_img('skins/icon_inactive.gif') . __('No');
  255. }
  256. $rowData[] = $each['id'];
  257. $rowData[] = $profileLink . $userData['fulladress'];
  258. $rowData[] = $userData['realname'];
  259. $rowData[] = $userData['ip'];
  260. $rowData[] = $userData['Tariff'];
  261. $rowData[] = $act;
  262. $rowData[] = $cash;
  263. $rowData[] = $credit;
  264. $rowData[] = $each['seal'];
  265. $rowData[] = $each['price'];
  266. $rowData[] = $each['length'];
  267. $jsonData->addRow($rowData);
  268. unset($rowData);
  269. }
  270. }
  271. }
  272. $jsonData->getJson();
  273. }
  274. /**
  275. * Returns JSON reply for jquery datatables with full list of available connection details
  276. *
  277. * @return void
  278. */
  279. public function ajaxGetDataUkv() {
  280. $ukv = new UkvSystem();
  281. $jsonData = new wf_JqDtHelper();
  282. $query = "SELECT * from `ukv_users` WHERE `cableseal` != '' ;";
  283. $all = simple_queryall($query);
  284. $rowData = array();
  285. if (!empty($all)) {
  286. foreach ($all as $io => $each) {
  287. $profileLink = wf_Link('?module=ukv&users=true&showuser=' . $each['id'], web_profile_icon() . ' ', false);
  288. $userAddress = @$ukv->userGetFullAddress($each['id']);
  289. $userRealname = $each['realname'];
  290. $act = wf_img('skins/icon_active.gif') . __('Yes');
  291. //finance check
  292. if (!$each['active']) {
  293. $act = wf_img('skins/icon_inactive.gif') . __('No');
  294. }
  295. $rowData[] = $each['id'];
  296. $rowData[] = $profileLink . $userAddress;
  297. $rowData[] = $userRealname;
  298. $rowData[] = $ukv->tariffGetName($each['tariffid']);
  299. $rowData[] = $act;
  300. $rowData[] = $each['cash'];
  301. $rowData[] = $each['cableseal'];
  302. $jsonData->addRow($rowData);
  303. unset($rowData);
  304. }
  305. }
  306. $jsonData->getJson();
  307. }
  308. }