ProcesLogModel.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. <?php
  2. Lib::loadClass('BaseModel');
  3. class ProcesLogModel extends BaseModel {
  4. function __construct($dao) {
  5. parent::__construct($dao);
  6. $this->fields['ID'] = '';
  7. $this->fields['TYPE'] = '';
  8. $this->fields['ID_PROCES_INIT'] = '';
  9. $this->fields['ID_STEP'] = '';// ID procesu
  10. $this->fields['ID_STEP_WSKAZNIK'] = '';// ID wskaznika w danym procesie ID_STEP
  11. $this->fields['ID_STANOWISKA'] = '';// ID stanowisk kroku aktualnie do wykonania
  12. $this->fields['ID_USER'] = '';// 0 - nie przypisane - patrz ID_STANOWISKA, przypisane do konkretnego stanowiska
  13. $this->fields['ID_KEY'] = '';// ID from external table - defined in ID_STEP_WSKAZNIK (parent zasob table), TODO: clear after next step? Yes
  14. $this->fields['A_UPDATE_ZASOB_ID'] = '';
  15. $this->fields['A_UPDATE_HIST_ID'] = '';
  16. $this->fields['log'] = '';
  17. //$this->fields['TIMESTAMPP'] = '';// automatic field - not used
  18. $this->fields['A_RECORD_CREATE_DATE'] = '';
  19. $this->fields['A_RECORD_CREATE_AUTHOR'] = '';
  20. $this->fields['A_RECORD_UPDATE_DATE'] = '';
  21. $this->fields['A_RECORD_UPDATE_AUTHOR'] = '';
  22. }
  23. /**
  24. * Get list of next step (Proces).
  25. * @returns array [ID] = DESC
  26. * TODO: use IF_GO_TO
  27. */
  28. function get_next_step() {
  29. $ret = array();
  30. if (!$this->get('ID')) {
  31. return $ret;
  32. }
  33. $cur_step_id = $this->get('ID_STEP');
  34. if ($cur_step_id <= 0) {
  35. $cur_step_id = $this->get('ID_PROCES_INIT');
  36. }
  37. $ret = $this->dao->get_proces_next_steps($cur_step_id);
  38. return $ret;
  39. }
  40. /**
  41. * Get list of next step wskaznik (Proces).
  42. * @returns wskaznik
  43. */
  44. function get_next_step_wskaznik() {
  45. $ret = null;
  46. if (!$this->get('ID')) {
  47. return $ret;
  48. }
  49. $cur_step_id = $this->get('ID_STEP');
  50. if ($cur_step_id <= 0) {
  51. $cur_step_id = $this->get('ID_PROCES_INIT');
  52. }
  53. $wsk_grouped = $this->get_wskazniki_grouped($cur_step_id);
  54. if (empty($wsk_grouped)) {
  55. return $ret;
  56. }
  57. $wsk_grouped_keys = array_keys($wsk_grouped);
  58. $cur_step_wsk_id = $this->get('ID_STEP_WSKAZNIK');
  59. // check if current id_step_wsk is set to last group
  60. if ($cur_step_wsk_id > 0) {
  61. if ($cur_step_wsk_id == end($wsk_grouped_keys) && $this->get('ID_KEY') > 0) {
  62. return $ret;// is finished
  63. }
  64. }
  65. // set default id_step_wsk if not set
  66. if (!in_array($cur_step_wsk_id, $wsk_grouped_keys)) {
  67. $cur_step_wsk_id = reset($wsk_grouped_keys);
  68. }
  69. // find current and return next
  70. $found_current = false;
  71. foreach ($wsk_grouped as $k_wsk_id => $v_edit) {
  72. if ($found_current) {
  73. return $v_edit;
  74. }
  75. if ($k_wsk_id == $cur_step_wsk_id) {
  76. $found_current = true;
  77. }
  78. }
  79. return $ret;
  80. }
  81. /**
  82. *
  83. * @retruns array of wskazniki - grouped
  84. * [first wsk id] => ProcesLogEdit
  85. * Cache in PROCES_LOG_WSK_GROUPED = array( log_id => array( step_id => array( wsk_id => array from ProcesLogEdit->to_array() ) ) )
  86. */
  87. function get_wskazniki_grouped($step_id) {
  88. static $_wsk;// TODO: save to ses by $this->get('ID') and $step_id
  89. if (!is_array($_wsk)) $_wsk = array();
  90. Lib::loadClass('ProcesLogEdit');
  91. if (!empty($_wsk[$step_id])) {
  92. // update _last_id_key in ProcesLogEdit
  93. foreach ($_wsk[$step_id] as $k_wsk_id => $v_plog_edit) {
  94. $v_plog_edit->set_last_id_key($this->get('ID_KEY'));
  95. $_SESSION['PROCES_LOG_WSK_GROUPED'][$this->get('ID')][$step_id][$k_wsk_id] = $v_plog_edit->to_array();
  96. }
  97. return $_wsk[$step_id];
  98. }
  99. $cache_on = false;
  100. if (empty($_SESSION['PROCES_LOG_WSK_GROUPED'])) $_SESSION['PROCES_LOG_WSK_GROUPED'] = array();
  101. if ($cache_on && !empty($_SESSION['PROCES_LOG_WSK_GROUPED'][$this->get('ID')][$step_id])) {
  102. $_wsk[$step_id] = array();
  103. foreach ($_SESSION['PROCES_LOG_WSK_GROUPED'][$this->get('ID')][$step_id] as $k_wsk_id => $v_plog_edit_array) {
  104. $_wsk[$step_id][$k_wsk_id] = new ProcesLogEdit($this->get('ID'), $k_wsk_id);
  105. $_wsk[$step_id][$k_wsk_id]->from_array($v_plog_edit_array);
  106. }
  107. DEBUG_S(1, "wsk from ses:", $_SESSION['PROCES_LOG_WSK_GROUPED'][$this->get('ID')][$step_id], __FILE__, __FUNCTION__, __LINE__);
  108. DEBUG_S(1, "wsk from ses done:", $_wsk[$step_id], __FILE__, __FUNCTION__, __LINE__);
  109. }
  110. else {
  111. $_wsk[$step_id] = array();
  112. $wskazniki = $this->dao->get_wskazniki($step_id);
  113. $wskazniki_tables = $this->dao->get_wskazniki_parent_tables($wskazniki);
  114. DEBUG_S(1, "wskazniki:", $wskazniki, __FILE__, __FUNCTION__, __LINE__);
  115. DEBUG_S(1, "wskazniki_tables:", $wskazniki_tables, __FILE__, __FUNCTION__, __LINE__);
  116. // TYPE to search parents: KOMORKA, TODO: add DANE
  117. $children_types = array('KOMORKA');
  118. if (!empty($wskazniki_tables)) {
  119. // add _tbl_parent
  120. foreach ($wskazniki as $k_id => $v_wsk) {
  121. if (!in_array($v_wsk->TYPE, $children_types)) {
  122. continue;// eg. pomin TYPE='TABELA'
  123. }
  124. if (array_key_exists($v_wsk->ID, $wskazniki_tables)) {
  125. $v_wsk->_tbl_parent = $wskazniki_tables[$v_wsk->ID];
  126. // TODO: find parent zasob ID
  127. if (!is_object($v_wsk->_tbl_parent)) {
  128. $v_wsk->_tbl_parent = $wskazniki_tables[$v_wsk->_tbl_parent];
  129. }
  130. if (!is_object($wskazniki[$k_id]->_tbl_parent)) {
  131. $v_wsk->_tbl_parent = $wskazniki_tables[$v_wsk->_tbl_parent];
  132. }
  133. } else {
  134. // no zasob ID in tables tree
  135. }
  136. }
  137. DEBUG_S(1, "wskazniki 2:", $wskazniki, __FILE__, __FUNCTION__, __LINE__);
  138. $last_group_wsk_id = 0;
  139. foreach ($wskazniki as $k_id => $v_wsk) {
  140. if (isset($v_wsk->_tbl_parent)) {
  141. if ($last_group_wsk_id == 0) {
  142. $last_group_wsk_id = $k_id;
  143. }
  144. if (!array_key_exists($last_group_wsk_id, $_wsk[$step_id])) {
  145. $_wsk[$step_id][$last_group_wsk_id] = new ProcesLogEdit($this->get('ID'), $last_group_wsk_id);
  146. $_wsk[$step_id][$last_group_wsk_id]->set_tbl_zasob_id($v_wsk->_tbl_parent->ID);
  147. // set_last_id_key if action is done - ID_KEY is set
  148. if ($this->get('ID_KEY') > 0 && $last_group_wsk_id == $this->get('ID_STEP_WSKAZNIK')) {
  149. $_wsk[$step_id][$last_group_wsk_id]->set_last_id_key($this->get('ID_KEY'));
  150. }
  151. }
  152. $_wsk[$step_id][$last_group_wsk_id]->add_komorka($v_wsk);
  153. } else {
  154. $last_group_wsk_id = 0;
  155. }
  156. }
  157. }
  158. DEBUG_S(1, "wsk[" . $step_id . "]:", $_wsk[$step_id], __FILE__, __FUNCTION__, __LINE__);
  159. // save data into SES cache
  160. if (!empty($_wsk[$step_id])) {
  161. foreach ($_wsk[$step_id] as $k_wsk_id => $v_plog_edit) {
  162. $sql_types = $this->dao->get_sql_types($v_plog_edit->get_table_name());
  163. DEBUG_S(1, "sql_types:", $sql_types, __FILE__, __FUNCTION__, __LINE__);
  164. $v_plog_edit->set_komorki_sql_types($sql_types);
  165. $_SESSION['PROCES_LOG_WSK_GROUPED'][$this->get('ID')][$step_id][$k_wsk_id] = $v_plog_edit->to_array();
  166. }
  167. $this->_garbage_collection();
  168. DEBUG_S(1, "wsk[" . $step_id . "] save into SES:", $_SESSION['PROCES_LOG_WSK_GROUPED'][$this->get('ID')][$step_id], __FILE__, __FUNCTION__, __LINE__);
  169. }
  170. }
  171. return $_wsk[$step_id];
  172. }
  173. /**
  174. * Garbage collection (GC) is a form of automatic memory management.
  175. */
  176. function _garbage_collection() {
  177. $max_cache_log_count = 10;// save 3 last log
  178. $max_cache_step_count = 10;// save 10 last steps
  179. if (empty($_SESSION['PROCES_LOG_WSK_GROUPED'])) $_SESSION['PROCES_LOG_WSK_GROUPED'] = array();
  180. if (count($_SESSION['PROCES_LOG_WSK_GROUPED']) > $max_cache_log_count) {
  181. // TODO: remove from the beginnig of array - without current log id $this->get('ID')
  182. DEBUG_S(1, "TODO: PROCES_LOG_WSK_GROUPED remove LOG $max_cache_log_count:", $_SESSION['PROCES_LOG_WSK_GROUPED'], __FILE__, __FUNCTION__, __LINE__);
  183. }
  184. if (count($_SESSION['PROCES_LOG_WSK_GROUPED'][$this->get('ID')]) > $max_cache_step_count) {
  185. // TODO: remove from the beginnig of array - without current step id
  186. DEBUG_S(1, "TODO: PROCES_LOG_WSK_GROUPED remove STEP $max_cache_step_count:", $_SESSION['PROCES_LOG_WSK_GROUPED'][$this->get('ID')], __FILE__, __FUNCTION__, __LINE__);
  187. }
  188. }
  189. /**
  190. *
  191. * @returns ProcesLogEdit object or null
  192. */
  193. function get_current_edit() {
  194. $ret = null;
  195. if ($this->get('TYPE') == 'END') {
  196. return $ret;
  197. }
  198. $wsk_group = $this->get_wskazniki_grouped($this->get('ID_STEP'));
  199. if (empty($wsk_group)) {
  200. return $ret;
  201. }
  202. $cur_wsk_id = null;
  203. if ($this->get('ID_STEP_WSKAZNIK') > 0) {
  204. if (array_key_exists($this->get('ID_STEP_WSKAZNIK'), $wsk_group)) {
  205. $cur_wsk_id = $this->get('ID_STEP_WSKAZNIK');
  206. } else {
  207. // TODO: Error w danych ID_STEP_WSKAZNIK!
  208. }
  209. }
  210. if (!$cur_wsk_id) {
  211. $cur_wsk_id = reset(array_keys($wsk_group));
  212. }
  213. if ($cur_wsk_id) {
  214. $ret = $wsk_group[$cur_wsk_id];
  215. }
  216. return $ret;
  217. }
  218. /**
  219. * Check if current step is finished.
  220. * @returns boolean.
  221. */
  222. function is_step_finished() {
  223. // TODO: ID_STEP_WSKAZNIK is in the end of array of wskazniki grouped by TABELA and record ID is saved, found (ID_KEY)
  224. $wsk_grouped = $this->get_wskazniki_grouped($this->get('ID_STEP'));
  225. if (empty($wsk_grouped)) {
  226. return true;
  227. } else {
  228. if ($this->get('ID_STEP_WSKAZNIK') > 0) {
  229. if ($this->get('ID_STEP_WSKAZNIK') == end(array_keys($wsk_grouped)) && $this->get('ID_KEY') > 0) {
  230. return true;
  231. }
  232. } else {
  233. return false;
  234. }
  235. }
  236. return false;
  237. }
  238. /**
  239. * Check if allow to move to next step.
  240. * @returns boolean.
  241. */
  242. function allow_next_step($next_id) {
  243. if (!$this->is_step_finished()) {
  244. return false;
  245. }
  246. $next_processes = $this->get_next_step();
  247. return array_key_exists($next_id, $next_processes);
  248. }
  249. /**
  250. * Check if allow to move to next step.
  251. * @returns boolean.
  252. */
  253. function allow_next_step_wskaznik($next_wsk_id) {
  254. if ($this->is_step_finished()) {
  255. return false;
  256. }
  257. $next_step_wsk = $this->get_next_step_wskaznik();
  258. if ($next_step_wsk != null && $next_step_wsk->get_log_wsk_id() == $next_wsk_id) {
  259. return true;
  260. }
  261. return false;
  262. }
  263. /**
  264. * Check if allow to move to end.
  265. * @returns boolean.
  266. */
  267. function allow_end() {
  268. $next_step_list = $this->get_next_step();
  269. if (empty($next_step_list)) {
  270. if ($this->is_step_finished()) {
  271. return true;
  272. }
  273. }
  274. return false;
  275. }
  276. /**
  277. * Change ProcesLog state to next step.
  278. * @returns true / false
  279. */
  280. function go_to_next_step($next_id) {
  281. // get proces
  282. // get wskazniki for this proces
  283. $wskazniki = ProcesHelper::get_wskazniki($next_id);
  284. $wsk_stanowiska = array();// [ID wskaznika] = ID zasobu (stanowiska)
  285. foreach ($wskazniki as $k => $v_wsk) {
  286. if ($v_wsk->TYPE == 'STANOWISKO') {
  287. $wsk_stanowiska [$v_wsk->CW_ID] = $v_wsk->ID;
  288. }
  289. }
  290. if (!empty($wsk_stanowiska)) {
  291. $this->set('ID_USER', 0);
  292. $this->set('ID_STANOWISKA', implode(",", array_values($wsk_stanowiska)));
  293. }
  294. $this->set('TYPE', 'STEP');// always set to STEP after update
  295. $this->set('ID_STEP', $next_id);
  296. $this->set('ID_STEP_WSKAZNIK', '');
  297. $this->set('ID_KEY', 0);// clear key ID - used only in _HIST
  298. $this->set('A_UPDATE_ZASOB_ID', 0);// clear key A_UPDATE_ZASOB_ID - used only in _HIST
  299. $this->set('A_UPDATE_HIST_ID', 0);// clear key A_UPDATE_HIST_ID - used only in _HIST
  300. if ($this->dao->save($this)) {
  301. return true;
  302. }
  303. return false;
  304. }
  305. /**
  306. * Change ProcesLog state to next step wskaznik.
  307. * @returns true / false
  308. */
  309. function go_to_next_step_wskaznik($next_wsk_id) {
  310. $wskazniki_grouped = $this->get_wskazniki_grouped($this->get('ID_STEP'));
  311. if (!array_key_exists($next_wsk_id, $wskazniki_grouped)) {
  312. return false;
  313. }
  314. // TODO: verify if $next_wsk_id is after current wskaznik
  315. $this->set('TYPE', 'STEP');// always set to STEP after update
  316. //$this->set('ID_STEP', );// dont change
  317. $this->set('ID_STEP_WSKAZNIK', $next_wsk_id);
  318. $this->set('ID_KEY', 0);// clear key ID - used only in _HIST
  319. $this->set('A_UPDATE_ZASOB_ID', 0);// clear key A_UPDATE_ZASOB_ID - used only in _HIST
  320. $this->set('A_UPDATE_HIST_ID', 0);// clear key A_UPDATE_HIST_ID - used only in _HIST
  321. if ($this->dao->save($this)) {
  322. return true;
  323. }
  324. return false;
  325. }
  326. /**
  327. * Change ProcesLog state quit.
  328. * @returns true / false
  329. */
  330. function go_to_end() {
  331. $this->set('TYPE', 'END');
  332. $this->set('ID_STEP', 0);
  333. $this->set('ID_STEP_WSKAZNIK', 0);
  334. $this->set('ID_STANOWISKA', '');
  335. $this->set('ID_KEY', 0);
  336. $this->set('A_UPDATE_ZASOB_ID', 0);
  337. $this->set('A_UPDATE_HIST_ID', 0);
  338. if ($this->dao->save($this)) {
  339. return true;
  340. }
  341. return false;
  342. }
  343. }