added migration seeders models and test script

This commit is contained in:
Jürg Hallenbarter
2026-04-29 14:20:41 +02:00
parent b2dab73f17
commit deba81fadb
38 changed files with 3389 additions and 0 deletions

View File

@@ -0,0 +1,59 @@
<?php
namespace App\Database\Migrations;
use CodeIgniter\Database\Migration;
class CreateUsersTable extends Migration
{
public function up()
{
$this->forge->addField([
'id' => [
'type' => 'CHAR',
'constraint' => 36,
'null' => false,
],
'email' => [
'type' => 'VARCHAR',
'constraint' => 255,
'null' => false,
],
'password_hash' => [
'type' => 'VARCHAR',
'constraint' => 255,
'null' => false,
],
'name' => [
'type' => 'VARCHAR',
'constraint' => 255,
'null' => true,
],
'avatar_url' => [
'type' => 'TEXT',
'null' => true,
],
'settings' => [
'type' => 'JSON',
'null' => true,
'default' => '{}',
],
'created_at' => [
'type' => 'DATETIME',
'null' => true,
],
'updated_at' => [
'type' => 'DATETIME',
'null' => true,
],
]);
$this->forge->addKey('id', true);
$this->forge->addUniqueKey('email');
$this->forge->createTable('users');
}
public function down()
{
$this->forge->dropTable('users');
}
}

View File

@@ -0,0 +1,53 @@
<?php
namespace App\Database\Migrations;
use CodeIgniter\Database\Migration;
class CreateCategoriesTable extends Migration
{
public function up()
{
$this->forge->addField([
'id' => [
'type' => 'CHAR',
'constraint' => 36,
'null' => false,
],
'user_id' => [
'type' => 'CHAR',
'constraint' => 36,
'null' => false,
],
'name' => [
'type' => 'VARCHAR',
'constraint' => 255,
'null' => false,
],
'color' => [
'type' => 'VARCHAR',
'constraint' => 7,
'null' => true,
'comment' => 'Hex color for UI',
],
'favorite' => [
'type' => 'BOOLEAN',
'default' => false,
],
'created_at' => [
'type' => 'DATETIME',
'null' => true,
],
]);
$this->forge->addKey('id', true);
$this->forge->addKey('user_id');
$this->forge->addUniqueKey(['user_id', 'name']);
$this->forge->addForeignKey('user_id', 'users', 'id', 'CASCADE', 'CASCADE');
$this->forge->createTable('categories');
}
public function down()
{
$this->forge->dropTable('categories');
}
}

View File

@@ -0,0 +1,52 @@
<?php
namespace App\Database\Migrations;
use CodeIgniter\Database\Migration;
class CreateProjectsTable extends Migration
{
public function up()
{
$this->forge->addField([
'id' => [
'type' => 'CHAR',
'constraint' => 36,
'null' => false,
],
'user_id' => [
'type' => 'CHAR',
'constraint' => 36,
'null' => false,
],
'name' => [
'type' => 'VARCHAR',
'constraint' => 255,
'null' => false,
],
'description' => [
'type' => 'TEXT',
'null' => true,
],
'color' => [
'type' => 'VARCHAR',
'constraint' => 7,
'null' => true,
'comment' => 'Hex color for UI',
],
'created_at' => [
'type' => 'DATETIME',
'null' => true,
],
]);
$this->forge->addKey('id', true);
$this->forge->addKey('user_id');
$this->forge->addForeignKey('user_id', 'users', 'id', 'CASCADE', 'CASCADE');
$this->forge->createTable('projects');
}
public function down()
{
$this->forge->dropTable('projects');
}
}

View File

