Diff #4 - trunk/src/Opus/DataTables/LoginHistory.php
3,193 bytes
|
|
January 20, 2025 at 08:20
|
Diff
Index: LoginHistory.php
--- LoginHistory.php (nonexistent) +++ LoginHistory.php (revision 4) @@ -0,0 +1,120 @@ +<?php +declare(strict_types = 1); + +namespace App\Webapp\Opus\DataTables; + +use Apex\Svc{App, Db, Convert}; +use Apex\Armor\User\Extra\LoginHistory as ArmorHistory; +use Apex\App\Interfaces\Opus\DataTableInterface; + +/** + * Data Table - LoginHistory + */ +class LoginHistory implements DataTableInterface +{ + + // Columns + public array $columns = [ + 'created_at' => 'Date', + 'username' => 'User', + 'ip_address' => 'IP Address', + 'user_agent' => 'User Agent', + 'view' => 'View' + ]; + public array $sortable = ['id']; + + // Other variables + public int $rows_per_page = 25; + public bool $has_search = false; + + // Form field (left-most column) + public string $form_field = 'none'; + public string $form_name = 'login_history_id'; + public string $form_value = 'id'; + + + /** + * Constructor + * + * All attributes within the ERROR: No 'alias' attribute exists within the function tag. tag are passed to this method during instantiation, + * and here you can define various properties (ie. userid, type, et al) to be used in below methods. + */ + public function __construct( + private array $attr, + private App $app, + private Db $db, + private Convert $convert, + private ArmorHistory $history + ) { + $this->uuid = $this->attr['uuid'] ?? ''; + $this->type = $attr['type'] ?? ''; + + if ($this->uuid != '') { + unset($this->columns['username']); + } + } + + /** + * Get total rows in data set - used for pagination. + */ + public function getTotal(string $search_term = ''):int + { + + if ($this->type != '') { + $total = $this->history->getCountType($this->type, false); + } else { + $total = $this->history->getCountUuid($this->uuid); + } + + // Return + return (int) $total; + } + + /** + * Get rows to display on current page. + * + * Should return an array with each element being an associative array representing one table row. + */ + public function getRows(int $start = 0, string $search_term = '', string $order_by = 'id asc'):array + { + + // Get rows + if ($this->type != '') { + $rows = $this->history->listType($this->type, false, $start, $this->rows_per_page); + } else { + $rows = $this->history->listUuid($this->uuid, $start, $this->rows_per_page); + } + + // Go through rows + $results = []; + foreach ($rows as $row) { + $results[] = $this->formatRow($row); + } + + // Return + return $results; + } + + /** + * Format individual row for display to browser. + */ + public function formatRow(array $row):array + { + + // Initialize + $user = $row['user'] ?? null; + + // Format + $date = $row['created_at']; + $row['created_at'] = $this->convert->date($date, true); + if ($user !== null) { + $row['username'] = $user->getUsername() . ' (' . $user->getFullName() . ')'; + } + $row['view'] = "
Full Code
<?php declare(strict_types = 1);
namespace App\Webapp\Opus\DataTables;
use Apex\Svc{App, Db, Convert}; use Apex\Armor\User\Extra\LoginHistory as ArmorHistory; use Apex\App\Interfaces\Opus\DataTableInterface;
/** * Data Table - LoginHistory */ class LoginHistory implements DataTableInterface {
// Columns
public array $columns = [
'created_at' => 'Date',
'username' => 'User',
'ip_address' => 'IP Address',
'user_agent' => 'User Agent',
'view' => 'View'
];
public array $sortable = ['id'];
// Other variables
public int $rows_per_page = 25;
public bool $has_search = false;
// Form field (left-most column)
public string $form_field = 'none';
public string $form_name = 'login_history_id';
public string $form_value = 'id';
/**
* Constructor
*
* All attributes within the <syrus20> tag are passed to this method during instantiation,
* and here you can define various properties (ie. userid, type, et al) to be used in below methods.
*/
public function __construct(
private array $attr,
private App $app,
private Db $db,
private Convert $convert,
private ArmorHistory $history
) {
$this->uuid = $this->attr['uuid'] ?? '';
$this->type = $attr['type'] ?? '';
if ($this->uuid != '') {
unset($this->columns['username']);
}
}
/**
* Get total rows in data set - used for pagination.
*/
public function getTotal(string $search_term = ''):int
{
if ($this->type != '') {
$total = $this->history->getCountType($this->type, false);
} else {
$total = $this->history->getCountUuid($this->uuid);
}
// Return
return (int) $total;
}
/**
* Get rows to display on current page.
*
* Should return an array with each element being an associative array representing one table row.
*/
public function getRows(int $start = 0, string $search_term = '', string $order_by = 'id asc'):array
{
// Get rows
if ($this->type != '') {
$rows = $this->history->listType($this->type, false, $start, $this->rows_per_page);
} else {
$rows = $this->history->listUuid($this->uuid, $start, $this->rows_per_page);
}
// Go through rows
$results = [];
foreach ($rows as $row) {
$results[] = $this->formatRow($row);
}
// Return
return $results;
}
/**
* Format individual row for display to browser.
*/
public function formatRow(array $row):array
{
// Initialize
$user = $row['user'] ?? null;
// Format
$date = $row['created_at'];
$row['created_at'] = $this->convert->date($date, true);
if ($user !== null) {
$row['username'] = $user->getUsername() . ' (' . $user->getFullName() . ')';
}
$row['view'] = "<center><syrus21></center>";
// Return
return $row;
}
}