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. }//end foreach
  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. }//end foreach
  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. }//end foreach
  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. }//end foreach
  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. }//end foreach
  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. $dbgExecTime->log('show...');
  130. if ($tree_procesy_filter->get_arg('filtr_view') == 'NOWY') {
  131. $filter_last->show_filters();
  132. echo'<div class="filters">';
  133. echo'<form action="'."".'" method="get" style="margin:0;padding:0;">';
  134. echo'<input type="hidden" name="task" value="'.App::get_task().'" />';
  135. $tree_procesy_filter->show_filters();
  136. {// save profile - btn
  137. echo'<nobr>';
  138. echo'<input type="submit" name="save_profile" value="'."Save".'" title="'."Save profile".'" />';
  139. echo'</nobr>';
  140. }
  141. {// load profile - btn
  142. echo'<nobr>';
  143. echo'<input type="submit" name="load_profile" value="'."Load".'" title="'."Load profile".'" />';
  144. echo'</nobr>';
  145. }
  146. echo'</form>';
  147. echo'</div>';// .filters
  148. $filter_hist = (isset($_SESSION['TREE-PROCESY']['filter_hist']))? $_SESSION['TREE-PROCESY']['filter_hist'] : array();
  149. if (!empty($filter_hist)) {
  150. echo'<form style="display:inline" action="" method="GET">';
  151. echo'<input type="hidden" name="task" value="'.App::get_task().'">';
  152. echo'<select name="filter_hist_id">';
  153. foreach ($filter_hist as $k => $v) {
  154. echo'<option value="'.$k.'">'.$v.'</option>';
  155. }
  156. echo'</select>';
  157. echo'</form>';
  158. }
  159. $tree = new Tree( $tbl );
  160. $tree->setProfiler($dbgExecTime);
  161. $clbk = 'tree_callback__show_item_from_'.$tbl;
  162. $tree->set_param('show_item_callback', $clbk);
  163. $tree->set_param('rozwin', ($tree_procesy_filter->get_arg('filtr_drzewo') == 'ROZWIN'));
  164. $tree->set_param('rozwin_proces', ($tree_procesy_filter->get_arg('filtr_procesy') == '+'));
  165. $tree->set_param('rozwin_zasoby', ($tree_procesy_filter->get_arg('filtr_zasoby') == '+'));
  166. $tree->set_param('rozwin_opis', ($tree_procesy_filter->get_arg('filtr_opis') == '+'));
  167. $tree->set_param('filtr_img', ($tree_procesy_filter->get_arg('filtr_img') == '+'));
  168. $tree->set_param('style', $tree_procesy_filter->get_arg('filtr_view'));
  169. $tree->set_param('search_id', $tree_procesy_filter->get_arg('filtr_search_id'));
  170. $tree->set_param('ProcesTblId', ProcesHelper::getZasobTableID('CRM_PROCES'));
  171. $tree->set_param('ZasobTblId', ProcesHelper::getZasobTableID('CRM_LISTA_ZASOBOW'));
  172. $tblPytaniaId = ProcesHelper::getZasobTableID('CRM_TESTY_PYTANIA');
  173. if ($tblPytaniaId > 0) {
  174. $userAcl = User::getAcl();
  175. $userAcl->fetchGroups();
  176. if ($userAcl->hasTableAcl($tblPytaniaId)) {
  177. $tblAcl = $userAcl->getTableAcl($tblPytaniaId);
  178. $tblAcl->init($forceTblAclInit = false);
  179. $tree->set_param('HasAclPytaniaTbl', true);
  180. }
  181. $tree->set_param('PytaniaTblId', $tblPytaniaId);
  182. }
  183. //TODO: check perm to edit
  184. if ($tree_procesy_filter->is_trash()) $tree->set_param('is_trash', true);
  185. $tree->set_param('editable', ($tree_procesy_filter->get_arg('filtr_edit') == 'TAK'));
  186. if ($tree_procesy_filter->get_arg('filtr_ajax') == 'TAK') {
  187. $tree->_limit = 300;
  188. $tree->_deep_limit = 1;
  189. }
  190. if ($tree_procesy_filter->get_arg('filtr_id') != 0) {
  191. $tree->showSubTree( $tree_procesy_filter->get_arg('filtr_id') );
  192. $dbgExecTime->log('done tree->showSubTree()');
  193. } else if ($tree_procesy_filter->get_arg('filtr_search_id') > 0) {
  194. $tree->showSearchNode( $tree_procesy_filter->get_arg('filtr_search_id') );
  195. $dbgExecTime->log('done tree->showSearchNode()');
  196. } else {
  197. $tree->show();
  198. $dbgExecTime->log('done tree->show()');
  199. }
  200. }
  201. else {
  202. echo'<pre>';
  203. {
  204. echo'<table cellspacing="0" cellpadding="1">';
  205. echo'<tr>';
  206. echo'<td>';
  207. echo "DRZEWO ";
  208. if ($_SESSION['ZWIN_DRZEWO']) {
  209. echo App::link("ROZWIN", "?ROZWIN_DRZEWO=1#tree");
  210. } else {
  211. echo App::link("ZWIN", "?ZWIN_DRZEWO=1#tree");
  212. }
  213. echo'</td>';
  214. echo'<td>';
  215. echo "&nbsp;/&nbsp;";
  216. echo'</td>';
  217. echo'<td>';
  218. echo "PROCESY ";
  219. if ($_SESSION['ROZWIN']) {
  220. echo App::link("ZWIN", "?TREE_ZWIN=1#tree");
  221. } else {
  222. echo App::link("ROZWIN", "?TREE_ROZWIN=1#tree");
  223. }
  224. echo'</td>';
  225. echo'<td>';
  226. echo "&nbsp;/&nbsp;";
  227. echo'</td>';
  228. echo'<td>';
  229. echo "ZASOBY ";
  230. if ($_SESSION['ROZWIN_ZASOBY']) {
  231. echo App::link("ZWIN", "?TREE_ZASOBY_ZWIN=1#tree");
  232. } else {
  233. echo App::link("ROZWIN", "?TREE_ZASOBY_ROZWIN=1#tree");
  234. }
  235. echo'</td>';
  236. echo'<td>';
  237. echo "&nbsp;/&nbsp;";
  238. echo'</td>';
  239. echo'<td>';
  240. echo "OPISY ";
  241. if ($_SESSION['ROZWIN_OPIS']) {
  242. echo App::link("ZWIN", "?TREE_OPISY_ZWIN=1#tree");
  243. } else {
  244. echo App::link("ROZWIN", "?TREE_OPISY_ROZWIN=1#tree");
  245. }
  246. echo'</td>';
  247. echo'<td>';
  248. echo "&nbsp;/&nbsp;";
  249. echo'</td>';
  250. echo'<td>';
  251. $js = "window.location.href='#TREE'+this.form.id.value;return false;";
  252. echo '<form style="display:inline" action="#" method="GET">';
  253. echo "Proces ID: ";
  254. echo'<input type="text" name="id" value="" size="5" />';
  255. echo'<button onclick="'.$js.'">'."Wyszukaj ID".'</button>';
  256. echo'</form>'."\n";
  257. echo'</td>';
  258. echo'<td>';
  259. echo "&nbsp;/&nbsp;";
  260. echo'</td>';
  261. echo'<td>';
  262. echo'Widok ';
  263. echo App::link("NOWY", "?filtr_view=NOWY");
  264. echo'</td>';
  265. {
  266. echo'<td>';
  267. echo "&nbsp;/&nbsp;";
  268. echo'</td>';
  269. echo'<td>';
  270. echo'<form action="" method="GET" style="display:inline">';
  271. echo'<input type="hidden" name="'."task".'" value="'.App::get_task().'" />';
  272. echo'<nobr>';
  273. echo'<input type="submit" name="load_profile" value="'."Load".'" title="'."Load profile".'" />';
  274. echo'</nobr>';
  275. echo'</form>';
  276. echo'</td>';
  277. }
  278. echo'</tr>';
  279. echo'</table>';// btns
  280. }
  281. tree_znajdz(0, 1, 0);
  282. echo'</pre>';
  283. }
  284. if($DBG_TIME){
  285. $dbgExecTime->log('end');
  286. $dbgExecTime->printDebug();
  287. }
  288. }
  289. function fun_CRM_PROCES_ADD() {
  290. $redirect = "?task=".App::get_task();
  291. $msg = '';
  292. $sql_obj = new stdClass();
  293. $tbl_name = 'CRM_PROCES';
  294. Lib::loadClass('Column');
  295. $tbl_conf = Column::getInstance($tbl_name);
  296. if (!$tbl_conf) {
  297. return;
  298. }
  299. foreach ($tbl_conf->getFieldList() as $field_id => $field_name) {
  300. if ($tbl_conf->allowCreate($field_name)) {
  301. $post_arg_name = 'ADD_' . $field_name;
  302. if (isset($_POST[$post_arg_name])) {
  303. $sql_obj->$field_name = $_POST[$post_arg_name];
  304. }
  305. }
  306. }//end foreach
  307. // nowy na koniec listy
  308. $sql = "select max(t.`SORT_PRIO`) as MAX_SORT_PRIO
  309. from `CRM_PROCES` as t
  310. where
  311. t.`PARENT_ID`='".$sql_obj->PARENT_ID."'
  312. ";
  313. $res = DB::query( $sql );
  314. while ($r = DB::fetch( $res )) {
  315. $sql_obj->SORT_PRIO = $r->MAX_SORT_PRIO + 1;
  316. }
  317. //TODO: post verify
  318. $new_id = DB::ADD_NEW_OBJ('CRM_PROCES', $sql_obj);
  319. if (V::get('DEBUG', '', $_SESSION)) App::add_msg("PASSED(1) NEW RECORD ID($new_id)");
  320. if ($new_id > 0) {
  321. App::add_msg("Dodano nowy rekord (ID ".$new_id.")");
  322. $redirect .= "&EDIT=".$new_id;
  323. }
  324. else {
  325. App::add_error("Error przy dodawaniu rekordu!");
  326. }
  327. App::redirect($redirect, $msg);
  328. }
  329. /**
  330. * @param $_POST['EDIT'] - ID rekordu
  331. */
  332. function fun_CRM_PROCES_EDIT() {
  333. $redirect = "?task=".App::get_task();
  334. $msg = '';
  335. $sql_obj = new stdClass();
  336. $tbl_name = 'CRM_PROCES';
  337. $sql_obj->ID = V::get('EDIT_ID', '', $_POST, 'int');
  338. // TODO: fields can be hidden
  339. Lib::loadClass('Column');
  340. $tbl_conf = Column::getInstance($tbl_name);
  341. if (!$tbl_conf) {
  342. return;
  343. }
  344. foreach ($tbl_conf->getFieldList() as $field_id => $field_name) {
  345. if ($tbl_conf->allowWrite($field_name)) {
  346. $post_arg_name = 'EDIT_' . $field_name;
  347. if (isset($_POST[$post_arg_name])) {
  348. // TODO: convert field types?
  349. $sql_obj->$field_name = $_POST[$post_arg_name];
  350. }
  351. }
  352. }//end foreach
  353. $redirect .= "&EDIT=".$sql_obj->ID;
  354. //TODO: post verify
  355. $id = $sql_obj->ID;
  356. $affected = DB::UPDATE_OBJ( 'CRM_PROCES', $sql_obj );
  357. if (V::get('DEBUG', '', $_SESSION)) App::add_msg("PASSED($affected)");
  358. if ($affected == 2) {
  359. App::add_msg("Zmieniono rekord (ID ".$id.")");
  360. }
  361. else if ($affected == 1) {
  362. App::add_msg("Zmieniono rekord (ID ".$id.") (Error: nie zapisano hostorii)");
  363. }
  364. else if ($affected < 0) {
  365. App::add_error("Blad podczas edytowania rekordu - nic nie zmieniono");
  366. }
  367. App::redirect($redirect, $msg);
  368. }
  369. function fun_CRM_PROCES_clean_trash() {
  370. $db = DB::getDB();
  371. if (!$db) {
  372. echo '<p class="red">' . "No DB!" . '</p>';
  373. return;
  374. } else if ($db->has_errors()) {
  375. echo '<p>' . "Error DB: " . implode('<br />', $db->get_errors()) . '</p>';
  376. return;
  377. }
  378. $affected_total = 0;
  379. // remove all root trash items
  380. $sql = "update `CRM_PROCES` as p
  381. set p.`A_STATUS`='DELETED', p.`A_RECORD_UPDATE_AUTHOR`='TrashRemoveRec', p.`A_RECORD_UPDATE_DATE`=NOW()
  382. where p.`PARENT_ID`=-1 and p.`A_STATUS`!='DELETED'
  383. ";
  384. $db->query($sql);
  385. $affected = $db->affected_rows();
  386. if ($affected) {
  387. $affected_total += $affected;
  388. }
  389. // remove all child trash items - run X times
  390. $loop_limit = 50;
  391. do {
  392. $sql = "update `CRM_PROCES` as p, `CRM_PROCES` as pp
  393. set p.`A_STATUS`='DELETED', p.`A_RECORD_UPDATE_AUTHOR`='TrashRemoveRec', p.`A_RECORD_UPDATE_DATE`=NOW()
  394. where p.`PARENT_ID`=pp.`ID` and (pp.`PARENT_ID`=-1 or pp.`A_STATUS`='DELETED') and p.`A_STATUS`!='DELETED'
  395. ";
  396. $db->query($sql);
  397. $affected = $db->affected_rows();
  398. if ($affected) {
  399. $affected_total += $affected;
  400. }
  401. } while ($affected > 0 && --$loop_limit >= 0);
  402. echo '<p>' . "Usunięto " . $affected_total . " rekordów." . '</p>';
  403. }