Advertisement
_ChS_

first look on new world

Dec 28th, 2014
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.34 KB | None | 0 0
  1. function _debug_mind_map_nodes( $id ) {
  2.  
  3.     $x = 0;
  4.     $y = 0;
  5.     $z = 0;
  6.     $index = "0x0";
  7.     $connected_to = 0;
  8.     $last_y = 0; $last_x = 0; $last_connected_to = 0;
  9.  
  10.     $indexes = array();
  11.    
  12.     // fetch all nodes from mind map sort by connected_to,x,y
  13.     $query = new EntityFieldQuery();
  14.     $query->entityCondition('entity_type', 'mind_map_node')
  15.     ->entityCondition('bundle', 'mind_map_node')
  16.     ->propertyOrderBy('connected_to', 'ASC')
  17.     ->propertyOrderBy('coordinate_y', 'DESC')
  18.     ->propertyOrderBy('coordinate_x', 'ASC')
  19.     ->propertyCondition('mind_map_id', $id, '=');
  20.     $result= $query->execute();
  21.  
  22.     $nodes_ids = null;
  23.     $parents_mapping = array();
  24.     if(isset($result['mind_map_node'])) {
  25.         $nodes_ids = array_keys(array_pop($result));
  26.         $nodes = entity_load('mind_map_node', $nodes_ids);
  27. //        dsm($nodes);
  28.  
  29.         $key = array_keys($nodes);
  30.         $size = sizeOf($key);
  31.        
  32.         $last_y_node = $nodes[$key[$size-1]];
  33.         $max_y = $last_y_node->coordinate_y;
  34.        
  35. //        dsm($max_y);
  36.         $max_x_limit = 0;
  37.         $node_x_limit_on_y = array();
  38.         for($i=0;$i<=$max_y;$i++){
  39.             // count query
  40.             $query = new EntityFieldQuery();
  41.             $query->entityCondition('entity_type', 'mind_map_node')
  42.             ->entityCondition('bundle', 'mind_map_node')
  43.             ->propertyCondition('coordinate_y', $i, '=')
  44.             ->propertyCondition('mind_map_id', $id, '=');
  45.             $result= $query->count()->execute();
  46.             $node_x_limit_on_y[] = $result;
  47.             if ( $result > $max_x_limit ) {
  48.                 $max_x_limit = $result;
  49.             }
  50.         }
  51. //        dsm($node_x_limit_on_y);
  52. //        $tmp_node_max_limit = 0;        
  53.         $x_total_on_y = 0;
  54.         $iteration_on_same_y = 0;
  55.         $iteration_nodes_count_on_y = array();
  56.         $iteration_count_for_all_y = array();
  57.        
  58.         for ($i=0; $i<$size; $i++) {
  59.            
  60.             $start_x_on_y = 0;
  61.  
  62.             $connected_to = $nodes[$key[$i]]->connected_to;
  63.             $y = $nodes[$key[$i]]->coordinate_y;
  64.             $x = $nodes[$key[$i]]->coordinate_x;
  65. //            if( isset($parents_mapping[$nodes[$key[$i]]->connected_to][]) ) {}
  66. if ( isset( $nodes[$nodes[$key[$i]]->connected_to] ) ) {
  67.     $parents_mapping[$nodes[$key[$i]]->connected_to]['root'] = $nodes[$nodes[$key[$i]]->connected_to]->connected_to;    
  68. } else {
  69.     $parents_mapping[$nodes[$key[$i]]->connected_to]['root'] = 0;
  70. }
  71.  
  72. $parents_mapping[$nodes[$key[$i]]->connected_to][$i] = $key[$i];
  73. //            $indexes['nodes'][$i]['connected_to'] = $nodes[$key[$i]]->connected_to;
  74. //            if($last_y != $y && $x != $last_x && $x != 0) {
  75.             if($last_y != $y) {
  76.                 $iteration_nodes_count_on_y = array();
  77.                 $iteration_on_same_y = 0;
  78. //                dsm($node_x_limit_on_y[$y]);
  79.                 for ($j=0, $last_connected_to_tmp = $nodes[$key[$i+$j]]->connected_to; $j<$node_x_limit_on_y[$y]; $j++) {
  80. //                    dsm($nodes[$key[$i+$j]]->connected_to);
  81.                     $connected_to_tmp = $nodes[$key[$i+$j]]->connected_to;
  82.                     if($last_connected_to_tmp != $connected_to_tmp) {
  83.                         $iteration_nodes_count_on_y[$connected_to_tmp]=0;
  84.                         $iteration_nodes_count_on_y[$connected_to_tmp]+=1;
  85.                         ++$iteration_on_same_y;
  86.                     } else {
  87.                         if( !isset($iteration_nodes_count_on_y[$last_connected_to_tmp]) ) {
  88.                             $iteration_nodes_count_on_y[$last_connected_to_tmp]=0;
  89.                         }
  90.                         $iteration_nodes_count_on_y[$last_connected_to_tmp]+=1;
  91.                     }
  92.                     $last_connected_to_tmp = $connected_to_tmp;
  93.                 }
  94.                 $iteration_count_for_all_y[$y] = $iteration_nodes_count_on_y;
  95.                 $iteration_on_same_y = 0;
  96. //                dsm('next y');
  97. //                dsm($iteration_nodes_count_on_y);
  98.                 $x_total_on_y = 0;
  99.                 if( $node_x_limit_on_y[$y] > $node_x_limit_on_y[$y-1] ) {
  100.                     $node_max_x_limit_on_y = $node_x_limit_on_y[$y];
  101.                 } else {
  102.                     $node_max_x_limit_on_y = $max_x_limit;
  103.                 }
  104.                 if ( $node_max_x_limit_on_y % 2 == 0 ) {
  105.                     $start_x_on_y = -$max_x_limit + $node_max_x_limit_on_y;
  106.                 } else {
  107.                     $start_x_on_y = -$max_x_limit + $node_max_x_limit_on_y +1;
  108.                 }
  109.  
  110.             } else {
  111.  
  112.                 ++$x_total_on_y;
  113.                 $node_max_x_limit_on_y = $node_x_limit_on_y[$y];
  114.             }
  115.  
  116.            
  117. //            dsm($node_max_x_limit_on_y);
  118. //            dsm($max_x_limit);
  119.            
  120.             $last_connected_to = $connected_to;
  121.             $last_y = $y;
  122.             $last_x = $x;
  123.            
  124.             $index = $connected_to."x".$y."x".$x;
  125.             $indexes['nodes'][$i]['id'] = $index;
  126.  
  127.             $indexes['nodes'][$i]['x'] = $start_x_on_y + $x_total_on_y +2;
  128.             $indexes['nodes'][$i]['y'] = $y;
  129.             $indexes['nodes'][$i]['size'] = 8;
  130.             $indexes['nodes'][$i]['label'] =  $nodes[$key[$i]]->title;
  131.             if( $last_x != 0 ) {
  132.                 ++$iteration_on_same_y;
  133.             }
  134.             ++$node_max_x_limit_on_y;
  135.         }
  136. //    dsm($indexes);
  137.    
  138.     // fix cartesian x for y on each 1+n node on the right
  139.     $count_on_0_level = sizeof($parents_mapping[0])-1;
  140.     $keys = array_keys($parents_mapping[0]);
  141.     // drop first entry : root
  142.     $values = array_values($parents_mapping[0]);
  143. //    $max = count($parents_mapping[0])
  144.     //ommit 1st entry : root
  145. //dsm($keys);
  146.     $offset = 0;
  147.     $queue = array();
  148.    
  149.     $width_x_on_y = array();
  150.    
  151.     for ( $i=0; $i < $count_on_0_level; $i++ ) {
  152.         $subqueue = array($parents_mapping[0][$i]);
  153. //        array_shift($subqueue);
  154.         $done = array();
  155.         while($subqueue) {
  156.             $link = array_shift($subqueue);
  157.  
  158.             $done[] = $link;
  159. //            echo "<pre>done";var_dump($done);echo "</pre>";
  160.             if(isset($parents_mapping[$link])) {
  161.                 $subnode = $parents_mapping[$link];
  162. //                echo "<pre>subnode";var_dump($subnode);echo "</pre>";
  163.                 foreach( $subnode as $k=>$v) {
  164.                     if((int)$v != 0 && !in_array((int)$v, $done) && !in_array((int)$v, $subqueue)) {
  165.                         $subqueue[] = (int)$v;
  166.                     }
  167.                 }
  168. //                echo "<pre>subqueue";var_dump($subqueue);echo "</pre>";
  169.                
  170.             }
  171. //            if($i==2){ exit; }
  172.         }
  173.         $queue[] = $done;
  174.         $cnt = count($queue[$i]);
  175.  
  176. //        dsm(" i = {$i} ");
  177. //        $connected_to = $nodes[$key[$i]]->connected_to;
  178.         for($j = 0; $j < $cnt; $j++ ) {
  179.             $x = $nodes[$queue[$i][$j]]->coordinate_x;
  180.             $y = $nodes[$queue[$i][$j]]->coordinate_y;
  181. //            $connected_to = $parents_mapping[$queue[$j]]->coonencted_to_x;
  182.             if( $y != 0 && $y == $nodes[$queue[$i][$j-1]]->coordinate_y ) {
  183.                 $width_x_on_y_for_n_root_child[$i][$y] += 1;
  184. //                dsm('y != 0 and y-1 == y ');
  185.             } else if ( $y != 0 ) {
  186.                 $width_x_on_y_for_n_root_child[$i][$y] = 1;
  187. //                dsm('y != 0 but y-1 != y ');
  188.             } else {
  189.                 $width_x_on_y_for_n_root_child[$i][0] = 1;
  190. //                dsm('y = 0 ');
  191.             }
  192.         }
  193.     }
  194.     $cnt = count($queue);
  195.     for($i=0;$i<$cnt;$i++) {
  196.        
  197.         $cnt_in = count($queue[$i]);
  198.         $keys_in = array_keys($queue[$i]);
  199.         $values_in = array_values($queue[$i]);
  200.  
  201.         $mapped_nodes = array();
  202.         for($j=0;$j<$cnt_in;$j++) {
  203.            
  204.             $needle = null;
  205.             if(isset($parents_mapping[$values_in[$j]])) {
  206. //                $needle = $parents_mapping[$values_in[$j]]['root'];
  207. //                $mapped_nodes[$parents_mapping[$needle][$keys_in[$j]]] = $values_in[$j];
  208.             }        
  209.         }
  210. //        dsm($mapped_nodes);
  211.     }
  212.     dsm($width_x_on_y_for_n_root_child);
  213.     dsm($queue);
  214. /*
  215.     for ( $i=1; $i < $count_on_0_level; $i++ ) {
  216.         dsm($key[$i]);
  217. //dsm($parents_mapping[$parents_mapping[0][$i]]);
  218. //dsm($parents_mapping[$parents_mapping[0][$i]]);
  219.         $max = count($parents_mapping[$parents_mapping[0][$i]]);
  220.         $in_keys = array_keys($parents_mapping[$parents_mapping[0][$i]]);
  221. //        dsm($in_keys);
  222.         for ( $j=0; $j<$max; $j++) {
  223.             if( $j !== 0 ) {
  224. //                dsm($indexes['nodes'][$in_keys[$j]]['x']);
  225.                 $indexes['nodes'][$in_keys[$j]]['x'] += $i;
  226.             } else {
  227. //                dsm('root');
  228.                 $indexes['nodes'][$keys[$i+1]]['x'] = $indexes['nodes'][$in_keys[$j+1]]['x'] + $i;
  229.             }
  230. //            as $idx => $subnode
  231.            
  232.         }
  233. */    
  234.         //drupal_set_message($count_on_0_level);
  235. //        $offset += $iteration_count_for_all_y[$indexes[$i]['y']];
  236. /*
  237.         foreach($count_on_0_level[$keys[$i]] as $k=>$subnode) {
  238.             $indexes[$k]['x'] += $offset;
  239.         }
  240. */
  241.     }
  242.  
  243.     dsm($parents_mapping);
  244. //    dsm($iteration_count_for_all_y);
  245.     dsm($indexes);
  246.     dsm($nodes);
  247.     return $indexes;
  248.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement