profile_defs.php 21.3 KB
Newer Older
1 2
<?php
#
3
# Copyright (c) 2006-2016 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 23 24
# 
# {{{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/>.
# 
# }}}
#
#
25 26 27 28 29 30

function TBvalid_rspec($token) {
    return TBcheck_dbslot($token, "apt_profiles", "rspec",
			  TBDB_CHECKDBSLOT_WARN|TBDB_CHECKDBSLOT_ERROR);
}

31 32 33 34 35 36 37 38
class Profile
{
    var	$profile;
    var $project;

    #
    # Constructor by lookup on unique index.
    #
39 40
    function Profile($token, $version = null) {
	$safe_profileid = addslashes($token);
Leigh B Stoller's avatar
Leigh B Stoller committed
41
	
42
	if (preg_match("/^\w+\-\w+\-\w+\-\w+\-\w+$/", $token)) {
43 44 45 46 47
	    #
	    # First look to see if the uuid is for the profile itself,
	    # which means current version. Otherwise look for a
	    # version with the uuid.
	    #
48
	    $query_result =
49 50
		DBQueryWarn("select i.*,v.*,i.uuid as profile_uuid, ".
                            "    i.disabled as profile_disabled ".
51 52 53 54 55 56 57 58
			    "  from apt_profiles as i ".
			    "left join apt_profile_versions as v on ".
			    "     v.profileid=i.profileid and ".
			    "     v.version=i.version ".
			    "where i.uuid='$token' and v.deleted is null");

	    if (!$query_result || !mysql_num_rows($query_result)) {
		$query_result =
59 60
		    DBQueryWarn("select i.*,v.*,i.uuid as profile_uuid, ".
                                "    i.disabled as profile_disabled ".
61 62 63 64 65 66
				"  from apt_profile_versions as v ".
				"left join apt_profiles as i on ".
				"     v.profileid=i.profileid ".
				"where v.uuid='$token' and ".
				"      v.deleted is null");
	    }
Leigh B Stoller's avatar
Leigh B Stoller committed
67
	}
68
	elseif (is_null($version)) {
Leigh B Stoller's avatar
Leigh B Stoller committed
69
	    $query_result =
70 71
		DBQueryWarn("select i.*,v.*,i.uuid as profile_uuid, ".
                            "    i.disabled as profile_disabled ".
72
			    "  from apt_profiles as i ".
73 74 75 76
			    "left join apt_profile_versions as v on ".
			    "     v.profileid=i.profileid and ".
			    "     v.version=i.version ".
			    "where i.profileid='$safe_profileid'");
Leigh B Stoller's avatar
Leigh B Stoller committed
77
	}
78 79 80
	else {
	    $safe_version = addslashes($version);
	    $query_result =
81 82
	        DBQueryWarn("select i.*,v.*,i.uuid as profile_uuid, ".
                            "    i.disabled as profile_disabled ".
83
			    "  from apt_profile_versions as v ".
84 85 86 87 88
			    "left join apt_profiles as i on ".
			    "     i.profileid=v.profileid ".
			    "where v.profileid='$safe_profileid' and ".
			    "      v.version='$safe_version' and ".
			    "      v.deleted is null");
Leigh B Stoller's avatar
Leigh B Stoller committed
89
	}
90 91 92 93 94 95 96 97 98 99 100 101 102 103
	if (!$query_result || !mysql_num_rows($query_result)) {
	    $this->profile = null;
	    return;
	}
	$this->profile = mysql_fetch_array($query_result);

	# Load lazily;
	$this->project    = null;
    }
    # accessors
    function field($name) {
	return (is_null($this->profile) ? -1 : $this->profile[$name]);
    }
    function name()	    { return $this->field('name'); }
104 105
    function profileid()    { return $this->field('profileid'); }
    function version()      { return $this->field('version'); }
106 107
    function creator()	    { return $this->field('creator'); }
    function creator_idx()  { return $this->field('creator_idx'); }
108 109
    function updater()	    { return $this->field('updater'); }
    function updater_idx()  { return $this->field('updater_idx'); }
110 111 112
    function pid()	    { return $this->field('pid'); }
    function pid_idx()	    { return $this->field('pid_idx'); }
    function created()	    { return $this->field('created'); }
113 114
    function published()    { return $this->field('published'); }
    function deleted()	    { return $this->field('deleted'); }
115
    function uuid()	    { return $this->field('uuid'); }
116
    function profile_uuid() { return $this->field('profile_uuid'); }
117 118 119 120
    function ispublic()	    { return $this->field('public'); }
    function shared()	    { return $this->field('shared'); }
    function listed()	    { return $this->field('listed'); }
    function rspec()	    { return $this->field('rspec'); }
121
    function script()	    { return $this->field('script'); }
122
    function paramdefs()    { return $this->field('paramdefs'); }
123 124
    function locked()	    { return $this->field('status'); }
    function status()	    { return $this->field('locked'); }
125
    function topdog()	    { return $this->field('topdog'); }
126 127
    function disabled()	    { return $this->field('disabled'); }
    function profile_disabled()    { return $this->field('profile_disabled'); }
128 129
    function parent_profileid()    { return $this->field('parent_profileid'); }
    function parent_version()      { return $this->field('parent_version'); }
130 131 132 133 134

    # Private means only in the same project.
    function IsPrivate() {
	return !($this->ispublic() || $this->shared());
    }
135 136 137 138
    # PP profiles have parameter defs.
    function isParameterized() {
	return ($this->paramdefs() != "" ? 1 : 0);
    }
139 140 141 142
    # A profile is disabled if version is disabled or entire profile is disabled
    function isDisabled() {
	return ($this->disabled() || $this->profile_disabled());
    }
143 144 145 146 147 148 149
    
    # Hmm, how does one cause an error in a php constructor?
    function IsValid() {
	return !is_null($this->profile);
    }

    # Lookup up a single profile by idx. 
150 151
    function Lookup($token, $version = null) {
	$foo = new Profile($token, $version);
152 153 154 155 156 157 158 159

	if ($foo->IsValid()) {
            # Insert into cache.
	    return $foo;
	}	
	return null;
    }

160
    function LookupByName($project, $name, $version = null) {
161 162 163 164 165 166
        if (is_object($project)) {
            $pid = $project->pid();
        }
        else {
            $pid = addslashes($project);
        }
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
	$safe_name = addslashes($name);

	if (preg_match("/^\w+\-\w+\-\w+\-\w+\-\w+$/", $name)) {
	    return Profile::Lookup($name);
	}
	elseif (is_null($version)) {
	    $query_result =
		DBQueryWarn("select i.profileid,i.version ".
			    "  from apt_profiles as i ".
			    "left join apt_profile_versions as v on ".
			    "     v.profileid=i.profileid and ".
			    "     v.version=i.version ".
			    "where i.pid='$pid' and ".
			    "      i.name='$safe_name'");
	}
	else {
	    $safe_version = addslashes($version);
	    $query_result =
185
		DBQueryWarn("select i.profileid,v.version ".
186 187 188 189 190 191 192
			    "  from apt_profiles as i ".
			    "left join apt_profile_versions as v on ".
			    "     v.profileid=i.profileid ".
			    "where i.pid='$pid' and ".
			    "      i.name='$safe_name' and ".
			    "      v.version='$safe_version'");
	}
193 194
	if ($query_result && mysql_num_rows($query_result)) {
	    $row = mysql_fetch_row($query_result);
195
	    return Profile::Lookup($row[0], $row[1]);
196 197 198
	}
	return null;
    }
199

200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
    #
    # Lookup the most recently published version of a profile.
    #
    function LookupMostRecentPublished() {
	$profileid = $this->profileid();

	$query_result = 
	    DBQueryWarn("select version from apt_profile_versions as v ".
			"where v.profileid='$profileid' and ".
			"      published is not null and ".
			"      deleted is null ".
			"order by published desc limit 1");

	if (!$query_result || !mysql_num_rows($query_result)) {
	    return null;
	}
	$row = mysql_fetch_row($query_result);
	return Profile::Lookup($profileid, $row[0]);
    }

220 221 222 223 224 225 226
    #
    # Refresh an instance by reloading from the DB.
    #
    function Refresh() {
	if (! $this->IsValid())
	    return -1;

227 228
	$profileid = $this->profileid();
	$version   = $this->version();
229 230

	$query_result =
231 232
	    DBQueryWarn("select * from apt_profile_versions ".
			"where profileid='$profileid' and version='$version'");
233 234 235 236 237 238 239 240 241 242
    
	if (!$query_result || !mysql_num_rows($query_result)) {
	    $this->profile    = NULL;
	    $this->project    = null;
	    return -1;
	}
	$this->profile    = mysql_fetch_array($query_result);
	$this->project    = null;
	return 0;
    }
Leigh B Stoller's avatar
Leigh B Stoller committed
243 244

    #
245
    # URL. To the specific version of the profile.
Leigh B Stoller's avatar
Leigh B Stoller committed
246 247
    #
    function URL() {
248
        global $APTBASE, $ISVSERVER, $ISAPT;
Leigh B Stoller's avatar
Leigh B Stoller committed
249 250 251
	
	$uuid = $this->uuid();

252
	if ($this->ispublic() || (!$ISAPT && $this->shared())) {
253 254
	    $pid  = $this->pid();
	    $name = $this->name();
255
	    $vers = $this->version();
256 257 258 259 260 261 262 263 264 265 266 267 268
	    if ($ISVSERVER)
		return "$APTBASE/p/$pid/$name/$vers";
	    return "$APTBASE/instantiate.php?profile=$name".
		"&project=$pid&version=$vers";
	}
	else {
	    if ($ISVSERVER)
		return "$APTBASE/p/$uuid";	    
	    return "$APTBASE/instantiate.php?profile=$uuid";
	}
    }
    # And the URL of the profile itself.
    function ProfileURL() {
269
        global $APTBASE, $ISVSERVER, $ISAPT;
270 271 272
	
	$uuid = $this->profile_uuid();

273
	if ($this->ispublic() || (!$ISAPT && $this->shared())) {
274 275 276 277 278
	    $pid  = $this->pid();
	    $name = $this->name();
	    if ($ISVSERVER)
		return "$APTBASE/p/$pid/$name";
	    return "$APTBASE/instantiate.php?profile=$name&project=$pid";
279 280
	}
	else {
281 282 283
	    if ($ISVSERVER)
		return "$APTBASE/p/$uuid";	    
	    return "$APTBASE/instantiate.php?profile=$uuid";
284
	}
Leigh B Stoller's avatar
Leigh B Stoller committed
285
    }
286 287 288 289 290 291 292 293 294

    #
    # Is this profile the highest numbered version.
    # 
    function IsHead() {
	$profileid = $this->profileid();

	$query_result =
	    DBQueryWarn("select max(version) from apt_profile_versions ".
295
			"where profileid='$profileid' and deleted is null");
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
	if (!$query_result || !mysql_num_rows($query_result)) {
	    return -1;
	}
	$row = mysql_fetch_row($query_result);
	return ($this->version() == $row[0] ? 1 : 0);
    }
    #
    # Does this profile have more then one version (history).
    # 
    function HasHistory() {
	$profileid = $this->profileid();

	$query_result =
	    DBQueryWarn("select count(*) from apt_profile_versions ".
			"where profileid='$profileid'");
	if (!$query_result || !mysql_num_rows($query_result)) {
	    return -1;
	}
	$row = mysql_fetch_row($query_result);
	return ($row[0] > 1 ? 1 : 0);
    }
    #
    # A profile can be published if it is a > version then the most
    # recent published profile. 
    # 
    function CanPublish() {
	$profileid = $this->profileid();

	# Already published. Might support unpublish at some point.
	if ($this->published())
	    return 0;

	$query_result =
	    DBQueryWarn("select version from apt_profile_versions ".
			"where profileid='$profileid' and ".
			"      published is not null ".
			"order by version desc limit 1");
	if (!$query_result || !mysql_num_rows($query_result)) {
	    return -1;
	}
	$row = mysql_fetch_row($query_result);
	$vers = $row[0];
	
	return ($this->version() > $row[0] ? 1 : 0);
    }
    #
    # A profile can be modified if it is a >= version then the most
    # recent published profile. 
    # 
    function CanModify() {
	$profileid = $this->profileid();

	$query_result =
	    DBQueryWarn("select version from apt_profile_versions ".
			"where profileid='$profileid' and ".
			"      published is not null ".
			"order by version desc limit 1");
	if (!$query_result || !mysql_num_rows($query_result)) {
	    return -1;
	}
	$row = mysql_fetch_row($query_result);
	$vers = $row[0];
	
	return ($this->version() >= $row[0] ? 1 : 0);
    }
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
    #
    # Has a profile been instantiated?
    #
    function HasActivity() {
	$profileid = $this->profileid();

	$query_result =
	    DBQueryWarn("select count(h.uuid) from apt_instance_history as h ".
			"where h.profile_id='$profileid'");

	if (!$query_result) {
	    return 0;
	}
	if (mysql_num_rows($query_result)) {
	    $row = mysql_fetch_row($query_result);
	    if ($row[0] > 0) {
		return 1;
	    }
	}
	$query_result =
	    DBQueryWarn("select count(uuid) from apt_instances ".
			"where profile_id='$profileid'");

	if (!$query_result) {
	    return 0;
	}
	if (mysql_num_rows($query_result)) {
	    $row = mysql_fetch_row($query_result);
	    if ($row[0] > 0) {
		return 1;
	    }
	}
	return 0;
    }
395 396 397 398 399 400 401 402

