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