template_show.php 14.2 KB
Newer Older
1 2 3 4 5 6 7
<?php
#
# EMULAB-COPYRIGHT
# Copyright (c) 2006 University of Utah and the Flux Group.
# 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 19
#
$uid = GETLOGIN();
LOGGEDINORDIE($uid);
$isadmin = ISADMIN($uid);

Leigh B. Stoller's avatar
Leigh B. Stoller committed
20 21 22
# Need these below, set in CheckArguments.
$pid = "";
$eid = "";
23
$gid = "";
24
$template = NULL;
25
$exptidx  = 0;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
26

27 28
function CheckArguments($guid, $version) {
    global $TB_EXPT_READINFO;
29
    global $uid, $pid, $eid, $gid, $template, $exptidx;
30 31 32 33

    #
    # Verify page arguments.
    # 
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
    if (!isset($guid) ||
	strcmp($guid, "") == 0) {
	USERERROR("You must provide a template GUID.", 1);
    }
    
    if (!isset($version) ||
	strcmp($version, "") == 0) {
	USERERROR("You must provide a template version number", 1);
    }
    if (!TBvalid_guid($guid)) {
	PAGEARGERROR("Invalid characters in GUID!");
    }
    if (!TBvalid_integer($version)) {
	PAGEARGERROR("Invalid characters in version!");
    }
    
50 51 52
    #
    # Check to make sure this is a valid template.
    #
53 54
    $template = Template::Lookup($guid, $version);    
    if (!$template) {
55 56 57
	USERERROR("The experiment template $guid/$version is not a ".
		  "valid experiment template!", 1);
    }
58 59 60
    $pid = $template->pid();
    $gid = $template->gid();
    $eid = $template->eid();
61 62
    $exptidx = TBExptIndex($pid, $eid);
    
63 64 65
    #
    # Verify Permission.
    #
66
    if (! $template->AccessCheck($uid, $TB_EXPT_READINFO)) {
67 68 69 70 71 72 73
	USERERROR("You do not have permission to view experiment ".
		  "template $guid/$version!", 1);
    }
}

CheckArguments($guid, $version);

74 75 76 77 78 79
#
# For the Sajax Interface
#
function Show($which, $zoom, $detail)
{
    global $pid, $eid, $uid, $TBSUEXEC_PATH, $TBADMINGROUP;
80
    global $template, $isadmin;
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 134 135 136 137 138
    $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";
	    }
139
	    $html .= "<input id=showexp_recursive type=checkbox value=Yep> ";
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
	    $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";
159 160 161 162 163 164 165 166 167 168

	# 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";
	}
169 170 171 172 173 174 175 176 177 178
    }
    elseif ($which == "nsfile") {
	$nsdata = "";

	$input_list = $template->InputFiles();

	for ($i = 0; $i < count($input_list); $i++) {
	    $nsdata .= htmlentities($input_list[$i]);
	    $nsdata .= "\n\n";
	}
179 180
	$html = "<pre><div align=left class=\"showexp_codeblock\">".
	    "$nsdata</div></pre>\n";
181 182 183 184 185 186 187 188 189 190 191

	$html .= "<button name=savens type=button value=1";
	$html .= " onclick=\"SaveNS();\">";
	$html .= "Save</button>\n";
    }
    return $html;
}

