don't display objects without ports in gvmap
[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
1190 if(!$allports)
1191 $where .= " AND remotePort.id is not NULL";
1192 }
1193 else
1194 {
1195 // $where = " WHERE Port.id = ? and remotePort.id is not NULL";
1196 $where = " WHERE Port.id = ?";
1197 $qparams[] = $port_id;
1198 }
1199
1200 // ORDER
1201 $order = " ORDER by oif_name, Port.Name";
1202
1203 $query .= $join.$where.$order;
1204
1205 $result = usePreparedSelectBlade ($query, $qparams);
1206
1207 $row = $result->fetchAll(PDO::FETCH_ASSOC);
1208
1209 $result->closeCursor();
1210
1211 return $row;
1212 }
1213
1214 function _getcolor($type = 'object', $key = 'default', $alpha = 'ff', &$array = NULL , $arraykey = 'color') {
1215
1216 $object = array(
1217 'current' => '#ff0000',
1218 );
1219 $port = array(
1220 'current' => '#ffff90',
1221 'remote' => '#ffffD0',
1222 );
1223
1224 $cluster = array(
1225 'current' => '#ff0000',
1226 'problem' => '#ff3030',
1227 );
1228
1229 $edge = array (
1230 'highlight' => '#ff0000',
1231 );
1232
1233 $oif_id = array(
1234 '16' => '#800000', /* AC-in */
1235 '1322' => '#ff4500', /* AC-out */
1236 '24' => '#000080', /* 1000base-t */
1237 );
1238
1239 $defaultcolor = '#000000'; /* black */
1240 $default = true;
1241
1242 if(isset(${$type}[$key]))
1243 {
1244 $default = false;
1245 $color = ${$type}[$key];
1246 }
1247 else
1248 $color = $defaultcolor;
1249
1250
1251 if($alpha != 'ff' || $default == false)
1252 {
1253 $color .= $alpha;
1254
1255 if($array !== NULL)
1256 $array[$arraykey] = $color;
1257 else
1258 return $color;
1259 }
1260 else
1261 return $defaultcolor;
1262
1263 } /* _getcolor */
1264
1265 function dump() {
1266 var_dump($this->gv);
1267 }
1268
1269 function export() {
1270 var_export($this->gv);
1271 }
1272
1273} /* class gvmap */
1274
1275/* -------------------------------------------------- */
1276
9276dc97 1277function linkmgmt_opupdate() {
fdc74cf0 1278
9276dc97
ME
1279 if(!isset($_POST['id']))
1280 exit;
fdc74cf0 1281
9276dc97
ME
1282 $ids = explode('_',$_POST['id'],3);
1283 $retval = strip_tags($_POST['value']);
1284
1285 if(isset($ids[1])) {
1286 if(permitted(NULL, NULL, 'set_link'))
1287 if(isset($ids[2]) && $ids[2] == 'back')
fdc74cf0 1288 linkmgmt_commitUpdatePortLink($ids[0], $ids[1], $retval, TRUE);
9276dc97 1289 else
fdc74cf0 1290 linkmgmt_commitUpdatePortLink($ids[0], $ids[1], $retval);
9276dc97
ME
1291 else
1292 $retval = "Permission denied!";
1293 } else {
1294 if(permitted(NULL, NULL, 'set_reserve_comment'))
1295 commitUpdatePortComment($ids[0], $retval);
1296 else
1297 $retval = "Permission denied!";
1298 }
fdc74cf0 1299
9276dc97
ME
1300 /* return what jeditable should display after edit */
1301 echo $retval;
1302
fdc74cf0 1303 exit;
9276dc97
ME
1304} /* opupdate */
1305
1306/* -------------------------------------------------- */
1307
1308/* similar to commitUpatePortLink in database.php with backend support */
fdc74cf0 1309function linkmgmt_commitUpdatePortLink($port_id1, $port_id2, $cable = NULL, $backend = FALSE) {
9276dc97
ME
1310
1311 /* TODO check permissions */
1312
1313 if($backend)
1314 $table = 'LinkBackend';
1315 else
1316 $table = 'Link';
1317
fdc74cf0 1318 return usePreparedExecuteBlade
9276dc97 1319 (
fdc74cf0 1320 "UPDATE $table SET cable=\"".(mb_strlen ($cable) ? $cable : NULL).
1321 "\" WHERE ( porta = ? and portb = ?) or (portb = ? and porta = ?)",
1322 array (
1323 $port_id1, $port_id2,
1324 $port_id1, $port_id2)
1325 );
1326
9276dc97
ME
1327} /* linkmgmt_commitUpdatePortLink */
1328
1329/* -------------------------------------------------- */
1330
1331function linkmgmt_opunlinkPort() {
1332 $port_id = $_REQUEST['port_id'];
1333 $linktype = $_REQUEST['linktype'];
1334
fdc74cf0 1335 portlist::var_dump_html($_REQUEST);
1336
9276dc97
ME
1337 /* check permissions */
1338 if(!permitted(NULL, NULL, 'set_link')) {
1339 exit;
1340 }
1341
1342 if($linktype == 'back')
fdc74cf0 1343 {
9276dc97 1344 $table = 'LinkBackend';
fdc74cf0 1345 $remote_id = $_REQUEST['remote_id'];
1346
1347 $retval = usePreparedExecuteBlade
1348 (
1349 "DELETE FROM $table WHERE ( porta = ? and portb = ?) or (portb = ? and porta = ?)",
1350 array (
1351 $port_id, $remote_id,
1352 $port_id, $remote_id)
1353 );
1354 }
9276dc97 1355 else
fdc74cf0 1356 {
9276dc97
ME
1357 $table = 'Link';
1358
fdc74cf0 1359 $retval = usePreparedDeleteBlade ($table, array('porta' => $port_id, 'portb' => $port_id), 'OR');
1360 }
1361
9276dc97
ME
1362 if($retval == 0)
1363 echo " Link not found";
1364 else
1365 echo " $retval Links deleted";
1366
fdc74cf0 1367
1368 unset($_GET['module']);
1369 unset($_GET['op']);
1370
1371 header('Location: ?'.http_build_query($_GET));
1372 //header('Location: ?page='.$_REQUEST['page'].'&tab='.$_REQUEST['tab'].'&object_id='.$_REQUEST['object_id']);
9276dc97
ME
1373 exit;
1374} /* opunlinkPort */
1375
1376/* -------------------------------------------------- */
1377
1378function linkmgmt_oplinkPort() {
1379
1380 $linktype = $_REQUEST['linktype'];
1381 $cable = $_REQUEST['cable'];
1382
1383 /* check permissions */
1384 if(!permitted(NULL, NULL, 'set_link')) {
1385 echo("Permission denied!");
1386 return;
1387 }
1388
1389 if(!isset($_REQUEST['link_list'])) {
1390 //portlist::var_dump_html($_REQUEST);
1391 $porta = $_REQUEST['port'];
9276dc97 1392
fdc74cf0 1393 foreach($_REQUEST['remote_ports'] as $portb)
1394 {
1395 $link_list[] = "${porta}_${portb}";
1396
1397 /* with no MULTILINK process first value only */
1398 if(!MULTILINK)
1399 break;
1400 }
9276dc97
ME
1401 } else
1402 $link_list = $_REQUEST['link_list'];
1403
1404 foreach($link_list as $link){
fdc74cf0 1405
9276dc97
ME
1406 $ids = preg_split('/[^0-9]/',$link);
1407 $porta = $ids[0];;
1408 $portb = $ids[1];
1409
1410 $ret = linkmgmt_linkPorts($porta, $portb, $linktype, $cable);
1411
fdc74cf0 1412 //error_log("$ret - $porta - $portb");
1413 $port_info = getPortInfo ($porta);
1414 $remote_port_info = getPortInfo ($portb);
9276dc97
ME
1415 showSuccess(
1416 sprintf
1417 (
1418 'Port %s %s successfully linked with port %s %s',
1419 formatPortLink ($port_info['id'], $port_info['name'], NULL, NULL),
1420 $linktype,
1421 formatPort ($remote_port_info),
1422 $linktype
1423 )
1424 );
1425 }
1426
9276dc97
ME
1427 addJS (<<<END
1428window.opener.location.reload(true);
1429window.close();
1430END
1431 , TRUE);
1432
1433 return;
1434} /* oplinkPort */
1435
1436/* -------------------------------------------------- */
1437
1438/*
1439 * same as in database.php extendend with linktype
1440 */
1441function linkmgmt_linkPorts ($porta, $portb, $linktype, $cable = NULL)
1442{
1443 if ($porta == $portb)
1444 throw new InvalidArgException ('porta/portb', $porta, "Ports can't be the same");
1445
1446 if($linktype == 'back')
fdc74cf0 1447 {
9276dc97 1448 $table = 'LinkBackend';
fdc74cf0 1449 $multilink = MULTILINK;
1450 }
9276dc97 1451 else
fdc74cf0 1452 {
9276dc97 1453 $table = 'Link';
fdc74cf0 1454 $multilink = false;
1455 }
9276dc97
ME
1456
1457 global $dbxlink;
1458 $dbxlink->exec ('LOCK TABLES '.$table.' WRITE');
fdc74cf0 1459
1460 if(!$multilink)
1461 {
1462 $result = usePreparedSelectBlade
1463 (
1464 'SELECT COUNT(*) FROM '.$table.' WHERE porta IN (?,?) OR portb IN (?,?)',
1465 array ($porta, $portb, $porta, $portb)
1466 );
1467
1468 if ($result->fetchColumn () != 0)
1469 {
1470 $dbxlink->exec ('UNLOCK TABLES');
1471 return "$linktype Port ${porta} or ${portb} is already linked";
1472 }
1473 $result->closeCursor ();
1474 }
1475
9276dc97
ME
1476 if ($porta > $portb)
1477 {
1478 $tmp = $porta;
1479 $porta = $portb;
1480 $portb = $tmp;
1481 }
1482 $ret = FALSE !== usePreparedInsertBlade
1483 (
1484 $table,
1485 array
1486 (
1487 'porta' => $porta,
1488 'portb' => $portb,
1489 'cable' => mb_strlen ($cable) ? $cable : ''
1490 )
1491 );
1492 $dbxlink->exec ('UNLOCK TABLES');
1493 $ret = $ret and FALSE !== usePreparedExecuteBlade
1494 (
1495 'UPDATE Port SET reservation_comment=NULL WHERE id IN(?, ?)',
1496 array ($porta, $portb)
1497 );
1498 return $ret ? '' : 'query failed';
1499}
1500
1501/* -------------------------------------------------- */
1502
1503/*
1504 * similar to renderPopupHTML in popup.php
1505 */
1506function linkmgmt_opPortLinkDialog() {
1507// portlist::var_dump_html($_REQUEST);
1508header ('Content-Type: text/html; charset=UTF-8');
1509?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
1510<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" style="height: 100%;">
1511<?php
1512
1513 $text = '<div style="background-color: #f0f0f0; border: 1px solid #3c78b5; padding: 10px; text-align: center;
1514 margin: 5px;">';
1515
fdc74cf0 1516 if(permitted(NULL,NULL,"set_link"))
1517 if (isset ($_REQUEST['do_link'])) {
1518 $text .= getOutputOf ('linkmgmt_oplinkPort');
1519 }
1520 else
1521 if(isset($_REQUEST['byname']))
1522 $text .= getOutputOf ('linkmgmt_renderPopupPortSelectorbyName');
1523 else
1524 $text .= getOutputOf ('linkmgmt_renderPopupPortSelector');
1525 else
1526 $text .= "Permission denied!";
1527
1528 $text .= '</div>';
1529
1530 echo '<head><title>RackTables pop-up</title>';
1531 printPageHeaders();
1532 echo '</head>';
1533 echo '<body style="height: 100%;">' . $text . '</body>';
1534?>
1535</html>
1536<?php
1537 exit;
1538} /* opPortLinkDialog */
1539
1540/* -------------------------------------------------- */
1541
1542/*
1543 * like findSparePorts in popup.php extended with linktype
1544 *
1545 * multilink
1546 *
1547 */
1548function linkmgmt_findSparePorts($port_info, $filter, $linktype, $multilink = false, $objectsonly = false, $byname = false, $portcompat = true, $src_object_id = NULL) {
1549
1550
1551 /*
1552 $linktable ports that will be returned if not linked in this table
1553 $linkinfotable display link for info only show backend links if you want front link a port
1554
1555 front: select ports no front connection and port compat, filter, ...
1556
1557 back:
1558
1559 */
1560
1561 if($linktype == 'back')
1562 {
1563 $linktable = 'LinkBackend';
1564 $linkinfotable = 'Link';
1565 }
1566 else
1567 {
1568 $linktable = 'Link';
1569 $linkinfotable = 'LinkBackend';
1570 }
1571
1572 $qparams = array();
1573 $whereparams = array();
1574
1575 // all ports with no link
1576 /* port:object -> linked port:object */
1577 $query = 'SELECT';
1578 $join = "";
1579 $where = " WHERE";
1580 $group = "";
1581 $order = " ORDER BY";
1582
1583 if($objectsonly)
1584 {
1585 $query .= " remotePort.object_id, CONCAT(IFNULL(remoteObject.name, CONCAT('[',remoteObjectDictionary.dict_value,']')), ' (', count(remotePort.id), ')') as name";
1586 $group .= " GROUP by remoteObject.id";
1587 }
1588 else
1589 if($byname)
1590 {
1591 if($linktype == 'back')
1592 $arrow = '=?=>';
1593 else
1594 $arrow = '-?->';
1595
1596 $query .= ' CONCAT(localPort.id, "_", remotePort.id),
1597 CONCAT(IFNULL(localObject.name, CONCAT("[",localObjectDictionary.dict_value,"]")), " : ", localPort.Name, " '.$arrow.'", remotePort.name, " : ", IFNULL(remoteObject.name,CONCAT("[",remoteObjectDictionary.dict_value,"]")))';
9276dc97 1598 }
fdc74cf0 1599 else
1600 {
1601
1602 if($linktype == 'front')
1603 $arrow = '==';
9276dc97 1604 else
fdc74cf0 1605 $arrow = '--';
9276dc97 1606
fdc74cf0 1607 $query .= " remotePort.id, CONCAT(IFNULL(remoteObject.name, CONCAT('[',remoteObjectDictionary.dict_value,']')), ' : ', remotePort.name,
1608 IFNULL(CONCAT(' $arrow ', IFNULL(infolnk.cable,''), ' $arrow> ', InfoPort.name, ' : ', IFNULL(InfoObject.name,CONCAT('[',InfoObjectDictionary.dict_value,']'))),'') ) as Text";
1609 }
9276dc97 1610
fdc74cf0 1611 $query .= " FROM Port as remotePort";
1612 $join .= " LEFT JOIN Object as remoteObject on remotePort.object_id = remoteObject.id";
1613 $order .= " remoteObject.name";
9276dc97 1614
fdc74cf0 1615 /* object type name */
1616 $join .= " LEFT JOIN Dictionary as remoteObjectDictionary on (remoteObjectDictionary.chapter_id = 1 AND remoteObject.objtype_id = remoteObjectDictionary.dict_key)";
9276dc97 1617
fdc74cf0 1618 if($byname)
1619 {
1620 /* by name */
1621 $join .= " JOIN Port as localPort on remotePort.name = localPort.name";
1622 $where .= " remotePort.object_id <> ? AND localPort.object_id = ?";
1623 $whereparams[] = $src_object_id;
1624 $whereparams[] = $src_object_id;
9276dc97 1625
fdc74cf0 1626 /* own port not linked */
1627 $join .= " LEFT JOIN $linktable as localLink on localPort.id in (localLink.porta, localLink.portb)";
1628 $join .= " LEFT JOIN Object as localObject on localObject.id = localPort.object_id";
1629 $where .= " AND localLink.porta is NULL";
1630
1631 /* object type name */
1632 $join .= " LEFT JOIN Dictionary as localObjectDictionary on (localObject.objtype_id = localObjectDictionary.dict_key AND localObjectDictionary.chapter_id = 1)";
1633 }
1634 else
1635 {
1636 /* exclude current port */
1637 $where .= " remotePort.id <> ?";
1638 $whereparams[] = $port_info['id'];
1639 $order .= " ,remotePort.name";
1640
1641 /* add info to remoteport */
1642 $join .= " LEFT JOIN $linkinfotable as infolnk on remotePort.id in (infolnk.porta, infolnk.portb)";
1643 $join .= " LEFT JOIN Port as InfoPort on InfoPort.id = ((infolnk.porta ^ infolnk.portb) ^ remotePort.id)";
1644 $join .= " LEFT JOIN Object as InfoObject on InfoObject.id = InfoPort.object_id";
1645
1646 /* object type name */
1647 $join .= " LEFT JOIN Dictionary as InfoObjectDictionary on (InfoObject.objtype_id = InfoObjectDictionary.dict_key AND InfoObjectDictionary.chapter_id = 1)";
1648 }
1649
1650 /* only ports which are not linked already */
1651 $join .= " LEFT JOIN $linktable as lnk on remotePort.id in (lnk.porta, lnk.portb)";
1652 $where .= " AND lnk.porta is NULL";
1653
1654 if($portcompat)
1655 {
1656 /* port compat */
1657 $join .= ' INNER JOIN PortInnerInterface pii ON remotePort.iif_id = pii.id
1658 INNER JOIN Dictionary d ON d.dict_key = remotePort.type';
1659 // porttype filter (non-strict match)
1660 $join .= ' INNER JOIN (
1661 SELECT Port.id FROM Port
1662 INNER JOIN
1663 (
1664 SELECT DISTINCT pic2.iif_id
1665 FROM PortInterfaceCompat pic2
1666 INNER JOIN PortCompat pc ON pc.type2 = pic2.oif_id';
1667
1668 if ($port_info['iif_id'] != 1)
1669 {
1670 $join .= " INNER JOIN PortInterfaceCompat pic ON pic.oif_id = pc.type1 WHERE pic.iif_id = ?";
1671 $qparams[] = $port_info['iif_id'];
1672 }
1673 else
1674 {
1675 $join .= " WHERE pc.type1 = ?";
1676 $qparams[] = $port_info['oif_id'];
1677 }
1678 $join .= " AND pic2.iif_id <> 1
1679 ) AS sub1 USING (iif_id)
1680 UNION
1681 SELECT Port.id
1682 FROM Port
1683 INNER JOIN PortCompat ON type1 = type
1684 WHERE iif_id = 1 and type2 = ?
1685 ) AS sub2 ON sub2.id = remotePort.id";
1686 $qparams[] = $port_info['oif_id'];
1687 }
9276dc97 1688
fdc74cf0 1689
1690 $qparams = array_merge($qparams, $whereparams);
9276dc97
ME
1691
1692 // rack filter
1693 if (! empty ($filter['racks']))
1694 {
fdc74cf0 1695 $where .= ' AND remotePort.object_id IN (SELECT DISTINCT object_id FROM RackSpace WHERE rack_id IN (' .
9276dc97
ME
1696 questionMarks (count ($filter['racks'])) . ')) ';
1697 $qparams = array_merge ($qparams, $filter['racks']);
1698 }
1699
fdc74cf0 1700 // object_id filter
1701 if (! empty ($filter['object_id']))
1702 {
1703 $where .= ' AND remoteObject.id = ?';
1704 $qparams[] = $filter['object_id'];
1705 }
1706 else
9276dc97
ME
1707 // objectname filter
1708 if (! empty ($filter['objects']))
1709 {
fdc74cf0 1710 $where .= ' AND remoteObject.name like ? ';
9276dc97
ME
1711 $qparams[] = '%' . $filter['objects'] . '%';
1712 }
fdc74cf0 1713
9276dc97
ME
1714 // portname filter
1715 if (! empty ($filter['ports']))
1716 {
fdc74cf0 1717 $where .= ' AND remotePort.name LIKE ? ';
9276dc97
ME
1718 $qparams[] = '%' . $filter['ports'] . '%';
1719 }
9276dc97 1720
fdc74cf0 1721 $query .= $join.$where.$group.$order;
9276dc97
ME
1722
1723 $result = usePreparedSelectBlade ($query, $qparams);
1724
1725 $row = $result->fetchAll(PDO::FETCH_GROUP|PDO::FETCH_UNIQUE|PDO::FETCH_COLUMN);
1726
fdc74cf0 1727 $result->closeCursor();
1728
1729 /* [id] => displaystring */
9276dc97 1730 return $row;
fdc74cf0 1731
9276dc97
ME
1732} /* findSparePorts */
1733
1734/* -------------------------------------------------- */
1735
1736/*
fdc74cf0 1737 * like renderPopupPortSelector in popup.php extenden with linktype
9276dc97 1738 */
fdc74cf0 1739function linkmgmt_renderPopupPortSelector()
1740{
1741 require_once 'inc/popup.php'; /* getProximateRacks */
9276dc97 1742
fdc74cf0 1743 global $lm_multilink_port_types;
9276dc97 1744
fdc74cf0 1745 assertUIntArg ('port');
1746 $port_id = $_REQUEST['port'];
9276dc97 1747
fdc74cf0 1748 $showlinktypeswitch = false;
9276dc97 1749
fdc74cf0 1750 if(isset($_GET['linktype']))
1751 $linktype = $_GET['linktype'];
1752 else
1753 $linktype = 'front';
9276dc97 1754
fdc74cf0 1755 if($linktype == 'both')
1756 {
9276dc97 1757
fdc74cf0 1758 /*
1759 * use POST front/back_view to set linktype
1760 * and show linktype switch button
1761 */
9276dc97 1762
fdc74cf0 1763 $showlinktypeswitch = true;
1764
1765 if(isset($_POST['front_view']))
1766 $linktype = 'front';
1767 else
1768 if(isset($_POST['back_view']))
1769 $linktype = 'back';
1770 else
1771 $linktype = 'front';
1772 }
1773
1774// portlist::var_dump_html($_POST);
1775
1776 $portcompat = true;
1777
1778 if($linktype == 'back')
1779 {
1780 if(isset($_POST['portcompat']))
1781 $portcompat = $_POST['portcompat'];
1782 }
9276dc97 1783
9276dc97
ME
1784 $object_id = $_REQUEST['object_id'];
1785 $port_info = getPortInfo ($port_id);
fdc74cf0 1786
1787 $multilink = MULTILINK && $linktype == 'back' && in_array($port_info['oif_id'], $lm_multilink_port_types);
1788
1789 if(isset ($_REQUEST['in_rack']))
1790 $in_rack = $_REQUEST['in_rack'] != 'off';
1791 else
1792 $in_rack = true;
9276dc97
ME
1793
1794// portlist::var_dump_html($port_info);
fdc74cf0 1795// portlist::var_dump_html($_GET);
1796// portlist::var_dump_html($_POST);
9276dc97
ME
1797
1798 // fill port filter structure
1799 $filter = array
1800 (
1801 'racks' => array(),
1802 'objects' => '',
fdc74cf0 1803 'object_id' => '',
9276dc97
ME
1804 'ports' => '',
1805 );
fdc74cf0 1806
1807 $remote_object = NULL;
1808 if(isset($_REQUEST['remote_object']))
1809 {
1810 $remote_object = $_REQUEST['remote_object'];
1811
1812 if($remote_object != 'NULL')
1813 $filter['object_id'] = $remote_object;
1814 }
1815
9276dc97
ME
1816 if (isset ($_REQUEST['filter-obj']))
1817 $filter['objects'] = $_REQUEST['filter-obj'];
1818 if (isset ($_REQUEST['filter-port']))
1819 $filter['ports'] = $_REQUEST['filter-port'];
1820 if ($in_rack)
1821 {
1822 $object = spotEntity ('object', $port_info['object_id']);
1823 if ($object['rack_id'])
1824 $filter['racks'] = getProximateRacks ($object['rack_id'], getConfigVar ('PROXIMITY_RANGE'));
1825 }
fdc74cf0 1826
1827 $objectlist = array('NULL' => '- Show All -');
1828 $objectlist = $objectlist + linkmgmt_findSparePorts($port_info, $filter, $linktype, $multilink, true, false, $portcompat);
1829
1830 $spare_ports = linkmgmt_findSparePorts ($port_info, $filter, $linktype, $multilink, false, false, $portcompat);
1831
1832 $maxsize = getConfigVar('MAXSELSIZE');
1833 $objectcount = count($objectlist);
1834
1835 if($linktype == 'back')
1836 $notlinktype = 'front';
1837 else
1838 $notlinktype = 'back';
9276dc97
ME
1839
1840 // display search form
1841 echo 'Link '.$linktype.' of ' . formatPort ($port_info) . ' to...';
1842 echo '<form method=POST>';
1843 startPortlet ($linktype.' Port list filter');
1844 // echo '<input type=hidden name="module" value="popup">';
1845 // echo '<input type=hidden name="helper" value="portlist">';
1846
1847 echo '<input type=hidden name="port" value="' . $port_id . '">';
fdc74cf0 1848 echo '<table><tr><td valign="top"><table><tr><td>';
1849
1850 echo '<table align="center"><tr>';
1851
1852// 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
1853 echo '<td class="tdleft"><label>Object name:<br><input type=text size=8 name="filter-obj" value="' . htmlspecialchars ($filter['objects'], ENT_QUOTES) . '"></label></td>';
1854 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 1855 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 1856 echo '</tr></table>';
fdc74cf0 1857
1858 echo '</td></tr><tr><td>';
1859 echo 'Object name (count ports)<br>';
1860 echo getSelect ($objectlist, array ('name' => 'remote_object',
1861 'size' => ($objectcount <= $maxsize ? $objectcount : $maxsize)),
1862 $remote_object, FALSE);
1863
1864 echo '</td></tr></table></td>';
1865 echo '<td valign="top"><table><tr><td><input type=submit value="update objects / ports"></td></tr>';
1866
1867 if($showlinktypeswitch)
1868 echo '<tr height=150px><td><input type=submit value="Switch to '.$notlinktype.' view" name="'.$notlinktype.'_view"></tr></td>';
1869
1870 if($linktype == 'back')
1871 {
1872 echo '<input type="hidden" name="portcompat" value="0">';
1873 echo '<tr height=150px><td><input type=checkbox onchange="this.form.submit();" name="portcompat"'.( $portcompat ? 'checked="checked" ' : '' ).'value="1">Port Compatibility</input></tr></td>';
1874 echo '<input type="hidden" name="back_view">';
1875 }
1876
1877 echo '</table></td>';
1878
9276dc97 1879 finishPortlet();
fdc74cf0 1880 echo '</td><td>';
9276dc97
ME
1881
1882 // display results
1883 startPortlet ('Compatible spare '.$linktype.' ports');
fdc74cf0 1884 echo "spare $linktype Object:Port -- $notlinktype cableID --> $notlinktype Port:Object<br>";
1885
1886 if($multilink)
1887 echo "Multilink<br>";
9276dc97
ME
1888
1889 if (empty ($spare_ports))
1890 echo '(nothing found)';
1891 else
1892 {
fdc74cf0 1893 $linkcount = count($spare_ports);
1894
1895 $options = array(
1896 'name' => 'remote_ports[]',
1897 'size' => getConfigVar ('MAXSELSIZE'),
1898 'size' => ($linkcount <= $maxsize ? $linkcount : $maxsize),
1899 );
1900
1901 if($multilink)
1902 $options['multiple'] = 'multiple';
1903
1904 echo getSelect ($spare_ports, $options, NULL, FALSE);
1905
9276dc97
ME
1906 echo "<p>$linktype Cable ID: <input type=text id=cable name=cable>";
1907 echo "<p><input type='submit' value='Link $linktype' name='do_link'>";
1908 }
1909 finishPortlet();
fdc74cf0 1910 echo '</td></tr></table>';
9276dc97
ME
1911 echo '</form>';
1912
1913} /* linkmgmt_renderPopUpPortSelector */
1914
1915/* -------------------------------------------------- */
1916
1917/*
1918 * similar to renderPopupPortSelector but let you select the destination object
1919 * and displays possible backend links with ports of the same name
1920 */
1921function linkmgmt_renderPopupPortSelectorbyName()
1922{
1923 $linktype = $_REQUEST['linktype'];
1924 $object_id = $_REQUEST['object_id'];
1925
1926 $object = spotEntity ('object', $object_id);
1927
fdc74cf0 1928 $objectlist = linkmgmt_findSparePorts(NULL, NULL, $linktype, false, true, TRUE, false, $object_id);
1929
1930 $objectname = $object['dname'];
1931
1932 /* remove self from list */
1933 unset($objectlist[$object_id]);
1934
9276dc97
ME
1935 if(isset($_REQUEST['remote_object']))
1936 $remote_object = $_REQUEST['remote_object'];
1937 else
fdc74cf0 1938 {
1939 /* choose first object from list */
1940 $keys = array_keys($objectlist);
1941
1942 if(isset($keys[0]))
1943 $remote_object = $keys[0];
1944 else
1945 $remote_object = NULL;
1946 }
9276dc97
ME
1947
1948 if($remote_object)
fdc74cf0 1949 {
1950 $filter['object_id'] = $remote_object;
1951 $link_list = linkmgmt_findSparePorts(NULL, $filter, $linktype, false, false, TRUE, false, $object_id);
1952 }
1953 else
1954 $link_list = linkmgmt_findSparePorts(NULL, NULL, $linktype, false, false, TRUE, false, $object_id);
9276dc97
ME
1955
1956 // display search form
fdc74cf0 1957 echo 'Link '.$linktype.' of ' . formatPortLink($object_id, $objectname, NULL, NULL) . ' Ports by Name to...';
9276dc97 1958 echo '<form method=POST>';
9276dc97 1959
fdc74cf0 1960 echo '<table align="center"><tr><td>';
1961 startPortlet ('Object list');
9276dc97 1962
fdc74cf0 1963 $maxsize = getConfigVar('MAXSELSIZE');
1964 $objectcount = count($objectlist);
9276dc97 1965
fdc74cf0 1966 echo 'Object name (count ports)<br>';
1967 echo getSelect ($objectlist, array ('name' => 'remote_object',
1968 'size' => ($objectcount <= $maxsize ? $objectcount : $maxsize)),
1969 $remote_object, FALSE);
1970 echo '</td><td><input type=submit value="show '.$linktype.' ports>"></td>';
9276dc97
ME
1971 finishPortlet();
1972
fdc74cf0 1973 echo '<td>';
9276dc97
ME
1974 // display results
1975 startPortlet ('Possible Backend Link List');
1976 echo "Select links to create:<br>";
1977 if (empty ($link_list))
1978 echo '(nothing found)';
1979 else
1980 {
fdc74cf0 1981 $linkcount = count($link_list);
1982
1983 $options = array(
1984 'name' => 'link_list[]',
1985 'size' => ($linkcount <= $maxsize ? $linkcount : $maxsize),
1986 'multiple' => 'multiple',
1987 );
1988
1989 echo getSelect ($link_list,$options, NULL, FALSE);
1990
9276dc97
ME
1991 echo "<p>$linktype Cable ID: <input type=text id=cable name=cable>";
1992 echo "<p><input type='submit' value='Link $linktype' name='do_link'>";
1993 }
1994 finishPortlet();
fdc74cf0 1995 echo '</td></tr></table>';
9276dc97
ME
1996 echo '</form>';
1997
fdc74cf0 1998} /* linkmgmt_renderPopUpPortSelectorByName */
9276dc97 1999
fdc74cf0 2000/* ------------------------------------------------ */
9276dc97
ME
2001
2002function linkmgmt_tabhandler($object_id) {
2003 global $lm_cache;
2004
2005 $target = makeHrefProcess(portlist::urlparams('op','update'));
2006
fdc74cf0 2007 addJS('js/jquery.jeditable.mini.js');
9276dc97 2008
fdc74cf0 2009 /* TODO if (permitted (NULL, 'ports', 'set_reserve_comment')) */
9276dc97
ME
2010 /* TODO Link / unlink permissions */
2011
2012 $lm_cache['allowcomment'] = permitted(NULL, NULL, 'set_reserve_comment'); /* RackCode {$op_set_reserve_comment} */
2013 $lm_cache['allowlink'] = permitted(NULL, NULL, 'set_link'); /* RackCode {$op_set_link} */
fdc74cf0 2014
9276dc97
ME
2015 //portlist::var_dump_html($lm_cache);
2016
2017 /* init jeditable fields/tags */
2018 if($lm_cache['allowcomment'])
2019 addJS('$(document).ready(function() { $(".editcmt").editable("'.$target.'",{placeholder : "add comment"}); });' , TRUE);
2020
2021 if($lm_cache['allowlink'])
2022 addJS('$(document).ready(function() { $(".editcable").editable("'.$target.'",{placeholder : "edit cableID"}); });' , TRUE);
2023
9276dc97
ME
2024 /* linkmgmt for current object */
2025 linkmgmt_renderObjectLinks($object_id);
2026
2027 /* linkmgmt for every child */
2028 //$parents = getEntityRelatives ('parents', 'object', $object_id);
2029 $children = getEntityRelatives ('children', 'object', $object_id); //'entity_id'
2030
2031 //portlist::var_dump_html($children);
2032
2033 foreach($children as $child) {
2034 echo '<h1>Links for Child: '.$child['name'].'</h1>';
2035 linkmgmt_renderObjectLinks($child['entity_id']);
2036 }
2037
9276dc97
ME
2038 return;
2039
2040} /* tabhandler */
2041
2042/* -------------------------------------------------- */
2043function linkmgmt_renderObjectLinks($object_id) {
2044
fdc74cf0 2045 $object = spotEntity ('object', $object_id);
9276dc97
ME
2046 $object['attr'] = getAttrValues($object_id);
2047
2048 /* get ports */
2049 /* calls getObjectPortsAndLinks */
2050 amplifyCell ($object);
2051
2052 //$ports = getObjectPortsAndLinks($object_id);
2053 $ports = $object['ports'];
2054
fdc74cf0 2055 /* reindex array so key starts at 0 */
2056 $ports = array_values($ports);
9276dc97
ME
2057
2058 /* URL param handling */
2059 if(isset($_GET['allports'])) {
2060 $allports = $_GET['allports'];
2061 } else
2062 $allports = FALSE;
2063
2064 if(isset($_GET['allback'])) {
2065 $allback = $_GET['allback'];
2066 } else
2067 $allback = FALSE;
2068
2069 echo '<table><tr>';
2070
2071 if($allports) {
fdc74cf0 2072
9276dc97
ME
2073 echo '<td width=200><a href="'.makeHref(portlist::urlparams('allports','0','0'))
2074 .'">Hide Ports without link</a></td>';
2075 } else
2076 echo '<td width=200><a href="'.makeHref(portlist::urlparams('allports','1','0'))
2077 .'">Show All Ports</a></td>';
2078
2079 echo '<td width=200><span onclick=window.open("'.makeHrefProcess(portlist::urlparamsarray(
fdc74cf0 2080 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
2081
2082 if($allback) {
fdc74cf0 2083
9276dc97
ME
2084 echo '<td width=200><a href="'.makeHref(portlist::urlparams('allback','0','0'))
2085 .'">Collapse Backend Links on same Object</a></td>';
2086 } else
2087 echo '<td width=200><a href="'.makeHref(portlist::urlparams('allback','1','0'))
2088 .'">Expand Backend Links on same Object</a></td>';
2089
fdc74cf0 2090 /* Graphviz map */
2091 echo '<td width=100><span onclick=window.open("'.makeHrefProcess(portlist::urlparamsarray(
2092 array('op' => 'map','usemap' => 1))).'","name","height=800,width=800,scrollbars=yes");><a>Object Map</a></span></td>';
2093
9276dc97
ME
2094 /* Help */
2095 echo '<td width=200><span onclick=window.open("'.makeHrefProcess(portlist::urlparamsarray(
2096 array('op' => 'Help'))).'","name","height=400,width=500");><a>Help</a></span></td>';
2097
2098 if(isset($_REQUEST['hl_port_id']))
2099 $hl_port_id = $_REQUEST['hl_port_id'];
2100 else
fdc74cf0 2101 $hl_port_id = NULL;
9276dc97
ME
2102
2103 echo '</tr></table>';
2104
fdc74cf0 2105
2106 echo '<br><br><table id=renderobjectlinks0>';
9276dc97
ME
2107
2108 /* switch display order depending on backend links */
2109 $first = portlist::hasbackend($object_id);
2110
fdc74cf0 2111 $rowcount = 0;
2112 foreach($ports as $key => $port) {
9276dc97
ME
2113
2114 $plist = new portlist($port, $object_id, $allports, $allback);
2115
fdc74cf0 2116 //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>";
2117
2118 if($plist->printportlistrow($first, $hl_port_id, ($rowcount % 2 ? portlist::ALTERNATE_ROW_BGCOLOR : "#ffffff")) )
2119 $rowcount++;
2120
9276dc97
ME
2121 }
2122
2123 echo "</table>";
2124
2125} /* renderObjectLinks */
2126
9276dc97
ME
2127/* -------------------------------------------------- */
2128
2129/*
2130 * Portlist class
fdc74cf0 2131 * gets all linked ports to spezified port
9276dc97
ME
2132 * and prints this list as table row
2133 *
2134 */
2135class portlist {
2136
2137 public $list = array();
2138
2139 private $object_id;
2140 private $port_id;
2141 private $port;
2142
2143 private $first_id;
2144 private $front_count;
2145
2146 private $last_id;
2147 private $back_count;
2148
2149 private $count = 0;
2150
2151 private $allback = FALSE;
2152
fdc74cf0 2153 private $multilink = MULTILINK;
2154
9276dc97
ME
2155 const B2B_LINK_BGCOLOR = '#d8d8d8';
2156 const CURRENT_PORT_BGCOLOR = '#ffff99';
2157 const CURRENT_OBJECT_BGCOLOR = '#ff0000';
2158 const HL_PORT_BGCOLOR = '#00ff00';
fdc74cf0 2159 const ALTERNATE_ROW_BGCOLOR = '#f0f0f0';
9276dc97
ME
2160
2161 /* Possible LOOP detected after count links print only */
2162 const MAX_LOOP_COUNT = 13;
2163
2164 private $loopcount;
2165
fdc74cf0 2166 private $gv = NULL;
2167
9276dc97 2168 function __construct($port, $object_id, $allports = FALSE, $allback = FALSE) {
9276dc97
ME
2169
2170 $this->object_id = $object_id;
2171
2172 $this->port = $port;
2173
2174 $port_id = $port['id'];
fdc74cf0 2175
9276dc97 2176 $this->port_id = $port_id;
fdc74cf0 2177
9276dc97
ME
2178 $this->first_id = $port_id;
2179 $this->last_id = $port_id;
2180
2181 $this->allback = $allback;
2182
fdc74cf0 2183 $this->_getportlists($port_id);
9276dc97
ME
2184
2185 if(!$allports)
2186 if($this->count == 0 || ( ($this->count == 1) && (!empty($this->list[$port_id]['back'])) ) ) {
2187 $this->list = array();
2188 $this->first_id = NULL;
2189 }
2190
fdc74cf0 2191 // $this->var_dump_html($this->list);
9276dc97
ME
2192
2193 } /* __construct */
2194
2195
2196 /*
fdc74cf0 2197 * get front and back portlist
2198 */
2199 function _getportlists($port_id) {
2200
2201 /* Front Port */
2202 $this->count = 0;
2203 $this->_getportlist($this->_getportdata($port_id),FALSE, TRUE);
2204 $this->front_count = $this->count;
2205
2206 /* Back Port */
2207 $this->count = 0;
2208 $this->_getportlist($this->_getportdata($port_id), TRUE, FALSE);
2209 $this->back_count = $this->count;
2210
2211 $this->count = $this->front_count + $this->back_count;
2212 }
2213
2214 /*
2215 * gets front or back port of src_port
2216 * and adds it to the list
9276dc97
ME
2217 */
2218 /* !!! recursive */
2219 function _getportlist(&$src_port, $back = FALSE, $first = TRUE) {
9276dc97 2220
fdc74cf0 2221 $src_port_id = $src_port['id'];
2222
2223 if($back)
9276dc97
ME
2224 $linktype = 'back';
2225 else
2226 $linktype = 'front';
fdc74cf0 2227
9276dc97
ME
2228 if(!empty($src_port[$linktype])) {
2229
fdc74cf0 2230 /* multilink */
2231 foreach($src_port[$linktype] as &$src_link) {
2232 $dst_port_id = $src_link['id'];
9276dc97 2233
fdc74cf0 2234 if(!$this->_loopdetect($src_port,$dst_port_id,$src_link,$linktype)) {
2235 //error_log("no loop $linktype>".$dst_port_id);
2236 $this->count++;
2237 $this->_getportlist($this->_getportdata($dst_port_id), !$back, $first);
2238 }
9276dc97 2239 }
fdc74cf0 2240
9276dc97
ME
2241 } else {
2242 if($first) {
fdc74cf0 2243 $this->first_id = $src_port_id;
9276dc97
ME
2244 // $this->front_count = $this->count; /* doesn't work on loops */
2245 } else {
fdc74cf0 2246 $this->last_id = $src_port_id;
9276dc97
ME
2247 // $this->back_count = $this->count; /* doesn't work on loops */
2248 }
2249
2250 }
2251
fdc74cf0 2252 } /* _getportlist */
9276dc97
ME
2253
2254 /*
2255 * as name suggested
2256 */
fdc74cf0 2257 function _loopdetect(&$src_port, $dst_port_id, &$src_link, $linktype) {
9276dc97 2258
fdc74cf0 2259 /* TODO multilink*/
9276dc97
ME
2260 if(array_key_exists($dst_port_id, $this->list)) {
2261
fdc74cf0 2262 // $dst_port = $this->list[$dst_port_id];
9276dc97 2263
fdc74cf0 2264 //echo "LOOP :".$src_port['id']."-->".$dst_port_id;
9276dc97 2265
fdc74cf0 2266 /* print loop at least once */
2267 if($dst_port_id == $this->port_id)
2268 {
2269 $src_link['loop'] = $dst_port_id;
2270 return TRUE;
2271 }
9276dc97 2272
9276dc97 2273 }
fdc74cf0 2274
2275 return FALSE;
2276
9276dc97 2277 } /* _loopdetect */
fdc74cf0 2278
9276dc97
ME
2279 /*
2280 * get all data for one port
2281 * name, object, front link, back link
2282 */
2283 function &_getportdata($port_id) {
2284 /* sql bitwise xor: porta ^ portb */
2285 //select cable, ((porta ^ portb) ^ 4556) as port from Link where (4556 in (porta, portb));
2286
2287 //error_log("_getportdata $port_id");
fdc74cf0 2288
9276dc97
ME
2289 /* TODO single sql ? */
2290
fdc74cf0 2291 $result = usePreparedSelectBlade
2292 (
2293 'SELECT Port.id, Port.name, Port.label, Port.type, Port.l2address, Port.object_id, Port.reservation_comment,
2294 Object.name as "obj_name"
9276dc97 2295 from Port
fdc74cf0 2296 join Object on Object.id = Port.object_id
9276dc97
ME
2297 where Port.id = ?',
2298 array($port_id)
fdc74cf0 2299 );
2300 $datarow = $result->fetchAll(PDO::FETCH_ASSOC);
9276dc97 2301
fdc74cf0 2302 $result->closeCursor();
2303 unset($result);
2304
2305 $result = usePreparedSelectBlade
2306 (
2307 'SELECT Port.id, Link.cable, Port.name, Port.label, Port.type, Port.l2address, Port.object_id,
9276dc97
ME
2308 CONCAT(Link.porta,"_",Link.portb) as link_id from Link
2309 join Port
2310 where (? in (Link.porta,Link.portb)) and ((Link.porta ^ Link.portb) ^ ? ) = Port.id',
2311 array($port_id, $port_id)
fdc74cf0 2312 );
2313 $frontrow = $result->fetchAll(PDO::FETCH_ASSOC);
2314
2315 $result->closeCursor();
2316 unset($result);
9276dc97 2317
fdc74cf0 2318 $result = usePreparedSelectBlade
2319 (
2320 'SELECT Port.id, LinkBackend.cable, Port.name, Port.label, Port.type, Port.l2address, Port.object_id,
9276dc97
ME
2321 CONCAT(LinkBackend.porta,"_",LinkBackend.portb,"_back") as link_id from LinkBackend
2322 join Port
2323 where (? in (LinkBackend.porta,LinkBackend.portb)) and ((LinkBackend.porta ^ LinkBackend.portb) ^ ? ) = Port.id',
2324 array($port_id, $port_id)
fdc74cf0 2325 );
2326 $backrow = $result->fetchAll(PDO::FETCH_ASSOC);
2327
2328 $result->closeCursor();
2329 unset($result);
9276dc97
ME
2330
2331 $retval = $datarow[0];
2332
2333 if(!empty($frontrow))
fdc74cf0 2334 $retval['front']= $frontrow;
9276dc97
ME
2335 else
2336 $retval['front'] = array();
2337
2338 if(!empty($backrow))
fdc74cf0 2339 $retval['back'] = $backrow;
9276dc97
ME
2340 else
2341 $retval['back'] = array();
2342
2343 // $this->var_dump_html($retval);
2344
2345 /* return reference */
2346 return ($this->list[$port_id] = &$retval);
2347
2348 } /* _getportdata */
2349
2350 /*
2351 */
fdc74cf0 2352 function printport(&$port, $multilink = false) {
2353
9276dc97 2354 /* set bgcolor for current port */
fdc74cf0 2355 if($port['id'] == $this->port_id) {
9276dc97
ME
2356 $bgcolor = 'bgcolor='.self::CURRENT_PORT_BGCOLOR;
2357 $idtag = ' id='.$port['id'];
2358 } else {
2359 $bgcolor = '';
2360 $idtag = '';
2361 }
2362
2363 $mac = trim(preg_replace('/(..)/','$1:',$port['l2address']),':');
2364
fdc74cf0 2365 $title = "Label: ${port['label']}\nMAC: $mac\nTypeID: ${port['type']}\nPortID: ${port['id']}";
9276dc97
ME
2366
2367 echo '<td'.$idtag.' align=center '.$bgcolor.' title="'.$title.'"><pre>[<a href="'
2368 .makeHref(array('page'=>'object', 'tab' => 'linkmgmt', 'object_id' => $port['object_id'], 'hl_port_id' => $port['id']))
2369 .'#'.$port['id']
fdc74cf0 2370 .'">'.$port['name'].'</a>]</pre>'.($multilink ? $this->_getlinkportsymbol($port['id'], 'back') : '' ).'</td>';
9276dc97
ME
2371
2372 } /* printport */
2373
2374 /*
2375 */
2376 function printcomment(&$port) {
fdc74cf0 2377
9276dc97
ME
2378 if(!empty($port['reservation_comment'])) {
2379 $prefix = '<b>Reserved: </b>';
2380 } else
2381 $prefix = '';
2382
2383 echo '<td>'.$prefix.'<i><a class="editcmt" id='.$port['id'].'>'.$port['reservation_comment'].'</a></i></td>';
2384
2385 } /* printComment */
2386
2387
2388 /*
2389 */
2390 function printobject($object_id, $object_name) {
2391 if($object_id == $this->object_id) {
2392 $color='color: '.self::CURRENT_OBJECT_BGCOLOR;
2393 } else {
2394 $color='';
2395 }
2396
2397 echo '<td><table align=center cellpadding=5 cellspacing=0 border=1><tr><td align=center><a style="font-weight:bold;'
2398 .$color.'" href="'.makeHref(array('page'=>'object', 'tab' => 'linkmgmt', 'object_id' => $object_id))
2399 .'"><pre>'.$object_name.'</pre></a><pre>'.$this->_getRackInfo($object_id, 'font-size:80%')
2400 .'</pre></td></tr></table></td>';
2401
2402 } /* printobject */
2403
2404 /*
2405 */
fdc74cf0 2406 function printlink($src_port_id, &$dst_link, $linktype) {
9276dc97
ME
2407
2408 if($linktype == 'back')
2409 $arrow = '====>';
2410 else
2411 $arrow = '---->';
2412
2413 /* link */
2414 echo '<td align=center>';
2415
fdc74cf0 2416 echo '<pre><a class="editcable" id='.$dst_link['link_id'].'>'.$dst_link['cable']
9276dc97 2417 ."</a></pre><pre>$arrow</pre>"
fdc74cf0 2418 .$this->_printUnLinkPort($src_port_id, $dst_link, $linktype);
9276dc97
ME
2419
2420 echo '</td>';
2421 } /* printlink */
2422
2423 /*
2424 * print cableID dst_port:dst_object
2425 */
fdc74cf0 2426 function _printportlink($src_port_id, $dst_port_id, &$dst_link, $back = FALSE) {
9276dc97 2427
fdc74cf0 2428 global $lm_multilink_port_types;
9276dc97 2429
fdc74cf0 2430 $multilink = MULTILINK;
9276dc97 2431
fdc74cf0 2432 if(!isset($this->list[$dst_port_id]))
2433 {
2434 /* get port not in list */
2435 // echo "<td>AHHH $src_port_id $dst_port_id --> $back</td>";
2436 // echo "<td>load".$this->var_dump_html($dst_link)." tree</td>";
2437// echo "<td>".$dst_link['cable']." ".$dst_link['name']."</td><td>not displayed</td>";
9276dc97 2438
fdc74cf0 2439 if($back)
2440 echo "<td>></td>";
2441
2442 // TODO check if multilink is needed here
2443 $this->printport($dst_link, $multilink && in_array($dst_link['type'], $lm_multilink_port_types));
2444 echo "<td>...</td>";
9276dc97 2445
fdc74cf0 2446 return TRUE;
9276dc97 2447
fdc74cf0 2448 // $this->_getportlist($this->list[$src_port_id], $back, !$back);
2449 }
9276dc97 2450
fdc74cf0 2451 $dst_port = $this->list[$dst_port_id];
2452 $object_id = $dst_port['object_id'];
2453 $obj_name = $dst_port['obj_name'];
9276dc97 2454
fdc74cf0 2455 if($obj_name == NULL)
2456 {
2457 $tmpobj = spotEntity('object', $dst_port['object_id']);
2458 $dst_port['obj_name'] = $tmpobj['dname'];
2459 $obj_name = $tmpobj['dname'];
2460 }
9276dc97 2461
fdc74cf0 2462 $loop = FALSE;
2463 $edgeport = ($dst_link == NULL) || empty($dst_port['front']) || empty($dst_port['back']);
2464
2465 if($back) {
2466 $linktype = 'back';
2467 } else {
2468 $linktype = 'front';
2469 }
9276dc97 2470
fdc74cf0 2471 $sameobject = FALSE;
2472
2473 if(isset($dst_link['loop']))
2474 $loop = TRUE;
2475
2476 if($dst_link != NULL) {
2477
2478 $src_object_id = $this->list[$src_port_id]['object_id'];
2479
2480 if(!$this->allback && $object_id == $src_object_id && $back) {
2481 $sameobject = TRUE;
9276dc97 2482 } else {
fdc74cf0 2483 $this->printlink($src_port_id, $dst_link, $linktype);
2484 }
2485
2486 } else {
2487 $this->_printlinkportsymbol($dst_port_id, $linktype);
2488 $edgeport = true;
9276dc97
ME
2489
2490 if(!$back)
fdc74cf0 2491 $this->printcomment($dst_port);
2492 }
9276dc97
ME
2493
2494 if($back) {
2495 if(!$sameobject)
2496 $this->printobject($object_id,$obj_name);
fdc74cf0 2497
9276dc97
ME
2498 echo "<td>></td>";
2499
2500 /* align ports nicely */
fdc74cf0 2501 if($dst_port['id'] == $this->port_id)
2502 echo '</td></tr></table id=printportlink1></td><td><table align=left><tr>';
9276dc97
ME
2503 }
2504
2505 /* print [portname] */
fdc74cf0 2506 // TODO check multilink symbols front/back edgeports
2507 $this->printport($dst_port, $multilink && in_array($dst_port['type'], $lm_multilink_port_types));
9276dc97
ME
2508
2509 if($loop)
2510 echo '<td bgcolor=#ff9966>LOOP</td>';
2511
2512 if(!$back) {
2513
2514 /* align ports nicely */
fdc74cf0 2515 if($dst_port['id'] == $this->port_id)
2516 echo '</td></tr></table id=printportlink2></td><td><table align=left><tr>';
9276dc97
ME
2517
2518 echo "<td><</td>";
2519 $this->printobject($object_id,$obj_name);
2520
fdc74cf0 2521 if(empty($dst_port['back']))
2522 $this->_printlinkportsymbol($dst_port_id, 'back');
9276dc97 2523 } else
fdc74cf0 2524 if(empty($dst_port['front'])) {
2525 $this->printcomment($dst_port);
2526 $this->_printlinkportsymbol($dst_port_id, 'front');
9276dc97 2527 }
fdc74cf0 2528
9276dc97 2529 if($loop) {
fdc74cf0 2530 if(isset($dst_link['loopmaxcount']))
9276dc97
ME
2531 $reason = " (MAX LOOP COUNT reached)";
2532 else
2533 $reason = '';
2534
fdc74cf0 2535 showWarning("Possible Loop on Port ($linktype) ".$dst_port['name'].$reason);
9276dc97
ME
2536 return FALSE;
2537 }
2538
2539 return TRUE;
2540
fdc74cf0 2541 } /* _printportlink */
9276dc97
ME
2542
2543 /*
2544 * print <tr>..</tr>
2545 */
fdc74cf0 2546 function printportlistrow($first = TRUE, $hl_port_id = NULL, $rowbgcolor = '#ffffff') {
2547
9276dc97
ME
2548 $this->loopcount = 0;
2549
2550 if($this->first_id == NULL)
fdc74cf0 2551 return false;
9276dc97
ME
2552
2553 if($first)
2554 $id = $this->first_id;
2555 else
2556 $id = $this->last_id;
2557
9276dc97
ME
2558 if($hl_port_id == $this->port_id)
2559 $hlbgcolor = "bgcolor=".self::HL_PORT_BGCOLOR;
2560 else
fdc74cf0 2561 $hlbgcolor = "bgcolor=$rowbgcolor";
9276dc97
ME
2562
2563 $link = NULL;
2564
2565 $port = $this->list[$id];
2566
fdc74cf0 2567 $urlparams = array(
2568 'module' => 'redirect',
2569 'page' => 'object',
2570 'tab' => 'linkmgmt',
2571 'op' => 'map',
2572 'usemap' => 1,
2573 'object_id' => $port['object_id'],
2574 );
2575
2576 if($hl_port_id !== NULL)
2577 $urlparams['hl_port_id'] = $hl_port_id;
2578 else
2579 $urlparams['port_id'] = $id;
2580
2581 $title = "linkcount: ".$this->count." (".$this->front_count."/".$this->back_count.")\nTypeID: ${port['type']}\nPortID: $id";
2582
2583 $onclick = 'onclick=window.open("'.makeHrefProcess(portlist::urlparamsarray(
2584 $urlparams)).'","Map","height=500,width=800,scrollbars=yes");';
9276dc97
ME
2585
2586 /* Current Port */
fdc74cf0 2587 echo '<tr '.$hlbgcolor.'><td nowrap="nowrap" bgcolor='.self::CURRENT_PORT_BGCOLOR.' title="'.$title.
2588 '"><a '.$onclick.'>'.
2589 $this->port['name'].': </a></td>';
9276dc97 2590
fdc74cf0 2591 echo "<td><table id=printportlistrow1 align=right><tr><td>";
2592
2593 $back = empty($this->list[$id]['back']);
2594
2595 $this->_printportlink(NULL, $id, $link, $back);
9276dc97
ME
2596
2597 $this->_printportlist($id, !$back);
fdc74cf0 2598 echo "</td></tr></table id=printportlistrow2></td></tr>";
9276dc97
ME
2599
2600 /* horizontal line */
2601 echo '<tr><td height=1 colspan=3 bgcolor=#e0e0e0></td></tr>';
2602
fdc74cf0 2603 return true;
2604
9276dc97
ME
2605 } /* printportlist */
2606
2607 /*
fdc74cf0 2608 * print <td>
9276dc97
ME
2609 * prints all ports in a list starting with start_port_id
2610 */
2611 /* !!! recursive */
2612 function _printportlist($src_port_id, $back = FALSE) {
2613
2614 if($back)
2615 $linktype = 'back';
2616 else
2617 $linktype = 'front';
2618
fdc74cf0 2619 if(!empty($this->list[$src_port_id][$linktype])) {
9276dc97 2620
fdc74cf0 2621 $linkcount = count($this->list[$src_port_id][$linktype]);
9276dc97 2622
fdc74cf0 2623 if($linkcount > 1)
2624 echo "<td bgcolor=#f00000></td><td><table id=_printportlist1>";
9276dc97 2625
fdc74cf0 2626 $lastkey = $linkcount - 1;
9276dc97 2627
fdc74cf0 2628 foreach($this->list[$src_port_id][$linktype] as $key => &$link) {
2629
2630 if($linkcount > 1) {
2631 echo "<tr style=\"background-color:".( $key % 2 ? self::ALTERNATE_ROW_BGCOLOR : "#ffffff" )."\"><td><table id=_printportlist2><tr>";
2632 }
2633
2634 $dst_port_id = $link['id'];
2635
2636 $this->loopcount++;
2637
2638 if($this->loopcount > self::MAX_LOOP_COUNT) {
2639 // $src_port_name = $this->list[$src_port_id]['name'];
2640 // $dst_port_name = $this->list[$dst_port_id]['name'];
9276dc97 2641
fdc74cf0 2642 $link['loop'] = $dst_port_id;
2643 $link['loopmaxcount'] = $dst_port_id;
2644
2645 /* loop warning is handeld in _printportlink() */
2646 //showWarning("MAX LOOP COUNT reached $src_port_name -> $dst_port_name".self::MAX_LOOP_COUNT);
2647 //return; /* return after _printportlink */
2648 }
2649
2650 if(!$this->_printportlink($src_port_id, $dst_port_id, $link, $back))
2651 {
9276dc97 2652 return;
fdc74cf0 2653 }
9276dc97 2654
fdc74cf0 2655 $this->_printportlist($dst_port_id,!$back);
9276dc97 2656
fdc74cf0 2657 if($linkcount > 1) {
2658 echo "</tr></table></td></tr>"
2659 .( $key != $lastkey ? "<tr><td height=1 colspan=100% bgcolor=#c0c0c0><td></tr>" : "");
2660 }
2661 }
9276dc97 2662
fdc74cf0 2663 if($linkcount > 1)
2664 echo "</table></td>";
2665 }
9276dc97
ME
2666 } /* _printportlist */
2667
fdc74cf0 2668 /*
9276dc97
ME
2669 * returns linked Row / Rack Info for object_id
2670 *
2671 */
2672 function _getRackInfo($object_id, $style = '') {
2673 global $lm_cache;
2674
2675 $rackinfocache = $lm_cache['rackinfo'];
2676
2677 /* if not in cache get it */
2678 if(!array_key_exists($object_id,$rackinfocache)) {
2679
fdc74cf0 2680 $object = spotEntity('object', $object_id);
2681 $rack_id = $object['rack_id'];
9276dc97 2682
fdc74cf0 2683 if($rack_id)
2684 $rackinfocache[$object_id] = spotEntity('rack', $object['rack_id']);
9276dc97
ME
2685 }
2686
9276dc97 2687
fdc74cf0 2688 $rack = &$rackinfocache[$object_id];
2689
2690 if(empty($rack))
9276dc97
ME
2691 return '<span style="'.$style.'">Unmounted</span>';
2692 else
fdc74cf0 2693 return '<a style="'.$style.'" href='.makeHref(array('page'=>'row', 'row_id'=>$rack['row_id'])).'>'.$rack['row_name']
2694 .'</a>/<a style="'.$style.'" href='.makeHref(array('page'=>'rack', 'rack_id'=>$rack['id'])).'>'
2695 .$rack['name'].'</a>';
9276dc97
ME
2696
2697 } /* _getRackInfo */
2698
fdc74cf0 2699 /*
2700 * return link symbol
2701 */
2702 function _getlinkportsymbol($port_id, $linktype) {
2703 $retval = '<span onclick=window.open("'.makeHrefProcess(portlist::urlparamsarray(
2704 array('op' => 'PortLinkDialog','port' => $port_id,'linktype' => $linktype ))).'","name","height=800,width=800");'
2705 .'>';
2706
2707 $img = getImageHREF ('plug', $linktype.' Link this port');
2708
2709 if($linktype == 'back')
2710 $img = str_replace('<img',
2711 '<img style="transform:rotate(180deg);-o-transform:rotate(180deg);-ms-transform:rotate(180deg);-moz-transform:rotate(180deg);-webkit-transform:rotate(180deg);"',
2712 $img);
2713
2714 $retval .= $img;
2715 $retval .= "</span>";
2716 return $retval;
2717
2718 } /* _getlinkportsymbol */
9276dc97 2719
fdc74cf0 2720 /*
2721 * print link symbol
9276dc97
ME
2722 *
2723 */
fdc74cf0 2724 function _printlinkportsymbol($port_id, $linktype = 'front') {
9276dc97
ME
2725 global $lm_cache;
2726
2727 if(!$lm_cache['allowlink'])
2728 return;
9276dc97
ME
2729
2730 echo "<td align=center>";
2731
fdc74cf0 2732 echo $this->_getlinkportsymbol($port_id, $linktype);
9276dc97
ME
2733
2734 echo "</td>";
2735
fdc74cf0 2736 } /* _printlinkportsymbol */
9276dc97 2737
fdc74cf0 2738 /*
2739 * return link cut symbol
9276dc97
ME
2740 *
2741 * TODO $opspec_list
2742 */
fdc74cf0 2743 function _printUnLinkPort($src_port_id, &$dst_link, $linktype) {
9276dc97
ME
2744 global $lm_cache;
2745
2746 if(!$lm_cache['allowlink'])
2747 return '';
2748
fdc74cf0 2749 $src_port = $this->list[$src_port_id];
9276dc97 2750
fdc74cf0 2751 $dst_port = $this->list[$dst_link['id']];
9276dc97
ME
2752
2753 /* use RT unlink for front link, linkmgmt unlink for back links */
2754 if($linktype == 'back')
2755 $tab = 'linkmgmt';
2756 else
2757 $tab = 'ports';
9276dc97 2758
fdc74cf0 2759 return '<a href='.
9276dc97
ME
2760 makeHrefProcess(array(
2761 'op'=>'unlinkPort',
fdc74cf0 2762 'port_id'=>$src_port_id,
2763 'remote_id' => $dst_port['id'],
9276dc97
ME
2764 'object_id'=>$this->object_id,
2765 'tab' => $tab,
2766 'linktype' => $linktype)).
2767 ' onclick="return confirm(\'unlink ports '.$src_port['name']. ' -> '.$dst_port['name']
fdc74cf0 2768 .' ('.$linktype.') with cable ID: '.$dst_link['cable'].'?\');">'.
9276dc97
ME
2769 getImageHREF ('cut', $linktype.' Unlink this port').'</a>';
2770
2771 } /* _printUnLinkPort */
2772
2773
2774 /*
fdc74cf0 2775 *
9276dc97 2776 */
fdc74cf0 2777 static function urlparams($name, $value, $defaultvalue = NULL) {
9276dc97
ME
2778
2779 $urlparams = $_GET;
2780
2781 if($value == $defaultvalue) {
2782
fdc74cf0 2783 /* remove param */
2784 unset($urlparams[$name]);
9276dc97 2785
fdc74cf0 2786 } else {
9276dc97 2787
fdc74cf0 2788 $urlparams[$name] = $value;
9276dc97
ME
2789
2790 }
2791
2792 return $urlparams;
2793
2794 } /* urlparams */
2795
2796 /*
2797 * $params = array('name' => 'value', ...)
2798 */
fdc74cf0 2799 static function urlparamsarray($params) {
9276dc97
ME
2800
2801 $urlparams = $_GET;
2802
2803 foreach($params as $name => $value) {
2804
2805 if($value == NULL) {
2806
fdc74cf0 2807 /* remove param */
2808 unset($urlparams[$name]);
9276dc97 2809
fdc74cf0 2810 } else {
9276dc97 2811
fdc74cf0 2812 $urlparams[$name] = $value;
9276dc97 2813
fdc74cf0 2814 }
9276dc97
ME
2815 }
2816
2817 return $urlparams;
2818
2819 } /* urlparamsarray */
2820
2821 /* */
fdc74cf0 2822 static function hasbackend($object_id) {
9276dc97
ME
2823 /* sql bitwise xor: porta ^ portb */
2824 //select cable, ((porta ^ portb) ^ 4556) as port from Link where (4556 in (porta, portb));
2825
fdc74cf0 2826 $result = usePreparedSelectBlade
2827 (
9276dc97
ME
2828 'SELECT count(*) from Port
2829 join LinkBackend on (porta = id or portb = id )
2830 where object_id = ?',
2831 array($object_id)
fdc74cf0 2832 );
2833 $retval = $result->fetchColumn();
2834
2835 $result->closeCursor();
2836
2837 return $retval != 0;
9276dc97
ME
2838
2839 } /* hasbackend */
2840
2841 /* for debugging only */
2842 function var_dump_html(&$var) {
2843 echo "<pre>------------------Start Var Dump -------------------------\n";
2844 var_dump($var);
2845 echo "\n---------------------END Var Dump ------------------------</pre>";
2846 }
2847
2848} /* portlist */
2849
2850/* -------------------------------------------------- */
2851
2852?>