forked from OPSnet/Gazelle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Spine
committed
Apr 7, 2024
1 parent
34d5e6d
commit 88d1a19
Showing
13 changed files
with
302 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
namespace Gazelle\Manager; | ||
|
||
class UserNavigation extends \Gazelle\BaseManager { | ||
final public const LIST_KEY = 'unav_list'; | ||
final public const ID_KEY = 'zz_unav_%d'; | ||
|
||
/** | ||
* Create a forum control rule | ||
*/ | ||
public function create( | ||
string $tag, | ||
string $title, | ||
string $target, | ||
string $tests, | ||
bool $testUser, | ||
bool $mandatory, | ||
bool $initial, | ||
): \Gazelle\UserNavigation { | ||
self::$db->prepared_query(" | ||
INSERT INTO nav_items | ||
(tag, title, target, tests, test_user, mandatory, initial) | ||
VALUES (?, ?, ?, ?, ?, ?, ?) | ||
", $tag, $title, $target, $tests, $testUser, $mandatory, $initial | ||
); | ||
$id = self::$db->inserted_id(); | ||
self::$cache->delete_value(self::LIST_KEY); | ||
return new \Gazelle\UserNavigation($id); | ||
} | ||
|
||
public function findById(int $controlId): ?\Gazelle\UserNavigation { | ||
$key = sprintf(self::ID_KEY, $controlId); | ||
$id = self::$cache->get_value($key); | ||
if ($id === false) { | ||
$id = self::$db->scalar(" | ||
SELECT ID FROM forums_nav WHERE ID = ? | ||
", $controlId | ||
); | ||
if (!is_null($id)) { | ||
self::$cache->cache_value($key, $id, 7200); | ||
} | ||
} | ||
return $id ? new \Gazelle\UserNavigation($id) : null; | ||
} | ||
|
||
public function userControlList(\Gazelle\User $user): array { | ||
$navList = $user->navigationList(); | ||
$list = []; | ||
foreach ($this->fullList() as $n) { | ||
if (($n['mandatory'] || in_array($n['id'], $navList)) || (!count($navList) && $n['initial'])) { | ||
$list[] = $n; | ||
} | ||
} | ||
return $list; | ||
} | ||
|
||
public function fullList(): array { | ||
$list = self::$cache->get_value(self::LIST_KEY); | ||
if (!$list) { | ||
self::$db->prepared_query(" | ||
SELECT | ||
id, | ||
tag, | ||
title, | ||
target, | ||
tests, | ||
test_user, | ||
mandatory, | ||
initial | ||
FROM nav_items | ||
"); | ||
$list = self::$db->to_array("id", MYSQLI_ASSOC, false); | ||
self::$cache->cache_value(self::LIST_KEY, $list, 0); | ||
} | ||
return $list; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
namespace Gazelle; | ||
|
||
class UserNavigation extends BaseObject { | ||
final public const tableName = 'nav_items'; | ||
final public const CACHE_KEY = 'unav_%d'; | ||
|
||
public function flush(): static { | ||
self::$cache->delete_value(sprintf(self::CACHE_KEY, $this->id)); | ||
self::$cache->delete_value(Manager\UserNavigation::LIST_KEY); | ||
unset($this->info); | ||
return $this; | ||
} | ||
public function link(): string { | ||
return "<a href=\"{$this->location()}\">User Link Editor</a>"; | ||
} | ||
public function location(): string { | ||
return "tools.php?action=navigation"; | ||
} | ||
|
||
public function info(): array { | ||
if (isset($this->info)) { | ||
return $this->info; | ||
} | ||
$key = sprintf(self::CACHE_KEY, $this->id); | ||
$info = self::$cache->get_value($key); | ||
if ($info === false) { | ||
$info = self::$db->rowAssoc(" | ||
SELECT tag, | ||
title, | ||
target, | ||
tests, | ||
test_user, | ||
mandatory, | ||
initial | ||
FROM nav_items | ||
WHERE id = ? | ||
", $this->id | ||
); | ||
self::$cache->cache_value($key, $info, 86400); | ||
} | ||
$this->info = $info; | ||
return $this->info; | ||
} | ||
|
||
public function isTestUser(): bool { | ||
return $this->info()['test_user']; | ||
} | ||
|
||
public function isMandatory(): bool { | ||
return $this->info()['mandatory']; | ||
} | ||
|
||
public function isInitial(): bool { | ||
return $this->info()['initial']; | ||
} | ||
|
||
public function tag(): string { | ||
return $this->info()['tag']; | ||
} | ||
|
||
public function target(): string { | ||
return $this->info()['target']; | ||
} | ||
|
||
public function tests(): string { | ||
return $this->info()['tests']; | ||
} | ||
|
||
public function title(): string { | ||
return $this->info()['title']; | ||
} | ||
|
||
public function modify(): bool { | ||
$success = parent::modify(); | ||
if ($success) { | ||
self::$cache->delete_value(Manager\UserNavigation::LIST_KEY); | ||
} | ||
return $success; | ||
} | ||
|
||
public function remove(): int { | ||
$id = $this->id; | ||
$this->flush(); | ||
self::$db->prepared_query(" | ||
DELETE FROM nav_items WHERE id = ? | ||
", $this->id | ||
); | ||
$affected = self::$db->affected_rows(); | ||
if ($affected) { | ||
self::$cache->delete_multi([ | ||
sprintf(Manager\UserNavigation::ID_KEY, $id), | ||
Manager\UserNavigation::LIST_KEY, | ||
]); | ||
} | ||
return $affected; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#!/usr/bin/env php | ||
<?php | ||
|
||
require_once(__DIR__ . '/../lib/bootstrap.php'); | ||
|
||
$db = Gazelle\DB::DB(); | ||
|
||
$db->prepared_query(" | ||
SELECT UserID, NavItems FROM users_info WHERE NavItems != ''; | ||
"); | ||
|
||
$manager = new Gazelle\Manager\User; | ||
|
||
$n = 0; | ||
foreach ($db->to_array(false, MYSQLI_NUM, false) as [$id, $items]) { | ||
$user = $manager->findById($id); | ||
if (is_null($user)) { | ||
continue; | ||
} | ||
$user->setField('nav_list', array_map('intval', explode(',', $items)))->modify(); | ||
++$n; | ||
} | ||
|
||
echo "migrated $n users\n"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
misc/phinx/migrations/20240129000000_user_navigation_json.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
use Phinx\Migration\AbstractMigration; | ||
|
||
final class UserNavigationJson extends AbstractMigration { | ||
public function change(): void { | ||
$this->table('users_main') | ||
->addColumn('nav_list', 'json', ['null' => true]) | ||
->save(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.