Sindbad~EG File Manager

Current Path : /home/beeson/public_html/WebMathXpert/ThreeBodyWeb/
Upload File :
Current File : //home/beeson/public_html/WebMathXpert/ThreeBodyWeb/BrowseThreeBody.php

<?php
// BrowseThreeBody.php
session_start();
$dbFile = 'ThreeBodyDatabase.db';
$db = new SQLite3($dbFile);

// Fetch simulation documents.
$result = $db->query("SELECT * FROM documents ORDER BY date_created DESC");
$documents = [];
while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
    $documents[] = $row;
}

// For each document, fetch associated bodies.
$documentsData = [];
foreach ($documents as $doc) {
    $docId = $doc['DocumentNumber'];
    $stmt = $db->prepare("SELECT mass, color, r, x0, y0, p0, q0 FROM bodies WHERE DocumentNumber = :doc");
    $stmt->bindValue(':doc', $docId, SQLITE3_INTEGER);
    $resultBodies = $stmt->execute();
    $bodiesArray = [];
    while ($bodyRow = $resultBodies->fetchArray(SQLITE3_ASSOC)) {
        $bodiesArray[] = $bodyRow;
    }
    $doc['bodies'] = $bodiesArray; 
    // Fetch document-level data
    $stmt = $db->prepare("SELECT integrationMethod, tmax, caption FROM documents WHERE DocumentNumber = :doc");
    $stmt->bindValue(':doc', $docId, SQLITE3_INTEGER);
    $resultDoc = $stmt->execute();
    $docRow = $resultDoc->fetchArray(SQLITE3_ASSOC);
    if ($docRow) {
        $doc['integrationMethod'] = $docRow['integrationMethod'];
        $doc['tmax'] = $docRow['tmax'];
        $doc['caption'] = $docRow['caption'];
    }
    
    $documentsData[$docId] = $doc;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Browse Gallery</title>
  <style>
    body {
      background-color: #e6f2ff;
      font-family: Arial, sans-serif;
      margin: 10px;
      max-width: 100%;
      overflow-x: hidden;
    }
    .gallery {
      display: flex;
      flex-wrap: wrap;
      gap: 10px;
    }
	 .thumb-container {
	   width: 150px;
	   cursor: pointer;
	   background-color: cornsilk;
	   display: flex;
	   flex-direction: column;
	   align-items: center;
	 }

	 .thumb {
	   width: 150px;
	   height: 100px;
	   object-fit: cover;
	   border: 2px solid transparent;
	   transition: border 0.3s;
	 }

	 .caption {
	   font-size: 0.9em;
	   color: #333;
	   text-align: center;
	   white-space: nowrap;
	   overflow: hidden;
	   text-overflow: ellipsis;
	   width: 150px;
	   background-color: #e6f2ff;
	   padding: 2px 4px;
	   margin-top: 1px;
	   box-sizing: border-box;
	 }
    .doc-number {
      position: absolute;
      top: 2px;
      left: 2px;
      background: rgba(0, 0, 0, 0.5);
      color: #fff;
      font-size: 0.8em;
      padding: 1px 4px;
      border-radius: 2px;
      z-index: 2;
    }
    .fixed-buttons {
      position: fixed;
      bottom: 10px;
      left: 10px;
    }
    .fixed-buttons button {
      padding: 10px 20px;
      margin-right: 10px;
      font-size: 16px;
      cursor: pointer;
      background-color: #00008B; /* dark blue */
      color: #fff;            /* white text */
      border: none;
      border-radius: 4px;
    }
    .fixed-buttons button:hover {
      background-color: #0000a0;
    }
	 .caption {
	     font-size: 0.9em;
	     color: #333;
	     margin-top: 4px;
	     text-align: center;
	     white-space: nowrap;
	     overflow: hidden;
	     text-overflow: ellipsis;
	     max-width: 100%;
	 }
  </style>
  <script>
    // Embed documentsData for use in JavaScript.
    window.documentsData = <?php echo json_encode($documentsData); ?>;
    let selectedDocId = null;
    function selectDocument(docId) {
      selectedDocId = docId;
      // Remove selection highlight from all thumbnails.
      document.querySelectorAll('.thumb').forEach(img => {
        img.classList.remove('selected');
      });
      document.getElementById('doc-' + docId).classList.add('selected'); 
      postSimulation('solve');    
      // as of 3.25.25  there is no more 'selection' and then click a button 
		// instead, clicking the thumbnail directly leads (indirectly) to DisplaySolution.php
    }
    function postSimulation(action) { 
       // selectedDodId must be valid when this is called 
       // action should be 'edit' or 'solve'
      const docData = window.documentsData[selectedDocId];
      if (!docData) {
        alert("Saved problem data not found with ", selectedDocId);
        return;
      }
      // Build a hidden form.
      const form = document.createElement("form");
      form.method = "post"; 
      form.action = "ThreeBody.php?indatabase=true";
      
      // document-level fields.
      const fields = ["nbodies", "caption", "image_filename", "tmax", "integrationMethod"];
      fields.forEach(field => {
        const input = document.createElement("input");
        input.type = "hidden";
        input.name = field;  
        input.value = (docData[field] !== null && docData[field] !== undefined) ? docData[field] : ""; 
        form.appendChild(input);
      });
      
      // Populate body data as arrays (e.g., mass[], color[], etc.).
      const bodyFields = ["mass", "color", "r", "x0", "y0", "p0", "q0"];
      if (docData.bodies && docData.bodies.length > 0) {
        docData.bodies.forEach(body => {
          bodyFields.forEach(field => {
            const input = document.createElement("input");
            input.type = "hidden";
            input.name = field + "[]";
            input.value = (body[field] !== null && body[field] !== undefined) ? body[field] : "";
            form.appendChild(input);
          });
        });
      }
      
      // Add the hidden field "solveNow" if action is "solve".
      if (action === "solve") {
        const solveInput = document.createElement("input");
        solveInput.type = "hidden";
        solveInput.name = "solveNow";
        solveInput.value = "1";
        form.appendChild(solveInput);
      }
      
      document.body.appendChild(form);
      form.submit();
    }
	 document.addEventListener("DOMContentLoaded", () => {
	   const gallery = document.querySelector(".gallery");

	   let tapTimer = null;

	   gallery.addEventListener("click", (e) => {
	     const container = e.target.closest(".thumb-container");
	     if (!container) return;

	     const docId = container.dataset.docid; 
	     console.log("docID = ", docId);

	     // Start timer to distinguish tap from double-click or long-press
	     if (tapTimer === null) {
	       tapTimer = setTimeout(() => { 
	         selectedDocId = docId;
	         postSimulation('solve');  // Single click or tap: view simulation
	         tapTimer = null;
	       }, 250);
	     }
	   });

	   gallery.addEventListener("dblclick", (e) => {
	     const container = e.target.closest(".thumb-container");
	     if (!container) return;

	     clearTimeout(tapTimer);
	     tapTimer = null;

	     const docId = container.dataset.docid; 
        selectedDocId = docId;
        postSimulation('edit');  // Double-click: edit simulation
	   });

	   gallery.addEventListener("touchend", () => {
	     if (tapTimer !== null) {
	       clearTimeout(tapTimer);
	       tapTimer = null;
	     }
	   });
	 });
  </script>
</head>
<body>
  <h1>Gallery</h1>
  <h3> Click to solve and draw; double-click to edit </h3>
  <div class="gallery">
    <?php foreach ($documents as $doc): 
        // Use image_filename for thumbnail; if empty, use a placeholder image.
		//  $imgSrc = "/ThreeBodyWeb/images/" . htmlspecialchars($doc['image_filename']);
        $imgSrc = !empty($doc['image_filename']) ? "./images/" . htmlspecialchars($doc['image_filename']) : "placeholder.svg";
		 
        // Optional caption: display the caption if available.
        $caption = !empty($doc['caption']) ? htmlspecialchars($doc['caption']) : "";
    ?>
	 <div class="thumb-container" data-docid="<?php echo $doc['DocumentNumber']; ?>">
	    <img src="<?php echo $imgSrc; ?>" alt="Problem <?php echo $doc['DocumentNumber']; ?>"
	         id="doc-<?php echo $doc['DocumentNumber']; ?>" class="thumb">
	    <?php if (!empty($caption)): ?>
	      <div class='caption' title="<?php echo $caption; ?>"><?php echo $caption; ?></div>
	    <?php endif; ?>
	 </div>
    <?php endforeach; ?>
  </div>
</body>
</html>

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists