The Wanderer
Member
Hello,
I would like to create a widget that displays a 'Progress to X Messages'.
I have been playing around with AI tools, the following process and code has been recommended.
I'm looking for feedback on the process and code - any thoughts?
1. Create a Widget Definition
Go to Admin Control Panel → Appearance → Widgets → Add Widget and choose:
2. Add the Callback Code
In your src/XF/Widget/ directory, create a new PHP file called ProgressToX.php and paste:
3. Create a Custom Permission
Go to Admin Control Panel → Groups & Permissions → Permissions → Add Permission and define:
4. Assign Permission to User Groups
Go to Admin → Groups & Permissions → User Group Permissions, and for each group:
Go to Admin → Appearance → Templates → Add Template and name it: widget_process_to_x
Paste this HTML/CSS:
I would like to create a widget that displays a 'Progress to X Messages'.
I have been playing around with AI tools, the following process and code has been recommended.
I'm looking for feedback on the process and code - any thoughts?
1. Create a Widget Definition
Go to Admin Control Panel → Appearance → Widgets → Add Widget and choose:
- Type: PHP Callback
- Display in: Sidebar or wherever you want
- Callback Class: XF:Widget
- Callback Method: renderProgressToX
2. Add the Callback Code
In your src/XF/Widget/ directory, create a new PHP file called ProgressToX.php and paste:
Code:
<?php
namespace XF\Widget;
use XF\Widget\AbstractWidget;
class ProgressToX extends AbstractWidget
{
public function render()
{
$visitor = \XF::visitor();
// Check if user has permission to view the widget
if (!$visitor->hasPermission('forum', 'viewProgressWidget')) {
return ''; // Don't render the widget
}
$target = X;
$current = $this->app->finder('XF:Post')->total();
$percent = min(100, round(($current / $target) * 100, 2));
return $this->renderer('widget_progress_to_X', [
'current' => $current,
'target' => $target,
'percent' => $percent
]);
}
}
3. Create a Custom Permission
Go to Admin Control Panel → Groups & Permissions → Permissions → Add Permission and define:
- Permission Group: forum
- Permission ID: viewProgressWidget
- Title: Can view progress to X messages widget
4. Assign Permission to User Groups
Go to Admin → Groups & Permissions → User Group Permissions, and for each group:
- Set to Yes or No depending on who should see it.
Go to Admin → Appearance → Templates → Add Template and name it: widget_process_to_x
Paste this HTML/CSS:
Code:
<div class="block">
<div class="block-container">
<h3 class="block-header"> Progress to X Messages</h3>
<div class="block-body">
<div style="margin-bottom: 8px;">{$current|number} / {$target|number} messages</div>
<div style="background: #eee; border-radius: 4px; overflow: hidden;">
<div style="width: {$percent}%; background: #4caf50; height: 20px; text-align: center; color: white;">
{$percent}% Complete
</div>
</div>
</div>
</div>
</div>