proces.php 15 KB

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