microcrud for user list
This commit is contained in:
parent
4e03d3cce3
commit
d2b50573c6
3 changed files with 74 additions and 118 deletions
18
composer.lock
generated
18
composer.lock
generated
|
@ -342,16 +342,16 @@
|
|||
},
|
||||
{
|
||||
"name": "shish/ffsphp",
|
||||
"version": "v0.0.1",
|
||||
"version": "v0.0.2",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/shish/ffsphp.git",
|
||||
"reference": "6b1874cf05b0b6bbdf7b118ca081097d1f830cd7"
|
||||
"reference": "16c98d57c80bb4848f20253c8c1e5fe7f6c5823f"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/shish/ffsphp/zipball/6b1874cf05b0b6bbdf7b118ca081097d1f830cd7",
|
||||
"reference": "6b1874cf05b0b6bbdf7b118ca081097d1f830cd7",
|
||||
"url": "https://api.github.com/repos/shish/ffsphp/zipball/16c98d57c80bb4848f20253c8c1e5fe7f6c5823f",
|
||||
"reference": "16c98d57c80bb4848f20253c8c1e5fe7f6c5823f",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
|
@ -380,7 +380,7 @@
|
|||
],
|
||||
"description": "A collection of workarounds for stupid PHP things",
|
||||
"homepage": "https://github.com/shish/ffsphp",
|
||||
"time": "2019-11-25T15:37:09+00:00"
|
||||
"time": "2019-11-29T12:00:09+00:00"
|
||||
},
|
||||
{
|
||||
"name": "shish/microcrud",
|
||||
|
@ -388,12 +388,12 @@
|
|||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/shish/microcrud.git",
|
||||
"reference": "7c917baa46f137c5e0f6bd4d9874b1c61014797e"
|
||||
"reference": "2a7ae6efe5b2c8aa9d68436cfa310cea5ad0e653"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/shish/microcrud/zipball/7c917baa46f137c5e0f6bd4d9874b1c61014797e",
|
||||
"reference": "7c917baa46f137c5e0f6bd4d9874b1c61014797e",
|
||||
"url": "https://api.github.com/repos/shish/microcrud/zipball/2a7ae6efe5b2c8aa9d68436cfa310cea5ad0e653",
|
||||
"reference": "2a7ae6efe5b2c8aa9d68436cfa310cea5ad0e653",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
|
@ -429,7 +429,7 @@
|
|||
"crud",
|
||||
"generator"
|
||||
],
|
||||
"time": "2019-11-28T21:02:52+00:00"
|
||||
"time": "2019-11-29T21:15:17+00:00"
|
||||
},
|
||||
{
|
||||
"name": "shish/microhtml",
|
||||
|
|
|
@ -2,6 +2,61 @@
|
|||
|
||||
require_once "events.php";
|
||||
|
||||
use function MicroHTML\A;
|
||||
use MicroCRUD\Column;
|
||||
use MicroCRUD\EnumColumn;
|
||||
use MicroCRUD\TextColumn;
|
||||
use MicroCRUD\Table;
|
||||
|
||||
class UserNameColumn extends TextColumn {
|
||||
public function display(array $row) {
|
||||
return A(["href"=>make_link("user/{$row[$this->name]}")], $row[$this->name]);
|
||||
}
|
||||
}
|
||||
|
||||
class UserLinksColumn extends Column {
|
||||
public function __construct() {
|
||||
parent::__construct("links", "User Links", "(1=1)");
|
||||
$this->sortable = false;
|
||||
}
|
||||
public function create_input(array $inputs) {
|
||||
return "";
|
||||
}
|
||||
public function read_input(array $inputs) {
|
||||
return "";
|
||||
}
|
||||
public function display(array $row) {
|
||||
return A(["href"=>make_link("post/list/user_id={$row['id']}/1")], "Posts");
|
||||
}
|
||||
}
|
||||
|
||||
class UserTable extends Table
|
||||
{
|
||||
public function __construct(\FFSPHP\PDO $db)
|
||||
{
|
||||
global $_shm_user_classes;
|
||||
$classes = [];
|
||||
foreach($_shm_user_classes as $cls) {
|
||||
$classes[$cls->name] = $cls->name;
|
||||
}
|
||||
ksort($classes);
|
||||
parent::__construct($db);
|
||||
$this->table = "users";
|
||||
$this->base_query = "SELECT * FROM users";
|
||||
$this->size = 100;
|
||||
$this->limit = 1000000;
|
||||
$this->columns = [
|
||||
new UserNameColumn("name", "Name"),
|
||||
new EnumColumn("class", "Class", $classes),
|
||||
// Added later, for admins only
|
||||
// new TextColumn("email", "Email"),
|
||||
new UserLinksColumn(),
|
||||
];
|
||||
$this->order_by = ["name"];
|
||||
$this->table_attrs = ["class" => "zebra"];
|
||||
}
|
||||
}
|
||||
|
||||
class UserCreationException extends SCoreException
|
||||
{
|
||||
}
|
||||
|
@ -51,36 +106,15 @@ class UserPage extends Extension
|
|||
} elseif ($event->get_arg(0) == "create") {
|
||||
$this->page_create();
|
||||
} elseif ($event->get_arg(0) == "list") {
|
||||
$limit = 50;
|
||||
|
||||
$page_num = $event->try_page_num(1);
|
||||
$offset = ($page_num-1) * $limit;
|
||||
|
||||
$q = "WHERE 1=1";
|
||||
$a = [];
|
||||
|
||||
if (@$_GET['username']) {
|
||||
$q .= " AND SCORE_STRNORM(name) LIKE SCORE_STRNORM(:name)";
|
||||
$a["name"] = '%' . $_GET['username'] . '%';
|
||||
$t = new UserTable($database->raw_db());
|
||||
$t->token = $user->get_auth_token();
|
||||
$t->inputs = $_GET;
|
||||
if ($user->can(Permissions::DELETE_USER)) {
|
||||
$col = new TextColumn("email", "Email");
|
||||
// $t->columns[] = $col;
|
||||
array_splice($t->columns, 2, 0, [$col]);
|
||||
}
|
||||
|
||||
if ($user->can(Permissions::DELETE_USER) && @$_GET['email']) {
|
||||
$q .= " AND SCORE_STRNORM(email) LIKE SCORE_STRNORM(:email)";
|
||||
$a["email"] = '%' . $_GET['email'] . '%';
|
||||
}
|
||||
|
||||
if (@$_GET['class']) {
|
||||
$q .= " AND class LIKE :class";
|
||||
$a["class"] = $_GET['class'];
|
||||
}
|
||||
$where = $database->scoreql_to_sql($q);
|
||||
|
||||
$count = $database->get_one("SELECT count(*) FROM users $where", $a);
|
||||
$a["offset"] = $offset;
|
||||
$a["limit"] = $limit;
|
||||
$rows = $database->get_all("SELECT * FROM users $where LIMIT :limit OFFSET :offset", $a);
|
||||
$users = array_map("_new_user", $rows);
|
||||
$this->theme->display_user_list($page, $users, $user, $page_num, $count/$limit);
|
||||
$this->theme->display_user_list($page, $t->table($t->query()), $t->paginator());
|
||||
} elseif ($event->get_arg(0) == "logout") {
|
||||
$this->page_logout();
|
||||
}
|
||||
|
@ -185,7 +219,6 @@ class UserPage extends Extension
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
private function display_stats(UserPageBuildingEvent $event)
|
||||
{
|
||||
global $user, $page, $config;
|
||||
|
@ -202,7 +235,6 @@ class UserPage extends Extension
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
if ($user->id == $event->display_user->id) {
|
||||
$ubbe = new UserBlockBuildingEvent();
|
||||
send_event($ubbe);
|
||||
|
|
|
@ -13,88 +13,12 @@ class UserPageTheme extends Themelet
|
|||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* #param User[] $users
|
||||
*/
|
||||
public function display_user_list(Page $page, array $users, User $user, int $page_num, int $page_total)
|
||||
public function display_user_list(Page $page, $table, $paginator)
|
||||
{
|
||||
$page->set_title("User List");
|
||||
$page->set_heading("User List");
|
||||
$page->add_block(new NavBlock());
|
||||
|
||||
$html = "<table class='zebra'>";
|
||||
|
||||
$html .= "<tr>";
|
||||
$html .= "<td>Name</td>";
|
||||
if ($user->can(Permissions::DELETE_USER)) {
|
||||
$html .= "<td>Email</td>";
|
||||
}
|
||||
$html .= "<td>Class</td>";
|
||||
$html .= "<td>Action</td>";
|
||||
$html .= "</tr>";
|
||||
|
||||
$h_username = html_escape(@$_GET['username']);
|
||||
$h_email = html_escape(@$_GET['email']);
|
||||
$h_class = html_escape(@$_GET['class']);
|
||||
|
||||
$html .= "<tr>" . make_form("user_admin/list", "GET");
|
||||
$html .= "<td><input type='text' name='username' value='$h_username'/></td>";
|
||||
if ($user->can(Permissions::DELETE_USER)) {
|
||||
$html .= "<td><input type='text' name='email' value='$h_email'/></td>";
|
||||
}
|
||||
$html .= "<td><input type='text' name='class' value='$h_class'/></td>";
|
||||
$html .= "<td><input type='submit' value='Search'/></td>";
|
||||
$html .= "</form></tr>";
|
||||
|
||||
foreach ($users as $duser) {
|
||||
$h_name = html_escape($duser->name);
|
||||
$h_email = html_escape($duser->email);
|
||||
$h_class = html_escape($duser->class->name);
|
||||
$u_link = make_link("user/" . url_escape($duser->name));
|
||||
$u_posts = make_link("post/list/user_id=" . url_escape($duser->id) . "/1");
|
||||
|
||||
$html .= "<tr>";
|
||||
$html .= "<td><a href='$u_link'>$h_name</a></td>";
|
||||
if ($user->can(Permissions::DELETE_USER)) {
|
||||
$html .= "<td>$h_email</td>";
|
||||
}
|
||||
$html .= "<td>$h_class</td>";
|
||||
$html .= "<td><a href='$u_posts'>Show Posts</a></td>";
|
||||
$html .= "</tr>";
|
||||
}
|
||||
|
||||
$html .= "</table>";
|
||||
|
||||
$page->add_block(new Block("Users", $html));
|
||||
$this->display_paginator($page, "user_admin/list", $this->get_args(), $page_num, $page_total);
|
||||
}
|
||||
|
||||
protected function ueie($var)
|
||||
{
|
||||
if (isset($_GET[$var])) {
|
||||
return $var."=".url_escape($_GET[$var]);
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
protected function get_args()
|
||||
{
|
||||
$args = "";
|
||||
// Check if each arg is actually empty and skip it if so
|
||||
if (strlen($this->ueie("username"))) {
|
||||
$args .= $this->ueie("username")."&";
|
||||
}
|
||||
if (strlen($this->ueie("email"))) {
|
||||
$args .= $this->ueie("email")."&";
|
||||
}
|
||||
if (strlen($this->ueie("class"))) {
|
||||
$args .= $this->ueie("class")."&";
|
||||
}
|
||||
// If there are no args at all, set $args to null to prevent an unnecessary ? at the end of the paginator url
|
||||
if (strlen($args) == 0) {
|
||||
$args = null;
|
||||
}
|
||||
return $args;
|
||||
$page->add_block(new Block("Users", $table . $paginator));
|
||||
}
|
||||
|
||||
public function display_user_links(Page $page, User $user, $parts)
|
||||
|
|
Reference in a new issue