UsersHelper.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. <?php
  2. Lib::loadClass('UsersLdapHelper');
  3. class UsersHelper {
  4. public static function _parse_query(&$params) {
  5. $sql_where = '';
  6. //if (!empty($params)) {
  7. $sql_where_and_arr = array();
  8. $sql_where_and_arr[] = "a.`A_STATUS` in('WAITING','NORMAL','MONITOR','WARNING')";
  9. $sql_where_and_arr[] = "a.`ADM_ADMIN_DESC`!='Kandydat'";
  10. //} else {
  11. // $sql_where = "1=1";
  12. //}
  13. if (isset($params['ADM_ADMIN_LEVEL'])) {
  14. $adm_lvl = V::get('ADM_ADMIN_LEVEL', 0, $params, 'int');
  15. $sql_where_and_arr[] = "a.`ADM_ADMIN_LEVEL`='{$adm_lvl}'";
  16. }
  17. if (!empty($params['group'])) {
  18. $sql_where_and_arr[] = "(select up.`ID`
  19. from `CRM_AUTH_PROFILE` as up
  20. where
  21. up.`REMOTE_TABLE`='ADMIN_USERS'
  22. and up.`A_STATUS` in('WAITING', 'NORMAL')
  23. and up.`REMOTE_ID`=a.`ID`
  24. and up.`ID_ZASOB`='{$params['group']}'
  25. limit 1
  26. )>0";
  27. }
  28. $sql_where = implode(" and ", $sql_where_and_arr);
  29. return $sql_where;
  30. }
  31. public static function get_users_total($params = array()) {
  32. $total = 0;
  33. $db = DB::getDB();
  34. $sql_where = UsersHelper::_parse_query($params);
  35. $sql = "select count(1) as cnt
  36. from `ADMIN_USERS` as a
  37. where {$sql_where}
  38. ";
  39. $res = $db->query($sql);
  40. if ($r = $db->fetch($res)) {
  41. $total = $r->cnt;
  42. }
  43. return $total;
  44. }
  45. public static function &get_users_list($params = array(), $limit = 10, $limit_start = 0, $order_by = '', $order_dir = '') {
  46. $ret = array();
  47. $sql_where = UsersHelper::_parse_query($params);
  48. $sql_limit = "";
  49. if ($limit > 0) {
  50. $sql_limit = "limit {$limit}";
  51. if ($limit_start > 0) $sql_limit .= " offset {$limit_start}";
  52. }
  53. $allowed_order_by = array();
  54. $allowed_order_by[] = 'ID';
  55. $allowed_order_by[] = 'ADM_NAME';
  56. $sql_order_by = "";
  57. if (in_array($order_by, $allowed_order_by)) {
  58. $sql_order_by .= " order by {$order_by}";
  59. if (in_array($order_dir, array('DESC','ASC'))) {
  60. $sql_order_by .= " {$order_dir}";
  61. }
  62. }
  63. // 'WAITING','NORMAL','MONITOR','WARNING','OFF_SOFT','OFF_HARD','DELETED'
  64. $db = DB::getDB();
  65. $sql = "SELECT a.`ID`
  66. , a.`A_STATUS`
  67. , a.`ADM_ACCOUNT`
  68. , a.`ADM_ADMIN_LEVEL`
  69. , a.`ADM_ADMIN_DESC` -- stanowisko
  70. , a.`ADM_NAME` -- imie i nazwisko
  71. , a.`ADM_NIP` -- NIP
  72. , a.`ADM_PESEL` -- nr. PESEL
  73. , a.`ADM_PHONE` -- nr. telefonu
  74. , a.`EMAIL` as EMAIL -- adres email
  75. , a.`ADM_OTHER_INFO`
  76. , group_concat(', ',tx.T_TELBOX_NAME) as T_TELBOX_NEIGHBOUR_IN_ID_NAME,
  77. a.L_APPOITMENT_USER , a.A_ADM_COMPANY, a.A_CLASSIFIED
  78. from `ADMIN_USERS` as a
  79. left join CRM_AUTH_PROFILE as cp on (cp.REMOTE_TABLE='ADMIN_USERS' and cp.REMOTE_ID=a.ID)
  80. left join TELBOXES as tx on (tx.ID=cp.T_TELBOX_NEIGHBOUR_IN_ID)
  81. where {$sql_where}
  82. group by a.ID
  83. {$sql_order_by}
  84. {$sql_limit}
  85. ";
  86. $res = $db->query($sql);
  87. while ($r = $db->fetch($res)) {
  88. $ret[$r->ID] = $r;
  89. }
  90. return $ret;
  91. }
  92. public static function get_user_by_id( $id ) {
  93. $ret = null;
  94. if ($id <= 0) return $ret;
  95. $sql_where = "a.`ID`='{$id}'";
  96. $db = DB::getDB();
  97. $sql = "SELECT a.`ID`
  98. , a.`A_STATUS`
  99. , a.`ADM_ACCOUNT`
  100. , a.`ADM_ADMIN_LEVEL`
  101. , a.`ADM_ADMIN_DESC` -- stanowisko
  102. , a.`ADM_NAME` -- imie i nazwisko
  103. , a.`ADM_NIP` -- NIP
  104. , a.`ADM_PESEL` -- nr. PESEL
  105. , a.`ADM_PHONE` -- nr. telefonu
  106. , a.`EMAIL` as EMAIL -- adres email
  107. , tx.T_TELBOX_NAME
  108. from `ADMIN_USERS` as a
  109. left join CRM_AUTH_PROFILE as cp on (cp.REMOTE_TABLE='ADMIN_USERS' and cp.REMOTE_ID=a.ID)
  110. left join TELBOXES as tx on (tx.ID=cp.T_TELBOX_NEIGHBOUR_IN_ID)
  111. where {$sql_where}
  112. ";
  113. $res = $db->query($sql);
  114. if ($r = $db->fetch($res)) {
  115. $ret = $r;
  116. }
  117. return $ret;
  118. }
  119. public static function getUserByEmail($email) {
  120. if (empty($email)) return null;
  121. $usr = null;
  122. $db = DB::getDB();
  123. $sql = "SELECT a.`ID`
  124. , a.`A_STATUS`
  125. , a.`ADM_ACCOUNT`
  126. , a.`ADM_ADMIN_LEVEL`
  127. , a.`ADM_ADMIN_DESC` -- stanowisko
  128. , a.`ADM_NAME` -- imie i nazwisko
  129. , a.`ADM_NIP` -- NIP
  130. , a.`ADM_PESEL` -- nr. PESEL
  131. , a.`ADM_PHONE` -- nr. telefonu
  132. , a.`EMAIL` -- adres email
  133. , a.`EMPLOYEE_TYPE` -- EMPLOYEE_TYPE (ldap:employeeType)
  134. , a.`A_SYNC_LDAP_DATE`
  135. from `ADMIN_USERS` as a
  136. where a.`EMAIL`='{$email}'
  137. ";
  138. $res = $db->query($sql);
  139. if ($r = $db->fetch($res)) {
  140. $usr = $r;
  141. }
  142. return $usr;
  143. }
  144. public static function getUserByName($userName) {
  145. if (empty($userName)) return null;
  146. $user = null;
  147. $db = DB::getDB();
  148. $sql = "SELECT a.`ID`
  149. , a.`A_STATUS`
  150. , a.`ADM_ACCOUNT`
  151. , a.`ADM_ADMIN_LEVEL`
  152. , a.`ADM_ADMIN_DESC` -- stanowisko
  153. , a.`ADM_NAME` -- imie i nazwisko
  154. , a.`ADM_NIP` -- NIP
  155. , a.`ADM_PESEL` -- nr. PESEL
  156. , a.`ADM_PHONE` -- nr. telefonu
  157. , a.`EMAIL` -- adres email
  158. , a.`EMPLOYEE_TYPE` -- EMPLOYEE_TYPE (ldap:employeeType)
  159. , a.`A_SYNC_LDAP_DATE`
  160. from `ADMIN_USERS` as a
  161. where a.`ADM_ACCOUNT`='{$userName}'
  162. ";
  163. $res = $db->query($sql);
  164. if ($r = $db->fetch($res)) {
  165. $user = $r;
  166. }
  167. return $user;
  168. }
  169. public static function getUsersByGroupId($zasobId) {
  170. if (empty($zasobId)) return null;
  171. $users = array();
  172. $db = DB::getDB();
  173. $sql = "select u.`ID`
  174. , u.`A_STATUS`
  175. , u.`ADM_ACCOUNT`
  176. , u.`ADM_ADMIN_LEVEL`
  177. , u.`ADM_ADMIN_DESC` -- stanowisko
  178. , u.`ADM_NAME` -- imie i nazwisko
  179. , u.`ADM_NIP` -- NIP
  180. , u.`ADM_PESEL` -- nr. PESEL
  181. , u.`ADM_PHONE` -- nr. telefonu
  182. , u.`EMAIL` -- adres email
  183. , u.`EMPLOYEE_TYPE` -- EMPLOYEE_TYPE (ldap:employeeType)
  184. , u.`A_SYNC_LDAP_DATE`
  185. from `CRM_AUTH_PROFILE` as up
  186. left join `ADMIN_USERS` as u on (u.`ID`=up.`REMOTE_ID`)
  187. where
  188. up.`ID_ZASOB`='{$zasobId}'
  189. and up.`REMOTE_TABLE`='ADMIN_USERS'
  190. and up.`A_STATUS` in('WAITING', 'NORMAL')
  191. and u.`A_STATUS` in('WAITING', 'NORMAL')
  192. group by u.`ID`
  193. ";
  194. $res = $db->query($sql);
  195. while ($r = $db->fetch($res)) {
  196. $users[$r->ID] = $r;
  197. }
  198. return $users;
  199. }
  200. public static function getUsersByGroupsIds($zasobyIds, $ignoreUsrIds = array()) {
  201. if (empty($zasobyIds)) return null;
  202. $users = array();
  203. $db = DB::getDB();
  204. $sqlIds = " and up.`ID_ZASOB` in(" . implode(",", $zasobyIds) . ")";
  205. $sqlIgnore = "";
  206. if (!empty($ignoreUsrIds)) {
  207. $sqlIgnore = " and u.`ID` not in(" . implode(",", $ignoreUsrIds) . ")";
  208. }
  209. $sql = "select u.`ID`
  210. , u.`A_STATUS`
  211. , u.`ADM_ACCOUNT`
  212. , u.`ADM_ADMIN_LEVEL`
  213. , u.`ADM_ADMIN_DESC` -- stanowisko
  214. , u.`ADM_NAME` -- imie i nazwisko
  215. , u.`ADM_NIP` -- NIP
  216. , u.`ADM_PESEL` -- nr. PESEL
  217. , u.`ADM_PHONE` -- nr. telefonu
  218. , u.`EMAIL` -- adres email
  219. , u.`EMPLOYEE_TYPE` -- EMPLOYEE_TYPE (ldap:employeeType)
  220. , u.`A_SYNC_LDAP_DATE`
  221. from `CRM_AUTH_PROFILE` as up
  222. left join `ADMIN_USERS` as u on (u.`ID`=up.`REMOTE_ID`)
  223. where up.`REMOTE_TABLE`='ADMIN_USERS'
  224. and up.`A_STATUS` in('WAITING', 'NORMAL')
  225. and u.`A_STATUS` in('WAITING', 'NORMAL')
  226. {$sqlIds}
  227. {$sqlIgnore}
  228. group by u.`ID`
  229. ";
  230. $res = $db->query($sql);
  231. while ($r = $db->fetch($res)) {
  232. $users[$r->ID] = $r;
  233. }
  234. return $users;
  235. }
  236. public static function get_group_list() {
  237. static $_groups;
  238. if (!$_groups) {
  239. $_groups = array();
  240. $db = DB::getDB();
  241. $sql = "select z.`ID`, z.`DESC`, z.`OPIS`
  242. from `CRM_LISTA_ZASOBOW` as z
  243. where
  244. z.`TYPE` in('STANOWISKO','PODMIOT')
  245. order by z.`DESC`
  246. ";
  247. $res = $db->query($sql);
  248. while ($r = $db->fetch($res)) {
  249. $_groups[$r->ID] = $r;
  250. }
  251. }
  252. return $_groups;
  253. }
  254. public static function get_localisation_list() {
  255. static $_groups;
  256. if (!$_groups) {
  257. $_groups = array();
  258. $db = DB::getDB();
  259. $sql = "select tx.`ID`, tx.`T_TELBOX_NAME`, tx.`T_TELBOX_TYPE`
  260. from `TELBOXES` as tx
  261. where
  262. tx.`A_STATUS`!='DELETED'
  263. order by tx.`T_TELBOX_NAME`
  264. ";
  265. $res = $db->query($sql);
  266. while ($r = $db->fetch($res)) {
  267. $_groups[$r->ID] = $r;
  268. }
  269. }
  270. return $_groups;
  271. }
  272. public static function add_groups_to_user_list(&$items) {
  273. if (empty($items)) return;
  274. $user_list_id = array();
  275. foreach ($items as $k_ind => $v_item) {
  276. $items[$k_ind]->groups = array();
  277. $user_list_id[] = "'{$v_item->ID}'";
  278. }
  279. if (empty($user_list_id)) return;
  280. $db = DB::getDB();
  281. $sql = "select z.`ID`, z.`DESC`, z.`OPIS`
  282. , up.`REMOTE_ID`
  283. from `CRM_AUTH_PROFILE` as up
  284. left join `CRM_LISTA_ZASOBOW` as z on(z.`ID`=up.`ID_ZASOB`)
  285. where
  286. up.`REMOTE_ID` in (" . implode(",", $user_list_id) . ")
  287. and up.`A_STATUS` in('WAITING', 'NORMAL')
  288. and up.`REMOTE_TABLE`='ADMIN_USERS'
  289. and z.`ID` is not null
  290. and z.`TYPE` in('STANOWISKO','PODMIOT')
  291. ";
  292. $res = $db->query($sql);
  293. while ($r = $db->fetch($res)) {
  294. $items[$r->REMOTE_ID]->groups[$r->ID] = $r->DESC;//array('DESC'=>$r->DESC , 'OPIS'=>$r->OPIS);
  295. }
  296. }
  297. public static function get_group_by_user($userID, $params = array()) {
  298. return self::getGroupByUser($userID, $params);
  299. }
  300. public static function getGroupByUserName($userName, $params = array()) {
  301. $db = DB::getDB();
  302. $userID = 0;
  303. $sql = "select u.`ID`
  304. from `ADMIN_USERS` as u
  305. where u.`ADM_ACCOUNT`='{$userName}'
  306. ";
  307. $res = $db->query($sql);
  308. if ($r = $db->fetch($res)) {
  309. $userID = $r->ID;
  310. }
  311. if ($userID > 0) {
  312. return self::getGroupByUser($userID, $params);
  313. }
  314. return null;
  315. }
  316. public static function getGroupByUser($userID, $params = array()) {
  317. $sql_select = array();
  318. $sql_left_join = "";
  319. $sql_select[] = "z.`ID`";
  320. $sql_select[] = "z.`DESC`";
  321. $sql_select[] = "z.`OPIS`";
  322. $sql_select[] = "z.`A_LDAP_GID`";
  323. $telbox = V::get('T_TELBOX_NAME', 0, $params, 'int');
  324. $SHOW_IN_PERIOD_MARK = V::get('SHOW_IN_PERIOD_MARK', 0, $params, 'string');
  325. if ($telbox > 0) {
  326. $sql_left_join = "left join `TELBOXES` as tx on(tx.`ID`=up.`T_TELBOX_NEIGHBOUR_IN_ID`)";
  327. $sql_select[] = "tx.`T_TELBOX_NAME`";
  328. }
  329. $sql_select_where_and = "";
  330. if (!empty($SHOW_IN_PERIOD_MARK)) {
  331. $sql_select_where_and .= " and up.`SHOW_IN_PERIOD_MARK`='{$SHOW_IN_PERIOD_MARK}' ";
  332. }
  333. $sql_select = implode(', ', $sql_select);
  334. $sql = "
  335. select {$sql_select}
  336. from `CRM_AUTH_PROFILE` as up
  337. left join `CRM_LISTA_ZASOBOW` as z on(z.`ID`=up.`ID_ZASOB`)
  338. {$sql_left_join}
  339. where
  340. up.`REMOTE_ID`='{$userID}'
  341. and up.`A_STATUS` in('WAITING', 'NORMAL')
  342. and up.`REMOTE_TABLE`='ADMIN_USERS'
  343. and z.`ID` is not null
  344. and z.`TYPE` in('STANOWISKO','PODMIOT')
  345. {$sql_select_where_and}
  346. ";
  347. return array_map(function ($row) {
  348. return (object)$row;
  349. }, DB::getPDO()->fetchAllByKey($sql, 'ID'));
  350. }
  351. public static function getLDAPGroupByUserName($userName) {
  352. return UsersLdapHelper::getUserGroups($userName);
  353. }
  354. public static function getLDAPGroupsAll() {
  355. return UsersLdapHelper::getGroupsAll();
  356. }
  357. public static function getGroupsAll() {
  358. $allGroups = array();
  359. $db = DB::getDB();
  360. $sql = "select z.`ID`, z.`TYPE`, z.`DESC`, z.`A_LDAP_GID`
  361. from `CRM_LISTA_ZASOBOW` as z
  362. where
  363. z.`A_STATUS` in('WAITING', 'NORMAL')
  364. and z.`TYPE` in('STANOWISKO','DZIAL','PODMIOT')
  365. ";
  366. $res = $db->query($sql);
  367. while ($r = $db->fetch($res)) {
  368. $allGroups[$r->ID] = $r;
  369. }
  370. return $allGroups;
  371. }
  372. /**
  373. * @params $ids array of integer
  374. */
  375. public static function getGroupsByLdapGids($ldapGids) {
  376. $groups = array();
  377. if (empty($ldapGids)) {
  378. return false;
  379. }
  380. $db = DB::getDB();
  381. $sql = "select z.`ID`, z.`TYPE`, z.`DESC`, z.`A_LDAP_GID`
  382. from `CRM_LISTA_ZASOBOW` as z
  383. where
  384. z.`A_STATUS` in('WAITING', 'NORMAL')
  385. and z.`TYPE` in('STANOWISKO','DZIAL','PODMIOT')
  386. and z.`A_LDAP_GID` in(" . implode(",", $ldapGids) . ")
  387. ";
  388. $res = $db->query($sql);
  389. while ($r = $db->fetch($res)) {
  390. $groups[$r->ID] = $r;
  391. }
  392. return $groups;
  393. }
  394. public static function getUsersAll() {
  395. $allGroups = array();
  396. $sql_order_by = '';
  397. $sql_limit = '';
  398. $sql_where = '';
  399. //if (!empty($params)) {
  400. $sql_where_and_arr = array();
  401. $sql_where_and_arr[] = "a.`A_STATUS` in('WAITING','NORMAL','MONITOR','WARNING')";
  402. $sql_where_and_arr[] = "a.`ADM_ADMIN_DESC`!='Kandydat'";
  403. //} else {
  404. // $sql_where = "1=1";
  405. //}
  406. if (isset($params['ADM_ADMIN_LEVEL'])) {
  407. $adm_lvl = V::get('ADM_ADMIN_LEVEL', 0, $params, 'int');
  408. $sql_where_and_arr[] = "a.`ADM_ADMIN_LEVEL`='{$adm_lvl}'";
  409. }
  410. $sql_where = implode(" and ", $sql_where_and_arr);
  411. $db = DB::getDB();
  412. $sql = "SELECT a.`ID`
  413. , a.`A_STATUS`
  414. , a.`ADM_ACCOUNT`
  415. , a.`ADM_ADMIN_LEVEL`
  416. , a.`ADM_ADMIN_DESC` -- stanowisko
  417. , a.`ADM_NAME` -- imie i nazwisko
  418. , a.`ADM_NIP` -- NIP
  419. , a.`ADM_PESEL` -- nr. PESEL
  420. , a.`ADM_PHONE` -- nr. telefonu
  421. , a.`EMAIL` as EMAIL -- adres email
  422. , a.`ADM_OTHER_INFO`
  423. , group_concat(', ',tx.T_TELBOX_NAME) as T_TELBOX_NEIGHBOUR_IN_ID_NAME
  424. from `ADMIN_USERS` as a
  425. left join CRM_AUTH_PROFILE as cp on (cp.REMOTE_TABLE='ADMIN_USERS' and cp.REMOTE_ID=a.ID)
  426. left join TELBOXES as tx on (tx.ID=cp.T_TELBOX_NEIGHBOUR_IN_ID)
  427. where {$sql_where}
  428. group by a.ID
  429. -- {$sql_order_by}
  430. -- {$sql_limit}
  431. ";
  432. $res = $db->query($sql);
  433. while ($r = $db->fetch($res)) {
  434. $allGroups[$r->ID] = $r;
  435. }
  436. return $allGroups;
  437. }
  438. public static function getLDAPUsersAll() {
  439. return UsersLdapHelper::getUsersAll();
  440. }
  441. public static function getLDAPUserByName($userName, $allAttrs = false) {
  442. return UsersLdapHelper::getUser($userName, $allAttrs);
  443. }
  444. public static function getUserAcl($user_id) {
  445. static $_acl;
  446. if (!$_acl || !is_array($_acl)) {
  447. $_acl = array();
  448. }
  449. if ($user_id <= 0) {
  450. return false;
  451. }
  452. if (!array_key_exists($user_id, $_acl)) {
  453. Lib::loadClass('UserAcl');
  454. $_acl[$user_id] = new UserAcl($user_id);
  455. }
  456. return $_acl[$user_id];
  457. }
  458. public static function add_group($user_id, $group_id, $add_localisation) {
  459. if ($group_id <= 0) return -1;
  460. if ($user_id <= 0) return -1;
  461. $db = DB::getDB();
  462. $sqlToday = date('Y-m-d-H:i');
  463. $sqlUser = User::getName();
  464. $sql = "insert into `CRM_AUTH_PROFILE` (`ID_ZASOB`, `REMOTE_ID`, `REMOTE_TABLE`, `A_RECORD_CREATE_DATE`, `A_RECORD_CREATE_AUTHOR`,`T_TELBOX_NEIGHBOUR_IN_ID`)
  465. select `ID`, '{$user_id}' as user_id, 'ADMIN_USERS' as REMOTE_TABLE
  466. , '{$sqlToday}' as A_RECORD_CREATE_DATE
  467. , '{$sqlUser}' as A_RECORD_CREATE_AUTHOR , '{$add_localisation}' as T_TELBOX_NEIGHBOUR_IN_ID
  468. from `CRM_LISTA_ZASOBOW`
  469. where `ID`='{$group_id}' and `TYPE` in('STANOWISKO','PODMIOT')
  470. ";
  471. $db->query($sql);
  472. $ret_id = $db->insert_id();
  473. return $ret_id;
  474. }
  475. public static function remove_group($user_id, $group_id) {
  476. if ($group_id <= 0) return -1;
  477. if ($user_id <= 0) return -1;
  478. $db = DB::getDB();
  479. $sql = "select `ID` from `CRM_AUTH_PROFILE` where `ID_ZASOB`='{$group_id}' and `REMOTE_ID`='{$user_id}' and `REMOTE_TABLE`='ADMIN_USERS' ";
  480. $res = $db->query($sql);
  481. if ($r = $db->fetch($res)) {
  482. $table = 'CRM_AUTH_PROFILE';
  483. $sql_obj = new stdClass();
  484. $sql_obj->ID = $r->ID;
  485. $sql_obj->A_STATUS = 'DELETED';
  486. $ret = $db->UPDATE_OBJ($table, $sql_obj);
  487. if ($ret > 0) {
  488. $sql = "delete from `CRM_AUTH_PROFILE` where `ID`='{$r->ID}' limit 1;";
  489. $db->query($sql);
  490. }
  491. return $ret;
  492. } else {
  493. return -1;
  494. }
  495. }
  496. public static function getGroupsTree() {
  497. self::_fetchGroupsTreeAll();
  498. }
  499. public function getGroupsTreeRoot() {
  500. $dataAll = self::_fetchGroupsTreeAll();
  501. return $dataAll['treeRoot'];
  502. }
  503. public function getGroupsTreeItems() {
  504. $dataAll = self::_fetchGroupsTreeAll();
  505. return $dataAll['items'];
  506. }
  507. public static function _fetchGroupsTreeAll() {
  508. static $dataAll;
  509. if (is_array($dataAll)) {
  510. return $dataAll;
  511. }
  512. $dataAll = array();
  513. $filterTreeTypes = array('STANOWISKO','DZIAL','PODMIOT');
  514. // tree flat
  515. $db = DB::getDB();
  516. $sql = "select z.`ID`, z.`PARENT_ID`, z.`TYPE`, z.`DESC`, z.`A_LDAP_GID`
  517. from `CRM_LISTA_ZASOBOW` as z
  518. where
  519. z.`A_STATUS` in('WAITING', 'NORMAL')
  520. ";
  521. $res = $db->query($sql);
  522. $dataAll['items'] = array();
  523. $treeZasoby = array();
  524. while ($r = $db->fetch($res)) {
  525. $treeZasoby[$r->ID] = $r->PARENT_ID;
  526. if (in_array($r->TYPE, $filterTreeTypes)) {
  527. $r->sub = array();
  528. $dataAll['items'][$r->ID] = $r;
  529. }
  530. }
  531. // find parent rec
  532. $dataAll['treeRoot'] = array();
  533. foreach ($dataAll['items'] as $kID => $vGroup) {
  534. $pID = $vGroup->PARENT_ID;
  535. $isSub = false;
  536. for ($i = 0, $limit = 100; $i < $limit; $i++) {
  537. if (array_key_exists($pID, $dataAll['items'])) {
  538. $dataAll['items'][$pID]->sub[] = $kID;
  539. //unset($dataAll['items'][$kID]);
  540. $isSub = true;
  541. break;
  542. }
  543. $pID = $treeZasoby[$pID];
  544. }
  545. // not found = root
  546. if (!$isSub) {
  547. $dataAll['treeRoot'][] = $kID;
  548. }
  549. }
  550. return $dataAll;
  551. }
  552. }