proces.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. <?php
  2. Lib::loadClass('ProcesHelper');
  3. Lib::loadClass('DebugExecutionTime');
  4. Lib::loadClass('UI');
  5. /**
  6. * @param $_GET['EDIT'] - ID rekordu do edycji (inline)
  7. * @param $_GET['EDIT_ID'] - ID rekordu do edycji (inline)
  8. */
  9. function task_CRM_PROCES() {
  10. User_procesy5_check_access();
  11. UI::setTitleJsTag("⋋ Procesy (drzewo)");
  12. $_GET['ADM_AREA'] = V::get('ADM_AREA', "BN%", $_GET);
  13. $_GET['EDIT'] = V::get('EDIT', 0, $_GET, 'int');
  14. if (($edit_id = V::get('EDIT_ID', 0, $_GET, 'int')) > 0) {
  15. $_GET['EDIT'] = $edit_id;
  16. }
  17. $tbl = 'CRM_PROCES';
  18. $DBG_TIME = ('1' == V::get('DBG_TIME', '', $_GET));
  19. $dbgExecTime = new DebugExecutionTime();
  20. if ($DBG_TIME) $dbgExecTime->activate();
  21. $dbgExecTime->log('start');
  22. $tbl_conf = Column::getInstance($tbl);
  23. if (!$tbl_conf) {
  24. echo'<p class="red">'."Brak konfiguracji dla ".$tbl."!".'</p>';
  25. return;
  26. }
  27. $dbgExecTime->log('Column::getInstance');
  28. $visible_cols = $tbl_conf->getVisibleFieldList();
  29. // form decorators:
  30. $tbl_search_params = array();
  31. $tbl_search_params['ID'] = 'ID';
  32. $tbl_search_params['PARENT_ID'] = 'P_ID';
  33. $tbl_search_params['TYPE'] = 'TYPE';
  34. $tbl_search_params['DESC'] = 'DESC';
  35. $tbl_search_params['OPIS'] = 'OPIS';
  36. $tbl_search_params['DESC_PL'] = 'DESC_PL';
  37. $tbl_search_params['TAGS'] = 'TAGS';
  38. $tbl_search_params['A_STATUS'] = 'A_STATUS';
  39. $tbl_search_params['L_APPOITMENT_USER'] = 'L_APPOITMENT_USER';
  40. $tbl_search_params['L_APPOITMENT_DATE'] = 'L_APPOITMENT_DATE';
  41. $tbl_search_params['L_APPOITMENT_INFO'] = 'L_APPOITMENT_INFO';
  42. $tbl_search_params['A_ADM_COMPANY'] = 'A_ADM_COMPANY';
  43. $tbl_search_params['IF_TRUE_GOTO'] = 'IF_TRUE_GOTO';
  44. $tbl_search_params['IF_TRUE_GOTO_FLAG'] = 'IF_TRUE_GOTO_FLAG';
  45. $tbl_search_params['A_RECORD_CREATE_AUTHOR'] = 'A_RECORD_CREATE_AUTHOR';
  46. $tbl_search_params['A_RECORD_CREATE_DATE'] = 'A_RECORD_CREATE_DATE';
  47. $tbl_search_params['A_RECORD_UPDATE_AUTHOR'] = 'A_RECORD_UPDATE_AUTHOR';
  48. $tbl_search_params['A_RECORD_UPDATE_DATE'] = 'A_RECORD_UPDATE_DATE';
  49. Lib::loadClass('FilterLast');
  50. $filter_last = new FilterLast( $_REQUEST, $tbl, 'session');
  51. $filter_last->add_filter( 'last_id', array('filtr_id','filtr_search_id','ID','EDIT'), "Ostatnio uzywane ID", 'int' );
  52. $filter_last->_save_args();// save args from request
  53. {// TABLE - Filter
  54. $tbl = 'CRM_PROCES';
  55. $tbl_filter = new stdClass();
  56. $tbl_filter->key = 'tbl-'.$tbl;
  57. $tbl_filter->filters = array();
  58. // TODO: stronicowanie $tbl_filter->filters: _page_nr, _limit, _total (need sql - only after change params)
  59. // read args from session, or get dafault values
  60. foreach ($tbl_search_params as $k => $req_field_name) {
  61. $tbl_filter->filters[$req_field_name] = V::get($req_field_name, '%', $_SESSION[$tbl_filter->key]);
  62. }
  63. // read args from $_GET if isset
  64. foreach ($tbl_search_params as $k => $req_field_name) {
  65. if (isset($_GET[$req_field_name])) {
  66. $tbl_filter->filters[$req_field_name] = $_GET[$req_field_name];
  67. }
  68. }
  69. // save args in session and $_GET
  70. foreach ($tbl_filter->filters as $req_field_name => $v) {
  71. $_SESSION[$tbl_filter->key][$req_field_name] = $v;
  72. $_GET[$req_field_name] = $v;
  73. }
  74. echo'<div style="border:1px solid red; display:none;">';
  75. foreach ($tbl_filter->filters as $req_field_name => $v) {
  76. echo'<br />arg['.$req_field_name.'] = '.$v;
  77. }
  78. echo'</div>';
  79. }// TABLE - Filter
  80. {// TREE - Filter
  81. $tbl = 'CRM_PROCES';
  82. Lib::loadClass('FilterFactory');
  83. $tree_procesy_filter = FilterFactory::build($tbl);
  84. $tree_procesy_filter->setArgs($_GET);
  85. $tree_procesy_filter->set_trash('filtr_id', -1);
  86. {// zapisz stan - ajax function to save filters stan and opened tree nodes, etc.
  87. $filtr_ses_key = $tree_procesy_filter->_key;
  88. $cookie_key = 'TREE_'.$tbl;
  89. $profile_key = 'Filtr_'.$filtr_ses_key;
  90. if (V::get('save_profile', '', $_REQUEST)) {
  91. $profile_val = array();
  92. $profile_val['filtr'] = $_SESSION[$filtr_ses_key];
  93. $profile_val['tree'] = $_COOKIE[$cookie_key];// cookie key from Tree class - 'TREE_'. $tbl @see __construct
  94. User::setProfile($profile_key, $profile_val);
  95. $ret = User::saveProfile();
  96. }
  97. else if (V::get('load_profile', '', $_REQUEST)) {
  98. //User::loadProfile();// proile is loaded in USERS_COLUMN_INIT after login
  99. $profile_val = User::getProfile($profile_key);
  100. $_COOKIE[$cookie_key] = $profile_val['tree'];
  101. echo'<script type="text/javascript">'."
  102. jQuery(document).ready(function(){
  103. jQuery.cookie('".$cookie_key."','" . $_COOKIE[$cookie_key] . "');
  104. });
  105. ".'</script>';
  106. $_SESSION[$filtr_ses_key] = $profile_val['filtr'];
  107. $tree_procesy_filter->_read_args( true );// force load args from session
  108. }
  109. }// zapisz stan - ajax function to save filters stan and opened tree nodes, etc.
  110. $tree_procesy_filter->_save_args();// force save_args (execute at the end of show_filters)
  111. echo'<div style="border:1px solid red; display:none;">';
  112. foreach ($tree_procesy_filter->filters as $arg => $v) {
  113. echo'<br />arg['.$arg.'] = '.$tree_procesy_filter->get_arg($arg);
  114. }
  115. echo'</div>';
  116. }// TREE - Filter
  117. echo'<h1>';
  118. echo App::link("Procesy", array('task'=>App::get_task(), 'filtr_id'=>''));
  119. if ($tree_procesy_filter->is_trash()) {//$_GET['filtr_id'] == -1) {
  120. echo ' &raquo; '."Kosz";
  121. echo " " . App::link("czyść", array('task'=>App::get_task(), 'function_init'=>"fun_CRM_PROCES_clean_trash"), array('title'=>"Ustaw status rekordów w koszu na DELETED", 'class'=>'btn-p5', 'style'=>'font-size:13px;'));
  122. } else if ($tree_procesy_filter->get_arg('filtr_id') > 0) {//$_GET['filtr_id'] > 0) {
  123. echo ' &raquo; '."Proces [".$tree_procesy_filter->get_arg('filtr_id')."]";
  124. }
  125. echo '<span class="pull-right">'.App::link("Mapa procesów", array('task'=>'VIEW_PROCES_MAP')).'</span>';
  126. echo'</h1>';
  127. $procesZasobId = ProcesHelper::getZasobTableID('CRM_PROCES');
  128. echo App::link("Pokaz Tabele", "index.php?MENU_INIT=VIEWTABLE_AJAX&ZASOB_ID={$procesZasobId}");
  129. echo "<br>".App::link("Dodaj nowy rekord", "index.php?MENU_INIT=VIEWTABLE_AJAX&ZASOB_ID={$procesZasobId}#CREATE");
  130. echo'<br />';
  131. $TREE = array();
  132. echo'<div id="tree"></div>';
  133. session_write_close();// End the current session and store session data. Below $_SESSION is read-only.
  134. $dbgExecTime->log('show...');
  135. if ($tree_procesy_filter->get_arg('filtr_view') == 'NOWY') {
  136. $filter_last->show_filters();
  137. echo'<div class="filters">';
  138. echo'<form action="'."".'" method="get" style="margin:0;padding:0;">';
  139. echo'<input type="hidden" name="task" value="'.App::get_task().'" />';
  140. $tree_procesy_filter->show_filters();
  141. {// save profile - btn
  142. echo'<nobr>';
  143. echo'<input type="submit" name="save_profile" value="'."Save".'" title="'."Save profile".'" />';
  144. echo'</nobr>';
  145. }
  146. {// load profile - btn
  147. echo'<nobr>';
  148. echo'<input type="submit" name="load_profile" value="'."Load".'" title="'."Load profile".'" />';
  149. echo'</nobr>';
  150. }
  151. echo'</form>';
  152. echo'</div>';// .filters
  153. $filter_hist = (isset($_SESSION['TREE-PROCESY']['filter_hist']))? $_SESSION['TREE-PROCESY']['filter_hist'] : array();
  154. if (!empty($filter_hist)) {
  155. echo'<form style="display:inline" action="" method="GET">';
  156. echo'<input type="hidden" name="task" value="'.App::get_task().'">';
  157. echo'<select name="filter_hist_id">';
  158. foreach ($filter_hist as $k => $v) {
  159. echo'<option value="'.$k.'">'.$v.'</option>';
  160. }
  161. echo'</select>';
  162. echo'</form>';
  163. }
  164. $tree = new Tree( $tbl );
  165. $tree->setProfiler($dbgExecTime);
  166. $clbk = 'tree_callback__show_item_from_'.$tbl;
  167. $tree->set_param('show_item_callback', $clbk);
  168. $tree->set_param('rozwin', ($tree_procesy_filter->get_arg('filtr_drzewo') == 'ROZWIN'));
  169. $tree->set_param('rozwin_proces', ($tree_procesy_filter->get_arg('filtr_procesy') == '+'));
  170. $tree->set_param('rozwin_zasoby', ($tree_procesy_filter->get_arg('filtr_zasoby') == '+'));
  171. $tree->set_param('rozwin_opis', ($tree_procesy_filter->get_arg('filtr_opis') == '+'));
  172. $tree->set_param('filtr_img', ($tree_procesy_filter->get_arg('filtr_img') == '+'));
  173. $tree->set_param('style', $tree_procesy_filter->get_arg('filtr_view'));
  174. $tree->set_param('search_id', $tree_procesy_filter->get_arg('filtr_search_id'));
  175. $tree->set_param('ProcesTblId', ProcesHelper::getZasobTableID('CRM_PROCES'));
  176. $tree->set_param('ZasobTblId', ProcesHelper::getZasobTableID('CRM_LISTA_ZASOBOW'));
  177. $tree->set_param('WskaznikTblId', ProcesHelper::getZasobTableID('CRM_WSKAZNIK'));
  178. $tblPytaniaId = ProcesHelper::getZasobTableID('CRM_TESTY_PYTANIA');
  179. if ($tblPytaniaId > 0) {
  180. $userAcl = User::getAcl();
  181. $userAcl->fetchGroups();
  182. if ($userAcl->hasTableAcl($tblPytaniaId)) {
  183. $tblAcl = $userAcl->getTableAcl($tblPytaniaId);
  184. $tblAcl->init($forceTblAclInit = false);
  185. $tree->set_param('HasAclPytaniaTbl', true);
  186. }
  187. $tree->set_param('PytaniaTblId', $tblPytaniaId);
  188. }
  189. //TODO: check perm to edit
  190. if ($tree_procesy_filter->is_trash()) $tree->set_param('is_trash', true);
  191. $tree->set_param('editable', ($tree_procesy_filter->get_arg('filtr_edit') == 'TAK'));
  192. if ($tree_procesy_filter->get_arg('filtr_ajax') == 'TAK') {
  193. $tree->_limit = 300;
  194. $tree->_deep_limit = 1;
  195. }
  196. if ($tree_procesy_filter->get_arg('filtr_id') != 0) {
  197. $tree->showSubTree( $tree_procesy_filter->get_arg('filtr_id') );
  198. $dbgExecTime->log('done tree->showSubTree()');
  199. } else if ($tree_procesy_filter->get_arg('filtr_search_id') > 0) {
  200. $tree->showSearchNode( $tree_procesy_filter->get_arg('filtr_search_id') );
  201. $dbgExecTime->log('done tree->showSearchNode()');
  202. } else {
  203. $tree->show();
  204. $dbgExecTime->log('done tree->show()');
  205. }
  206. }
  207. else {
  208. echo'<pre>';
  209. {
  210. echo'<table cellspacing="0" cellpadding="1">';
  211. echo'<tr>';
  212. echo'<td>';
  213. echo "DRZEWO ";
  214. if ($_SESSION['ZWIN_DRZEWO']) {
  215. echo App::link("ROZWIN", "?ROZWIN_DRZEWO=1#tree");
  216. } else {
  217. echo App::link("ZWIN", "?ZWIN_DRZEWO=1#tree");
  218. }
  219. echo'</td>';
  220. echo'<td>';
  221. echo "&nbsp;/&nbsp;";
  222. echo'</td>';
  223. echo'<td>';
  224. echo "PROCESY ";
  225. if ($_SESSION['ROZWIN']) {
  226. echo App::link("ZWIN", "?TREE_ZWIN=1#tree");
  227. } else {
  228. echo App::link("ROZWIN", "?TREE_ROZWIN=1#tree");
  229. }
  230. echo'</td>';
  231. echo'<td>';
  232. echo "&nbsp;/&nbsp;";
  233. echo'</td>';
  234. echo'<td>';
  235. echo "ZASOBY ";
  236. if ($_SESSION['ROZWIN_ZASOBY']) {
  237. echo App::link("ZWIN", "?TREE_ZASOBY_ZWIN=1#tree");
  238. } else {
  239. echo App::link("ROZWIN", "?TREE_ZASOBY_ROZWIN=1#tree");
  240. }
  241. echo'</td>';
  242. echo'<td>';
  243. echo "&nbsp;/&nbsp;";
  244. echo'</td>';
  245. echo'<td>';
  246. echo "OPISY ";
  247. if ($_SESSION['ROZWIN_OPIS']) {
  248. echo App::link("ZWIN", "?TREE_OPISY_ZWIN=1#tree");
  249. } else {
  250. echo App::link("ROZWIN", "?TREE_OPISY_ROZWIN=1#tree");
  251. }
  252. echo'</td>';
  253. echo'<td>';
  254. echo "&nbsp;/&nbsp;";
  255. echo'</td>';
  256. echo'<td>';
  257. $js = "window.location.href='#TREE'+this.form.id.value;return false;";
  258. echo '<form style="display:inline" action="#" method="GET">';
  259. echo "Proces ID: ";
  260. echo'<input type="text" name="id" value="" size="5" />';
  261. echo'<button onclick="'.$js.'">'."Wyszukaj ID".'</button>';
  262. echo'</form>'."\n";
  263. echo'</td>';
  264. echo'<td>';
  265. echo "&nbsp;/&nbsp;";
  266. echo'</td>';
  267. echo'<td>';
  268. echo'Widok ';
  269. echo App::link("NOWY", "?filtr_view=NOWY");
  270. echo'</td>';
  271. {
  272. echo'<td>';
  273. echo "&nbsp;/&nbsp;";
  274. echo'</td>';
  275. echo'<td>';
  276. echo'<form action="" method="GET" style="display:inline">';
  277. echo'<input type="hidden" name="'."task".'" value="'.App::get_task().'" />';
  278. echo'<nobr>';
  279. echo'<input type="submit" name="load_profile" value="'."Load".'" title="'."Load profile".'" />';
  280. echo'</nobr>';
  281. echo'</form>';
  282. echo'</td>';
  283. }
  284. echo'</tr>';
  285. echo'</table>';// btns
  286. }
  287. tree_znajdz(0, 1, 0);
  288. echo'</pre>';
  289. }
  290. if($DBG_TIME){
  291. $dbgExecTime->log('end');
  292. $dbgExecTime->printDebug();
  293. }
  294. }
  295. function fun_CRM_PROCES_ADD() {
  296. $redirect = "?task=".App::get_task();
  297. $msg = '';
  298. $sql_obj = new stdClass();
  299. $tbl_name = 'CRM_PROCES';
  300. Lib::loadClass('Column');
  301. $tbl_conf = Column::getInstance($tbl_name);
  302. if (!$tbl_conf) {
  303. return;
  304. }
  305. foreach ($tbl_conf->getFieldList() as $field_id => $field_name) {
  306. if ($tbl_conf->allowCreate($field_name)) {
  307. $post_arg_name = 'ADD_' . $field_name;
  308. if (isset($_POST[$post_arg_name])) {
  309. $sql_obj->$field_name = $_POST[$post_arg_name];
  310. }
  311. }
  312. }
  313. // nowy na koniec listy
  314. $sql = "select max(t.`SORT_PRIO`) as MAX_SORT_PRIO
  315. from `CRM_PROCES` as t
  316. where
  317. t.`PARENT_ID`='".$sql_obj->PARENT_ID."'
  318. ";
  319. $res = DB::query( $sql );
  320. while ($r = DB::fetch( $res )) {
  321. $sql_obj->SORT_PRIO = $r->MAX_SORT_PRIO + 1;
  322. }
  323. //TODO: post verify
  324. $new_id = DB::ADD_NEW_OBJ('CRM_PROCES', $sql_obj);
  325. if (V::get('DEBUG', '', $_SESSION)) App::add_msg("PASSED(1) NEW RECORD ID($new_id)");
  326. if ($new_id > 0) {
  327. App::add_msg("Dodano nowy rekord (ID ".$new_id.")");
  328. $redirect .= "&EDIT=".$new_id;
  329. }
  330. else {
  331. App::add_error("Error przy dodawaniu rekordu!");
  332. }
  333. App::redirect($redirect, $msg);
  334. }
  335. /**
  336. * @param $_POST['EDIT'] - ID rekordu
  337. */
  338. function fun_CRM_PROCES_EDIT() {
  339. $redirect = "?task=".App::get_task();
  340. $msg = '';
  341. $sql_obj = new stdClass();
  342. $tbl_name = 'CRM_PROCES';
  343. $sql_obj->ID = V::get('EDIT_ID', '', $_POST, 'int');
  344. // TODO: fields can be hidden
  345. Lib::loadClass('Column');
  346. $tbl_conf = Column::getInstance($tbl_name);
  347. if (!$tbl_conf) {
  348. return;
  349. }
  350. foreach ($tbl_conf->getFieldList() as $field_id => $field_name) {
  351. if ($tbl_conf->allowWrite($field_name)) {
  352. $post_arg_name = 'EDIT_' . $field_name;
  353. if (isset($_POST[$post_arg_name])) {
  354. // TODO: convert field types?
  355. $sql_obj->$field_name = $_POST[$post_arg_name];
  356. }
  357. }
  358. }
  359. $redirect .= "&EDIT=".$sql_obj->ID;
  360. //TODO: post verify
  361. $id = $sql_obj->ID;
  362. $affected = DB::UPDATE_OBJ( 'CRM_PROCES', $sql_obj );
  363. if (V::get('DEBUG', '', $_SESSION)) App::add_msg("PASSED($affected)");
  364. if ($affected == 2) {
  365. App::add_msg("Zmieniono rekord (ID ".$id.")");
  366. }
  367. else if ($affected == 1) {
  368. App::add_msg("Zmieniono rekord (ID ".$id.") (Error: nie zapisano hostorii)");
  369. }
  370. else if ($affected < 0) {
  371. App::add_error("Blad podczas edytowania rekordu - nic nie zmieniono");
  372. }
  373. App::redirect($redirect, $msg);
  374. }
  375. function fun_CRM_PROCES_clean_trash() {
  376. $db = DB::getDB();
  377. if (!$db) {
  378. echo '<p class="red">' . "No DB!" . '</p>';
  379. return;
  380. } else if ($db->has_errors()) {
  381. echo '<p>' . "Error DB: " . implode('<br />', $db->get_errors()) . '</p>';
  382. return;
  383. }
  384. $affected_total = 0;
  385. // remove all root trash items
  386. $sql = "update `CRM_PROCES` as p
  387. set p.`A_STATUS`='DELETED', p.`A_RECORD_UPDATE_AUTHOR`='TrashRemoveRec', p.`A_RECORD_UPDATE_DATE`=NOW()
  388. where p.`PARENT_ID`=-1 and p.`A_STATUS`!='DELETED'
  389. ";
  390. $db->query($sql);
  391. $affected = $db->affected_rows();
  392. if ($affected) {
  393. $affected_total += $affected;
  394. }
  395. // remove all child trash items - run X times
  396. $loop_limit = 50;
  397. do {
  398. $sql = "update `CRM_PROCES` as p, `CRM_PROCES` as pp
  399. set p.`A_STATUS`='DELETED', p.`A_RECORD_UPDATE_AUTHOR`='TrashRemoveRec', p.`A_RECORD_UPDATE_DATE`=NOW()
  400. where p.`PARENT_ID`=pp.`ID` and (pp.`PARENT_ID`=-1 or pp.`A_STATUS`='DELETED') and p.`A_STATUS`!='DELETED'
  401. ";
  402. $db->query($sql);
  403. $affected = $db->affected_rows();
  404. if ($affected) {
  405. $affected_total += $affected;
  406. }
  407. } while ($affected > 0 && --$loop_limit >= 0);
  408. echo '<p>' . "Usunięto " . $affected_total . " rekordów." . '</p>';
  409. }