    #
    # Permission check; does user have permission to instantiate the
    # profile. At the moment, view/instantiate are the same.
    #
    function CanInstantiate($user) {
	$profileid = $this->profileid();

403 404 405
        if (ISADMIN()) {
            return 1;
        }
406 407
	if ($this->shared() || $this->ispublic() ||
	    $this->creator_idx() == $user->uid_idx()) {
408 409 410
	    return 1;
	}
	# Otherwise a project membership test.
411
	$project = Project::Lookup($this->pid_idx());
412 413 414 415 416 417 418 419 420 421 422 423
	if (!$project) {
	    return 0;
	}
	$isapproved = 0;
	if ($project->IsMember($user, $isapproved) && $isapproved) {
	    return 1;
	}
	return 0;
    }
    function CanView($user) {
	return $this->CanInstantiate($user);
    }
Leigh B Stoller's avatar
Leigh B Stoller committed
424 425 426
    function CanClone($user) {
	return $this->CanInstantiate($user);
    }
427 428 429 430 431
    function CanEdit($user) {
        if ($this->creator_idx() == $user->uid_idx() || ISADMIN())
	    return 1;
        return 0;
    }
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
    function CanDelete($user) {
	# Want to know if the project is APT or Cloud/Emulab. APT projects
        # may not delete profiles (yet).
	$project = Project::Lookup($this->pid_idx());
	if (!$project) {
	    return 0;
	}
        if (!$this->IsHead()) {
            return 0;
        }
        if (ISADMIN() || STUDLY()) {
            return 1;
        }
        if (!$project->isAPT()) {
            return 1;
        }
        # APT profiles may not be deleted if published.
        if (!$this->published()) {
            return 1;
        }
        return 0;
    }
454

455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
    function UsageInfo($user) {
        $profile_id  = $this->profileid();
        $userclause  = "";

        if ($user) {
            $creator_idx = $user->idx();
            $userclause  = "and creator_idx='$creator_idx' ";
        }

        #
        # This is last used.
        #
        $query_result =
            DBQueryFatal("select max(UNIX_TIMESTAMP(created)) ".
                         "  from apt_instances ".
                         "where profile_id='$profile_id' ".
                         $userclause);
        $row = mysql_fetch_row($query_result);
        if (!$row[0]) {
            $query_result =
                DBQueryFatal("select max(UNIX_TIMESTAMP(created)) ".
                             "  from apt_instance_history ".
                             "where profile_id='$profile_id' ".
                             $userclause);
            $row = mysql_fetch_row($query_result);
        }
        if (!$row[0]) {
            return array(0, 0);
        }
        $lastused = $row[0];

        #
        # Now we want number of times used.
        #
        $count = 0;
        $query_result =
            DBQueryFatal("select ".
                         "(select count(profile_id) ".
                         "   from apt_instances ".
                         " where profile_id='$profile_id' ".
                           $userclause . ") as count1, ".
                         "(select count(profile_id) ".
                         "   from apt_instance_history ".
                         " where profile_id='$profile_id' ".
                           $userclause . ") as count2");
        if (mysql_num_rows($query_result)) {
            $row   = mysql_fetch_row($query_result);
            $count = ($row[0] ? $row[0] : 0) + ($row[1] ? $row[1] : 0);
        }
        return array($lastused, $count);
    }

    function isFavorite($user) {
        if (!$user) {
            return 0;
        }
        $profile_id  = $this->profileid();
        $user_idx    = $user->idx();

        $query_result =
            DBQueryFatal("select * from apt_profile_favorites ".
                         "where uid_idx='$user_idx' and ".
                         "      profileid='$profile_id'");

        return mysql_num_rows($query_result);
    }

    function MarkFavorite($user) {
        $profile_id  = $this->profileid();
        $user_uid    = $user->uid();
        $user_idx    = $user->idx();

        if (!DBQueryWarn("replace into apt_profile_favorites set ".
                         "  uid='$user_uid',uid_idx='$user_idx', ".
529
                         "  profileid='$profile_id',marked=now()")) {
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
            return -1;
        }
        return 0;
    }

    function ClearFavorite($user) {
        $profile_id  = $this->profileid();
        $user_uid    = $user->uid();
        $user_idx    = $user->idx();

        if (!DBQueryWarn("delete from apt_profile_favorites ".
                         "where uid_idx='$user_idx' and ".
                         "      profileid='$profile_id'")) {
            return -1;
        }
        return 0;
    }

548 549 550 551 552
    function BestAggregate($rspec = null) {
	if (!$rspec) {
	    $rspec = $this->rspec();
	}
	$parsed_xml = simplexml_load_string($rspec);
553 554 555 556 557 558 559 560

	foreach ($parsed_xml->node as $node) {
	    # No XEN VMs on Cloudlab yet.
	    if ($node->sliver_type &&
		$node->sliver_type["name"] &&
		$node->sliver_type["name"] == "emulab-xen") {
		return "Utah APT";
	    }
561
	    if ($node->hardware_type &&
562 563 564 565 566 567 568 569 570 571 572
		$node->hardware_type["name"]) {
                if ($node->hardware_type["name"] == "m400") {
                    return "Utah Cloudlab";
                }
                elseif ($node->hardware_type["name"] == "dl360") {
                    return "Utah DDC";
                }
                elseif ($node->hardware_type["name"] == "r320" ||
                        $node->hardware_type["name"] == "c6220") {
                    return "Utah APT";
                }
573
	    }
574 575 576 577 578 579 580
	    # Check URL
	    if (! ($node->sliver_type &&
		   $node->sliver_type->disk_image &&
		   ($node->sliver_type->disk_image["url"] ||
		    $node->sliver_type->disk_image["name"]))) {
		continue;
	    }
581
            
582 583
	    if ($node->sliver_type->disk_image["name"]) {
		$name = $node->sliver_type->disk_image["name"];
584 585 586 587 588 589 590
		if (preg_match("/^http/", $name)) {
                    $url = $name;
                }
                else {
                    #
                    # The only image that runs on Cloudlab is UBUNTU14-64-ARM
                    #
591
                    if (preg_match("/ARM/", $name) ||
592
		        preg_match("/HPC/", $name) ||
593
		        preg_match("/OSCNF/", $name)) {
594 595 596 597
                        return "Utah Cloudlab";
                    }
                    return "Utah APT";
                }
598
	    }
599 600 601 602 603 604 605 606 607 608 609
            else {
                $url = $node->sliver_type->disk_image["url"];
            }
            if (preg_match("/utah\.cloudlab\.us/", $url)) {
                return "Utah Cloudlab";
            }
            if (preg_match("/emulab\.net/", $url) ||
                preg_match("/geniracks\.net/", $url) ||
                preg_match("/instageni/", $url)) {
                return "Utah APT";
            }
610 611 612
	}
	return null;
    }
613 614 615 616 617 618 619 620 621

