template_show.php 15 KB
Newer Older
1 2
<?php
#
3
# Copyright (c) 2006-2011 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
23 24
#
include("defs.php3");
25
include_once("template_defs.php");
26 27 28
require("Sajax.php");
sajax_init();
sajax_export("Show", "GraphChange");
29 30

#
31
# Only known and logged in users ...
32
#
33 34 35
$this_user = CheckLoginOrDie();
$uid       = $this_user->uid();
$isadmin   = ISADMIN();
36

37 38 39 40 41 42 43
#
# Verify page arguments 
#
$reqargs = RequiredPageArguments("template", PAGEARG_TEMPLATE);
$optargs = OptionalPageArguments("action",   PAGEARG_STRING,
				 "show",     PAGEARG_STRING,
				 "confirmed",PAGEARG_STRING);
44

45 46
if (! ($experiment = $template->GetExperiment())) {
    TBERROR("Could not find experiment object for template!", 1);
47 48
}

49 50 51 52 53
# Need these below
$guid = $template->guid();
$vers = $template->vers();
$pid  = $template->pid();
$eid  = $template->eid();
54
$tid  = $template->tid();
55 56 57
$exptidx  = $experiment->idx();
$unix_gid = $experiment->UnixGID();
$this_url = CreateURL("template_show", $template);
58 59
$project  = $template->GetProject();
$unix_pid = $project->unix_gid();
60 61 62 63 64 65 66 67

#
# Verify Permission.
#
if (! $template->AccessCheck($this_user, $TB_EXPT_READINFO)) {
    USERERROR("You do not have permission to view experiment ".
	      "template $guid/$vers", 1);
}
68

69 70 71 72 73 74
#
# For the Sajax Interface
#
function Show($which, $zoom, $detail)
{
    global $pid, $eid, $uid, $TBSUEXEC_PATH, $TBADMINGROUP;
75
    global $template, $isadmin;
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
    $html = "";

    if ($which == "vis") {
	if ($zoom == 0) {
            # Default is whatever we have; to avoid regen of the image.
	    list ($zoom, $detail) = $template->CurrentVisDetails();	    
	}
	else {
            # Sanity check but lets not worry about throwing an error.
	    if (!TBvalid_float($zoom))
		$zoom = 1.25;
	    if (!TBvalid_integer($detail))
		$detail = 1;
    	}

	ob_start();
	$template->ShowVis($zoom, $detail);
	$html = ob_get_contents();
	ob_end_clean();

	$zoomout = sprintf("%.2f", $zoom / 1.25);
	$zoomin  = sprintf("%.2f", $zoom * 1.25);

	$html .= "<button name=viszoomout type=button value=$zoomout";
	$html .= " onclick=\"VisChange('$zoomout', $detail);\">";
	$html .= "Zoom Out</button>\n";
	$html .= "<button name=viszoomin type=button value=$zoomin";
	$html .= " onclick=\"VisChange('$zoomin', $detail);\">";
	$html .= "Zoom In</button>\n";

	if ($detail) {
	    $html .= "<button name=hidedetail type=button value=0";
	    $html .= " onclick=\"VisChange('$zoom', 0);\">";
	    $html .= "Hide Details</button>\n";
	}
	else {
	    $html .= "<button name=showdetail type=button value=1";
	    $html .= " onclick=\"VisChange('$zoom', 1);\">";
	    $html .= "Show Details</button>\n";
	}
    }
    elseif ($which == "graph") {
	ob_start();
	$template->ShowGraph();
	$html = ob_get_contents();
	ob_end_clean();

	if (! $template->IsRoot()) {
	    if ($template->IsHidden()) {
		$html .= "<button name=showtemplate type=button value=Show";
		$html .= " onclick=\"GraphChange('showtemplate');\">";
		$html .= "Show Template</button>&nbsp";
	    }
	    else {
		$html .= "<button name=hidetemplate type=button value=Hide";
		$html .= " onclick=\"GraphChange('hidetemplate');\">";
		$html .= "Hide Template</button>&nbsp";
	    }
134
	    $html .= "<input id=showexp_recursive type=checkbox value=Yep> ";
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
	    $html .= "Recursive? &nbsp &nbsp &nbsp &nbsp ";
	}
	$root = Template::LookupRoot($template->guid());

        # We overload the hidden bit on the root.
	if ($root->IsHidden()) {
	    $html .= "<button name=showhidden type=button value=showhidden";
	    $html .= " onclick=\"GraphChange('showhidden');\">";
	    $html .= "Show Hidden Templates</button>&nbsp &nbsp &nbsp &nbsp ";
	}
	else {
	    $html .= "<button name=hidehidden type=button value=hidehidden";
	    $html .= " onclick=\"GraphChange('hidehidden');\"> ";
	    $html .= "Hide Hidden Templates</button>&nbsp &nbsp &nbsp &nbsp ";
	}
	$html .= "<button name=zoomout type=button value=out";
	$html .= " onclick=\"GraphChange('zoomout');\">Zoom Out</button>\n";
	$html .= "<button name=zoomin type=button value=in";
	$html .= " onclick=\"GraphChange('zoomin');\">Zoom In</button>\n";
154 155 156 157 158 159 160 161 162 163

	# A delete button with a confirm box right there.
	if ($isadmin) {
	    $html .= "<br><br>\n";
	    $html .= "<button name=deletetemplate type=button value=Delete";
	    $html .= " onclick=\"DeleteTemplate();\">";
	    $html .= "<font color=red>Delete</font></button>&nbsp";	
	    $html .= "<input id=confirm_delete type=checkbox value=Yep> ";
	    $html .= "Confirm";
	}
164 165 166 167 168 169 170 171 172 173
    }
    elseif ($which == "nsfile") {
	$nsdata = "";

	$input_list = $template->InputFiles();

	for ($i = 0; $i < count($input_list); $i++) {
	    $nsdata .= htmlentities($input_list[$i]);
	    $nsdata .= "\n\n";
	}
174 175
	$html = "<pre><div align=left class=\"showexp_codeblock\">".
	    "$nsdata</div></pre>\n";
176 177 178 179 180

	$html .= "<button name=savens type=button value=1";
	$html .= " onclick=\"SaveNS();\">";
	$html .= "Save</button>\n";
    }
181
    
182 183 184 185 186 187
    return $html;
}

#
# Sajax callback for operating on the template graph.
#
188
function GraphChange($action, $recursive = 0, $no_output = 0)
189
{
190
    global $pid, $unix_gid, $eid, $uid, $guid, $TBSUEXEC_PATH, $TBADMINGROUP;
191
    global $template, $unix_pid;
192
    $html = "";
193

194
    $reqarg  = "-a ";
Leigh Stoller's avatar
Leigh Stoller committed
195
    $versarg = $template->vers();
196

197 198 199 200 201 202 203 204 205
    if ($action == "zoomout" || $action == "zoomin") {
	$optarg = "";
	
	if ($action == "zoomin") {
	    $optarg = "-z in";
	}
	else {
	    $optarg = "-z out";
	}
206

207
        # Need to update the template graph.
208
	SUEXEC($uid, "$unix_pid,$unix_gid", "webtemplate_graph $optarg $guid",
209 210 211
	       SUEXEC_ACTION_DIE);
    }
    else {
212 213
	$optarg  = ($recursive ? "-r" : "");
	
Leigh Stoller's avatar
Leigh Stoller committed
214
	if ($action == "showtemplate") {
215
	    $reqarg .= "show";
216
	}
Leigh Stoller's avatar
Leigh Stoller committed
217
	elseif ($action == "hidetemplate") {
218
	    $reqarg .= "hide";
219
	}
Leigh Stoller's avatar
Leigh Stoller committed
220
	elseif ($action == "showhidden") {
221
	    $reqarg .= "showhidden";
Leigh Stoller's avatar
Leigh Stoller committed
222 223
	    # Applies only to root template
	    $versarg = "1";
224
	}
Leigh Stoller's avatar
Leigh Stoller committed
225
	elseif ($action == "hidehidden") {
226
	    $reqarg .= "hidehidden";
Leigh Stoller's avatar
Leigh Stoller committed
227 228
	    # Applies only to root template
	    $versarg = "1";
229
	}
Leigh Stoller's avatar
Leigh Stoller committed
230 231 232 233 234
	elseif ($action == "activate") {
	    $reqarg .= "activate";
	}
	elseif ($action == "inactivate") {
	    $reqarg .= "inactivate";
235 236
	}
	else {
Leigh Stoller's avatar
Leigh Stoller committed
237 238
	    PAGEARGERROR("Invalid action $action");
	    return;
239
	}
Leigh Stoller's avatar
Leigh Stoller committed
240
	$reqarg .= " $guid/$versarg";
241
	
242
	SUEXEC($uid, "$unix_pid,$unix_gid",
243 244 245
	       "webtemplate_control $reqarg $optarg",
	       SUEXEC_ACTION_DIE);
    }
Leigh Stoller's avatar
Leigh Stoller committed
246
    $template->Refresh();
247 248 249 250 251 252

    $html = "";
    if (! $no_output)
	$html = Show("graph", 0, 0);
    
    return $html;
253 254
}

255 256 257 258 259 260
#
# See if this request is to the above function. Does not return
# if it is. Otherwise return and continue on.
#
sajax_handle_client_request();

Leigh Stoller's avatar
Leigh Stoller committed
261 262 263 264
#
# Active/Inactive is a plain menu link.
#
if (isset($action) && ($action == "activate" || $action == "inactivate")) {
265
    GraphChange($action, 0, 1);
Leigh Stoller's avatar
Leigh Stoller committed
266 267
}

268 269 270 271
# Delete is just plain special!
if (isset($action) && $action == "deletetemplate" &&
    isset($confirmed) && $confirmed == "yep") {

272 273
    PAGEHEADER("Delete Template: $guid/$vers");
    STARTBUSY("Deleting template $guid/$vers recursively");
274 275

    # Pass recursive option all the time.
276
    $retval = SUEXEC($uid, "$unix_pid,$unix_gid",
277
		     "webtemplate_delete -r $guid/$vers",
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
		     SUEXEC_ACTION_IGNORE);

    CLEARBUSY();
    
    #
    # Fatal Error. Report to the user, even though there is not much he can
    # do with the error. Also reports to tbops.
    # 
    if ($retval < 0) {
	SUEXECERROR(SUEXEC_ACTION_CONTINUE);
    }

    # User error. Tell user and exit.
    if ($retval) {
	SUEXECERROR(SUEXEC_ACTION_USERERROR);
	PAGEFOOTER();
	return;
    }
    #
    # Okay, lets zap back to the root, unless this was the root.
    #
    if ($template->IsRoot()) {
300
	PAGEREPLACE(CreateURL("showuser", $this_user));
301 302 303 304 305 306 307
    }
    else {
	PAGEREPLACE("template_show.php?guid=$guid&version=1");
    }
    return;
}

308 309 310
#
# Standard Testbed Header after argument checking.
#
311
PAGEHEADER("Template $tid ($guid/$vers)");
312 313

SUBPAGESTART();
314

315 316
SUBMENUSTART("Template Options");

Leigh Stoller's avatar
Leigh Stoller committed
317 318
if ($template->IsActive()) {
    WRITESUBMENUBUTTON("InActivate Template &nbsp &nbsp",
319
		       "${this_url}&action=inactivate");
Leigh Stoller's avatar
Leigh Stoller committed
320 321
}
else {
Leigh Stoller's avatar
Leigh Stoller committed
322
    WRITESUBMENUBUTTON("Activate Template &nbsp &nbsp",
323
		       "${this_url}&action=activate");
Leigh Stoller's avatar
Leigh Stoller committed
324
}
325 326

WRITESUBMENUBUTTON("Modify Template",
327
		   CreateURL("template_modify", $template));
328 329

WRITESUBMENUBUTTON("Instantiate Template",
330
		   CreateURL("template_swapin", $template));
331

332 333
WRITESUBMENUBUTTON("Create New Template", CreateURL("template_create"));

334
WRITESUBMENUBUTTON("Add Metadata",
335
		   CreateURL("template_metadata", $template) . "&action=add");
336

337 338
if ($template->EventCount() > 0) {
    WRITESUBMENUBUTTON("Edit Template Events",
339
		       CreateURL("template_editevents", $template));
340 341
}

342 343 344
WRITESUBMENUBUTTON("Search Template",
		   CreateURL("template_search", $template));

345 346 347 348
# We show the user the datastore for the template;
# the rest of it is not important.
WRITESUBMENUBUTTON("Browse Datastore",
		   CreateURL("archive_view", $template));
349

350 351 352
WRITESUBMENUBUTTON("Browse CVS Repository",
		   CreateURL("cvswebwrap", $template));

353 354
WRITESUBMENUBUTTON("View Records",
		   CreateURL("template_history", $template));
355 356 357 358 359 360

SUBMENUEND_2A();

#
# Ick.
#
361 362
if (($stats = $experiment->GetStats())) {
    $rsrcidx = $stats->rsrcidx();
363

364 365 366 367
    echo "<br>
          <img border=1 alt='template visualization'
               src='showthumb.php3?idx=$rsrcidx'>";
}
368

369 370
if ($template->InstanceCount()) {
    $template->ShowInstances();
371 372
}

373 374
SUBMENUEND_2B();

375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
# See below; for getting the tab correct at the first page load.
if (!isset($show)) {
    $show = "vis";
}
if ($show == "vis") {
    $li_current = "li_vis";
    $init_show  = Show("vis", 0, 0);
}
elseif ($show == "nsfile") {
    $li_current = "li_nsfile";
    $init_show  = Show("nsfile", 0, 0);
}
elseif ($show == "graph") {
    $li_current = "li_graph";
    $init_show  = Show("graph", 0, 0);
}

Leigh Stoller's avatar
Leigh Stoller committed
392 393 394
#
# The center area is a form that can show NS file, Template Graph, or Vis.
#
395
echo "<script type='text/javascript' src='template_sup.js'></script>\n";
Leigh Stoller's avatar
Leigh Stoller committed
396
echo "<script type='text/javascript' language='javascript'>
397
        var li_current = '$li_current';
Leigh Stoller's avatar
Leigh Stoller committed
398
        function Show(which) {
399 400 401 402 403 404 405 406 407 408
	    li = getObjbyName(li_current);
            li.style.backgroundColor = '#DDE';
            li.style.borderBottom = 'none';

            li_current = 'li_' + which;
	    li = getObjbyName(li_current);
            li.style.backgroundColor = 'white';
            li.style.borderBottom = '1px solid white';

            x_Show(which, 0, 0, Show_cb);
Leigh Stoller's avatar
Leigh Stoller committed
409 410
            return false;
        }
411 412 413 414 415 416
        function Show_cb(html) {
	    visarea = getObjbyName('showexp_visarea');
            if (visarea) {
                visarea.innerHTML = html;
            }
        }
417 418 419 420 421
        function Setup() {
	    li = getObjbyName(li_current);
            li.style.backgroundColor = 'white';
            li.style.borderBottom = '1px solid white';
        }