@@ -0,0 +1,83 @@
<?php
namespace App\Database\Migrations;
use CodeIgniter\Database\Migration;
class CreateTodosTable extends Migration
{
public function up()
{
$this->forge->addField([
'id' => [
'type' => 'CHAR',
'constraint' => 36,
'null' => false,
],
'user_id' => [
'type' => 'CHAR',
'constraint' => 36,
'null' => false,
],
'title' => [
'type' => 'VARCHAR',
'constraint' => 255,
'null' => false,
],
'description' => [
'type' => 'TEXT',
'null' => true,
],
'status' => [
'type' => 'ENUM',
'constraint' => ['open', 'in_progress', 'completed', 'archived'],
'default' => 'open',
],
'due_date' => [
'type' => 'DATE',
'null' => true,
],
'due_time' => [
'type' => 'TIME',
'null' => true,
],
'sync_enabled' => [
'type' => 'BOOLEAN',
'default' => true,
],
'reminder_enabled' => [
'type' => 'BOOLEAN',
'default' => false,
],
'recurring_enabled' => [
'type' => 'BOOLEAN',
'default' => false,
],
'project_id' => [
'type' => 'CHAR',
'constraint' => 36,
'null' => true,
],
'created_at' => [
'type' => 'DATETIME',
'null' => true,
],
'updated_at' => [
'type' => 'DATETIME',
'null' => true,
],
]);
$this->forge->addKey('id', true);
$this->forge->addKey('user_id');
$this->forge->addKey('due_date');
$this->forge->addKey('status');
$this->forge->addForeignKey('user_id', 'users', 'id', 'CASCADE', 'CASCADE');
$this->forge->addForeignKey('project_id', 'projects', 'id', 'SET NULL', 'CASCADE');
$this->forge->createTable('todos');
}
public function down()
{
$this->forge->dropTable('todos');
}
}

View File

@@ -0,0 +1,33 @@
<?php
namespace App\Database\Migrations;
use CodeIgniter\Database\Migration;
class CreateTodoCategoriesTable extends Migration
{
public function up()
{
$this->forge->addField([
'todo_id' => [
'type' => 'CHAR',
'constraint' => 36,
'null' => false,
],
'category_id' => [
'type' => 'CHAR',
'constraint' => 36,
'null' => false,
],
]);
$this->forge->addPrimaryKey(['todo_id', 'category_id']);
$this->forge->addForeignKey('todo_id', 'todos', 'id', 'CASCADE', 'CASCADE');
$this->forge->addForeignKey('category_id', 'categories', 'id', 'CASCADE', 'CASCADE');
$this->forge->createTable('todo_categories');
}
public function down()
{
$this->forge->dropTable('todo_categories');
}
}

View File

@@ -0,0 +1,64 @@
<?php
namespace App\Database\Migrations;
use CodeIgniter\Database\Migration;
class CreateRecurringTasksTable extends Migration
{
public function up()
{
$this->forge->addField([
'id' => [
'type' => 'CHAR',
'constraint' => 36,
'null' => false,
],
'user_id' => [
'type' => 'CHAR',
'constraint' => 36,
'null' => false,
],
'title' => [
'type' => 'VARCHAR',
'constraint' => 255,
'null' => false,
],
'description' => [
'type' => 'TEXT',
'null' => true,
],
'schedule' => [
'type' => 'ENUM',
'constraint' => ['daily', 'weekly', 'monthly', 'custom'],
'null' => false,
],
'custom_days' => [
'type' => 'JSON',
'null' => true,
'comment' => 'Array of days e.g., ["mon","wed","fri"] when schedule=custom',
],
'favorite' => [
'type' => 'BOOLEAN',
'default' => false,
],
'created_at' => [
'type' => 'DATETIME',
'null' => true,
],
'updated_at' => [
'type' => 'DATETIME',
'null' => true,
],
]);
$this->forge->addKey('id', true);
$this->forge->addKey('user_id');
$this->forge->addForeignKey('user_id', 'users', 'id', 'CASCADE', 'CASCADE');
$this->forge->createTable('recurring_tasks');
}
public function down()
{
$this->forge->dropTable('recurring_tasks');
}
}

View File

@@ -0,0 +1,33 @@
<?php
namespace App\Database\Migrations;
use CodeIgniter\Database\Migration;
class CreateRecurringTaskCategoriesTable extends Migration
{
public function up()
{
$this->forge->addField([
'recurring_task_id' => [
'type' => 'CHAR',
'constraint' => 36,
'null' => false,
],
'category_id' => [
'type' => 'CHAR',
'constraint' => 36,
'null' => false,
],
]);
$this->forge->addPrimaryKey(['recurring_task_id', 'category_id']);
$this->forge->addForeignKey('recurring_task_id', 'recurring_tasks', 'id', 'CASCADE', 'CASCADE');
$this->forge->addForeignKey('category_id', 'categories', 'id', 'CASCADE', 'CASCADE');
$this->forge->createTable('recurring_task_categories');
}
public function down()
{
$this->forge->dropTable('recurring_task_categories');
}
}

View File

@@ -0,0 +1,73 @@
<?php
namespace App\Database\Migrations;
use CodeIgniter\Database\Migration;
class CreateActivityLogsTable extends Migration
{
public function up()
{
$this->forge->addField([
'id' => [
'type' => 'CHAR',
'constraint' => 36,
'null' => false,
],
'user_id' => [
'type' => 'CHAR',
'constraint' => 36,
'null' => true,
'comment' => 'Nullable for anonymous events',
],
'action' => [
'type' => 'VARCHAR',
'constraint' => 255,
'null' => false,
'comment' => 'e.g., todo_created, login, theme_installed',
],
'entity_type' => [
'type' => 'VARCHAR',
'constraint' => 100,
'null' => true,
'comment' => 'todo, category, project, etc.',
],
'entity_id' => [
'type' => 'CHAR',
'constraint' => 36,
'null' => true,
],
'details' => [
'type' => 'JSON',
'null' => true,
'default' => '{}',
'comment' => 'before/after values, metadata',
],
'ip_address' => [
'type' => 'VARCHAR',
'constraint' => 45,
'null' => true,
],
'user_agent' => [
'type' => 'TEXT',
'null' => true,
],
'created_at' => [
'type' => 'DATETIME',
'null' => true,
],
]);
$this->forge->addKey('id', true);
$this->forge->addKey('user_id');
$this->forge->addKey('created_at');
$this->forge->addKey('action');
$this->forge->addKey(['entity_type', 'entity_id']);
$this->forge->addForeignKey('user_id', 'users', 'id', 'SET NULL', 'CASCADE');
$this->forge->createTable('activity_logs');
}
public function down()
{
$this->forge->dropTable('activity_logs');
}
}

View File

@@ -0,0 +1,82 @@
<?php
namespace App\Database\Migrations;
use CodeIgniter\Database\Migration;
class CreateMarketplaceThemesTable extends Migration
{
public function up()
{
$this->forge->addField([
'id' => [
'type' => 'CHAR',
'constraint' => 36,
'null' => false,
],
'name' => [
'type' => 'VARCHAR',
'constraint' => 255,
'null' => false,
],
'display_name' => [
'type' => 'VARCHAR',
'constraint' => 255,
'null' => false,
],
'description' => [
'type' => 'TEXT',
'null' => true,
],
'author' => [
'type' => 'VARCHAR',
'constraint' => 255,
'null' => true,
],
'version' => [
'type' => 'VARCHAR',
'constraint' => 50,
'null' => true,
],
'thumbnail_url' => [
'type' => 'TEXT',
'null' => true,
],
'download_url' => [
'type' => 'TEXT',
'null' => false,
],
'price' => [
'type' => 'DECIMAL',
'constraint' => '10,2',
'default' => 0,
],
'is_published' => [
'type' => 'BOOLEAN',
'default' => true,
],
'metadata' => [
'type' => 'JSON',
'null' => true,
'default' => '{}',
'comment' => 'tags, screenshots, etc.',
],
'created_at' => [
'type' => 'DATETIME',
'null' => true,
],
'updated_at' => [
'type' => 'DATETIME',
'null' => true,
],
]);
$this->forge->addKey('id', true);
$this->forge->addUniqueKey('name');
$this->forge->createTable('marketplace_themes');
}
public function down()
{
$this->forge->dropTable('marketplace_themes');
}
}

