UsersHelper.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  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. //static $_groups;// TODO: whould be $_groups[$user_id] - array of stanowiska
  318. //if (!$_groups) {
  319. $_groups = array();
  320. $db = DB::getDB();
  321. $sql_select = array();
  322. $sql_left_join = "";
  323. $sql_select[] = "z.`ID`";
  324. $sql_select[] = "z.`DESC`";
  325. $sql_select[] = "z.`OPIS`";
  326. $sql_select[] = "z.`A_LDAP_GID`";
  327. $telbox = V::get('T_TELBOX_NAME', 0, $params, 'int');
  328. $SHOW_IN_PERIOD_MARK = V::get('SHOW_IN_PERIOD_MARK', 0, $params, 'string');
  329. if ($telbox > 0) {
  330. $sql_left_join = "left join `TELBOXES` as tx on(tx.`ID`=up.`T_TELBOX_NEIGHBOUR_IN_ID`)";
  331. $sql_select[] = "tx.`T_TELBOX_NAME`";
  332. }
  333. $sql_select_where_and="";
  334. if (!empty($SHOW_IN_PERIOD_MARK)) {
  335. $sql_select_where_and.= " and up.`SHOW_IN_PERIOD_MARK`='{$SHOW_IN_PERIOD_MARK}' ";
  336. }
  337. $sql_select = implode(', ', $sql_select);
  338. $sql = "select {$sql_select}
  339. from `CRM_AUTH_PROFILE` as up
  340. left join `CRM_LISTA_ZASOBOW` as z on(z.`ID`=up.`ID_ZASOB`)
  341. {$sql_left_join}
  342. where
  343. up.`REMOTE_ID`='{$userID}'
  344. and up.`A_STATUS` in('WAITING', 'NORMAL')
  345. and up.`REMOTE_TABLE`='ADMIN_USERS'
  346. and z.`ID` is not null
  347. and z.`TYPE` in('STANOWISKO','PODMIOT')
  348. {$sql_select_where_and}
  349. ";
  350. $res = $db->query($sql);
  351. while ($r = $db->fetch($res)) {
  352. $_groups[$r->ID] = $r;
  353. }
  354. //}
  355. return $_groups;
  356. }
  357. public static function getLDAPGroupByUserName($userName) {
  358. return UsersLdapHelper::getUserGroups($userName);
  359. }
  360. public static function getLDAPGroupsAll() {
  361. return UsersLdapHelper::getGroupsAll();
  362. }
  363. public static function getGroupsAll() {
  364. $allGroups = array();
  365. $db = DB::getDB();
  366. $sql = "select z.`ID`, z.`TYPE`, z.`DESC`, z.`A_LDAP_GID`
  367. from `CRM_LISTA_ZASOBOW` as z
  368. where
  369. z.`A_STATUS` in('WAITING', 'NORMAL')
  370. and z.`TYPE` in('STANOWISKO','DZIAL','PODMIOT')
  371. ";
  372. $res = $db->query($sql);
  373. while ($r = $db->fetch($res)) {
  374. $allGroups[$r->ID] = $r;
  375. }
  376. return $allGroups;
  377. }
  378. /**
  379. * @params $ids array of integer
  380. */
  381. public static function getGroupsByLdapGids($ldapGids) {
  382. $groups = array();
  383. if (empty($ldapGids)) {
  384. return false;
  385. }
  386. $db = DB::getDB();
  387. $sql = "select z.`ID`, z.`TYPE`, z.`DESC`, z.`A_LDAP_GID`
  388. from `CRM_LISTA_ZASOBOW` as z
  389. where
  390. z.`A_STATUS` in('WAITING', 'NORMAL')
  391. and z.`TYPE` in('STANOWISKO','DZIAL','PODMIOT')
  392. and z.`A_LDAP_GID` in(" . implode(",", $ldapGids) . ")
  393. ";
  394. $res = $db->query($sql);
  395. while ($r = $db->fetch($res)) {
  396. $groups[$r->ID] = $r;
  397. }
  398. return $groups;
  399. }
  400. public static function getUsersAll() {
  401. $allGroups = array();
  402. $sql_order_by = '';
  403. $sql_limit = '';
  404. $sql_where = '';
  405. //if (!empty($params)) {
  406. $sql_where_and_arr = array();
  407. $sql_where_and_arr[] = "a.`A_STATUS` in('WAITING','NORMAL','MONITOR','WARNING')";
  408. $sql_where_and_arr[] = "a.`ADM_ADMIN_DESC`!='Kandydat'";
  409. //} else {
  410. // $sql_where = "1=1";
  411. //}
  412. if (isset($params['ADM_ADMIN_LEVEL'])) {
  413. $adm_lvl = V::get('ADM_ADMIN_LEVEL', 0, $params, 'int');
  414. $sql_where_and_arr[] = "a.`ADM_ADMIN_LEVEL`='{$adm_lvl}'";
  415. }
  416. $sql_where = implode(" and ", $sql_where_and_arr);
  417. $db = DB::getDB();
  418. $sql = "SELECT a.`ID`
  419. , a.`A_STATUS`
  420. , a.`ADM_ACCOUNT`
  421. , a.`ADM_ADMIN_LEVEL`
  422. , a.`ADM_ADMIN_DESC` -- stanowisko
  423. , a.`ADM_NAME` -- imie i nazwisko
  424. , a.`ADM_NIP` -- NIP
  425. , a.`ADM_PESEL` -- nr. PESEL
  426. , a.`ADM_PHONE` -- nr. telefonu
  427. , a.`EMAIL` as EMAIL -- adres email
  428. , a.`ADM_OTHER_INFO`
  429. , group_concat(', ',tx.T_TELBOX_NAME) as T_TELBOX_NEIGHBOUR_IN_ID_NAME
  430. from `ADMIN_USERS` as a
  431. left join CRM_AUTH_PROFILE as cp on (cp.REMOTE_TABLE='ADMIN_USERS' and cp.REMOTE_ID=a.ID)
  432. left join TELBOXES as tx on (tx.ID=cp.T_TELBOX_NEIGHBOUR_IN_ID)
  433. where {$sql_where}
  434. group by a.ID
  435. -- {$sql_order_by}
  436. -- {$sql_limit}
  437. ";
  438. $res = $db->query($sql);
  439. while ($r = $db->fetch($res)) {
  440. $allGroups[$r->ID] = $r;
  441. }
  442. return $allGroups;
  443. }
  444. public static function getLDAPUsersAll() {
  445. return UsersLdapHelper::getUsersAll();
  446. }
  447. public static function getLDAPUserByName($userName, $allAttrs = false) {
  448. return UsersLdapHelper::getUser($userName, $allAttrs);
  449. }
  450. public static function getUserAcl($user_id) {
  451. static $_acl;
  452. if (!$_acl || !is_array($_acl)) {
  453. $_acl = array();
  454. }
  455. if ($user_id <= 0) {
  456. return false;
  457. }
  458. if (!array_key_exists($user_id, $_acl)) {
  459. Lib::loadClass('UserAcl');
  460. $_acl[$user_id] = new UserAcl($user_id);
  461. }
  462. return $_acl[$user_id];
  463. }
  464. public static function add_group($user_id, $group_id, $add_localisation) {
  465. if ($group_id <= 0) return -1;
  466. if ($user_id <= 0) return -1;
  467. $db = DB::getDB();
  468. $sqlToday = date('Y-m-d-H:i');
  469. $sqlUser = User::getName();
  470. $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`)
  471. select `ID`, '{$user_id}' as user_id, 'ADMIN_USERS' as REMOTE_TABLE
  472. , '{$sqlToday}' as A_RECORD_CREATE_DATE
  473. , '{$sqlUser}' as A_RECORD_CREATE_AUTHOR , '{$add_localisation}' as T_TELBOX_NEIGHBOUR_IN_ID
  474. from `CRM_LISTA_ZASOBOW`
  475. where `ID`='{$group_id}' and `TYPE` in('STANOWISKO','PODMIOT')
  476. ";
  477. $db->query($sql);
  478. $ret_id = $db->insert_id();
  479. return $ret_id;
  480. }
  481. public static function remove_group($user_id, $group_id) {
  482. if ($group_id <= 0) return -1;
  483. if ($user_id <= 0) return -1;
  484. $db = DB::getDB();
  485. $sql = "select `ID` from `CRM_AUTH_PROFILE` where `ID_ZASOB`='{$group_id}' and `REMOTE_ID`='{$user_id}' and `REMOTE_TABLE`='ADMIN_USERS' ";
  486. $res = $db->query($sql);
  487. if ($r = $db->fetch($res)) {
  488. $table = 'CRM_AUTH_PROFILE';
  489. $sql_obj = new stdClass();
  490. $sql_obj->ID = $r->ID;
  491. $sql_obj->A_STATUS = 'DELETED';
  492. $ret = $db->UPDATE_OBJ($table, $sql_obj);
  493. if ($ret > 0) {
  494. $sql = "delete from `CRM_AUTH_PROFILE` where `ID`='{$r->ID}' limit 1;";
  495. $db->query($sql);
  496. }
  497. return $ret;
  498. } else {
  499. return -1;
  500. }
  501. }
  502. public static function getGroupsTree() {
  503. self::_fetchGroupsTreeAll();
  504. }
  505. public function getGroupsTreeRoot() {
  506. $dataAll = self::_fetchGroupsTreeAll();
  507. return $dataAll['treeRoot'];
  508. }
  509. public function getGroupsTreeItems() {
  510. $dataAll = self::_fetchGroupsTreeAll();
  511. return $dataAll['items'];
  512. }
  513. public static function _fetchGroupsTreeAll() {
  514. static $dataAll;
  515. if (is_array($dataAll)) {
  516. return $dataAll;
  517. }
  518. $dataAll = array();
  519. $filterTreeTypes = array('STANOWISKO','DZIAL','PODMIOT');
  520. // tree flat
  521. $db = DB::getDB();
  522. $sql = "select z.`ID`, z.`PARENT_ID`, z.`TYPE`, z.`DESC`, z.`A_LDAP_GID`
  523. from `CRM_LISTA_ZASOBOW` as z
  524. where
  525. z.`A_STATUS` in('WAITING', 'NORMAL')
  526. ";
  527. $res = $db->query($sql);
  528. $dataAll['items'] = array();
  529. $treeZasoby = array();
  530. while ($r = $db->fetch($res)) {
  531. $treeZasoby[$r->ID] = $r->PARENT_ID;
  532. if (in_array($r->TYPE, $filterTreeTypes)) {
  533. $r->sub = array();
  534. $dataAll['items'][$r->ID] = $r;
  535. }
  536. }
  537. // find parent rec
  538. $dataAll['treeRoot'] = array();
  539. foreach ($dataAll['items'] as $kID => $vGroup) {
  540. $pID = $vGroup->PARENT_ID;
  541. $isSub = false;
  542. for ($i = 0, $limit = 100; $i < $limit; $i++) {
  543. if (array_key_exists($pID, $dataAll['items'])) {
  544. $dataAll['items'][$pID]->sub[] = $kID;
  545. //unset($dataAll['items'][$kID]);
  546. $isSub = true;
  547. break;
  548. }
  549. $pID = $treeZasoby[$pID];
  550. }
  551. // not found = root
  552. if (!$isSub) {
  553. $dataAll['treeRoot'][] = $kID;
  554. }
  555. }
  556. return $dataAll;
  557. }
  558. }