    function GenerateFormFragment() {
	$json_data = $this->paramdefs();
	
	if (!$json_data || $json_data == "") {
	    return "";
	}
	$fields    = json_decode($json_data);
	$defaults  = array();
622 623 624
	$formBasic    = "";
	$formAdvanced = "";
	$formGroups   = "";
625 626

	while (list ($name, $val) = each ($fields)) {
627
	    $form    = "";
628 629 630 631
	    $type    = $val->type;
	    $prompt  = $val->description;
	    $defval  = $val->defaultValue;
	    $options = $val->legalValues;
632 633 634 635 636 637 638
	    $longhelp  = $val->longDescription;
	    $advanced  = $val->advanced;
	    $groupId   = $val->groupId;
	    $groupName = $val->groupName;
	    $hasGroup = false;
	    $data_help_string = "";
	    $advanced_attr = "";
639 640 641 642 643

	    $defaults[$name] = $defval;
	    if (!isset($prompt) || !$prompt) {
		$prompt = $name;
	    }
644 645 646 647 648 649 650 651 652 653 654 655 656 657
	    if (!isset($advanced)) {
		$advanced = false;
	    }
            # Let advanced-tagged params dominate groupId; we don't generate groupId yet anyway.
	    if ($advanced) {
		$advanced_attr = " pp-param-group='advanced' pp-param-group-name='Advanced Parameters'";
	    }
	    else if (isset($groupId) && $groupId && isset($groupName) && $groupName) {
		$advanced_attr = " pp-param-group='$groupId' pp-param-group-name='$groupName'";
		$hasGroup = true;
	    }
	    if (isset($longhelp) && $longhelp) {
		$data_help_string = "data-help='$longhelp'";
	    }
658 659 660 661 662

	    if ($type == "boolean") {
		$form .=
		    "<input name='$name' ".
		    "      <%- formfields.${name} %> ".
663 664
		    "      style='margin: 0px; height: 34px;' ".
		    "      class='format-me' ".
665 666
		    "      data-key='$name' ".
		    "      data-label='$prompt' ".
667
		    "      $data_help_string $advanced_attr".
668
		    "      value='checked' ".
669
		    "      type='checkbox'>";
670 671 672 673 674 675 676 677 678 679 680 681 682
		if ($defval) {
		    $defaults[$name] = "checked";
		}
		else {
		    $defaults[$name] = "";
		}
	    }
	    elseif ($options) {
		$form .=
		    "<select name='$name' ".
		    "       class='form-control format-me' ".
		    "       data-key='$name' ".
		    "       data-label='$prompt' ".
683
		    "       $data_help_string $advanced_attr".
684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
		    "       placeholder='Please Select'> ";
		foreach ($options as $option) {
		    if (gettype($option) == "array") {
			$oval = $option[0];
			$okey = $option[1];
		    }
		    else {
			$okey = $oval = $option;
		    }
		    $form .= "<option";
		    $form .= 
			"<% if (_.has(formfields, '$name') && ".
			"       formfields.${name} == '$oval') { %> ".
			"   selected ".
			"<% } %> ".
			"value='$oval'>$okey</option>";
		}
		$form .= "</select>";
	    }
	    else {
		$form .=
		    "<input name='$name' ".
		    "value='<%- formfields.${name} %>' ".
		    "class='form-control format-me' ".
		    "data-key='$name' ".
		    "data-label='$prompt' ".
710
		    "$data_help_string $advanced_attr".
711 712
		    "type='text'>";
	    }
713 714 715 716 717 718 719 720 721 722

	    if ($advanced) {
		$formAdvanced .= $form;
	    }
	    else if ($hasGroup) {
		$formGroups .= $form;
	    }
	    else {
		$formBasic .= $form;
	    }
723
	}
724 725 726 727

	$finalForm = $formBasic . $formAdvanced . $formGroups;

	return array($finalForm, $defaults);
728
    }
729 730
}
?>