UsersHelper.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  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. $sql_where = UsersHelper::_parse_query( $params );
  34. $sql = "select count(1) as cnt
  35. from `ADMIN_USERS` as a
  36. where ".$sql_where."
  37. ";
  38. $res = DB::query($sql);
  39. if ($r = DB::fetch($res)) {
  40. $total = $r->cnt;
  41. }
  42. return $total;
  43. }
  44. public static function &get_users_list( $params = array(), $limit = 10, $limit_start = 0, $order_by = '', $order_dir = '' ) {
  45. $ret = array();
  46. $sql_where = UsersHelper::_parse_query( $params );
  47. $sql_limit = "";
  48. if ($limit > 0) {
  49. $sql_limit = "limit " . $limit;
  50. if ($limit_start > 0) $sql_limit .= " offset $limit_start";
  51. }
  52. $allowed_order_by = array();
  53. $allowed_order_by []= 'ID';
  54. $allowed_order_by []= 'ADM_NAME';
  55. $sql_order_by = "";
  56. if (in_array($order_by, $allowed_order_by)) {
  57. $sql_order_by .= " order by ".$order_by."";
  58. if (in_array($order_dir, array('DESC','ASC'))) {
  59. $sql_order_by .= " ".$order_dir;
  60. }
  61. }
  62. // 'WAITING','NORMAL','MONITOR','WARNING','OFF_SOFT','OFF_HARD','DELETED'
  63. $sql = "SELECT a.`ID`
  64. , a.`A_STATUS`
  65. , a.`ADM_ACCOUNT`
  66. , a.`ADM_ADMIN_LEVEL`
  67. , a.`ADM_ADMIN_DESC` -- stanowisko
  68. , a.`ADM_NAME` -- imie i nazwisko
  69. , a.`ADM_NIP` -- NIP
  70. , a.`ADM_PESEL` -- nr. PESEL
  71. , a.`ADM_PHONE` -- nr. telefonu
  72. , a.`EMAIL` as EMAIL -- adres email
  73. , a.`ADM_OTHER_INFO`
  74. , group_concat(', ',tx.T_TELBOX_NAME) as T_TELBOX_NEIGHBOUR_IN_ID_NAME,
  75. a.L_APPOITMENT_USER , a.A_ADM_COMPANY, a.A_CLASSIFIED
  76. from `ADMIN_USERS` as a
  77. left join CRM_AUTH_PROFILE as cp on (cp.REMOTE_TABLE='ADMIN_USERS' and cp.REMOTE_ID=a.ID)
  78. left join TELBOXES as tx on (tx.ID=cp.T_TELBOX_NEIGHBOUR_IN_ID)
  79. where ".$sql_where."
  80. group by a.ID
  81. ".$sql_order_by."
  82. ".$sql_limit."
  83. ";
  84. $res = DB::query($sql);
  85. while ($r = DB::fetch($res)) {
  86. $ret [$r->ID] = $r;
  87. }
  88. return $ret;
  89. }
  90. public static function get_user_by_id( $id ) {
  91. $ret = null;
  92. if ($id <= 0) return $ret;
  93. $sql_where = "a.`ID`='".$id."'";
  94. $sql = "SELECT a.`ID`
  95. , a.`A_STATUS`
  96. , a.`ADM_ACCOUNT`
  97. , a.`ADM_ADMIN_LEVEL`
  98. , a.`ADM_ADMIN_DESC` -- stanowisko
  99. , a.`ADM_NAME` -- imie i nazwisko
  100. , a.`ADM_NIP` -- NIP
  101. , a.`ADM_PESEL` -- nr. PESEL
  102. , a.`ADM_PHONE` -- nr. telefonu
  103. , a.`EMAIL` as EMAIL -- adres email
  104. , tx.T_TELBOX_NAME
  105. from `ADMIN_USERS` as a
  106. left join CRM_AUTH_PROFILE as cp on (cp.REMOTE_TABLE='ADMIN_USERS' and cp.REMOTE_ID=a.ID)
  107. left join TELBOXES as tx on (tx.ID=cp.T_TELBOX_NEIGHBOUR_IN_ID)
  108. where ".$sql_where."
  109. ";
  110. $res = DB::query($sql);
  111. if ($r = DB::fetch($res)) {
  112. $ret = $r;
  113. }
  114. return $ret;
  115. }
  116. public static function getUserByEmail($email) {
  117. if (empty($email)) return null;
  118. $usr = null;
  119. $db = DB::getDB();
  120. $sql = "SELECT a.`ID`
  121. , a.`A_STATUS`
  122. , a.`ADM_ACCOUNT`
  123. , a.`ADM_ADMIN_LEVEL`
  124. , a.`ADM_ADMIN_DESC` -- stanowisko
  125. , a.`ADM_NAME` -- imie i nazwisko
  126. , a.`ADM_NIP` -- NIP
  127. , a.`ADM_PESEL` -- nr. PESEL
  128. , a.`ADM_PHONE` -- nr. telefonu
  129. , a.`EMAIL` -- adres email
  130. , a.`EMPLOYEE_TYPE` -- EMPLOYEE_TYPE (ldap:employeeType)
  131. , a.`A_SYNC_LDAP_DATE`
  132. from `ADMIN_USERS` as a
  133. where a.`EMAIL`='{$email}'
  134. ";
  135. $res = $db->query($sql);
  136. if ($r = $db->fetch($res)) {
  137. $usr = $r;
  138. }
  139. return $usr;
  140. }
  141. public static function getUserByName($userName) {
  142. if (empty($userName)) return null;
  143. $user = null;
  144. $db = DB::getDB();
  145. $sql = "SELECT a.`ID`
  146. , a.`A_STATUS`
  147. , a.`ADM_ACCOUNT`
  148. , a.`ADM_ADMIN_LEVEL`
  149. , a.`ADM_ADMIN_DESC` -- stanowisko
  150. , a.`ADM_NAME` -- imie i nazwisko
  151. , a.`ADM_NIP` -- NIP
  152. , a.`ADM_PESEL` -- nr. PESEL
  153. , a.`ADM_PHONE` -- nr. telefonu
  154. , a.`EMAIL` -- adres email
  155. , a.`EMPLOYEE_TYPE` -- EMPLOYEE_TYPE (ldap:employeeType)
  156. , a.`A_SYNC_LDAP_DATE`
  157. from `ADMIN_USERS` as a
  158. where a.`ADM_ACCOUNT`='{$userName}'
  159. ";
  160. $res = $db->query($sql);
  161. if ($r = $db->fetch($res)) {
  162. $user = $r;
  163. }
  164. return $user;
  165. }
  166. public static function getUsersByGroupId($zasobId) {
  167. if (empty($zasobId)) return null;
  168. $users = array();
  169. $db = DB::getDB();
  170. $sql = "select u.`ID`
  171. , u.`A_STATUS`
  172. , u.`ADM_ACCOUNT`
  173. , u.`ADM_ADMIN_LEVEL`
  174. , u.`ADM_ADMIN_DESC` -- stanowisko
  175. , u.`ADM_NAME` -- imie i nazwisko
  176. , u.`ADM_NIP` -- NIP
  177. , u.`ADM_PESEL` -- nr. PESEL
  178. , u.`ADM_PHONE` -- nr. telefonu
  179. , u.`EMAIL` -- adres email
  180. , u.`EMPLOYEE_TYPE` -- EMPLOYEE_TYPE (ldap:employeeType)
  181. , u.`A_SYNC_LDAP_DATE`
  182. from `CRM_AUTH_PROFILE` as up
  183. left join `ADMIN_USERS` as u on (u.`ID`=up.`REMOTE_ID`)
  184. where
  185. up.`ID_ZASOB`='{$zasobId}'
  186. and up.`REMOTE_TABLE`='ADMIN_USERS'
  187. and up.`A_STATUS` in('WAITING', 'NORMAL')
  188. and u.`A_STATUS` in('WAITING', 'NORMAL')
  189. group by u.`ID`
  190. ";
  191. $res = $db->query($sql);
  192. while ($r = $db->fetch($res)) {
  193. $users[$r->ID] = $r;
  194. }
  195. return $users;
  196. }
  197. public static function getUsersByGroupsIds($zasobyIds, $ignoreUsrIds = array()) {
  198. if (empty($zasobyIds)) return null;
  199. $users = array();
  200. $db = DB::getDB();
  201. $sqlIds = " and up.`ID_ZASOB` in(" . implode(",", $zasobyIds) . ")";
  202. $sqlIgnore = "";
  203. if (!empty($ignoreUsrIds)) {
  204. $sqlIgnore = " and u.`ID` not in(" . implode(",", $ignoreUsrIds) . ")";
  205. }
  206. $sql = "select u.`ID`
  207. , u.`A_STATUS`
  208. , u.`ADM_ACCOUNT`
  209. , u.`ADM_ADMIN_LEVEL`
  210. , u.`ADM_ADMIN_DESC` -- stanowisko
  211. , u.`ADM_NAME` -- imie i nazwisko
  212. , u.`ADM_NIP` -- NIP
  213. , u.`ADM_PESEL` -- nr. PESEL
  214. , u.`ADM_PHONE` -- nr. telefonu
  215. , u.`EMAIL` -- adres email
  216. , u.`EMPLOYEE_TYPE` -- EMPLOYEE_TYPE (ldap:employeeType)
  217. , u.`A_SYNC_LDAP_DATE`
  218. from `CRM_AUTH_PROFILE` as up
  219. left join `ADMIN_USERS` as u on (u.`ID`=up.`REMOTE_ID`)
  220. where up.`REMOTE_TABLE`='ADMIN_USERS'
  221. and up.`A_STATUS` in('WAITING', 'NORMAL')
  222. and u.`A_STATUS` in('WAITING', 'NORMAL')
  223. {$sqlIds}
  224. {$sqlIgnore}
  225. group by u.`ID`
  226. ";
  227. $res = $db->query($sql);
  228. while ($r = $db->fetch($res)) {
  229. $users[$r->ID] = $r;
  230. }
  231. return $users;
  232. }
  233. public static function get_group_list() {
  234. static $_groups;
  235. if (!$_groups) {
  236. $_groups = array();
  237. $sql = "select z.`ID`, z.`DESC`, z.`OPIS`
  238. from `CRM_LISTA_ZASOBOW` as z
  239. where
  240. z.`TYPE` in('STANOWISKO','PODMIOT')
  241. order by z.`DESC`
  242. ";
  243. $res = DB::query($sql);
  244. while ($r = DB::fetch($res)) {
  245. $_groups[$r->ID] = $r;
  246. }
  247. }
  248. return $_groups;
  249. }
  250. public static function get_localisation_list() {
  251. static $_groups;
  252. if (!$_groups) {
  253. $_groups = array();
  254. $sql = "select tx.`ID`, tx.`T_TELBOX_NAME`, tx.`T_TELBOX_TYPE`
  255. from `TELBOXES` as tx
  256. where
  257. tx.`A_STATUS`!='DELETED'
  258. order by tx.`T_TELBOX_NAME`
  259. ";
  260. $res = DB::query($sql);
  261. while ($r = DB::fetch($res)) {
  262. $_groups[$r->ID] = $r;
  263. }
  264. }
  265. return $_groups;
  266. }
  267. public static function add_groups_to_user_list( &$items ) {
  268. if (empty($items)) return;
  269. $user_list_id = array();
  270. foreach ($items as $k_ind => $v_item) {
  271. $items[$k_ind]->groups = array();
  272. $user_list_id []= "'" . $v_item->ID . "'";
  273. }//end foreach
  274. if (empty($user_list_id)) return;
  275. $sql = "select z.`ID`, z.`DESC`, z.`OPIS`
  276. , up.`REMOTE_ID`
  277. from `CRM_AUTH_PROFILE` as up
  278. left join `CRM_LISTA_ZASOBOW` as z on(z.`ID`=up.`ID_ZASOB`)
  279. where
  280. up.`REMOTE_ID` in (" . implode(",", $user_list_id) . ")
  281. and up.`A_STATUS` in('WAITING', 'NORMAL')
  282. and up.`REMOTE_TABLE`='ADMIN_USERS'
  283. and z.`ID` is not null
  284. and z.`TYPE` in('STANOWISKO','PODMIOT')
  285. ";
  286. $res = DB::query($sql);
  287. while ($r = DB::fetch($res)) {
  288. $items[$r->REMOTE_ID]->groups [$r->ID] = $r->DESC;//array('DESC'=>$r->DESC , 'OPIS'=>$r->OPIS);
  289. }
  290. }
  291. public static function get_group_by_user($userID, $params = array()) {
  292. return self::getGroupByUser($userID, $params);
  293. }
  294. public static function getGroupByUserName($userName, $params = array()) {
  295. $db = DB::getDB();
  296. $userID = 0;
  297. $sql = "select u.`ID`
  298. from `ADMIN_USERS` as u
  299. where u.`ADM_ACCOUNT`='{$userName}'
  300. ";
  301. $res = $db->query($sql);
  302. if ($r = $db->fetch($res)) {
  303. $userID = $r->ID;
  304. }
  305. if ($userID > 0) {
  306. return self::getGroupByUser($userID, $params);
  307. }
  308. return null;
  309. }
  310. public static function getGroupByUser($userID, $params = array()) {
  311. //static $_groups;// TODO: whould be $_groups[$user_id] - array of stanowiska
  312. //if (!$_groups) {
  313. $_groups = array();
  314. $db = DB::getDB();
  315. $sql_select = array();
  316. $sql_left_join = "";
  317. $sql_select []= "z.`ID`";
  318. $sql_select []= "z.`DESC`";
  319. $sql_select []= "z.`OPIS`";
  320. $sql_select []= "z.`A_LDAP_GID`";
  321. $telbox = V::get('T_TELBOX_NAME', 0, $params, 'int');
  322. $SHOW_IN_PERIOD_MARK = V::get('SHOW_IN_PERIOD_MARK', 0, $params, 'string');
  323. if ($telbox > 0) {
  324. $sql_left_join = "left join `TELBOXES` as tx on(tx.`ID`=up.`T_TELBOX_NEIGHBOUR_IN_ID`)";
  325. $sql_select []= "tx.`T_TELBOX_NAME`";
  326. }
  327. $sql_select_where_and="";
  328. if (!empty($SHOW_IN_PERIOD_MARK)) {
  329. $sql_select_where_and.= " and up.`SHOW_IN_PERIOD_MARK`='{$SHOW_IN_PERIOD_MARK}' ";
  330. }
  331. $sql_select = implode(', ', $sql_select);
  332. $sql = "select {$sql_select}
  333. from `CRM_AUTH_PROFILE` as up
  334. left join `CRM_LISTA_ZASOBOW` as z on(z.`ID`=up.`ID_ZASOB`)
  335. {$sql_left_join}
  336. where
  337. up.`REMOTE_ID`='{$userID}'
  338. and up.`A_STATUS` in('WAITING', 'NORMAL')
  339. and up.`REMOTE_TABLE`='ADMIN_USERS'
  340. and z.`ID` is not null
  341. and z.`TYPE` in('STANOWISKO','PODMIOT')
  342. {$sql_select_where_and}
  343. ";
  344. $res = $db->query($sql);
  345. while ($r = $db->fetch($res)) {
  346. $_groups[$r->ID] = $r;
  347. }
  348. //}
  349. return $_groups;
  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. }