template_show.php 13.9 KB
Newer Older
1 2 3
<?php
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2006, 2007 University of Utah and the Flux Group.
5 6 7
# All rights reserved.
#
include("defs.php3");
8
include_once("template_defs.php");
9 10 11
require("Sajax.php");
sajax_init();
sajax_export("Show", "GraphChange");
12 13

#
14
# Only known and logged in users ...
15
#
16 17 18
$this_user = CheckLoginOrDie();
$uid       = $this_user->uid();
$isadmin   = ISADMIN();
19

20 21 22 23 24 25 26
#
# Verify page arguments 
#
$reqargs = RequiredPageArguments("template", PAGEARG_TEMPLATE);
$optargs = OptionalPageArguments("action",   PAGEARG_STRING,
				 "show",     PAGEARG_STRING,
				 "confirmed",PAGEARG_STRING);
27

28 29
if (! ($experiment = $template->GetExperiment())) {
    TBERROR("Could not find experiment object for template!", 1);
30 31
}

32 33 34 35 36
# Need these below
$guid = $template->guid();
$vers = $template->vers();
$pid  = $template->pid();
$eid  = $template->eid();
37
$tid  = $template->tid();
38 39 40 41 42 43 44 45 46 47 48
$exptidx  = $experiment->idx();
$unix_gid = $experiment->UnixGID();
$this_url = CreateURL("template_show", $template);

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

50 51 52 53 54 55
#
# For the Sajax Interface
#
function Show($which, $zoom, $detail)
{
    global $pid, $eid, $uid, $TBSUEXEC_PATH, $TBADMINGROUP;
56
    global $template, $isadmin;
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 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
    $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";
	    }
115
	    $html .= "<input id=showexp_recursive type=checkbox value=Yep> ";
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
	    $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";
135 136 137 138 139 140 141 142 143 144

	# 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";
	}
145 146 147 148 149 150 151 152 153 154
    }
    elseif ($which == "nsfile") {
	$nsdata = "";

	$input_list = $template->InputFiles();

	for ($i = 0; $i < count($input_list); $i++) {
	    $nsdata .= htmlentities($input_list[$i]);
	    $nsdata .= "\n\n";
	}
155 156
	$html = "<pre><div align=left class=\"showexp_codeblock\">".
	    "$nsdata</div></pre>\n";
157 158 159 160 161

	$html .= "<button name=savens type=button value=1";
	$html .= " onclick=\"SaveNS();\">";
	$html .= "Save</button>\n";
    }
162
    
163 164 165 166 167 168
    return $html;
}

