don't display backend links without front connection on "Showing all objects" gv map
[racktables-contribs] / linkmgmt.php
CommitLineData
9276dc97
ME
1<?php
2/*
fdc74cf0 3 * Link Management for RT 0.20.x
9276dc97 4 *
fdc74cf0 5 * Features:
6 * - create links between ports
7 * - create backend links between ports
8 * - visually display links / chains
9 * e.g.
10 * (Object)>[port] -- front --> [port]<(Object) == back == > (Object)>[port] -- front --> [port]<(Object)
11 * - Link object backend ports by name (e.g. handy for patch panels)
12 * - change/create CableID (needs jquery.jeditable.mini.js)
13 * - change/create Port Reservation Comment (needs jquery.jeditable.mini.js)
14 * - multiple backend links for supported port types (e.g. AC-in, DC)
15 * - GraphViz Maps (Objects, Ports and Links) (needs GraphViz_Image 1.3.0)
16 * - object,port or link highligthing (just click on it)
17 * - context menu to link and unlink ports
9276dc97 18 *
fdc74cf0 19 * Usage:
20 * 1. select "Link Management" tab
21 * 2. you should see link chains of all linked ports
22 * 3. to display all ports hit "Show All Ports" in the left upper corner
23 * 4. to link all ports with the same name of two different objects use "Link Object Ports by Name"
24 * a. select the other object you want to backend link to
25 * b. "show back ports" gives you the list of possible backend links
26 * !! Important port names have to be the same on both objects !!
27 * e.g. (Current Object):Portname -?-> Portname:(Selected Object)
28 * c. select all backend link to create (Ctrl + a for all)
29 * d. Enter backend CableID for all selected links
30 * e. "Link back" create the backend links
31 * 5. If you have an backend link within the same Object the link isn't shown until
32 * "Expand Backend Links on same Object" is hit
33 * 6. "Object Map" displays Graphviz Map of current object
34 * 7. To get a Graphviz Map of a single port click the port name on the left
9276dc97
ME
35 *
36 *
fdc74cf0 37 * Requirements:
38 * PHP 5 (http://php.net/)
39 * GraphViz_Image 1.3.0 or newer (http://pear.php.net/package/Image_GraphViz)
40 * GraphViz (http://www.graphviz.org/)
9276dc97
ME
41 *
42 * INSTALL:
43 *
fdc74cf0 44 * 1. create LinkBackend Table in your RackTables database
45 *
46 * Multilink table
9276dc97
ME
47
48CREATE TABLE `LinkBackend` (
49 `porta` int(10) unsigned NOT NULL DEFAULT '0',
50 `portb` int(10) unsigned NOT NULL DEFAULT '0',
51 `cable` char(64) DEFAULT NULL,
52 PRIMARY KEY (`porta`,`portb`),
fdc74cf0 53 KEY `LinkBackend_FK_b` (`portb`),
54 CONSTRAINT `LinkBackend_FK_a` FOREIGN KEY (`porta`) REFERENCES `Port` (`id`) ON DELETE CASCADE,
55 CONSTRAINT `LinkBackend_FK_b` FOREIGN KEY (`portb`) REFERENCES `Port` (`id`) ON DELETE CASCADE
9276dc97
ME
56) ENGINE=InnoDB DEFAULT CHARSET=utf8;
57
fdc74cf0 58 * 2. copy jquery.jeditable.mini.js to js/ directory (http://www.appelsiini.net/download/jquery.jeditable.mini.js)
59 * 3. copy linkmgmt.php to plugins directory
60 *
61 * Ready to go!
62 *
63 *
64 * UPDATE TABLE:
65 *
66 * Update from non-multilink table
67 * ALTER TABLE
68
69ALTER TABLE LinkBackend ADD KEY `LinkBackend_FK_b` (`portb`);
70ALTER TABLE LinkBackend DROP INDEX porta;
71ALTER TABLE LinkBackend DROP INDEX portb;
72
73 *
9276dc97
ME
74 *
75 * TESTED on FreeBSD 9.0, nginx/1.0.11, php 5.3.9
fdc74cf0 76 * GraphViz_Image 1.3.0
77 * and RackTables <= 0.20.3
9276dc97 78 *
fdc74cf0 79 * (c)2012,2013 Maik Ehinger <m.ehinger@ltur.de>
9276dc97
ME
80 */
81
fdc74cf0 82/**
83 * The newest version of this plugin can be found at:
9276dc97 84 *
fdc74cf0 85 * https://github.com/github138/myRT-contribs/tree/develop-0.20.x
9276dc97
ME
86 *
87 */
88
89/*************************
90 * TODO
91 *
92 * - code cleanups
93 * - bug fixing
94 *
fdc74cf0 95 * - fix loopdectect for multiport
96 * MAX_LOOP_COUNT
97 * loop highlight gv map
9276dc97 98 *
fdc74cf0 99 * - fix column alignment with multilinks
100 *
101 * - put selected object/port top left of graph
102 * - multlink count for Graphviz maps empty or full dot
103 *
104 * - csv list
9276dc97
ME
105 *
106 */
107
fdc74cf0 108/* DEBUG */
109//error_reporting(E_ALL);
9276dc97
ME
110
111$tab['object']['linkmgmt'] = 'Link Management';
112$tabhandler['object']['linkmgmt'] = 'linkmgmt_tabhandler';
113//$trigger['object']['linkmgmt'] = 'linkmgmt_tabtrigger';
114
115$ophandler['object']['linkmgmt']['update'] = 'linkmgmt_opupdate';
116$ophandler['object']['linkmgmt']['unlinkPort'] = 'linkmgmt_opunlinkPort';
117$ophandler['object']['linkmgmt']['PortLinkDialog'] = 'linkmgmt_opPortLinkDialog';
118$ophandler['object']['linkmgmt']['Help'] = 'linkmgmt_opHelp';
119
fdc74cf0 120$ophandler['object']['linkmgmt']['map'] = 'linkmgmt_opmap';
121$ajaxhandler['lm_mapinfo'] = 'linkmgmt_ajax_mapinfo';
122
123/* ------------------------------------------------- */
124
125Const MULTILINK = TRUE;
126
127/* -------------------------------------------------- */
128
129$lm_multilink_port_types = array(
130 16, /* AC-in */
131 //1322, /* AC-out */
132 1399, /* DC */
133 );
9276dc97
ME
134
135/* -------------------------------------------------- */
136
137$lm_cache = array(
138 'allowcomment' => TRUE, /* RackCode ${op_set_reserve_comment} */
139 'allowlink' => TRUE, /* RackCode ${op_set_link} */
140 'rackinfo' => array(),
141 );
142
143/* -------------------------------------------------- */
144
145//function linkmgmt_tabtrigger() {
146// return 'std';
147//} /* linkmgmt_tabtrigger */
148
149/* -------------------------------------------------- */
150
151function linkmgmt_opHelp() {
152?>
153 <table cellspacing=10><tr><th>Help</th><tr>
154 <tr><td width=150></td><td width=150 style="font-weight:bold;color:<?php echo portlist::CURRENT_OBJECT_BGCOLOR; ?>">Current Object</td></tr>
155 <tr><td></td><td bgcolor=<?php echo portlist::CURRENT_PORT_BGCOLOR; ?>>[current port]</td></tr>
156 <tr><td>front link</td><td>[port]<(Object)</td><td>back link</td></tr>
157 <tr><td>back link</td><td>(Object)>[port]</td><td>front link</td></tr>
158 <tr><td></td><td><pre>----></pre></td><td>Front link</td></tr>
159 <tr><td></td><td><pre>====></pre></td><td>Backend link</td></tr>
160 <tr><td></td><td>Link Symbol</td><td>Create new link</td></tr>
161 <tr><td></td><td>Cut Symbol</td><td>Delete link</td></tr>
162
163 </table>
164
165<?php
166 exit;
167} /* opHelp */
168
169/* -------------------------------------------------- */
170
fdc74cf0 171function linkmgmt_ajax_mapinfo() {
172
173 require_once 'inc/interface.php'; /* renderCell */
174
175 $object_id = NULL;
176 $port_id = NULL;
177 $remote_id = NULL;
178 $linktype = NULL;
179
180 if(isset($_REQUEST['object_id']))
181 $object_id = $_REQUEST['object_id'];
182
183 if(isset($_REQUEST['port_id']))
184 $port_id = $_REQUEST['port_id'];
185
186 if(isset($_REQUEST['remote_id']))
187 $remote_id = $_REQUEST['remote_id'];
188
189 if(isset($_REQUEST['linktype']))
190 $linktype = $_REQUEST['linktype'];
191
192 $debug = NULL;
193 if(isset($_REQUEST['debug']))
194 $debug['value'] = $_REQUEST['debug'];
195
196 $info = array();
197
198 echo "<table style=\"font-size:12;\"><tr>";
199
200 if($port_id != NULL)
201 {
202 $port = new linkmgmt_RTport($port_id);
203
204 echo "<td>";
205 $port->printtable('both');
206 echo "</td>";
207
208 if($debug)
209 $debug['port'] = &$port;
210
211 if($remote_id != NULL)
212 {
213
214 $remote_port = new linkmgmt_RTport($remote_id);
215
216 echo "<td><table align=\"center\">";
217
218 // TODO cableid
219 echo "<tr><td><pre>".($linktype == 'back' ? ' ===> ' : ' ---> ')."</pre></td></tr>";
220
221 $port->printunlinktr($linktype, $remote_port);
222
223 echo "</table></td>";
224
225
226 echo "<td>";
227 $remote_port->printtable('both');
228 echo "</td>";
229
230 if($debug)
231 $debug['remote_port'] = &$remote_port;
232
233 }
234 else
235 $port->printunlinktr();
236
237
238 }
239 echo "</tr><tr>";
240
241 echo "<td>";
242 $object = linkmgmt_RTport::printobjecttable($object_id);
243 echo "</td>";
244
245 if($debug)
246 $debug['object'] = &$object;
247
248 if($remote_id != NULL)
249 {
250
251 echo "<td></td>"; /* link */
252 echo "<td>";
253 $remote_object = linkmgmt_RTport::printobjecttable($remote_port->port['object_id']);
254 echo "</td>";
255
256 if($debug)
257 $debug['remote_object'] = &$remote_object;
258 }
259
260 echo "</tr></table>";
261
262 if($debug)
263 {
264 echo "<pre>--- Debug ---";
265 var_dump($debug);
266 echo "</pre>";
267 }
268
269 exit;
270}
271
272/* -------------------------------------- */
273
274class linkmgmt_RTport {
275
276 private $port_id = NULL;
277
278 public $port = false;
279
280 function __construct($port_id) {
281
282 $this->port = getPortInfo($port_id);
283
284 if($this->port === false)
285 return;
286
287 /* successfully get port info */
288 $this->port_id = $port_id;
289
290 } /* __construct */
291
292 function isvalid() {
293 return ($port_id !== NULL);
294 }
295
296 function getlinks($type = 'front') {
297 } /* getlinks */
298
299 function printtable($linktype = 'front') {
300
301 if($this->port_id == NULL)
302 return;
303
304 echo "<table>";
305
306 $urlparams = array(
307 'module' => 'redirect',
308 'page' => 'object',
309 'tab' => 'linkmgmt',
310 'op' => 'map',
311 'object_id' => $this->port['object_id'],
312 'port_id' => $this->port_id,
313 'usemap' => 1,
314 );
315
316 echo '<tr><td><a title="don\'t highlight port" href="?'.http_build_query($urlparams).'">-phl</a></td>';
317
318 $urlparams['hl'] = 'p';
319 echo '<td><a title="highlight port" href="?'.http_build_query($urlparams).'">+phl</a></td></tr>';
320
321 $this->_printinforow($this->port,
322 array(
323 'id' => 'Port ID',
324 'name' => 'Port Name',
325 'oif_name' => 'Port Type',
326 'l2address' => 'MAC',
327 'reservation_comment' => 'comment',
328 )
329 ); /* printinforow */
330
331 $this->printlinktr($linktype);
332
333 echo "</table>";
334 } /* printtable */
335
336 function printlinktr($linktype = 'front') {
337 if($this->port_id === NULL)
338 return;
339
340 $urlparams = array(
341 'tab' => 'linkmgmt',
342 'page' => 'object',
343 'op'=>'PortLinkDialog',
344 'port'=>$this->port_id,
345 'object_id'=>$this->port['object_id'],
346 'linktype' => $linktype,
347 );
348
349 echo "<tr><td align=\"center\"><a href='".
350 makeHrefProcess($urlparams).
351 "'>";
352 printImageHREF ('plug', 'Link this port');
353 echo "</a></td></tr>";
354 } /* link */
355
356 function printunlinktr($linktype = 'front', $remote_port = NULL) {
357 if($this->port_id === NULL)
358 return;
359
360 $urlparams = array(
361 'tab' => 'linkmgmt',
362 'op'=>'unlinkPort',
363 'port_id'=>$this->port_id,
364 'object_id'=>$this->port['object_id'],
365 'linktype' => $linktype,
366 );
367
368 $confirmmsg = "unlink port ".$this->port['name'];
369
370 if($remote_port !== NULL)
371 {
372 $urlparams['remote_id'] = $remote_port->port['id'];
373 $confirmmsg .= ' -> '.$remote_port->port['name'];
374 }
375
376 $confirmmsg .= " ($linktype)"; // TODO cableid
377
378 echo "<tr><td align=\"center\"><a href='".makeHrefProcess($urlparams).
379 "' onclick=\"return confirm('$confirmmsg');\">";
380 printImageHREF ('cut', 'Unlink this port');
381 echo "</a></td></tr>";
382
383 } /* unlink */
384
385 /* TODO move to object class */
386 static function printobjecttable($object_id = NULL) {
387
388 if($object_id === NULL)
389 return;
390
391 $object = spotEntity ('object', $object_id);
392
393 if($object === false)
394 return;
395
396 if($object['rack_id'])
397 {
398 $rack = spotEntity('rack', $object['rack_id']);
399
400 $object['row_name'] = $rack['row_name'];
401 $object['rack_name'] = $rack['name'];
402 }
403
404 echo "<table><tr><td>";
405 renderCell($object);
406 echo "</td></tr><tr><td><table>";
407
408 self::_printinforow($object,
409 array(
410 'id' => 'ID',
411 'dname' => 'Name',
412 'label' => 'Label',
413 'rack_name' => 'Rack',
414 'row_name' => 'Row',
415 )
416
417 ); /* printinforow */
418
419 $urlparams = array(
420 'module' => 'redirect',
421 'page' => 'object',
422 'tab' => 'linkmgmt',
423 'op' => 'map',
424 'object_id' => $object_id,
425 'usemap' => 1,
426 );
427
428 echo '<tr><td><a title="don\'t highlight object" href="?'.http_build_query($urlparams).'">-ohl</a></td>';
429
430 $urlparams['hl'] = 'o';
431 echo '<td><a title="highlight object" href="?'.http_build_query($urlparams).'">+ohl</a></td></tr>';
432
433 echo "</table></td></tr></table>";
434
435 return $object;
436
437 } /* printobjecttable */
438
439 static function _printinforow(&$data, $config) {
440
441 foreach($config as $key => $name)
442 {
443 if(isset($data[$key]))
444 {
445 $value = $data[$key];
446 if(!empty($value))
447 echo "<tr><td align=\"right\" nowrap=\"nowrap\" style=\"font-size:10;\">$name:</td><td nowrap=\"nowrap\">$value</td></tr>";
448 }
449 }
450
451 } /* _printinforow */
452} /* class RTport */
453
454/* -------------------------------------------------- */
455
456function linkmgmt_opmap() {
457
458 /* display require errors "white screen of death" */
459 $errorlevel = error_reporting();
460 error_reporting(E_ALL);
461
462 require_once 'Image/GraphViz.php';
463
464 error_reporting($errorlevel);
465
466 $object_id = NULL;
467 $port_id = NULL;
468 $remote_id = NULL;
469 $allports = false;
470 $usemap = false;
471 $command = NULL;
472
473 /* highlight object */
474 $hl = NULL;
475 if(isset($_REQUEST['hl']))
476 {
477 $hl = $_REQUEST['hl'];
478 unset($_REQUEST['hl_object_id']);
479 unset($_REQUEST['hl_port_id']);
480
481 if($hl == 'o')
482 {
483 unset($_GET['port_id']);
484 unset($_GET['remote_id']);
485 }
486
487 }
488
489 if(!$hl && isset($_REQUEST['hl_object_id']))
490 {
491 $hl = 'o';
492 $object_id = $_REQUEST['hl_object_id'];
493 unset($_REQUEST['object_id']);
494 unset($_REQUEST['hl_port_id']);
495 unset($_REQUEST['port_id']);
496 }
497
498 if(isset($_REQUEST['object_id']))
499 $object_id = $_REQUEST['object_id'];
500
501 if(isset($_REQUEST['type']))
502 {
503 $type = $_REQUEST['type'];
504 }
505 else
506 $type = 'gif';
507
508 /* highlight port */
509 if(!$hl && isset($_REQUEST['hl_port_id']))
510 {
511 $hl = 'p';
512 $port_id = $_REQUEST['hl_port_id'];
513 unset($_REQUEST['port_id']);
514 }
515
516 if(isset($_REQUEST['allports']))
517 {
518 $allports = $_REQUEST['allports'];
519 }
520
521 if(isset($_REQUEST['port_id']))
522 {
523 $port_id = $_REQUEST['port_id'];
524 }
525
526 if(isset($_REQUEST['usemap']))
527 $usemap = $_REQUEST['usemap'];
528
529 if($hl == 'p' && $port_id === NULL)
530 {
531 unset($_GET['hl']);
532 unset($_GET['port_id']);
533 unset($_GET['remote_id']);
534 }
535
536 if($hl == 'o')
537 unset($_GET['remote_id']);
538
539 if(isset($_REQUEST['remote_id']))
540 $remote_id = $_REQUEST['remote_id'];
541
542 /* show all objects */
543 if(isset($_REQUEST['all']))
544 {
545 $object_id = NULL;
546 $port_id = NULL;
547 $hl = NULL;
548 unset($_GET['hl']);
549 }
550
551 if(isset($_REQUEST['cmd']))
552 $command = $_REQUEST['cmd'];
553
554 if(isset($_REQUEST['debug']))
555 $debug = $_REQUEST['debug'];
556 else
557 $debug = False;
558
559 if($debug) echo "-- DEBUG --<br>";
560
561 $gvmap = new linkmgmt_gvmap($object_id, $port_id, $allports, $hl, $remote_id);
562
563 if($debug) echo "-- after gvmap --<br>";
564
565 switch($type) {
566 case 'gif':
567 case 'png':
568 case 'bmp':
569 case 'jpeg':
570 case 'tif':
571 case 'wbmp':
572 $ctype = "image/$type";
573 break;
574 case 'jpg':
575 $ctype = "image/jpeg";
576 break;
577 case 'svg':
578 $ctype = 'image/svg+xml';
579 break;
580 case 'pdf':
581 $ctype = 'application/pdf';
582 break;
583 case 'cmapx':
584 $ctype = 'text/plain';
585 break;
586
587 }
588
589 if($usemap)
590 {
591
592 if($debug) echo "-- usemap --<br>";
593
594 /* add context menu to Ports, Objects, Links, ...
595 */
596
597 echo "<script>
598 function initcontextmenu() {
599 var maps = document.getElementsByTagName('map');
600 for(var i=0;i<maps.length;i++) {
601 var areas = maps[i].childNodes;
602
603 for(j=0;j<areas.length;j++) {
604 if(areas[j].nodeType == 1)
605 {
606 // console.log(areas[j].id);
607 // attr = document.createAttribute('onmouseover','ahh');
608 // areas[j].setAttribute(attr);
609 // areas[j].onmouseover = 'menu(this);';
610
611 areas[j].addEventListener('contextmenu',menu,false);
612 // areas[j].oncontextmenu = 'menu(this, event);';
613 // console.log(areas[j].oncontextmenu);
614 }
615 }
616
617 }
618
619 };
620
621 function menu(event) {
622
623 // console.log('Menu');
624
625 if(!event)
626 event = window.event;
627
628 var parent = event.target;
629
630 // console.log('--' + parent);
631
632 var ids = parent.id.split('-');
633
634 if(ids[0] == 'graph1')
635 return false;
636
637 var object_id = ids[0];
638
639 var url ='?module=ajax&ac=lm_mapinfo&object_id=' + object_id;
640
641 // links ='<li><a href=' + object_id + '>Object</a></li>';
642
643 if(ids[1] != '')
644 {
645 var port_id = ids[1];
646 url += '&port_id=' + port_id;
647 // links += '<li><a href=' + port_id + '>Port</a></li>';
648
649 if(ids[2] != '')
650 {
651 var remote_id = ids[2];
652
653 if(ids[3] != '')
654 {
655 var linktype = ids[3];
656 url += '&remote_id=' + remote_id + '&linktype=' + linktype;
657 // links += '<li><a href=' + port_id + '_' + remote_id + '_' + linktype + '>Unlink</a></li>';
658 }
659 }
660
661 }
662
663
664 var xmlHttp = new XMLHttpRequest();
665 xmlHttp.open('GET', url, false);
666 xmlHttp.send(null);
667
668 var infodiv = document.getElementById('info');
669 infodiv.innerHTML = xmlHttp.responseText;
670
671 // linkdiv = document.getElementById('link');
672 // linkdiv.innerHTML = links;
673
674 var menudiv = document.getElementById('menu');
675 menudiv.style.position = 'absolute';
676 menudiv.style.top = (event.clientY + document.body.scrollTop) + 'px';
677 menudiv.style.left = (event.clientX + document.body.scrollLeft) + 'px';
678 menudiv.style.display = '';
679
680 return false;
681 };
682
683 function mousedown(event) {
684 // console.log('mouse down');
685
686 if(!event)
687 event = window.event;
688
689 if(event.button != 2)
690 return true;
691
692 var menudiv = document.getElementById('menu');
693
694 menudiv.style.display = 'none';
695
696 return false;
697 };
698
699 </script>";
700
701 echo "<body oncontextmenu=\"return false\" onmousedown=\"mousedown(event);\" onload=\"initcontextmenu();\">";
702
703 echo "<div id=\"menu\" style=\"display:none; background-color:#ffff90\">
704 <div id=\"info\"></div>
705 <ul id=\"link\" style=\"list-style-type:none\"></ul>
706 </div>";
707
708 echo $gvmap->fetch('cmapx', $command);
709
710 if($debug) echo "-- after map gvmap --<br>";
711
712 echo "<img src=\"data:$ctype;base64,".
713 base64_encode($gvmap->fetch($type, $command)).
714 "\" usemap=#map$object_id />";
715
716 if($debug)
717 {
718 echo "<pre>";
719 echo $gvmap->export();
720 echo "</pre>";
721
722 echo "<pre>".$gvmap->parse()."</pre>";
723 }
724 }
725 else
726 {
727 $gvmap->image($type);
728 }
729
730 exit;
731
732} /* linkmgmt_opmap */
733
734/* ------------------------------------- */
735class linkmgmt_gvmap {
736
737 private $object_id = NULL;
738 private $port_id = NULL;
739 private $remote_id = NULL;
740
741 private $gv = NULL;
742
743 private $ports = array();
744
745 private $allports = false;
746 private $back = NULL;
747
748 private $alpa = 'ff';
749
750 private $errorlevel = NULL;
751
752 function __construct($object_id = NULL, $port_id = NULL, $allports = false, $hl = NULL, $remote_id = NULL) {
753 $this->allports = $allports;
754
755 $hl_object_id = NULL;
756 $hl_port_id = NULL;
757
758 $hllabel = "";
759
760 /* suppress strict standards warnings for Image_GraphViz and PHP 5.4.0
761 * output would corrupt image data
762 */
763 $this->errorlevel = error_reporting();
764 error_reporting($this->errorlevel & !E_STRICT);
765
766 $graphattr = array(
767 'rankdir' => 'RL',
768 // 'ranksep' => '0',
769 'nodesep' => '0',
770 // 'overlay' => false,
771 );
772
773 unset($_GET['module']);
774
775 $_GET['all'] = 1;
776
777 $graphattr['URL'] = makeHrefProcess($_GET);
778
779 unset($_GET['all']);
780
781 $this->gv = new Image_GraphViz(true, $graphattr, "map".$object_id);
782
783 switch($hl)
784 {
785 case 'p':
786 case 'port':
787 $hllabel = " (Port highlight)";
788 $hl_object_id = $object_id;
789 $hl_port_id = $port_id;
790 $port_id = NULL;
791 $this->alpha = '30';
792 break;
793 case 'o':
794 case 'object':
795 $hllabel = " (Object highlight)";
796 $hl_object_id = $object_id;
797 $object_id = NULL;
798 $this->alpha = '30';
799 break;
800
801 }
802
803 $this->object_id = $object_id;
804 $this->port_id = $port_id;
805 $this->remote_id = $remote_id;
806
807 if($object_id === NULL)
808 {
809 unset($_GET['all']);
810 $_GET['hl'] = 'o';
811
812 $this->gv->addAttributes(array(
813 'label' => 'Showing all objects'.$hllabel,
814 'labelloc' => 't',
815 )
816 );
817
818 $objects = listCells('object');
819
820 foreach($objects as $obj)
821 $this->_add($this->gv, $obj['id'], NULL);
822 }
823 else
824 {
825 $object = spotEntity ('object', $object_id);
826
827 $this->gv->addAttributes(array(
828 'label' => "Graph for ${object['dname']}$hllabel",
829 'labelloc' => 't',
830 )
831 );
832
833 $this->_add($this->gv, $object_id, ($this->allports ? NULL : $port_id));
834
835 $children = getEntityRelatives ('children', 'object', $object_id); //'entity_id'
836
837 foreach($children as $child)
838 $this->_add($this->gv, $child['entity_id'], NULL);
839 }
840
841 /* highlight object/port */
842 if($hl !== NULL)
843 {
844
845 $this->alpha = 'ff';
846
847 $this->ports = array();
848 $this->back = NULL;
849
850 $this->object_id = $hl_object_id;
851 $this->port_id = $hl_port_id;
852
853 $hlgv = new Image_GraphViz(true, $graphattr);
854
855 $this->_add($hlgv, $hl_object_id , $hl_port_id);
856
857 /* merge higlight graph */
858 // edgedfrom - from - to - id
859 foreach($hlgv->graph['edgesFrom'] as $from => $nodes) {
860 foreach($nodes as $to => $ports) {
861 // TODO ports id
862
863 if(isset($this->gv->graph['edgesFrom'][$from][$to]))
864 $this->gv->graph['edgesFrom'][$from][$to] = $hlgv->graph['edgesFrom'][$from][$to];
865 else
866 if(isset($this->gv->graph['edgesFrom'][$to][$from]))
867 {
868 unset($this->gv->graph['edgesFrom'][$to][$from]);
869 $this->gv->graph['edgesFrom'][$from][$to] = $hlgv->graph['edgesFrom'][$from][$to];
870 }
871 }
872 }
873 // leads to duplicate edges from->to and to->from
874 //$this->gv->graph['edgesFrom'] = $hlgv->graph['edgesFrom'] + $this->gv->graph['edgesFrom'];
875
876 /* merge nodes */
877 foreach($hlgv->graph['nodes'] as $cluster => $node)
878 {
879 $this->gv->graph['nodes'][$cluster] = $hlgv->graph['nodes'][$cluster] + $this->gv->graph['nodes'][$cluster];
880 }
881
882 $this->gv->graph['clusters'] = $hlgv->graph['clusters'] + $this->gv->graph['clusters'];
883 $this->gv->graph['subgraphs'] = $hlgv->graph['subgraphs'] + $this->gv->graph['subgraphs'];
884
885 }
886
887 // portlist::var_dump_html($this->gv);
888
889 // echo $this->gv->parse();
890 // error_reporting( E_ALL ^ E_NOTICE);
891 } /* __construct */
892
893 function __destruct() {
894 error_reporting($this->errorlevel);
895 }
896
897 // !!!recursiv !!!
898 function _add($gv, $object_id, $port_id = NULL) {
899 global $lm_multilink_port_types;
900
901 /* used only for Graphviz ...
902 * !! numeric ids cause Image_Graphviz problems on nested clusters !!
903 */
904 $cluster_id = "c$object_id";
905
906 if($port_id === NULL)
907 {
908 if(
909 isset($gv->graph['clusters'][$cluster_id]) ||
910 isset($gv->graph['subgraphs'][$cluster_id])
911 )
912 return;
913 }
914 else
915 {
916 if(isset($this->ports[$port_id]))
917 return;
918 }
919
920 $object = spotEntity ('object', $object_id);
921 // $object['attr'] = getAttrValues($object_id);
922
923 $clusterattr = array();
924
925 $this->_getcolor('cluster', 'default', $this->alpha, $clusterattr, 'color');
926 $this->_getcolor('cluster', 'default', $this->alpha, $clusterattr, 'fontcolor');
927
928 if($this->object_id == $object_id)
929 {
930 $clusterattr['rank'] = 'source';
931
932 $this->_getcolor('cluster', 'current', $this->alpha, $clusterattr, 'color');
933 $this->_getcolor('cluster', 'current', $this->alpha, $clusterattr, 'fontcolor');
934 }
935
936 $clustertitle = "${object['dname']}";
937 $clusterattr['tooltip'] = $clustertitle;
938
939 unset($_GET['module']); // makeHrefProcess adds this
940 unset($_GET['port_id']);
941 unset($_GET['remote_id']);
942 $_GET['object_id'] = $object_id;
943 //$_GET['hl'] = 'o';
944
945 $clusterattr['URL'] = makeHrefProcess($_GET);
946
947 //has_problems
948 if($object['has_problems'] != 'no')
949 {
950 $clusterattr['style'] = 'filled';
951 $this->_getcolor('cluster', 'problem', $this->alpha, $clusterattr, 'fillcolor');
952 }
953
954 if(!empty($object['container_name']))
955 $clustertitle .= "<BR/>${object['container_name']}";
956
957 if($object['rack_id'])
958 {
959 $rack = spotEntity('rack', $object['rack_id']);
960
961 if(!empty($rack['row_name']) || !empty($rack['name']))
962 $clustertitle .= "<BR/>${rack['row_name']} / ${rack['name']}";
963 }
964
965 $embedin = $object['container_id'];
966 if(empty($embedin))
967 $embedin = 'default';
968 else
969 {
970 $embedin = "c$embedin"; /* see cluster_id */
971
972 /* add container / cluster if not already exists */
973 $this->_add($gv, $object['container_id'], NULL);
974 }
975
fdc74cf0 976
977 if($this->back != 'front' || $port_id === NULL || $this->allports)
978 $front = $this->_getObjectPortsAndLinks($object_id, 'front', $port_id, $this->allports);
979 else
980 $front = array();
981
982 if($this->back != 'back' || $port_id === NULL || $this->allports)
983 $backend = $this->_getObjectPortsAndLinks($object_id, 'back', $port_id, $this->allports);
984 else
985 $backend = array();
986
987 $ports = array_merge($front,$backend);
988
989 if(empty($ports))
990 {
991 /* needed because of gv_image empty cluster bug (invalid foreach argument) */
992 $gv->addNode('dummy', array(
993 // 'label' =>'No Ports found/connected',
994 'label' =>'',
995 'fontsize' => 0,
996 'size' => 0,
997 'width' => 0,
998 'height' => 0,
999 'shape' => 'point',
1000 'style' => 'invis',
1001 ), $cluster_id);
fca4e38a 1002 return;
fdc74cf0 1003 }
1004
fca4e38a 1005 $clusterattr['id'] = "$object_id----"; /* used for js context menu */
1006
1007 $gv->addCluster($cluster_id, $clustertitle, $clusterattr, $embedin);
1008
fdc74cf0 1009 foreach($ports as $key => $port) {
1010
1011 $this->back = $port['linktype'];
1012
1013 $nodelabel = "${port['name']}";
1014
1015 if($port['iif_id'] != '1' )
1016 $nodelabel .= "<BR/><FONT POINT-SIZE=\"8\">${port['iif_name']}</FONT>";
1017
1018 $nodelabel .= "<BR/><FONT POINT-SIZE=\"8\">${port['oif_name']}</FONT>";
1019
1020 $nodeattr = array(
1021 'label' => $nodelabel,
1022 );
1023
1024 $this->_getcolor('port', 'default',$this->alpha, $nodeattr, 'fontcolor');
1025 $this->_getcolor('oif_id', $port['oif_id'],$this->alpha, $nodeattr, 'color');
1026
1027 if($this->port_id == $port['id']) {
1028 $nodeattr['style'] = 'filled';
1029 $nodeattr['fillcolor'] = $this->_getcolor('port', 'current', $this->alpha);
1030 }
1031
1032 if($this->remote_id == $port['id']) {
1033 $nodeattr['style'] = 'filled';
1034 $nodeattr['fillcolor'] = $this->_getcolor('port', 'remote', $this->alpha);
1035 }
1036
1037 $nodeattr['tooltip'] = "${port['name']}";
1038
1039 unset($_GET['module']);
1040 unset($_GET['remote_id']);
1041 $_GET['object_id'] = $port['object_id'];
1042 $_GET['port_id'] = $port['id'];
1043 $_GET['hl'] = 'p';
1044
1045 $nodeattr['URL'] = makeHrefProcess($_GET);
1046 $nodeattr['id'] = "${port['object_id']}-${port['id']}--"; /* for js context menu */
1047
1048 $gv->addNode($port['id'],
1049 $nodeattr,
1050 "c${port['object_id']}"); /* see cluster_id */
1051
1052 $this->ports[$port['id']] = true;
1053
1054 if(!empty($port['remote_id'])) {
1055
1056 //$this->_add($gv, $port['remote_object_id'], ($port_id === NULL ? NULL : $port['remote_id']));
1057
1058 $this->_add($gv, $port['remote_object_id'], $port['remote_id']);
1059
1060 if(
1061 !isset($gv->graph['edgesFrom'][$port['id']][$port['remote_id']]) &&
1062 !isset($gv->graph['edgesFrom'][$port['remote_id']][$port['id']])
1063 ) {
1064
1065 $linktype = $port['linktype'];
1066
1067 $edgetooltip = $port['object_name'].':'.$port['name'].
1068 ' - '.$port['cableid'].' -> '.
1069 $port['remote_name'].':'.$port['remote_object_name'];
1070
1071 $edgeattr = array(
1072 'fontsize' => 8,
1073 'label' => $port['cableid'],
1074 'tooltip' => $edgetooltip,
1075 'sametail' => $linktype,
1076 'samehead' => $linktype,
1077 );
1078
1079 $this->_getcolor('edge', 'default', $this->alpha, $edgeattr, 'color');
1080 $this->_getcolor('edge', 'default', $this->alpha, $edgeattr, 'fontcolor');
1081
1082 if($linktype == 'back' )
1083 {
1084 $edgeattr['style'] = 'dashed';
1085 $edgeattr['arrowhead'] = 'none';
1086 $edgeattr['arrowtail'] = 'none';
1087
1088 /* multilink ports */
1089 if(in_array($port['oif_id'], $lm_multilink_port_types))
1090 {
1091 $edgeattr['dir'] = 'both';
1092 $edgeattr['arrowtail'] = 'dot';
1093 }
1094
1095 if(in_array($port['remote_oif_id'], $lm_multilink_port_types))
1096 {
1097 $edgeattr['dir'] = 'both';
1098 $edgeattr['arrowhead'] = 'dot';
1099 }
1100 }
1101
1102 if(
1103 ($port['id'] == $this->port_id && $port['remote_id'] == $this->remote_id) ||
1104 ($port['id'] == $this->remote_id && $port['remote_id'] == $this->port_id)
1105 )
1106 {
1107 $this->_getcolor('edge', 'highlight', 'ff', $edgeattr, 'color');
1108 $edgeattr['penwidth'] = 2; /* bold */
1109 }
1110
1111 unset($_GET['module']);
1112 $_GET['object_id'] = $port['object_id'];
1113 $_GET['port_id'] = $port['id'];
1114 $_GET['remote_id'] = $port['remote_id'];
1115
1116 $edgeattr['URL'] = makeHrefProcess($_GET);
1117
1118 $edgeattr['id'] = $port['object_id']."-".$port['id']."-".$port['remote_id']."-".$linktype; /* for js context menu */
1119
1120 $gv->addEdge(array($port['id'] => $port['remote_id']),
1121 $edgeattr,
1122 array(
1123 $port['id'] => $linktype,
1124 $port['remote_id'] => $linktype,
1125 )
1126 );
1127 }
1128 }
1129
1130 }
1131
1132 // portlist::var_dump_html($port);
1133 }
1134
1135 function fetch($type = 'png', $command = NULL) {
1136 $ret = $this->gv->fetch($type, $command);
1137 return $ret;
1138 }
1139
1140 function image($type = 'png', $command = NULL) {
1141 $this->gv->image($type, $command);
1142 }
1143
1144 function parse() {
1145 return $this->gv->parse();
1146 }
1147
1148 /* should be compatible with getObjectPortsAndLinks from RT database.php */
1149 function _getObjectPortsAndLinks($object_id, $linktype = 'front', $port_id = NULL, $allports = false) {
1150
1151 if($linktype == 'front')
1152 $linktable = 'Link';
1153 else
1154 $linktable = 'LinkBackend';
1155
1156 $qparams = array();
1157
1158 $query = "SELECT
1159 '$linktype' as linktype,
1160 Port.*,
1161 Port.type AS oif_id,
1162 PortInnerInterface.iif_name as iif_name,
1163 Dictionary.dict_value as oif_name,
1164 Object.id as object_id, Object.name as object_name,
1165 LinkTable.cable as cableid,
1166 remoteObject.id as remote_object_id, remoteObject.name as remote_object_name,
1167 remotePort.id as remote_id, remotePort.name as remote_name,
1168 remotePort.type AS remote_oif_id,
1169 remotePortInnerInterface.iif_name as remote_iif_name,
1170 remoteDictionary.dict_value as remote_oif_name
1171 FROM Port";
1172
1173 // JOIN
1174 $join = " LEFT JOIN PortInnerInterface on PortInnerInterface.id = Port.iif_id
1175 LEFT JOIN Dictionary on Dictionary.dict_key = Port.type
1176 LEFT JOIN $linktable as LinkTable on Port.id in (LinkTable.porta, LinkTable.portb)
1177 LEFT JOIN Object on Object.id = Port.object_id
1178 LEFT JOIN Port as remotePort on remotePort.id = ((LinkTable.porta ^ LinkTable.portb) ^ Port.id)
1179 LEFT JOIN Object as remoteObject on remoteObject.id = remotePort.object_id
1180 LEFT JOIN PortInnerInterface as remotePortInnerInterface on remotePortInnerInterface.id = remotePort.iif_id
1181 LEFT JOIN Dictionary as remoteDictionary on remoteDictionary.dict_key = remotePort.type
1182 ";
1183
1184 // WHERE
1185 if($port_id === NULL)
1186 {
1187 $where = " WHERE Object.id = ?";
1188 $qparams[] = $object_id;
1189
09749fb9 1190 if(!$allports) {
fdc74cf0 1191 $where .= " AND remotePort.id is not NULL";
09749fb9 1192
1193 if($linktype != 'front') {
1194 $join .= "LEFT JOIN Link as FrontLink on Port.id in (FrontLink.porta ,FrontLink.portb)
1195 LEFT JOIN Link as FrontRemoteLink on remotePort.id in (FrontRemoteLink.porta, FrontRemoteLink.portb)
1196 ";
1197 $where .= " AND FrontLink.porta is not NULL AND FrontRemoteLink.porta is not NULL";
1198 }
1199 }
fdc74cf0 1200 }
1201 else
1202 {
1203 // $where = " WHERE Port.id = ? and remotePort.id is not NULL";
1204 $where = " WHERE Port.id = ?";
1205 $qparams[] = $port_id;
1206 }
1207
1208 // ORDER
1209 $order = " ORDER by oif_name, Port.Name";
1210
1211 $query .= $join.$where.$order;
1212
1213 $result = usePreparedSelectBlade ($query, $qparams);
1214
1215 $row = $result->fetchAll(PDO::FETCH_ASSOC);
1216
1217 $result->closeCursor();
1218
1219 return $row;
1220 }
1221
1222 function _getcolor($type = 'object', $key = 'default', $alpha = 'ff', &$array = NULL , $arraykey = 'color') {
1223
1224 $object = array(
1225 'current' => '#ff0000',
1226 );
1227 $port = array(
1228 'current' => '#ffff90',
1229 'remote' => '#ffffD0',
1230 );
1231
1232 $cluster = array(
1233 'current' => '#ff0000',
1234 'problem' => '#ff3030',
1235 );
1236
1237 $edge = array (
1238 'highlight' => '#ff0000',
1239 );
1240
1241 $oif_id = array(
1242 '16' => '#800000', /* AC-in */
1243 '1322' => '#ff4500', /* AC-out */
1244 '24' => '#000080', /* 1000base-t */
1245 );
1246
1247 $defaultcolor = '#000000'; /* black */
1248 $default = true;
1249
1250 if(isset(${$type}[$key]))
1251 {
1252 $default = false;
1253 $color = ${$type}[$key];
1254 }
1255 else
1256 $color = $defaultcolor;
1257
1258
1259 if($alpha != 'ff' || $default == false)
1260 {
1261 $color .= $alpha;
1262
1263 if($array !== NULL)
1264 $array[$arraykey] = $color;
1265 else
1266 return $color;
1267 }
1268 else
1269 return $defaultcolor;
1270
1271 } /* _getcolor */
1272
1273 function dump() {
1274 var_dump($this->gv);
1275 }
1276
1277 function export() {
1278 var_export($this->gv);
1279 }
1280
1281} /* class gvmap */
1282
1283/* -------------------------------------------------- */
1284
9276dc97 1285function linkmgmt_opupdate() {
fdc74cf0 1286
9276dc97
ME
1287 if(!isset($_POST['id']))
1288 exit;
fdc74cf0 1289
9276dc97
ME
1290 $ids = explode('_',$_POST['id'],3);
1291 $retval = strip_tags($_POST['value']);
1292
1293 if(isset($ids[1])) {
1294 if(permitted(NULL, NULL, 'set_link'))
1295 if(isset($ids[2]) && $ids[2] == 'back')
fdc74cf0 1296 linkmgmt_commitUpdatePortLink($ids[0], $ids[1], $retval, TRUE);
9276dc97 1297 else
fdc74cf0 1298 linkmgmt_commitUpdatePortLink($ids[0], $ids[1], $retval);
9276dc97
ME
1299 else
1300 $retval = "Permission denied!";
1301 } else {
1302 if(permitted(NULL, NULL, 'set_reserve_comment'))
1303 commitUpdatePortComment($ids[0], $retval);
1304 else
1305 $retval = "Permission denied!";
1306 }
fdc74cf0 1307
9276dc97
ME
1308 /* return what jeditable should display after edit */
1309 echo $retval;
1310
fdc74cf0 1311 exit;
9276dc97
ME
1312} /* opupdate */
1313
1314/* -------------------------------------------------- */
1315
1316/* similar to commitUpatePortLink in database.php with backend support */
fdc74cf0 1317function linkmgmt_commitUpdatePortLink($port_id1, $port_id2, $cable = NULL, $backend = FALSE) {
9276dc97
ME
1318
1319 /* TODO check permissions */
1320
1321 if($backend)
1322 $table = 'LinkBackend';
1323 else
1324 $table = 'Link';
1325
fdc74cf0 1326 return usePreparedExecuteBlade
9276dc97 1327 (
fdc74cf0 1328 "UPDATE $table SET cable=\"".(mb_strlen ($cable) ? $cable : NULL).
1329 "\" WHERE ( porta = ? and portb = ?) or (portb = ? and porta = ?)",
1330 array (
1331 $port_id1, $port_id2,
1332 $port_id1, $port_id2)
1333 );
1334
9276dc97
ME
1335} /* linkmgmt_commitUpdatePortLink */
1336
1337/* -------------------------------------------------- */
1338
1339function linkmgmt_opunlinkPort() {
1340 $port_id = $_REQUEST['port_id'];
1341 $linktype = $_REQUEST['linktype'];
1342
fdc74cf0 1343 portlist::var_dump_html($_REQUEST);
1344
9276dc97
ME
1345 /* check permissions */
1346 if(!permitted(NULL, NULL, 'set_link')) {
1347 exit;
1348 }
1349
1350 if($linktype == 'back')
fdc74cf0 1351 {
9276dc97 1352 $table = 'LinkBackend';
fdc74cf0 1353 $remote_id = $_REQUEST['remote_id'];
1354
1355 $retval = usePreparedExecuteBlade
1356 (
1357 "DELETE FROM $table WHERE ( porta = ? and portb = ?) or (portb = ? and porta = ?)",
1358 array (
1359 $port_id, $remote_id,
1360 $port_id, $remote_id)
1361 );
1362 }
9276dc97 1363 else
fdc74cf0 1364 {
9276dc97
ME
1365 $table = 'Link';
1366
fdc74cf0 1367 $retval = usePreparedDeleteBlade ($table, array('porta' => $port_id, 'portb' => $port_id), 'OR');
1368 }
1369
9276dc97
ME
1370 if($retval == 0)
1371 echo " Link not found";
1372 else
1373 echo " $retval Links deleted";
1374
fdc74cf0 1375
1376 unset($_GET['module']);
1377 unset($_GET['op']);
1378
1379 header('Location: ?'.http_build_query($_GET));
1380 //header('Location: ?page='.$_REQUEST['page'].'&tab='.$_REQUEST['tab'].'&object_id='.$_REQUEST['object_id']);
9276dc97
ME
1381 exit;
1382} /* opunlinkPort */
1383
1384/* -------------------------------------------------- */
1385
1386function linkmgmt_oplinkPort() {
1387
1388 $linktype = $_REQUEST['linktype'];
1389 $cable = $_REQUEST['cable'];
1390
1391 /* check permissions */
1392 if(!permitted(NULL, NULL, 'set_link')) {
1393 echo("Permission denied!");
1394 return;
1395 }
1396
1397 if(!isset($_REQUEST['link_list'])) {
1398 //portlist::var_dump_html($_REQUEST);
1399 $porta = $_REQUEST['port'];
9276dc97 1400
fdc74cf0 1401 foreach($_REQUEST['remote_ports'] as $portb)
1402 {
1403 $link_list[] = "${porta}_${portb}";
1404
1405 /* with no MULTILINK process first value only */
1406 if(!MULTILINK)
1407 break;
1408 }
9276dc97
ME
1409 } else
1410 $link_list = $_REQUEST['link_list'];
1411
1412 foreach($link_list as $link){
fdc74cf0 1413
9276dc97
ME
1414 $ids = preg_split('/[^0-9]/',$link);
1415 $porta = $ids[0];;
1416 $portb = $ids[1];
1417
1418 $ret = linkmgmt_linkPorts($porta, $portb, $linktype, $cable);
1419
fdc74cf0 1420 //error_log("$ret - $porta - $portb");
1421 $port_info = getPortInfo ($porta);
1422 $remote_port_info = getPortInfo ($portb);
9276dc97
ME
1423 showSuccess(
1424 sprintf
1425 (
1426 'Port %s %s successfully linked with port %s %s',
1427 formatPortLink ($port_info['id'], $port_info['name'], NULL, NULL),
1428 $linktype,
1429 formatPort ($remote_port_info),
1430 $linktype
1431 )
1432 );
1433 }
1434
9276dc97
ME
1435 addJS (<<<END
1436window.opener.location.reload(true);
1437window.close();
1438END
1439 , TRUE);
1440
1441 return;
1442} /* oplinkPort */
1443
1444/* -------------------------------------------------- */
1445
1446/*
1447 * same as in database.php extendend with linktype
1448 */
1449function linkmgmt_linkPorts ($porta, $portb, $linktype, $cable = NULL)
1450{
1451 if ($porta == $portb)
1452 throw new InvalidArgException ('porta/portb', $porta, "Ports can't be the same");
1453
1454 if($linktype == 'back')
fdc74cf0 1455 {
9276dc97 1456 $table = 'LinkBackend';
fdc74cf0 1457 $multilink = MULTILINK;
1458 }
9276dc97 1459 else
fdc74cf0 1460 {
9276dc97 1461 $table = 'Link';
fdc74cf0 1462 $multilink = false;
1463 }
9276dc97
ME
1464
1465 global $dbxlink;
1466 $dbxlink->exec ('LOCK TABLES '.$table.' WRITE');
fdc74cf0 1467
1468 if(!$multilink)
1469 {
1470 $result = usePreparedSelectBlade
1471 (
1472 'SELECT COUNT(*) FROM '.$table.' WHERE porta IN (?,?) OR portb IN (?,?)',
1473 array ($porta, $portb, $porta, $portb)
1474 );
1475
1476 if ($result->fetchColumn () != 0)
1477 {
1478 $dbxlink->exec ('UNLOCK TABLES');
1479 return "$linktype Port ${porta} or ${portb} is already linked";
1480 }
1481 $result->closeCursor ();
1482 }
1483
9276dc97
ME
1484 if ($porta > $portb)
1485 {
1486 $tmp = $porta;
1487 $porta = $portb;
1488 $portb = $tmp;
1489 }
1490 $ret = FALSE !== usePreparedInsertBlade
1491 (
1492 $table,
1493 array
1494 (
1495 'porta' => $porta,
1496 'portb' => $portb,
1497 'cable' => mb_strlen ($cable) ? $cable : ''
1498 )
1499 );
1500 $dbxlink->exec ('UNLOCK TABLES');
1501 $ret = $ret and FALSE !== usePreparedExecuteBlade
1502 (
1503 'UPDATE Port SET reservation_comment=NULL WHERE id IN(?, ?)',
1504 array ($porta, $portb)
1505 );
1506 return $ret ? '' : 'query failed';
1507}
1508
1509/* -------------------------------------------------- */
1510
1511/*
1512 * similar to renderPopupHTML in popup.php
1513 */
1514function linkmgmt_opPortLinkDialog() {
1515// portlist::var_dump_html($_REQUEST);
1516header ('Content-Type: text/html; charset=UTF-8');
1517?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
1518<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" style="height: 100%;">
1519<?php
1520
1521 $text = '<div style="background-color: #f0f0f0; border: 1px solid #3c78b5; padding: 10px; text-align: center;
1522 margin: 5px;">';
1523
fdc74cf0 1524 if(permitted(NULL,NULL,"set_link"))
1525 if (isset ($_REQUEST['do_link'])) {
1526 $text .= getOutputOf ('linkmgmt_oplinkPort');
1527 }
1528 else
1529 if(isset($_REQUEST['byname']))
1530 $text .= getOutputOf ('linkmgmt_renderPopupPortSelectorbyName');
1531 else
1532 $text .= getOutputOf ('linkmgmt_renderPopupPortSelector');
1533 else
1534 $text .= "Permission denied!";
1535
1536 $text .= '</div>';
1537
1538 echo '<head><title>RackTables pop-up</title>';
1539 printPageHeaders();
1540 echo '</head>';
1541 echo '<body style="height: 100%;">' . $text . '</body>';
1542?>
1543</html>
1544<?php
1545 exit;
1546} /* opPortLinkDialog */
1547
1548/* -------------------------------------------------- */
1549
1550/*
1551 * like findSparePorts in popup.php extended with linktype
1552 *
1553 * multilink
1554 *
1555 */
1556function linkmgmt_findSparePorts($port_info, $filter, $linktype, $multilink = false, $objectsonly = false, $byname = false, $portcompat = true, $src_object_id = NULL) {
1557
1558
1559 /*
1560 $linktable ports that will be returned if not linked in this table
1561 $linkinfotable display link for info only show backend links if you want front link a port
1562
1563 front: select ports no front connection and port compat, filter, ...
1564
1565 back:
1566
1567 */
1568
1569 if($linktype == 'back')
1570 {
1571 $linktable = 'LinkBackend';
1572 $linkinfotable = 'Link';
1573 }
1574 else
1575 {
1576 $linktable = 'Link';
1577 $linkinfotable = 'LinkBackend';
1578 }
1579
1580 $qparams = array();
1581 $whereparams = array();
1582
1583 // all ports with no link
1584 /* port:object -> linked port:object */
1585 $query = 'SELECT';
1586 $join = "";
1587 $where = " WHERE";
1588 $group = "";
1589 $order = " ORDER BY";
1590
1591 if($objectsonly)
1592 {
1593 $query .= " remotePort.object_id, CONCAT(IFNULL(remoteObject.name, CONCAT('[',remoteObjectDictionary.dict_value,']')), ' (', count(remotePort.id), ')') as name";
1594 $group .= " GROUP by remoteObject.id";
1595 }
1596 else
1597 if($byname)
1598 {
1599 if($linktype == 'back')
1600 $arrow = '=?=>';
1601 else
1602 $arrow = '-?->';
1603
1604 $query .= ' CONCAT(localPort.id, "_", remotePort.id),
1605 CONCAT(IFNULL(localObject.name, CONCAT("[",localObjectDictionary.dict_value,"]")), " : ", localPort.Name, " '.$arrow.'", remotePort.name, " : ", IFNULL(remoteObject.name,CONCAT("[",remoteObjectDictionary.dict_value,"]")))';
9276dc97 1606 }
fdc74cf0 1607 else
1608 {
1609
1610 if($linktype == 'front')
1611 $arrow = '==';
9276dc97 1612 else
fdc74cf0 1613 $arrow = '--';
9276dc97 1614
fdc74cf0 1615 $query .= " remotePort.id, CONCAT(IFNULL(remoteObject.name, CONCAT('[',remoteObjectDictionary.dict_value,']')), ' : ', remotePort.name,
1616 IFNULL(CONCAT(' $arrow ', IFNULL(infolnk.cable,''), ' $arrow> ', InfoPort.name, ' : ', IFNULL(InfoObject.name,CONCAT('[',InfoObjectDictionary.dict_value,']'))),'') ) as Text";
1617 }
9276dc97 1618
fdc74cf0 1619 $query .= " FROM Port as remotePort";
1620 $join .= " LEFT JOIN Object as remoteObject on remotePort.object_id = remoteObject.id";
1621 $order .= " remoteObject.name";
9276dc97 1622
fdc74cf0 1623 /* object type name */
1624 $join .= " LEFT JOIN Dictionary as remoteObjectDictionary on (remoteObjectDictionary.chapter_id = 1 AND remoteObject.objtype_id = remoteObjectDictionary.dict_key)";
9276dc97 1625
fdc74cf0 1626 if($byname)
1627 {
1628 /* by name */
1629 $join .= " JOIN Port as localPort on remotePort.name = localPort.name";
1630 $where .= " remotePort.object_id <> ? AND localPort.object_id = ?";
1631 $whereparams[] = $src_object_id;
1632 $whereparams[] = $src_object_id;
9276dc97 1633
fdc74cf0 1634 /* own port not linked */
1635 $join .= " LEFT JOIN $linktable as localLink on localPort.id in (localLink.porta, localLink.portb)";
1636 $join .= " LEFT JOIN Object as localObject on localObject.id = localPort.object_id";
1637 $where .= " AND localLink.porta is NULL";
1638
1639 /* object type name */
1640 $join .= " LEFT JOIN Dictionary as localObjectDictionary on (localObject.objtype_id = localObjectDictionary.dict_key AND localObjectDictionary.chapter_id = 1)";
1641 }
1642 else
1643 {
1644 /* exclude current port */
1645 $where .= " remotePort.id <> ?";
1646 $whereparams[] = $port_info['id'];
1647 $order .= " ,remotePort.name";
1648
1649 /* add info to remoteport */
1650 $join .= " LEFT JOIN $linkinfotable as infolnk on remotePort.id in (infolnk.porta, infolnk.portb)";
1651 $join .= " LEFT JOIN Port as InfoPort on InfoPort.id = ((infolnk.porta ^ infolnk.portb) ^ remotePort.id)";
1652 $join .= " LEFT JOIN Object as InfoObject on InfoObject.id = InfoPort.object_id";
1653
1654 /* object type name */
1655 $join .= " LEFT JOIN Dictionary as InfoObjectDictionary on (InfoObject.objtype_id = InfoObjectDictionary.dict_key AND InfoObjectDictionary.chapter_id = 1)";
1656 }
1657
1658 /* only ports which are not linked already */
1659 $join .= " LEFT JOIN $linktable as lnk on remotePort.id in (lnk.porta, lnk.portb)";
1660 $where .= " AND lnk.porta is NULL";
1661
1662 if($portcompat)
1663 {
1664 /* port compat */
1665 $join .= ' INNER JOIN PortInnerInterface pii ON remotePort.iif_id = pii.id
1666 INNER JOIN Dictionary d ON d.dict_key = remotePort.type';
1667 // porttype filter (non-strict match)
1668 $join .= ' INNER JOIN (
1669 SELECT Port.id FROM Port
1670 INNER JOIN
1671 (
1672 SELECT DISTINCT pic2.iif_id
1673 FROM PortInterfaceCompat pic2
1674 INNER JOIN PortCompat pc ON pc.type2 = pic2.oif_id';
1675
1676 if ($port_info['iif_id'] != 1)
1677 {
1678 $join .= " INNER JOIN PortInterfaceCompat pic ON pic.oif_id = pc.type1 WHERE pic.iif_id = ?";
1679 $qparams[] = $port_info['iif_id'];
1680 }
1681 else
1682 {
1683 $join .= " WHERE pc.type1 = ?";
1684 $qparams[] = $port_info['oif_id'];
1685 }
1686 $join .= " AND pic2.iif_id <> 1
1687 ) AS sub1 USING (iif_id)
1688 UNION
1689 SELECT Port.id
1690 FROM Port
1691 INNER JOIN PortCompat ON type1 = type
1692 WHERE iif_id = 1 and type2 = ?
1693 ) AS sub2 ON sub2.id = remotePort.id";
1694 $qparams[] = $port_info['oif_id'];
1695 }
9276dc97 1696
fdc74cf0 1697
1698 $qparams = array_merge($qparams, $whereparams);
9276dc97
ME
1699
1700 // rack filter
1701 if (! empty ($filter['racks']))
1702 {
fdc74cf0 1703 $where .= ' AND remotePort.object_id IN (SELECT DISTINCT object_id FROM RackSpace WHERE rack_id IN (' .
9276dc97
ME
1704 questionMarks (count ($filter['racks'])) . ')) ';
1705 $qparams = array_merge ($qparams, $filter['racks']);
1706 }
1707
fdc74cf0 1708 // object_id filter
1709 if (! empty ($filter['object_id']))
1710 {
1711 $where .= ' AND remoteObject.id = ?';
1712 $qparams[] = $filter['object_id'];
1713 }
1714 else
9276dc97
ME
1715 // objectname filter
1716 if (! empty ($filter['objects']))
1717 {
fdc74cf0 1718 $where .= ' AND remoteObject.name like ? ';
9276dc97
ME
1719 $qparams[] = '%' . $filter['objects'] . '%';
1720 }
fdc74cf0 1721
9276dc97
ME
1722 // portname filter
1723 if (! empty ($filter['ports']))
1724 {
fdc74cf0 1725 $where .= ' AND remotePort.name LIKE ? ';
9276dc97
ME
1726 $qparams[] = '%' . $filter['ports'] . '%';
1727 }
9276dc97 1728
fdc74cf0 1729 $query .= $join.$where.$group.$order;
9276dc97
ME
1730
1731 $result = usePreparedSelectBlade ($query, $qparams);
1732
1733 $row = $result->fetchAll(PDO::FETCH_GROUP|PDO::FETCH_UNIQUE|PDO::FETCH_COLUMN);
1734
fdc74cf0 1735 $result->closeCursor();
1736
1737 /* [id] => displaystring */
9276dc97 1738 return $row;
fdc74cf0 1739
9276dc97
ME
1740} /* findSparePorts */
1741
1742/* -------------------------------------------------- */
1743
1744/*
fdc74cf0 1745 * like renderPopupPortSelector in popup.php extenden with linktype
9276dc97 1746 */
fdc74cf0 1747function linkmgmt_renderPopupPortSelector()
1748{
1749 require_once 'inc/popup.php'; /* getProximateRacks */
9276dc97 1750
fdc74cf0 1751 global $lm_multilink_port_types;
9276dc97 1752
fdc74cf0 1753 assertUIntArg ('port');
1754 $port_id = $_REQUEST['port'];
9276dc97 1755
fdc74cf0 1756 $showlinktypeswitch = false;
9276dc97 1757
fdc74cf0 1758 if(isset($_GET['linktype']))
1759 $linktype = $_GET['linktype'];
1760 else
1761 $linktype = 'front';
9276dc97 1762
fdc74cf0 1763 if($linktype == 'both')
1764 {
9276dc97 1765
fdc74cf0 1766 /*
1767 * use POST front/back_view to set linktype
1768 * and show linktype switch button
1769 */
9276dc97 1770
fdc74cf0 1771 $showlinktypeswitch = true;
1772
1773 if(isset($_POST['front_view']))
1774 $linktype = 'front';
1775 else
1776 if(isset($_POST['back_view']))
1777 $linktype = 'back';
1778 else
1779 $linktype = 'front';
1780 }
1781
1782// portlist::var_dump_html($_POST);
1783
1784 $portcompat = true;
1785
1786 if($linktype == 'back')
1787 {
1788 if(isset($_POST['portcompat']))
1789 $portcompat = $_POST['portcompat'];
1790 }
9276dc97 1791
9276dc97
ME
1792 $object_id = $_REQUEST['object_id'];
1793 $port_info = getPortInfo ($port_id);
fdc74cf0 1794
1795 $multilink = MULTILINK && $linktype == 'back' && in_array($port_info['oif_id'], $lm_multilink_port_types);
1796
1797 if(isset ($_REQUEST['in_rack']))
1798 $in_rack = $_REQUEST['in_rack'] != 'off';
1799 else
1800 $in_rack = true;
9276dc97
ME
1801
1802// portlist::var_dump_html($port_info);
fdc74cf0 1803// portlist::var_dump_html($_GET);
1804// portlist::var_dump_html($_POST);
9276dc97
ME
1805
1806 // fill port filter structure
1807 $filter = array
1808 (
1809 'racks' => array(),
1810 'objects' => '',
fdc74cf0 1811 'object_id' => '',
9276dc97
ME
1812 'ports' => '',
1813 );
fdc74cf0 1814
1815 $remote_object = NULL;
1816 if(isset($_REQUEST['remote_object']))
1817 {
1818 $remote_object = $_REQUEST['remote_object'];
1819
1820 if($remote_object != 'NULL')
1821 $filter['object_id'] = $remote_object;
1822 }
1823
9276dc97
ME
1824 if (isset ($_REQUEST['filter-obj']))
1825 $filter['objects'] = $_REQUEST['filter-obj'];
1826 if (isset ($_REQUEST['filter-port']))
1827 $filter['ports'] = $_REQUEST['filter-port'];
1828 if ($in_rack)
1829 {
1830 $object = spotEntity ('object', $port_info['object_id']);
1831 if ($object['rack_id'])
1832 $filter['racks'] = getProximateRacks ($object['rack_id'], getConfigVar ('PROXIMITY_RANGE'));
1833 }
fdc74cf0 1834
1835 $objectlist = array('NULL' => '- Show All -');
1836 $objectlist = $objectlist + linkmgmt_findSparePorts($port_info, $filter, $linktype, $multilink, true, false, $portcompat);
1837
1838 $spare_ports = linkmgmt_findSparePorts ($port_info, $filter, $linktype, $multilink, false, false, $portcompat);
1839
1840 $maxsize = getConfigVar('MAXSELSIZE');
1841 $objectcount = count($objectlist);
1842
1843 if($linktype == 'back')
1844 $notlinktype = 'front';
1845 else
1846 $notlinktype = 'back';
9276dc97
ME
1847
1848 // display search form
1849 echo 'Link '.$linktype.' of ' . formatPort ($port_info) . ' to...';
1850 echo '<form method=POST>';
1851 startPortlet ($linktype.' Port list filter');
1852 // echo '<input type=hidden name="module" value="popup">';
1853 // echo '<input type=hidden name="helper" value="portlist">';
1854
1855 echo '<input type=hidden name="port" value="' . $port_id . '">';
fdc74cf0 1856 echo '<table><tr><td valign="top"><table><tr><td>';
1857
1858 echo '<table align="center"><tr>';
1859
1860// echo '<td nowrap="nowrap"><input type="hidden" name="linktype" value="front" /><input type="checkbox" name="linktype" value="back"'.($linktype == 'back' ? ' checked="checked"' : '' ).'>link backend</input></td></tr><tr>';
9276dc97
ME
1861 echo '<td class="tdleft"><label>Object name:<br><input type=text size=8 name="filter-obj" value="' . htmlspecialchars ($filter['objects'], ENT_QUOTES) . '"></label></td>';
1862 echo '<td class="tdleft"><label>Port name:<br><input type=text size=6 name="filter-port" value="' . htmlspecialchars ($filter['ports'], ENT_QUOTES) . '"></label></td>';
fdc74cf0 1863 echo '<td class="tdleft" valign="bottom"><input type="hidden" name="in_rack" value="off" /><label><input type=checkbox value="1" name="in_rack"'.($in_rack ? ' checked="checked"' : '').' onchange="this.form.submit();">Nearest racks</label></td>';
9276dc97 1864 echo '</tr></table>';
fdc74cf0 1865
1866 echo '</td></tr><tr><td>';
1867 echo 'Object name (count ports)<br>';
1868 echo getSelect ($objectlist, array ('name' => 'remote_object',
1869 'size' => ($objectcount <= $maxsize ? $objectcount : $maxsize)),
1870 $remote_object, FALSE);
1871
1872 echo '</td></tr></table></td>';
1873 echo '<td valign="top"><table><tr><td><input type=submit value="update objects / ports"></td></tr>';
1874
1875 if($showlinktypeswitch)
1876 echo '<tr height=150px><td><input type=submit value="Switch to '.$notlinktype.' view" name="'.$notlinktype.'_view"></tr></td>';
1877
1878 if($linktype == 'back')
1879 {
1880 echo '<input type="hidden" name="portcompat" value="0">';
1881 echo '<tr height=150px><td><input type=checkbox onchange="this.form.submit();" name="portcompat"'.( $portcompat ? 'checked="checked" ' : '' ).'value="1">Port Compatibility</input></tr></td>';
1882 echo '<input type="hidden" name="back_view">';
1883 }
1884
1885 echo '</table></td>';
1886
9276dc97 1887 finishPortlet();
fdc74cf0 1888 echo '</td><td>';
9276dc97
ME
1889
1890 // display results
1891 startPortlet ('Compatible spare '.$linktype.' ports');
fdc74cf0 1892 echo "spare $linktype Object:Port -- $notlinktype cableID --> $notlinktype Port:Object<br>";
1893
1894 if($multilink)
1895 echo "Multilink<br>";
9276dc97
ME
1896
1897 if (empty ($spare_ports))
1898 echo '(nothing found)';
1899 else
1900 {
fdc74cf0 1901 $linkcount = count($spare_ports);
1902
1903 $options = array(
1904 'name' => 'remote_ports[]',
1905 'size' => getConfigVar ('MAXSELSIZE'),
1906 'size' => ($linkcount <= $maxsize ? $linkcount : $maxsize),
1907 );
1908
1909 if($multilink)
1910 $options['multiple'] = 'multiple';
1911
1912 echo getSelect ($spare_ports, $options, NULL, FALSE);
1913
9276dc97
ME
1914 echo "<p>$linktype Cable ID: <input type=text id=cable name=cable>";
1915 echo "<p><input type='submit' value='Link $linktype' name='do_link'>";
1916 }
1917 finishPortlet();
fdc74cf0 1918 echo '</td></tr></table>';
9276dc97
ME
1919 echo '</form>';
1920
1921} /* linkmgmt_renderPopUpPortSelector */
1922
1923/* -------------------------------------------------- */
1924
1925/*
1926 * similar to renderPopupPortSelector but let you select the destination object
1927 * and displays possible backend links with ports of the same name
1928 */
1929function linkmgmt_renderPopupPortSelectorbyName()
1930{
1931 $linktype = $_REQUEST['linktype'];
1932 $object_id = $_REQUEST['object_id'];
1933
1934 $object = spotEntity ('object', $object_id);
1935
fdc74cf0 1936 $objectlist = linkmgmt_findSparePorts(NULL, NULL, $linktype, false, true, TRUE, false, $object_id);
1937
1938 $objectname = $object['dname'];
1939
1940 /* remove self from list */
1941 unset($objectlist[$object_id]);
1942
9276dc97
ME
1943 if(isset($_REQUEST['remote_object']))
1944 $remote_object = $_REQUEST['remote_object'];
1945 else
fdc74cf0 1946 {
1947 /* choose first object from list */
1948 $keys = array_keys($objectlist);
1949
1950 if(isset($keys[0]))
1951 $remote_object = $keys[0];
1952 else
1953 $remote_object = NULL;
1954 }
9276dc97
ME
1955
1956 if($remote_object)
fdc74cf0 1957 {
1958 $filter['object_id'] = $remote_object;
1959 $link_list = linkmgmt_findSparePorts(NULL, $filter, $linktype, false, false, TRUE, false, $object_id);
1960 }
1961 else
1962 $link_list = linkmgmt_findSparePorts(NULL, NULL, $linktype, false, false, TRUE, false, $object_id);
9276dc97
ME
1963
1964 // display search form
fdc74cf0 1965 echo 'Link '.$linktype.' of ' . formatPortLink($object_id, $objectname, NULL, NULL) . ' Ports by Name to...';
9276dc97 1966 echo '<form method=POST>';
9276dc97 1967
fdc74cf0 1968 echo '<table align="center"><tr><td>';
1969 startPortlet ('Object list');
9276dc97 1970
fdc74cf0 1971 $maxsize = getConfigVar('MAXSELSIZE');
1972 $objectcount = count($objectlist);
9276dc97 1973
fdc74cf0 1974 echo 'Object name (count ports)<br>';
1975 echo getSelect ($objectlist, array ('name' => 'remote_object',
1976 'size' => ($objectcount <= $maxsize ? $objectcount : $maxsize)),
1977 $remote_object, FALSE);
1978 echo '</td><td><input type=submit value="show '.$linktype.' ports>"></td>';
9276dc97
ME
1979 finishPortlet();
1980
fdc74cf0 1981 echo '<td>';
9276dc97
ME
1982 // display results
1983 startPortlet ('Possible Backend Link List');
1984 echo "Select links to create:<br>";
1985 if (empty ($link_list))
1986 echo '(nothing found)';
1987 else
1988 {
fdc74cf0 1989 $linkcount = count($link_list);
1990
1991 $options = array(
1992 'name' => 'link_list[]',
1993 'size' => ($linkcount <= $maxsize ? $linkcount : $maxsize),
1994 'multiple' => 'multiple',
1995 );
1996
1997 echo getSelect ($link_list,$options, NULL, FALSE);
1998
9276dc97
ME
1999 echo "<p>$linktype Cable ID: <input type=text id=cable name=cable>";
2000 echo "<p><input type='submit' value='Link $linktype' name='do_link'>";
2001 }
2002 finishPortlet();
fdc74cf0 2003 echo '</td></tr></table>';
9276dc97
ME
2004 echo '</form>';
2005
fdc74cf0 2006} /* linkmgmt_renderPopUpPortSelectorByName */
9276dc97 2007
fdc74cf0 2008/* ------------------------------------------------ */
9276dc97
ME
2009
2010function linkmgmt_tabhandler($object_id) {
2011 global $lm_cache;
2012
2013 $target = makeHrefProcess(portlist::urlparams('op','update'));
2014
fdc74cf0 2015 addJS('js/jquery.jeditable.mini.js');
9276dc97 2016
fdc74cf0 2017 /* TODO if (permitted (NULL, 'ports', 'set_reserve_comment')) */
9276dc97
ME
2018 /* TODO Link / unlink permissions */
2019
2020 $lm_cache['allowcomment'] = permitted(NULL, NULL, 'set_reserve_comment'); /* RackCode {$op_set_reserve_comment} */
2021 $lm_cache['allowlink'] = permitted(NULL, NULL, 'set_link'); /* RackCode {$op_set_link} */
fdc74cf0 2022
9276dc97
ME
2023 //portlist::var_dump_html($lm_cache);
2024
2025 /* init jeditable fields/tags */
2026 if($lm_cache['allowcomment'])
2027 addJS('$(document).ready(function() { $(".editcmt").editable("'.$target.'",{placeholder : "add comment"}); });' , TRUE);
2028
2029 if($lm_cache['allowlink'])
2030 addJS('$(document).ready(function() { $(".editcable").editable("'.$target.'",{placeholder : "edit cableID"}); });' , TRUE);
2031
9276dc97
ME
2032 /* linkmgmt for current object */
2033 linkmgmt_renderObjectLinks($object_id);
2034
2035 /* linkmgmt for every child */
2036 //$parents = getEntityRelatives ('parents', 'object', $object_id);
2037 $children = getEntityRelatives ('children', 'object', $object_id); //'entity_id'
2038
2039 //portlist::var_dump_html($children);
2040
2041 foreach($children as $child) {
2042 echo '<h1>Links for Child: '.$child['name'].'</h1>';
2043 linkmgmt_renderObjectLinks($child['entity_id']);
2044 }
2045
9276dc97
ME
2046 return;
2047
2048} /* tabhandler */
2049
2050/* -------------------------------------------------- */
2051function linkmgmt_renderObjectLinks($object_id) {
2052
fdc74cf0 2053 $object = spotEntity ('object', $object_id);
9276dc97
ME
2054 $object['attr'] = getAttrValues($object_id);
2055
2056 /* get ports */
2057 /* calls getObjectPortsAndLinks */
2058 amplifyCell ($object);
2059
2060 //$ports = getObjectPortsAndLinks($object_id);
2061 $ports = $object['ports'];
2062
fdc74cf0 2063 /* reindex array so key starts at 0 */
2064 $ports = array_values($ports);
9276dc97
ME
2065
2066 /* URL param handling */
2067 if(isset($_GET['allports'])) {
2068 $allports = $_GET['allports'];
2069 } else
2070 $allports = FALSE;
2071
2072 if(isset($_GET['allback'])) {
2073 $allback = $_GET['allback'];
2074 } else
2075 $allback = FALSE;
2076
2077 echo '<table><tr>';
2078
2079 if($allports) {
fdc74cf0 2080
9276dc97
ME
2081 echo '<td width=200><a href="'.makeHref(portlist::urlparams('allports','0','0'))
2082 .'">Hide Ports without link</a></td>';
2083 } else
2084 echo '<td width=200><a href="'.makeHref(portlist::urlparams('allports','1','0'))
2085 .'">Show All Ports</a></td>';
2086
2087 echo '<td width=200><span onclick=window.open("'.makeHrefProcess(portlist::urlparamsarray(
fdc74cf0 2088 array('op' => 'PortLinkDialog','linktype' => 'back','byname' => '1'))).'","name","height=700,width=800,scrollbars=yes");><a>Link Object Ports by Name</a></span></td>';
9276dc97
ME
2089
2090 if($allback) {
fdc74cf0 2091
9276dc97
ME
2092 echo '<td width=200><a href="'.makeHref(portlist::urlparams('allback','0','0'))
2093 .'">Collapse Backend Links on same Object</a></td>';
2094 } else
2095 echo '<td width=200><a href="'.makeHref(portlist::urlparams('allback','1','0'))
2096 .'">Expand Backend Links on same Object</a></td>';
2097
fdc74cf0 2098 /* Graphviz map */
2099 echo '<td width=100><span onclick=window.open("'.makeHrefProcess(portlist::urlparamsarray(
2100 array('op' => 'map','usemap' => 1))).'","name","height=800,width=800,scrollbars=yes");><a>Object Map</a></span></td>';
2101
9276dc97
ME
2102 /* Help */
2103 echo '<td width=200><span onclick=window.open("'.makeHrefProcess(portlist::urlparamsarray(
2104 array('op' => 'Help'))).'","name","height=400,width=500");><a>Help</a></span></td>';
2105
2106 if(isset($_REQUEST['hl_port_id']))
2107 $hl_port_id = $_REQUEST['hl_port_id'];
2108 else
fdc74cf0 2109 $hl_port_id = NULL;
9276dc97
ME
2110
2111 echo '</tr></table>';
2112
fdc74cf0 2113
2114 echo '<br><br><table id=renderobjectlinks0>';
9276dc97
ME
2115
2116 /* switch display order depending on backend links */
2117 $first = portlist::hasbackend($object_id);
2118
fdc74cf0 2119 $rowcount = 0;
2120 foreach($ports as $key => $port) {
9276dc97
ME
2121
2122 $plist = new portlist($port, $object_id, $allports, $allback);
2123
fdc74cf0 2124 //echo "<td><img src=\"index.php?module=redirect&page=object&tab=linkmgmt&op=map&object_id=$object_id&port_id=${port['id']}&allports=$allports\" ></td>";
2125
2126 if($plist->printportlistrow($first, $hl_port_id, ($rowcount % 2 ? portlist::ALTERNATE_ROW_BGCOLOR : "#ffffff")) )
2127 $rowcount++;
2128
9276dc97
ME
2129 }
2130
2131 echo "</table>";
2132
2133} /* renderObjectLinks */
2134
9276dc97
ME
2135/* -------------------------------------------------- */
2136
2137/*
2138 * Portlist class
fdc74cf0 2139 * gets all linked ports to spezified port
9276dc97
ME
2140 * and prints this list as table row
2141 *
2142 */
2143class portlist {
2144
2145 public $list = array();
2146
2147 private $object_id;
2148 private $port_id;
2149 private $port;
2150
2151 private $first_id;
2152 private $front_count;
2153
2154 private $last_id;
2155 private $back_count;
2156
2157 private $count = 0;
2158
2159 private $allback = FALSE;
2160
fdc74cf0 2161 private $multilink = MULTILINK;
2162
9276dc97
ME
2163 const B2B_LINK_BGCOLOR = '#d8d8d8';
2164 const CURRENT_PORT_BGCOLOR = '#ffff99';
2165 const CURRENT_OBJECT_BGCOLOR = '#ff0000';
2166 const HL_PORT_BGCOLOR = '#00ff00';
fdc74cf0 2167 const ALTERNATE_ROW_BGCOLOR = '#f0f0f0';
9276dc97
ME
2168
2169 /* Possible LOOP detected after count links print only */
2170 const MAX_LOOP_COUNT = 13;
2171
2172 private $loopcount;
2173
fdc74cf0 2174 private $gv = NULL;
2175
9276dc97 2176 function __construct($port, $object_id, $allports = FALSE, $allback = FALSE) {
9276dc97
ME
2177
2178 $this->object_id = $object_id;
2179
2180 $this->port = $port;
2181
2182 $port_id = $port['id'];
fdc74cf0 2183
9276dc97 2184 $this->port_id = $port_id;
fdc74cf0 2185
9276dc97
ME
2186 $this->first_id = $port_id;
2187 $this->last_id = $port_id;
2188
2189 $this->allback = $allback;
2190
fdc74cf0 2191 $this->_getportlists($port_id);
9276dc97
ME
2192
2193 if(!$allports)
2194 if($this->count == 0 || ( ($this->count == 1) && (!empty($this->list[$port_id]['back'])) ) ) {
2195 $this->list = array();
2196 $this->first_id = NULL;
2197 }
2198
fdc74cf0 2199 // $this->var_dump_html($this->list);
9276dc97
ME
2200
2201 } /* __construct */
2202
2203
2204 /*
fdc74cf0 2205 * get front and back portlist
2206 */
2207 function _getportlists($port_id) {
2208
2209 /* Front Port */
2210 $this->count = 0;
2211 $this->_getportlist($this->_getportdata($port_id),FALSE, TRUE);
2212 $this->front_count = $this->count;
2213
2214 /* Back Port */
2215 $this->count = 0;
2216 $this->_getportlist($this->_getportdata($port_id), TRUE, FALSE);
2217 $this->back_count = $this->count;
2218
2219 $this->count = $this->front_count + $this->back_count;
2220 }
2221
2222 /*
2223 * gets front or back port of src_port
2224 * and adds it to the list
9276dc97
ME
2225 */
2226 /* !!! recursive */
2227 function _getportlist(&$src_port, $back = FALSE, $first = TRUE) {
9276dc97 2228
fdc74cf0 2229 $src_port_id = $src_port['id'];
2230
2231 if($back)
9276dc97
ME
2232 $linktype = 'back';
2233 else
2234 $linktype = 'front';
fdc74cf0 2235
9276dc97
ME
2236 if(!empty($src_port[$linktype])) {
2237
fdc74cf0 2238 /* multilink */
2239 foreach($src_port[$linktype] as &$src_link) {
2240 $dst_port_id = $src_link['id'];
9276dc97 2241
fdc74cf0 2242 if(!$this->_loopdetect($src_port,$dst_port_id,$src_link,$linktype)) {
2243 //error_log("no loop $linktype>".$dst_port_id);
2244 $this->count++;
2245 $this->_getportlist($this->_getportdata($dst_port_id), !$back, $first);
2246 }
9276dc97 2247 }
fdc74cf0 2248
9276dc97
ME
2249 } else {
2250 if($first) {
fdc74cf0 2251 $this->first_id = $src_port_id;
9276dc97
ME
2252 // $this->front_count = $this->count; /* doesn't work on loops */
2253 } else {
fdc74cf0 2254 $this->last_id = $src_port_id;
9276dc97
ME
2255 // $this->back_count = $this->count; /* doesn't work on loops */
2256 }
2257
2258 }
2259
fdc74cf0 2260 } /* _getportlist */
9276dc97
ME
2261
2262 /*
2263 * as name suggested
2264 */
fdc74cf0 2265 function _loopdetect(&$src_port, $dst_port_id, &$src_link, $linktype) {
9276dc97 2266
fdc74cf0 2267 /* TODO multilink*/
9276dc97
ME
2268 if(array_key_exists($dst_port_id, $this->list)) {
2269
fdc74cf0 2270 // $dst_port = $this->list[$dst_port_id];
9276dc97 2271
fdc74cf0 2272 //echo "LOOP :".$src_port['id']."-->".$dst_port_id;
9276dc97 2273
fdc74cf0 2274 /* print loop at least once */
2275 if($dst_port_id == $this->port_id)
2276 {
2277 $src_link['loop'] = $dst_port_id;
2278 return TRUE;
2279 }
9276dc97 2280
9276dc97 2281 }
fdc74cf0 2282
2283 return FALSE;
2284
9276dc97 2285 } /* _loopdetect */
fdc74cf0 2286
9276dc97
ME
2287 /*
2288 * get all data for one port
2289 * name, object, front link, back link
2290 */
2291 function &_getportdata($port_id) {
2292 /* sql bitwise xor: porta ^ portb */
2293 //select cable, ((porta ^ portb) ^ 4556) as port from Link where (4556 in (porta, portb));
2294
2295 //error_log("_getportdata $port_id");
fdc74cf0 2296
9276dc97
ME
2297 /* TODO single sql ? */
2298
fdc74cf0 2299 $result = usePreparedSelectBlade
2300 (
2301 'SELECT Port.id, Port.name, Port.label, Port.type, Port.l2address, Port.object_id, Port.reservation_comment,
2302 Object.name as "obj_name"
9276dc97 2303 from Port
fdc74cf0 2304 join Object on Object.id = Port.object_id
9276dc97
ME
2305 where Port.id = ?',
2306 array($port_id)
fdc74cf0 2307 );
2308 $datarow = $result->fetchAll(PDO::FETCH_ASSOC);
9276dc97 2309
fdc74cf0 2310 $result->closeCursor();
2311 unset($result);
2312
2313 $result = usePreparedSelectBlade
2314 (
2315 'SELECT Port.id, Link.cable, Port.name, Port.label, Port.type, Port.l2address, Port.object_id,
9276dc97
ME
2316 CONCAT(Link.porta,"_",Link.portb) as link_id from Link
2317 join Port
2318 where (? in (Link.porta,Link.portb)) and ((Link.porta ^ Link.portb) ^ ? ) = Port.id',
2319 array($port_id, $port_id)
fdc74cf0 2320 );
2321 $frontrow = $result->fetchAll(PDO::FETCH_ASSOC);
2322
2323 $result->closeCursor();
2324 unset($result);
9276dc97 2325
fdc74cf0 2326 $result = usePreparedSelectBlade
2327 (
2328 'SELECT Port.id, LinkBackend.cable, Port.name, Port.label, Port.type, Port.l2address, Port.object_id,
9276dc97
ME
2329 CONCAT(LinkBackend.porta,"_",LinkBackend.portb,"_back") as link_id from LinkBackend
2330 join Port
2331 where (? in (LinkBackend.porta,LinkBackend.portb)) and ((LinkBackend.porta ^ LinkBackend.portb) ^ ? ) = Port.id',
2332 array($port_id, $port_id)
fdc74cf0 2333 );
2334 $backrow = $result->fetchAll(PDO::FETCH_ASSOC);
2335
2336 $result->closeCursor();
2337 unset($result);
9276dc97
ME
2338
2339 $retval = $datarow[0];
2340
2341 if(!empty($frontrow))
fdc74cf0 2342 $retval['front']= $frontrow;
9276dc97
ME
2343 else
2344 $retval['front'] = array();
2345
2346 if(!empty($backrow))
fdc74cf0 2347 $retval['back'] = $backrow;
9276dc97
ME
2348 else
2349 $retval['back'] = array();
2350
2351 // $this->var_dump_html($retval);
2352
2353 /* return reference */
2354 return ($this->list[$port_id] = &$retval);
2355
2356 } /* _getportdata */
2357
2358 /*
2359 */
fdc74cf0 2360 function printport(&$port, $multilink = false) {
2361
9276dc97 2362 /* set bgcolor for current port */
fdc74cf0 2363 if($port['id'] == $this->port_id) {
9276dc97
ME
2364 $bgcolor = 'bgcolor='.self::CURRENT_PORT_BGCOLOR;
2365 $idtag = ' id='.$port['id'];
2366 } else {
2367 $bgcolor = '';
2368 $idtag = '';
2369 }
2370
2371 $mac = trim(preg_replace('/(..)/','$1:',$port['l2address']),':');
2372
fdc74cf0 2373 $title = "Label: ${port['label']}\nMAC: $mac\nTypeID: ${port['type']}\nPortID: ${port['id']}";
9276dc97
ME
2374
2375 echo '<td'.$idtag.' align=center '.$bgcolor.' title="'.$title.'"><pre>[<a href="'
2376 .makeHref(array('page'=>'object', 'tab' => 'linkmgmt', 'object_id' => $port['object_id'], 'hl_port_id' => $port['id']))
2377 .'#'.$port['id']
fdc74cf0 2378 .'">'.$port['name'].'</a>]</pre>'.($multilink ? $this->_getlinkportsymbol($port['id'], 'back') : '' ).'</td>';
9276dc97
ME
2379
2380 } /* printport */
2381
2382 /*
2383 */
2384 function printcomment(&$port) {
fdc74cf0 2385
9276dc97
ME
2386 if(!empty($port['reservation_comment'])) {
2387 $prefix = '<b>Reserved: </b>';
2388 } else
2389 $prefix = '';
2390
2391 echo '<td>'.$prefix.'<i><a class="editcmt" id='.$port['id'].'>'.$port['reservation_comment'].'</a></i></td>';
2392
2393 } /* printComment */
2394
2395
2396 /*
2397 */
2398 function printobject($object_id, $object_name) {
2399 if($object_id == $this->object_id) {
2400 $color='color: '.self::CURRENT_OBJECT_BGCOLOR;
2401 } else {
2402 $color='';
2403 }
2404
2405 echo '<td><table align=center cellpadding=5 cellspacing=0 border=1><tr><td align=center><a style="font-weight:bold;'
2406 .$color.'" href="'.makeHref(array('page'=>'object', 'tab' => 'linkmgmt', 'object_id' => $object_id))
2407 .'"><pre>'.$object_name.'</pre></a><pre>'.$this->_getRackInfo($object_id, 'font-size:80%')
2408 .'</pre></td></tr></table></td>';
2409
2410 } /* printobject */
2411
2412 /*
2413 */
fdc74cf0 2414 function printlink($src_port_id, &$dst_link, $linktype) {
9276dc97
ME
2415
2416 if($linktype == 'back')
2417 $arrow = '====>';
2418 else
2419 $arrow = '---->';
2420
2421 /* link */
2422 echo '<td align=center>';
2423
fdc74cf0 2424 echo '<pre><a class="editcable" id='.$dst_link['link_id'].'>'.$dst_link['cable']
9276dc97 2425 ."</a></pre><pre>$arrow</pre>"
fdc74cf0 2426 .$this->_printUnLinkPort($src_port_id, $dst_link, $linktype);
9276dc97
ME
2427
2428 echo '</td>';
2429 } /* printlink */
2430
2431 /*
2432 * print cableID dst_port:dst_object
2433 */
fdc74cf0 2434 function _printportlink($src_port_id, $dst_port_id, &$dst_link, $back = FALSE) {
9276dc97 2435
fdc74cf0 2436 global $lm_multilink_port_types;
9276dc97 2437
fdc74cf0 2438 $multilink = MULTILINK;
9276dc97 2439
fdc74cf0 2440 if(!isset($this->list[$dst_port_id]))
2441 {
2442 /* get port not in list */
2443 // echo "<td>AHHH $src_port_id $dst_port_id --> $back</td>";
2444 // echo "<td>load".$this->var_dump_html($dst_link)." tree</td>";
2445// echo "<td>".$dst_link['cable']." ".$dst_link['name']."</td><td>not displayed</td>";
9276dc97 2446
fdc74cf0 2447 if($back)
2448 echo "<td>></td>";
2449
2450 // TODO check if multilink is needed here
2451 $this->printport($dst_link, $multilink && in_array($dst_link['type'], $lm_multilink_port_types));
2452 echo "<td>...</td>";
9276dc97 2453
fdc74cf0 2454 return TRUE;
9276dc97 2455
fdc74cf0 2456 // $this->_getportlist($this->list[$src_port_id], $back, !$back);
2457 }
9276dc97 2458
fdc74cf0 2459 $dst_port = $this->list[$dst_port_id];
2460 $object_id = $dst_port['object_id'];
2461 $obj_name = $dst_port['obj_name'];
9276dc97 2462
fdc74cf0 2463 if($obj_name == NULL)
2464 {
2465 $tmpobj = spotEntity('object', $dst_port['object_id']);
2466 $dst_port['obj_name'] = $tmpobj['dname'];
2467 $obj_name = $tmpobj['dname'];
2468 }
9276dc97 2469
fdc74cf0 2470 $loop = FALSE;
2471 $edgeport = ($dst_link == NULL) || empty($dst_port['front']) || empty($dst_port['back']);
2472
2473 if($back) {
2474 $linktype = 'back';
2475 } else {
2476 $linktype = 'front';
2477 }
9276dc97 2478
fdc74cf0 2479 $sameobject = FALSE;
2480
2481 if(isset($dst_link['loop']))
2482 $loop = TRUE;
2483
2484 if($dst_link != NULL) {
2485
2486 $src_object_id = $this->list[$src_port_id]['object_id'];
2487
2488 if(!$this->allback && $object_id == $src_object_id && $back) {
2489 $sameobject = TRUE;
9276dc97 2490 } else {
fdc74cf0 2491 $this->printlink($src_port_id, $dst_link, $linktype);
2492 }
2493
2494 } else {
2495 $this->_printlinkportsymbol($dst_port_id, $linktype);
2496 $edgeport = true;
9276dc97
ME
2497
2498 if(!$back)
fdc74cf0 2499 $this->printcomment($dst_port);
2500 }
9276dc97
ME
2501
2502 if($back) {
2503 if(!$sameobject)
2504 $this->printobject($object_id,$obj_name);
fdc74cf0 2505
9276dc97
ME
2506 echo "<td>></td>";
2507
2508 /* align ports nicely */
fdc74cf0 2509 if($dst_port['id'] == $this->port_id)
2510 echo '</td></tr></table id=printportlink1></td><td><table align=left><tr>';
9276dc97
ME
2511 }
2512
2513 /* print [portname] */
fdc74cf0 2514 // TODO check multilink symbols front/back edgeports
2515 $this->printport($dst_port, $multilink && in_array($dst_port['type'], $lm_multilink_port_types));
9276dc97
ME
2516
2517 if($loop)
2518 echo '<td bgcolor=#ff9966>LOOP</td>';
2519
2520 if(!$back) {
2521
2522 /* align ports nicely */
fdc74cf0 2523 if($dst_port['id'] == $this->port_id)
2524 echo '</td></tr></table id=printportlink2></td><td><table align=left><tr>';
9276dc97
ME
2525
2526 echo "<td><</td>";
2527 $this->printobject($object_id,$obj_name);
2528
fdc74cf0 2529 if(empty($dst_port['back']))
2530 $this->_printlinkportsymbol($dst_port_id, 'back');
9276dc97 2531 } else
fdc74cf0 2532 if(empty($dst_port['front'])) {
2533 $this->printcomment($dst_port);
2534 $this->_printlinkportsymbol($dst_port_id, 'front');
9276dc97 2535 }
fdc74cf0 2536
9276dc97 2537 if($loop) {
fdc74cf0 2538 if(isset($dst_link['loopmaxcount']))
9276dc97
ME
2539 $reason = " (MAX LOOP COUNT reached)";
2540 else
2541 $reason = '';
2542
fdc74cf0 2543 showWarning("Possible Loop on Port ($linktype) ".$dst_port['name'].$reason);
9276dc97
ME
2544 return FALSE;
2545 }
2546
2547 return TRUE;
2548
fdc74cf0 2549 } /* _printportlink */
9276dc97
ME
2550
2551 /*
2552 * print <tr>..</tr>
2553 */
fdc74cf0 2554 function printportlistrow($first = TRUE, $hl_port_id = NULL, $rowbgcolor = '#ffffff') {
2555
9276dc97
ME
2556 $this->loopcount = 0;
2557
2558 if($this->first_id == NULL)
fdc74cf0 2559 return false;
9276dc97
ME
2560
2561 if($first)
2562 $id = $this->first_id;
2563 else
2564 $id = $this->last_id;
2565
9276dc97
ME
2566 if($hl_port_id == $this->port_id)
2567 $hlbgcolor = "bgcolor=".self::HL_PORT_BGCOLOR;
2568 else
fdc74cf0 2569 $hlbgcolor = "bgcolor=$rowbgcolor";
9276dc97
ME
2570
2571 $link = NULL;
2572
2573 $port = $this->list[$id];
2574
fdc74cf0 2575 $urlparams = array(
2576 'module' => 'redirect',
2577 'page' => 'object',
2578 'tab' => 'linkmgmt',
2579 'op' => 'map',
2580 'usemap' => 1,
2581 'object_id' => $port['object_id'],
2582 );
2583
2584 if($hl_port_id !== NULL)
2585 $urlparams['hl_port_id'] = $hl_port_id;
2586 else
2587 $urlparams['port_id'] = $id;
2588
2589 $title = "linkcount: ".$this->count." (".$this->front_count."/".$this->back_count.")\nTypeID: ${port['type']}\nPortID: $id";
2590
2591 $onclick = 'onclick=window.open("'.makeHrefProcess(portlist::urlparamsarray(
2592 $urlparams)).'","Map","height=500,width=800,scrollbars=yes");';
9276dc97
ME
2593
2594 /* Current Port */
fdc74cf0 2595 echo '<tr '.$hlbgcolor.'><td nowrap="nowrap" bgcolor='.self::CURRENT_PORT_BGCOLOR.' title="'.$title.
2596 '"><a '.$onclick.'>'.
2597 $this->port['name'].': </a></td>';
9276dc97 2598
fdc74cf0 2599 echo "<td><table id=printportlistrow1 align=right><tr><td>";
2600
2601 $back = empty($this->list[$id]['back']);
2602
2603 $this->_printportlink(NULL, $id, $link, $back);
9276dc97
ME
2604
2605 $this->_printportlist($id, !$back);
fdc74cf0 2606 echo "</td></tr></table id=printportlistrow2></td></tr>";
9276dc97
ME
2607
2608 /* horizontal line */
2609 echo '<tr><td height=1 colspan=3 bgcolor=#e0e0e0></td></tr>';
2610
fdc74cf0 2611 return true;
2612
9276dc97
ME
2613 } /* printportlist */
2614
2615 /*
fdc74cf0 2616 * print <td>
9276dc97
ME
2617 * prints all ports in a list starting with start_port_id
2618 */
2619 /* !!! recursive */
2620 function _printportlist($src_port_id, $back = FALSE) {
2621
2622 if($back)
2623 $linktype = 'back';
2624 else
2625 $linktype = 'front';
2626
fdc74cf0 2627 if(!empty($this->list[$src_port_id][$linktype])) {
9276dc97 2628
fdc74cf0 2629 $linkcount = count($this->list[$src_port_id][$linktype]);
9276dc97 2630
fdc74cf0 2631 if($linkcount > 1)
2632 echo "<td bgcolor=#f00000></td><td><table id=_printportlist1>";
9276dc97 2633
fdc74cf0 2634 $lastkey = $linkcount - 1;
9276dc97 2635
fdc74cf0 2636 foreach($this->list[$src_port_id][$linktype] as $key => &$link) {
2637
2638 if($linkcount > 1) {
2639 echo "<tr style=\"background-color:".( $key % 2 ? self::ALTERNATE_ROW_BGCOLOR : "#ffffff" )."\"><td><table id=_printportlist2><tr>";
2640 }
2641
2642 $dst_port_id = $link['id'];
2643
2644 $this->loopcount++;
2645
2646 if($this->loopcount > self::MAX_LOOP_COUNT) {
2647 // $src_port_name = $this->list[$src_port_id]['name'];
2648 // $dst_port_name = $this->list[$dst_port_id]['name'];
9276dc97 2649
fdc74cf0 2650 $link['loop'] = $dst_port_id;
2651 $link['loopmaxcount'] = $dst_port_id;
2652
2653 /* loop warning is handeld in _printportlink() */
2654 //showWarning("MAX LOOP COUNT reached $src_port_name -> $dst_port_name".self::MAX_LOOP_COUNT);
2655 //return; /* return after _printportlink */
2656 }
2657
2658 if(!$this->_printportlink($src_port_id, $dst_port_id, $link, $back))
2659 {
9276dc97 2660 return;
fdc74cf0 2661 }
9276dc97 2662
fdc74cf0 2663 $this->_printportlist($dst_port_id,!$back);
9276dc97 2664
fdc74cf0 2665 if($linkcount > 1) {
2666 echo "</tr></table></td></tr>"
2667 .( $key != $lastkey ? "<tr><td height=1 colspan=100% bgcolor=#c0c0c0><td></tr>" : "");
2668 }
2669 }
9276dc97 2670
fdc74cf0 2671 if($linkcount > 1)
2672 echo "</table></td>";
2673 }
9276dc97
ME
2674 } /* _printportlist */
2675
fdc74cf0 2676 /*
9276dc97
ME
2677 * returns linked Row / Rack Info for object_id
2678 *
2679 */
2680 function _getRackInfo($object_id, $style = '') {
2681 global $lm_cache;
2682
2683 $rackinfocache = $lm_cache['rackinfo'];
2684
2685 /* if not in cache get it */
2686 if(!array_key_exists($object_id,$rackinfocache)) {
2687
fdc74cf0 2688 $object = spotEntity('object', $object_id);
2689 $rack_id = $object['rack_id'];
9276dc97 2690
fdc74cf0 2691 if($rack_id)
2692 $rackinfocache[$object_id] = spotEntity('rack', $object['rack_id']);
9276dc97
ME
2693 }
2694
9276dc97 2695
fdc74cf0 2696 $rack = &$rackinfocache[$object_id];
2697
2698 if(empty($rack))
9276dc97
ME
2699 return '<span style="'.$style.'">Unmounted</span>';
2700 else
fdc74cf0 2701 return '<a style="'.$style.'" href='.makeHref(array('page'=>'row', 'row_id'=>$rack['row_id'])).'>'.$rack['row_name']
2702 .'</a>/<a style="'.$style.'" href='.makeHref(array('page'=>'rack', 'rack_id'=>$rack['id'])).'>'
2703 .$rack['name'].'</a>';
9276dc97
ME
2704
2705 } /* _getRackInfo */
2706
fdc74cf0 2707 /*
2708 * return link symbol
2709 */
2710 function _getlinkportsymbol($port_id, $linktype) {
2711 $retval = '<span onclick=window.open("'.makeHrefProcess(portlist::urlparamsarray(
2712 array('op' => 'PortLinkDialog','port' => $port_id,'linktype' => $linktype ))).'","name","height=800,width=800");'
2713 .'>';
2714
2715 $img = getImageHREF ('plug', $linktype.' Link this port');
2716
2717 if($linktype == 'back')
2718 $img = str_replace('<img',
2719 '<img style="transform:rotate(180deg);-o-transform:rotate(180deg);-ms-transform:rotate(180deg);-moz-transform:rotate(180deg);-webkit-transform:rotate(180deg);"',
2720 $img);
2721
2722 $retval .= $img;
2723 $retval .= "</span>";
2724 return $retval;
2725
2726 } /* _getlinkportsymbol */
9276dc97 2727
fdc74cf0 2728 /*
2729 * print link symbol
9276dc97
ME
2730 *
2731 */
fdc74cf0 2732 function _printlinkportsymbol($port_id, $linktype = 'front') {
9276dc97
ME
2733 global $lm_cache;
2734
2735 if(!$lm_cache['allowlink'])
2736 return;
9276dc97
ME
2737
2738 echo "<td align=center>";
2739
fdc74cf0 2740 echo $this->_getlinkportsymbol($port_id, $linktype);
9276dc97
ME
2741
2742 echo "</td>";
2743
fdc74cf0 2744 } /* _printlinkportsymbol */
9276dc97 2745
fdc74cf0 2746 /*
2747 * return link cut symbol
9276dc97
ME
2748 *
2749 * TODO $opspec_list
2750 */
fdc74cf0 2751 function _printUnLinkPort($src_port_id, &$dst_link, $linktype) {
9276dc97
ME
2752 global $lm_cache;
2753
2754 if(!$lm_cache['allowlink'])
2755 return '';
2756
fdc74cf0 2757 $src_port = $this->list[$src_port_id];
9276dc97 2758
fdc74cf0 2759 $dst_port = $this->list[$dst_link['id']];
9276dc97
ME
2760
2761 /* use RT unlink for front link, linkmgmt unlink for back links */
2762 if($linktype == 'back')
2763 $tab = 'linkmgmt';
2764 else
2765 $tab = 'ports';
9276dc97 2766
fdc74cf0 2767 return '<a href='.
9276dc97
ME
2768 makeHrefProcess(array(
2769 'op'=>'unlinkPort',
fdc74cf0 2770 'port_id'=>$src_port_id,
2771 'remote_id' => $dst_port['id'],
9276dc97
ME
2772 'object_id'=>$this->object_id,
2773 'tab' => $tab,
2774 'linktype' => $linktype)).
2775 ' onclick="return confirm(\'unlink ports '.$src_port['name']. ' -> '.$dst_port['name']
fdc74cf0 2776 .' ('.$linktype.') with cable ID: '.$dst_link['cable'].'?\');">'.
9276dc97
ME
2777 getImageHREF ('cut', $linktype.' Unlink this port').'</a>';
2778
2779 } /* _printUnLinkPort */
2780
2781
2782 /*
fdc74cf0 2783 *
9276dc97 2784 */
fdc74cf0 2785 static function urlparams($name, $value, $defaultvalue = NULL) {
9276dc97
ME
2786
2787 $urlparams = $_GET;
2788
2789 if($value == $defaultvalue) {
2790
fdc74cf0 2791 /* remove param */
2792 unset($urlparams[$name]);
9276dc97 2793
fdc74cf0 2794 } else {
9276dc97 2795
fdc74cf0 2796 $urlparams[$name] = $value;
9276dc97
ME
2797
2798 }
2799
2800 return $urlparams;
2801
2802 } /* urlparams */
2803
2804 /*
2805 * $params = array('name' => 'value', ...)
2806 */
fdc74cf0 2807 static function urlparamsarray($params) {
9276dc97
ME
2808
2809 $urlparams = $_GET;
2810
2811 foreach($params as $name => $value) {
2812
2813 if($value == NULL) {
2814
fdc74cf0 2815 /* remove param */
2816 unset($urlparams[$name]);
9276dc97 2817
fdc74cf0 2818 } else {
9276dc97 2819
fdc74cf0 2820 $urlparams[$name] = $value;
9276dc97 2821
fdc74cf0 2822 }
9276dc97
ME
2823 }
2824
2825 return $urlparams;
2826
2827 } /* urlparamsarray */
2828
2829 /* */
fdc74cf0 2830 static function hasbackend($object_id) {
9276dc97
ME
2831 /* sql bitwise xor: porta ^ portb */
2832 //select cable, ((porta ^ portb) ^ 4556) as port from Link where (4556 in (porta, portb));
2833
fdc74cf0 2834 $result = usePreparedSelectBlade
2835 (
9276dc97
ME
2836 'SELECT count(*) from Port
2837 join LinkBackend on (porta = id or portb = id )
2838 where object_id = ?',
2839 array($object_id)
fdc74cf0 2840 );
2841 $retval = $result->fetchColumn();
2842
2843 $result->closeCursor();
2844
2845 return $retval != 0;
9276dc97
ME
2846
2847 } /* hasbackend */
2848
2849 /* for debugging only */
2850 function var_dump_html(&$var) {
2851 echo "<pre>------------------Start Var Dump -------------------------\n";
2852 var_dump($var);
2853 echo "\n---------------------END Var Dump ------------------------</pre>";
2854 }
2855
2856} /* portlist */
2857
2858/* -------------------------------------------------- */
2859
2860?>