View File

@@ -0,0 +1,54 @@
<?php
namespace App\Database\Migrations;
use CodeIgniter\Database\Migration;
class CreateUserThemesTable extends Migration
{
public function up()
{
$this->forge->addField([
'id' => [
'type' => 'CHAR',
'constraint' => 36,
'null' => false,
],
'user_id' => [
'type' => 'CHAR',
'constraint' => 36,
'null' => false,
],
'theme_id' => [
'type' => 'CHAR',
'constraint' => 36,
'null' => false,
],
'installed_at' => [
'type' => 'DATETIME',
'null' => true,
],
'active' => [
'type' => 'BOOLEAN',
'default' => false,
'comment' => 'Whether this is the user\'s currently active theme',
],
'custom_settings' => [
'type' => 'JSON',
'null' => true,
'default' => '{}',
'comment' => 'User overrides for theme variables',
],
]);
$this->forge->addKey('id', true);
$this->forge->addUniqueKey(['user_id', 'theme_id']);
$this->forge->addForeignKey('user_id', 'users', 'id', 'CASCADE', 'CASCADE');
$this->forge->addForeignKey('theme_id', 'marketplace_themes', 'id', 'CASCADE', 'CASCADE');
$this->forge->createTable('user_themes');
}
public function down()
{
$this->forge->dropTable('user_themes');
}
}

View File

@@ -0,0 +1,52 @@
<?php
namespace App\Database\Migrations;
use CodeIgniter\Database\Migration;
class CreateAiProvidersTable extends Migration
{
public function up()
{
$this->forge->addField([
'id' => [
'type' => 'CHAR',
'constraint' => 36,
'null' => false,
],
'name' => [
'type' => 'VARCHAR',
'constraint' => 100,
'null' => false,
'comment' => 'openai, anthropic, google, etc.',
],
'display_name' => [
'type' => 'VARCHAR',
'constraint' => 255,
'null' => false,
],
'base_url' => [
'type' => 'TEXT',
'null' => true,
'comment' => 'Override endpoint',
],
'is_builtin' => [
'type' => 'BOOLEAN',
'default' => true,
'comment' => 'False for user-added custom providers',
],
'created_at' => [
'type' => 'DATETIME',
'null' => true,
],
]);
$this->forge->addKey('id', true);
$this->forge->addUniqueKey('name');
$this->forge->createTable('ai_providers');
}
public function down()
{
$this->forge->dropTable('ai_providers');
}
}

View File

@@ -0,0 +1,62 @@
<?php
namespace App\Database\Migrations;
use CodeIgniter\Database\Migration;
class CreateUserApiKeysTable extends Migration
{
public function up()
{
$this->forge->addField([
'id' => [
'type' => 'CHAR',
'constraint' => 36,
'null' => false,
],
'user_id' => [
'type' => 'CHAR',
'constraint' => 36,
'null' => false,
],
'provider_id' => [
'type' => 'CHAR',
'constraint' => 36,
'null' => false,
],
'api_key_encrypted' => [
'type' => 'TEXT',
'null' => false,
'comment' => 'Store encrypted API key',
],
'label' => [
'type' => 'VARCHAR',
'constraint' => 255,
'null' => true,
'comment' => 'e.g., Work OpenAI Key',
],
'is_active' => [
'type' => 'BOOLEAN',
'default' => true,
],
'created_at' => [
'type' => 'DATETIME',
'null' => true,
],
'last_used_at' => [
'type' => 'DATETIME',
'null' => true,
],
]);
$this->forge->addKey('id', true);
$this->forge->addUniqueKey(['user_id', 'provider_id']);
$this->forge->addForeignKey('user_id', 'users', 'id', 'CASCADE', 'CASCADE');
$this->forge->addForeignKey('provider_id', 'ai_providers', 'id', 'CASCADE', 'CASCADE');
$this->forge->createTable('user_api_keys');
}
public function down()
{
$this->forge->dropTable('user_api_keys');
}
}

View File

