template_defs.php 69.5 KB
Newer Older
1 2
<?php
#
3
# Copyright (c) 2006-2012 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
#
#
25 26
# The template class is really just a wrapper around the DB data, plus
# some access routines and printing functions. 
27
#
28
class Template
29
{
30
    var	$template;
31
    var $experiment;
32
    
33 34 35 36 37 38 39
    function Template($guid, $vers) {
	$guid = addslashes($guid);
	$vers = addslashes($vers);
	
	$query_result =
	    DBQueryWarn("select * from experiment_templates ".
			"where guid='$guid' and vers='$vers'");
40

41 42 43 44 45
	if (!$query_result || !mysql_num_rows($query_result)) {
	    $this->template = NULL;
	    return;
	}
	$this->template = mysql_fetch_array($query_result);
46 47

	# 
48
	# Underlying experiment for easy access.
49 50 51 52
	#
	$pid = $this->pid();
	$eid = $this->eid();

53 54
	if (($experiment = Experiment::LookupByPidEid($pid, $eid))) {
	    $this->experiment = $experiment;
55
	}
56 57
    }

58 59 60
    # Hmm, how does one cause an error in a php constructor?
    function IsValid() {
	return !is_null($this->template);
61
    }
62

63 64 65
    # Do class level lookup.
    function Lookup($guid, $vers) {
	$foo = new Template($guid, $vers);
66

67 68 69 70 71 72 73
	if ($foo->IsValid())
	    return $foo;
	return null;
    }
    # Do class level lookup for the root template.
    function LookupRoot($guid) {
	$foo = new Template($guid, 1); 
74

75 76 77
	if ($foo->IsValid())
	    return $foo;
	return null;
78
    }
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    # Look up by pid,eid is which also unique across templates.
    function LookupbyEid($pid, $eid) {
	$query_result =
	    DBQueryWarn("select guid,vers from experiment_templates  ".
			"where pid='$pid' and eid='$eid'");

	if (!$query_result || !mysql_num_rows($query_result))
	    return null;
	
	$row = mysql_fetch_array($query_result);
	$guid = $row['guid'];
	$vers = $row['vers'];
	
	$foo = new Template($guid, $vers); 

	if ($foo->IsValid())
	    return $foo;
	return null;
    }
98 99

    #
100 101 102
    # Refresh a template instance by reloading from the DB.
    #
    function Refresh() {
103
	if (! $this->IsValid())
104
	    return -1;
105

106 107
	$guid = $this->guid();
	$vers = $this->vers();
108
    
109 110 111 112 113 114 115 116 117
	$query_result =
	    DBQueryWarn("select * from experiment_templates ".
			"where guid='$guid' and vers='$vers'");

	if (!$query_result || !mysql_num_rows($query_result)) {
	    $this->template = NULL;
	    return -1;
	}
	$this->template = mysql_fetch_array($query_result);
118 119 120
	return 0;
    }

121 122 123
    # accessors
    function guid() {
	return (is_null($this->template) ? -1 : $this->template['guid']);
124
    }
125 126
    function vers() {
	return (is_null($this->template) ? -1 : $this->template['vers']);
127
    }
128 129
    function pid() {
	return (is_null($this->template) ? -1 : $this->template['pid']);
130
    }
131 132
    function gid() {
	return (is_null($this->template) ? -1 : $this->template['gid']);
133
    }
134 135
    function eid() {
	return (is_null($this->template) ? -1 : $this->template['eid']);
136
    }
137 138
    function tid() {
	return (is_null($this->template) ? -1 : $this->template['tid']);
139
    }
140 141
    function uid() {
	return (is_null($this->template) ? -1 : $this->template['uid']);
142
    }
143
    function path() {
144 145
	if (!$this->experiment)
	    return -1;
146
	$experiment = $this->GetExperiment();
147
	return $experiment->path();
148
    }
149 150
    function IsHidden() {
	return (is_null($this->template) ? -1 : $this->template['hidden']);
151
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
152 153 154
    function IsActive() {
	return (is_null($this->template) ? -1 : $this->template['active']);
    }
155 156 157 158 159 160 161 162 163 164 165 166
    function created() {
	return (is_null($this->template) ? -1 : $this->template['created']);
    }
    function description() {
	return (is_null($this->template) ? -1 :
		$this->template['description']);
    }
    function parent_guid() {
	return (is_null($this->template) ? -1 :$this->template['parent_guid']);
    }
    function parent_vers() {
	return (is_null($this->template) ? -1 :$this->template['parent_vers']);
167
    }
168 169 170
    function child_vers() {
	return (is_null($this->template) ? -1 :$this->template['child_vers']);
    }
171 172 173
    function logfile() {
	return (is_null($this->template) ? -1 :$this->template['logfile']);
    }
174

175 176 177 178
    # The root template has no parent guid.
    function IsRoot() {
	if (is_null($this->template))
	    return -1;
179

180
	return is_null($this->template['parent_guid']);
181 182
    }

183 184
    # Return the underlying experiment for this template.
    function GetExperiment() {
185 186 187 188 189 190 191 192
	if (! $this->experiment) {
	    $pid = $self->pid();
	    $eid = $self->eid();
	    
	    if (($experiment = Experiment::LookupByPidEid($pid, $eid))) {
		$this->experiment = $experiment;
	    }
	}
193 194 195
	return $this->experiment;
    }

196 197 198 199 200 201 202 203 204
    function GetProject() {
	$pid = $this->pid();

	if (! ($project = Project::Lookup($pid))) {
	    TBERROR("Could not lookup project $pid!", 1);
	}
	return $project;
    }

205 206 207 208 209 210 211 212
    function GetLogfile() {
	$this->Refresh();
	
	if ($this->logfile()) 
	    return Logfile::Lookup($this->logfile());
	return null;
    }

213 214
    # Return the unixgid for operating on this template.
    function UnixGID() {
215
	$experiment = $this->GetExperiment();
216 217 218 219 220

	return $experiment->UnixGID();
    }

    function AccessCheck($user, $access_type) {
221 222 223 224 225 226 227 228 229 230
	global $TB_EXPT_READINFO;
	global $TB_EXPT_MODIFY;
	global $TB_EXPT_DESTROY;
	global $TB_EXPT_UPDATE;
	global $TB_EXPT_MIN;
	global $TB_EXPT_MAX;
	global $TBDB_TRUST_USER;
	global $TBDB_TRUST_LOCALROOT;
	global $TBDB_TRUST_GROUPROOT;
	global $TBDB_TRUST_PROJROOT;
231
	$mintrust = $TB_EXPT_READINFO;
232 233 234 235 236

	if ($access_type < $TB_EXPT_MIN ||
	    $access_type > $TB_EXPT_MAX) {
	    TBERROR("Invalid access type: $access_type!", 1);
	}
237

238 239 240 241 242 243 244 245
        #
        # Admins do whatever they want!
        # 
	if (ISADMIN()) {
	    return 1;
	}
	$pid = $this->pid();
	$gid = $this->gid();
246
	$uid = $user->uid();
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261

	if ($access_type == $TB_EXPT_READINFO) {
	    $mintrust = $TBDB_TRUST_USER;
	}
	else {
	    $mintrust = $TBDB_TRUST_LOCALROOT;
	}

        #
        # Either proper permission in the group, or group_root in the project.
        # This lets group_roots muck with other peoples experiments, including
        # those in groups they do not belong to.
        #
	return TBMinTrust(TBGrpTrust($uid, $pid, $gid), $mintrust) ||
	    TBMinTrust(TBGrpTrust($uid, $pid, $pid), $TBDB_TRUST_GROUPROOT);
262
    }
263

264
    #
265
    # Display a template in its own table.
266
    #
267 268 269 270 271 272 273 274 275
    function Show() {
	$guid        = $this->guid();
	$vers        = $this->vers();
	$pid         = $this->pid();
	$gid         = $this->gid();
	$uid         = $this->uid();
	$tid         = $this->tid();
	$created     = $this->created();
	$description = $this->description();
276
	$path        = $this->path();
277

278 279 280 281 282
	if (! ($user = User::Lookup($uid))) {
	    TBERROR("Could not lookup object for user $uid", 1);
	}
	$showuser_url = CreateURL("showuser", $user);
	
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
        #
        # We need the metadata guid/version for the TID and description since
        # they are mungable metadata.
        #
	$tid_metadata  = $this->LookupMetadataByName("TID");
	$desc_metadata = $this->LookupMetadataByName("description");
	
	if ($tid_metadata == NULL) {
	    TBERROR("Could not find Metadata 'TID' for $guid/$vers", 1);
	}
	if ($desc_metadata == NULL) {
	    TBERROR("Could not find Metadata 'description' for $guid/$vers",1);
	}
	$tid_guid  = $tid_metadata->guid();
	$tid_vers  = $tid_metadata->vers();
	$desc_guid = $desc_metadata->guid();
	$desc_vers = $desc_metadata->vers();
300
    
301 302 303 304 305 306 307 308
        #
        # Generate the table.
        #
	echo "<center>
               <h3>Details</h3>
              </center>\n";

	echo "<table align=center cellpadding=2 cellspacing=2 border=1>\n";
309
    
310
	ShowItem("GUID",
311
		 MakeLink("template",
312 313 314
			  "guid=$guid&version=$vers", "$guid/$vers"));
	
	ShowItem("ID",
315 316 317
		 MakeAnchor(CreateURL("template_metadata", $this) .
			    "&action=modify".
			    "&metadata=${tid_guid}/${tid_vers}", $tid));
318 319 320
	
	ShowItem("Project", MakeLink("project", "pid=$pid", $pid));
	ShowItem("Group",   $gid);
321
	ShowItem("Creator", MakeAnchor($showuser_url, $uid));
322
	ShowItem("Created", $created);
323

324 325 326 327
	$onmouseover = MakeMouseOver($description);
	if (strlen($description) > 40) {
	    $description = substr($description, 0, 40) . " <b>... </b>";
	}
328 329 330 331 332
	ShowItem("Description",
		 MakeAnchor(CreateURL("template_metadata", $this) .
			    "&action=modify".
			    "&metadata=${desc_guid}/${desc_vers}",
			    $description, $onmouseover));
333
    
334 335 336 337 338 339 340 341 342 343 344 345
	if (! $this->IsRoot()) {
	    $parent_guid = $this->parent_guid();
	    $parent_vers = $this->parent_vers();
	    
	    ShowItem("Parent Template",
		     MakeLink("template",
			      "guid=$parent_guid&version=$parent_vers",
			      "$parent_guid/$parent_vers"));
	}
	echo "</table>\n";
    }

346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
    # 
    # Summary data for the entire tree of templates.
    #
    function ShowSummary() {
	$guid = $this->guid();
	$vers = $this->vers();
	$id   = "${guid}_${vers}_summary";

	$query_result =
	    DBQueryFatal("select * from experiment_templates ".
			 "where guid='$guid' order by vers");

	if (! mysql_num_rows($query_result))
	    return;

	AddSortedTable($id);
	echo "<table align=center id='$id'
		     border=1 cellpadding=5 cellspacing=2>\n";

	echo "<thead class='sort'>\n";
 	echo "<tr>
                <th>Vers</th>
                <th>Parent</th>
                <th>TID</th>
                <th>Created</th>
                <th>Description</th>
              </tr>\n";
 	echo "</thead>\n";

	while ($row = mysql_fetch_array($query_result)) {
	    $vers    = $row['vers'];
	    $tid     = $row['tid'];
	    $pvers   = $row['parent_vers'];
	    $tid     = $row['tid'];
	    $desc    = $row['description'];
	    $created = $row['created'];

	    $onmouseover = MakeMouseOver($desc);
	    if (strlen($desc) > 30) {
		$desc = substr($desc, 0, 30) . " <b>... </b>";
	    }
	    $parent_link = MakeLink("template",
				    "guid=$guid&version=$pvers", "$pvers");

	    $current_link = MakeLink("template",
				     "guid=$guid&version=$vers", "$vers");

	    echo "<tr>".
		"<td>$current_link</td>".
		"<td>$parent_link</td>".
		"<td>$tid</td>".
		"<td>$created</td>".
		"<td $onmouseover>$desc</td>";
	}
	echo "</table>\n";
    }

403 404 405 406 407 408 409 410 411 412 413 414 415 416
    #
    # Page header; spit back some html for the typical page header.
    #
    function PageHeader() {
	$guid = $this->guid();
	$vers = $this->vers();
	$html = "<font size=+2>Template <b>" .
	    MakeLink("template",
		     "guid=$guid&version=$vers", "$guid/$vers") . 
	    "</b></font>";

	return $html;
    }

417 418 419 420 421 422
    #
    # Display template parameters and default values in a table
    #
    function ShowParameters() {
	$guid = $this->guid();
	$vers = $this->vers();
423
	$id   = "${guid}_${vers}_parameters";
424 425
	
	$query_result =
426 427 428 429 430 431 432 433 434 435
	    DBQueryFatal("select p.name,p.value, ".
			 "       p.metadata_guid,p.metadata_vers, ".
			 "       m.value as description ".
			 "   from experiment_template_parameters as p ".
			 "left join experiment_template_metadata_items as m ".
			 "  on m.guid=p.metadata_guid and ".
			 "     m.vers=p.metadata_vers ".
			 "where p.parent_guid='$guid' and ".
			 "      p.parent_vers='$vers'");
    
436 437 438
	if (!$query_result ||
	    !mysql_num_rows($query_result))
	    return 0;
439

440
	AddSortedTable($id);
441
	echo "<center>
442
               <h3>Parameters</h3>
443
             </center> 
444 445
             <table align=center id='$id'
		    border=1 cellpadding=5 cellspacing=2>\n";
446

447
	echo "<thead class='sort'>\n";
448 449 450
 	echo "<tr>
                <th>Name</th>
                <th>Default Value</th>
451
                <th>Description</th>
452
              </tr>\n";
453
	echo "</thead>\n";
454 455

	while ($row = mysql_fetch_array($query_result)) {
456 457 458 459 460 461
	    $name	   = $row['name'];
	    $value	   = $row['value'];
	    $metadata_guid = $row['metadata_guid'];
	    $metadata_vers = $row['metadata_vers'];
	    $description   = $row['description'];
	    
462 463 464 465 466 467
	    if (!isset($value)) {
		$value = "&nbsp";
	    }

	    echo "<tr>
                   <td>$name</td>
468 469 470
                   <td>$value</td>";

	    if (is_null($description)) {
471 472
		$url = CreateURL("template_metadata", $this);
		
473
		echo "<td>".
474 475 476
		     "<a href='${url}&action=add&".
		     "metadata_type=parameter_description&".
		     "formfields[name]=${name}'>Click to add</a></td>\n";
477 478 479 480 481 482 483
	    }
	    else {
		$onmouseover = MakeMouseOver($description);
		if (strlen($description) > 30) {
		    $description =
			substr($description, 0, 30) . " <b>... </b>";
		}
484 485 486 487 488
		$url = CreateURL("template_metadata", $this);
		
		echo "<td><a href='${url}&action=modify".
		     "&metadata=${metadata_guid}/${metadata_vers}' " .
		     "$onmouseover>$description</a></td>\n";
489 490 491
	    }
	    echo "</tr>\n";
	    }
492 493 494
	echo "</table>\n";
	return 1;
    }
495
	
496 497 498 499 500 501
    #
    # Display template metadata and values in a table
    #
    function ShowMetadata() {
	$guid = $this->guid();
	$vers = $this->vers();
502
	$id   = "${guid}_${vers}_metadata";
503 504 505
	
	$query_result =
	    DBQueryFatal("select i.* from experiment_template_metadata as m ".
506 507 508
		     "left join experiment_template_metadata_items as i on ".
		     "     m.metadata_guid=i.guid and m.metadata_vers=i.vers ".
		     "where m.parent_guid='$guid' and ".
509 510
		     "      m.parent_vers='$vers' and ".
		     "      m.internal=0 and m.hidden=0");
511 512 513

	if (! mysql_num_rows($query_result))
	    return 0;
514

515
	AddSortedTable($id);
516
	echo "<center>
517
               <h3>Metadata</h3>
518
             </center> 
519 520
             <table id='$id'
                    align=center border=1 cellpadding=5 cellspacing=2>\n";
521

522
	echo "<thead class='sort'>\n";
523 524
 	echo "<tr>
                <th>Edit</th>
525
                <th>Delete</th>
526 527 528
                <th>Name</th>
                <th>Value</th>
              </tr>\n";
529
	echo "</thead>\n";
530 531 532 533 534 535

	while ($row = mysql_fetch_array($query_result)) {
	    $name	   = $row['name'];
	    $value	   = $row['value'];
	    $metadata_guid = $row['guid'];
	    $metadata_vers = $row['vers'];
536 537 538
	    $onmouseover   = "";
	    
	    if (!isset($value) || $value == "") {
539 540
		$value = "&nbsp";
	    }
541 542 543 544 545 546
	    else {
		$onmouseover = MakeMouseOver($value);
		
		if (strlen($value) > 40) {
		    $value = substr($value, 0, 40) . "<b> ... </b></a>";
		}
547
	    }
548

549 550 551
	    $meta_url = CreateURL("template_metadata", $this) .
		"&metadata=${metadata_guid}/${metadata_vers}";
		
552 553
	    echo "<tr>
   	           <td align=center>
554 555
                     <a href='${meta_url}&action=modify'>
                       <img border=0 alt='modify' src='greenball.gif'></A></td>
556
   	           <td align=center>
557 558
                     <a href='${meta_url}&action=delete'>
                       <img border=0 alt='delete' src='redball.gif'></A></td>
559
                   <td>$name</td>
560
                   <td $onmouseover>$value</td>
561 562 563 564 565 566
                  </tr>\n";
  	}
	echo "</table>\n";
	return 1;
    }

567 568 569 570 571 572
    #
    # Show the instance list for a template.
    #
    function ShowInstances() {
	$guid = $this->guid();
	$vers = $this->vers();
573 574

	$query_result =
575 576 577 578 579 580 581 582 583 584 585 586 587 588
	    DBQueryFatal("select e.*,count(r.node_id) as nodes, ".
			 "    round(minimum_nodes+.1,0) as min_nodes ".
			 "from experiment_template_instances as i ".
			 "left join experiments as e on e.idx=i.exptidx ".
			 "left join reserved as r on e.pid=r.pid and ".
			 "     e.eid=r.eid ".
			 "where e.pid is not null and ".
			 "      (i.parent_guid='$guid' and ".
			 "       i.parent_vers='$vers') ".
			 "group by e.pid,e.eid order by e.state,e.eid");

	if (! mysql_num_rows($query_result))
	    return;
	
589
	echo "<center>
590
               <h3>Template Instances</h3>
591 592 593 594 595 596
             </center> 
             <table align=center border=1 cellpadding=5 cellspacing=2>\n";

	echo "<tr>
               <th>EID</th>
               <th>State</th>
597
               <th align=center>Nodes</th>
598 599 600 601 602 603 604 605 606 607 608 609 610
              </tr>\n";

	$idlemark = "<b>*</b>";
	$stalemark = "<b>?</b>";
	
	while ($row = mysql_fetch_array($query_result)) {
	    $pid       = $row['pid'];
	    $eid       = $row['eid'];
	    $state     = $row['state'];
	    $nodes     = $row['nodes'];
	    $minnodes  = $row['min_nodes'];
	    $ignore    = $row['idle_ignore'];
	    $name      = $row['expt_name'];
611 612 613 614 615 616

	    if (! ($experiment = Experiment::LookupByPidEid($pid, $eid))) {
		TBERROR("Could not map $pid/$eid to its object", 1);
	    }
	    $idlehours = $experiment->IdleTime();
	    $stale     = $experiment->IdleStale();
617 618 619 620
	    
	    if ($nodes == 0) {
		$nodes = "<font color=green>$minnodes</font>";
	    }
621
	    elseif ($row["swap_requests"] > 0) {
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
		$nodes .= $idlemark;
	    }

	    $idlestr = $idlehours;
	    
	    if ($idlehours > 0) {
		if ($stale) {
		    $idlestr .= $stalemark;
		}
		if ($ignore) {
		    $idlestr = "($idlestr)";
		}
	    }
	    elseif ($idlehours == -1) {
		$idlestr = "&nbsp;";
	    }
	    
	    echo "<tr>
                   <td><A href='showexp.php3?pid=$pid&eid=$eid'>$eid</A></td>
  		   <td>$state</td>
                   <td align=center>$nodes</td>
                 </tr>\n";
	}
	echo "</table>\n";
    }

648 649 650 651 652 653
    #
    # Show the historical instance list for a template.
    #
    function ShowHistory($expand) {
	$guid = $this->guid();
	$vers = $this->vers();
654

655
	$query_result =
656 657 658 659 660 661
	    DBQueryFatal("select i.*,r.archive_tag ".
			 "  from experiment_template_instances as i ".
			 "left join experiment_stats as s on ".
			 "     s.exptidx=i.exptidx ".
			 "left join experiment_resources as r on ".
			 "     r.idx=s.rsrcidx ".
662 663 664 665 666 667 668
			 "where (i.parent_guid='$guid' and ".
			 "       i.parent_vers='$vers') ".
			 "order by i.start_time");

	if (! mysql_num_rows($query_result))
	    return 0;
	
669
	echo "<center>
Leigh B. Stoller's avatar
Leigh B. Stoller committed
670
               <h3>Template History (Swapins)</h3>
671 672 673 674
             </center> 
             <table align=center border=1 cellpadding=5 cellspacing=2>\n";

	echo "<tr>
675 676
               <th align=center>&nbsp</th>
               <th>ID</th>
677 678
               <th>Start Time</th>
               <th>Stop Time</th>
679
	       <th>Description</th>
680 681 682 683 684 685
              </tr>\n";

	$idlemark = "<b>*</b>";
	$stalemark = "<b>?</b>";
	
	while ($row = mysql_fetch_array($query_result)) {
686 687 688 689 690 691 692 693 694
	    $pid        = $row['pid'];
	    $eid        = $row['eid'];
	    $start      = $row['start_time'];
	    $stop       = $row['stop_time'];
	    $exptidx    = $row['exptidx'];
	    $idx        = $row['idx'];
	    $tag        = $row['archive_tag'];
	    $description= $row['description'];
	    $onmouseover= "";
695 696 697 698

	    if (! isset($stop)) {
		$stop = "&nbsp";
	    }
699 700

	    $expandit = ((isset($expand) && $expand == $idx) ? 1 : 0);
701 702 703 704 705 706 707 708 709 710 711

	    if (isset($description) && $description != "") {
		$onmouseover = MakeMouseOver($description);
		if (strlen($description) > 30) {
		    $description = substr($description, 0, 30) . " <b>...</b>";
		}
	    }
	    else {
		$description = "&nbsp ";
	    }

712
	    echo "<tr>\n";
713 714 715
	    echo " <td align=center>";
	    if ($expandit) {
		echo "<a href=template_history.php".
716
                         "?guid=$guid&version=$vers>".
717 718 719 720
		         "<img border=0 alt='c' src='/icons/down.png'></a>\n";
	    }
	    else {
 		echo "<a href=template_history.php".
721
                         "?guid=$guid&version=$vers&expand=$idx#$idx>".
722 723
		         "<img border=0 alt='e' src='/icons/right.png'></a>\n";
	    }
724
	    echo " </td>";
725

726
	    echo " <td align=center>".
727
		      MakeLink("instance", "instance=$exptidx", $eid);
728
	    echo " </td>\n";
729

730 731 732
	    echo " <td>$start</td>
                   <td>$stop</td>
                   <td $onmouseover>$description</td>\n";
733

734
	    echo "</tr>\n";
735 736

	    if ($expandit) {
737 738
		$instance = new TemplateInstance($exptidx);
		
739 740 741
		echo "<tr>\n";
		echo " <a NAME=$idx></a>\n";
		echo "<td>&nbsp</td>\n";
742
		echo " <td colspan=4>\n";
743
		$instance->ShowRunList(0, 0);
744 745 746
		echo " </td>\n";
		echo "</tr>\n";
	    }
747 748 749
	}
	echo "</table>\n";
    }
750 751 752 753 754 755

    #
    # Dump the image map for a template to the output.
    #
    function ShowGraph() {
	$guid = $this->guid();
756
	$vers = $this->vers();
757
	# Make the link unique to force reload on the client side.
758
	$now  = time();
759 760 761 762 763 764 765 766 767 768 769 770 771

	$query_result =
	    DBQueryFatal("select imap from experiment_template_graphs ".
			 "where parent_guid='$guid'");

	if (!mysql_num_rows($query_result)) {
	    USERERROR("Experiment Template $guid is no longer in the DB!", 1);
	}
	$row  = mysql_fetch_array($query_result);
	$imap = $row['imap'];

	echo "<center>";
	echo "<div id=fee style='display: block; overflow: hidden; ".
772 773
	    "position: relative; z-index:1010; height: 400px; ".
	    "width: 700px; border: 2px solid black;'>\n";
774
	echo "<div id=\"mygraphdiv\" style='position:relative;'>\n";
775

776 777 778 779 780
	echo "<div id='CurrentTemplate' style='display: block; opacity: 1; ".
	    "visibility: hidden; ".
	    "position: absolute; z-index:1020; left: 0px; top: 0px;".
	    "height: 0px; width: 0px; border: 2px solid red;'></div>\n";

781
	echo $imap;
782 783
	echo "<img id=\"mygraphimg\" border=0 usemap=\"#TemplateGraph\" ";
	echo "      onLoad=\"setTimeout('ShowGraphInit();', 10);\" ";
784
	echo "      style='cursor: move;' ";
785
	echo "      src='template_graph.php?guid=$guid&version=1&now=$now'>\n";
786 787 788 789
	echo "</div>\n";
	echo "</div>\n";
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
790 791 792 793 794 795 796 797 798 799 800 801 802 803
    #
    # Dump the NS file into its own iframe.
    #
    function ShowNS() {
	$guid = $this->guid();
	$vers = $this->vers();

	echo "<center>";
	echo "<iframe width=700 height=400 scrolling=auto
                      src='spitnsdata.php3?guid=$guid&version=$vers'
                      border=2></iframe>\n";
	echo "</center>";
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
804 805 806 807 808 809 810
    #
    # Dump the visualization into its own iframe.
    #
    function ShowVis($zoom = 1.25, $detail = 1) {
	$guid = $this->guid();
	$pid  = $this->pid();
	$eid  = $this->eid();
811 812
	# Make the link unique to force reload on the client side.
	$now  = time();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
813 814 815 816 817

	echo "<center>";
	echo "<div id=fee style='display: block; overflow: hidden; ".
	    "position: relative; z-index:1010; height: 400px; ".
	    "width: 700px; border: 2px solid black;'>\n";
818
	echo "<div id=\"myvisdiv\" style='position:relative;'>\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
819

820
	echo "<img id=\"myvizimg\" border=0 style='cursor: move;' ";
821
	echo "      onLoad=\"setTimeout('ShowVisInit();', 100);\" ";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
822
	echo "      src='top2image.php3?pid=$pid&eid=$eid".
823
	    "&zoom=$zoom&detail=$detail&now=$now'>\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839
	echo "</div>\n";
	echo "</div>\n";
    }

    #
    # Grab the zoom and detail for the current viz picture, as a default.
    #
    function CurrentVisDetails() {
	$pid = $this->pid();
	$eid = $this->eid();

	$query_result =
	    DBQueryFatal("select zoom,detail from vis_graphs ".
			 "where pid='$pid' and eid='$eid'");

	if (!mysql_num_rows($query_result)) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
840
	    return array(1.25, 1);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
841 842 843 844 845 846 847
	}
	$row    = mysql_fetch_array($query_result);
	$zoom   = $row['zoom'];
	$detail = $row['detail'];
	return array($zoom, $detail);
    }

848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901
    #
    # Grab array of input files for a template, indexed by input_idx.
    #
    function InputFiles() {
	$guid = $this->guid();
	$vers = $this->vers();
	
	$input_list = array();

	$query_result =
	    DBQueryFatal("select * from experiment_template_inputs ".
			 "where parent_guid='$guid' and parent_vers='$vers'");

	while ($row = mysql_fetch_array($query_result)) {
	    $input_idx = $row['input_idx'];

	    $input_query =
		DBQueryFatal("select input ".
			     "  from experiment_template_input_data ".
			     "where idx='$input_idx'");

	    $input_row = mysql_fetch_array($input_query);
	    $input_list[] = $input_row['input'];
	}
	return $input_list;
    }

    #
    # Return number of parameters.
    #
    function ParameterCount() {
	$guid = $this->guid();
	$vers = $this->vers();
	
	$query_result =
	    DBQueryFatal("select name,value ".
			 "   from experiment_template_parameters ".
			 "where parent_guid='$guid' and ".
			 "      parent_vers='$vers'");

	return mysql_num_rows($query_result);
    }

    #
    # Return number of metadata items
    #
    function MetadataCount() {
	$guid = $this->guid();
	$vers = $this->vers();

	$query_result =
	    DBQueryFatal("select internal ".
			 "  from experiment_template_metadata as m ".
			 "where m.parent_guid='$guid' and ".
902 903
			 "      m.parent_vers='$vers' and ".
			 "      m.internal=0 and m.hidden=0");
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964

	return mysql_num_rows($query_result);
    }

    #
    # Return number of instances
    #
    function InstanceCount() {
	$guid = $this->guid();
	$vers = $this->vers();

	$query_result =
	    DBQueryFatal("select * from experiment_template_instances as i ".
			 "where (i.parent_guid='$guid' and ".
			 "       i.parent_vers='$vers')");

	return mysql_num_rows($query_result);
    }

    #
    # Look for a metadata item in a template, by the guid/vers of the
    # metadata. Returns a class instance (see below).
    #
    function LookupMetadataByGUID($metadata_guid, $metadata_vers) {
	return TemplateMetadata::TemplateLookupByGUID($this,
						      $metadata_guid,
						      $metadata_vers);
    }
    # Ditto by name,
    function LookupMetadataByName($metadata_name) {
	return TemplateMetadata::TemplateLookupByName($this, $metadata_name);
    }

    # Grab the graph data.
    function GraphImage(&$image) {
	$guid = $this->guid();

	$query_result =
	    DBQueryWarn("select image from experiment_template_graphs ".
			"where parent_guid='$guid'");

	if (!$query_result || !mysql_num_rows($query_result)) {
	    return -1;
	}
	$row   = mysql_fetch_array($query_result);
	$image = $row['image'];
	return 0;
    }

    #
    # Return an array of the formal parameters for a template.
    #
    function FormalParameters(&$parameters) {
	$parameters = array();
	$guid = $this->guid();
	$vers = $this->vers();
	
	$query_result =
	    DBQueryFatal("select name,value ".
			 "   from experiment_template_parameters ".
			 "where parent_guid='$guid' and ".
965 966
			 "      parent_vers='$vers' ".
			 "order by name");
967 968 969 970 971 972 973 974 975

	while ($row = mysql_fetch_array($query_result)) {
	    $name	= $row['name'];
	    $value	= $row['value'];

	    $parameters[$name] = $value;
	}
	return 0;
    }
976

977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004
    #
    # Return an array of the formal parameters for a template.
    #
    function FormalParameterMouseOvers(&$mouseovers) {
	$mouseovers = array();
	$guid = $this->guid();
	$vers = $this->vers();
	
	$query_result =
	    DBQueryFatal("select p.name,m.value as description ".
			 "   from experiment_template_parameters as p ".
			 "left join experiment_template_metadata_items as m ".
			 "  on m.guid=p.metadata_guid and ".
			 "     m.vers=p.metadata_vers ".
			 "where p.parent_guid='$guid' and ".
			 "      p.parent_vers='$vers'");
    
	while ($row = mysql_fetch_array($query_result)) {
	    $name	 = $row['name'];
	    $description = $row['description'];

	    if (isset($description) && $description != "") {
		$mouseovers[$name] = MakeMouseOver($description);
	    }
	}
	return 0;
    }

1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
    #
    # Find next candidate for a template (modify) TID
    #
    function NextTID() {
	$tid  = $this->tid();
	$guid = $this->guid();

	$query_result =
	    DBQueryFatal("select MAX(vers) from experiment_templates ".
			 "where guid='$guid'");

	if (mysql_num_rows($query_result) == 0) {
	    return "T" . substr(md5(uniqid($foo, true)), 0, 10);
	}
	$row = mysql_fetch_array($query_result);
	$foo = $row[0] + 1;
	return "${tid}-V${foo}"; 
    }
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085

    #
    # Return array of template events, ordered by time.
    #
    function EventList(&$eventlist) {
	$eventlist = array();
	$guid = $this->guid();
	$vers = $this->vers();
	
	$query_result =
	    DBQueryFatal("select * from experiment_template_events ".
			 "where parent_guid='$guid' and ".
			 "      parent_vers='$vers' ".
			 "order by time");

	$i = 0;
	while ($row = mysql_fetch_array($query_result)) {
	    $eventlist[$i++] = $row;
	}
	return 0;
    }
    function EventCount() {
	$guid = $this->guid();
	$vers = $this->vers();
	
	$query_result =
	    DBQueryFatal("select count(*) from experiment_template_events ".
			 "where parent_guid='$guid' and ".
			 "      parent_vers='$vers' ");

	$row   = mysql_fetch_array($query_result);
	$count = $row[0];
	return $count;
    }
    function DeleteEvent($vname) {
	$guid = $this->guid();
	$vers = $this->vers();
	
	DBQueryFatal("delete from experiment_template_events ".
		     "where parent_guid='$guid' and ".
		     "      parent_vers='$vers' and ".
		     "      vname='$vname'");

	return 0;
    }
    function ModifyEvent($vname, $changes) {
	$guid = $this->guid();
	$vers = $this->vers();
	$sets = array();
	
	while (list ($key, $value) = each ($changes)) {
	    $value  = addslashes($value);
	    $sets[] = "$key='$value'";
	}
	
	DBQueryFatal("update experiment_template_events set ".
		     implode(",", $sets) . " ".
		     "where parent_guid='$guid' and ".
		     "      parent_vers='$vers' and ".
		     "      vname='$vname'");

	return 0;
    }
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137

    function SavedSearches($user) {
	$guid    = $this->guid();
	$vers    = $this->vers();
	$uid_idx = $user->uid_idx();
	
	$query_result =
	    DBQueryFatal("select name from experiment_template_searches ".
			 "where parent_guid='$guid' and ".
			 "      parent_vers='$vers' and uid_idx='$uid_idx'");

	if (!mysql_num_rows($query_result))
	    return null;

	$result = array();

	while ($row = mysql_fetch_array($query_result)) {
	    $name = $row['name'];
	    $result[$name] = $name;
	}
	return $result;
    }
    function SavedSearch($name, $user) {
	$guid    = $this->guid();
	$vers    = $this->vers();
	$uid_idx = $user->uid_idx();
	
	$query_result =
	    DBQueryFatal("select expr from experiment_template_searches ".
			 "where parent_guid='$guid' and ".
			 "      parent_vers='$vers' and ".
			 "      uid_idx='$uid_idx'  and ".
			 "      name='$name'");

	if (!mysql_num_rows($query_result))
	    return array();

	$row = mysql_fetch_array($query_result);
	return unserialize($row['expr']);
    }
    function DeleteSearch($name, $user) {
	$guid    = $this->guid();
	$vers    = $this->vers();
	$uid_idx = $user->uid_idx();
	
	$query_result =
	    DBQueryFatal("delete from experiment_template_searches ".
			 "where parent_guid='$guid' and ".
			 "      parent_vers='$vers' and ".
			 "      uid_idx='$uid_idx'  and ".
			 "      name='$name'");
    }
1138 1139
}

1140
#
1141
# This is the class for a template instance (swapin).
1142
#
1143
class TemplateInstance
1144
{
1145 1146
    var	$template;
    var $instance;
1147
    var $experiment;
1148

1149
    #
1150
    # Instances are found by their index. 
1151
    #
1152 1153
    function TemplateInstance($idx) {
	$idx = addslashes($idx);
1154 1155 1156 1157

	$query_result =
	    DBQueryFatal("select * ".
			 "  from experiment_template_instances ".
1158
			 "where idx='$idx'");
1159 1160 1161 1162 1163 1164
	
	if (!$query_result || !mysql_num_rows($query_result)) {
	    $this->template = NULL;
	    $this->instance = NULL;
	    return;
	}
1165 1166 1167 1168 1169 1170 1171 1172
	$this->instance   = mysql_fetch_array($query_result);
	$this->template   = new Template($this->instance['parent_guid'],
					 $this->instance['parent_vers']);
	$this->experiment = null;
	
	if ($this->instance['exptidx']) {
	    $this->experiment = Experiment::Lookup($this->instance['exptidx']);
	}
1173 1174 1175 1176 1177 1178 1179 1180 1181
    }
    
    # Hmm, how does one cause an error in a php constructor?
    function IsValid() {
	return !is_null($this->instance);
    }

    # Do class level lookup.
    function LookupByExptidx($exptidx) {
1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
	$exptidx = addslashes($exptidx);
	
	$query_result =
	    DBQueryWarn("select idx from experiment_template_instances  ".
			"where exptidx='$exptidx'");

	if (!$query_result || !mysql_num_rows($query_result))
	    return null;
	
	$row = mysql_fetch_array($query_result);
	$foo = new TemplateInstance($row[0]);

	if ($foo->IsValid())
	    return $foo;
	return null;
    }

    # Do class level lookup.
    function LookupByIdx($idx) {
	$foo = new TemplateInstance($idx);
1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220

	if ($foo->IsValid())
	    return $foo;
	return null;
    }

    # accessors
    function idx() {
	return (is_null($this->instance) ? -1 : $this->instance['idx']);
    }
    function exptidx() {
	return (is_null($this->instance) ? -1 : $this->instance['exptidx']);
    }
    function runidx() {
	return (is_null($this->instance) ? -1 : $this->instance['runidx']);
    }
    function pid() {
	return (is_null($this->instance) ? -1 : $this->instance['pid']);
    }
1221 1222 1223 1224 1225 1226
    function gid() {
	if (is_null($this->instance))
	    return -1;
	$template = $this->GetTemplate();
	return $template->gid();
    }
1227 1228 1229
    function eid() {
	return (is_null($this->instance) ? -1 : $this->instance['eid']);
    }
1230 1231 1232 1233
    # This is what I should have called this:
    function id() {
	return $this->eid();
    }
1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252
    function uid() {
	return (is_null($this->instance) ? -1 : $this->instance['uid']);
    }
    function guid() {
	return (is_null($this->instance) ? -1 :
		$this->instance['parent_guid']);
    }
    function vers() {
	return (is_null($this->instance) ? -1 :
		$this->instance['parent_vers']);
    }
    function start_time() {
	return (is_null($this->instance) ? -1 :
		$this->instance['start_time']);
    }
    function stop_time() {
	return (is_null($this->instance) ? -1 :
		$this->instance['stop_time']);
    }
1253 1254 1255 1256
    function pause_time() {
	return (is_null($this->instance) ? -1 :
		$this->instance['pause_time']);
    }
1257 1258 1259 1260
    function description() {
	return (is_null($this->instance) ? -1 :
		$this->instance['description']);
    }
1261 1262 1263 1264
    function logfileid() {
	return (is_null($this->instance) ? -1 :
		$this->instance['logfileid']);
    }
1265 1266 1267
    function template() {
	return (is_null($this->instance) ? -1 : $this->template);
    }
1268 1269 1270 1271
    function GetTemplate() {
	return $this->template;
    }
    function GetExperiment() {
1272 1273 1274 1275 1276 1277
	if ($this->experiment)
	    return $this->experiment;

	if ($this->exptidx()) {
	    $this->experiment = Experiment::Lookup($this->exptidx());
	}
1278 1279
	return $this->experiment;
    }
1280 1281 1282
    function GetLogfile() {
	return Logfile::Lookup($this->logfileid());
    }
1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294

    #
    # Load the project object for an experiment.
    #
    function Project() {
	$pid = $this->pid();

	if (! ($project = Project::Lookup($pid))) {
	    TBERROR("Could not lookup project $pid!", 1);
	}
	return $project;
    }
1295

1296 1297
    # Is instance actually running (current experiment).
    function Instantiated() {
1298
	return ($this->GetExperiment() ? 1 : 0);
1299 1300
    }

1301 1302 1303 1304 1305 1306
    function AccessCheck($user, $access_type) {
	$template = $this->template;

	return $template->AccessCheck($user, $access_type);
    }

1307 1308 1309
    #
    # Show an instance.
    #
1310
    function Show($detailed, $withanno = 0, $showhidden = 0) {
1311
	$exptidx = $this->exptidx();
1312
	$runidx  = $this->runidx();
1313 1314 1315 1316 1317 1318
	$guid    = $this->guid();
	$vers    = $this->vers();
	$pid     = $this->pid();
	$uid     = $this->uid();
	$start   = $this->start_time();
	$stop    = $this->stop_time();
1319 1320 1321 1322 1323 1324 1325 1326
	$description = $this->description();
	$template    = $this->template();
	$pcount      = $template->ParameterCount();

	# Instance descriptions are metatdata that can be changed.
	# But use a naming convention. 
	$desc_metaname = "__instance_description_$exptidx";
	$desc_metadata = $template->LookupMetadataByName($desc_metaname);
1327

1328 1329 1330 1331
	if (! ($user = User::Lookup($uid))) {
	    TBERROR("Could not lookup object for user $uid", 1);
	}
	$showuser_url = CreateURL("showuser", $user);
1332 1333 1334 1335 1336 1337

	# If the instance is swapped in, we can provide a link to
	# experiment.
	if ($this->experiment) {
	    $showexp_url  = CreateURL("showexp", $this->experiment);
	}
1338

1339 1340 1341 1342 1343 1344 1345
	# If there are hidden runs, then add an option to show them
	$query_result =
	    DBQueryFatal("select count(*) from experiment_runs ".
			 "where exptidx='$exptidx' and hidden!=0");
	$row = mysql_fetch_array($query_result);
	$numhidden = $row[0];
	
1346 1347
	if ($detailed) {
	    SUBPAGESTART();
1348
	    SUBMENUSTART("Instance Options");
1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360
	    WRITESUBMENUBUTTON("Export Instance",
			       CreateURL("template_export", $this));

	    WRITESUBMENUBUTTON("Load Database",
			       CreateURL("template_analyze", $this));
	
	    WRITESUBMENUBUTTON("View Archive",
			       CreateURL("archive_view", $this));
	    
	    WRITESUBMENUBUTTON("Replay",
			       CreateURL("swapin", $template, 
					 "replay_instance_idx", $exptidx));
1361 1362 1363 1364 1365 1366 1367 1368 1369

	    if ($numhidden) {
		$tag = ($showhidden ? "Hide" : "Show");
		$val = ($showhidden ? 0 : 1);
		
		WRITESUBMENUBUTTON("$tag Hidden Runs",
				   CreateURL("instance_show", $this,
					     "showhidden", $val));
	    }
1370 1371
	    SUBMENUEND();
	}
1372
	
1373
	echo "<center>\n";
1374 1375 1376 1377 1378 1379
	if ($detailed && $pcount) {
	    echo "<table border=0 bgcolor=#000 color=#000 class=stealth ".
		 " cellpadding=0 cellspacing=0 align=center>\n";
            echo "<tr valign=top>";
	    echo "<td class=stealth align=center>\n";
	}
1380
	echo "<center>
1381
               <h3>Instance Details</h3>
1382 1383 1384 1385 1386 1387
             </center>\n";

	echo "<table align=center cellpadding=2 cellspacing=2 border=1>\n";
    
	ShowItem("Template",
		 MakeLink("template",
1388 1389
			  "guid=$guid&version=$vers", "$guid/$vers"));
	ShowItem("ID",          $exptidx);
1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420

	if ($description && $description != "") {
	    $onmouseover = MakeMouseOver($description);
	    if (strlen($description) > 40) {
		$description = substr($description, 0, 40) . " <b>... </b>";
	    }
	}
	else {
	    $onmouseover = "";
	    $description = "Click to Add";
	}

	# If no metadata, assume its for an instance prior to modifiable
	# instance descriptions.
	if ($desc_metadata) {
	    $desc_anchor = 
		MakeAnchor(CreateURL("template_metadata",
				     $template, $desc_metadata) .
			   "&action=modify",
			   $description, $onmouseover);
	}
	else {
	    $desc_anchor = 
		MakeAnchor(CreateURL("template_metadata", $template) .
			   "&action=add".
			   "&metadata_type=instance_description".
			   "&formfields[name]=${desc_metaname}",
			   $description, $onmouseover);
	}
        ShowItem("Description", $desc_anchor);
	
1421 1422 1423
	if ($this->experiment) {
	    ShowItem("Experiment",  MakeAnchor($showexp_url, $this->eid()));
	}
1424
	ShowItem("Project",     MakeLink("project", "pid=$pid", $pid));
1425
	ShowItem("Creator",     MakeAnchor($showuser_url, $uid));
1426 1427
	ShowItem("Started",     $start);
	ShowItem("Stopped",     (isset($stop) ? $stop : "&nbsp"));
1428
	ShowItem("Current Run", (isset($runidx) ? $runidx : "&nbsp"));
1429 1430
	echo "</table>\n";

1431 1432 1433 1434 1435 1436 1437 1438
	if ($detailed && $pcount) {
	    echo "</td>";
	    echo "<td align=center class=stealth> &nbsp &nbsp &nbsp </td>\n";
	    echo "<td class=stealth align=center>\n";
	    $this->ShowBindings();
	    echo "</tr>";
	    echo "</table>\n";
	}
1439 1440 1441
	if ($detailed) {
	    SUBPAGEEND();
	}
1442 1443 1444 1445
	if ($withanno) {
	    $this->ShowAnnotation();
	}

1446
	if ($detailed) {
1447
	    $this->ShowRunList(1, $showhidden);
1448
	}
1449

1450 1451
	echo "</center>\n";
	
1452
    }
1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465

    #
    # Page header; spit back some html for the typical page header.
    #
    function PageHeader() {
	$template = $this->template();	
	$exptidx  = $this->exptidx();
	$html     = $template->PageHeader();
	$guid     = $this->guid();
	$vers     = $this->vers();
	$eid      = $this->eid();

	$html .= "<font size=+2>, Instance <b>" .
1466
	    MakeLink("instance", "instance=$exptidx", $eid) .
1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502
	    "</b></font>";
	return $html;
    }

    #
    # A variant that points to the active experiment.
    #
    function ExpPageHeader() {
	$template = $this->template();	
	$html     = $template->PageHeader();
	$guid     = $this->guid();
	$vers     = $this->vers();
	$pid      = $this->pid();
	$eid      = $this->eid();

	$html .= "<font size=+2>, Instance <b>" .
	    MakeLink("project", "pid=$pid", $pid) . "/" .
	    MakeLink("experiment", "pid=$pid&eid=$eid", $eid) . 
	    "</b></font>";

	return $html;
    }
    
    #
    # Ditto for a run, although run should its own class.
    #
    function RunPageHeader($runidx) {
	$template = $this->template();	
	$exptidx  = $this->exptidx();
	$html     = $this->PageHeader();
	$guid     = $this->guid();
	$vers     = $this->vers();
	$eid      = $this->eid();
	$runid    = $this->GetRunID($runidx);

	$html .= "<font size=+2>, Run <b>" .
1503 1504
	    MakeAnchor(CreateURL("experimentrun_show", $this,
				 "runidx", $runidx), $runid) .
1505 1506 1507
	    "</b></font>";
	return $html;
    }
1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530

    #
    # Show the current bindings. This depends on the state of the instance.
    # If not swapped in, the instance bindings are current. If swapped in,
    # the most recent run bindings are current (current run, or last run).
    #
    function ShowCurrentBindings() {
	global $TB_EXPTSTATE_SWAPPED;
	
	if ($this->experiment) {
	    $experiment = $this->experiment;

	    if ($experiment->state() == $TB_EXPTSTATE_SWAPPED) {
		# Show the instance bindings is swapped.
		return $this->ShowBindings();
	    }
	    elseif ($this->runidx()) {
		return $this->ShowRunBindings();
	    }
	    return $this->ShowLastRunBindings();
	}
	return $this->ShowBindings();
    }
1531 1532 1533 1534 1535 1536
    
    #
    # Display instance bindings in a table
    #
    function ShowBindings() {
	$instance_idx = $this->idx();
1537
	$id = "${instance_idx}_bindings";
1538

1539 1540 1541
	$query_result =
	    DBQueryWarn("select * from experiment_template_instance_bindings ".
			"where instance_idx='$instance_idx'");
1542

1543 1544 1545
	if (!mysql_num_rows($query_result))
	    return 0;

1546
	AddSortedTable($id);