Benjamin Harris 2 месяцев назад
Родитель
Сommit
63c2c6ad7f

+ 5 - 5
controllers/blockSubmit.php

@@ -47,10 +47,10 @@ if ($name === '' || $blockId === '') {
 
 if ($action === 'create') {
     $stmt = $pdo->prepare('
-        INSERT INTO block_info (modx_user_id, name, block_id, location, area, gps, status, date_added)
-        VALUES (?, ?, ?, ?, ?, ?, 0, CURDATE())
+        INSERT INTO block_info (modx_user_id, name, block_id, location, area, gps, analysis_type, status, date_added)
+        VALUES (?, ?, ?, ?, ?, ?, ?, 0, CURDATE())
     ');
-    $stmt->execute([$userId, $name, $blockId, $location, (int) $areaHa, $gps]);
+    $stmt->execute([$userId, $name, $blockId, $location, $areaHa, $gps, $soilType]);
 
     $_SESSION['flash_success'] = 'Paddock "' . htmlspecialchars($name, ENT_QUOTES, 'UTF-8') . '" created.';
     header('Location: /dashboard/crop-cards/');
@@ -77,10 +77,10 @@ if ($action === 'edit') {
 
     $stmt = $pdo->prepare('
         UPDATE block_info
-        SET name = ?, block_id = ?, location = ?, area = ?, gps = ?
+        SET name = ?, block_id = ?, location = ?, area = ?, gps = ?, analysis_type = ?
         WHERE id = ? AND modx_user_id = ?
     ');
-    $stmt->execute([$name, $blockId, $location, (int) $areaHa, $gps, $recordId, $userId]);
+    $stmt->execute([$name, $blockId, $location, $areaHa, $gps, $soilType, $recordId, $userId]);
 
     $_SESSION['flash_success'] = 'Paddock "' . htmlspecialchars($name, ENT_QUOTES, 'UTF-8') . '" updated.';
 

+ 6 - 0
dashboard/crop-cards/block-detail.php

@@ -68,6 +68,7 @@ $stmtSoil->execute([$userId, $block['block_id']]);
 $soilTests = $stmtSoil->fetchAll(PDO::FETCH_ASSOC);
 
 // ── Recent plant tests ────────────────────────────────────────────────────────
+// Linked via site_id until migration 003 adds block_id column to plant_records
 $stmtPlant = $pdo->prepare(
     'SELECT id, rand, date_sampled AS date, site_id, crop_type, client_name
      FROM plant_records
@@ -78,6 +79,7 @@ $stmtPlant->execute([$userId, $block['block_id']]);
 $plantTests = $stmtPlant->fetchAll(PDO::FETCH_ASSOC);
 
 // ── Recent water tests ────────────────────────────────────────────────────────
+// Linked via site_id until migration 003 adds block_id column to water_records
 $stmtWater = $pdo->prepare(
     'SELECT id, rand, date_sampled AS date, site_id, analysis_type, client_name
      FROM water_records
@@ -200,6 +202,10 @@ include __DIR__ . '/../../layouts/navbar.php';
                                             <?php endif; ?>
                                         </td>
                                     </tr>
+                                    <tr>
+                                        <th class="text-muted pe-2 text-nowrap">Soil Type</th>
+                                        <td><?= !empty($block['analysis_type'] ?? '') ? ucfirst($h($block['analysis_type'])) : '<span class="text-muted">—</span>' ?></td>
+                                    </tr>
                                     <tr>
                                         <th class="text-muted pe-2 text-nowrap">Added</th>
                                         <td><?= $h($block['date_added']) ?: '—' ?></td>

+ 4 - 4
dashboard/crop-cards/index.php

@@ -158,17 +158,17 @@ include __DIR__ . '/../../layouts/navbar.php';
                         <div class="row mb-2">
                             <div class="col border border-success rounded p-2">
                                 <div class="row align-items-center">
-                                    <div class="col-md-1 text-center fw-bold"><?= $i + 1 ?></div>
-                                    <div class="col-6 col-sm-11 col-md-2">
+                                    <div class="col-1 text-center fw-bold"><?= $i + 1 ?></div>
+                                    <div class="col-2">
                                         <div class="row g-1">
-                                            <div class="col-6">
+                                            <div class="col">
                                                 <a class="btn btn-primary btn-sm w-100"
                                                    href="/dashboard/crop-cards/block-detail.php?rid=<?= $rowid ?>&id=<?= $h($row['block_id']) ?>&block=<?= urlencode($row['name'] ?? '') ?>"
                                                    title="View block">
                                                     <i class="fas fa-globe-asia text-white"></i>
                                                 </a>
                                             </div>
-                                            <div class="col-6">
+                                            <div class="col">
                                                 <button type="button"
                                                         class="btn btn-success btn-sm w-100"
                                                         data-bs-toggle="modal"

+ 54 - 0
database/migrations/003_paddock_schema_fixes.sql

@@ -0,0 +1,54 @@
+-- =============================================================================
+-- Migration 003 — Paddock schema fixes
+-- Run once: mysql -u <user> -p cropmonitor < 003_paddock_schema_fixes.sql
+-- =============================================================================
+
+-- ── block_info fixes ──────────────────────────────────────────────────────────
+
+-- gps was INT — GPS strings like "-33.8688, 151.2093" were silently lost
+ALTER TABLE `block_info`
+    MODIFY COLUMN `gps` VARCHAR(100) DEFAULT NULL;
+
+-- area was INT — fractional hectares (e.g. 12.5) were truncated
+ALTER TABLE `block_info`
+    MODIFY COLUMN `area` DECIMAL(10,2) DEFAULT NULL;
+
+-- soil type is captured in the create/edit modal but had no column
+ALTER TABLE `block_info`
+    ADD COLUMN `analysis_type` VARCHAR(30) DEFAULT NULL
+    AFTER `area`;
+
+-- utf8 for all text fields (was latin1)
+ALTER TABLE `block_info`
+    CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
+
+
+-- ── plant_records — add block_id for paddock linking ─────────────────────────
+
+ALTER TABLE `plant_records`
+    ADD COLUMN `block_id` VARCHAR(50) DEFAULT NULL
+    AFTER `site_id`;
+
+CREATE INDEX `idx_plant_block` ON `plant_records` (`modx_user_id`, `block_id`);
+
+
+-- ── water_records — add block_id for paddock linking ─────────────────────────
+
+ALTER TABLE `water_records`
+    ADD COLUMN `block_id` VARCHAR(50) DEFAULT NULL
+    AFTER `site_id`;
+
+CREATE INDEX `idx_water_block` ON `water_records` (`modx_user_id`, `block_id`);
+
+
+-- ── crop_info — richer crop tracking ─────────────────────────────────────────
+
+ALTER TABLE `crop_info`
+    ADD COLUMN `variety`       VARCHAR(50)  DEFAULT NULL AFTER `name`,
+    ADD COLUMN `planting_date` DATE         DEFAULT NULL AFTER `variety`,
+    ADD COLUMN `harvest_date`  DATE         DEFAULT NULL AFTER `planting_date`,
+    ADD COLUMN `status`        VARCHAR(20)  DEFAULT 'active' AFTER `harvest_date`,
+    ADD COLUMN `notes`         TEXT         DEFAULT NULL AFTER `status`;
+
+ALTER TABLE `crop_info`
+    CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;