superedit-GRAPH_VIEW_PROCES.funkcje-sqix.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. <?php
  2. function GRAPH_VIEW_PROCES() {
  3. $id_proces = V::get('id_proces', '', $_REQUEST, 'int');
  4. if ($id_proces <= 0) {
  5. echo'<p class="err box box-red">'."Wrong ID".'</p>';
  6. return;
  7. }
  8. $db = DB::getDB();
  9. $proces = $db->get_by_id('CRM_PROCES', $id_proces);
  10. if (!$proces) {
  11. echo'<p class="err box box-red">'."Process {$id_proces} not exists".'</p>';
  12. return;
  13. }
  14. $ajaxTask = V::get('ajaxTask', '', $_REQUEST);
  15. if ($ajaxTask == 'getInfo') {
  16. $id = V::get('id', 0, $_REQUEST, 'int');
  17. if ($id > 0) {
  18. $step = $db->get_by_id('CRM_PROCES', $id);
  19. //echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;"> (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($step);echo'</pre>';
  20. ?>
  21. <p><code><?php echo $step->ID; ?></code> <?php echo $step->DESC; ?></p>
  22. <p><?php echo $step->OPIS; ?></p>
  23. <?php
  24. }
  25. exit;
  26. }
  27. $graph = new stdClass();
  28. $graph->htmlID = 'graph_proces_id_' . $id_proces;
  29. $graph->procesTreeFlat = array();
  30. $graph->procesTreeGoto = array();
  31. $graph->treeItems = array();
  32. $graph->elements = new stdClass();
  33. $graph->elements->nodes = array();
  34. $graph->elements->edges = array();
  35. /* elements: {
  36. nodes: [
  37. { data: { id: 'j', name: 'Jerry' } },
  38. { data: { id: 'e', name: 'Elaine' } },
  39. { data: { id: 'k', name: 'Kramer' } },
  40. { data: { id: 'g', name: 'George' } }
  41. ],
  42. edges: [
  43. { data: { source: 'j', target: 'e' } },
  44. { data: { source: 'j', target: 'k' } },
  45. { data: { source: 'j', target: 'g' } },
  46. { data: { source: 'e', target: 'j' } },
  47. { data: { source: 'e', target: 'k' } },
  48. { data: { source: 'k', target: 'j' } },
  49. { data: { source: 'k', target: 'e' } },
  50. { data: { source: 'k', target: 'g' } },
  51. { data: { source: 'g', target: 'j' } }
  52. ]
  53. },
  54. */
  55. function graph__addNodeID($id, &$graph,$name=null,&$path,$parent=null) {
  56. if (!array_key_exists($id, $graph->treeItems)) {
  57. $graph->treeItems[$id]= true;
  58. if(empty($name)) $name=$id;
  59. if(!empty($path)) { $name=$name." ".implode(',', $path);// $path=array();
  60. }
  61. DEBUG_S(-3,'added id,name,path,parent',array($id,$name,$path,$parent),__FILE__,__FUNCTION__,__LINE__);
  62. $graph->elements->nodes[] = array('data'=>array('id'=>$id, 'name'=>$name));
  63. if(!empty($parent)) $graph->elements->edges[] = array('data'=>array('source'=>$id, 'target'=>$parent ));
  64. //echo "<br> w AddNode:";
  65. }
  66. }
  67. function TestIfHasDeep($id_proces_step) {
  68. $sql="select `ID`,`IF_TRUE_GOTO` from `CRM_PROCES` where `PARENT_ID`=$id_proces_step and `A_STATUS` in ('WAITING','NORMAL') ";
  69. $res=DB::query($sql);
  70. while($h=DB::fetch($res)) {
  71. $ret[$h->ID]='PARENT_ID';
  72. }
  73. $sql="select `ID`,`IF_TRUE_GOTO`,`IF_TRUE_GOTO_FLAG` from `CRM_PROCES` where `ID`=$id_proces_step and `IF_TRUE_GOTO`>0 and `A_STATUS` in ('WAITING','NORMAL') ";
  74. $res=DB::query($sql);
  75. while($h=DB::fetch($res)) {
  76. $ret[$h->IF_TRUE_GOTO]=$h->IF_TRUE_GOTO_FLAG;
  77. }
  78. return $ret;
  79. }
  80. function SearchNextIf($id_proces_step,$path,$loop=0,&$graph) { //szukamy pierwszego rozwidlenia w gore
  81. $loop++;
  82. if($loop>1000) die('blad petli');
  83. $path[]=$id_proces_step;
  84. echo "<br> {$id_proces_step} sprawdzam 98";
  85. $next=TestIfHasDeep($id_proces_step);
  86. DEBUG_S(-3,"{$id_proces_step} Next detext",$next,__FILE__,__FUNCTION__,__LINE__);
  87. if(count($next)==1) { //pojedyncze chodzenie - wyplaszczamy
  88. echo "<br>{$id_proces_step} 87 count has ".count($next)." i jade ".key($next);
  89. SearchNextIf(key($next),$path,$loop,$graph);
  90. } else if(count($next)>1) { //wiecej niz jeden wpis- sprawdzamy, czy nie ma pustych
  91. echo "<br>".$id_proces_step." count 90 has ".count($next);
  92. $wyjsc=array();
  93. foreach($next as $nx=>$nx_type) {
  94. echo "<br> {$id_proces_step} 93 foreach next as nx:".$nx." type ".$nx_type;
  95. $next_=TestIfHasDeep($nx);
  96. if(count($next_)>0) $wyjsc[]=$nx ; else echo "<br> {$id_proces_step} ({$nx}) nie posiada wyjscia ";
  97. echo "<br>{$id_proces_step} Wyjsc 96: ".count($wyjsc);
  98. }
  99. echo "<br>{$id_proces_step} Wyjsc 98: ".count($wyjsc);
  100. if(count($wyjsc)==1) {
  101. echo "<br> ".$id_proces_step." ma jedno wyjscie ({$wyjsc[0]}), idziemy nim dalej ";
  102. if($next[$wyjsc[0]]=='PARENT_ID') {
  103. SearchNextIf($wyjsc[0],$path,$loop,$graph);
  104. }
  105. } else if(count($wyjsc)==0) {
  106. echo "<br> {$id_proces_step} 118 dupa cnt".count($wyjsc);
  107. //return $path;
  108. } else {
  109. DEBUG_S(-3,"{$id_proces_step} wyjsc - dodaje punkt procesu",$wyjsc,__FILE__,__FUNCTION__,__LINE__);
  110. foreach($wyjsc as $exit) {
  111. graph__addNodeID($exit,$graph,$exit,$path,$path[0]);
  112. if($next[$exit]=='PARENT_ID')
  113. SearchNextIf($exit,$path,$loop,$graph);
  114. else if($next[$exit]=='GOTO_AND_RETURN') {
  115. echo "goto and return";
  116. graph__addNodeID($exit,$graph,$exit,$path,$path[0]);
  117. $path=array();
  118. }
  119. }
  120. // die('inny przyp 99');
  121. }
  122. } else {
  123. echo "<br> {$id_proces_step} 111 else ";
  124. DEBUG_S(-3,'path',$path,__FILE__,__FUNCTION__,__LINE__);
  125. graph__addNodeID($path[0],$graph,$path[0],$path,null);
  126. // $graph->elements->nodes[] = array('data'=>array('id'=>100, 'name'=>100));
  127. //print_r($graph);
  128. }
  129. }
  130. function SearchParentsWithExits($id_start,$id_proces_step,&$graph,$string,$loop=0,&$path_arr=array()) {
  131. SearchNextIf($id_proces_step,$path,0,$graph);
  132. // print_r($graph);
  133. //print_r($path);
  134. // graph__addNodeID($h->ID, $graph,$h->ID.' '.implode(',',$path));
  135. // $graph->elements->edges[] = array('data'=>array('source'=>$id_proces_step, 'target'=>$h->ID, 'type'=>'GOTO'));
  136. // SearchParentsWithExits($h->ID,$h->ID,&$graph,$string,&$loop,$path_arr);
  137. }
  138. $db = DB::getDB();
  139. $sql= "select p.`ID`,`DESC` from `CRM_PROCES` as p where p.`A_STATUS` in('WAITING','NORMAL') and p.`TYPE`='PROCES_INIT' and ID=994 limit 1";
  140. $res=DB::query($sql);
  141. while($h=DB::fetch($res)) {
  142. SearchParentsWithExits($h->ID,$h->ID,$graph,'Proces '.$h->ID,0);
  143. //graph__addNodeID($h->ID, $graph);
  144. graph__addNodeID('1', $graph,'1');
  145. // graph__addNodeID($h->ID, $graph,$h->ID);
  146. // graph__addNodeID('100', $graph,'100');
  147. //$graph->treeItems[$h->ID] = true;
  148. //$graph->treeItems[101] = true;
  149. // $graph->elements->nodes[] = array('data'=>array('id'=>$h->ID, 'name'=>$h->ID , 'type'=>'procesInit'));
  150. // $graph->elements->nodes[] = array('data'=>array('id'=>101, 'name'=>101 , 'type'=>'procesInit'));
  151. // $graph->elements->edges[] = array('data'=>array('source'=>994, 'target'=>101));
  152. }
  153. foreach($graph->elements->nodes as $var) {
  154. // echo "<br>\n NODE".$var[data][id];
  155. $nodes[$var[data][id]]=$var[data][id];
  156. }
  157. foreach($graph->elements->edges as $var) {
  158. echo "<br>\n".$var[data][source]." ".$var[data][target]." ";
  159. if(isset($nodes[$var[data][source]])) echo "OK1"; else echo "FAIL1";
  160. if(isset($nodes[$var[data][target]])) echo "OK2"; else echo "FAIL2";
  161. }
  162. DEBUG_S(-3,'nodes',$graph->elements->nodes);
  163. DEBUG_S(-3,'edges',$graph->elements->edges);
  164. /*
  165. $sql = "select p.`ID`, p.`PARENT_ID`
  166. from `CRM_PROCES` as p
  167. where p.`A_STATUS` in('WAITING','NORMAL')
  168. ";
  169. $res = $db->query($sql);
  170. while ($r = $db->fetch($res)) {
  171. $graph->procesTreeFlat[$r->PARENT_ID] []= $r->ID;
  172. }
  173. $sql = "select p.`IF_TRUE_GOTO` as ID, p.`ID` as PARENT_ID
  174. from `CRM_PROCES` as p
  175. where p.`A_STATUS` in('WAITING','NORMAL')
  176. and p.IF_TRUE_GOTO>0
  177. -- and p.IF_TRUE_GOTO_FLAG='GOTO_AND_RETURN'
  178. ";
  179. $res = $db->query($sql);
  180. while ($r = $db->fetch($res)) {
  181. $graph->procesTreeGoto[$r->PARENT_ID] []= $r->ID;
  182. }
  183. function createGraphRec($id, $tree, &$graph, $lvl = 0) {
  184. $graph->treeItems[$id] = true;
  185. settype($id, 'string');
  186. if ($id == 3122) {
  187. $graph->elements->nodes[] = array('data'=>array('id'=>$id, 'name'=>$id, 'type'=>'procesInit', 'lvl'=>$lvl));
  188. } else {
  189. $graph->elements->nodes[] = array('data'=>array('id'=>$id, 'name'=>$id, 'lvl'=>$lvl));
  190. }
  191. if ($lvl > 1) {
  192. // return;
  193. }
  194. if (!empty($tree[$id])) {
  195. foreach ($tree[$id] as $vChildID) {
  196. $graph->elements->edges[] = array('data'=>array('source'=>$id, 'target'=>$vChildID));
  197. createGraphRec($vChildID, $tree, $graph, $lvl + 1);
  198. }
  199. }
  200. }
  201. createGraphRec($id_proces, $graph->procesTreeFlat, $graph);
  202. */
  203. /*
  204. // addGraphGoto
  205. foreach ($graph->procesTreeGoto as $kID => $vGotoIds) {
  206. //$graph->treeItems[$id] = true;
  207. if (array_key_exists($kID, $graph->treeItems)) {
  208. foreach ($vGotoIds as $vGotoID) {
  209. graph__addNodeID($vGotoID, $graph);
  210. $graph->elements->edges[] = array('data'=>array('source'=>$kID, 'target'=>$vGotoID, 'type'=>'GOTO'));
  211. }
  212. } else {
  213. foreach ($vGotoIds as $vGotoID) {
  214. if (array_key_exists($vGotoID, $graph->treeItems)) {
  215. //echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;"> (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r("TODO: add node ($kID); add edge from $kID to $vGotoID");echo'</pre>';
  216. graph__addNodeID($kID, $graph);
  217. //$graph->elements->edges[] = array('data'=>array('source'=>$kID, 'target'=>$vGotoID, 'type'=>'GOTO'));
  218. }
  219. }
  220. }
  221. }
  222. */
  223. ?>
  224. <style type="text/css">
  225. #<?php echo $graph->htmlID; ?>_wrap {
  226. position:relative;
  227. width:1100px;
  228. margin:0 auto;
  229. border:1px solid #ccc;
  230. }
  231. #<?php echo $graph->htmlID; ?> {
  232. height:560px;
  233. border-bottom:1px solid #ccc;
  234. }
  235. #<?php echo $graph->htmlID; ?>_info {
  236. padding:3px;
  237. }
  238. #<?php echo $graph->htmlID; ?>_wrap .actions { position:absolute; top:20px; left:-40px; margin:0; padding:0; list-style:none; }
  239. #<?php echo $graph->htmlID; ?>_wrap .actions button { border-radius:0; background:#fff; border:1px solid #ccc; }
  240. </style>
  241. <script src="./stuff/cytoscape.js/arbor.js"></script>
  242. <script src="./stuff/cytoscape.js/cytoscape.js"></script>
  243. <script>
  244. jQuery(loadInfo = function(nodeID){
  245. $.ajax({
  246. url: 'index.php?MENU_INIT=GRAPH_VIEW_PROCES&id_proces=<?php echo $id_proces; ?>&ajaxTask=getInfo&id=' + nodeID + '&HEADER_NOT_INIT=YES',
  247. type: 'GET',
  248. dataType: 'text',
  249. data: '',
  250. async: true,
  251. success: function (data) {
  252. jQuery('#<?php echo $graph->htmlID; ?>_info').html(data);
  253. },
  254. error: function (err) {
  255. console.log('err');
  256. }
  257. });
  258. });
  259. jQuery(loadCy = function(){
  260. jQuery('#<?php echo $graph->htmlID; ?>').cytoscape({
  261. layout: {
  262. name: 'arbor'
  263. },
  264. style: cytoscape.stylesheet()
  265. .selector('node')
  266. .css({
  267. 'shape': 'rectangle',
  268. 'width': '464px', 'height': '26px',
  269. 'font-size': '10px',
  270. 'content': 'data(name)',
  271. 'text-valign': 'center',
  272. 'color': 'white',
  273. 'text-outline-width': 2,
  274. 'text-outline-color': '#888'
  275. })
  276. .selector('edge')
  277. .css({
  278. 'target-arrow-shape': 'triangle'
  279. })
  280. .selector(':selected')
  281. .css({
  282. 'background-color': 'black',
  283. 'line-color': 'black',
  284. 'target-arrow-color': 'black',
  285. 'source-arrow-color': 'black'
  286. })
  287. .selector('.faded')
  288. .css({
  289. 'opacity': 0.25,
  290. 'text-opacity': 0
  291. })
  292. .selector('edge.neighborhood')
  293. .css({
  294. 'background-color': 'blue',
  295. 'color': 'orange'
  296. })
  297. .selector('node.procesInit')
  298. .css({
  299. 'background-color': 'red',
  300. 'text-outline-color': 'red',
  301. 'font-weight': 'bold'
  302. }),
  303. elements: <?php echo json_encode($graph->elements); ?>,
  304. ready: function(){
  305. var cy = this;
  306. window.cy_<?php echo $graph->htmlID; ?> = this;
  307. cy.elements("node[type = 'procesInit']").addClass('procesInit');
  308. cy.elements("node[lvl > 1]").hide();
  309. cy.elements().unselectify();
  310. cy.on('tap', 'node', function(e){
  311. //console.log('on tap node', e)
  312. var node = e.cyTarget;
  313. var neighborhood = node.neighborhood().add(node);
  314. var nodeID = node.id();
  315. loadInfo(nodeID);
  316. cy.elements().addClass('faded').unselect();
  317. neighborhood.removeClass('faded');
  318. node.select();
  319. cy.edges().removeClass('neighborhood');
  320. node.connectedEdges().addClass('neighborhood');
  321. neighborhood.show();
  322. cy.center(neighborhood);
  323. });
  324. cy.on('tap', function(e){
  325. if (e.cyTarget === cy){
  326. cy.elements().removeClass('faded');
  327. }
  328. });
  329. },
  330. showOverlay: false
  331. });
  332. });
  333. jQuery(document).ready(function(){
  334. loadCy();
  335. var parent = $('#<?php echo $graph->htmlID; ?>')
  336. .parent()
  337. parent.find('.actions .cy-refresh')
  338. .on('mousedown touchstart', function(){
  339. loadCy();
  340. cy.attr('style', '');
  341. });
  342. parent.find('.actions .cy-zoom-plus')
  343. .on('mousedown touchstart', function(){
  344. window.cy_<?php echo $graph->htmlID; ?>.zoom(window.cy_<?php echo $graph->htmlID; ?>.zoom() + 0.1);
  345. });
  346. parent.find('.actions .cy-zoom-minus')
  347. .on('mousedown touchstart', function(){
  348. window.cy_<?php echo $graph->htmlID; ?>.zoom(window.cy_<?php echo $graph->htmlID; ?>.zoom() - 0.1);
  349. });
  350. });
  351. </script>
  352. <div id="<?php echo $graph->htmlID; ?>_wrap">
  353. <div id="<?php echo $graph->htmlID; ?>"></div>
  354. <div id="<?php echo $graph->htmlID; ?>_info"></div>
  355. <ul class="actions">
  356. <li><button class="btn cy-refresh"><i title="Refresh" class="ico-refresh"></i></button></li>
  357. <li><button class="btn cy-zoom-plus"><i title="Zoom +" class="ico-plus"></i></button></li>
  358. <li><button class="btn cy-zoom-minus"><i title="Zoom -" class="ico-minus"></i></button></li>
  359. </ul>
  360. </div>
  361. <?php
  362. }