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