Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Card type widget #347

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Bonfire.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Bonfire\Widgets\Types\Charts\Charts;
use Bonfire\Widgets\Types\Stats\Stats;
use Bonfire\Widgets\Types\Cards\Cards;

include_once __DIR__ . '/Common.php';

Expand Down Expand Up @@ -115,6 +116,10 @@ private function setupWidgets()
$widgets->createWidget(Charts::class, 'charts');
$widgets->widget('charts')
->createCollection('charts');

$widgets->createWidget(Cards::class, 'cards');
$widgets->widget('cards')
->createCollection('cards');
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/Dashboard/Config/Dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Dashboard extends BaseConfig
{
public array $cells = [
'Bonfire\Dashboard\DashboardCells::quickLinks',
'Bonfire\Widgets\Cells\WidgetCells::cards',
'Bonfire\Widgets\Cells\WidgetCells::stats',
'Bonfire\Widgets\Cells\WidgetCells::charts',
];
Expand Down
42 changes: 42 additions & 0 deletions src/Groups/Libraries/GroupsCardWidget.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/**
* This file is part of Bonfire.
*
* (c) Lonnie Ezell <lonnieje@gmail.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace Bonfire\Groups\Libraries;

/**
* Provides card widget of groups
*/
class GroupsCardWidget
{
protected $viewPrefix = 'Bonfire\Groups\Views\\';

/**
* Displays the list of most recent logins
*/
public function listGroups()
{

$groups = setting('AuthGroups.groups');
asort($groups);

// Find the number of users in this group
foreach ($groups as $alias => &$group) {
$group['user_count'] = db_connect()
->table('auth_groups_users')
->where('group', $alias)
->countAllResults(true);
}

return view($this->viewPrefix . '_groups_card', [
'groups' => $groups,
]);
}
}
14 changes: 14 additions & 0 deletions src/Groups/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use Bonfire\Menus\MenuItem;
use Bonfire\Widgets\Types\Charts\ChartsItem;
use Bonfire\Widgets\Types\Stats\StatsItem;
use Bonfire\Widgets\Types\Cards\CardsItem;
use Bonfire\Groups\Libraries\GroupsCardWidget;

/**
* User Module setup
Expand All @@ -38,6 +40,7 @@ public function initAdmin()

// Settings widgets stats on dashboard
$widgets = service('widgets');

$groups = setting('AuthGroups.groups');
$statsItem = new StatsItem([
'bgColor' => 'bg-teal',
Expand All @@ -48,6 +51,17 @@ public function initAdmin()
]);
$widgets->widget('stats')->collection('stats')->addItem($statsItem);

// Card on dashboard
$cardsItem = new CardsItem([
'bgColor' => 'bg-teal',
'title' => 'Group List',
'value' => (new GroupsCardWidget)->listGroups(),
'url' => ADMIN_AREA . '/settings/groups',
'faIcon' => 'fas fa-user-friends',
'permission' => 'groups.settings',
]);
$widgets->widget('cards')->collection('cards')->addItem($cardsItem);

// Chart Section Begin
$statsItem = new ChartsItem([
'title' => 'User classification by group',
Expand Down
24 changes: 24 additions & 0 deletions src/Groups/Views/_groups_card.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<table class="table">
<thead>
<tr>
<th>Role</th>
<th>Description</th>
<th class="text-center"># Users</th>
</tr>
</thead>
<tbody>
<?php if (isset($groups) && count($groups)) : ?>
<?php foreach ($groups as $alias => $group) : ?>
<tr>
<td>
<a href="<?= site_url(ADMIN_AREA . '/settings/groups/' . $alias) ?>">
<?= esc($group['title']) ?>
</a>
</td>
<td><?= esc($group['description']) ?></td>
<td class="text-center"><?= esc(number_format($group['user_count'])) ?></td>
</tr>
<?php endforeach ?>
<?php endif ?>
</tbody>
</table>
41 changes: 41 additions & 0 deletions src/Users/Libraries/LoginsCardWidget.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/**
* This file is part of Bonfire.
*
* (c) Lonnie Ezell <lonnieje@gmail.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace Bonfire\Users\Libraries;

use CodeIgniter\Shield\Models\LoginModel;

/**
* Provides recent logins list for dashboard
*/
class LoginsCardWidget
{
protected $viewPrefix = 'Bonfire\Users\Views\\';

/**
* Displays the form fields for user meta fields.
*/
public function showRecentLogins()
{

/** @var LoginModel $loginModel */
$loginModel = model(LoginModel::class);
$logins = $loginModel->select('users.first_name, users.last_name, auth_logins.identifier, auth_logins.ip_address, auth_logins.date, auth_logins.success')
->orderBy('date', 'desc')
->join('users', 'auth_logins.user_id = users.id', 'left')
->findAll(6);


return view($this->viewPrefix . '_recent_logins_card', [
'logins' => $logins,
]);
}
}
17 changes: 17 additions & 0 deletions src/Users/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

use Bonfire\Core\BaseModule;
use Bonfire\Menus\MenuItem;
use Bonfire\Widgets\Types\Cards\CardsItem;
use Bonfire\Users\Libraries\LoginsCardWidget;

class Module extends BaseModule
{
Expand All @@ -39,5 +41,20 @@ public function initAdmin()
'permission' => 'users.settings',
]);
$sidebar->menu('sidebar')->collection('settings')->addItem($item);

