User::class, 'foreign_key' => 'range_id' ]; parent::configure($config); } /** * Store the default layout for the specified user. * * @param string $user_id */ public static function setInitialWidgets($user_id): void { if (self::countBySQL('range_id = ?', [$user_id]) === 0) { $stmt = DBManager::get()->prepare( 'INSERT INTO widget_user (pluginid, position, range_id, col, chdate) SELECT pluginid, position, :user_id, col, UNIX_TIMESTAMP() FROM widget_default WHERE perm = :perm' ); $stmt->execute([ 'user_id' => $user_id, 'perm' => $GLOBALS['perm']->get_perm($user_id) ]); } } /** * Return the list of widgets (by column) shown to this user. * * @param string $user_id * * @return array array of columns with widget ids */ public static function getWidgets($user_id): array { $widgets = self::findBySQL('is_active = 1 AND range_id = ? ORDER BY position', [$user_id]); $result = []; foreach ($widgets as $widget) { $result[$widget->col][] = $widget->pluginid; } if (empty($result)) { $result = WidgetDefault::getWidgets($GLOBALS['perm']->get_perm($user_id)); } return $result; } /** * Return whether the user has a certain widget enabled. * * @param string $user_id * @param int $plugin_id * * @return bool */ public static function hasWidget($user_id, $plugin_id): bool { $widgets = self::getWidgets($user_id); return in_array($plugin_id, array_merge(...$widgets)); } /** * Add a widget for the given user (left column). * * @param string $user_id * @param string $plugin_id * * @return WidgetUser inserted WidgetUser instance */ public static function addWidget($user_id, $plugin_id): WidgetUser { self::setInitialWidgets($user_id); $stmt = DBManager::get()->prepare('SELECT MAX(position) + 1 FROM widget_user WHERE range_id = ?'); $stmt->execute([$user_id]); $position = $stmt->fetchColumn() ?: 0; return self::create([ 'pluginid' => $plugin_id, 'position' => $position, 'range_id' => $user_id ]); } /** * Remove a widget for the given user (if enabled). * * @param string $user_id * @param string $plugin_id * * @return int number of removed widgets */ public static function removeWidget($user_id, $plugin_id): int { self::setInitialWidgets($user_id); return DBManager::get()->execute("UPDATE widget_user SET is_active = 0 WHERE range_id = ? AND pluginid = ?", [$user_id, $plugin_id]); } }