superedit-GRAPH_VIEW_PROCES.old.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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) {
  56. if (!array_key_exists($id, $graph->treeItems)) {
  57. if(empty($name)) $name=$id;
  58. $graph->elements->nodes[] = array('data'=>array('id'=>$id, 'name'=>$id));
  59. }
  60. }
  61. function SearchParentsWithExits($id_start,$id_proces_step,&$graph,$string,$loop=0,&$path_arr=array()) {
  62. graph__addNodeID('994', $graph,'994');
  63. graph__addNodeID('3468', $graph,'3468');
  64. return ;
  65. $loop++;
  66. if($loop>100) { return false;
  67. die('error loop');
  68. }
  69. $sql="select `ID`,`IF_TRUE_GOTO`,`TYPE`,`PARENT_ID` from `CRM_PROCES` where PARENT_ID=994 and `A_STATUS` in ('WAITING','NORMAL') " ;
  70. $res=DB::query($sql);
  71. if(DB::num_rows($res) >0) {
  72. while($h=DB::fetch($res)) {
  73. $path_arr[$h->ID]=true;
  74. $string.=" \n".$h->ID;
  75. //
  76. // $graph->elements->edges[] = array('data'=>array('source'=>$id_proces_step, 'target'=>$h->ID, 'type'=>'GOTO'));
  77. // SearchParentsWithExits($h->ID,$h->ID,&$graph,$string,&$loop,$path_arr);
  78. }
  79. }
  80. }
  81. $db = DB::getDB();
  82. $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";
  83. $res=DB::query($sql);
  84. while($h=DB::fetch($res)) {
  85. SearchParentsWithExits($h->ID,$h->ID,$graph,'Proces '.$h->ID,0);
  86. // graph__addNodeID(995,$graph,995);
  87. // graph__addNodeID(3468, $graph,3468);
  88. //$graph->treeItems[$h->ID] = true;
  89. //$graph->elements->nodes[] = array('data'=>array('id'=>$h->ID, 'name'=>$h->ID.substr($h->DESC,0,50) , 'type'=>'procesInit'));
  90. }
  91. echo 'dupaxxx';
  92. foreach($graph->elements->nodes as $var) {
  93. //echo "<br>\n".$var[data][id];
  94. $nodes[$var[data][id]]=$var[data][id];
  95. }
  96. foreach($graph->elements->edges as $var) {
  97. echo "<br>\n".$var[data][source]." ".$var[data][target]." ";
  98. if(isset($nodes[$var[data][source]])) echo "OK1"; else echo "FAIL1";
  99. if(isset($nodes[$var[data][target]])) echo "OK2"; else echo "FAIL2";
  100. }
  101. DEBUG_S(-3,'nodes',$graph->elements->nodes);
  102. DEBUG_S(-3,'edges',$graph->elements->edges);
  103. DEBUG_S(-3,'edges',$graph);
  104. /*
  105. $sql = "select p.`ID`, p.`PARENT_ID`
  106. from `CRM_PROCES` as p
  107. where p.`A_STATUS` in('WAITING','NORMAL')
  108. ";
  109. $res = $db->query($sql);
  110. while ($r = $db->fetch($res)) {
  111. $graph->procesTreeFlat[$r->PARENT_ID] []= $r->ID;
  112. }
  113. $sql = "select p.`IF_TRUE_GOTO` as ID, p.`ID` as PARENT_ID
  114. from `CRM_PROCES` as p
  115. where p.`A_STATUS` in('WAITING','NORMAL')
  116. and p.IF_TRUE_GOTO>0
  117. -- and p.IF_TRUE_GOTO_FLAG='GOTO_AND_RETURN'
  118. ";
  119. $res = $db->query($sql);
  120. while ($r = $db->fetch($res)) {
  121. $graph->procesTreeGoto[$r->PARENT_ID] []= $r->ID;
  122. }
  123. function createGraphRec($id, $tree, &$graph, $lvl = 0) {
  124. $graph->treeItems[$id] = true;
  125. settype($id, 'string');
  126. if ($id == 3122) {
  127. $graph->elements->nodes[] = array('data'=>array('id'=>$id, 'name'=>$id, 'type'=>'procesInit', 'lvl'=>$lvl));
  128. } else {
  129. $graph->elements->nodes[] = array('data'=>array('id'=>$id, 'name'=>$id, 'lvl'=>$lvl));
  130. }
  131. if ($lvl > 1) {
  132. // return;
  133. }
  134. if (!empty($tree[$id])) {
  135. foreach ($tree[$id] as $vChildID) {
  136. $graph->elements->edges[] = array('data'=>array('source'=>$id, 'target'=>$vChildID));
  137. createGraphRec($vChildID, $tree, $graph, $lvl + 1);
  138. }
  139. }
  140. }
  141. createGraphRec($id_proces, $graph->procesTreeFlat, $graph);
  142. */
  143. /*
  144. // addGraphGoto
  145. foreach ($graph->procesTreeGoto as $kID => $vGotoIds) {
  146. //$graph->treeItems[$id] = true;
  147. if (array_key_exists($kID, $graph->treeItems)) {
  148. foreach ($vGotoIds as $vGotoID) {
  149. graph__addNodeID($vGotoID, $graph);
  150. $graph->elements->edges[] = array('data'=>array('source'=>$kID, 'target'=>$vGotoID, 'type'=>'GOTO'));
  151. }
  152. } else {
  153. foreach ($vGotoIds as $vGotoID) {
  154. if (array_key_exists($vGotoID, $graph->treeItems)) {
  155. //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>';
  156. graph__addNodeID($kID, $graph);
  157. //$graph->elements->edges[] = array('data'=>array('source'=>$kID, 'target'=>$vGotoID, 'type'=>'GOTO'));
  158. }
  159. }
  160. }
  161. }
  162. */
  163. ?>
  164. <style type="text/css">
  165. #<?php echo $graph->htmlID; ?>_wrap {
  166. position:relative;
  167. width:1100px;
  168. margin:0 auto;
  169. border:1px solid #ccc;
  170. }
  171. #<?php echo $graph->htmlID; ?> {
  172. height:560px;
  173. border-bottom:1px solid #ccc;
  174. }
  175. #<?php echo $graph->htmlID; ?>_info {
  176. padding:3px;
  177. }
  178. #<?php echo $graph->htmlID; ?>_wrap .actions { position:absolute; top:20px; left:-40px; margin:0; padding:0; list-style:none; }
  179. #<?php echo $graph->htmlID; ?>_wrap .actions button { border-radius:0; background:#fff; border:1px solid #ccc; }
  180. </style>
  181. <script src="./stuff/cytoscape.js/arbor.js"></script>
  182. <script src="./stuff/cytoscape.js/cytoscape.js"></script>
  183. <script>
  184. jQuery(loadInfo = function(nodeID){
  185. $.ajax({
  186. url: 'index.php?MENU_INIT=GRAPH_VIEW_PROCES&id_proces=<?php echo $id_proces; ?>&ajaxTask=getInfo&id=' + nodeID + '&HEADER_NOT_INIT=YES',
  187. type: 'GET',
  188. dataType: 'text',
  189. data: '',
  190. async: true,
  191. success: function (data) {
  192. jQuery('#<?php echo $graph->htmlID; ?>_info').html(data);
  193. },
  194. error: function (err) {
  195. console.log('err');
  196. }
  197. });
  198. });
  199. jQuery(loadCy = function(){
  200. jQuery('#<?php echo $graph->htmlID; ?>').cytoscape({
  201. layout: {
  202. name: 'arbor'
  203. },
  204. style: cytoscape.stylesheet()
  205. .selector('node')
  206. .css({
  207. 'shape': 'rectangle',
  208. 'width': '464px', 'height': '26px',
  209. 'font-size': '10px',
  210. 'content': 'data(name)',
  211. 'text-valign': 'center',
  212. 'color': 'white',
  213. 'text-outline-width': 2,
  214. 'text-outline-color': '#888'
  215. })
  216. .selector('edge')
  217. .css({
  218. 'target-arrow-shape': 'triangle'
  219. })
  220. .selector(':selected')
  221. .css({
  222. 'background-color': 'black',
  223. 'line-color': 'black',
  224. 'target-arrow-color': 'black',
  225. 'source-arrow-color': 'black'
  226. })
  227. .selector('.faded')
  228. .css({
  229. 'opacity': 0.25,
  230. 'text-opacity': 0
  231. })
  232. .selector('edge.neighborhood')
  233. .css({
  234. 'background-color': 'blue',
  235. 'color': 'orange'
  236. })
  237. .selector('node.procesInit')
  238. .css({
  239. 'background-color': 'red',
  240. 'text-outline-color': 'red',
  241. 'font-weight': 'bold'
  242. }),
  243. elements: <?php echo json_encode($graph->elements); ?>,
  244. ready: function(){
  245. var cy = this;
  246. window.cy_<?php echo $graph->htmlID; ?> = this;
  247. cy.elements("node[type = 'procesInit']").addClass('procesInit');
  248. cy.elements("node[lvl > 1]").hide();
  249. cy.elements().unselectify();
  250. cy.on('tap', 'node', function(e){
  251. //console.log('on tap node', e)
  252. var node = e.cyTarget;
  253. var neighborhood = node.neighborhood().add(node);
  254. var nodeID = node.id();
  255. loadInfo(nodeID);
  256. cy.elements().addClass('faded').unselect();
  257. neighborhood.removeClass('faded');
  258. node.select();
  259. cy.edges().removeClass('neighborhood');
  260. node.connectedEdges().addClass('neighborhood');
  261. neighborhood.show();
  262. cy.center(neighborhood);
  263. });
  264. cy.on('tap', function(e){
  265. if (e.cyTarget === cy){
  266. cy.elements().removeClass('faded');
  267. }
  268. });
  269. },
  270. showOverlay: false
  271. });
  272. });
  273. jQuery(document).ready(function(){
  274. loadCy();
  275. var parent = $('#<?php echo $graph->htmlID; ?>')
  276. .parent()
  277. parent.find('.actions .cy-refresh')
  278. .on('mousedown touchstart', function(){
  279. loadCy();
  280. cy.attr('style', '');
  281. });
  282. parent.find('.actions .cy-zoom-plus')
  283. .on('mousedown touchstart', function(){
  284. window.cy_<?php echo $graph->htmlID; ?>.zoom(window.cy_<?php echo $graph->htmlID; ?>.zoom() + 0.1);
  285. });
  286. parent.find('.actions .cy-zoom-minus')
  287. .on('mousedown touchstart', function(){
  288. window.cy_<?php echo $graph->htmlID; ?>.zoom(window.cy_<?php echo $graph->htmlID; ?>.zoom() - 0.1);
  289. });
  290. });
  291. </script>
  292. <div id="<?php echo $graph->htmlID; ?>_wrap">
  293. <div id="<?php echo $graph->htmlID; ?>"></div>
  294. <div id="<?php echo $graph->htmlID; ?>_info"></div>
  295. <ul class="actions">
  296. <li><button class="btn cy-refresh"><i title="Refresh" class="ico-refresh"></i></button></li>
  297. <li><button class="btn cy-zoom-plus"><i title="Zoom +" class="ico-plus"></i></button></li>
  298. <li><button class="btn cy-zoom-minus"><i title="Zoom -" class="ico-minus"></i></button></li>
  299. </ul>
  300. </div>
  301. <?php
  302. }