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