#
# Sajax callback for operating on the template graph.
#
192
function GraphChange($action, $recursive = 0, $no_output = 0)
193 194 195 196
{
    global $pid, $gid, $eid, $uid, $guid, $TBSUEXEC_PATH, $TBADMINGROUP;
    global $template;
    $html = "";
197 198 199

    # Need this for scripts.
    TBGroupUnixInfo($pid, $gid, $unix_gid, $unix_name);
200 201
    
    $reqarg  = "-a ";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
202
    $versarg = $template->vers();
203

204 205 206 207 208 209 210 211 212
    if ($action == "zoomout" || $action == "zoomin") {
	$optarg = "";
	
	if ($action == "zoomin") {
	    $optarg = "-z in";
	}
	else {
	    $optarg = "-z out";
	}
213

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

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

262 263 264 265 266 267
#
# 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
268 269 270 271
#
# Active/Inactive is a plain menu link.
#
if (isset($action) && ($action == "activate" || $action == "inactivate")) {
272
    GraphChange($action, 0, 1);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
273 274
}

275 276 277 278 279 280 281 282
# Delete is just plain special!
if (isset($action) && $action == "deletetemplate" &&
    isset($confirmed) && $confirmed == "yep") {

    # Need this for scripts.
    TBGroupUnixInfo($pid, $gid, $unix_gid, $unix_name);

    PAGEHEADER("Delete Template: $guid/$version");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
283
    STARTBUSY("Deleting template $guid/$version recursively");
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317

    # Pass recursive option all the time.
    $retval = SUEXEC($uid, "$pid,$unix_gid",
		     "webtemplate_delete -r $guid/$version",
		     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()) {
	PAGEREPLACE("showuser.php3?target_uid=$uid");
    }
    else {
	PAGEREPLACE("template_show.php?guid=$guid&version=1");
    }
    return;
}

318 319 320 321 322 323
#
# Standard Testbed Header after argument checking.
#
PAGEHEADER("Experiment Template: $guid/$version");

SUBPAGESTART();
324

325 326
SUBMENUSTART("Template Options");

Leigh B. Stoller's avatar
Leigh B. Stoller committed
327 328
if ($template->IsActive()) {
    WRITESUBMENUBUTTON("InActivate Template &nbsp &nbsp",
Leigh B. Stoller's avatar
Leigh B. Stoller committed
329
		       "template_show.php?guid=$guid".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
330
		       "&version=$version&action=inactivate");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
331 332
}
else {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
333
    WRITESUBMENUBUTTON("Activate Template &nbsp &nbsp",
Leigh B. Stoller's avatar
Leigh B. Stoller committed
334
		       "template_show.php?guid=$guid".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
335
		       "&version=$version&action=activate");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
336
}
337 338 339 340 341 342 343 344 345 346 347

WRITESUBMENUBUTTON("Modify Template",
		   "template_modify.php?guid=$guid&version=$version");

WRITESUBMENUBUTTON("Instantiate Template",
		   "template_swapin.php?guid=$guid&version=$version");

WRITESUBMENUBUTTON("Add Metadata",
		   "template_metadata.php?action=add&".
		   "guid=$guid&version=$version");

348 349 350 351 352
if ($template->EventCount() > 0) {
    WRITESUBMENUBUTTON("Edit Template Events",
		       "template_editevents.php?guid=$guid&version=$version");
}

353
# We show the user the datastore for the template; the rest of it is not important.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
354
WRITESUBMENUBUTTON("Template Archive",
355
		   "archive_view.php3/$exptidx/trunk?exptidx=$exptidx");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
356

Leigh B. Stoller's avatar
Leigh B. Stoller committed
357
WRITESUBMENUBUTTON("Template Record",
358 359 360 361 362 363 364 365 366 367 368 369 370
		   "template_history.php?guid=$guid&version=$version");

SUBMENUEND_2A();

#
# Ick.
#
$rsrcidx = TBrsrcIndex($pid, $eid);

echo "<br>
      <img border=1 alt='template visualization'
           src='showthumb.php3?idx=$rsrcidx'>";

371 372
if ($template->InstanceCount()) {
    $template->ShowInstances();
373 374
}

375 376
SUBMENUEND_2B();

Leigh B. Stoller's avatar
Leigh B. Stoller committed
377 378 379
#
# The center area is a form that can show NS file, Template Graph, or Vis.
#
380
echo "<script type='text/javascript' src='template_sup.js'></script>\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
381
echo "<script type='text/javascript' language='javascript'>
382
        var li_current = 'li_vis';
Leigh B. Stoller's avatar
Leigh B. Stoller committed
383
        function Show(which) {
384 385 386 387 388 389 390 391 392 393
	    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
394 395
            return false;
        }
396 397 398 399 400 401 402 403 404 405 406 407 408
        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\", 
			      \"Tarea${version}\");
409
            tt_Init();
410 411 412
        }
        function VisChange(zoom, detail) {
            x_Show('vis', zoom, detail, Show_cb);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
413 414
            return false;
        }
415 416 417 418 419 420 421 422 423 424 425 426 427 428
        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;
            }
	    window.location.replace('template_show.php?guid=$guid" .
                  "&version=$version&action=deletetemplate&confirmed=yep');
            return false;
        }
429
        function GraphChange(action) {
430 431 432 433 434 435 436 437
            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
438 439
            return false;
        }
440 441 442 443 444 445 446 447 448
        function SaveNS() {
            window.open('spitnsdata.php3?guid=$guid&version=$version',
                        '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>";
449

450 451 452 453
#
# 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
454

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

475
#
476
# Start out with  ...
477 478
#
echo "<div align=center width=\"100%\" id=\"showexp_visarea\">\n";
479 480 481 482 483 484 485 486 487 488 489 490 491
if (!isset($show) || $show == "vis") {
    echo Show("vis", 0, 0);
}
elseif ($show == "nsfile") {
    echo Show("nsfile", 0, 0);
}
elseif ($show == "graph") {
    echo Show("graph", 0, 0);
}
else {
    echo Show("vis", 0, 0);

}
492 493
echo "</div>\n";
echo "</div>\n";
494

495 496
SUBPAGEEND();

497 498
$paramcount = $template->ParameterCount();
$metacount  = $template->MetadataCount();
499 500 501 502 503 504 505
$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";

506
$template->Show();
507 508 509 510 511 512 513 514

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) {
515
	$template->ShowParameters();
516 517 518 519 520
	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";
521
	$template->ShowMetadata();
522 523
    }
    elseif ($paramcount) {
524
	$template->ShowParameters();
525 526
    }
    else {
527
	$template->ShowMetadata();
528 529
    }
    echo "</td>\n";
530 531
}

532 533 534
echo "</tr>\n";
echo "</table>\n";
echo "</center>\n";
535 536 537 538 539 540

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