This repository has been archived on 2024-09-05. You can view files and clone it, but cannot push or open issues or pull requests.
shimmie2/ext/user_config/main.php

58 lines
1.6 KiB
PHP
Raw Normal View History

<?php
2019-07-04 17:48:33 +00:00
/** @var $user_config Config */
global $user_config;
// The user object doesn't exist until after database setup operations and the first wave of InitExtEvents,
// so we can't reliably access this data until then. This event is triggered by the system after all of that is done.
class InitUserConfigEvent extends Event
{
public $user;
public $user_config;
2019-07-04 17:48:33 +00:00
public function __construct(User $user, Config $user_config)
{
$this->user = $user;
2019-07-04 17:48:33 +00:00
$this->user_config = $user_config;
}
}
class UserConfig extends Extension
{
private const VERSION = "ext_user_config_version";
2019-09-29 13:50:31 +00:00
public function onUserLogin(UserLoginEvent $event)
2019-07-04 17:48:33 +00:00
{
global $database, $user_config;
$user_config = new DatabaseConfig($database, "user_config", "user_id", $event->user->id);
2019-09-29 13:50:31 +00:00
send_event(new InitUserConfigEvent($event->user, $user_config));
}
2019-11-03 19:04:57 +00:00
public function onDatabaseUpgrade(DatabaseUpgradeEvent $event): void
{
global $config, $database;
2019-11-03 19:04:57 +00:00
if ($this->get_version(self::VERSION) < 1) {
$database->create_table("user_config", "
user_id INTEGER NOT NULL,
name VARCHAR(128) NOT NULL,
value TEXT,
PRIMARY KEY (user_id, name),
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
");
$database->execute("CREATE INDEX user_config_user_id_idx ON user_config(user_id)");
2019-11-03 19:04:57 +00:00
$this->set_version(self::VERSION, 1);
}
}
// This needs to happen before any other events, but after db upgrade
public function get_priority(): int
{
return 6;
}
}