Skip to content

Commit

Permalink
Add Expiration Date
Browse files Browse the repository at this point in the history
  • Loading branch information
gevorgmansuryan committed Jun 11, 2024
1 parent 1d27b3a commit 340f318
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 5 deletions.
9 changes: 9 additions & 0 deletions Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ public static function showBreakingNews()
return false;
}

if ($expires = $module->settings->get('expiresAt')) {
$now = new \DateTime('now', new \DateTimeZone('UTC'));
$expires = new \DateTime($expires, new \DateTimeZone('UTC'));

if ($expires < $now) {
return false;
}
}

// Check group restrictions
$activeGroups = $module->settings->getSerialized('activeGroups');
// If no group is ticked, everyone will see this breaking news
Expand Down
19 changes: 19 additions & 0 deletions assets/BreakingNewsAsset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace humhub\modules\breakingnews\assets;

use yii\web\AssetBundle;

class BreakingNewsAsset extends AssetBundle
{
public $sourcePath = '@breakingnews/resources';
public $css = [];
public $js = [
'js/humhub.breakingnews.js'
];

public $publishOptions = [
'forceCopy' => true,
];

}
2 changes: 1 addition & 1 deletion controllers/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function actionIndex()
{
$form = new EditForm();

if ($form->load(Yii::$app->request->post()) && $form->validate() && $form->save()) {
if ($form->load(Yii::$app->request->post()) && $form->save()) {

return $this->redirect(Url::to(['/breakingnews/admin/index']));
}
Expand Down
27 changes: 27 additions & 0 deletions models/EditForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

namespace humhub\modules\breakingnews\models;

use DateTime;
use DateTimeZone;
use humhub\modules\breakingnews\Module;
use humhub\modules\content\widgets\richtext\RichText;
use humhub\libs\DbDateValidator;
use Yii;
use yii\base\Model;

Expand All @@ -15,11 +18,14 @@ class EditForm extends Model
private $module;

public $active;
public $expiresAt;
public $activeGroups;
public $title;
public $message;
public $reset;

public $expiresTime;

/**
* @inheritdoc
*/
Expand All @@ -29,8 +35,25 @@ public function init()

$this->title = $this->module->settings->get('title');
$this->message = $this->module->settings->get('message');
$this->expiresAt = $this->module->settings->get('expiresAt');
$this->active = $this->module->settings->get('active');
$this->activeGroups = $this->module->settings->getSerialized('activeGroups');

$this->initTime();
}

private function initTime()
{
if ($this->expiresAt === null) {
$this->expiresTime = '';
} else {
$expiresAt = new DateTime($this->expiresAt, new DateTimeZone('UTC'));
$this->expiresTime = Yii::$app->formatter->asTime($expiresAt, 'short');

if ($expiresAt < new DateTime('now', new DateTimeZone('UTC'))) {
$this->active = false;
}
}
}

/**
Expand All @@ -40,6 +63,8 @@ public function rules()
{
return [
[['title', 'message'], 'required'],
[['expiresAt'], DbDateValidator::class, 'timeAttribute' => 'expiresTime'],
[['expiresTime'], 'date', 'type' => 'time', 'format' => Yii::$app->formatter->isShowMeridiem() ? 'h:mm a' : 'php:H:i'],
[['reset', 'active', 'activeGroups'], 'safe'],
];
}
Expand All @@ -51,6 +76,7 @@ public function attributeLabels()
{
return [
'active' => Yii::t('BreakingnewsModule.forms_BreakingNewsEditForm', 'Active'),
'expiresAt' => Yii::t('BreakingnewsModule.forms_BreakingNewsEditForm', 'Expired at'),
'activeGroups' => Yii::t('BreakingnewsModule.forms_BreakingNewsEditForm', 'Groups whose members will see this breaking news'),
'title' => Yii::t('BreakingnewsModule.forms_BreakingNewsEditForm', 'Title'),
'message' => Yii::t('BreakingnewsModule.forms_BreakingNewsEditForm', 'Message'),
Expand Down Expand Up @@ -81,6 +107,7 @@ public function save(): bool
$this->module->settings->set('title', $this->title);
$this->module->settings->set('message', $this->message);
$this->module->settings->set('active', $this->active);
$this->module->settings->set('expiresAt', $this->expiresAt);
$this->module->settings->setSerialized('activeGroups', $this->activeGroups);

$lastTimeStamp = $this->module->settings->get('timestamp');
Expand Down
13 changes: 13 additions & 0 deletions resources/js/humhub.breakingnews.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
humhub.module('breakingnews', function (module, require, $) {

var changeStatus = function(event) {
var isChecked = event.$target.prop('checked');
var expirationRow = $('#expiration_row');

isChecked ? expirationRow.show() : expirationRow.hide();
}

module.export({
changeStatus: changeStatus,
});
});
35 changes: 33 additions & 2 deletions views/admin/index.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,52 @@
<?php

use humhub\modules\breakingnews\assets\BreakingNewsAsset;
use humhub\modules\breakingnews\models\EditForm;
use humhub\modules\content\widgets\richtext\RichTextField;
use humhub\modules\ui\form\widgets\ActiveForm;
use humhub\modules\user\models\Group;
use humhub\widgets\Button;
use humhub\modules\ui\form\widgets\DatePicker;
use humhub\modules\ui\form\widgets\TimePicker;
use humhub\libs\Html;
use yii\helpers\ArrayHelper;
use yii\web\JsExpression;
use yii\web\View;

/**
* @var EditForm $model
* @var View $this
*/

BreakingNewsAsset::register($this);

/* @var EditForm $model */
?>
<div class="panel panel-default">
<div class="panel-heading"><?= Yii::t('BreakingnewsModule.views_admin_index', 'Breaking News Configuration') ?></div>
<div class="panel-body">

<?php $form = ActiveForm::begin() ?>

<?= $form->field($model, 'active')->checkbox() ?>
<?= $form->field($model, 'active')->checkbox(['data' => ['action-change' => 'breakingnews.changeStatus']]) ?>

<div class="row" id="expiration_row" style="<?= !$model->active ? Html::cssStyleFromArray(['display' => 'none']) : '' ?>">
<div class="col-sm-6 col-xs-6" style="z-index: 10">
<?= $form
->field($model, 'expiresAt')
->widget(DatePicker::class, [
'clientOptions' => [
'minDate' => new JsExpression('new Date()'),
],
]) ?>
</div>
<div class="col-sm-6 col-xs-6">
<?= $form
->field($model, 'expiresTime')
->widget(TimePicker::class)
->label("&nbsp") ?>
</div>
</div>

<?= $form->field($model, 'title') ?>
<?= $form->field($model, 'message')->widget(RichTextField::class) ?>
<?= $form->beginCollapsibleFields(Yii::t('BreakingnewsModule.views_admin_index', 'Groups restriction')) ?>
Expand Down
2 changes: 0 additions & 2 deletions widgets/BreakingNewsWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,3 @@ public function run()
}

}

?>

0 comments on commit 340f318

Please sign in to comment.