// Load widgets service
$widgets = service('widgets');

// Card on dashboard
$cardsItem = new CardsItem([
'bgColor' => 'bg-gray',
'title' => 'Latest logins',
'value' => (new LoginsCardWidget)->showRecentLogins(),
'url' => ADMIN_AREA . '/users',
'faIcon' => 'fas fa-sign-in-alt',
'permission' => 'users.view',
]);
$widgets->widget('cards')->collection('cards')->addItem($cardsItem);

}
}
28 changes: 28 additions & 0 deletions src/Users/Views/_recent_logins_card.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<table class="table">
<thead>
<tr>
<th>Name, IP</th>
<th>Date</th>
<th>OK?</th>
</tr>
</thead>
<?php if (isset($logins) && count($logins)) : ?>
<tbody>
<?php foreach ($logins as $login) : ?>
<tr>
<td><?= ($login->first_name ?? 'unidentified') . '&nbsp' . $login->last_name . '<br>' . $login->ip_address ?></td>
<td><?= app_date($login->date, true, true) ?></td>
<td>
<?php if ((int) $login->success === 1) : ?>
<span class="badge rounded-pill bg-success">Yes</span>
<?php else : ?>
<span class="badge rounded-pill bg-danger">No</span>
<?php endif ?>
</td>
</tr>
<?php endforeach ?>
</tbody>
<?php else : ?>
<div class="alert alert-secondary">No recent login attempts.</div>
<?php endif ?>
</table>
13 changes: 13 additions & 0 deletions src/Widgets/Cells/WidgetCells.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ public function stats()
]);
}

/**
* Displays the card blocks in the admin dashboard.
*/
public function cards()
{
$widgets = service('widgets');

return view('Bonfire\Widgets\Views\Cells\cards', [
'cards' => $widgets->widget('cards')->items(),
'manager' => $widgets->manager(),
]);
}

/**
* Displays the chart blocks in the admin dashboard.
*/
Expand Down
24 changes: 24 additions & 0 deletions src/Widgets/Config/Cards.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/**
* This file is part of Bonfire.
*
* (c) Lonnie Ezell <lonnieje@gmail.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace Bonfire\Widgets\Config;

use CodeIgniter\Config\BaseConfig;

class Cards extends BaseConfig
{
/**
* --------------------------------------------------------------------------
* Show the Link "View Detail"
* --------------------------------------------------------------------------
*/
public bool $cards_showLink = true;
}
2 changes: 2 additions & 0 deletions src/Widgets/Controllers/WidgetsSettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ private function saveWidgetSettings()
private function saveStatsSettings()
{
setting('Stats.stats_showLink', $this->request->getPost('stats_showLink') ?? false);
setting('Cards.cards_showLink', $this->request->getPost('cards_showLink') ?? false);
}

/**
Expand Down Expand Up @@ -254,6 +255,7 @@ public function resetSettings(): \CodeIgniter\HTTP\RedirectResponse
}

setting()->forget('Stats.stats_showLink');
setting()->forget('Cards.cards_showLink');

setting()->forget('LineChart.line_showTitle');
setting()->forget('LineChart.line_showSubTitle');
Expand Down
11 changes: 11 additions & 0 deletions src/Widgets/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,17 @@ public function manager(): array
}
break;

case 'Cards':
foreach ($items as $item) {
$results[] = [
'widget' => $pos,
'title' => $item->title(),
'index' => $i,
];
$i++;
}
break;

case 'Charts':
foreach ($items as $item) {
$results[] = [
Expand Down
58 changes: 58 additions & 0 deletions src/Widgets/Types/Cards/Cards.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/**
* This file is part of Bonfire.
*
* (c) Lonnie Ezell <lonnieje@gmail.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace Bonfire\Widgets\Types\Cards;

use Bonfire\Widgets\BaseWidget;
use Bonfire\Widgets\Interfaces\Widgets;

class Cards extends BaseWidget implements Widgets
{
public function createCollection(string $name): CardsCollection
{
$collection = new CardsCollection();
$collection->setName($name);

$this->items[] = $collection;

return $collection;
}

/**
* Locates a collection by name.
*
* @return mixed
*/
public function collection(string $name)
{
foreach ($this->items as $item) {
if ($item instanceof CardsCollection && $item->name() === $name) {
return $item;
}
}
}

public function collect(string $name, array $items)
{
$collection = $this->collection($name);

if ($collection === null) {
$collection = new CardsCollection();
$collection->setName($name)->setTitle(ucfirst($name));

$this->items[] = $collection;
}

$collection->addItems($items);

return $collection;
}
}
Loading
Loading