template_editevents.php 7.27 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 27 28 29
#
include("defs.php3");
include_once("template_defs.php");

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

34 35 36 37 38 39 40 41 42 43 44 45 46 47
#
# Verify page arguments.
#
$reqargs = RequiredPageArguments("template",   PAGEARG_TEMPLATE);

# Need these below.
$guid = $template->guid();
$vers = $template->vers();

if (! $template->AccessCheck($this_user, $TB_EXPT_UPDATE)) {
    USERERROR("You do not have permission to instantiate experiment template ".
	      "$guid/$vers!", 1);
}

48 49 50 51 52 53 54 55 56 57 58 59
#
# Spit the form out using the array of data.
#
function SPITFORM($template, $formfields, $errors)
{
    PAGEHEADER("Edit Template Events");

    if ($template->EventList($eventlist) != 0) {
	TBERROR("Could not get eventlist for template!", 1);
    }

    $guid = $template->guid();
60
    $vers = $template->vers();
61 62

    echo $template->PageHeader();
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
    echo "<br>\n";
    
    echo "<center>\n";
    $template->Show();
    echo "</center>\n";
    echo "<br>\n";

    if (!count($eventlist)) {
	echo "<center>";
	echo "<font size=+1 color=red>This template has no events!</font>\n";
	echo "</center>\n";
	return;
    }

    if ($errors) {
	echo "<table class=nogrid
                     align=center border=0 cellpadding=6 cellspacing=0>
              <tr>
                 <th align=center colspan=2>
                   <font size=+1 color=red>
                      &nbsp;Oops, please fix the following errors!&nbsp;
                   </font>
                 </td>
              </tr>\n";

	while (list ($name, $message) = each ($errors)) {
89 90
            # XSS prevention.
	    $message = CleanString($message);
91 92 93 94 95 96 97 98 99
	    echo "<tr>
                     <td align=right>
                       <font color=red>$name:&nbsp;</font></td>
                     <td align=left>
                       <font color=red>$message</font></td>
                  </tr>\n";
	}
	echo "</table><br>\n";
    }
100 101 102 103
    # XSS prevention.
    while (list ($key, $val) = each ($formfields)) {
	$formfields[$key] = CleanString($val);
    }
104

105 106 107
    $url = CreateURL("template_editevents", $template);
    
    echo "<form action='$url'  method=post>\n";
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
    echo "<table align=center border=1>\n";

    echo "<tr>";
    echo "<th align=center>Delete</th>\n";
    echo "<th align=center>Time</th>\n";
#    echo "<th align=center>Name</th>\n";
    echo "<th align=center>Node</th>\n";
    echo "<th align=center>Args</th>\n";
    echo "</tr><tr></tr>\n";

    #
    # Show a table of events with a delete button named by the event.
    #
    while (list ($index, $dbrow) = each ($eventlist)) {
	$vname       = $dbrow["vname"];
	$vnode       = $dbrow["vnode"];
	$delete_name = "delete_${vname}";
	$time_name   = "time_${vname}";
	$args_name   = "args_${vname}";
	    
	echo "<tr>";
	echo "<td align=center><input type=checkbox value=checked
                         name=\"formfields[$delete_name]\"
131
                         " . $formfields["$delete_name"] . ">
132 133 134 135 136
              </td>\n";
	
	echo "<td class='pad4'>
                  <input type=text
                         name=\"formfields[$time_name]\"
137
                         value=\"" . $formfields["$time_name"] . "\"
138 139 140 141 142 143 144 145 146 147
	                 size=10
                         maxlength=10>
              </td>\n";

#	echo "<td class='pad4'>$vname</td>\n";
	echo "<td class='pad4'>$vnode</td>\n";

	echo "<td class='pad4'>
                  <input type=text
                         name=\"formfields[$args_name]\"
148
                         value=\"" . $formfields["$args_name"] . "\"
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
	                 size=64
                         maxlength=1024>
              </td>\n";

	echo "</tr>\n";
    }
    echo "<tr>
              <td class='pad4' align=center colspan=4>
                <b><input type=submit name=save value='Save Changes'></b>
              </td>
         </tr>
        </form>
        </table>\n";
}

# Event list master, used below.
if ($template->EventList($eventlist) != 0) {
    TBERROR("Could not get eventlist for template!", 1);
}

#
# On first load, display virgin form and exit.
#
if (!isset($save)) {
    $defaults = array();

    while (list ($index, $dbrow) = each ($eventlist)) {
	$vname       = $dbrow["vname"];
	$time        = $dbrow["time"];
	$arguments   = $dbrow["arguments"];
	$delete_name = "delete_${vname}";
	$time_name   = "time_${vname}";
	$args_name   = "args_${vname}";

183 184 185
	$defaults[$delete_name] = "";
	$defaults[$time_name]   = $time;
	$defaults[$args_name]   = $arguments;
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
    }
    
    SPITFORM($template, $defaults, 0);
    PAGEFOOTER();
    return;
}
elseif (! isset($formfields)) {
    PAGEARGERROR();
}

#
# Okay, validate form arguments.
#
$errors  = array();
$deletes = array();
$changes = array();

#
# Use the master event list from the template to validate.
#
while (list ($index, $dbrow) = each ($eventlist)) {
    $vname       = $dbrow["vname"];
    $time        = $dbrow["time"];
    $args        = $dbrow["args"];
    $delete_name = "delete_${vname}";
    $time_name   = "time_${vname}";
    $args_name   = "args_${vname}";

    # Delete button
    if (isset($formfields[$delete_name]) &&
	$formfields[$delete_name] == "checked") {
	
	$deletes[$vname] = $vname;
	continue;
    }
    
    # Modify the time.
    if (isset($formfields[$time_name]) && $formfields[$time_name] != "$time") {
	$newtime = $formfields[$time_name];
	
	if (!TBvalid_float($newtime)) {
	    $errors["$vname time"] = TBFieldErrorString();
	}
	elseif ($newtime < 0) {
	    $errors["$vname time"] = "Cannot be less then zero";
	}
	else {
	    if (!isset($changes[$vname])) {
		$changes[$vname] = array();
	    }
	    $changes[$vname]["time"] = $newtime;
	}
    }

    # Modify the arguments
    if (isset($formfields[$args_name]) && $formfields[$args_name] != "$args") {
	$newargs = $formfields[$args_name];

	if (! TBcheck_dbslot($newargs, 'eventlist','arguments',
			     TBDB_CHECKDBSLOT_WARN|TBDB_CHECKDBSLOT_ERROR)) {
	    $errors["$vname time"] = TBFieldErrorString();
	}
	else {
	    if (!isset($changes[$vname])) {
		$changes[$vname] = array();
	    }
	    $changes[$vname]["arguments"] = $newargs;
	}
    }
}

if (count($errors)) {
    SPITFORM($template, $formfields, $errors);
    PAGEFOOTER();
    exit(1);
}

#
# Do the deletes and changes.
#
reset($eventlist);

while (list ($index, $dbrow) = each ($eventlist)) {
    $vname       = $dbrow["vname"];

    if (isset($deletes[$vname])) {
	$template->DeleteEvent($vname);
	continue;
    }
    if (isset($changes[$vname])) {
	$template->ModifyEvent($vname, $changes[$vname]);
	continue;
    }
}

# Zap back to this page.
282
header("Location: ".  CreateURL("template_editevents.php", $template));
283 284

?>