ProcesLogDao.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <?php
  2. /**
  3. * wykonywanie procesu zapisywane w tabeli `CRM_PROCES_LOG`
  4. * `CRM_PROCES_LOG`.`TYPE`:
  5. * 'INIT' - rozpoczeto proces
  6. * 'STEP' - krok `ID_STEP`
  7. * 'END' - zakonczono proces
  8. *
  9. * ID_PROCES_INIT (C)
  10. * ID_STEP (W) - aktualny krok
  11. * ID_STEP_WSKAZNIK (W) - aktualny wskaznik w kroku
  12. *
  13. * stany:
  14. * edycja rekordu - wedlug zawartosci pola ID_STEP_WSKAZNIK (tabela/zrodlo danych z drzewa zasobow)
  15. * ID_STEP_WSKAZNIK jest ustawiony - jesli nie to pierwszy w kolejnosci
  16. * ID_KEY - jeszcze nie ustawiony - jesli ustawiony to pokazac rekord/edycje, akcje - NEXT
  17. *
  18. * akcje:
  19. * 'save_edit' - zapis formularza edycji rekordu
  20. * @see fun_PROCES_STEP_SAVE @param _action
  21. * next step - nastepny krok (jesli nie aktualny wskaznik jest ostatnim w kroku)
  22. * ID_STEP_WSKAZNIK - jest ustalany po wybraniu/edycji/utworzeniu/akcji na rekordzie ID_KEY zasobu/tabeli A_UPDATE_ZASOB_ID
  23. * next step wsk - nastepny wskaznik w kroku
  24. * zmiana ID_STEP_WSKAZNIK na nastepny w kolejnosci bez ustalania ID_KEY
  25. *
  26. *
  27. *
  28. * Obsługa plików:
  29. * Zasoby:
  30. * serwer (host)
  31. * ZASOB_PLIKOW (path eg. /home/samba)
  32. * ZASOB_PLIKOW (PROJEKTY) -> /home/samba/PROJEKTY
  33. * [X] SCHEMAT KATALOG (PR)
  34. * DANE 1 (ID_PROJ)
  35. * DANE 2 (DATE)
  36. * DANE 3 (TYPE)
  37. * DANE 4 (OPIS)
  38. * SCHEMAT KATALOG ()
  39. * DANE 1 (part 1)
  40. * DANE 2 (part 2)
  41. * PLIK
  42. * DANE 1 (row ID)
  43. * DANE 2 (rodzaj) z parent [X]
  44. * DANE 3 (typ)
  45. * DANE (PISMO)
  46. * DANE (ZDJECIE)
  47. * DANE (...)
  48. * DANE 4 (opis) - reczny
  49. * DANE 5 (data utworzenia)
  50. * DANE 6 (data wersji)
  51. * DANE 7 (ext)
  52. *
  53. * PLIK OLD 2011-10-28.PISMO.Podanie_o_przesuniecie_te.2011-10-28.16883.pdf)
  54. * PLIK NEW row_ID.PISMO.zasob_ID.Podanie_o_przesuniecie_te.2011-10-28.2011-10-28.pdf)
  55. * PLIK NEW row_ID.PR.PISMO.Podanie_o_przesuniecie_te.2011-10-28.2011-10-28.pdf)
  56. * PLIK NEW row_ID . rodzaj . typ . opis . data crete . data update . ext)
  57. */
  58. Lib::loadClass('BaseDao');
  59. Lib::loadClass('ProcesLogModel');
  60. class ProcesLogDao extends BaseDao {
  61. function __construct($db) {
  62. parent::__construct($db, 'CRM_PROCES_LOG');
  63. }
  64. function get_by_id($id) {
  65. $null = null;
  66. $id = (int)$id;
  67. if ($id <= 0) return $null;
  68. $db = DB::getDB();
  69. $sql = "select t.*
  70. from `{$this->_table}` as t
  71. where t.`ID`='{$id}'
  72. ";
  73. $res = $db->query($sql);
  74. if ($h = $db->fetch_assoc($res)) {
  75. $model = new ProcesLogModel($this);
  76. $model->setDataFromDB($h);
  77. return $model;
  78. }
  79. return $null;
  80. }
  81. function get_hist($id) {
  82. }
  83. /**
  84. * @returns int
  85. */
  86. function get_user_log_list_count($user_id, $user_groups) {
  87. $ret = 0;
  88. $sql_where = $this->_get_user_log_list_query_where($user_id, $user_groups);
  89. $sql = "select count(1) as cnt
  90. from `{$this->_table}` as plog
  91. where {$sql_where}
  92. ";
  93. $res = $this->_db->query($sql);
  94. if ($r = $this->_db->fetch($res)) {
  95. $ret = $r->cnt;
  96. }
  97. return $ret;
  98. }
  99. /**
  100. * @returns array of ProcesLogModel objects.
  101. */
  102. function get_user_log_list($user_id, $user_groups, $limitstart = 0) {
  103. $ret = array();
  104. $sql_offset = intval($limitstart);
  105. $sql_where = $this->_get_user_log_list_query_where($user_id, $user_groups);
  106. $sql = "select plog.*
  107. from `{$this->_table}` as plog
  108. where {$sql_where}
  109. limit 20 offset {$sql_offset}
  110. ";
  111. $res = $this->_db->query($sql);
  112. while ($r = $this->_db->fetch($res)) {
  113. $model = new ProcesLogModel($this);
  114. $model->setDataFromDB($r);
  115. $ret[] = $model;
  116. }
  117. return $ret;
  118. }
  119. function _get_user_log_list_query_where($user_id, $user_groups) {
  120. $sql_where = '';
  121. $sql_where_groups_add = '';
  122. if (!empty($user_groups)) {
  123. $sql_where_groups_and = array();
  124. foreach ($user_groups as $v_group_id) {
  125. $sql_where_groups_and[] = "FIND_IN_SET('$v_group_id', plog.`ID_STANOWISKA`)";
  126. }
  127. $sql_where_groups_add .= " or (plog.`ID_USER`=0 and (" . implode(" or ", $sql_where_groups_and) . "))";
  128. }
  129. $sql_where = "( (plog.`ID_USER`>0 and plog.`ID_USER`='{$user_id}')
  130. {$sql_where_groups_add} )
  131. ";
  132. return $sql_where;
  133. }
  134. /**
  135. * Create ProcesLogModel and save data into table `PROCES_LOG`.
  136. * @returns boolean.
  137. * @see save
  138. */
  139. function proces_log_init($user_id, $user_login, $proces_id) {
  140. $log_model = new ProcesLogModel($this);
  141. $log_model->set('A_RECORD_CREATE_DATE', date("Y-m-d-H:i"));
  142. $log_model->set('A_RECORD_CREATE_AUTHOR', $user_login);
  143. $log_model->set('ID_USER', $user_id);
  144. $log_model->set('TYPE', 'INIT');
  145. $log_model->set('ID_PROCES_INIT', $proces_id);
  146. $log_model->set('ID_STEP', $proces_id);
  147. //$log_model->set('ID_STANOWISKA', '');// nieokreślony
  148. $log_model->set('ID_KEY', 0);
  149. return $this->save($log_model);
  150. }
  151. /**
  152. * Get model actions by state.
  153. * @returns array of actions.
  154. */
  155. function get_model_actions($model) {
  156. $actions = array();
  157. if (!$model->get('ID')) {
  158. return $ret;
  159. }
  160. switch ($model->get('TYPE')) {
  161. case 'INIT':
  162. $actions['step'] = "kontynuuj";
  163. break;
  164. case 'STEP':
  165. $actions['step'] = "kontynuuj";
  166. $actions['quit'] = "zakończ";
  167. break;
  168. case 'END':
  169. $actions['hist'] = "hist";
  170. break;
  171. }
  172. return $actions;
  173. }
  174. /**
  175. * Check if user or group ids has access to log model.
  176. * @returns boolean.
  177. */
  178. function check_access($modelLog, $user_id, $user_groups_ids) {
  179. if (!$modelLog->get('ID')) {
  180. return false;
  181. }
  182. if ($modelLog->get('ID_USER') == $user_id) {
  183. return true;
  184. } else if ($modelLog->get('ID_USER') == 0) {
  185. $log_groups = explode(',', $modelLog->get('ID_STANOWISKA'));
  186. if (!empty($log_groups)) {
  187. foreach ($user_groups_ids as $v_group_id) {
  188. if (in_array($v_group_id, $log_groups)) {
  189. return true;
  190. }
  191. }
  192. }
  193. }
  194. return false;
  195. }
  196. /**
  197. *
  198. * @returns ProcesLogEditModel or null
  199. * TODO: RMME - not used
  200. */
  201. function get_next_edit($modelLog, $wskazniki, $current_edit) {
  202. $ret = null;
  203. return $ret;
  204. }
  205. function get_proces_next_steps($cur_step_id) {
  206. $ret = array();
  207. if ($cur_step_id <= 0) {
  208. return $ret;
  209. }
  210. $cur_row = $this->_db->get_by_id('CRM_PROCES', $cur_step_id);
  211. if (!$cur_row) {
  212. return $ret;
  213. }
  214. $sql = "select p.`ID`, p.`DESC`
  215. from `CRM_PROCES` as p
  216. where p.`PARENT_ID`='{$cur_step_id}'
  217. and p.`A_STATUS` in ('WAITING', 'NORMAL')
  218. order by p.`SORT_PRIO` asc, p.`ID` asc
  219. ";
  220. $res = $this->_db->query($sql);
  221. while ($r = $this->_db->fetch($res)) {
  222. $ret [$r->ID] = $r->DESC;
  223. }
  224. if ($cur_row->IF_TRUE_GOTO > 0) {
  225. $goto_row = $this->_db->get_by_id('CRM_PROCES', $cur_row->IF_TRUE_GOTO);
  226. if ($goto_row) {
  227. $ret [$goto_row->ID] = $goto_row->DESC;
  228. }
  229. }
  230. return $ret;
  231. }
  232. function get_wskazniki($proces_id) {
  233. $ret = array();
  234. $proces_id = (int)$proces_id;
  235. if ($proces_id <= 0) {
  236. return $ret;
  237. }
  238. $sql = "select
  239. clz.*
  240. , 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
  241. , w.`A_HAS_IMAGE` as CW_A_HAS_IMAGE
  242. , cp.`PYTANIE` as CP_PYTANIE, cp.`OPIS` as CP_OPIS, cp.`FORM_TREAT` as CP_FORM_TREAT
  243. from `CRM_WSKAZNIK` as w
  244. left join `CRM_LISTA_ZASOBOW` as clz on (clz.`ID`=w.`ID_ZASOB`)
  245. left join `CRM_PRZYPADEK` as cp on (cp.`ID`=w.`ID_PRZYPADEK`)
  246. where
  247. w.`ID_PROCES`='{$proces_id}'
  248. and w.`A_STATUS` in('WAITING','NORMAL','MONITOR')
  249. order by w.`SORT_PRIO` asc, w.`ID` asc
  250. ";
  251. $res = $this->_db->query($sql);
  252. while ($r = $this->_db->fetch($res)) {
  253. $ret[$r->CW_ID] = $r;
  254. }
  255. return $ret;
  256. }
  257. /**
  258. * Fetch sql types.
  259. * @returns array( Field => array(
  260. * [Type] => int(10)
  261. * [Null] => NO
  262. * [Key] => PRI
  263. * [Default] =>
  264. * [Extra] => auto_increment )
  265. * TODO: use cache SES
  266. */
  267. function get_sql_types($sql_table) {
  268. $db = DB::getDB();
  269. $types = array();
  270. $sql = "show fields from `{$sql_table}`;";
  271. $res = $db->query($sql);
  272. while ($r = $db->fetch($res)) {
  273. $field = $r->Field;
  274. unset($r->Field);
  275. $types [$field]= $r;
  276. }
  277. return $types;
  278. }
  279. /**
  280. * @returns array of wsk id's tree flat - TODO:
  281. */
  282. function get_wskazniki_parent_tables($wskazniki) {
  283. $ret = array();
  284. if (empty($wskazniki)) {
  285. return $ret;
  286. }
  287. $komorka_ids = array();
  288. foreach ($wskazniki as $k_id => $v_wsk) {
  289. if ($v_wsk->TYPE == 'KOMORKA') {// TODO: 'DANE' under 'KOMORKA'
  290. $komorka_ids[] = $v_wsk->ID;
  291. }
  292. }
  293. if (empty($komorka_ids)) {
  294. return $ret;
  295. }
  296. $sql = "select z.`ID`, z.`TYPE`, z.`DESC`
  297. , pz.`ID` as p__ID, pz.`TYPE` as p__TYPE, pz.`DESC` as p__DESC
  298. from `CRM_LISTA_ZASOBOW` as z
  299. left join `CRM_LISTA_ZASOBOW` as pz on(pz.`ID`=z.`PARENT_ID` and pz.`A_STATUS` in ('WAITING','NORMAL'))
  300. where
  301. z.`ID` in(" . implode(",", $komorka_ids) . ")
  302. and z.`A_STATUS` in ('WAITING','NORMAL')
  303. ";
  304. $res = $this->_db->query($sql);
  305. while ($r = $this->_db->fetch($res)) {
  306. // TODO: use TYPE = DANE
  307. if ($r->TYPE == 'TABELA') {
  308. $ret [$r->ID]= (object)array('ID'=>$r->ID, 'DESC'=>$r->DESC, 'TYPE'=>$r->TYPE);
  309. } else if ($r->p__TYPE == 'TABELA') {
  310. $ret [$r->p__ID]= (object)array('ID'=>$r->p__ID, 'DESC'=>$r->p__DESC, 'TYPE'=>$r->p__TYPE);
  311. $ret [$r->ID]= $r->p__ID;
  312. }
  313. }
  314. return $ret;
  315. }
  316. }