• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lonnieezell / Bonfire2 / 13932012071

18 Mar 2025 07:37PM UTC coverage: 45.094% (-0.5%) from 45.594%
13932012071

push

github

web-flow
Merge pull request #564 from dgvirtual/prevent-unnecessary-queries-on-widgets

Prevent unnecessary queries related to widgets

47 of 88 new or added lines in 7 files covered. (53.41%)

20 existing lines in 1 file now uncovered.

1590 of 3526 relevant lines covered (45.09%)

63.96 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

71.15
/src/Users/Module.php
1
<?php
2

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

12
namespace Bonfire\Users;
13

14
use Bonfire\Core\BaseModule;
15
use Bonfire\Menus\MenuItem;
16
use Bonfire\Users\Models\UserModel;
17
use Bonfire\Widgets\Types\Stats\StatsItem;
18
use CodeIgniter\View\Table;
19

20
class Module extends BaseModule
21
{
22
    /**
23
     * Setup our admin area needs.
24
     */
25
    public function initAdmin()
26
    {
27
        // Add to the Content menu
28
        $sidebar = service('menus');
438✔
29
        $item    = new MenuItem([
438✔
30
            'title'           => lang('Users.usersModTitle'),
438✔
31
            'namedRoute'      => 'user-list',
438✔
32
            'fontAwesomeIcon' => 'fas fa-users',
438✔
33
            'permission'      => 'users.view',
438✔
34
        ]);
438✔
35
        $sidebar->menu('sidebar')->collection('content')->addItem($item);
438✔
36

37
        // Add Users Settings
38
        $item = new MenuItem([
438✔
39
            'title'           => lang('Users.usersModTitle'),
438✔
40
            'namedRoute'      => 'user-settings',
438✔
41
            'fontAwesomeIcon' => 'fas fa-user-cog',
438✔
42
            'permission'      => 'users.settings',
438✔
43
        ]);
438✔
44
        $sidebar->menu('sidebar')->collection('settings')->addItem($item);
438✔
45

46
        // Settings widgets stats on dashboard
47
        $widgets   = service('widgets');
438✔
48
        $statsItem = new StatsItem([
438✔
49
            'bgColor' => 'bg-danger',
438✔
50
            'title'   => 'Users in Recycler',
438✔
51
            // assigning param directly possible, but it always executes the query, even if not on dashboard or the stats item not enabled, not optimal
52
            // 'value'   => (new UserModel())->onlyDeleted()->countAllResults(),
53
            'id'     => 'usersInRecycler693',
438✔
54
            'url'    => ADMIN_AREA . '/tools/recycler?r=users',
438✔
55
            'faIcon' => 'fa fa-users',
438✔
56
        ]);
438✔
57
        // better way of retrieving deleted users, executes only when widget is displayed:
58
        $statsItem->addValue('users', 'deleted_at IS NOT NULL');
438✔
59
        // or, the following can be used with more complicated queries:
60
        // $statsItem->addValueByFreeQuery('SELECT COUNT(*) AS count FROM users WHERE deleted_at IS NOT NULL;');
61
        $widgets->widget('stats')->collection('stats')->addItem($statsItem);
438✔
62

63
        $widgets    = service('widgets');
438✔
64
        $statsItem1 = new StatsItem([
438✔
65
            'bgColor' => 'bg-purple',
438✔
66
            'title'   => 'Users by Group',
438✔
67
            'id'      => 'usersExpTable693',
438✔
68
            'value'   => $this->buildTableUsersByGroup('usersExpTable693'),
438✔
69
            'url'     => ADMIN_AREA . '/users',
438✔
70
            'faIcon'  => 'fa fa-users',
438✔
71
        ]);
438✔
72
        $widgets->widget('stats')->collection('stats')->addItem($statsItem1);
438✔
73
    }
74

75
    /**
76
     * Build the table for the Users by Group stats item to be displayed within widget.
77
     *
78
     * @param string $statsId - id of the stats item
79
     */
80
    private function buildTableUsersByGroup($statsId): string
81
    {
82
        // Check if we are on Dashboard page and the chart is enabled, return empty string if not
83
        if (current_url() !== config('App')->baseURL . '/' . ADMIN_AREA || setting('Stats.Stats_' . $statsId) !== 'on') {
438✔
84
            return '';
438✔
85
        }
NEW
86
        $users = new UserModel();
×
NEW
87
        $users->select('auth_groups_users.group, COUNT(auth_groups_users.user_id) as count');
×
NEW
88
        $users->join('auth_groups_users', 'auth_groups_users.user_id = users.id');
×
NEW
89
        $users->groupBy('auth_groups_users.group');
×
NEW
90
        $users->orderBy('auth_groups_users.group');
×
NEW
91
        $users = $users->findAll();
×
92

NEW
93
        $table    = new Table();
×
NEW
94
        $template = [
×
NEW
95
            'table_open' => '<table style="width: 80%; background-color: transparent; color: white;">',
×
NEW
96
        ];
×
NEW
97
        $table->setTemplate($template);
×
NEW
98
        $table->setHeading('Group', 'Count');
×
99

NEW
100
        foreach ($users as $user) {
×
NEW
101
            $table->addRow($user->group, $user->count);
×
102
        }
103

NEW
104
        return $table->generate();
×
105
    }
106
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc