api.branches.php 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626
  1. <?php
  2. /**
  3. * ISP branches implementation
  4. */
  5. class UbillingBranches {
  6. /**
  7. * Contains current user login
  8. *
  9. * @var string
  10. */
  11. protected $myLogin = '';
  12. /**
  13. * Contains available branches as branchid=>admin login
  14. *
  15. * @var array
  16. */
  17. protected $myBranches = array();
  18. /**
  19. * Contains available branches as id=>branch data
  20. *
  21. * @var array
  22. */
  23. protected $branches = array();
  24. /**
  25. * Contains login assins for branches as login=>branchid
  26. *
  27. * @var array
  28. */
  29. protected $branchesLogins = array();
  30. /**
  31. * Users logins allowed for current administrator as login=>branchid
  32. *
  33. * @var array
  34. */
  35. protected $myUsers = array();
  36. /**
  37. * Contains branches admins as id=>data
  38. *
  39. * @var array
  40. */
  41. protected $branchesAdmins = array();
  42. /**
  43. * Contains array of id=>assigndata assigns for cities
  44. *
  45. * @var array
  46. */
  47. protected $branchesCities = array();
  48. /**
  49. * Contains available cities names as cityid=>cityname
  50. *
  51. * @var array
  52. */
  53. protected $allCityNames = array();
  54. /**
  55. * Contains array of accessible cities for current administrator as cityid=>cityname
  56. *
  57. * @var array
  58. */
  59. protected $myCities = array();
  60. /**
  61. * Contains array of id=>assigndata for tariffs
  62. *
  63. * @var array
  64. */
  65. protected $branchesTariffs = array();
  66. /**
  67. * Contains all available tariffs as tariffname=>fee
  68. *
  69. * @var array
  70. */
  71. protected $allTariffs = array();
  72. /**
  73. * Contains array of accessible tariffs for current administrator as tariffname=>tariffname
  74. *
  75. * @var array
  76. */
  77. protected $myTariffs = array();
  78. /**
  79. * Contains array of id=>assingdata for services
  80. *
  81. * @var array
  82. */
  83. protected $branchesServices = array();
  84. /**
  85. * Contains array of available services as id=>name
  86. *
  87. * @var array
  88. */
  89. protected $allServices = array();
  90. /**
  91. * Contains array of accessible services for current administrator as serviceid=>servicename
  92. *
  93. * @var array
  94. */
  95. protected $myServices = array();
  96. /**
  97. * Contains system alter.ini config as key=>value
  98. *
  99. * @var array
  100. */
  101. protected $altCfg = array();
  102. /**
  103. * Contains system mussages object placeholder
  104. *
  105. * @var object
  106. */
  107. protected $messages = '';
  108. /**
  109. * Branches enabled flag
  110. *
  111. * @var bool
  112. */
  113. protected $branchesEnabled = false;
  114. const URL_ME = '?module=branches';
  115. const URL_USERPROFILE = '?module=userprofile&username=';
  116. const URL_TRAFFSTATS = '?module=traffstats&username=';
  117. const URL_ADDCASH = '?module=addcash&username=';
  118. const EX_NO_BRANCH = 'EX_BRANCHID_NOT_EXISTS';
  119. const EX_NO_NAME = 'EX_EMPTY_BRANCH_NAME';
  120. const EX_NO_USER = 'EX_EMPTY_LOGIN';
  121. const EX_NO_CITY = 'EX_EMPTY_CITY';
  122. const EX_NO_TARIFF = 'EX_EMPTY_TARIFF';
  123. const EX_NO_SERVICE = 'EX_EMPTY_SERVICE';
  124. const EX_NO_ADMIN = 'EX_EMPTY_ADMIN';
  125. public function __construct() {
  126. $this->loadAlter();
  127. if ($this->altCfg['BRANCHES_ENABLED']) {
  128. $this->branchesEnabled = true;
  129. $this->setLogin();
  130. $this->initMessages();
  131. $this->loadBranches();
  132. $this->loadBranchesAdmins();
  133. $this->setMyBranches();
  134. $this->loadBranchesUsers();
  135. } else {
  136. $this->branchesEnabled = false;
  137. }
  138. }
  139. /**
  140. * Sets current user login
  141. *
  142. * @return void
  143. */
  144. protected function setLogin() {
  145. $this->myLogin = whoami();
  146. }
  147. /**
  148. * Loads system alter config into protected property
  149. *
  150. * @global object $ubillingConfig
  151. *
  152. * @return void
  153. */
  154. protected function loadAlter() {
  155. global $ubillingConfig;
  156. $this->altCfg = $ubillingConfig->getAlter();
  157. }
  158. /**
  159. * Inits system messages helper object for further usage
  160. *
  161. * @return void
  162. */
  163. protected function initMessages() {
  164. $this->messages = new UbillingMessageHelper();
  165. }
  166. /**
  167. * Loads available branches from database
  168. *
  169. * @return void
  170. */
  171. protected function loadBranches() {
  172. $query = "SELECT * from `branches` ORDER BY `id` DESC";
  173. $all = simple_queryall($query);
  174. if (!empty($all)) {
  175. foreach ($all as $io => $each) {
  176. $this->branches[$each['id']] = $each;
  177. }
  178. }
  179. }
  180. /**
  181. * Loads available branches admins from database
  182. *
  183. * @return void
  184. */
  185. protected function loadBranchesAdmins() {
  186. if (!empty($this->branches)) {
  187. $query = "SELECT * from `branchesadmins` ORDER BY `id` DESC";
  188. $all = simple_queryall($query);
  189. if (!empty($all)) {
  190. foreach ($all as $io => $each) {
  191. $this->branchesAdmins[$each['id']] = $each;
  192. }
  193. }
  194. }
  195. }
  196. /**
  197. * Loads cities assigns from database into protected prop. Must be executed first before isMyCity() usage.
  198. *
  199. * @return void
  200. */
  201. public function loadCities() {
  202. $this->allCityNames = zb_AddressGetFullCityNames();
  203. $query = "SELECT * from `branchescities`";
  204. $all = simple_queryall($query);
  205. if (!empty($all)) {
  206. foreach ($all as $io => $each) {
  207. $this->branchesCities[$each['id']]['branchid'] = $each['branchid'];
  208. $this->branchesCities[$each['id']]['cityid'] = $each['cityid'];
  209. if (isset($this->myBranches[$each['branchid']])) {
  210. $this->myCities[$each['cityid']] = $this->allCityNames[$each['cityid']];
  211. }
  212. }
  213. }
  214. }
  215. /**
  216. * Loads cities assigns from database into protected prop
  217. *
  218. * @return void
  219. */
  220. public function loadTariffs() {
  221. $this->allTariffs = zb_TariffGetPricesAll();
  222. $query = "SELECT * from `branchestariffs`";
  223. $all = simple_queryall($query);
  224. if (!empty($all)) {
  225. foreach ($all as $io => $each) {
  226. $this->branchesTariffs[$each['id']]['branchid'] = $each['branchid'];
  227. $this->branchesTariffs[$each['id']]['tariff'] = $each['tariff'];
  228. if (isset($this->myBranches[$each['branchid']])) {
  229. $this->myTariffs[$each['tariff']] = $each['tariff'];
  230. }
  231. }
  232. }
  233. }
  234. /**
  235. * Loads services assings from database into protected properties
  236. *
  237. * @return void
  238. */
  239. public function loadServices() {
  240. $servicesTmp = multinet_get_services();
  241. if (!empty($servicesTmp)) {
  242. foreach ($servicesTmp as $io => $each) {
  243. $this->allServices[$each['id']] = $each['desc'];
  244. }
  245. }
  246. $query = "SELECT * from `branchesservices`";
  247. $all = simple_queryall($query);
  248. if (!empty($all)) {
  249. foreach ($all as $io => $each) {
  250. $this->branchesServices[$each['id']]['branchid'] = $each['branchid'];
  251. $this->branchesServices[$each['id']]['serviceid'] = $each['serviceid'];
  252. if (isset($this->myBranches[$each['branchid']])) {
  253. $this->myServices[$each['serviceid']] = $this->allServices[$each['serviceid']];
  254. }
  255. }
  256. }
  257. }
  258. /**
  259. * Gets current administrator branches IDs and sets it intoprotected prop
  260. *
  261. * @return void
  262. */
  263. protected function setMyBranches() {
  264. if (!empty($this->branchesAdmins)) {
  265. foreach ($this->branchesAdmins as $io => $each) {
  266. if ($each['admin'] == $this->myLogin) {
  267. $this->myBranches[$each['branchid']] = $this->myLogin;
  268. }
  269. }
  270. }
  271. }
  272. /**
  273. * Loads available user-branch pairs from database
  274. *
  275. * @return void
  276. */
  277. protected function loadBranchesUsers() {
  278. $query = "SELECT * from `branchesusers`";
  279. $all = simple_queryall($query);
  280. if (!empty($all)) {
  281. foreach ($all as $io => $each) {
  282. $this->branchesLogins[$each['login']] = $each['branchid'];
  283. if (isset($this->myBranches[$each['branchid']])) {
  284. $this->myUsers[$each['login']] = $each['branchid'];
  285. }
  286. }
  287. }
  288. }
  289. /**
  290. * Creates new branch
  291. *
  292. * @param string $name
  293. *
  294. * @return int
  295. */
  296. public function createBranch($name) {
  297. $nameF = mysql_real_escape_string($name);
  298. if (!empty($nameF)) {
  299. $query = "INSERT INTO `branches` (`id`,`name`) VALUES ";
  300. $query .= "(NULL,'" . $nameF . "');";
  301. nr_query($query);
  302. $newId = simple_get_lastid('branches');
  303. log_register('BRANCH CREATE [' . $newId . '] `' . $name . '`');
  304. return ($newId);
  305. } else {
  306. throw new Exception(self::EX_NO_NAME);
  307. }
  308. }
  309. /**
  310. * Checks is branch have assigned users
  311. *
  312. * @param int $branchId
  313. *
  314. * @return bool
  315. */
  316. public function isBranchProtected($branchId) {
  317. $branchId = vf($branchId, 3);
  318. $result = false;
  319. if (isset($this->branches[$branchId])) {
  320. if (!empty($this->branchesLogins)) {
  321. foreach ($this->branchesLogins as $eachLogin => $eachId) {
  322. if ($branchId == $eachId) {
  323. $result = true;
  324. break;
  325. }
  326. }
  327. }
  328. }
  329. return ($result);
  330. }
  331. /**
  332. * Deletes branch by its ID
  333. *
  334. * @param int $branchId
  335. *
  336. * @return void
  337. */
  338. public function deleteBranch($branchId) {
  339. $branchId = vf($branchId, 3);
  340. if (isset($this->branches[$branchId])) {
  341. $query = "DELETE from `branches` WHERE `id`='" . $branchId . "';";
  342. nr_query($query);
  343. //admins cleanup
  344. $queryAdmins = "DELETE from `branchesadmins` WHERE `branchid`='" . $branchId . "';";
  345. nr_query($queryAdmins);
  346. log_register('BRANCH DELETE [' . $branchId . ']');
  347. }
  348. }
  349. /**
  350. * Updates existing branch name
  351. *
  352. * @param int $branchId
  353. * @param string $branchName
  354. *
  355. * @return void
  356. */
  357. public function editBranch($branchId, $branchName) {
  358. $branchId = vf($branchId, 3);
  359. if (isset($this->branches[$branchId])) {
  360. simple_update_field('branches', 'name', $branchName, "WHERE `id`='" . $branchId . "'");
  361. log_register('BRANCH UPDATE [' . $branchId . '] `' . $branchName . '`');
  362. }
  363. }
  364. /**
  365. * Checks is admin assigned to some branch, to prevent duplicates
  366. *
  367. * @param int $branchId
  368. * @param string $adminLogin
  369. *
  370. * @return bool
  371. */
  372. protected function isAdminBranchAssigned($branchId, $adminLogin) {
  373. $result = false;
  374. if (!empty($this->branchesAdmins)) {
  375. foreach ($this->branchesAdmins as $io => $each) {
  376. if (($each['branchid'] == $branchId) AND ( $each['admin'] == $adminLogin)) {
  377. $result = true;
  378. break;
  379. }
  380. }
  381. }
  382. return ($result);
  383. }
  384. /**
  385. * Assigns administrator with some existing branch
  386. *
  387. * @param int $branchId
  388. * @param string $admin
  389. * @throws Exception
  390. *
  391. * @return void
  392. */
  393. public function adminAssignBranch($branchId, $admin) {
  394. $branchId = vf($branchId, 3);
  395. $admin = trim($admin);
  396. $adminF = mysql_real_escape_string($admin);
  397. if (isset($this->branches[$branchId])) {
  398. if (!empty($adminF)) {
  399. if (!$this->isAdminBranchAssigned($branchId, $admin)) {
  400. $query = "INSERT INTO `branchesadmins` (`id`,`branchid`,`admin`) VALUES";
  401. $query .= "(NULL,'" . $branchId . "','" . $adminF . "');";
  402. nr_query($query);
  403. log_register('BRANCH ASSIGN [' . $branchId . '] ADMIN {' . $admin . '}');
  404. }
  405. } else {
  406. throw new Exception(self::EX_NO_ADMIN);
  407. }
  408. } else {
  409. throw new Exception(self::EX_NO_BRANCH);
  410. }
  411. }
  412. /**
  413. * Deassigns administrator with some existing branch
  414. *
  415. * @param int $branchId
  416. * @param string $admin
  417. * @throws Exception
  418. *
  419. * @return void
  420. */
  421. public function adminDeassignBranch($branchId, $admin) {
  422. $branchId = vf($branchId, 3);
  423. $admin = trim($admin);
  424. $adminF = mysql_real_escape_string($admin);
  425. if (isset($this->branches[$branchId])) {
  426. if (!empty($adminF)) {
  427. $query = "DELETE from `branchesadmins` WHERE `branchid`='" . $branchId . "' AND `admin`='" . $adminF . "';";
  428. nr_query($query);
  429. log_register('BRANCH DEASSIGN [' . $branchId . '] ADMIN {' . $admin . '}');
  430. } else {
  431. throw new Exception(self::EX_NO_ADMIN);
  432. }
  433. } else {
  434. throw new Exception(self::EX_NO_BRANCH);
  435. }
  436. }
  437. /**
  438. * Assigns user login with existing branch ID
  439. *
  440. * @param type $branchId
  441. * @param type $login
  442. * @throws Exception
  443. *
  444. * @return void
  445. */
  446. public function userAssignBranch($branchId, $login) {
  447. $branchId = vf($branchId, 3);
  448. $login = trim($login);
  449. $loginF = mysql_real_escape_string($login);
  450. if (isset($this->branches[$branchId])) {
  451. if (!empty($loginF)) {
  452. $query = "INSERT INTO `branchesusers` (`id`,`branchid`,`login`) VALUES ";
  453. $query .= "(NULL,'" . $branchId . "','" . $loginF . "');";
  454. nr_query($query);
  455. log_register('BRANCH ASSIGN [' . $branchId . '] USER (' . $login . ')');
  456. } else {
  457. throw new Exception(self::EX_NO_USER);
  458. }
  459. } else {
  460. throw new Exception(self::EX_NO_BRANCH);
  461. }
  462. }
  463. /**
  464. * Deletes user assigned branch
  465. *
  466. * @param string $login
  467. *
  468. * @return void
  469. */
  470. public function userDeleteBranch($login) {
  471. $login = trim($login);
  472. $loginF = mysql_real_escape_string($login);
  473. $currentBranch = @$this->branchesLogins[$login];
  474. if (!empty($currentBranch)) {
  475. $query = "DELETE from `branchesusers` WHERE `login`='" . $loginF . "';";
  476. nr_query($query);
  477. log_register('BRANCH UNASSIGN [' . $currentBranch . '] USER (' . $login . ')');
  478. }
  479. }
  480. /**
  481. * Checks is user accessible by current administrator
  482. *
  483. * @param string $login
  484. *
  485. * @return bool
  486. */
  487. public function isMyUser($login) {
  488. $result = false;
  489. if ($this->branchesEnabled) {
  490. if (cfr('ROOT')) {
  491. $result = true;
  492. } else {
  493. if (cfr('BRANCHES')) {
  494. if (isset($this->myUsers[$login])) {
  495. $result = true;
  496. } else {
  497. $result = false;
  498. }
  499. } else {
  500. $result = true;
  501. }
  502. }
  503. } else {
  504. $result = true;
  505. }
  506. return ($result);
  507. }
  508. /**
  509. * Checks is city accessible by current administrator
  510. *
  511. * @param int $cityId
  512. *
  513. * @return bool
  514. */
  515. public function isMyCity($cityId) {
  516. $result = false;
  517. if ($this->branchesEnabled) {
  518. if (cfr('ROOT')) {
  519. $result = true;
  520. } else {
  521. if (cfr('BRANCHES')) {
  522. if (isset($this->myCities[$cityId])) {
  523. $result = true;
  524. } else {
  525. $result = false;
  526. }
  527. } else {
  528. $result = true;
  529. }
  530. }
  531. } else {
  532. $result = true;
  533. }
  534. return ($result);
  535. }
  536. /**
  537. * Checks is tariff accessible by current administrator
  538. *
  539. * @param string $tariffName
  540. *
  541. * @return bool
  542. */
  543. public function isMyTariff($tariffName) {
  544. $result = false;
  545. if ($this->branchesEnabled) {
  546. if (cfr('ROOT')) {
  547. $result = true;
  548. } else {
  549. if (cfr('BRANCHES')) {
  550. if (isset($this->myTariffs[$tariffName])) {
  551. $result = true;
  552. } else {
  553. $result = false;
  554. }
  555. } else {
  556. $result = true;
  557. }
  558. }
  559. } else {
  560. $result = true;
  561. }
  562. return ($result);
  563. }
  564. /**
  565. * Checks is service accessible by current administrator
  566. *
  567. * @param int $serviceId
  568. *
  569. * @return bool
  570. */
  571. public function isMyService($serviceId) {
  572. $result = false;
  573. if ($this->branchesEnabled) {
  574. if (cfr('ROOT')) {
  575. $result = true;
  576. } else {
  577. if (cfr('BRANCHES')) {
  578. if (isset($this->myServices[$serviceId])) {
  579. $result = true;
  580. } else {
  581. $result = false;
  582. }
  583. } else {
  584. $result = true;
  585. }
  586. }
  587. } else {
  588. $result = true;
  589. }
  590. return ($result);
  591. }
  592. /**
  593. * Checks is branch accessible by current administrator
  594. *
  595. * @param int $branchId
  596. *
  597. * @return bool
  598. */
  599. public function isMyBranch($branchId) {
  600. $result = false;
  601. if ($this->branchesEnabled) {
  602. if (cfr('ROOT')) {
  603. $result = true;
  604. } else {
  605. if (cfr('BRANCHES')) {
  606. if (isset($this->myBranches[$branchId])) {
  607. $result = true;
  608. } else {
  609. $result = false;
  610. }
  611. } else {
  612. $result = true;
  613. }
  614. }
  615. } else {
  616. $result = true;
  617. }
  618. return ($result);
  619. }
  620. /**
  621. * Returns user assigned branch
  622. *
  623. * @param string $login
  624. *
  625. * @return int
  626. */
  627. public function userGetBranch($login) {
  628. $result = '';
  629. if (isset($this->branchesLogins[$login])) {
  630. $result = $this->branchesLogins[$login];
  631. }
  632. return ($result);
  633. }
  634. /**
  635. * Returns user branch name by his login
  636. *
  637. * @param string $login
  638. *
  639. * @return string
  640. */
  641. public function userGetBranchName($login) {
  642. $result = '';
  643. $branchId = $this->userGetBranch($login);
  644. if (!empty($branchId)) {
  645. $result = $this->getBranchName($branchId);
  646. }
  647. return ($result);
  648. }
  649. /**
  650. * Perfoms city to branch assign
  651. *
  652. * @param int $branchId
  653. * @param int $cityId
  654. *
  655. * @return void
  656. */
  657. public function cityAssignBranch($branchId, $cityId) {
  658. $branchId = vf($branchId, 3);
  659. $cityId = vf($cityId, 3);
  660. if (isset($this->branches[$branchId])) {
  661. if (!empty($cityId)) {
  662. $query = "INSERT INTO `branchescities` (`id`,`branchid`,`cityid`) VALUES ";
  663. $query .= "(NULL,'" . $branchId . "','" . $cityId . "');";
  664. nr_query($query);
  665. log_register('BRANCH ASSIGN [' . $branchId . '] CITY [' . $cityId . ']');
  666. } else {
  667. throw new Exception(self::EX_NO_CITY);
  668. }
  669. } else {
  670. throw new Exception(self::EX_NO_BRANCH);
  671. }
  672. }
  673. /**
  674. * Performs deletion of city assignation to some branch
  675. *
  676. * @param int $branchId
  677. * @param int $cityId
  678. * @throws Exception
  679. *
  680. * @return void
  681. */
  682. public function cityDeassignBranch($branchId, $cityId) {
  683. $branchId = vf($branchId, 3);
  684. $cityId = vf($cityId, 3);
  685. if (isset($this->branches[$branchId])) {
  686. if (!empty($cityId)) {
  687. $query = "DELETE from `branchescities` WHERE `branchid`='" . $branchId . "' AND `cityid`='" . $cityId . "';";
  688. nr_query($query);
  689. log_register('BRANCH DEASSIGN [' . $branchId . '] CITY [' . $cityId . ']');
  690. } else {
  691. throw new Exception(self::EX_NO_CITY);
  692. }
  693. } else {
  694. throw new Exception(self::EX_NO_BRANCH);
  695. }
  696. }
  697. /**
  698. * Perfoms tariff to branch assign
  699. *
  700. * @param int $branchId
  701. * @param string $tariff
  702. *
  703. * @return void
  704. */
  705. public function tariffAssignBranch($branchId, $tariff) {
  706. $branchId = vf($branchId, 3);
  707. $tariffF = mysql_real_escape_string($tariff);
  708. if (isset($this->branches[$branchId])) {
  709. if (!empty($tariff)) {
  710. $query = "INSERT INTO `branchestariffs` (`id`,`branchid`,`tariff`) VALUES ";
  711. $query .= "(NULL,'" . $branchId . "','" . $tariffF . "');";
  712. nr_query($query);
  713. log_register('BRANCH ASSIGN [' . $branchId . '] TARIFF `' . $tariff . '`');
  714. } else {
  715. throw new Exception(self::EX_NO_TARIFF);
  716. }
  717. } else {
  718. throw new Exception(self::EX_NO_BRANCH);
  719. }
  720. }
  721. /**
  722. * Performs deletion of tariff assignation to some branch
  723. *
  724. * @param int $branchId
  725. * @param string $tariff
  726. * @throws Exception
  727. *
  728. * @return void
  729. */
  730. public function tariffDeassignBranch($branchId, $tariff) {
  731. $branchId = vf($branchId, 3);
  732. $tariffF = mysql_real_escape_string($tariff);
  733. if (isset($this->branches[$branchId])) {
  734. if (!empty($tariff)) {
  735. $query = "DELETE from `branchestariffs` WHERE `branchid`='" . $branchId . "' AND `tariff`='" . $tariffF . "';";
  736. nr_query($query);
  737. log_register('BRANCH DEASSIGN [' . $branchId . '] TARIFF `' . $tariff . '`');
  738. } else {
  739. throw new Exception(self::EX_NO_TARIFF);
  740. }
  741. } else {
  742. throw new Exception(self::EX_NO_BRANCH);
  743. }
  744. }
  745. /**
  746. * Perfoms service to branch assign
  747. *
  748. * @param int $branchId
  749. * @param int $serviceId
  750. *
  751. * @return void
  752. */
  753. public function serviceAssignBranch($branchId, $serviceId) {
  754. $branchId = vf($branchId, 3);
  755. $serviceId = vf($serviceId, 3);
  756. if (isset($this->branches[$branchId])) {
  757. if (!empty($serviceId)) {
  758. $query = "INSERT INTO `branchesservices` (`id`,`branchid`,`serviceid`) VALUES ";
  759. $query .= "(NULL,'" . $branchId . "','" . $serviceId . "');";
  760. nr_query($query);
  761. log_register('BRANCH ASSIGN [' . $branchId . '] SERVICE [' . $serviceId . ']');
  762. } else {
  763. throw new Exception(self::EX_NO_SERVICE);
  764. }
  765. } else {
  766. throw new Exception(self::EX_NO_BRANCH);
  767. }
  768. }
  769. /**
  770. * Performs deletion of service assignation to some branch
  771. *
  772. * @param int $branchId
  773. * @param int $serviceId
  774. * @throws Exception
  775. *
  776. * @return void
  777. */
  778. public function serviceDeassignBranch($branchId, $serviceId) {
  779. $branchId = vf($branchId, 3);
  780. $tariff = vf($serviceId, 3);
  781. if (isset($this->branches[$branchId])) {
  782. if (!empty($serviceId)) {
  783. $query = "DELETE from `branchesservices` WHERE `branchid`='" . $branchId . "' AND `serviceid`='" . $serviceId . "';";
  784. nr_query($query);
  785. log_register('BRANCH DEASSIGN [' . $branchId . '] SERVICE [' . $serviceId . ']');
  786. } else {
  787. throw new Exception(self::EX_NO_SERVICE);
  788. }
  789. } else {
  790. throw new Exception(self::EX_NO_BRANCH);
  791. }
  792. }
  793. /**
  794. * Renders branches module control panel interface
  795. *
  796. * @return string
  797. */
  798. public function panel() {
  799. $result = '';
  800. //hide control panel on user branch editing
  801. if (!wf_CheckGet(array('userbranch'))) {
  802. if (cfr('BRANCHES')) {
  803. $result .= wf_Link(self::URL_ME . '&userlist=true', wf_img('skins/ukv/users.png') . ' ' . __('Users'), false, 'ubButton') . ' ';
  804. if (cfr('BRANCHESREG')) {
  805. $result .= wf_Link('?module=userreg&branchesback=true', wf_img('skins/ukv/add.png') . ' ' . __('Users registration'), false, 'ubButton') . ' ';
  806. }
  807. if (cfr('BRANCHESFINREP')) {
  808. $result .= wf_Link(self::URL_ME . '&finreport=true', wf_img('skins/icon_dollar.gif') . ' ' . __('Finance report'), false, 'ubButton') . ' ';
  809. }
  810. if (cfr('BRANCHESSIGREP')) {
  811. $result .= wf_Link(self::URL_ME . '&sigreport=true', wf_img('skins/ukv/report.png') . ' ' . __('Signup report'), false, 'ubButton') . ' ';
  812. }
  813. }
  814. if (cfr('BRANCHESCONF')) {
  815. $result .= wf_Link(self::URL_ME . '&settings=true', wf_img('skins/icon_extended.png') . ' ' . __('Settings'), false, 'ubButton') . ' ';
  816. }
  817. }
  818. return ($result);
  819. }
  820. /**
  821. * Renders user list container
  822. *
  823. * @return string
  824. */
  825. public function renderUserList() {
  826. $result = '';
  827. //basic columns set
  828. $columns = array('Full address', 'Real Name', 'Branch', 'IP', 'Tariff', 'Active', 'Traffic', 'Balance', 'Credit');
  829. //some optional data columns here
  830. if ($this->altCfg['DN_ONLINE_DETECT']) {
  831. $trafficIndex = array_search('Traffic', $columns);
  832. zb_array_insert($columns, $trafficIndex, 'Online');
  833. }
  834. if ($this->altCfg['ONLINE_SHOW_CONTRACT_FIELD']) {
  835. $nameIndex = array_search('Real Name', $columns);
  836. zb_array_insert($columns, $nameIndex, 'Contract');
  837. }
  838. if (@$this->altCfg['ONLINE_SHOW_PHONES']) {
  839. $ipIndex = array_search('IP', $columns);
  840. zb_array_insert($columns, $ipIndex, 'Phones');
  841. }
  842. $result = wf_JqDtLoader($columns, self::URL_ME . '&userlist=true&ajaxuserlist=true', false, __('Users'), 50, '');
  843. return ($result);
  844. }
  845. /**
  846. * Returns branch name by its ID
  847. *
  848. * @param int $branchId
  849. *
  850. * @return string
  851. */
  852. public function getBranchName($branchId) {
  853. $result = '';
  854. if (isset($this->branches[$branchId])) {
  855. $result = $this->branches[$branchId]['name'];
  856. }
  857. return ($result);
  858. }
  859. /**
  860. * Returns array of available branches as branchid=>branchname
  861. *
  862. * @return array
  863. */
  864. public function getBranchesAvailable() {
  865. $result = array();
  866. if (!empty($this->branches)) {
  867. foreach ($this->branches as $io => $each) {
  868. $result[$each['id']] = $each['name'];
  869. }
  870. }
  871. return ($result);
  872. }
  873. /**
  874. * Builds and renders users list JSON data
  875. *
  876. * @return void
  877. */
  878. public function renderUserListJson() {
  879. $json = new wf_JqDtHelper();
  880. if (!empty($this->branchesLogins)) {
  881. $allAddress = zb_AddressGetFulladdresslistCached();
  882. $allRealNames = zb_UserGetAllRealnames();
  883. $allUserData = zb_UserGetAllStargazerDataAssoc();
  884. $dnFlag = ($this->altCfg['DN_ONLINE_DETECT']) ? true : false;
  885. $contractFlag = ($this->altCfg['ONLINE_SHOW_CONTRACT_FIELD']) ? true : false;
  886. $phonesFlag = (@$this->altCfg['ONLINE_SHOW_PHONES']) ? true : false;
  887. $ishimuraOption = MultiGen::OPTION_ISHIMURA;
  888. $ishimuraTable = MultiGen::NAS_ISHIMURA;
  889. $additionalTraffic = array();
  890. if (@$this->altCfg[$ishimuraOption]) {
  891. $query_hideki = "SELECT `login`,`D0`,`U0` from `" . $ishimuraTable . "` WHERE `month`='" . date("n") . "' AND `year`='" . curyear() . "'";
  892. $dataHideki = simple_queryall($query_hideki);
  893. if (!empty($dataHideki)) {
  894. foreach ($dataHideki as $io => $each) {
  895. $additionalTraffic[$each['login']] = $each['D0'] + $each['U0'];
  896. }
  897. }
  898. }
  899. if ($contractFlag) {
  900. $allContracts = zb_UserGetAllLoginContracts();
  901. }
  902. if ($phonesFlag) {
  903. $allUserPhones = zb_GetAllOnlineTabPhones();
  904. }
  905. foreach ($this->branchesLogins as $login => $branchId) {
  906. if ($this->isMyUser($login)) {
  907. if (isset($allUserData[$login])) {
  908. $tinet = 0;
  909. for ($classcounter = 0; $classcounter <= 9; $classcounter++) {
  910. $dc = 'D' . $classcounter . '';
  911. $uc = 'U' . $classcounter . '';
  912. $tinet = $tinet + ($allUserData[$login][$dc] + $allUserData[$login][$uc]);
  913. }
  914. //ishimura traffic mixing
  915. $currentAdditionalTraff = (isset($additionalTraffic[$login])) ? $additionalTraffic[$login] : 0;
  916. $tinet = $tinet + $currentAdditionalTraff;
  917. $userLinks = wf_Link(self::URL_TRAFFSTATS . $login, web_stats_icon()) . ' ';
  918. $userLinks .= wf_Link(self::URL_USERPROFILE . $login, web_profile_icon()) . ' ';
  919. if ($this->altCfg['FAST_CASH_LINK']) {
  920. $userLinks .= wf_Link(self::URL_ADDCASH . $login . '#cashfield', web_cash_icon()) . ' ';
  921. }
  922. @$userAddress = $allAddress[$login];
  923. @$userRealName = $allRealNames[$login];
  924. $activeFlag = ($allUserData[$login]['Cash'] >= -$allUserData[$login]['Credit']) ? web_bool_led(true) . ' ' . __('Yes') : web_bool_led(false) . ' ' . __('No');
  925. $data[] = $userLinks . ' ' . $userAddress;
  926. if ($contractFlag) {
  927. $data[] = @$allContracts[$login];
  928. }
  929. $data[] = $userRealName;
  930. $data[] = $this->getBranchName($branchId);
  931. if ($phonesFlag) {
  932. $data[] = @$allUserPhones[$login];
  933. }
  934. $data[] = $allUserData[$login]['IP'];
  935. $data[] = $allUserData[$login]['Tariff'];
  936. $data[] = $activeFlag;
  937. if ($dnFlag) {
  938. $onlineFlag = (file_exists('content/dn/' . $login)) ? web_star() . ' ' . __('Yes') : web_star_black() . ' ' . __('No');
  939. $data[] = $onlineFlag;
  940. }
  941. $data[] = zb_TraffToGb($tinet);
  942. $data[] = $allUserData[$login]['Cash'];
  943. $data[] = $allUserData[$login]['Credit'];
  944. $json->addRow($data);
  945. unset($data);
  946. }
  947. }
  948. }
  949. }
  950. $json->getJson();
  951. }
  952. /**
  953. * Renders finance report
  954. *
  955. * @return string
  956. */
  957. public function renderFinanceReport() {
  958. $result = '';
  959. $whereFilter = '';
  960. $totalSumm = 0;
  961. $totalCount = 0;
  962. $monthArr = months_array_localized();
  963. $allAddress = zb_AddressGetFulladdresslistCached();
  964. $allRealNames = zb_UserGetAllRealnames();
  965. $paymentTypes = zb_CashGetAllCashTypes();
  966. $allservicenames = zb_VservicesGetAllNamesLabeled();
  967. $inputs = wf_YearSelector('yearsel', __('Year') . ' ', false);
  968. $inputs .= wf_Selector('monthsel', $monthArr, __('Month') . ' ', date("m"), false);
  969. $inputs .= wf_Submit(__('Payments by month'));
  970. $monthForm = wf_Form('', 'POST', $inputs, 'glamour');
  971. $monthForm .= wf_CleanDiv();
  972. $inputsDate = wf_DatePickerPreset('datesel', curdate());
  973. $inputsDate .= wf_Submit(__('Payments by date'));
  974. $dateForm = wf_Form('', 'POST', $inputsDate, 'glamour');
  975. $dateForm .= wf_CleanDiv();
  976. $controlCells = wf_TableCell($monthForm);
  977. $controlCells .= wf_TableCell($dateForm);
  978. $controlRows = wf_TableRow($controlCells);
  979. $result .= wf_TableBody($controlRows, '60%', 0, '');
  980. $filterDate = (wf_CheckPost(array('yearsel'))) ? vf($_POST['yearsel'], 3) : curyear();
  981. if (wf_CheckPost(array('monthsel'))) {
  982. $filterDate .= '-' . vf($_POST['monthsel'], 3);
  983. } else {
  984. $filterDate .= '-' . date("m");
  985. }
  986. $whereFilter = "WHERE `date` LIKE '" . $filterDate . "-%' ";
  987. if (wf_CheckPost(array('datesel'))) {
  988. $filterDate = mysql_real_escape_string($_POST['datesel']);
  989. $whereFilter = "WHERE `date` LIKE '" . $filterDate . "%' ";
  990. }
  991. $query = "SELECT * from `payments` " . $whereFilter . " ORDER BY `id` DESC";
  992. $all = simple_queryall($query);
  993. if (!empty($all)) {
  994. $cells = wf_TableCell(__('ID'));
  995. $cells .= wf_TableCell(__('Date'));
  996. $cells .= wf_TableCell(__('Cash'));
  997. $cells .= wf_TableCell(__('Login'));
  998. $cells .= wf_TableCell(__('Full address'));
  999. $cells .= wf_TableCell(__('Real Name'));
  1000. $cells .= wf_TableCell(__('Branch'));
  1001. $cells .= wf_TableCell(__('Cash type'));
  1002. $cells .= wf_TableCell(__('Notes'));
  1003. $cells .= wf_TableCell(__('Admin'));
  1004. $rows = wf_TableRow($cells, 'row1');
  1005. foreach ($all as $io => $each) {
  1006. if (isset($this->branchesLogins[$each['login']])) {
  1007. if ($this->isMyUser($each['login'])) {
  1008. $cells = wf_TableCell($each['id']);
  1009. $cells .= wf_TableCell($each['date']);
  1010. $cells .= wf_TableCell($each['summ']);
  1011. $loginLink = wf_Link(self::URL_USERPROFILE . $each['login'], web_profile_icon() . ' ' . $each['login']);
  1012. $cells .= wf_TableCell($loginLink);
  1013. $cells .= wf_TableCell(@$allAddress[$each['login']]);
  1014. $cells .= wf_TableCell(@$allRealNames[$each['login']]);
  1015. $cells .= wf_TableCell(@$this->getBranchName($this->branchesLogins[$each['login']]));
  1016. $cells .= wf_TableCell(__($paymentTypes[$each['cashtypeid']]));
  1017. $cells .= wf_TableCell(zb_TranslatePaymentNote($each['note'], $allservicenames));
  1018. $cells .= wf_TableCell($each['admin']);
  1019. $rows .= wf_TableRow($cells, 'row3');
  1020. if ($each['summ'] > 0) {
  1021. $totalSumm += $each['summ'];
  1022. $totalCount++;
  1023. }
  1024. }
  1025. }
  1026. }
  1027. $result .= wf_tag('h3') . __('Payments by') . ' ' . $filterDate . wf_tag('h3', true);
  1028. $result .= wf_TableBody($rows, '100%', 0, 'sortable');
  1029. $result .= wf_tag('b');
  1030. $result .= __('Total money') . ': ' . $totalSumm;
  1031. $result .= wf_tag('br');
  1032. $result .= __('Payments count') . ': ' . $totalCount;
  1033. $result .= wf_tag('b', true);
  1034. } else {
  1035. $result .= $this->messages->getStyledMessage(__('Nothing found'), 'info');
  1036. }
  1037. return ($result);
  1038. }
  1039. /**
  1040. * Renders branch users signup report
  1041. *
  1042. * @return string
  1043. */
  1044. public function renderSignupReport() {
  1045. $result = '';
  1046. $showYear = (wf_CheckPost(array('yearsel'))) ? vf($_POST['yearsel'], 3) : curyear();
  1047. $query = "SELECT * from `userreg` WHERE `date` LIKE '" . $showYear . "-%' ORDER BY `id` DESC";
  1048. $all = simple_queryall($query);
  1049. $yearTmp = array();
  1050. $yearCount = 0;
  1051. $todayCount = 0;
  1052. $monthNames = months_array_localized();
  1053. $curDate = curdate();
  1054. $curMonth = curmonth();
  1055. $monthSignupsTmp = array();
  1056. //preparing per year stats array
  1057. foreach ($monthNames as $monthNum => $monthName) {
  1058. $yearTmp[$monthNum] = 0;
  1059. }
  1060. if (!empty($all)) {
  1061. foreach ($all as $io => $each) {
  1062. if (isset($this->branchesLogins[$each['login']])) {
  1063. if ($this->isMyUser($each['login'])) {
  1064. if (ispos($each['date'], $curDate)) {
  1065. $todayCount++;
  1066. }
  1067. if (ispos($each['date'], $curMonth)) {
  1068. $monthSignupsTmp[$each['id']] = $each;
  1069. }
  1070. $regTimestamp = strtotime($each['date']);
  1071. $regMonth = date("m", $regTimestamp);
  1072. $yearTmp[$regMonth] ++;
  1073. $yearCount++;
  1074. }
  1075. }
  1076. }
  1077. $result .= $this->messages->getStyledMessage(__('Today signups') . ': ' . wf_tag('b') . $todayCount . wf_tag('b', true), 'info');
  1078. $inputs = wf_YearSelector('yearsel', 'Year') . ' ';
  1079. $inputs .= wf_Submit(__('Show'));
  1080. $result .= wf_delimiter();
  1081. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  1082. $result .= wf_CleanDiv();
  1083. $cells = wf_TableCell('');
  1084. $cells .= wf_TableCell(__('Month'));
  1085. $cells .= wf_TableCell(__('Signups'));
  1086. $cells .= wf_TableCell(__('Visual'));
  1087. $rows = wf_TableRow($cells, 'row1');
  1088. foreach ($yearTmp as $eachMonth => $monthCount) {
  1089. $cells = wf_TableCell($eachMonth);
  1090. $cells .= wf_TableCell($monthNames[$eachMonth]);
  1091. $cells .= wf_TableCell($monthCount);
  1092. $cells .= wf_TableCell(web_bar($monthCount, $yearCount));
  1093. $rows .= wf_TableRow($cells, 'row3');
  1094. }
  1095. $result .= wf_tag('br') . wf_tag('b') . __('User signups by year') . ' ' . $showYear . wf_tag('b', true) . wf_tag('br');
  1096. $result .= wf_TableBody($rows, '100%', 0, 'sortable');
  1097. $result .= wf_tag('b') . __('Total') . ': ' . $yearCount . wf_tag('b', true);
  1098. if (!empty($monthSignupsTmp)) {
  1099. $cells = wf_TableCell(__('ID'));
  1100. $cells .= wf_TableCell(__('Date'));
  1101. $cells .= wf_TableCell(__('Administrator'));
  1102. $cells .= wf_TableCell(__('Login'));
  1103. $cells .= wf_TableCell(__('Branch'));
  1104. $cells .= wf_TableCell(__('Full address'));
  1105. $rows = wf_TableRow($cells, 'row1');
  1106. foreach ($monthSignupsTmp as $io => $each) {
  1107. $cells = wf_TableCell($each['id']);
  1108. $cells .= wf_TableCell($each['date']);
  1109. $cells .= wf_TableCell($each['admin']);
  1110. $cells .= wf_TableCell($each['login']);
  1111. $cells .= wf_TableCell(@$this->getBranchName($this->branchesLogins[$each['login']]));
  1112. $userLink = wf_Link(self::URL_USERPROFILE . $each['login'], web_profile_icon()) . ' ';
  1113. $cells .= wf_TableCell($userLink . $each['address']);
  1114. $rows .= wf_TableRow($cells, 'row3');
  1115. }
  1116. $result .= wf_tag('br') . wf_tag('b') . __('Current month user signups') . wf_tag('b', true) . wf_tag('br');
  1117. $result .= wf_TableBody($rows, '100%', 0, 'sortable');
  1118. }
  1119. } else {
  1120. $result = $this->messages->getStyledMessage(__('Nothing found'), 'info');
  1121. }
  1122. return ($result);
  1123. }
  1124. /**
  1125. * Returns branch editing form
  1126. *
  1127. * @param int $branchId
  1128. *
  1129. * @return string
  1130. */
  1131. protected function renderBranchEditForm($branchId) {
  1132. $branchId = vf($branchId, 3);
  1133. $result = '';
  1134. if (isset($this->branches[$branchId])) {
  1135. $inputs = wf_HiddenInput('editbranch', 'true');
  1136. $inputs .= wf_HiddenInput('editbranchid', $branchId);
  1137. $inputs .= wf_TextInput('editbranchname', __('Name'), $this->branches[$branchId]['name'], true);
  1138. $inputs .= wf_Submit(__('Save'));
  1139. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  1140. } else {
  1141. $result .= self::EX_NO_BRANCH;
  1142. }
  1143. return ($result);
  1144. }
  1145. /**
  1146. * Renders list of available branches and reqired controls for its management
  1147. *
  1148. * @return string
  1149. */
  1150. protected function renderBranchesConfigForm() {
  1151. $result = '';
  1152. if (!empty($this->branches)) {
  1153. $cells = wf_TableCell(__('ID'));
  1154. $cells .= wf_TableCell(__('Name'));
  1155. $cells .= wf_TableCell(__('Actions'));
  1156. $rows = wf_TableRow($cells, 'row1');
  1157. foreach ($this->branches as $io => $each) {
  1158. $cells = wf_TableCell($each['id']);
  1159. $cells .= wf_TableCell($each['name']);
  1160. $actControls = wf_JSAlert(self::URL_ME . '&settings=true&deletebranch=' . $each['id'], web_delete_icon(), $this->messages->getDeleteAlert());
  1161. $actControls .= wf_modalAuto(web_edit_icon(), __('Edit'), $this->renderBranchEditForm($each['id']), '') . ' ';
  1162. $cells .= wf_TableCell($actControls);
  1163. $rows .= wf_TableRow($cells, 'row3');
  1164. }
  1165. $result .= wf_TableBody($rows, '100%', 0, 'sortable');
  1166. } else {
  1167. $result .= $this->messages->getStyledMessage(__('No branches available'), 'warning');
  1168. }
  1169. $inputs = wf_HiddenInput('newbranch', 'true');
  1170. $inputs .= wf_TextInput('newbranchname', __('New branch name'), '', false) . ' ';
  1171. $inputs .= wf_Submit(__('Create'));
  1172. $createForm = wf_Form('', 'POST', $inputs, 'glamour');
  1173. $result .= $createForm;
  1174. return ($result);
  1175. }
  1176. /**
  1177. * Returns branches admins assignation list and config form
  1178. *
  1179. * @return string
  1180. */
  1181. protected function renderAdminConfigForm() {
  1182. $result = '';
  1183. if (!empty($this->branches)) {
  1184. if (!empty($this->branchesAdmins)) {
  1185. $cells = wf_TableCell(__('ID'));
  1186. $cells .= wf_TableCell(__('Branch'));
  1187. $cells .= wf_TableCell(__('Admin'));
  1188. $cells .= wf_TableCell(__('Actions'));
  1189. $rows = wf_TableRow($cells, 'row1');
  1190. foreach ($this->branchesAdmins as $io => $each) {
  1191. $cells = wf_TableCell($each['id']);
  1192. $cells .= wf_TableCell($this->getBranchName($each['branchid']));
  1193. $cells .= wf_TableCell($each['admin']);
  1194. $actControls = wf_JSAlert(self::URL_ME . '&settings=true&deleteadmin=' . $each['admin'] . '&adminbranchid=' . $each['branchid'], web_delete_icon(), $this->messages->getDeleteAlert());
  1195. $cells .= wf_TableCell($actControls);
  1196. $rows .= wf_TableRow($cells, 'row3');
  1197. }
  1198. $result .= wf_TableBody($rows, '100%', 0, 'sortable');
  1199. } else {
  1200. $result .= $this->messages->getStyledMessage(__('No branches admins available'), 'warning');
  1201. $result .= wf_tag('br');
  1202. }
  1203. //admin assign form
  1204. $allAdmins = rcms_scandir('content/users/');
  1205. $adminsTmp = array();
  1206. if (!empty($allAdmins)) {
  1207. foreach ($allAdmins as $io => $each) {
  1208. $adminsTmp[$each] = $each;
  1209. }
  1210. }
  1211. $branchesTmp = array();
  1212. if (!empty($this->branches)) {
  1213. foreach ($this->branches as $io => $each) {
  1214. $branchesTmp[$io] = $each['name'];
  1215. }
  1216. }
  1217. $inputs = wf_Selector('newadminbranch', $branchesTmp, __('Branch'), '', false) . ' ';
  1218. $inputs .= wf_Selector('newadminlogin', $adminsTmp, __('Admin'), '', false) . ' ';
  1219. $inputs .= wf_Submit(__('Assign'));
  1220. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  1221. }
  1222. return ($result);
  1223. }
  1224. /**
  1225. * Returns branches=>cities assign list and config form
  1226. *
  1227. * @return string
  1228. */
  1229. public function renderCitiesConfigForm() {
  1230. $result = '';
  1231. //manually preloading cities bingings
  1232. $this->loadCities();
  1233. if (!empty($this->branches)) {
  1234. if (!empty($this->branchesCities)) {
  1235. $cells = wf_TableCell(__('ID'));
  1236. $cells .= wf_TableCell(__('Branch'));
  1237. $cells .= wf_TableCell(__('City'));
  1238. $cells .= wf_TableCell(__('Actions'));
  1239. $rows = wf_TableRow($cells, 'row1');
  1240. foreach ($this->branchesCities as $io => $each) {
  1241. $cells = wf_TableCell($io);
  1242. $cells .= wf_TableCell($this->getBranchName($each['branchid']));
  1243. $cells .= wf_TableCell($this->allCityNames[$each['cityid']]);
  1244. $actControls = wf_JSAlert(self::URL_ME . '&settings=true&deletecity=' . $each['cityid'] . '&citybranchid=' . $each['branchid'], web_delete_icon(), $this->messages->getDeleteAlert());
  1245. $cells .= wf_TableCell($actControls);
  1246. $rows .= wf_TableRow($cells, 'row3');
  1247. }
  1248. $result .= wf_TableBody($rows, '100%', 0, 'sortable');
  1249. } else {
  1250. $result .= $this->messages->getStyledMessage(__('No branches cities assigns available'), 'warning');
  1251. }
  1252. //assign form
  1253. $branchesTmp = array();
  1254. if (!empty($this->branches)) {
  1255. foreach ($this->branches as $io => $each) {
  1256. $branchesTmp[$io] = $each['name'];
  1257. }
  1258. }
  1259. $inputs = wf_Selector('newcitybranchid', $branchesTmp, __('Branch'), '', false) . ' ';
  1260. $inputs .= wf_Selector('newcityid', $this->allCityNames, __('City'), '', false) . ' ';
  1261. $inputs .= wf_Submit(__('Assign'));
  1262. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  1263. }
  1264. return ($result);
  1265. }
  1266. /**
  1267. * Returns branches=>tariffs assign list and config form
  1268. *
  1269. * @return string
  1270. */
  1271. public function renderTariffsConfigForm() {
  1272. $result = '';
  1273. //manually preloading tariffs bingings
  1274. $this->loadTariffs();
  1275. if (!empty($this->branches)) {
  1276. if (!empty($this->branchesTariffs)) {
  1277. $cells = wf_TableCell(__('ID'));
  1278. $cells .= wf_TableCell(__('Branch'));
  1279. $cells .= wf_TableCell(__('Tariff'));
  1280. $cells .= wf_TableCell(__('Actions'));
  1281. $rows = wf_TableRow($cells, 'row1');
  1282. foreach ($this->branchesTariffs as $io => $each) {
  1283. $cells = wf_TableCell($io);
  1284. $cells .= wf_TableCell($this->getBranchName($each['branchid']));
  1285. $cells .= wf_TableCell($each['tariff']);
  1286. $actControls = wf_JSAlert(self::URL_ME . '&settings=true&deletetariff=' . $each['tariff'] . '&tariffbranchid=' . $each['branchid'], web_delete_icon(), $this->messages->getDeleteAlert());
  1287. $cells .= wf_TableCell($actControls);
  1288. $rows .= wf_TableRow($cells, 'row3');
  1289. }
  1290. $result .= wf_TableBody($rows, '100%', 0, 'sortable');
  1291. } else {
  1292. $result .= $this->messages->getStyledMessage(__('No branches tariffs assigns available'), 'warning');
  1293. }
  1294. //assign form
  1295. $branchesTmp = array();
  1296. if (!empty($this->branches)) {
  1297. foreach ($this->branches as $io => $each) {
  1298. $branchesTmp[$io] = $each['name'];
  1299. }
  1300. }
  1301. $tariffsTmp = array();
  1302. if (!empty($this->allTariffs)) {
  1303. foreach ($this->allTariffs as $tariffName => $tariffFee) {
  1304. $tariffsTmp[$tariffName] = $tariffName;
  1305. }
  1306. }
  1307. $inputs = wf_Selector('newtariffbranchid', $branchesTmp, __('Branch'), '', false) . ' ';
  1308. $inputs .= wf_Selector('newtariffname', $tariffsTmp, __('Tariff'), '', false) . ' ';
  1309. $inputs .= wf_Submit(__('Assign'));
  1310. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  1311. }
  1312. return ($result);
  1313. }
  1314. /**
  1315. * Returns branches=>services assign list and config form
  1316. *
  1317. * @return string
  1318. */
  1319. public function renderServicesConfigForm() {
  1320. $result = '';
  1321. //manually preloading services bindings
  1322. $this->loadServices();
  1323. if (!empty($this->branches)) {
  1324. if (!empty($this->branchesServices)) {
  1325. $cells = wf_TableCell(__('ID'));
  1326. $cells .= wf_TableCell(__('Branch'));
  1327. $cells .= wf_TableCell(__('Service'));
  1328. $cells .= wf_TableCell(__('Actions'));
  1329. $rows = wf_TableRow($cells, 'row1');
  1330. foreach ($this->branchesServices as $io => $each) {
  1331. $cells = wf_TableCell($io);
  1332. $cells .= wf_TableCell($this->getBranchName($each['branchid']));
  1333. $cells .= wf_TableCell($this->allServices[$each['serviceid']]);
  1334. $actControls = wf_JSAlert(self::URL_ME . '&settings=true&deleteservice=' . $each['serviceid'] . '&servicebranchid=' . $each['branchid'], web_delete_icon(), $this->messages->getDeleteAlert());
  1335. $cells .= wf_TableCell($actControls);
  1336. $rows .= wf_TableRow($cells, 'row3');
  1337. }
  1338. $result .= wf_TableBody($rows, '100%', 0, 'sortable');
  1339. } else {
  1340. $result .= $this->messages->getStyledMessage(__('No branches services assigns available'), 'warning');
  1341. }
  1342. //assign form
  1343. $branchesTmp = array();
  1344. if (!empty($this->branches)) {
  1345. foreach ($this->branches as $io => $each) {
  1346. $branchesTmp[$io] = $each['name'];
  1347. }
  1348. }
  1349. $inputs = wf_Selector('newservicebranchid', $branchesTmp, __('Branch'), '', false) . ' ';
  1350. $inputs .= wf_Selector('newserviceid', $this->allServices, __('Service'), '', false) . ' ';
  1351. $inputs .= wf_Submit(__('Assign'));
  1352. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  1353. }
  1354. return ($result);
  1355. }
  1356. /**
  1357. * Returns branches management form
  1358. *
  1359. * @return string
  1360. */
  1361. public function renderSettingsBranches() {
  1362. $result = '';
  1363. if (cfr('BRANCHESCONF')) {
  1364. $result .= wf_tag('h3') . __('Branches') . wf_tag('h3', true);
  1365. $result .= $this->renderBranchesConfigForm();
  1366. $result .= wf_tag('h3') . __('Administrators') . wf_tag('h3', true);
  1367. $result .= $this->renderAdminConfigForm();
  1368. $result .= wf_tag('h3') . __('Cities') . wf_tag('h3', true);
  1369. $result .= $this->renderCitiesConfigForm();
  1370. $result .= wf_tag('h3') . __('Tariffs') . wf_tag('h3', true);
  1371. $result .= $this->renderTariffsConfigForm();
  1372. $result .= wf_tag('h3') . __('Services') . wf_tag('h3', true);
  1373. $result .= $this->renderServicesConfigForm();
  1374. } else {
  1375. $result = $this->messages->getStyledMessage(__('Access denied'), 'error');
  1376. }
  1377. return ($result);
  1378. }
  1379. /**
  1380. * Contols user module branch access rights
  1381. *
  1382. * @return void
  1383. */
  1384. public function accessControl() {
  1385. if (($this->myLogin != 'guest') AND ( $this->myLogin != 'external')) {
  1386. if ($this->branchesEnabled) {
  1387. $controlVars = array('username', 'login', 'inetlogin', 'userlogin');
  1388. foreach ($controlVars as $io => $each) {
  1389. if (wf_CheckGet(array($each))) {
  1390. if (!$this->isMyUser($_GET[$each])) {
  1391. log_register('BRANCH ACCESS FAIL (' . $_GET[$each] . ') ADMIN {' . $this->myLogin . '}');
  1392. die('Access denied');
  1393. }
  1394. }
  1395. }
  1396. }
  1397. }
  1398. }
  1399. /**
  1400. * Returns selector widget for accessible branches
  1401. *
  1402. * @param string $name
  1403. * @param int $selected
  1404. *
  1405. * @return string
  1406. */
  1407. public function branchSelector($name, $selected = '') {
  1408. $result = '';
  1409. if (!empty($this->branches)) {
  1410. $params = array();
  1411. foreach ($this->branches as $branchId => $branchData) {
  1412. if ($this->isMyBranch($branchId)) {
  1413. $params[$branchId] = $this->getBranchName($branchId);
  1414. }
  1415. }
  1416. if (!empty($params)) {
  1417. $result = wf_Selector($name, $params, $result, $selected, false);
  1418. }
  1419. }
  1420. return ($result);
  1421. }
  1422. /**
  1423. * Renders users assign/editing branch form
  1424. *
  1425. * @param string $userLogin
  1426. *
  1427. * @return string
  1428. */
  1429. public function renderUserBranchFrom($userLogin) {
  1430. $result = '';
  1431. $allUserAddress = zb_AddressGetFullCityaddresslist();
  1432. $currentBranchId = $this->userGetBranch($userLogin);
  1433. $currentBranchName = $this->getBranchName($currentBranchId);
  1434. $cells = wf_TableCell(__('User'), '', 'row2');
  1435. $cells .= wf_TableCell(@$allUserAddress[$userLogin] . ' (' . $userLogin . ')');
  1436. $rows = wf_TableRow($cells, 'row3');
  1437. $cells = wf_TableCell(__('Current branch'), '', 'row2');
  1438. $cells .= wf_TableCell($currentBranchName);
  1439. $rows .= wf_TableRow($cells, 'row3');
  1440. $branchControls = $this->branchSelector('newuserbranchid', $currentBranchId);
  1441. if (cfr('ROOT') OR cfr('BRANCHESUSERMOD')) {
  1442. $branchControls .= ' ' . wf_CheckInput('newuserbranchdelete', __('Delete branch'), false, false);
  1443. }
  1444. $cells = wf_TableCell(__('New branch'), '', 'row2');
  1445. $cells .= wf_TableCell($branchControls);
  1446. $rows .= wf_TableRow($cells, 'row3');
  1447. $inputs = wf_TableBody($rows, '100%', 0, '');
  1448. $inputs .= wf_HiddenInput('newuserbranchlogin', $userLogin);
  1449. $inputs .= wf_Submit(__('Change'));
  1450. $result .= wf_Form('', 'POST', $inputs, '');
  1451. $result .= wf_delimiter();
  1452. $result .= web_UserControls($userLogin);
  1453. return ($result);
  1454. }
  1455. /**
  1456. * Catches and performs user branch changing if required
  1457. *
  1458. * @return void
  1459. */
  1460. public function catchUserBranchEditRequest() {
  1461. $result = '';
  1462. if (wf_CheckPost(array('newuserbranchid', 'newuserbranchlogin'))) {
  1463. $allUsers = zb_UserGetAllStargazerDataAssoc();
  1464. $userLogin = $_POST['newuserbranchlogin'];
  1465. if (isset($allUsers[$userLogin])) {
  1466. $currentBranchId = $this->userGetBranch($userLogin);
  1467. $newBranchId = $_POST['newuserbranchid'];
  1468. //change is really reqired?
  1469. if (!wf_CheckPost(array('newuserbranchdelete'))) {
  1470. if ($currentBranchId != $newBranchId) {
  1471. if ($this->isMyBranch($newBranchId)) {
  1472. $this->userDeleteBranch($userLogin);
  1473. $this->userAssignBranch($newBranchId, $userLogin);
  1474. rcms_redirect(self::URL_ME . '&userbranch=' . $userLogin);
  1475. } else {
  1476. $result = $this->messages->getStyledMessage(__('Access denied'), 'error');
  1477. }
  1478. }
  1479. } else {
  1480. if (cfr('ROOT') OR cfr('BRANCHESUSERMOD')) {
  1481. $this->userDeleteBranch($userLogin);
  1482. rcms_redirect(self::URL_ME . '&userbranch=' . $userLogin);
  1483. } else {
  1484. $result = $this->messages->getStyledMessage(__('Access denied'), 'error');
  1485. }
  1486. }
  1487. } else {
  1488. $result = $this->messages->getStyledMessage(__('No such user available'), 'error');
  1489. }
  1490. }
  1491. //something happens
  1492. if (!empty($result)) {
  1493. show_window(__('Result'), $result);
  1494. }
  1495. }
  1496. }
  1497. ?>