ProcesHelper.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. <?php
  2. class ProcesHelper {
  3. public static function get_wskazniki( $id ) {
  4. $ret = array();
  5. $sql = "select
  6. clz.*
  7. , w.`ID` as CW_ID, w.`TYP` as CW_TYP, w.`OPIS_ZASOB` as OPIS_ZASOB, w.`SORT_PRIO` as CW_SORT_PRIO, w.`ID_PRZYPADEK` as CW_ID_PRZYPADEK
  8. , w.`A_HAS_IMAGE` as CW_A_HAS_IMAGE
  9. , cp.`PYTANIE` as CP_PYTANIE, cp.`OPIS` as CP_OPIS, cp.`FORM_TREAT` as CP_FORM_TREAT
  10. from `CRM_WSKAZNIK` as w
  11. left join `CRM_LISTA_ZASOBOW` as clz on (clz.`ID`=w.`ID_ZASOB`)
  12. left join `CRM_PRZYPADEK` as cp on (cp.`ID`=w.`ID_PRZYPADEK`)
  13. where
  14. w.`ID_PROCES`='".$id."'
  15. and w.`A_STATUS` in('WAITING','NORMAL','MONITOR')
  16. order by w.`SORT_PRIO` asc, w.`ID` asc
  17. ";
  18. $res = DB::query( $sql );
  19. while ($r = DB::fetch( $res )) {
  20. $ret[$r->CW_ID] = $r;
  21. }
  22. return $ret;
  23. }
  24. public static function getWskaznikiByIds($ids, $params = array()) {
  25. $wskazniki = array();
  26. if (empty($ids)) {
  27. return $wskazniki;
  28. }
  29. $db = DB::getDB();
  30. $sql = "select
  31. clz.*
  32. , w.`ID_PROCES`
  33. , w.`ID` as CW_ID, w.`TYP` as CW_TYP, w.`OPIS_ZASOB` as OPIS_ZASOB, w.`SORT_PRIO` as CW_SORT_PRIO, w.`ID_PRZYPADEK` as CW_ID_PRZYPADEK
  34. , w.`A_HAS_IMAGE` as CW_A_HAS_IMAGE
  35. , cp.`PYTANIE` as CP_PYTANIE, cp.`OPIS` as CP_OPIS, cp.`FORM_TREAT` as CP_FORM_TREAT
  36. from `CRM_WSKAZNIK` as w
  37. left join `CRM_LISTA_ZASOBOW` as clz on (clz.`ID`=w.`ID_ZASOB`)
  38. left join `CRM_PRZYPADEK` as cp on (cp.`ID`=w.`ID_PRZYPADEK`)
  39. where
  40. w.`ID_PROCES` in(" . implode(",", $ids) . ")
  41. and w.`A_STATUS` in('WAITING','NORMAL','MONITOR')
  42. order by w.`SORT_PRIO` asc, w.`ID` asc
  43. ";
  44. $res = $db->query($sql);
  45. while ($r = $db->fetch($res)) {
  46. $wskazniki[$r->ID_PROCES][$r->CW_ID] = $r;
  47. }
  48. if (1 == V::get('group_stanowiska', '', $params)) {
  49. foreach ($wskazniki as $kProcesId => $vWskazniki) {
  50. $stanowiska = array();
  51. foreach ($vWskazniki as $kWskId => $vWsk) {
  52. if ($vWsk->TYPE == 'STANOWISKO') {
  53. $stanowiska[$kWskId] = $vWsk;
  54. }
  55. }
  56. if (!empty($stanowiska)) {
  57. foreach ($stanowiska as $kWskId => $vWsk) {
  58. unset($wskazniki[$kProcesId][$kWskId]);
  59. }
  60. }
  61. $newWsk = array('stanowiska'=>$stanowiska, 'inne'=>$wskazniki[$kProcesId]);
  62. $wskazniki[$kProcesId] = $newWsk;
  63. }
  64. }
  65. return $wskazniki;
  66. }
  67. public static function get_list_for_user_count( $user_groups, $params = array() ) {
  68. $ret = array();
  69. $sql = self::_get_list_for_user_sql( $user_groups, $params, true );
  70. $res = DB::query( $sql );
  71. if ($r = DB::fetch( $res )) {
  72. $ret = $r->cnt;
  73. }
  74. return $ret;
  75. }
  76. public static function get_list_for_user( $user_groups, $params = array() ) {
  77. $ret = array();
  78. $sql = self::_get_list_for_user_sql( $user_groups, $params );
  79. $res = DB::query( $sql );
  80. while ($r = DB::fetch( $res )) {
  81. $ret []= $r;
  82. }
  83. return $ret;
  84. }
  85. /**
  86. * TODO: find user proces
  87. */
  88. public static function _get_list_for_user_sql( $user_groups, $params = array(), $count = false ) {
  89. $sql_select = array();
  90. $sql_limit = "";
  91. $sql_where = "";
  92. if ($count) {
  93. $sql_select []= "count(1) as cnt";
  94. } else {
  95. $sql_select []= "t.*";
  96. $sql_limit = V::get('limit', '10', $params);
  97. $sql_offset = V::get('offset', '0', $params);
  98. $sql_limit = "limit $sql_limit offset $sql_offset";
  99. }
  100. $sql_select = implode(", ", $sql_select);
  101. $sql = "select
  102. $sql_select
  103. from `CRM_PROCES_LOG` as t
  104. -- left join `` as p on(p.``=t.``)
  105. where
  106. $sql_where
  107. $sql_limit
  108. ";
  109. return $sql;
  110. }
  111. /**
  112. * Get allowed Stanowiska ID for given proces.
  113. * @param $proces_id - proces ID
  114. * @returns array of Stanowiska ID
  115. * Search recursively up by p.PARENT_ID, stop when find p.PROCES_INIT or find z.STANOWISKO
  116. */
  117. public static function get_allowed_stanowiska_by_proces_id( $proces_id ) {
  118. $ret = array();
  119. $proces_id = intval($proces_id);
  120. if ($proces_id <= 0) return $ret;
  121. // TODO: DB::get_by_id('CRM_PROCES', $proces_id);
  122. // TODO: check TYPE=PROCES_INIT -> get STANOWISKO and stop
  123. // TODO: find wskazniki STANOWISKO
  124. // TODO: if !empty return else recursive - TODO: add recursive limit!
  125. $sql = "select
  126. p.`TYPE` as p__TYPE
  127. , z.`ID` as z__ID
  128. , z.`TYPE` as z__TYPE
  129. from `CRM_PROCES` as p
  130. left join `CRM_WSKAZNIK` as w on(w.`ID_PROCES`=p.`ID`)
  131. left join `CRM_LISTA_ZASOBOW` as z on (z.`ID`=w.`ID_ZASOB`)
  132. where
  133. p.`ID`='".$proces_id."'
  134. and w.`A_STATUS` in('WAITING','NORMAL','MONITOR')
  135. and z.`TYPE`='STANOWISKO'
  136. ";
  137. $res = DB::query( $sql );
  138. while ($r = DB::fetch( $res )) {
  139. $ret []= $r->z__ID;
  140. }
  141. //echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;">ret: ';print_r($ret);echo'</pre>';
  142. return $ret;
  143. }
  144. public static function getProcesByUser( $user_name, $groups ) {
  145. $ret = array();
  146. $sql = "select plog.*
  147. from `CRM_PROCES_LOG` as plog
  148. where
  149. ( plog.`A_RECORD_CREATE_AUTHOR`='".$user_name."'
  150. or plog.`A_RECORD_UPDATE_AUTHOR`='".$user_name."'
  151. )
  152. order by plog.ID DESC
  153. ";
  154. $res = DB::query($sql);
  155. while ($r = DB::fetch($res)) {
  156. $ret[$r->ID] = $r;
  157. }
  158. return $ret;
  159. }
  160. public static function split_wskazniki_by_table( &$wsk ) {
  161. $wsk_split = array();
  162. echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;">wsk: ';print_r($wsk);echo'</pre>';
  163. foreach ($wsk as $k_id => $w) {
  164. $w->_parents = TreeHelper::get_all_parents( 'CRM_LISTA_ZASOBOW', $w->PARENT_ID );
  165. }
  166. foreach ($wsk as $k_id => $w) {// $w->CW_ID == $k_id
  167. if ($w->TYPE == 'KOMORKA') {
  168. $last_wsk = end($wsk_split);
  169. if (empty($last_wsk)) {
  170. }
  171. } else if ($w->TYPE == 'DOKUMENTY') {
  172. // file
  173. }
  174. }//end foreach
  175. }
  176. /**
  177. * @param $stanowiska_id - array of integer
  178. */
  179. public static function get_procesy_by_stanowiska( $stanowiska_id = array() ) {
  180. $ret = array();
  181. if (empty($stanowiska_id)) {
  182. return $ret;
  183. }
  184. $sql_stanowiska_id = array();
  185. foreach ($stanowiska_id as $v_id) {
  186. $v_id = intval($v_id);
  187. if ($v_id > 0) {
  188. $sql_stanowiska_id []= "'" . $v_id . "'";
  189. }
  190. }
  191. if (empty($sql_stanowiska_id)) {
  192. return $ret;
  193. }
  194. $sql = "select
  195. p.`ID`
  196. , p.`PARENT_ID`
  197. , p.`TYPE`
  198. , p.`DESC`
  199. , w.`ID` as w__ID
  200. , w.`OPIS_ZASOB` as w__OPIS_ZASOB
  201. , z.`ID` as z__ID
  202. from `CRM_PROCES` as p
  203. left join `CRM_WSKAZNIK` as w on(w.`ID_PROCES`=p.`ID`)
  204. left join `CRM_LISTA_ZASOBOW` as z on(z.`ID`=w.`ID_ZASOB`)
  205. where
  206. p.`TYPE`='PROCES_INIT'
  207. and p.`A_STATUS` in('WAITING','NORMAL','MONITOR')
  208. and w.`A_STATUS` in('WAITING','NORMAL','MONITOR')
  209. -- and w.`ID_PRZYPADEK`=1
  210. and z.`A_STATUS` in('WAITING','NORMAL','MONITOR')
  211. and z.`TYPE`='STANOWISKO'
  212. and z.`ID` is not null
  213. and z.`ID` in (" . implode(",", $sql_stanowiska_id) . ")
  214. order by p.`TEST_SORT_PRIO` DESC, p.`ID` ASC
  215. ";
  216. $res = DB::query($sql);
  217. while ($r = DB::fetch($res)) {
  218. $ret []= $r;
  219. }
  220. return $ret;
  221. }
  222. /**
  223. * List of PROCES_INIT - only order info.
  224. * @return array ID => TEST_SORT_PRIO
  225. */
  226. public static function get_procesy_init_list_order() {
  227. $ret = array();
  228. $db = DB::getDB();
  229. $sql = "select
  230. p.`ID`
  231. , p.`TEST_SORT_PRIO`
  232. from `CRM_PROCES` as p
  233. where
  234. p.`TYPE`='PROCES_INIT'
  235. and p.`A_STATUS` in('WAITING','NORMAL','MONITOR')
  236. order by p.`TEST_SORT_PRIO` DESC, p.`ID` ASC
  237. ";
  238. $res = $db->query($sql);
  239. while ($r = $db->fetch($res)) {
  240. $ret [$r->ID]= $r->TEST_SORT_PRIO;
  241. }
  242. return $ret;
  243. }
  244. /**
  245. * List of PROCES_INIT to sort.
  246. */
  247. public static function get_procesy_init_list() {
  248. $ret = array();
  249. $db = DB::getDB();
  250. $sql = "select
  251. p.`ID`
  252. , p.`PARENT_ID`
  253. , p.`TYPE`
  254. , p.`DESC`
  255. , p.`TEST_SORT_PRIO`
  256. -- , w.`ID` as w__ID
  257. -- , w.`OPIS_ZASOB` as w__OPIS_ZASOB
  258. -- , z.`ID` as z__ID
  259. from `CRM_PROCES` as p
  260. -- left join `CRM_WSKAZNIK` as w on(w.`ID_PROCES`=p.`ID`)
  261. -- left join `CRM_LISTA_ZASOBOW` as z on(z.`ID`=w.`ID_ZASOB`)
  262. where
  263. p.`TYPE`='PROCES_INIT'
  264. and p.`A_STATUS` in('WAITING','NORMAL','MONITOR')
  265. -- and w.`A_STATUS` in('WAITING','NORMAL','MONITOR')
  266. -- and w.`ID_PRZYPADEK`=1
  267. -- and z.`A_STATUS` in('WAITING','NORMAL','MONITOR')
  268. -- and z.`TYPE`='STANOWISKO'
  269. -- and z.`ID` is not null
  270. order by p.`TEST_SORT_PRIO` DESC, p.`ID` ASC
  271. ";
  272. $res = $db->query($sql);
  273. while ($r = $db->fetch($res)) {
  274. $ret []= $r;
  275. }
  276. return $ret;
  277. }
  278. /**
  279. * Przeniesienie procesu w kolejności w testach.
  280. */
  281. public static function proces_init_move($proces_id, $sort_prio_dir) {
  282. $db = DB::getDB();
  283. $proces_list = self::get_procesy_init_list();
  284. $proces_list = array_reverse($proces_list);
  285. $wsk = array();
  286. $wsk_order = array();
  287. $sort_prio = 0;
  288. foreach ($proces_list as $r) {
  289. $wsk [$r->ID] = $sort_prio;//$r->SORT_PRIO;
  290. $wsk_order [$sort_prio]= $r->ID;
  291. $sort_prio += 1;
  292. }
  293. if (empty($wsk)) return;
  294. $wsk_new = array();
  295. if ($sort_prio_dir == 'dw') {// wskaznik $wsk_id 'w gore' (zmniejsz sort_prio)
  296. $old_wsk_id = $wsk[ $proces_id ];
  297. if ($old_wsk_id == 0) return;
  298. //echo'zamien "'.$proces_id.'" na "'.$wsk_order[ $wsk[ $proces_id ] - 1 ].'"';
  299. $tmp = $wsk_order[ $wsk[ $proces_id ] ];
  300. $wsk_order[ $wsk[ $proces_id ] ] = $wsk_order[ $wsk[ $proces_id ] - 1 ];
  301. $wsk_order[ $wsk[ $proces_id ] - 1 ] = $tmp;
  302. } else {// wskaznik $wsk_id 'w dol' (zwieksz sort_prio)
  303. $old_wsk_id = $wsk[ $proces_id ];
  304. if ($old_wsk_id + 1 == count($wsk)) return;
  305. //echo'zamien "'.$proces_id.'" na "'.$wsk_order[ $wsk[ $proces_id ] + 1 ].'"';
  306. $tmp = $wsk_order[ $wsk[ $proces_id ] ];
  307. $wsk_order[ $wsk[ $proces_id ] ] = $wsk_order[ $wsk[ $proces_id ] + 1 ];
  308. $wsk_order[ $wsk[ $proces_id ] + 1 ] = $tmp;
  309. }
  310. if (empty($wsk_order)) return;
  311. foreach ($wsk_order as $k_sort_prio => $v_proces_id) {
  312. $sql = "update `CRM_PROCES` set `TEST_SORT_PRIO`='{$k_sort_prio}' where `ID`='{$v_proces_id}'; ";
  313. $db->query( $sql );
  314. }
  315. return true;
  316. }
  317. public static function proces_flag($proces_id, $goto_id, $flag) {
  318. $ret = '';
  319. switch ($flag) {
  320. case 'GOTO':
  321. $ret = '<span style="color:#FF0000">' . "&rarr;" . '</span>' . $goto_id;
  322. break;
  323. case 'GOTO_AND_RETURN':
  324. $ret = '<span style="color:#FF0000">' . "&rarr;" . '</span>' . $goto_id . '<span style="color:#FF0000">' . "&crarr;" . '</span>';
  325. break;
  326. case 'FORK':
  327. $ret = '<span style="color:#FF0000">' . "&oplus;&rarr;" . '</span>' . $goto_id;
  328. break;
  329. }
  330. return $ret;
  331. }
  332. /**
  333. * Get proces GOTO data list.
  334. * @param array $params
  335. * $params['return_by']:
  336. * '' - default retrun array('ID'=>$row)
  337. * 'dest' - return array('IF_TRUE_GOTO'=>array('ID'=>$row))
  338. * @returns array
  339. */
  340. public static function get_goto_list($params = array()) {
  341. $ret = array();
  342. $return_by = V::get('return_by', '', $params);
  343. $db = DB::getDB();
  344. $sql = "select
  345. p.`ID`
  346. , p.`IF_TRUE_GOTO`
  347. , p.`IF_TRUE_GOTO_FLAG`
  348. from `CRM_PROCES` as p
  349. where
  350. p.`IF_TRUE_GOTO`>0
  351. and p.`PARENT_ID`>=0
  352. and p.`A_STATUS` in('WAITING','NORMAL')
  353. ";
  354. $res = $db->query($sql);
  355. while ($r = $db->fetch($res)) {
  356. if ($return_by == 'dest') {
  357. $ret[$r->IF_TRUE_GOTO] []= $r;
  358. } else {
  359. $ret[$r->ID] = $r;
  360. }
  361. }
  362. return $ret;
  363. }
  364. public static function get_wskaznik( $wsk_id ) {
  365. $ret = array();
  366. $sql = "select
  367. clz.*
  368. , w.`ID` as CW_ID, w.`TYP` as CW_TYP, w.`OPIS_ZASOB` as OPIS_ZASOB, w.`SORT_PRIO` as CW_SORT_PRIO, w.`ID_PRZYPADEK` as CW_ID_PRZYPADEK
  369. , w.`A_HAS_IMAGE` as CW_A_HAS_IMAGE
  370. , cp.`PYTANIE` as CP_PYTANIE, cp.`OPIS` as CP_OPIS, cp.`FORM_TREAT` as CP_FORM_TREAT
  371. from `CRM_WSKAZNIK` as w
  372. left join `CRM_LISTA_ZASOBOW` as clz on (clz.`ID`=w.`ID_ZASOB`)
  373. left join `CRM_PRZYPADEK` as cp on (cp.`ID`=w.`ID_PRZYPADEK`)
  374. where
  375. w.`ID`='" . $wsk_id . "'
  376. and w.`A_STATUS` in('WAITING','NORMAL','MONITOR')
  377. ";
  378. $res = DB::query( $sql );
  379. if ($r = DB::fetch( $res )) {
  380. $ret[$r->CW_ID] = $r;
  381. }
  382. return $ret;
  383. }
  384. public static function get_proces( $next_id ) {
  385. return DB::get_by_id('CRM_PROCES', $next_id);
  386. }
  387. public static function getZasobTableInfo($zasobID) {
  388. $zasobObj = null;
  389. $db = DB::getDB();
  390. $sql = "select z.`ID`, z.`DESC`, z.`DESC_PL`, z.`OPIS`
  391. , zp.`ID` as P__ID, zp.`DESC` as P__DESC, zp.`TYPE` as P__TYPE
  392. from `CRM_LISTA_ZASOBOW` as z
  393. left join `CRM_LISTA_ZASOBOW` as zp on(zp.`ID`=z.`PARENT_ID`)
  394. where z.`ID`='{$zasobID}'
  395. and z.`TYPE`='TABELA'
  396. ";
  397. $res = $db->query($sql);
  398. if ($res) {
  399. $zasobObj = $db->fetch($res);
  400. }
  401. return $zasobObj;
  402. }
  403. public static function getZasobTableID($tblName) {
  404. $zasobID = 0;
  405. $db = DB::getDB();
  406. $sql = "select z.`ID`
  407. from `CRM_LISTA_ZASOBOW` as z
  408. where z.`DESC`='{$tblName}'
  409. and z.`TYPE`='TABELA'
  410. ";
  411. $res = $db->query($sql);
  412. if ($res) {
  413. $r = $db->fetch($res);
  414. $zasobID = $r->ID;
  415. }
  416. return $zasobID;
  417. }
  418. }