@@ -0,0 +1,52 @@
<?php
namespace App\Database\Migrations;
use CodeIgniter\Database\Migration;
class CreateUserAiSettingsTable extends Migration
{
public function up()
{
$this->forge->addField([
'user_id' => [
'type' => 'CHAR',
'constraint' => 36,
'null' => false,
],
'default_provider_id' => [
'type' => 'CHAR',
'constraint' => 36,
'null' => true,
],
'default_model' => [
'type' => 'VARCHAR',
'constraint' => 100,
'null' => true,
'comment' => 'e.g., gpt-4, claude-3-opus',
],
'max_tokens' => [
'type' => 'INT',
'constraint' => 11,
'default' => 2048,
],
'temperature' => [
'type' => 'FLOAT',
'default' => 0.7,
],
'updated_at' => [
'type' => 'DATETIME',
'null' => true,
],
]);
$this->forge->addPrimaryKey('user_id');
$this->forge->addForeignKey('user_id', 'users', 'id', 'CASCADE', 'CASCADE');
$this->forge->addForeignKey('default_provider_id', 'ai_providers', 'id', 'SET NULL', 'CASCADE');
$this->forge->createTable('user_ai_settings');
}
public function down()
{
$this->forge->dropTable('user_ai_settings');
}
}

View File

@@ -0,0 +1,65 @@
<?php
namespace App\Database\Migrations;
use CodeIgniter\Database\Migration;
class CreateAiChatsTable extends Migration
{
public function up()
{
$this->forge->addField([
'id' => [
'type' => 'CHAR',
'constraint' => 36,
'null' => false,
],
'user_id' => [
'type' => 'CHAR',
'constraint' => 36,
'null' => false,
],
'title' => [
'type' => 'VARCHAR',
'constraint' => 255,
'null' => true,
'comment' => 'Generated from first message or user-set',
],
'provider_id' => [
'type' => 'CHAR',
'constraint' => 36,
'null' => true,
],
'model_used' => [
'type' => 'VARCHAR',
'constraint' => 100,
'null' => true,
'comment' => 'Snapshot of model at chat creation',
],
'system_prompt' => [
'type' => 'TEXT',
'null' => true,
'comment' => 'Optional custom system prompt for this chat',
],
'created_at' => [
'type' => 'DATETIME',
'null' => true,
],
'updated_at' => [
'type' => 'DATETIME',
'null' => true,
],
]);
$this->forge->addKey('id', true);
$this->forge->addKey('user_id');
$this->forge->addKey('updated_at');
$this->forge->addForeignKey('user_id', 'users', 'id', 'CASCADE', 'CASCADE');
$this->forge->addForeignKey('provider_id', 'ai_providers', 'id', 'SET NULL', 'CASCADE');
$this->forge->createTable('ai_chats');
}
public function down()
{
$this->forge->dropTable('ai_chats');
}
}

View File

@@ -0,0 +1,52 @@
<?php
namespace App\Database\Migrations;
use CodeIgniter\Database\Migration;
class CreateAiMessagesTable extends Migration
{
public function up()
{
$this->forge->addField([
'id' => [
'type' => 'CHAR',
'constraint' => 36,
'null' => false,
],
'chat_id' => [
'type' => 'CHAR',
'constraint' => 36,
'null' => false,
],
'role' => [
'type' => 'ENUM',
'constraint' => ['user', 'assistant', 'system'],
'null' => false,
],
'content' => [
'type' => 'TEXT',
'null' => false,
],
'tokens_used' => [
'type' => 'INT',
'constraint' => 11,
'null' => true,
'comment' => 'Optional token count for billing/analysis',
],
'created_at' => [
'type' => 'DATETIME',
'null' => true,
],
]);
$this->forge->addKey('id', true);
$this->forge->addKey('chat_id');
$this->forge->addForeignKey('chat_id', 'ai_chats', 'id', 'CASCADE', 'CASCADE');
$this->forge->createTable('ai_messages');
}
public function down()
{
$this->forge->dropTable('ai_messages');
}
}

View File

@@ -0,0 +1,40 @@
<?php
namespace App\Database\Seeds;
use CodeIgniter\Database\Seeder;
class AiProvidersSeeder extends Seeder
{
public function run()
{
$data = [
[
'id' => '550e8400-e29b-41d4-a716-446655440001',
'name' => 'openai',
'display_name' => 'OpenAI',
'base_url' => 'https://api.openai.com/v1',
'is_builtin' => true,
'created_at' => date('Y-m-d H:i:s'),
],
[
'id' => '550e8400-e29b-41d4-a716-446655440002',
'name' => 'anthropic',
'display_name' => 'Anthropic',
'base_url' => 'https://api.anthropic.com',
'is_builtin' => true,
'created_at' => date('Y-m-d H:i:s'),
],
[
'id' => '550e8400-e29b-41d4-a716-446655440003',
'name' => 'google',
'display_name' => 'Google AI',
'base_url' => 'https://generativelanguage.googleapis.com/v1',
'is_builtin' => true,
'created_at' => date('Y-m-d H:i:s'),
],
];
$this->db->table('ai_providers')->insertBatch($data);
}
}

View File

@@ -0,0 +1,46 @@
<?php
namespace App\Database\Seeds;
use CodeIgniter\Database\Seeder;
class MarketplaceThemesSeeder extends Seeder
{
public function run()
{
$data = [
[
'id' => '550e8400-e29b-41d4-a716-446655440010',
'name' => 'default-light',
'display_name' => 'Default Light',
'description' => 'Clean and simple light theme',
'author' => 'System',
'version' => '1.0.0',
'thumbnail_url' => null,
'download_url' => '/themes/default-light.zip',
'price' => 0,
'is_published' => true,
'metadata' => json_encode(['tags' => ['light', 'clean']]),
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s'),
],
[
'id' => '550e8400-e29b-41d4-a716-446655440011',
'name' => 'default-dark',
'display_name' => 'Default Dark',
'description' => 'Dark theme for night owls',
'author' => 'System',
'version' => '1.0.0',
'thumbnail_url' => null,
'download_url' => '/themes/default-dark.zip',
'price' => 0,
'is_published' => true,
'metadata' => json_encode(['tags' => ['dark', 'night']]),
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s'),
],
];
$this->db->table('marketplace_themes')->insertBatch($data);
}
}

View File

