Skip to content

Commit

Permalink
Merge pull request #10 from humhub-contrib/enh/9-dashboard-display-op…
Browse files Browse the repository at this point in the history
…tions

New options for widget visibility on dashboard
  • Loading branch information
luke- authored Oct 21, 2021
2 parents bfdc971 + c0af30d commit dad27d2
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 81 deletions.
15 changes: 1 addition & 14 deletions Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

namespace humhub\modules\newmembers;

use yii\helpers\Url;
use humhub\models\Setting;
use humhub\modules\newmembers\widgets\NewMembersSidebarWidget;
use yii\helpers\Url;

class Module extends \humhub\components\Module
{
Expand All @@ -27,16 +26,4 @@ public function getConfigUrl()
return Url::to(['/newmembers/config/config']);
}

/**
* @inerhitdoc
*/
public function enable()
{
parent::enable();
Setting::Set('panelTitle', 'New Members', 'newmembers');
Setting::Set('maxMembers', 10, 'newmembers');
}

}

?>
15 changes: 2 additions & 13 deletions controllers/ConfigController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Yii;
use humhub\modules\admin\components\Controller;
use humhub\modules\newmembers\forms\NewMembersConfigureForm;
use humhub\models\Setting;

/**
* Defines the configure actions.
Expand All @@ -22,22 +21,12 @@ class ConfigController extends Controller
public function actionConfig()
{
$form = new NewMembersConfigureForm();
$form->panelTitle = Setting::Get('panelTitle', 'newmembers');
$form->maxMembers = Setting::Get('maxMembers', 'newmembers');
$form->fromDate = Setting::Get('fromDate', 'newmembers');

if ($form->load(Yii::$app->request->post()) && $form->validate()) {
$form->panelTitle = Setting::Set('panelTitle', $form->panelTitle, 'newmembers');
$form->maxMembers = Setting::Set('maxMembers', $form->maxMembers, 'newmembers');
$form->fromDate = Setting::Set('fromDate', $form->fromDate, 'newmembers');

Yii::$app->getSession()->setFlash('data-saved', Yii::t('AdminModule.controllers_SettingController', 'Saved'));
$this->redirect(['/newmembers/config/config']);
if ($form->load(Yii::$app->request->post()) && $form->save()) {
$this->view->saved();
}

return $this->render('config', ['model' => $form]);
}

}

?>
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Changelog
=========

1.0.2 (Unreleased)
----------------------------
- Enh #9: New options for widget visibility on dashboard

1.0.1 (September 23, 2021)
----------------------------
- Enh: Updated module icon
Expand Down
88 changes: 88 additions & 0 deletions forms/NewMembersConfigureForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,120 @@

namespace humhub\modules\newmembers\forms;

use humhub\modules\newmembers\Module;
use Yii;
use yii\base\Model;

class NewMembersConfigureForm extends Model
{
/**
* @var Module $module
*/
public $module;

/**
* @var string $panelTitle
*/
public $panelTitle;

/**
* @var int $maxMembers
*/
public $maxMembers;

/**
* @var string $fromDate
*/
public $fromDate;

/**
* @var bool $displayForMembers
*/
public $displayForMembers;

/**
* @var bool $displayForGuests
*/
public $displayForGuests;

public function init()
{
parent::init();

$this->module = Yii::$app->getModule('newmembers');

$this->panelTitle = $this->module->settings->get('panelTitle', 'New Members');
$this->maxMembers = (int) $this->module->settings->get('maxMembers', 10);
$this->fromDate = $this->module->settings->get('fromDate');
$this->displayForMembers = (bool) $this->module->settings->get('displayForMembers', true);
$this->displayForGuests = (bool) $this->module->settings->get('displayForGuests', true);
}

/**
* @inheritdoc
*/
public function rules()
{
return [
[['maxMembers', 'panelTitle'], 'required'],
['maxMembers', 'integer', 'min' => '0'],
['fromDate', 'date', 'format' => 'yyyy-MM-dd hh:mm:ss'],
[['displayForMembers', 'displayForGuests'], 'boolean'],
];
}

/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'panelTitle' => Yii::t('NewmembersModule.base', 'The panel title for the dashboard widget.'),
'maxMembers' => Yii::t('NewmembersModule.base', 'The number of most active users that will be shown.'),
'fromDate' => Yii::t('NewmembersModule.base', 'From which registration date should new users displayed as new?'),
'displayForMembers' => Yii::t('NewmembersModule.base', 'For logged in members'),
'displayForGuests' => Yii::t('NewmembersModule.base', 'For guests'),
];
}

/**
* @inheritdoc
*/
public function attributeHints()
{
return [
'fromDate' => Yii::t('NewmembersModule.base', 'This value is maybe necessary after an import from existing users. Let it empty if your user base grows naturally.'),
];
}

public function save(): bool
{
if (!$this->validate()) {
return false;
}

$this->module->settings->set('panelTitle', $this->panelTitle);
$this->module->settings->set('maxMembers', $this->maxMembers);
$this->module->settings->set('fromDate', $this->fromDate);
$this->module->settings->set('displayForMembers', $this->displayForMembers);
$this->module->settings->set('displayForGuests', $this->displayForGuests);

return true;
}

/**
* Check if the widget is visible on dashboard for current User
*
* @return bool
*/
public function isVisible(): bool
{
if ($this->maxMembers < 1) {
// Nothing to display
return false;
}

return Yii::$app->user->isGuest ? $this->displayForGuests : $this->displayForMembers;
}

}
2 changes: 1 addition & 1 deletion module.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
}
],
"homepage": "https://github.com/humhub-contrib/newmembers",
"version": "1.0.1",
"version": "1.0.2",
"humhub": {
"minVersion": "1.4"
}
Expand Down
53 changes: 17 additions & 36 deletions views/config/config.php
Original file line number Diff line number Diff line change
@@ -1,50 +1,31 @@
<?php

use yii\helpers\Url;
use yii\helpers\Html;
use humhub\widgets\DataSaved;
use humhub\compat\CActiveForm;
use humhub\libs\Html;
use humhub\modules\newmembers\forms\NewMembersConfigureForm;
use humhub\widgets\Button;
use yii\bootstrap\ActiveForm;

/* @var NewMembersConfigureForm $model */
?>

<div class="panel panel-default">
<div
class="panel-heading"><?= Yii::t('NewmembersModule.base', '<strong>New Members</strong> Module Configuration'); ?></div>
<div class="panel-heading"><?= Yii::t('NewmembersModule.base', '<strong>New Members</strong> Module Configuration'); ?></div>
<div class="panel-body">
<?php $form = ActiveForm::begin() ?>

<br>

<?php $form = CActiveForm::begin(); ?>

<?= $form->errorSummary($model); ?>

<div class="form-group">
<?= $form->labelEx($model, 'panelTitle'); ?>
<?= $form->textField($model, 'panelTitle', ['class' => 'form-control']); ?>
<?= $form->error($model, 'panelTitle'); ?>
</div>

<div class="form-group">
<?= $form->labelEx($model, 'maxMembers'); ?>
<?= $form->textField($model, 'maxMembers', ['class' => 'form-control']); ?>
<?= $form->error($model, 'maxMembers'); ?>
</div>

<div class="form-group">
<?= $form->labelEx($model, 'fromDate'); ?>
<?= $form->textField($model, 'fromDate', ['class' => 'form-control']); ?>
<p class="help-block"><?= Yii::t('NewmembersModule.base', 'This value is maybe necessary after an import from existing users. Let it empty if your user base grows naturally.'); ?></p>
<?= $form->error($model, 'fromDate'); ?>
<?= $form->field($model, 'panelTitle') ?>
<?= $form->field($model, 'maxMembers') ?>
<?= $form->field($model, 'fromDate') ?>

</div>
<?= Html::label(Yii::t('NewmembersModule.base', 'Show on dashboard'), null, ['class' => 'control-label']) ?>
<?= $form->field($model, 'displayForMembers')->checkbox() ?>
<?= $form->field($model, 'displayForGuests')->checkbox() ?>

<hr>
<?php echo Html::submitButton(Yii::t('NewmembersModule.base', 'Save'), ['class' => 'btn btn-primary']); ?>
<a class="btn btn-default"
href="<?= Url::to(['/admin/module']); ?>"><?= Yii::t('NewmembersModule.base', 'Back to modules'); ?></a>

<!-- show flash message after saving -->
<?= DataSaved::widget(); ?>
<?= Button::save()->submit() ?>
<?= Button::defaultType(Yii::t('NewmembersModule.base', 'Back to modules'))->link(['/admin/module']) ?>

<?php CActiveForm::end(); ?>
<?php ActiveForm::end() ?>
</div>
</div>
32 changes: 16 additions & 16 deletions widgets/NewMembersSidebarWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace humhub\modules\newmembers\widgets;

use humhub\models\Setting;
use humhub\components\Widget;
use humhub\modules\newmembers\forms\NewMembersConfigureForm;
use humhub\modules\user\models\User;

/**
Expand All @@ -12,38 +13,37 @@
* @since 0.11
* @author Andreas Strobel
*/
class NewMembersSidebarWidget extends \humhub\components\Widget
class NewMembersSidebarWidget extends Widget
{

/**
* Execute widget
*/
public function run()
{
$maxMembers = (int) Setting::Get('maxMembers', 'newmembers');
$fromDate = Setting::Get('fromDate', 'newmembers');
$config = new NewMembersConfigureForm();

if (!$config->isVisible()) {
return '';
}

$newMembersQuery = User::find();
$newMembersQuery->limit($maxMembers);
$newMembersQuery->limit($config->maxMembers);
$newMembersQuery->andWhere(['user.status' => User::STATUS_ENABLED]);
$newMembersQuery->orderBy(['user.created_at' => SORT_DESC]);
if ($fromDate != null && $fromDate != "") {
$newMembersQuery->andWhere(['>=', 'user.created_at', $fromDate]);
if (!empty($config->fromDate)) {
$newMembersQuery->andWhere(['>=', 'user.created_at', $config->fromDate]);
}

$newMembers = $newMembersQuery->all();

if (count($newMembers) == 0) {
return;
if (empty($newMembers)) {
return '';
}

return $this->render('newMembers', array(
'newUsers' => $newMembers,
'title' => Setting::Get('panelTitle', 'newmembers')
));
return $this->render('newMembers', [
'newUsers' => $newMembers,
'title' => $config->panelTitle,
]);
}

}

?>
6 changes: 5 additions & 1 deletion widgets/views/newMembers.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
<?php

use humhub\modules\user\models\User;
use humhub\widgets\PanelMenu;
use yii\helpers\Html;

/* @var string $title */
/* @var User[] $newUsers */
?>

<div class="panel panel-default panel-new-people" id="panel-new-people">
<?= PanelMenu::widget(['id' => 'panel-new-people']); ?>
<div class="panel-heading">
<div class="panel-heading">
<?= $title; ?>
</div>
<div class="panel-body">
Expand Down

0 comments on commit dad27d2

Please sign in to comment.