UsersHelper.php 16 KB

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