ProcesLogDao.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. $sql = "select t.*
  69. from `" . $this->_table . "` as t
  70. where t.`ID`='" . $id . "'
  71. ";
  72. $res = DB::query($sql);
  73. if ($h = DB::fetch_assoc($res)) {
  74. $model = new ProcesLogModel($this);
  75. $model->setDataFromDB($h);
  76. return $model;
  77. }
  78. return $null;
  79. }
  80. function get_hist( $id ) {
  81. }
  82. /**
  83. * @returns int
  84. */
  85. function get_user_log_list_count( $user_id, $user_groups ) {
  86. $ret = 0;
  87. $sql_where = $this->_get_user_log_list_query_where($user_id, $user_groups);
  88. $sql = "select count(1) as cnt
  89. from `" . $this->_table . "` as plog
  90. where " . $sql_where . "
  91. ";
  92. $res = $this->_db->query($sql);
  93. if ($r = $this->_db->fetch($res)) {
  94. $ret = $r->cnt;
  95. }
  96. return $ret;
  97. }
  98. /**
  99. * @returns array of ProcesLogModel objects.
  100. */
  101. function get_user_log_list( $user_id, $user_groups, $limitstart = 0 ) {
  102. $ret = array();
  103. $sql_offset = intval($limitstart);
  104. $sql_where = $this->_get_user_log_list_query_where($user_id, $user_groups);
  105. $sql = "select plog.*
  106. from `" . $this->_table . "` as plog
  107. where " . $sql_where . "
  108. limit 20 offset " . $sql_offset . "
  109. ";
  110. $res = $this->_db->query($sql);
  111. while ($r = $this->_db->fetch($res)) {
  112. $model = new ProcesLogModel($this);
  113. $model->setDataFromDB($r);
  114. $ret []= $model;
  115. }
  116. return $ret;
  117. }
  118. function _get_user_log_list_query_where( $user_id, $user_groups ) {
  119. $sql_where = '';
  120. $sql_where_groups_add = '';
  121. if (!empty($user_groups)) {
  122. $sql_where_groups_and = array();
  123. foreach ($user_groups as $v_group_id) {
  124. $sql_where_groups_and []= "FIND_IN_SET('$v_group_id', plog.`ID_STANOWISKA`)";
  125. }
  126. $sql_where_groups_add .= " or (plog.`ID_USER`=0 and (" . implode(" or ", $sql_where_groups_and) . "))";
  127. }
  128. $sql_where = "( (plog.`ID_USER`>0 and plog.`ID_USER`='" . $user_id . "')
  129. " . $sql_where_groups_add . " )
  130. ";
  131. return $sql_where;
  132. }
  133. /**
  134. * Create ProcesLogModel and save data into table `PROCES_LOG`.
  135. * @returns boolean.
  136. * @see save
  137. */
  138. function proces_log_init($user_id, $user_login, $proces_id) {
  139. $log_model = new ProcesLogModel($this);
  140. $log_model->set('A_RECORD_CREATE_DATE', date("Y-m-d-H:i"));
  141. $log_model->set('A_RECORD_CREATE_AUTHOR', $user_login);
  142. $log_model->set('ID_USER', $user_id);
  143. $log_model->set('TYPE', 'INIT');
  144. $log_model->set('ID_PROCES_INIT', $proces_id);
  145. $log_model->set('ID_STEP', $proces_id);
  146. //$log_model->set('ID_STANOWISKA', '');// nieokre¶lony
  147. $log_model->set('ID_KEY', 0);
  148. return $this->save($log_model);
  149. }
  150. /**
  151. * Get model actions by state.
  152. * @returns array of actions.
  153. */
  154. function get_model_actions($model) {
  155. $actions = array();
  156. if (!$model->get('ID')) {
  157. return $ret;
  158. }
  159. switch ($model->get('TYPE')) {
  160. case 'INIT':
  161. $actions ['step']= "kontynuuj";
  162. break;
  163. case 'STEP':
  164. $actions ['step']= "kontynuuj";
  165. $actions ['quit']= "zakoñcz";
  166. break;
  167. case 'END':
  168. $actions ['hist']= "hist";
  169. break;
  170. }
  171. return $actions;
  172. }
  173. /**
  174. * Check if user or group ids has access to log model.
  175. * @returns boolean.
  176. */
  177. function check_access($modelLog, $user_id, $user_groups_ids) {
  178. if (!$modelLog->get('ID')) {
  179. return false;
  180. }
  181. if ($modelLog->get('ID_USER') == $user_id) {
  182. return true;
  183. } else if ($modelLog->get('ID_USER') == 0) {
  184. $log_groups = explode(',', $modelLog->get('ID_STANOWISKA'));
  185. if (!empty($log_groups)) {
  186. foreach ($user_groups_ids as $v_group_id) {
  187. if (in_array($v_group_id, $log_groups)) {
  188. return true;
  189. }
  190. }
  191. }
  192. }
  193. return false;
  194. }
  195. /**
  196. *
  197. * @returns ProcesLogEditModel or null
  198. * TODO: RMME - not used
  199. */
  200. function get_next_edit($modelLog, $wskazniki, $current_edit) {
  201. $ret = null;
  202. return $ret;
  203. }
  204. function get_proces_next_steps($cur_step_id) {
  205. $ret = array();
  206. if ($cur_step_id <= 0) {
  207. return $ret;
  208. }
  209. $cur_row = $this->_db->get_by_id('CRM_PROCES', $cur_step_id);
  210. if (!$cur_row) {
  211. return $ret;
  212. }
  213. $sql = "select p.`ID`, p.`DESC`
  214. from `CRM_PROCES` as p
  215. where p.`PARENT_ID`='" . $cur_step_id . "'
  216. and p.`A_STATUS` in ('WAITING', 'NORMAL')
  217. order by p.`SORT_PRIO` asc, p.`ID` asc
  218. ";
  219. $res = $this->_db->query($sql);
  220. while ($r = $this->_db->fetch($res)) {
  221. $ret [$r->ID] = $r->DESC;
  222. }
  223. if ($cur_row->IF_TRUE_GOTO > 0) {
  224. $goto_row = $this->_db->get_by_id('CRM_PROCES', $cur_row->IF_TRUE_GOTO);
  225. if ($goto_row) {
  226. $ret [$goto_row->ID] = $goto_row->DESC;
  227. }
  228. }
  229. return $ret;
  230. }
  231. function get_wskazniki($proces_id) {
  232. $ret = array();
  233. $proces_id = (int)$proces_id;
  234. if ($proces_id <= 0) {
  235. return $ret;
  236. }
  237. $sql = "select
  238. clz.*
  239. , 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
  240. , w.`A_HAS_IMAGE` as CW_A_HAS_IMAGE
  241. , cp.`PYTANIE` as CP_PYTANIE, cp.`OPIS` as CP_OPIS, cp.`FORM_TREAT` as CP_FORM_TREAT
  242. from `CRM_WSKAZNIK` as w
  243. left join `CRM_LISTA_ZASOBOW` as clz on (clz.`ID`=w.`ID_ZASOB`)
  244. left join `CRM_PRZYPADEK` as cp on (cp.`ID`=w.`ID_PRZYPADEK`)
  245. where
  246. w.`ID_PROCES`='".$proces_id."'
  247. and w.`A_STATUS` in('WAITING','NORMAL','MONITOR')
  248. order by w.`SORT_PRIO` asc, w.`ID` asc
  249. ";
  250. $res = $this->_db->query( $sql );
  251. while ($r = $this->_db->fetch( $res )) {
  252. $ret[$r->CW_ID] = $r;
  253. }
  254. return $ret;
  255. }
  256. /**
  257. * Fetch sql types.
  258. * @returns array( Field => array(
  259. * [Type] => int(10)
  260. * [Null] => NO
  261. * [Key] => PRI
  262. * [Default] =>
  263. * [Extra] => auto_increment )
  264. * TODO: use cache SES
  265. */
  266. function get_sql_types($sql_table) {
  267. $db = DB::getDB();
  268. $types = array();
  269. $sql = "show fields from `" . $sql_table . "`;";
  270. $res = $db->query($sql);
  271. while ($r = $db->fetch($res)) {
  272. $field = $r->Field;
  273. unset($r->Field);
  274. $types [$field]= $r;
  275. }
  276. return $types;
  277. }
  278. /**
  279. * @returns array of wsk id's tree flat - TODO:
  280. */
  281. function get_wskazniki_parent_tables($wskazniki) {
  282. $ret = array();
  283. if (empty($wskazniki)) {
  284. return $ret;
  285. }
  286. $komorka_ids = array();
  287. foreach ($wskazniki as $k_id => $v_wsk) {
  288. if ($v_wsk->TYPE == 'KOMORKA') {// TODO: 'DANE' under 'KOMORKA'
  289. $komorka_ids []= $v_wsk->ID;
  290. }
  291. }
  292. if (empty($komorka_ids)) {
  293. return $ret;
  294. }
  295. $sql = "select z.`ID`, z.`TYPE`, z.`DESC`
  296. , pz.`ID` as p__ID, pz.`TYPE` as p__TYPE, pz.`DESC` as p__DESC
  297. from `CRM_LISTA_ZASOBOW` as z
  298. left join `CRM_LISTA_ZASOBOW` as pz on(pz.`ID`=z.`PARENT_ID` and pz.`A_STATUS` in ('WAITING','NORMAL'))
  299. where
  300. z.`ID` in(" . implode(",", $komorka_ids) . ")
  301. and z.`A_STATUS` in ('WAITING','NORMAL')
  302. ";
  303. $res = $this->_db->query($sql);
  304. while ($r = $this->_db->fetch($res)) {
  305. // TODO: use TYPE = DANE
  306. if ($r->TYPE == 'TABELA') {
  307. $ret [$r->ID]= (object)array('ID'=>$r->ID, 'DESC'=>$r->DESC, 'TYPE'=>$r->TYPE);
  308. } else if ($r->p__TYPE == 'TABELA') {
  309. $ret [$r->p__ID]= (object)array('ID'=>$r->p__ID, 'DESC'=>$r->p__DESC, 'TYPE'=>$r->p__TYPE);
  310. $ret [$r->ID]= $r->p__ID;
  311. }
  312. }
  313. return $ret;
  314. }
  315. }