api.dealwithit.php 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618
  1. <?php
  2. /**
  3. * Per-user task scheduler
  4. */
  5. class DealWithIt {
  6. /**
  7. * Contains system alter config as key=>value
  8. *
  9. * @var array
  10. */
  11. protected $altCfg = array();
  12. /**
  13. * Contains available tasks as id=>taskdata
  14. *
  15. * @var array
  16. */
  17. protected $allTasks = array();
  18. /**
  19. * Contains available actions array as action=>name
  20. *
  21. * @var array
  22. */
  23. protected $actionNames = array();
  24. /**
  25. * Contains available actions icons as action=>icon URL
  26. *
  27. * @var array
  28. */
  29. protected $actionIcons = array();
  30. /**
  31. * Contains available actions array as callback url=>name
  32. *
  33. * @var array
  34. */
  35. protected $actions = array();
  36. /**
  37. * Contains admns Name as admin_login => admin_name
  38. *
  39. * @var array
  40. */
  41. protected $adminsName = array();
  42. /**
  43. * Discounts enable flag
  44. *
  45. * @var bool
  46. */
  47. protected $discountsFlag = false;
  48. /**
  49. * Discounts instance placeholder
  50. *
  51. * @var object
  52. */
  53. protected $discounts = '';
  54. /**
  55. * Contains current user login
  56. *
  57. * @var string
  58. */
  59. protected $userLogin = '';
  60. /**
  61. * Base module URL
  62. */
  63. const URL_ME = '?module=pl_dealwithit';
  64. public function __construct() {
  65. $this->loadAlter();
  66. $this->initDiscounts();
  67. $this->setActionNames();
  68. $this->setActionIcons();
  69. $this->setActionsURL();
  70. $this->setLogin();
  71. $this->loadTasks();
  72. $this->loadAdminsName();
  73. }
  74. /**
  75. * Sets user login to filter
  76. *
  77. * @param string $login
  78. *
  79. * @return void
  80. */
  81. protected function setLogin() {
  82. if (ubRouting::checkGet('username')) {
  83. $this->userLogin = ubRouting::get('username', 'mres');
  84. }
  85. }
  86. /**
  87. * Loads system alter.ini config for further usage
  88. *
  89. * @global object $ubillingConfig
  90. *
  91. * @return void
  92. */
  93. protected function loadAlter() {
  94. global $ubillingConfig;
  95. $this->altCfg = $ubillingConfig->getAlter();
  96. }
  97. /**
  98. * Inits discounts instance if enabled
  99. *
  100. * @return void
  101. */
  102. protected function initDiscounts() {
  103. if (@$this->altCfg[Discounts::OPTION_ENABLE]) {
  104. $this->discountsFlag = true;
  105. $this->discounts = new Discounts();
  106. }
  107. }
  108. /**
  109. * Loads existing tasks for further usage
  110. *
  111. * @return void
  112. */
  113. protected function loadTasks() {
  114. $query = "SELECT * from `dealwithit`";
  115. $query.= ($this->userLogin) ? ' WHERE `login` = "' . $this->userLogin . '"' : '';
  116. $all = simple_queryall($query);
  117. if (!empty($all)) {
  118. foreach ($all as $io => $each) {
  119. $this->allTasks[$each['id']] = $each;
  120. }
  121. }
  122. }
  123. /**
  124. * Fills available actions array with names
  125. *
  126. * @rerutn void
  127. */
  128. protected function setActionNames() {
  129. $this->actionNames = array(
  130. 'noaction' => '-',
  131. 'addcash' => __('Add cash'),
  132. 'corrcash' => __('Correct saldo'),
  133. 'setcash' => __('Set cash'),
  134. 'credit' => __('Change') . ' ' . __('credit'),
  135. 'creditexpire' => __('Change') . ' ' . __('credit expire date'),
  136. 'tariffchange' => __('Change') . ' ' . __('tariff'),
  137. 'tagadd' => __('Add tag'),
  138. 'tagdel' => __('Delete tag'),
  139. 'freeze' => __('Freeze user'),
  140. 'unfreeze' => __('Unfreeze user'),
  141. 'reset' => __('User reset'),
  142. 'setspeed' => __('Change speed override'),
  143. 'down' => __('Set user down'),
  144. 'undown' => __('Enable user'),
  145. 'ao' => __('Enable AlwaysOnline'),
  146. 'unao' => __('Disable AlwaysOnline')
  147. );
  148. if ($this->discountsFlag) {
  149. $this->actionNames['setdiscount'] = __('Change discount');
  150. }
  151. }
  152. /**
  153. * Returns all available dealwithit tasks
  154. *
  155. * @return array
  156. */
  157. public function getAvailableTasks() {
  158. return ($this->allTasks);
  159. }
  160. /**
  161. * Sets available actions array with icons
  162. *
  163. * @return void
  164. */
  165. protected function setActionIcons() {
  166. $this->actionIcons = array(
  167. 'addcash' => 'skins/icon_dollar.gif',
  168. 'corrcash' => 'skins/icon_dollar.gif',
  169. 'setcash' => 'skins/icon_dollar.gif',
  170. 'credit' => 'skins/icon_credit.gif',
  171. 'creditexpire' => 'skins/icon_calendar.gif',
  172. 'tariffchange' => 'skins/icon_tariff.gif',
  173. 'tagadd' => 'skins/tagiconsmall.png',
  174. 'tagdel' => 'skins/tagiconsmall.png',
  175. 'freeze' => 'skins/icon_passive.gif',
  176. 'unfreeze' => 'skins/icon_passive.gif',
  177. 'reset' => 'skins/refresh.gif',
  178. 'setspeed' => 'skins/icon_speed.gif',
  179. 'down' => 'skins/icon_down.gif',
  180. 'undown' => 'skins/icon_down.gif',
  181. 'ao' => 'skins/icon_online.gif',
  182. 'unao' => 'skins/icon_online.gif'
  183. );
  184. if ($this->discountsFlag) {
  185. $this->actionIcons['setdiscount'] = 'skins/icon_discount_16.png';
  186. }
  187. }
  188. /**
  189. * Fills available actions array with selector URL-s
  190. *
  191. * @rerutn void
  192. */
  193. protected function setActionsURL() {
  194. $this->actions = array(
  195. self::URL_ME . '&ajinput=noaction' => $this->actionNames['noaction'],
  196. self::URL_ME . '&ajinput=addcash' => $this->actionNames['addcash'],
  197. self::URL_ME . '&ajinput=corrcash' => $this->actionNames['corrcash'],
  198. self::URL_ME . '&ajinput=setcash' => $this->actionNames['setcash'],
  199. self::URL_ME . '&ajinput=credit' => $this->actionNames['credit'],
  200. self::URL_ME . '&ajinput=creditexpire' => $this->actionNames['creditexpire'],
  201. self::URL_ME . '&ajinput=tariffchange' => $this->actionNames['tariffchange'],
  202. self::URL_ME . '&ajinput=tagadd' => $this->actionNames['tagadd'],
  203. self::URL_ME . '&ajinput=tagdel' => $this->actionNames['tagdel'],
  204. self::URL_ME . '&ajinput=freeze' => $this->actionNames['freeze'],
  205. self::URL_ME . '&ajinput=unfreeze' => $this->actionNames['unfreeze'],
  206. self::URL_ME . '&ajinput=reset' => $this->actionNames['reset'],
  207. self::URL_ME . '&ajinput=setspeed' => $this->actionNames['setspeed'],
  208. self::URL_ME . '&ajinput=down' => $this->actionNames['down'],
  209. self::URL_ME . '&ajinput=undown' => $this->actionNames['undown'],
  210. self::URL_ME . '&ajinput=ao' => $this->actionNames['ao'],
  211. self::URL_ME . '&ajinput=unao' => $this->actionNames['unao']
  212. );
  213. if ($this->discountsFlag) {
  214. $this->actions[self::URL_ME . '&ajinput=setdiscount'] = $this->actionNames['setdiscount'];
  215. }
  216. }
  217. /**
  218. * Loads admis Name
  219. *
  220. * @return void
  221. */
  222. protected function loadAdminsName() {
  223. @$employeeLogins = unserialize(ts_GetAllEmployeeLoginsCached());
  224. if (!empty($employeeLogins)) {
  225. foreach ($employeeLogins as $login => $name) {
  226. $this->adminsName[$login] = $name;
  227. }
  228. }
  229. }
  230. /**
  231. * Init admin Name
  232. *
  233. * @param string $admin
  234. * @return void
  235. */
  236. protected function initAdminName($admin) {
  237. $result = '';
  238. if (!empty($admin)) {
  239. $result = (isset($this->adminsName[$admin])) ? $this->adminsName[$admin] : $admin;
  240. }
  241. return ($result);
  242. }
  243. /**
  244. * Logs tasks creation/execution to database
  245. *
  246. * @param string $id
  247. * @param string $date
  248. * @param string $login
  249. * @param string $action
  250. * @param string $param
  251. * @param string $note
  252. * @param bool $done
  253. *
  254. * @return void
  255. */
  256. protected function logTask($id, $date, $login, $action, $param, $note, $done) {
  257. $id = vf($id, 3);
  258. $admin = whoami();
  259. $mtime = curdatetime();
  260. if ($done) {
  261. $query = "UPDATE `dealwithithist` SET `done` = '1', `datetimedone` = '" . $mtime . "' WHERE `dealwithithist`.`originalid` = '" . $id . "'";
  262. } else {
  263. $query = "INSERT INTO `dealwithithist` (`id`,`originalid`,`mtime`,`date`,`login`,`action`,`param`,`note`,`admin`,`done`) VALUES";
  264. $query .= "(NULL,'" . $id . "','" . $mtime . "','" . $date . "', '" . $login . "','" . $action . "','" . $param . "','" . $note . "','" . $admin . "','0')";
  265. }
  266. nr_query($query);
  267. }
  268. /**
  269. * Creates scheduler task in database
  270. *
  271. * @param string $date
  272. * @param string $login
  273. * @param string $action
  274. * @param string $param
  275. * @param string $note
  276. *
  277. * @return int
  278. */
  279. public function createTask($date, $login, $action, $param, $note) {
  280. $dateF = mysql_real_escape_string($date);
  281. $loginF = mysql_real_escape_string($login);
  282. $actionF = mysql_real_escape_string($action);
  283. $paramF = mysql_real_escape_string($param);
  284. $noteF = mysql_real_escape_string($note);
  285. $query = "INSERT INTO `dealwithit` (`id`,`date`,`login`,`action`,`param`,`note`) VALUES";
  286. $query .= "(NULL,'" . $dateF . "','" . $loginF . "','" . $actionF . "','" . $paramF . "','" . $noteF . "');";
  287. nr_query($query);
  288. $newId = simple_get_lastid('dealwithit');
  289. $this->logTask($newId, $dateF, $loginF, $actionF, $paramF, $noteF, false);
  290. log_register('SCHEDULER CREATE ID [' . $newId . '] (' . $login . ') DATE `' . $date . ' `ACTION `' . $action . '` NOTE `' . $note . '`');
  291. return ($newId);
  292. }
  293. /**
  294. * Deletes existing task from database
  295. *
  296. * @param int $taskId
  297. *
  298. * @return void
  299. */
  300. public function deleteTask($taskId) {
  301. $taskId = vf($taskId, 3);
  302. if (isset($this->allTasks[$taskId])) {
  303. $taskData = $this->allTasks[$taskId];
  304. $query = "DELETE from `dealwithit` WHERE `id`='" . $taskId . "'";
  305. nr_query($query);
  306. log_register('SCHEDULER DELETE ID [' . $taskId . '] (' . $taskData['login'] . ') DATE `' . $taskData['date'] . ' `ACTION `' . $taskData['action'] . '`');
  307. }
  308. }
  309. /**
  310. * Renders task creation form
  311. *
  312. * @return string
  313. */
  314. public function renderCreateForm() {
  315. $result = '';
  316. $result .= wf_AjaxLoader();
  317. $inputs = wf_HiddenInput('newschedlogin', $this->userLogin);
  318. $inputs .= wf_DatePickerPreset('newscheddate', date('Y-m-d', strtotime('+1 day')), true) . ' ' . __('Target date') . wf_tag('br');
  319. $inputs .= wf_AjaxSelectorAC('ajparamcontainer', $this->actions, __('Task'), '', true);
  320. $inputs .= wf_AjaxContainer('ajparamcontainer');
  321. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  322. return ($result);
  323. }
  324. /**
  325. * Returns ajax inputs of required type
  326. *
  327. * @return string
  328. */
  329. public function catchAjRequest() {
  330. $result = '';
  331. if (wf_CheckGet(array('ajinput'))) {
  332. $request = vf($_GET['ajinput']);
  333. switch ($request) {
  334. case 'addcash':
  335. $result .= wf_HiddenInput('newschedaction', 'addcash');
  336. $result .= wf_TextInput('newschedparam', __('Sum'), '', true, 5);
  337. $allCashTypes = zb_CashGetTypesNamed();
  338. $result .= wf_Selector('newschedcashtype', $allCashTypes, __('Cash type'), '', true);
  339. break;
  340. case 'corrcash':
  341. $result .= wf_HiddenInput('newschedaction', 'corrcash');
  342. $result .= wf_TextInput('newschedparam', __('Sum'), '', true, 5);
  343. break;
  344. case 'setcash':
  345. $result .= wf_HiddenInput('newschedaction', 'setcash');
  346. $result .= wf_TextInput('newschedparam', __('Sum'), '', true, 5);
  347. break;
  348. case 'credit':
  349. $result .= wf_HiddenInput('newschedaction', 'credit');
  350. $result .= wf_TextInput('newschedparam', __('New credit'), '', true, 5);
  351. break;
  352. case 'creditexpire':
  353. $result .= wf_HiddenInput('newschedaction', 'creditexpire');
  354. $result .= wf_DatePickerPreset('newschedparam', curdate()) . ' ' . __('New credit expire') . wf_tag('br');
  355. break;
  356. case 'tariffchange':
  357. $result .= wf_HiddenInput('newschedaction', 'tariffchange');
  358. $result .= web_tariffselector('newschedparam') . ' ' . __('Tariff name') . wf_tag('br');
  359. break;
  360. case 'tagadd':
  361. $result .= wf_HiddenInput('newschedaction', 'tagadd');
  362. $allTags = array();
  363. $allTagsRaw = simple_queryall("SELECT * from `tagtypes`");
  364. if (!empty($allTagsRaw)) {
  365. foreach ($allTagsRaw as $io => $each) {
  366. $allTags[$each['id']] = $each['tagname'];
  367. }
  368. }
  369. $result .= wf_Selector('newschedparam', $allTags, __('Tag'), '', true);
  370. break;
  371. case 'tagdel':
  372. $result .= wf_HiddenInput('newschedaction', 'tagdel');
  373. $allTags = array();
  374. $allTagsRaw = simple_queryall("SELECT * from `tagtypes`");
  375. if (!empty($allTagsRaw)) {
  376. foreach ($allTagsRaw as $io => $each) {
  377. $allTags[$each['id']] = $each['tagname'];
  378. }
  379. }
  380. $result .= wf_Selector('newschedparam', $allTags, __('Tag'), '', true);
  381. break;
  382. case 'freeze':
  383. $result .= wf_HiddenInput('newschedaction', 'freeze');
  384. $result .= wf_HiddenInput('newschedparam', '');
  385. break;
  386. case 'unfreeze':
  387. $result .= wf_HiddenInput('newschedaction', 'unfreeze');
  388. $result .= wf_HiddenInput('newschedparam', '');
  389. break;
  390. case 'reset':
  391. $result .= wf_HiddenInput('newschedaction', 'reset');
  392. $result .= wf_HiddenInput('newschedparam', '');
  393. break;
  394. case 'setspeed':
  395. $result .= wf_HiddenInput('newschedaction', 'setspeed');
  396. $result .= wf_TextInput('newschedparam', __('New speed override'), '', true, 5);
  397. break;
  398. case 'down':
  399. $result .= wf_HiddenInput('newschedaction', 'down');
  400. $result .= wf_HiddenInput('newschedparam', '');
  401. break;
  402. case 'undown':
  403. $result .= wf_HiddenInput('newschedaction', 'undown');
  404. $result .= wf_HiddenInput('newschedparam', '');
  405. break;
  406. case 'ao':
  407. $result .= wf_HiddenInput('newschedaction', 'ao');
  408. $result .= wf_HiddenInput('newschedparam', '');
  409. break;
  410. case 'unao':
  411. $result .= wf_HiddenInput('newschedaction', 'unao');
  412. $result .= wf_HiddenInput('newschedparam', '');
  413. break;
  414. case 'setdiscount':
  415. $result .= wf_HiddenInput('newschedaction', 'setdiscount');
  416. $result .= wf_TextInput('newschedparam', __('New discount'), '', true, 5, 'digits');
  417. break;
  418. }
  419. $result .= wf_TextInput('newschednote', __('Notes'), '', true, 30);
  420. $result .= wf_Submit(__('Create'));
  421. if ($request == 'noaction') {
  422. $result = __('Please select action');
  423. }
  424. }
  425. die($result);
  426. }
  427. /**
  428. * Creates new schedule task
  429. *
  430. * @return void/error notice
  431. */
  432. public function catchCreateRequest() {
  433. $result = '';
  434. if (wf_CheckPost(array('newschedlogin', 'newschedaction', 'newscheddate'))) {
  435. $date = $_POST['newscheddate'];
  436. $action = $_POST['newschedaction'];
  437. $param = $_POST['newschedparam'];
  438. $note = $_POST['newschednote'];
  439. $login = $_POST['newschedlogin'];
  440. if (zb_checkDate($date)) {
  441. switch ($action) {
  442. //this action types requires non empty parameter
  443. case 'addcash':
  444. if ($param) {
  445. if (zb_checkMoney($param)) {
  446. $cashType = (wf_CheckPost(array('newschedcashtype'))) ? vf($_POST['newschedcashtype']) : 1;
  447. if ($cashType != 1) {
  448. $param .= '|' . $cashType;
  449. }
  450. $this->createTask($date, $login, $action, $param, $note);
  451. } else {
  452. $result = __('Wrong format of a sum of money to pay');
  453. }
  454. } else {
  455. $result = __('No all of required fields is filled');
  456. }
  457. break;
  458. case 'corrcash':
  459. if ($param) {
  460. if (zb_checkMoney($param)) {
  461. $this->createTask($date, $login, $action, $param, $note);
  462. } else {
  463. $result = __('Wrong format of a sum of money to pay');
  464. }
  465. } else {
  466. $result = __('No all of required fields is filled');
  467. }
  468. break;
  469. case 'setcash':
  470. if ($param) {
  471. if (zb_checkMoney($param)) {
  472. $this->createTask($date, $login, $action, $param, $note);
  473. } else {
  474. $result = __('Wrong format of a sum of money to pay');
  475. }
  476. } else {
  477. $result = __('No all of required fields is filled');
  478. }
  479. break;
  480. case 'credit':
  481. if ($param >= 0) {
  482. if (zb_checkMoney($param)) {
  483. $this->createTask($date, $login, $action, $param, $note);
  484. } else {
  485. $result = __('Wrong format of a sum of money to pay');
  486. }
  487. } else {
  488. $result = __('No all of required fields is filled');
  489. }
  490. break;
  491. case 'creditexpire':
  492. if ($param) {
  493. if (zb_checkDate($param)) {
  494. $this->createTask($date, $login, $action, $param, $note);
  495. } else {
  496. $result = __('Wrong date format');
  497. }
  498. } else {
  499. $result = __('No all of required fields is filled');
  500. }
  501. break;
  502. case 'tariffchange':
  503. if ($param) {
  504. $this->createTask($date, $login, $action, $param, $note);
  505. } else {
  506. $result = __('No all of required fields is filled');
  507. }
  508. break;
  509. case 'tagadd':
  510. if ($param) {
  511. $this->createTask($date, $login, $action, $param, $note);
  512. } else {
  513. $result = __('No all of required fields is filled');
  514. }
  515. break;
  516. case 'tagdel':
  517. if ($param) {
  518. $this->createTask($date, $login, $action, $param, $note);
  519. } else {
  520. $result = __('No all of required fields is filled');
  521. }
  522. break;
  523. //for this task types parameter may be empty
  524. case 'freeze':
  525. $this->createTask($date, $login, $action, $param, $note);
  526. break;
  527. case 'unfreeze':
  528. $this->createTask($date, $login, $action, $param, $note);
  529. break;
  530. case 'reset':
  531. $this->createTask($date, $login, $action, $param, $note);
  532. break;
  533. case 'setspeed':
  534. $this->createTask($date, $login, $action, $param, $note);
  535. break;
  536. case 'down':
  537. $this->createTask($date, $login, $action, $param, $note);
  538. break;
  539. case 'undown':
  540. $this->createTask($date, $login, $action, $param, $note);
  541. break;
  542. case 'ao':
  543. $this->createTask($date, $login, $action, $param, $note);
  544. break;
  545. case 'unao':
  546. $this->createTask($date, $login, $action, $param, $note);
  547. break;
  548. case 'setdiscount':
  549. $this->createTask($date, $login, $action, $param, $note);
  550. break;
  551. }
  552. } else {
  553. $result = __('Wrong date format');
  554. }
  555. } else {
  556. $result = __('Something went wrong');
  557. }
  558. return ($result);
  559. }
  560. /**
  561. *
  562. * @return void/error notice
  563. */
  564. public function catchCreateMassRequest() {
  565. $result = '';
  566. if (wf_CheckPost(array('newschedloginsarr', 'newschedaction', 'newscheddate'))) {
  567. if (!empty($_POST['_logins'])) {
  568. $date = $_POST['newscheddate'];
  569. $action = $_POST['newschedaction'];
  570. $param = $_POST['newschedparam'];
  571. $note = $_POST['newschednote'];
  572. $logins = array_keys($_POST['_logins']);
  573. if (zb_checkDate($date)) {
  574. switch ($action) {
  575. //this action types requires non empty parameter
  576. case 'addcash':
  577. if ($param) {
  578. if (zb_checkMoney($param)) {
  579. $cashType = (wf_CheckPost(array('newschedcashtype'))) ? vf($_POST['newschedcashtype']) : 1;
  580. if ($cashType != 1) {
  581. $param .= '|' . $cashType;
  582. }
  583. foreach ($logins as $login) {
  584. $this->createTask($date, $login, $action, $param, $note);
  585. }
  586. } else {
  587. $result = __('Wrong format of a sum of money to pay');
  588. }
  589. } else {
  590. $result = __('No all of required fields is filled');
  591. }
  592. break;
  593. case 'corrcash':
  594. if ($param) {
  595. if (zb_checkMoney($param)) {
  596. foreach ($logins as $login) {
  597. $this->createTask($date, $login, $action, $param, $note);
  598. }
  599. } else {
  600. $result = __('Wrong format of a sum of money to pay');
  601. }
  602. } else {
  603. $result = __('No all of required fields is filled');
  604. }
  605. break;
  606. case 'setcash':
  607. if ($param) {
  608. if (zb_checkMoney($param)) {
  609. foreach ($logins as $login) {
  610. $this->createTask($date, $login, $action, $param, $note);
  611. }
  612. } else {
  613. $result = __('Wrong format of a sum of money to pay');
  614. }
  615. } else {
  616. $result = __('No all of required fields is filled');
  617. }
  618. break;
  619. case 'credit':
  620. if ($param >= 0) {
  621. if (zb_checkMoney($param)) {
  622. foreach ($logins as $login) {
  623. $this->createTask($date, $login, $action, $param, $note);
  624. }
  625. } else {
  626. $result = __('Wrong format of a sum of money to pay');
  627. }
  628. } else {
  629. $result = __('No all of required fields is filled');
  630. }
  631. break;
  632. case 'creditexpire':
  633. if ($param) {
  634. if (zb_checkDate($param)) {
  635. foreach ($logins as $login) {
  636. $this->createTask($date, $login, $action, $param, $note);
  637. }
  638. } else {
  639. $result = __('Wrong date format');
  640. }
  641. } else {
  642. $result = __('No all of required fields is filled');
  643. }
  644. break;
  645. case 'tariffchange':
  646. if ($param) {
  647. foreach ($logins as $login) {
  648. $this->createTask($date, $login, $action, $param, $note);
  649. }
  650. } else {
  651. $result = __('No all of required fields is filled');
  652. }
  653. break;
  654. case 'tagadd':
  655. if ($param) {
  656. foreach ($logins as $login) {
  657. $this->createTask($date, $login, $action, $param, $note);
  658. }
  659. } else {
  660. $result = __('No all of required fields is filled');
  661. }
  662. break;
  663. case 'tagdel':
  664. if ($param) {
  665. foreach ($logins as $login) {
  666. $this->createTask($date, $login, $action, $param, $note);
  667. }
  668. } else {
  669. $result = __('No all of required fields is filled');
  670. }
  671. break;
  672. //for this task types parameter may be empty
  673. case 'freeze':
  674. foreach ($logins as $login) {
  675. $this->createTask($date, $login, $action, $param, $note);
  676. }
  677. break;
  678. case 'unfreeze':
  679. foreach ($logins as $login) {
  680. $this->createTask($date, $login, $action, $param, $note);
  681. }
  682. break;
  683. case 'reset':
  684. foreach ($logins as $login) {
  685. $this->createTask($date, $login, $action, $param, $note);
  686. }
  687. break;
  688. case 'setspeed':
  689. foreach ($logins as $login) {
  690. $this->createTask($date, $login, $action, $param, $note);
  691. }
  692. break;
  693. case 'down':
  694. foreach ($logins as $login) {
  695. $this->createTask($date, $login, $action, $param, $note);
  696. }
  697. break;
  698. case 'undown':
  699. foreach ($logins as $login) {
  700. $this->createTask($date, $login, $action, $param, $note);
  701. }
  702. break;
  703. case 'ao':
  704. foreach ($logins as $login) {
  705. $this->createTask($date, $login, $action, $param, $note);
  706. }
  707. break;
  708. case 'unao':
  709. foreach ($logins as $login) {
  710. $this->createTask($date, $login, $action, $param, $note);
  711. }
  712. break;
  713. case 'setdiscount':
  714. foreach ($logins as $login) {
  715. $this->createTask($date, $login, $action, $param, $note);
  716. }
  717. break;
  718. }
  719. } else {
  720. $result = __('Wrong date format');
  721. }
  722. } else {
  723. $result = __('You did not select any user');
  724. }
  725. } else {
  726. $result = __('Something went wrong');
  727. }
  728. return ($result);
  729. }
  730. /**
  731. * Renders available tasks data list
  732. *
  733. * @param sring $login
  734. *
  735. * @return string
  736. */
  737. public function AjaxDataTasksList() {
  738. $messages = new UbillingMessageHelper();
  739. $tmpArr = array();
  740. $allRealNames = zb_UserGetAllRealnames();
  741. $allAddress = zb_AddressGetFulladdresslistCached();
  742. $json = new wf_JqDtHelper();
  743. if (!empty($this->allTasks)) {
  744. foreach ($this->allTasks as $io => $each) {
  745. if (empty($this->userLogin)) {
  746. $tmpArr[$io] = $each;
  747. } else {
  748. if ($this->userLogin == $each['login']) {
  749. $tmpArr[$io] = $each;
  750. }
  751. }
  752. }
  753. }
  754. if (!empty($tmpArr)) {
  755. $curDate = curdate();
  756. foreach ($tmpArr as $io => $each) {
  757. $actionIcon = (isset($this->actionIcons[$each['action']])) ? wf_img_sized($this->actionIcons[$each['action']], $this->actionNames[$each['action']], '12', '12') . ' ' : '';
  758. $profileLink = wf_Link('?module=userprofile&username=' . $each['login'], web_profile_icon() . ' ' . $each['login'], false, '');
  759. $taskControls = wf_JSAlert(self::URL_ME . '&username=' . $each['login'] . '&deletetaskid=' . $each['id'], web_delete_icon(), $messages->getDeleteAlert());
  760. if (ispos($each['param'], '|')) {
  761. $paramFiltered = strstr($each['param'], '|', true);
  762. } else {
  763. $paramFiltered = $each['param'];
  764. }
  765. $data[] = $each['id'];
  766. $data[] = $this->colorizeData($each['date'], $curDate, $each['date']);
  767. $data[] = $profileLink;
  768. $data[] = @$allAddress[$each['login']];
  769. $data[] = @$allRealNames[$each['login']];
  770. $data[] = $actionIcon . $this->colorizeData($each['date'], $curDate, $this->actionNames[$each['action']]);
  771. $data[] = $paramFiltered;
  772. $data[] = $each['note'];
  773. $data[] = $taskControls;
  774. $json->addRow($data);
  775. unset($data);
  776. }
  777. }
  778. $json->getJson();
  779. }
  780. /**
  781. * Returns colorized string field based on planning date
  782. *
  783. * @param string $date
  784. * @param string $curDate
  785. * @param string $string
  786. *
  787. * @return string
  788. */
  789. protected function colorizeData($date, $curDate, $string) {
  790. $result = '';
  791. if ($date > $curDate) {
  792. $result = wf_tag('font', false, '', '') . $string . wf_tag('font', true);
  793. }
  794. if ($date == $curDate) {
  795. $result = wf_tag('font', false, '', 'color="#d45f00"') . $string . wf_tag('font', true);
  796. }
  797. if ($date < $curDate) {
  798. $result = wf_tag('font', false, '', 'color="#b71e00"') . $string . wf_tag('font', true);
  799. }
  800. return ($result);
  801. }
  802. /**
  803. * Returns container of tasks list
  804. *
  805. * @return string
  806. */
  807. public function renderTasksListAjax() {
  808. $result = '';
  809. $columns = array('ID', 'Target date', 'Login', 'Address', 'Real name', 'Task', 'Parameter', 'Notes', 'Actions');
  810. $opts = '"order": [[ 0, "desc" ]]';
  811. $module_link = (empty($this->userLogin)) ? '?module=report_dealwithit&ajax=true' : '?module=pl_dealwithit&ajax=true&username=' . $this->userLogin;
  812. $result = wf_JqDtLoader($columns, $module_link, false, 'Tasks', 100, $opts);
  813. return ($result);
  814. }
  815. /**
  816. * Renders available tasks data
  817. *
  818. * @return string
  819. */
  820. public function AjaxDataTasksHistory() {
  821. $tmpArr = array();
  822. $allRealNames = zb_UserGetAllRealnames();
  823. $allAddress = zb_AddressGetFulladdresslistCached();
  824. $query = "SELECT * from `dealwithithist` ORDER by `id` DESC";
  825. $allTasksHistory = simple_queryall($query);
  826. $json = new wf_JqDtHelper();
  827. if (!empty($allTasksHistory)) {
  828. foreach ($allTasksHistory as $io => $each) {
  829. $tmpArr[$io] = $each;
  830. }
  831. }
  832. if (!empty($tmpArr)) {
  833. foreach ($tmpArr as $io => $each) {
  834. $actionIcon = (isset($this->actionIcons[$each['action']])) ? wf_img_sized($this->actionIcons[$each['action']], $this->actionNames[$each['action']], '12', '12') . ' ' : '';
  835. $profileLink = wf_Link('?module=userprofile&username=' . $each['login'], web_profile_icon() . ' ' . $each['login'], false, '');
  836. if (ispos($each['param'], '|')) {
  837. $paramFiltered = strstr($each['param'], '|', true);
  838. } else {
  839. $paramFiltered = $each['param'];
  840. }
  841. $data[] = $each['originalid'];
  842. $data[] = $each['date'];
  843. $data[] = $each['mtime'];
  844. $data[] = $each['datetimedone'];
  845. $data[] = $profileLink;
  846. $data[] = @$allAddress[$each['login']];
  847. $data[] = @$allRealNames[$each['login']];
  848. $data[] = $actionIcon . @$this->actionNames[$each['action']];
  849. $data[] = $paramFiltered;
  850. $data[] = $each['note'];
  851. $data[] = web_bool_led($each['done']);
  852. $data[] = $this->initAdminName($each['admin']);
  853. $json->addRow($data);
  854. unset($data);
  855. }
  856. }
  857. $json->getJson();
  858. }
  859. /**
  860. * Returns container of tasks
  861. *
  862. * @return string
  863. */
  864. public function renderTasksHistoryAjax() {
  865. $result = '';
  866. $columns = array('ID', 'Target date', 'Create date', 'Changed', 'Login', 'Address', 'Real name', 'Task', 'Parameter', 'Notes', 'Done', 'Admin');
  867. $opts = '"order": [[ 0, "desc" ]]';
  868. $result = wf_JqDtLoader($columns, '?module=report_dealwithit&history=true&ajax=true', false, 'Tasks', 100, $opts);
  869. return ($result);
  870. }
  871. /**
  872. * Sets task as done / deletes it from database
  873. *
  874. * @param int $taskId
  875. *
  876. * @return void
  877. */
  878. protected function setTaskIsDone($taskId) {
  879. $taskId = vf($taskId, 3);
  880. if (isset($this->allTasks[$taskId])) {
  881. $taskData = $this->allTasks[$taskId];
  882. $this->logTask($taskId, $taskData['date'], $taskData['login'], $taskData['action'], $taskData['param'], $taskData['note'], true);
  883. $query = "DELETE from `dealwithit` WHERE `id`='" . $taskId . "'";
  884. nr_query($query);
  885. log_register('SCHEDULER DONE ID [' . $taskId . '] (' . $taskData['login'] . ')');
  886. }
  887. }
  888. /**
  889. * Performs available tasks processing
  890. *
  891. * @return void
  892. */
  893. public function tasksProcessing() {
  894. global $billing;
  895. $curdate = curdate();
  896. $rawUsers = zb_UserGetAllStargazerData();
  897. $allUsers = array();
  898. if (!empty($rawUsers)) {
  899. foreach ($rawUsers as $io => $each) {
  900. $allUsers[$each['login']] = $each;
  901. }
  902. }
  903. if (!empty($this->allTasks)) {
  904. foreach ($this->allTasks as $io => $each) {
  905. if (strtotime($each['date']) <= strtotime($curdate)) {
  906. if (isset($allUsers[$each['login']])) {
  907. $login = $each['login'];
  908. $param = $each['param'];
  909. switch ($each['action']) {
  910. case 'addcash':
  911. if (ispos($param, '|')) {
  912. $dataRaw = explode('|', $param);
  913. $summ = $dataRaw[0];
  914. $cashType = $dataRaw[1];
  915. } else {
  916. $summ = $param;
  917. $cashType = 1;
  918. }
  919. zb_CashAdd($login, $summ, 'add', $cashType, 'SCHEDULED');
  920. break;
  921. case 'corrcash':
  922. $corrNote = 'SCHEDULED';
  923. if (ispos($each['note'], 'DEFSALE:')) {
  924. $corrNote = $each['note'];
  925. }
  926. zb_CashAdd($login, $param, 'correct', 1, $corrNote);
  927. break;
  928. case 'setcash':
  929. zb_CashAdd($login, $param, 'set', 1, 'SCHEDULED');
  930. break;
  931. case 'credit':
  932. $billing->setcredit($login, $param);
  933. log_register('CHANGE Credit (' . $login . ') ON ' . $param);
  934. break;
  935. case 'creditexpire':
  936. $billing->setcreditexpire($login, $param);
  937. log_register('CHANGE CreditExpire (' . $login . ') ON ' . $param);
  938. break;
  939. case 'tariffchange':
  940. $billing->settariff($login, $param);
  941. log_register('CHANGE Tariff (' . $login . ') ON `' . $param . '`');
  942. //optional user reset
  943. if ($this->altCfg['TARIFFCHGRESET']) {
  944. $billing->resetuser($login);
  945. log_register('RESET User (' . $login . ')');
  946. }
  947. break;
  948. case 'tagadd':
  949. stg_add_user_tag($login, $param);
  950. break;
  951. case 'tagdel':
  952. stg_del_user_tagid($login, $param);
  953. break;
  954. case 'freeze':
  955. $billing->setpassive($login, 1);
  956. log_register('CHANGE Passive (' . $login . ') ON 1');
  957. break;
  958. case 'unfreeze':
  959. $billing->setpassive($login, 0);
  960. log_register('CHANGE Passive (' . $login . ') ON 0');
  961. break;
  962. case 'reset':
  963. $billing->resetuser($login);
  964. log_register('RESET User (' . $login . ')');
  965. break;
  966. case 'setspeed':
  967. zb_UserDeleteSpeedOverride($login);
  968. zb_UserCreateSpeedOverride($login, $param);
  969. $billing->resetuser($login);
  970. log_register("RESET User (" . $login . ")");
  971. break;
  972. case 'down':
  973. $billing->setdown($login, 1);
  974. log_register('CHANGE Down (' . $login . ') ON 1');
  975. break;
  976. case 'undown':
  977. $billing->setdown($login, 0);
  978. log_register('CHANGE Down (' . $login . ') ON 0');
  979. break;
  980. case 'ao':
  981. $billing->setao($login, 1);
  982. log_register('CHANGE AlwaysOnline (' . $login . ') ON 1');
  983. break;
  984. case 'unao':
  985. $billing->setao($login, 0);
  986. log_register('CHANGE AlwaysOnline (' . $login . ') ON 0');
  987. break;
  988. case 'setdiscount':
  989. $this->discounts->setDiscount($login, $param);
  990. log_register('CHANGE Discount (' . $login . ') PERCENT `' . $param . '`');
  991. break;
  992. }
  993. //flush task from database
  994. $this->setTaskIsDone($each['id']);
  995. } else {
  996. log_register('SCHEDULER FAIL ID [' . $each['id'] . '] USER (' . $each['login'] . ') NON EXISTS');
  997. $this->deleteTask($each['id']);
  998. }
  999. }
  1000. }
  1001. }
  1002. }
  1003. /**
  1004. * Returns user profile fileds search form
  1005. *
  1006. * @return string
  1007. */
  1008. public function renderDealWithItControl() {
  1009. $messages = new UbillingMessageHelper();
  1010. $controls = wf_Link('?module=report_dealwithit', wf_img('skins/dealwithitsmall.png') . ' ' . __('Available Held jobs for all users'), false, 'ubButton');
  1011. $controls .= wf_Link('?module=report_dealwithit&history=true', wf_img('skins/icon_calendar.gif') . ' ' . __('History'), false, 'ubButton');
  1012. $result = show_window('', $controls);
  1013. $result .= show_window(__('User search'), $this->renderUsersSearchForm());
  1014. if (wf_CheckPost(array('dealwithit_search')) and isset($_POST['dealwithit_search']['search_by'])) {
  1015. $logins = $this->SearchUsers($_POST['dealwithit_search']);
  1016. if (!empty($logins)) {
  1017. show_window(__('Create new task'), $this->renderUsersSearchResults($logins));
  1018. } else {
  1019. show_window('', $messages->getStyledMessage(__('Query returned empty result'), 'info'));
  1020. }
  1021. } elseif (wf_CheckPost(array('dealwithit_search')) and ! isset($_POST['dealwithit_search']['search_by']) and isset($_POST['dealwithit_search']['exclude'])) {
  1022. show_error(__('The search parameter is not selected. No, what to exclude from the request'));
  1023. } elseif (wf_CheckPost(array('dealwithit_search')) and ! isset($_POST['dealwithit_search']['search_by']) and ! isset($_POST['dealwithit_search']['exclude'])) {
  1024. show_error(__('No request parameters set'));
  1025. }
  1026. return ($result);
  1027. }
  1028. /**
  1029. * Returns user profile fileds search form
  1030. *
  1031. * @return string
  1032. */
  1033. protected function renderUsersSearchResults($logins) {
  1034. $result = '';
  1035. if (!empty($logins)) {
  1036. $availableUsers = zb_UserGetAllStargazerDataAssoc();
  1037. $cells = wf_TableCell(__('ID'));
  1038. $cells .= wf_TableCell(__('Login'));
  1039. $cells .= wf_TableCell(__('Address'));
  1040. $cells .= wf_TableCell(__('Real name'));
  1041. $cells .= wf_TableCell(__('IP'));
  1042. $cells .= wf_TableCell(__('Tariff'));
  1043. $cells .= wf_TableCell(__('Active'));
  1044. $cells .= wf_TableCell(__('Balance'));
  1045. $cells .= wf_TableCell(__('Credit'));
  1046. $cells .= wf_TableCell(__('Held jobs for this user'));
  1047. $cells .= wf_TableCell(wf_CheckInput('check', '', false, false), '', 'sorttable_nosort');
  1048. $rows = wf_TableRow($cells, 'row1');
  1049. $id = '1';
  1050. $allRealNames = zb_UserGetAllRealnames();
  1051. $allAddress = zb_AddressGetFulladdresslistCached();
  1052. $quary_user_data = "SELECT `login`,`Cash`,`Credit`,`Passive`,`Tariff`,`IP` from `users`";
  1053. $user_data = simple_queryall($quary_user_data);
  1054. $user_data_arr = array();
  1055. if (!empty($user_data)) {
  1056. foreach ($user_data as $logindata) {
  1057. $user_data_arr[$logindata['login']]['Cash'] = $logindata['Cash'];
  1058. $user_data_arr[$logindata['login']]['Credit'] = $logindata['Credit'];
  1059. $user_data_arr[$logindata['login']]['Passive'] = $logindata['Passive'];
  1060. $user_data_arr[$logindata['login']]['Tariff'] = $logindata['Tariff'];
  1061. $user_data_arr[$logindata['login']]['ip'] = $logindata['IP'];
  1062. }
  1063. }
  1064. if (!empty($this->allTasks)) {
  1065. $tmpArr = array();
  1066. foreach ($this->allTasks as $io => $each) {
  1067. $login = $each['login'];
  1068. $tmpArr[$login][] = $each['action'];
  1069. }
  1070. }
  1071. foreach ($logins as $login) {
  1072. //is this user real?
  1073. if (isset($availableUsers[$login])) {
  1074. //finance check
  1075. $cash = $user_data_arr[$login]['Cash'];
  1076. $credit = $user_data_arr[$login]['Credit'];
  1077. $passive = $user_data_arr[$login]['Passive'];
  1078. $tariff = $user_data_arr[$login]['Tariff'];
  1079. $ip = $user_data_arr[$login]['ip'];
  1080. // Display user status
  1081. $act = '<img src=skins/icon_active.gif>' . __('Yes');
  1082. if ($cash < '-' . $credit) {
  1083. $act = '<img src=skins/icon_inactive.gif>' . __('No');
  1084. }
  1085. $act .= $passive ? '<br> <img src=skins/icon_passive.gif>' . __('Freezed') : '';
  1086. $cells = wf_TableCell($id);
  1087. $cells .= wf_TableCell(wf_Link('?module=userprofile&username=' . $login, web_profile_icon() . $login, false, ''));
  1088. $cells .= wf_TableCell(@$allAddress[$login]);
  1089. $cells .= wf_TableCell(@$allRealNames[$login]);
  1090. $cells .= wf_TableCell($ip);
  1091. $cells .= wf_TableCell($tariff);
  1092. $cells .= wf_TableCell($act);
  1093. $cells .= wf_TableCell($cash);
  1094. $cells .= wf_TableCell($credit);
  1095. if (isset($tmpArr[$login])) {
  1096. $cells_temp = '';
  1097. foreach ($tmpArr[$login] as $task) {
  1098. $actionIcon = (isset($this->actionIcons[$task])) ? wf_img_sized($this->actionIcons[$task], $this->actionNames[$task], '12', '12') . ' ' : '';
  1099. $cells_temp .= $actionIcon . $this->actionNames[$task] . wf_tag('br');
  1100. }
  1101. $cells .= wf_TableCell($cells_temp);
  1102. } else {
  1103. $cells .= wf_TableCell('');
  1104. }
  1105. $cells .= wf_TableCell(wf_CheckInput('_logins[' . $login . ']', '', false, false));
  1106. $rows .= wf_TableRow($cells, 'row3');
  1107. $id++;
  1108. }
  1109. }
  1110. $result .= wf_AjaxLoader();
  1111. $inputs = wf_HiddenInput('newschedloginsarr', true);
  1112. $inputs .= wf_DatePickerPreset('newscheddate', date('Y-m-d', strtotime('+1 day')), true) . ' ' . __('Target date') . wf_tag('br');
  1113. $inputs .= wf_AjaxSelectorAC('ajparamcontainer', $this->actions, __('Task'), '', true);
  1114. $inputs .= wf_AjaxContainer('ajparamcontainer');
  1115. $inputs .= wf_tag('br');
  1116. $inputs .= wf_TableBody($rows, '100%', 0, 'sortable');
  1117. $result .= wf_Form('', 'POST', $inputs, '');
  1118. }
  1119. return ($result);
  1120. }
  1121. /**
  1122. * Returns search form
  1123. *
  1124. * @return void
  1125. */
  1126. protected function renderUsersSearchForm() {
  1127. $allcity = array();
  1128. $tmpCity = zb_AddressGetCityAllData();
  1129. if (!empty($tmpCity)) {
  1130. foreach ($tmpCity as $io => $each) {
  1131. $allcity[$each['id']] = $each['cityname'];
  1132. }
  1133. }
  1134. $param_selector_status = array(
  1135. '',
  1136. 'active' => __('Active'),
  1137. 'AlwaysOnline' => __('Always Online'),
  1138. 'inactive' => __('Inactive'),
  1139. 'down' => __('Disconnected'),
  1140. 'frozen' => __('Frozen'),
  1141. );
  1142. // Load tariffs
  1143. $alltariffs = zb_TariffsGetAll();
  1144. $tariffs_options = array();
  1145. if (!empty($alltariffs)) {
  1146. foreach ($alltariffs as $io => $eachtariff) {
  1147. $tariffs_options[$eachtariff['name']] = $eachtariff['name'];
  1148. }
  1149. }
  1150. // Load services
  1151. $allservices = multinet_get_services();
  1152. $services_options = array();
  1153. if (!empty($allservices)) {
  1154. foreach ($allservices as $io => $eachservice) {
  1155. $services_options[$eachservice['netid']] = $eachservice['desc'];
  1156. }
  1157. }
  1158. // Load tags
  1159. $query_alltags = "SELECT `id`,`tagname` FROM `tagtypes`";
  1160. $alltags = simple_queryall($query_alltags);
  1161. $tags_options = array();
  1162. if (!empty($alltags)) {
  1163. foreach ($alltags as $io => $eachtag) {
  1164. $tags_options[$eachtag['id']] = $eachtag['tagname'];
  1165. }
  1166. }
  1167. // Load switches
  1168. $query_allswitches = "SELECT * from `switches` ORDER BY `location`";
  1169. $allswitches = simple_queryall($query_allswitches);
  1170. $switches_options = array();
  1171. if (!empty($allswitches)) {
  1172. foreach ($allswitches as $io => $eachsw) {
  1173. $switches_options[$eachsw['id']] = $eachsw['ip'] . ' - ' . $eachsw['location'];
  1174. }
  1175. }
  1176. // Рисуем форму, которая включает в запрос пользователей
  1177. $cells = wf_TableCell(wf_tag('b') . __('Include in search query') . wf_tag('b'), '', '', 'colspan="3"');
  1178. $rows = wf_TableRow($cells, 'row2');
  1179. $cells = wf_TableCell(__('All fields'));
  1180. $cells .= wf_TableCell(wf_CheckInput('dealwithit_search[search_by][all_fields]', '', false));
  1181. $cells .= wf_TableCell(wf_TextInput('dealwithit_search[all_fields]', '', '', false));
  1182. $rows .= wf_TableRow($cells, 'row2');
  1183. $cells = wf_TableCell(__('City'));
  1184. $cells .= wf_TableCell(wf_CheckInput('dealwithit_search[search_by][city_id]', '', false));
  1185. $cells .= wf_TableCell(wf_Selector('dealwithit_search[city_id]', $allcity, '', '', false));
  1186. $rows .= wf_TableRow($cells, 'row2');
  1187. $cells = wf_TableCell(__('Tariff'));
  1188. $cells .= wf_TableCell(wf_CheckInput('dealwithit_search[search_by][tariff]', '', false));
  1189. $cells .= wf_TableCell(wf_Selector('dealwithit_search[tariff]', $tariffs_options, '', '', false));
  1190. $rows .= wf_TableRow($cells, 'row2');
  1191. $cells = wf_TableCell(__('Tariff contains'));
  1192. $cells .= wf_TableCell(wf_CheckInput('dealwithit_search[search_by][tariff_contains]', '', false));
  1193. $cells .= wf_TableCell(wf_TextInput('dealwithit_search[tariff_contains]', '', '', ''));
  1194. $rows .= wf_TableRow($cells, 'row2');
  1195. $cells = wf_TableCell(__('Status'));
  1196. $cells .= wf_TableCell(wf_CheckInput('dealwithit_search[search_by][user_status]', '', false));
  1197. $cells .= wf_TableCell(wf_Selector('dealwithit_search[user_status]', $param_selector_status, '', '', false));
  1198. $rows .= wf_TableRow($cells, 'row2');
  1199. $cells = wf_TableCell(__('Services'));
  1200. $cells .= wf_TableCell(wf_CheckInput('dealwithit_search[search_by][services]', '', false));
  1201. $cells .= wf_TableCell(wf_Selector('dealwithit_search[services]', $services_options, '', '', false));
  1202. $rows .= wf_TableRow($cells, 'row2');
  1203. $cells = wf_TableCell(__('Tags'));
  1204. $cells .= wf_TableCell(wf_CheckInput('dealwithit_search[search_by][tags]', '', false));
  1205. $cells .= wf_TableCell(wf_Selector('dealwithit_search[tags]', $tags_options, '', '', false));
  1206. $rows .= wf_TableRow($cells, 'row2');
  1207. $cells = wf_TableCell(__('Switch'));
  1208. $cells .= wf_TableCell(wf_CheckInput('dealwithit_search[search_by][switch]', '', false));
  1209. $cells .= wf_TableCell(wf_Selector('dealwithit_search[switch]', $switches_options, '', '', false));
  1210. $rows .= wf_TableRow($cells, 'row2');
  1211. // Рисуем форму, которая исключает из запроса пользователей
  1212. $cells_ex = wf_TableCell(wf_tag('b') . __('Exclude from search query') . wf_tag('/b'), '', '', 'colspan="3"');
  1213. $rows_ex = wf_TableRow($cells_ex, 'row2');
  1214. $cells_ex = wf_TableCell(__('All fields'));
  1215. $cells_ex .= wf_TableCell(wf_CheckInput('dealwithit_search[exclude][ex_all_fields]', '', false));
  1216. $cells_ex .= wf_TableCell(wf_TextInput('dealwithit_search[ex_all_fields]', '', '', false));
  1217. $rows_ex .= wf_TableRow($cells_ex, 'row2');
  1218. $cells_ex = wf_TableCell(__('City'));
  1219. $cells_ex .= wf_TableCell(wf_CheckInput('dealwithit_search[exclude][ex_city_id]', '', false));
  1220. $cells_ex .= wf_TableCell(wf_Selector('dealwithit_search[ex_city_id]', $allcity, '', '', false));
  1221. $rows_ex .= wf_TableRow($cells_ex, 'row2');
  1222. $cells_ex = wf_TableCell(__('Tariff'));
  1223. $cells_ex .= wf_TableCell(wf_CheckInput('dealwithit_search[exclude][ex_tariff]', '', false));
  1224. $cells_ex .= wf_TableCell(wf_Selector('dealwithit_search[ex_tariff]', $tariffs_options, '', '', false));
  1225. $rows_ex .= wf_TableRow($cells_ex, 'row2');
  1226. $cells_ex = wf_TableCell(__('Tariff contains'));
  1227. $cells_ex .= wf_TableCell(wf_CheckInput('dealwithit_search[exclude][ex_tariff_contains]', '', false));
  1228. $cells_ex .= wf_TableCell(wf_TextInput('dealwithit_search[ex_tariff_contains]', '', '', ''));
  1229. $rows_ex .= wf_TableRow($cells_ex, 'row2');
  1230. $cells_ex = wf_TableCell(__('Status'));
  1231. $cells_ex .= wf_TableCell(wf_CheckInput('dealwithit_search[exclude][ex_user_status]', '', false));
  1232. $cells_ex .= wf_TableCell(wf_Selector('dealwithit_search[ex_user_status]', $param_selector_status, '', '', false));
  1233. $rows_ex .= wf_TableRow($cells_ex, 'row2');
  1234. $cells_ex = wf_TableCell(__('Services'));
  1235. $cells_ex .= wf_TableCell(wf_CheckInput('dealwithit_search[exclude][ex_services]', '', false));
  1236. $cells_ex .= wf_TableCell(wf_Selector('dealwithit_search[ex_services]', $services_options, '', '', false));
  1237. $rows_ex .= wf_TableRow($cells_ex, 'row2');
  1238. $cells_ex = wf_TableCell(__('Tags'));
  1239. $cells_ex .= wf_TableCell(wf_CheckInput('dealwithit_search[exclude][ex_tags]', '', false));
  1240. $cells_ex .= wf_TableCell(wf_Selector('dealwithit_search[ex_tags]', $tags_options, '', '', false));
  1241. $rows_ex .= wf_TableRow($cells_ex, 'row2');
  1242. $cells_ex = wf_TableCell(__('Switch'));
  1243. $cells_ex .= wf_TableCell(wf_CheckInput('dealwithit_search[exclude][ex_switch]', '', false));
  1244. $cells_ex .= wf_TableCell(wf_Selector('dealwithit_search[ex_switch]', $switches_options, '', '', false));
  1245. $rows_ex .= wf_TableRow($cells_ex, 'row2');
  1246. $rows_ex .= wf_TableRow(wf_TableCell(wf_Submit('Search')));
  1247. $form = wf_TableBody($rows, '', 0, '', 'style="float: left; padding-right: 20px;"');
  1248. $form .= wf_TableBody($rows_ex, '', 0, '', 'style="float: left;"');
  1249. $result = wf_Form("", "POST", $form, 'glamour');
  1250. return ($result);
  1251. }
  1252. /**
  1253. * Returns search form
  1254. *
  1255. * @return array
  1256. */
  1257. protected function SearchUsers(array $dealwithit_search) {
  1258. $result = array();
  1259. $result_exclude = array();
  1260. $search_field = $_POST['dealwithit_search']['search_by'];
  1261. $exclude_field = @$_POST['dealwithit_search']['exclude'];
  1262. // Search login by City
  1263. if (isset($search_field['city_id']) and $search_field['city_id'] == 'on') {
  1264. // И шо я только курю
  1265. $query = "SELECT `login` FROM `street` LEFT JOIN city ON street.cityid=city.id
  1266. LEFT JOIN build ON build.streetid=street.id
  1267. LEFT JOIN apt ON apt.buildid=build.id
  1268. RIGHT JOIN address ON address.aptid=apt.id
  1269. WHERE cityid = '" . vf($_POST['dealwithit_search']['city_id'], 3) . "'";
  1270. $data_city = simple_queryall($query);
  1271. if (!empty($data_city)) {
  1272. foreach ($data_city as $login) {
  1273. $result[] = $login['login'];
  1274. }
  1275. }
  1276. }
  1277. // Search login by Tariff
  1278. if (isset($search_field['tariff']) and $search_field['tariff'] == 'on') {
  1279. $query = "SELECT `login` FROM `users` WHERE `Tariff` = '" . $_POST['dealwithit_search']['tariff'] . "'";
  1280. $data_tariff = simple_queryall($query);
  1281. if (!empty($data_tariff)) {
  1282. foreach ($data_tariff as $login) {
  1283. $result[] = $login['login'];
  1284. }
  1285. }
  1286. }
  1287. // Search login by Tariff contains string
  1288. if (isset($search_field['tariff_contains']) and $search_field['tariff_contains'] == 'on') {
  1289. $query = "SELECT `login` FROM `users` WHERE `Tariff` LIKE '%" . $_POST['dealwithit_search']['tariff_contains'] . "%'";
  1290. $data_tariff = simple_queryall($query);
  1291. if (!empty($data_tariff)) {
  1292. foreach ($data_tariff as $login) {
  1293. $result[] = $login['login'];
  1294. }
  1295. }
  1296. }
  1297. // Search login by status
  1298. if (isset($search_field['user_status']) and $search_field['user_status'] == 'on') {
  1299. $need_status = $_POST['dealwithit_search']['user_status'];
  1300. if (!empty($need_status)) {
  1301. switch ($need_status) {
  1302. case 'active':
  1303. $where = "`Cash` >= -`Credit` ";
  1304. break;
  1305. case 'AlwaysOnline':
  1306. $where = "`AlwaysOnline` ='1'";
  1307. break;
  1308. case 'inactive':
  1309. $where = "`Cash` < -`Credit` ";
  1310. break;
  1311. case 'down':
  1312. $where = "`Down` ='1'";
  1313. break;
  1314. case 'frozen':
  1315. $where = "`Passive` ='1'";
  1316. break;
  1317. }
  1318. $query = "SELECT `login` FROM `users` WHERE " . $where;
  1319. $data_status = simple_queryall($query);
  1320. if (!empty($data_status)) {
  1321. foreach ($data_status as $login) {
  1322. $result[] = $login['login'];
  1323. }
  1324. }
  1325. }
  1326. }
  1327. // Search login by Services
  1328. if (isset($search_field['services']) and $search_field['services'] == 'on') {
  1329. $query = "SELECT `login` FROM `users` INNER JOIN `nethosts` USING (`ip`) WHERE `nethosts`.`netid` = '" . vf($_POST['dealwithit_search']['services'], 3) . "'";
  1330. $data_services = simple_queryall($query);
  1331. if (!empty($data_services)) {
  1332. foreach ($data_services as $login) {
  1333. $result[] = $login['login'];
  1334. }
  1335. }
  1336. }
  1337. // hide dead users array
  1338. // This should be here, because below we will work with tags, among which may be buried users
  1339. if (!empty($result) and $this->altCfg['DEAD_HIDE']) {
  1340. if (!empty($this->altCfg['DEAD_TAGID'])) {
  1341. $deadUsers = array();
  1342. $tagDead = vf($this->altCfg['DEAD_TAGID'], 3);
  1343. $query_dead = "SELECT `login` from `tags` WHERE `tagid`='" . $tagDead . "'";
  1344. $alldead = simple_queryall($query_dead);
  1345. if (!empty($alldead)) {
  1346. foreach ($alldead as $idead => $eachDead) {
  1347. $deadUsers[] = $eachDead['login'];
  1348. }
  1349. $result = array_diff($result, $deadUsers);
  1350. }
  1351. }
  1352. }
  1353. // Search login by all fields
  1354. if (isset($search_field['all_fields']) and $search_field['all_fields'] == 'on') {
  1355. $data_fileds = zb_UserSearchAllFields($_POST['dealwithit_search']['all_fields'], false);
  1356. if (!empty($data_fileds) and is_array($data_fileds)) {
  1357. foreach ($data_fileds as $login) {
  1358. $result[] = $login;
  1359. }
  1360. }
  1361. }
  1362. // Search login by Tag
  1363. if (isset($search_field['tags']) and $search_field['tags'] == 'on') {
  1364. $query = "SELECT `login` from `tags` WHERE `tagid`='" . vf($_POST['dealwithit_search']['tags'], 3) . "'";
  1365. $data_tags = simple_queryall($query);
  1366. if (!empty($data_tags)) {
  1367. foreach ($data_tags as $login) {
  1368. $result[] = $login['login'];
  1369. }
  1370. }
  1371. }
  1372. // Search login by Switch
  1373. if (isset($search_field['switch']) and $search_field['switch'] == 'on') {
  1374. $query = "SELECT `login` from `switchportassign` WHERE `switchid`='" . vf($_POST['dealwithit_search']['switch'], 3) . "'";
  1375. $data_switches = simple_queryall($query);
  1376. if (!empty($data_switches)) {
  1377. foreach ($data_switches as $login) {
  1378. $result[] = $login['login'];
  1379. }
  1380. }
  1381. }
  1382. // Исключаем из запроса пользователей
  1383. // Начинаем заполнять массив исключения
  1384. // Exclude login from request by City
  1385. if (isset($exclude_field['ex_city_id']) and $exclude_field['ex_city_id'] == 'on') {
  1386. // И шо я только курю уже дважды
  1387. $query = "SELECT `login` FROM `street` LEFT JOIN city ON street.cityid=city.id
  1388. LEFT JOIN build ON build.streetid=street.id
  1389. LEFT JOIN apt ON apt.buildid=build.id
  1390. RIGHT JOIN address ON address.aptid=apt.id
  1391. WHERE cityid = '" . vf($_POST['dealwithit_search']['ex_city_id'], 3) . "'";
  1392. $data_city = simple_queryall($query);
  1393. if (!empty($data_city)) {
  1394. foreach ($data_city as $login) {
  1395. $result_exclude[] = $login['login'];
  1396. }
  1397. }
  1398. }
  1399. // Exclude login from request by Tariff
  1400. if (isset($exclude_field['ex_tariff']) and $exclude_field['ex_tariff'] == 'on') {
  1401. $query = "SELECT `login` FROM `users` WHERE `Tariff` = '" . $_POST['dealwithit_search']['ex_tariff'] . "'";
  1402. $data_tariff = simple_queryall($query);
  1403. if (!empty($data_tariff)) {
  1404. foreach ($data_tariff as $login) {
  1405. $result_exclude[] = $login['login'];
  1406. }
  1407. }
  1408. }
  1409. // Search login by Tariff contains string
  1410. if (isset($exclude_field['ex_tariff_contains']) and $exclude_field['ex_tariff_contains'] == 'on') {
  1411. $query = "SELECT `login` FROM `users` WHERE `Tariff` LIKE '%" . $_POST['dealwithit_search']['ex_tariff_contains'] . "%'";
  1412. $data_tariff = simple_queryall($query);
  1413. if (!empty($data_tariff)) {
  1414. foreach ($data_tariff as $login) {
  1415. $result_exclude[] = $login['login'];
  1416. }
  1417. }
  1418. }
  1419. // Exclude login from request by status
  1420. if (isset($exclude_field['ex_user_status']) and $exclude_field['ex_user_status'] == 'on') {
  1421. $need_status_exclude = $_POST['dealwithit_search']['ex_user_status'];
  1422. if (!empty($need_status_exclude)) {
  1423. switch ($need_status_exclude) {
  1424. case 'active':
  1425. $where = "`Cash` >= -`Credit` ";
  1426. break;
  1427. case 'AlwaysOnline':
  1428. $where = "`AlwaysOnline` ='1'";
  1429. break;
  1430. case 'inactive':
  1431. $where = "`Cash` < -`Credit` ";
  1432. break;
  1433. case 'down':
  1434. $where = "`Down` ='1'";
  1435. break;
  1436. case 'frozen':
  1437. $where = "`Passive` ='1'";
  1438. break;
  1439. }
  1440. $query_exclude = "SELECT `login` FROM `users` WHERE " . $where;
  1441. $data_status_exclude = simple_queryall($query_exclude);
  1442. if (!empty($data_status_exclude)) {
  1443. foreach ($data_status_exclude as $login) {
  1444. $result_exclude[] = $login['login'];
  1445. }
  1446. }
  1447. }
  1448. }
  1449. // Exclude login from request by Services
  1450. if (isset($exclude_field['ex_services']) and $exclude_field['ex_services'] == 'on') {
  1451. $query_exclude = "SELECT `login` FROM `users` INNER JOIN `nethosts` USING (`ip`) WHERE `nethosts`.`netid` = '" . vf($_POST['dealwithit_search']['ex_services'], 3) . "'";
  1452. $data_services_exclude = simple_queryall($query_exclude);
  1453. if (!empty($data_services_exclude)) {
  1454. foreach ($data_services_exclude as $login) {
  1455. $result_exclude[] = $login['login'];
  1456. }
  1457. }
  1458. }
  1459. // Exclude dead users array
  1460. // This should be here, because below we will work with tags, among which may be buried users
  1461. if (!empty($result_exclude) and $this->altCfg['DEAD_HIDE']) {
  1462. if (!empty($this->altCfg['DEAD_TAGID'])) {
  1463. $deadUsers = array();
  1464. $tagDead = vf($this->altCfg['DEAD_TAGID'], 3);
  1465. $query_dead = "SELECT `login` from `tags` WHERE `tagid`='" . $tagDead . "'";
  1466. $alldead = simple_queryall($query_dead);
  1467. if (!empty($alldead)) {
  1468. foreach ($alldead as $idead => $eachDead) {
  1469. $deadUsers[] = $eachDead['login'];
  1470. }
  1471. $result_exclude = array_diff($result_exclude, $deadUsers);
  1472. }
  1473. }
  1474. }
  1475. // Exclude login from request by all fields
  1476. if (isset($exclude_field['ex_all_fields']) and $exclude_field['ex_all_fields'] == 'on') {
  1477. $data_fileds_exclude = zb_UserSearchAllFields($_POST['dealwithit_search']['ex_all_fields'], false);
  1478. if (!empty($data_fileds_exclude) and is_array($data_fileds_exclude)) {
  1479. foreach ($data_fileds_exclude as $login) {
  1480. $result_exclude[] = $login;
  1481. }
  1482. }
  1483. }
  1484. // Exclude login from request by Tag
  1485. if (isset($exclude_field['ex_tags']) and $exclude_field['ex_tags'] == 'on') {
  1486. $query_exclude = "SELECT `login` from `tags` WHERE `tagid`='" . vf($_POST['dealwithit_search']['ex_tags'], 3) . "'";
  1487. $data_tags_exclude = simple_queryall($query_exclude);
  1488. if (!empty($data_tags_exclude)) {
  1489. foreach ($data_tags_exclude as $login) {
  1490. $result_exclude[] = $login['login'];
  1491. }
  1492. }
  1493. }
  1494. // Exclude login by switch
  1495. if (isset($exclude_field['ex_switch']) and $exclude_field['ex_switch'] == 'on') {
  1496. $query_exclude = "SELECT `login` from `switchportassign` WHERE `switchid`='" . vf($_POST['dealwithit_search']['ex_switch'], 3) . "'";
  1497. $data_switches_exclude = simple_queryall($query_exclude);
  1498. if (!empty($data_switches_exclude)) {
  1499. foreach ($data_switches_exclude as $login) {
  1500. $result_exclude[] = $login['login'];
  1501. }
  1502. }
  1503. }
  1504. // Сам прцоцесс исключения пользователей из результатов поиска
  1505. if (!empty($result) and ! empty($result_exclude)) {
  1506. $result = array_diff($result, $result_exclude);
  1507. }
  1508. // Delete duplicates that come from more that one selected options
  1509. if (!empty($result)) {
  1510. $result = array_unique($result);
  1511. }
  1512. return ($result);
  1513. }
  1514. }