PDO::ERRMODE_EXCEPTION, ]); $animals = $pdo->query('SELECT id FROM rescue_animal ORDER BY id')->fetchAll(PDO::FETCH_COLUMN); if (!$animals) { echo "No animals found\n"; exit(0); } $coverId = (int)$pdo->query("SELECT id FROM rescue_field_definition WHERE module_id='animal_management' AND field_key='cover_image_url' LIMIT 1")->fetchColumn(); $profileId = (int)$pdo->query("SELECT id FROM rescue_field_definition WHERE module_id='animal_management' AND field_key='profile_image_url' LIMIT 1")->fetchColumn(); $seedDir = '/var/www/localhost/htdocs/uploads/animal-management/seed'; $seedFiles = glob($seedDir . '/*.jpg'); sort($seedFiles); if (empty($seedFiles)) { echo "No seed images downloaded\n"; exit(1); } $now = date('Y-m-d H:i:s'); $idx = 0; $upsert = $pdo->prepare("INSERT INTO rescue_animal_field_value (animal_id, field_definition_id, value_text, value_json, created_at, updated_at) VALUES (:animal_id,:field_definition_id,:value_text,NULL,:created_at,:updated_at) ON DUPLICATE KEY UPDATE value_text=VALUES(value_text),updated_at=VALUES(updated_at)"); $insertGallery = $pdo->prepare("INSERT INTO rescue_animal_gallery_item (animal_id, file_path, file_id, source_post_id, source_type, caption, created_by, created_at, updated_at) VALUES (:animal_id,:file_path,NULL,NULL,'seed',NULL,NULL,:created_at,:updated_at)"); $deleteSeedGallery = $pdo->prepare("DELETE FROM rescue_animal_gallery_item WHERE animal_id=:animal_id AND source_type='seed'"); foreach ($animals as $animalIdRaw) { $animalId = (int)$animalIdRaw; $source = $seedFiles[$idx % count($seedFiles)]; $idx++; $targetDir = '/var/www/localhost/htdocs/uploads/animal-management/animals/' . $animalId; if (!is_dir($targetDir) && !mkdir($targetDir, 0775, true) && !is_dir($targetDir)) { echo "Could not create directory for animal {$animalId}\n"; continue; } $stamp = time() . '-' . $animalId; $coverName = 'cover-seed-' . $stamp . '.jpg'; $profileName = 'profile-seed-' . $stamp . '.jpg'; $galleryName = 'gallery-seed-' . $stamp . '.jpg'; $coverPath = $targetDir . '/' . $coverName; $profilePath = $targetDir . '/' . $profileName; $galleryPath = $targetDir . '/' . $galleryName; if (!copy($source, $coverPath) || !copy($source, $profilePath) || !copy($source, $galleryPath)) { echo "Could not copy seed image for animal {$animalId}\n"; continue; } $coverUrl = '/uploads/animal-management/animals/' . $animalId . '/' . $coverName; $profileUrl = '/uploads/animal-management/animals/' . $animalId . '/' . $profileName; $galleryUrl = '/uploads/animal-management/animals/' . $animalId . '/' . $galleryName; if ($coverId > 0) { $upsert->execute([ ':animal_id' => $animalId, ':field_definition_id' => $coverId, ':value_text' => $coverUrl, ':created_at' => $now, ':updated_at' => $now, ]); } if ($profileId > 0) { $upsert->execute([ ':animal_id' => $animalId, ':field_definition_id' => $profileId, ':value_text' => $profileUrl, ':created_at' => $now, ':updated_at' => $now, ]); } $deleteSeedGallery->execute([':animal_id' => $animalId]); $insertGallery->execute([ ':animal_id' => $animalId, ':file_path' => $galleryUrl, ':created_at' => $now, ':updated_at' => $now, ]); echo "Seeded animal {$animalId} with " . basename($source) . "\n"; }