422 423 424 425 426 427
        function ShowVisInit() {
            ADD_DHTML(\"myvisdiv\");
        }
        function ShowGraphInit() {
 	    ADD_DHTML(\"mygraphdiv\");
  	    SetActiveTemplate(\"mygraphimg\", \"CurrentTemplate\", 
428
			      \"Tarea${vers}\");
429
            tt_Init();
430 431 432
        }
        function VisChange(zoom, detail) {
            x_Show('vis', zoom, detail, Show_cb);
Leigh Stoller's avatar
Leigh Stoller committed
433 434
            return false;
        }
435 436 437 438 439 440 441 442 443 444
        function DeleteTemplate() {
            confirm_flag = 0;
            confirm_box  = getObjbyName('confirm_delete');

	    if (confirm_box) {
                confirm_flag = ((confirm_box.checked == true) ? 1 : 0);
            }
            if (confirm_flag == 0) {
                return false;
            }
445 446
	    window.location.replace('$this_url" .
                  "&action=deletetemplate&confirmed=yep');
447 448
            return false;
        }
449
        function GraphChange(action) {
450 451 452 453 454 455 456 457
            recursive_flag = 0;

	    recursive = getObjbyName('showexp_recursive');
            if (recursive) {
                recursive_flag = ((recursive.checked == true) ? 1 : 0);
            }

            x_GraphChange(action, recursive_flag, Show_cb);
Leigh Stoller's avatar
Leigh Stoller committed
458 459
            return false;
        }
460
        function SaveNS() {
461
            window.open('" . CreateURL("spitnsdata", $template) . "',
462 463 464 465 466 467 468
                        'Save NS File','width=650,height=400,toolbar=no,".
                        "resizeable=yes,scrollbars=yes,status=yes,".
	                "menubar=yes');
        }\n\n";
sajax_show_javascript();
echo "</script>\n";
echo "<script type='text/javascript' src='js/wz_dragdrop.js'></script>";
469

470 471 472 473
#
# This has to happen for dragdrop to work.
#
$bodyclosestring = "<script type='text/javascript'>SET_DHTML();</script>\n";
Leigh Stoller's avatar
Leigh Stoller committed
474

475 476 477 478 479 480
#
# This is the topbar
#
echo "<div width=\"100%\" align=center>\n";
echo "<ul id=\"topnavbar\">\n";
echo "<li>
481
          <a href=\"#A\" " .
482
               "class=topnavbar onfocus=\"this.hideFocus=true;\" ".
483
               "id=\"li_vis\" onclick=\"Show('vis');\">".
484
               "Topology</a></li>\n";
485
echo "<li>
486
          <a href=\"#B\" " .
487
               "class=topnavbar onfocus=\"this.hideFocus=true;\" ".
488 489
               "id=\"li_nsfile\" onclick=\"Show('nsfile');\">".
               "NS File</a></li>\n";
490
echo "<li>
491
          <a href=\"#C\" " .
492
               "class=topnavbar onfocus=\"this.hideFocus=true;\" ".
493 494
               "id=\"li_graph\" onclick=\"Show('graph');\">".
               "History</a></li>\n";
495
echo "</ul>\n";
496 497
echo "</div>\n";
echo "<div align=center id=topnavbarbottom>&nbsp</div>\n";
Leigh Stoller's avatar
Leigh Stoller committed
498

499
#
500
# Start out with  ...
501 502
#
echo "<div align=center width=\"100%\" id=\"showexp_visarea\">\n";
503
echo $init_show;
504
echo "</div>\n";
505

506 507
SUBPAGEEND();

508 509
$paramcount = $template->ParameterCount();
$metacount  = $template->MetadataCount();
510 511 512 513 514 515 516
$rowspan    = (($paramcount && $metacount) ? 2 : 1);

echo "<center>\n";
echo "<table border=0 bgcolor=#000 color=#000 class=stealth ".
     " cellpadding=0 cellspacing=0>\n";
echo "<tr valign=top><td rowspan=$rowspan class=stealth align=center>\n";

517
$template->Show();
518 519 520 521 522 523 524 525

echo "</td>\n";

if ($paramcount || $metacount) {
    echo "<td align=center class=stealth> &nbsp &nbsp &nbsp </td>\n";
    echo "<td align=center class=stealth> \n";
    
    if ($paramcount && $metacount) {
526
	$template->ShowParameters();
527 528 529 530 531
	echo "</td>\n";
	echo "</tr>\n";
	echo "<tr valign=top>";
	echo "<td align=center class=stealth> &nbsp &nbsp &nbsp </td>\n";
	echo "<td class=stealth align=center>\n";
532
	$template->ShowMetadata();
533 534
    }
    elseif ($paramcount) {
535
	$template->ShowParameters();
536 537
    }
    else {
538
	$template->ShowMetadata();
539 540
    }
    echo "</td>\n";
541 542
}

543 544 545
echo "</tr>\n";
echo "</table>\n";
echo "</center>\n";
546

547 548 549 550 551 552 553
#
# Get the active tab to look right.
#
echo "<script type='text/javascript' language='javascript'>
      Setup();
      </script>\n";

554 555 556 557 558
#
# Standard Testbed Footer
# 
PAGEFOOTER();
?>