proces.php 15 KB

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