Browse Source

Add UI support for re-ZTP'ing devices.

Joe Clarke 4 years ago
parent
commit
e0434778b1
1 changed files with 65 additions and 2 deletions
  1. 65 2
      www/index.php

+ 65 - 2
www/index.php

@@ -63,8 +63,10 @@ if (isset($_REQUEST['prev_checked'])) {
     $c = (isset($_REQUEST['checked'])) ? $_REQUEST['checked'] : array();
     $as = $_REQUEST['assigned_switch'];
     $d = (isset($_REQUEST['delete'])) ? $_REQUEST['delete'] : array();
+    $rz = (isset($_REQUEST['reztp'])) ? $_REQUEST['reztp'] : array();
     foreach ($pc as $swsn => $value) {
         $curr_del = (isset($d[$swsn])) ? $d[$swsn] : 0;
+	$curr_rz = (isset($rz[$swsn])) ? $rz[$swsn] : 0;
         if ($curr_del == 1) {
             $sql = "DELETE FROM DEVICE_MAP WHERE serial_number=:sn";
             try {
@@ -77,6 +79,17 @@ if (isset($_REQUEST['prev_checked'])) {
             }
             continue;
         }
+	if ($curr_rz == 1) {
+	    $sql = "UPDATE DEVICE_MAP SET should_re_ztp=:rz WHERE serial_number=:sn";
+	    try {
+	        $sth = $dbh->prepare($sql);
+		$sth->execute(['sn' => $swsn, 'rz' => 2]);
+		$sth->closeCursor();
+		$logger->info("User {$ruser} set physical switch {$swsn} to re-ZTP");
+	    } catch (PDOException $e) {
+		array_push($errors, "Failed to set switch {$swsn} to re-ZTP: {$e->getMessage()}");
+	    }
+	}
         $curr_checked = (isset($c[$swsn])) ? $c[$swsn] : 0;
         if ($value != $curr_checked || $pas[$swsn] != $as[$swsn]) {
             $nc = $curr_checked;
@@ -323,6 +336,48 @@ print_header(TOOL_NAME.': Physical Switches');
       });
     }
 
+    function reztp_all() {
+        if ($('#reztp_all_box').is(':checked')) {
+		$('.reztp').each(function() {
+			$(this).prop('checked', true);
+		});
+	} else {
+		$('.reztp').each(function() {
+			$(this).prop('checked', false);
+		});
+	}
+
+	return true;
+    }
+
+    function toggle_reztp_all(elem) {
+	    if (!$(elem).is(':checked')) {
+		    $('#reztp_all_box').prop('checked', false);
+	    }
+    }
+
+    function check_confirm() {
+	    var delete_conf = true;
+	    var reztp_conf = true;
+	    if (deletes > 0) {
+		    delete_conf = confirm("Are you sure you want to delete these " + deletes + " physical switch(es)?");
+	    }
+
+	    $('.reztp').each(function() {
+		    if ($(this).is(':checked')) {
+			    one_checked = true;
+			    return;
+		    }
+	    });
+
+	    if (one_checked) {
+		    reztp_conf = confirm("Are you sure you want to re-ZTP these switches?");
+	    }
+
+	    return reztp_conf && delete_conf;
+    }
+
+
     $(document).ready(function() {
       $('#devtable').DataTable({
         "scrollY": "400px",
@@ -391,7 +446,7 @@ foreach ($pids as $lsp => $value) {
 	    <a href="<?=$base?>/logicsw.php">Logical Switches</a></td>
         </tr>
       </table>
-      <form name="mod_phys_switch_form" method="POST" action="<?=$_SERVER['PHP_SELF']?>" onSubmit='if (deletes > 0) { return confirm("Are you sure you want to delete these " + deletes + " physical switch(es)?"); }'>
+      <form name="mod_phys_switch_form" method="POST" action="<?=$_SERVER['PHP_SELF']?>" onSubmit='return check_confirm();'>
 <?php
 
     foreach (array_merge($_GET, $_POST) as $name => $value) {
@@ -429,6 +484,7 @@ foreach ($errors as $error) {
             <th class="headlink">Row No.</th>
             <th class="headlink">Delete?</th>
             <th class="headlink">Assigned?</th>
+            <th class="headlink">Re-ZTP? <input type="checkbox" id="reztp_all_box" onchange="return reztp_all();"></th>
             <th class="headlink">Serial Number</th>
             <th class="headlink">Product ID</th>
             <th class="headlink">Max Ports</th>
@@ -443,14 +499,21 @@ $i = 0;
 foreach ($switches as $row) {
     $sn = $row['serial_number'];
     $checked = false;
+    $will_rz = false;
     if ($row['checked_out'] == 1) {
         $checked = true;
-    } ?>
+    }
+
+    if ($row['should_re_ztp'] > 0) {
+	$will_rz = true;
+    }
+?>
           <tr>
             <td><?=$i + 1?>.</td>
             <td><input type="checkbox" name="delete[<?=$sn?>]" value="1" onClick="if (this.checked == true) { deletes++; } else { deletes--; }"></td>
 	    <td><input type="hidden" name="prev_checked[<?=$sn?>]" value="<?=$row['checked_out']?>">
                 <input id="checked_<?=$i?>" type="checkbox" name="checked[<?=$sn?>]" value="1" <?=($checked) ? 'checked' : ''?>></td>
+		<td><input id="reztp_<?=$i?>" class="reztp" type="checkbox" name="reztp[<?=$sn?>]" value="1" onchange="toggle_reztp_all(this);" <?=($will_rz) ? 'checked' : ''?>></td>
 	    <td><?=$row['serial_number']?></td>
             <td><?=$row['pid']?></td>
             <td><?=$row['max_ports']?></td>