@@ -0,0 +1,313 @@
<?php
namespace App\Database\Seeds;
use CodeIgniter\Database\Seeder;
class SampleDataSeeder extends Seeder
{
public function run()
{
// Generate a UUID helper function
$generateUuid = function() {
return sprintf(
'%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
mt_rand(0, 0xffff), mt_rand(0, 0xffff),
mt_rand(0, 0xffff),
mt_rand(0, 0x0fff) | 0x4000,
mt_rand(0, 0x3fff) | 0x8000,
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
);
};
// Create a sample user (or get existing one)
$existingUser = $this->db->table('users')->where('email', 'demo@example.com')->get()->getRowArray();
if ($existingUser) {
$userId = $existingUser['id'];
} else {
$userId = $generateUuid();
$this->db->table('users')->insert([
'id' => $userId,
'email' => 'demo@example.com',
'password_hash' => password_hash('password123', PASSWORD_DEFAULT),
'name' => 'Demo User',
'avatar_url' => null,
'settings' => json_encode(['language' => 'en', 'default_view' => 'list']),
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s'),
]);
}
// Create sample categories (check for existing)
$existingCategories = $this->db->table('categories')->where('user_id', $userId)->get()->getResultArray();
$existingCategoryNames = array_column($existingCategories, 'name');
$categories = [];
$categoryNames = ['Work', 'Home', 'Personal'];
$categoryColors = ['#3B82F6', '#10B981', '#F59E0B'];
$categoryFavorites = [true, false, false];
foreach ($categoryNames as $index => $name) {
if (!in_array($name, $existingCategoryNames)) {
$categories[] = [
'id' => $generateUuid(),
'user_id' => $userId,
'name' => $name,
'color' => $categoryColors[$index],
'favorite' => $categoryFavorites[$index],
'created_at' => date('Y-m-d H:i:s'),
];
}
}
if (!empty($categories)) {
$this->db->table('categories')->insertBatch($categories);
}
// Get all categories for the user (existing + newly created)
$allCategories = $this->db->table('categories')->where('user_id', $userId)->get()->getResultArray();
$categories = [];
foreach ($categoryNames as $name) {
foreach ($allCategories as $cat) {
if ($cat['name'] === $name) {
$categories[] = $cat;
break;
}
}
}
// Create sample projects (check for existing)
$existingProjects = $this->db->table('projects')->where('user_id', $userId)->get()->getResultArray();
$existingProjectNames = array_column($existingProjects, 'name');
$projects = [];
$projectData = [
['name' => 'Web Redesign', 'description' => 'Redesign the company website', 'color' => '#8B5CF6'],
['name' => 'Home Renovation', 'description' => 'Renovate the kitchen and bathroom', 'color' => '#EC4899'],
['name' => 'Learning', 'description' => 'Learn new technologies and skills', 'color' => '#14B8A6'],
];
foreach ($projectData as $proj) {
if (!in_array($proj['name'], $existingProjectNames)) {
$projects[] = [
'id' => $generateUuid(),
'user_id' => $userId,
'name' => $proj['name'],
'description' => $proj['description'],
'color' => $proj['color'],
'created_at' => date('Y-m-d H:i:s'),
];
}
}
if (!empty($projects)) {
$this->db->table('projects')->insertBatch($projects);
}
// Get all projects for the user
$allProjects = $this->db->table('projects')->where('user_id', $userId)->get()->getResultArray();
$projects = [];
foreach ($projectData as $proj) {
foreach ($allProjects as $p) {
if ($p['name'] === $proj['name']) {
$projects[] = $p;
break;
}
}
}
$webRedesignId = isset($projects[0]) ? $projects[0]['id'] : null;
$homeRenovationId = isset($projects[1]) ? $projects[1]['id'] : null;
$learningId = isset($projects[2]) ? $projects[2]['id'] : null;
// Create sample todos (check for existing)
$existingTodos = $this->db->table('todos')->where('user_id', $userId)->get()->getResultArray();
$existingTodoTitles = array_column($existingTodos, 'title');
$todos = [];
$todoData = [
[
'title' => 'Bestehende Aufgaben analysieren',
'description' => 'Aktuellen Aufbau der Todo-App sichten und Felder abstimmen.',
'status' => 'open',
'due_date' => date('Y-m-d', strtotime('+7 days')),
'due_time' => '10:30:00',
'sync_enabled' => true,
'reminder_enabled' => false,
'recurring_enabled' => false,
'project_id' => $webRedesignId,
],
[
'title' => 'Wireframes erstellen',
'description' => 'Erste Skizzen für das neue Design machen.',
'status' => 'in_progress',
'due_date' => date('Y-m-d', strtotime('+14 days')),
'sync_enabled' => true,
'reminder_enabled' => true,
'recurring_enabled' => false,
'project_id' => $webRedesignId,
],
[
'title' => 'Küche planen',
'description' => 'Neue Küche auswählen und bestellen.',
'status' => 'open',
'due_date' => date('Y-m-d', strtotime('+30 days')),
'sync_enabled' => false,
'reminder_enabled' => true,
'recurring_enabled' => false,
'project_id' => $homeRenovationId,
],
[
'title' => 'CodeIgniter lernen',
'description' => 'Offizielle Dokumentation durchgehen.',
'status' => 'completed',
'due_date' => date('Y-m-d', strtotime('-5 days')),
'sync_enabled' => true,
'reminder_enabled' => false,
'recurring_enabled' => false,
'project_id' => $learningId,
],
[
'title' => 'Einkaufen',
'description' => 'Milch, Brot, Eier, Gemüse',
'status' => 'open',
'due_date' => date('Y-m-d', strtotime('+1 day')),
'sync_enabled' => true,
'reminder_enabled' => true,
'recurring_enabled' => false,
'project_id' => null,
],
];
foreach ($todoData as $todo) {
if (!in_array($todo['title'], $existingTodoTitles)) {
$todos[] = array_merge($todo, [
'id' => $generateUuid(),
'user_id' => $userId,
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s'),
]);
}
}
if (!empty($todos)) {
$this->db->table('todos')->insertBatch($todos);
}
// Get all todos for the user
$allTodos = $this->db->table('todos')->where('user_id', $userId)->get()->getResultArray();
// Link todos to categories
$workCategoryId = $categories[0]['id'];
$homeCategoryId = $categories[1]['id'];
$personalCategoryId = $categories[2]['id'];
$todoCategories = [];
$todoCategoryMap = [
'Bestehende Aufgaben analysieren' => $workCategoryId,
'Wireframes erstellen' => $workCategoryId,
'Küche planen' => $homeCategoryId,
'CodeIgniter lernen' => $workCategoryId,
'Einkaufen' => $personalCategoryId,
];
foreach ($allTodos as $todo) {
if (isset($todoCategoryMap[$todo['title']])) {
// Check if this link already exists
$existingLink = $this->db->table('todo_categories')
->where('todo_id', $todo['id'])
->where('category_id', $todoCategoryMap[$todo['title']])
->get()
->getRowArray();
if (!$existingLink) {
$todoCategories[] = [
'todo_id' => $todo['id'],
'category_id' => $todoCategoryMap[$todo['title']],
];
}
}
}
if (!empty($todoCategories)) {
$this->db->table('todo_categories')->insertBatch($todoCategories);
}
// Create sample recurring tasks (check for existing)
$existingRecurringTasks = $this->db->table('recurring_tasks')->where('user_id', $userId)->get()->getResultArray();
$existingRecurringTaskTitles = array_column($existingRecurringTasks, 'title');
$recurringTasks = [];
$recurringTaskData = [
[
'title' => 'Weekly review',
'description' => 'Plan next week\'s tasks',
'schedule' => 'weekly',
'custom_days' => json_encode([]),
'favorite' => true,
],
[
'title' => 'Clean the house',
'description' => 'Every Saturday',
'schedule' => 'custom',
'custom_days' => json_encode(['sat']),
'favorite' => false,
],
[
'title' => 'Daily standup',
'description' => 'Team meeting every morning',
'schedule' => 'daily',
'custom_days' => json_encode([]),
'favorite' => true,
],
];
foreach ($recurringTaskData as $task) {
if (!in_array($task['title'], $existingRecurringTaskTitles)) {
$recurringTasks[] = array_merge($task, [
'id' => $generateUuid(),
'user_id' => $userId,
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s'),
]);
}
}
if (!empty($recurringTasks)) {
$this->db->table('recurring_tasks')->insertBatch($recurringTasks);
}
// Get all recurring tasks for the user
$allRecurringTasks = $this->db->table('recurring_tasks')->where('user_id', $userId)->get()->getResultArray();
// Link recurring tasks to categories
$recurringTaskCategories = [];
$recurringTaskCategoryMap = [
'Weekly review' => $workCategoryId,
'Clean the house' => $homeCategoryId,
'Daily standup' => $workCategoryId,
];
foreach ($allRecurringTasks as $task) {
if (isset($recurringTaskCategoryMap[$task['title']])) {
// Check if this link already exists
$existingLink = $this->db->table('recurring_task_categories')
->where('recurring_task_id', $task['id'])
->where('category_id', $recurringTaskCategoryMap[$task['title']])
->get()
->getRowArray();
if (!$existingLink) {
$recurringTaskCategories[] = [
'recurring_task_id' => $task['id'],
'category_id' => $recurringTaskCategoryMap[$task['title']],
];
}
}
}
if (!empty($recurringTaskCategories)) {
$this->db->table('recurring_task_categories')->insertBatch($recurringTaskCategories);
}
}
}