#
# Sajax callback for operating on the template graph.
#
169
function GraphChange($action, $recursive = 0, $no_output = 0)
170
{
171
    global $pid, $unix_gid, $eid, $uid, $guid, $TBSUEXEC_PATH, $TBADMINGROUP;
172 173
    global $template;
    $html = "";
174

175
    $reqarg  = "-a ";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
176
    $versarg = $template->vers();
177

178 179 180 181 182 183 184 185 186
    if ($action == "zoomout" || $action == "zoomin") {
	$optarg = "";
	
	if ($action == "zoomin") {
	    $optarg = "-z in";
	}
	else {
	    $optarg = "-z out";
	}
187

188 189 190 191 192
        # Need to update the template graph.
	SUEXEC($uid, "$pid,$unix_gid", "webtemplate_graph $optarg $guid",
	       SUEXEC_ACTION_DIE);
    }
    else {
193 194
	$optarg  = ($recursive ? "-r" : "");
	
Leigh B. Stoller's avatar
Leigh B. Stoller committed
195
	if ($action == "showtemplate") {
196
	    $reqarg .= "show";
197
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
198
	elseif ($action == "hidetemplate") {
199
	    $reqarg .= "hide";
200
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
201
	elseif ($action == "showhidden") {
202
	    $reqarg .= "showhidden";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
203 204
	    # Applies only to root template
	    $versarg = "1";
205
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
206
	elseif ($action == "hidehidden") {
207
	    $reqarg .= "hidehidden";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
208 209
	    # Applies only to root template
	    $versarg = "1";
210
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
211 212 213 214 215
	elseif ($action == "activate") {
	    $reqarg .= "activate";
	}
	elseif ($action == "inactivate") {
	    $reqarg .= "inactivate";
216 217
	}
	else {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
218 219
	    PAGEARGERROR("Invalid action $action");
	    return;
220
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
221
	$reqarg .= " $guid/$versarg";
222 223 224 225 226
	
	SUEXEC($uid, "$pid,$unix_gid",
	       "webtemplate_control $reqarg $optarg",
	       SUEXEC_ACTION_DIE);
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
227
    $template->Refresh();
228 229 230 231 232 233

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

236 237 238 239 240 241
#
# 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 B. Stoller's avatar
Leigh B. Stoller committed
242 243 244 245
#
# Active/Inactive is a plain menu link.
#
if (isset($action) && ($action == "activate" || $action == "inactivate")) {
246
    GraphChange($action, 0, 1);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
247 248
}

249 250 251 252
# Delete is just plain special!
if (isset($action) && $action == "deletetemplate" &&
    isset($confirmed) && $confirmed == "yep") {

253 254
    PAGEHEADER("Delete Template: $guid/$vers");
    STARTBUSY("Deleting template $guid/$vers recursively");
255 256 257

    # Pass recursive option all the time.
    $retval = SUEXEC($uid, "$pid,$unix_gid",
258
		     "webtemplate_delete -r $guid/$vers",
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
		     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()) {
281
	PAGEREPLACE(CreateURL("showuser", $this_user));
282 283 284 285 286 287 288
    }
    else {
	PAGEREPLACE("template_show.php?guid=$guid&version=1");
    }
    return;
}

289 290 291
#
# Standard Testbed Header after argument checking.
#
292
PAGEHEADER("Template $tid ($guid/$vers)");
293 294

SUBPAGESTART();
295

296 297
SUBMENUSTART("Template Options");

Leigh B. Stoller's avatar
Leigh B. Stoller committed
298 299
if ($template->IsActive()) {
    WRITESUBMENUBUTTON("InActivate Template &nbsp &nbsp",
300
		       "${this_url}&action=inactivate");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
301 302
}
else {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
303
    WRITESUBMENUBUTTON("Activate Template &nbsp &nbsp",
304
		       "${this_url}&action=activate");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
305
}
306 307

WRITESUBMENUBUTTON("Modify Template",
308
		   CreateURL("template_modify", $template));
309 310

WRITESUBMENUBUTTON("Instantiate Template",
311
		   CreateURL("template_swapin", $template));
312

313 314
WRITESUBMENUBUTTON("Create New Template", CreateURL("template_create"));

315
WRITESUBMENUBUTTON("Add Metadata",
316
		   CreateURL("template_metadata", $template) . "&action=add");
317

318 319
if ($template->EventCount() > 0) {
    WRITESUBMENUBUTTON("Edit Template Events",
320
		       CreateURL("template_editevents", $template));
321 322
}

323 324 325
WRITESUBMENUBUTTON("Search Template",
		   CreateURL("template_search", $template));

326 327 328 329
# We show the user the datastore for the template;
# the rest of it is not important.
WRITESUBMENUBUTTON("Browse Datastore",
		   CreateURL("archive_view", $template));
Leigh B. Stoller's avatar
Leigh B. Stoller committed
330

331 332 333
WRITESUBMENUBUTTON("Browse CVS Repository",
		   CreateURL("cvswebwrap", $template));

334 335
WRITESUBMENUBUTTON("View Records",
		   CreateURL("template_history", $template));
336 337 338 339 340 341

SUBMENUEND_2A();

#
# Ick.
#
342 343
if (($stats = $experiment->GetStats())) {
    $rsrcidx = $stats->rsrcidx();
344

345 346 347 348
    echo "<br>
          <img border=1 alt='template visualization'
               src='showthumb.php3?idx=$rsrcidx'>";
}
349

350 351
if ($template->InstanceCount()) {
    $template->ShowInstances();
352 353
}

354 355
SUBMENUEND_2B();

356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
# 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 B. Stoller's avatar
Leigh B. Stoller committed
373 374 375
#
# The center area is a form that can show NS file, Template Graph, or Vis.
#
376
echo "<script type='text/javascript' src='template_sup.js'></script>\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
377
echo "<script type='text/javascript' language='javascript'>
378
        var li_current = '$li_current';
Leigh B. Stoller's avatar
Leigh B. Stoller committed
379
        function Show(which) {
380 381 382 383 384 385 386 387 388 389
	    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 B. Stoller's avatar
Leigh B. Stoller committed
390 391
            return false;
        }
392 393 394 395 396 397 398 399 400 401 402 403
        function Show_cb(html) {
	    visarea = getObjbyName('showexp_visarea');
            if (visarea) {
                visarea.innerHTML = html;
            }
        }
        function ShowVisInit() {
            ADD_DHTML(\"myvisdiv\");
        }
        function ShowGraphInit() {
 	    ADD_DHTML(\"mygraphdiv\");
  	    SetActiveTemplate(\"mygraphimg\", \"CurrentTemplate\", 
404
			      \"Tarea${vers}\");
405
            tt_Init();
406 407 408
        }
        function VisChange(zoom, detail) {
            x_Show('vis', zoom, detail, Show_cb);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
409 410
            return false;
        }
411 412 413 414 415 416 417 418 419 420
        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;
            }
421 422
	    window.location.replace('$this_url" .
                  "&action=deletetemplate&confirmed=yep');
423 424
            return false;
        }
425
        function GraphChange(action) {
426 427 428 429 430 431 432 433
            recursive_flag = 0;

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

            x_GraphChange(action, recursive_flag, Show_cb);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
434 435
            return false;
        }
436
        function SaveNS() {
437
            window.open('" . CreateURL("spitnsdata", $template) . "',
438 439 440 441 442 443 444
                        '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>";
445

446 447 448 449
#
# This has to happen for dragdrop to work.
#
$bodyclosestring = "<script type='text/javascript'>SET_DHTML();</script>\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
450

451 452 453 454 455 456
#
# This is the topbar
#
echo "<div width=\"100%\" align=center>\n";
echo "<ul id=\"topnavbar\">\n";
echo "<li>
457 458
          <a href=\"#A\" " .
               ($show == "vis" ? "style=\"background-color:white\"" : " ") .
459
               "id=\"li_vis\" onclick=\"Show('vis');\">".
460
               "Topology</a></li>\n";
461
echo "<li>
462 463 464 465
          <a href=\"#B\" " .
               ($show == "nsfile" ? "style=\"background-color:white\"" : " ") .
               "id=\"li_nsfile\" onclick=\"Show('nsfile');\">".
               "NS File</a></li>\n";
466
echo "<li>
467 468 469 470
          <a href=\"#C\" " .
               ($show == "graph" ? "style=\"background-color:white\"" : " ") .
               "id=\"li_graph\" onclick=\"Show('graph');\">".
               "History</a></li>\n";
471
echo "</ul>\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
472

473
#
474
# Start out with  ...
475 476
#
echo "<div align=center width=\"100%\" id=\"showexp_visarea\">\n";
477
echo $init_show;
478 479
echo "</div>\n";
echo "</div>\n";
480

481 482
SUBPAGEEND();

483 484
$paramcount = $template->ParameterCount();
$metacount  = $template->MetadataCount();
485 486 487 488 489 490 491
$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";

492
$template->Show();
493 494 495 496 497 498 499 500

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) {
501
	$template->ShowParameters();
502 503 504 505 506
	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";
507
	$template->ShowMetadata();
508 509
    }
    elseif ($paramcount) {
510
	$template->ShowParameters();
511 512
    }
    else {
513
	$template->ShowMetadata();
514 515
    }
    echo "</td>\n";
516 517
}

518 519 520
echo "</tr>\n";
echo "</table>\n";
echo "</center>\n";
521 522 523 524 525 526

#
# Standard Testbed Footer
# 
PAGEFOOTER();
?>