ProcesHelper.php 12 KB

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