Skip to content

Commit

Permalink
Merge pull request #1153 from proditis/issue-1151
Browse files Browse the repository at this point in the history
Add support for dynamic menus fixes #1151
  • Loading branch information
proditis authored Apr 18, 2024
2 parents 33242d8 + 642252b commit f105c5a
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

use yii\db\Migration;

/**
* Class m240418_224927_update_existing_discord_link_entries
*/
class m240418_224927_update_existing_discord_link_entries extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{
if(Yii::$app->sys->discord_invite_url!==false && Yii::$app->sys->discord_invite_url!=="")
{
$link=json_encode([
[
'name'=>'<i class="fab fa-discord text-discord"></i><p class="text-discord">Join our Discord!</p>',
'link'=>Yii::$app->sys->discord_invite_url
]
]);
$this->update('sysconfig',['val'=>$link,'id'=>'menu_items'],['id'=>'discord_invite_url']);
}
}

/**
* {@inheritdoc}
*/
public function safeDown()
{
echo "m240418_224927_update_existing_discord_link_entries cannot be reverted.\n";
}
}
26 changes: 26 additions & 0 deletions backend/modules/content/controllers/DefaultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,32 @@ public function actionFrontpageScenario()
]);
}

/**
* Updates menu items sysconfig key.
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionMenuItems()
{
$model=Sysconfig::findOneNew('menu_items');
if(Yii::$app->request->isPost)
{
$items=[];
foreach(Yii::$app->request->post('item') as $item)
{
if(trim($item['name'])!== "")
$items[]=$item;
}
if(($model->val=json_encode($items)) && $model->save())
Yii::$app->session->setFlash('success', Yii::t('app','Menu items updated'));
}

return $this->render('menu_items', [
'model' => $model,
'hint'=>Yii::t('app','Add or remove menu items from the frontend.')
]);
}

/**
* Updates Writeup Rules sysconfig key.
* @return mixed
Expand Down
11 changes: 11 additions & 0 deletions backend/modules/content/views/default/help/menu-items.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Menu items allow you to add entries to the frontend menu.

These support full HTML mode so that you can add icons and
other details that may be needed by some special cases.

The two fields are:
* **Link name**: The name that will be displayed for the entry. (eg. `<i class="fab fa-discord text-discord"></i><p class="text-discord">Join our Discord!</p>`)
* **URL**: An optional url that we will link to for this entry (eg. `http://example.com/`)


**WARNING:** There is no validation performed to the fields. Be careful not to break the frontend HTML.
64 changes: 64 additions & 0 deletions backend/modules/content/views/default/menu_items.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

use yii\helpers\Html;

use yii\widgets\ActiveForm;

/* @var $this yii\web\View */
/* @var $model app\modules\settings\models\Sysconfig */

$this->title='Update '.$model->id;
$this->params['breadcrumbs'][]=ucfirst(Yii::$app->controller->module->id);
$this->params['breadcrumbs'][]=$model->id;
yii\bootstrap5\Modal::begin([
'title' => '<h2><i class="bi bi-info-circle-fill"></i> '.Html::encode($this->title).' Help</h2>',
'toggleButton' => ['label' => '<i class="bi bi-info-circle-fill"></i> Help','class'=>'btn btn-info'],
]);
echo yii\helpers\Markdown::process($this->render('help/'.$this->context->action->id), 'gfm');
yii\bootstrap5\Modal::end();

?>
<div class="sysconfig-update">

<h1><?= Html::encode($this->title) ?></h1>

<?= Html::beginForm([''], 'POST') ?>
<div class="hint-block"><?=$hint?></div>
<?php
try {
$i=0;
foreach(json_decode($model->val,true) as $i => $item) {
echo '<div class="row">';
echo '<div class="col">',Html::label('Link name', "item[$i][name]", ['class' => 'label name']);
echo Html::input('text',"item[$i][name]",$item['name'], ['class'=>'form-control','style'=>"font-family:monospace;"]);
echo '<div class="hint-block">Update existing or delete link.</div></div>',"\n";

echo '<div class="col">',Html::label('URL', "item[$i][link]", ['class' => 'label link']);
echo Html::input('text',"item[$i][link]",$item['link'], ['class'=>'form-control','style'=>"font-family:monospace;"]);
echo '<div class="hint-block">Update or delete URL.</div></div>',"\n";
echo '</div><hr/>',"\n";
}
}
catch(\TypeError $e) {}
?>
<div class="row">
<div class="col">
<label class="label name" for="item[<?=intval($i)+1;?>][name]">Link name</label>
<input type="text" name="item[<?=intval($i)+1;?>][name]" class='form-control' style="font-family:monospace;" placeholder="<?=htmlentities('<i class="fab fa-discord text-discord"></i><p class="text-discord">Join our Discord!</p>')?>">
<div class="hint-block">Enter your desired html code to be displayed as link.</div>
</div>
<div class="col">
<label class="label link" for="item[<?=intval($i)+1;?>][link]">Link name</label>
<input type="text" name="item[<?=intval($i)+1;?>][link]" class='form-control' style="font-family:monospace;" placeholder="https://example.com/">
<div class="hint-block">Optionally enter a URL for the menu item to point.</div>
</div>

</div>
<div class="row">
<div class="form-group">
<?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?>
</div>
</div>
<?= Html::endForm(); ?>

</div>
5 changes: 0 additions & 5 deletions backend/modules/settings/models/ConfigureForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ class ConfigureForm extends Model
public $time_zone;
public $target_days_new=2;
public $target_days_updated=1;
public $discord_invite_url;
public $discord_news_webhook;
public $pf_state_limits;
public $stripe_apiKey;
Expand Down Expand Up @@ -127,7 +126,6 @@ class ConfigureForm extends Model
'dn_organizationName',
'dn_organizationalUnitName',
'discord_news_webhook',
'discord_invite_url',
'pf_state_limits',
'stripe_apiKey',
'stripe_publicApiKey',
Expand Down Expand Up @@ -162,7 +160,6 @@ public function rules()
'moderator_domain',
'twitter_account',
'twitter_hashtags',
'discord_invite_url',
'discord_news_webhook',
'time_zone',
'dn_countryName',
Expand Down Expand Up @@ -197,7 +194,6 @@ public function rules()
'registrations_end',
'twitter_account',
'twitter_hashtags',
'discord_invite_url',
'discord_news_webhook',
'time_zone',
'dn_countryName',
Expand Down Expand Up @@ -315,7 +311,6 @@ public function attributeLabels()
'dn_organizationalUnitName'=>'organizationalUnitName',
'target_days_new'=>'Target days is new',
'target_days_updated'=>'Target days is updated',
'discord_invite_url'=>'Discord invite URL',
'discord_news_webhook'=>'Discord News Webhook',
'monthly_leaderboards'=>'Monthly points leaderboards',
'subscriptions_menu_show'=>'Show subscriptions menu',
Expand Down
1 change: 0 additions & 1 deletion backend/modules/settings/views/sysconfig/configure.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@
<div class="row form-group">
<div class="col-sm-2"><?= $form->field($model, 'twitter_account')->textInput(['maxlength' => true])->hint('Twitter account to use for tagging and via') ?></div>
<div class="col-sm-2"><?= $form->field($model, 'twitter_hashtags')->textInput(['maxlength' => true])->hint('Twitter hashtags to use for tweets') ?></div>
<div class="col-sm-3"><?= $form->field($model, 'discord_invite_url')->textInput(['maxlength' => true])->hint('Discord URL to invite payers to your server') ?></div>
<div class="col-sm-5"><?= $form->field($model, 'discord_news_webhook')->textInput(['maxlength' => true])->hint('Discord Webhook URL to post platform news and updates') ?></div>
</div>
<hr/>
Expand Down
1 change: 1 addition & 0 deletions backend/views/layouts/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
['label' => 'Layout Override', 'url' => ['/content/layout-override/index'], 'visible' => !Yii::$app->user->isGuest,],
'<div class="dropdown-divider"></div>',
'<div class="dropdown-header">Static</div>',
['label' => 'Menu items', 'url' => ['/content/default/menu-items'], 'visible' => !Yii::$app->user->isGuest,],
['label' => 'Frontpage Scenario', 'url' => ['/content/default/frontpage-scenario'], 'visible' => !Yii::$app->user->isGuest,],
['label' => 'Offense Scenario', 'url' => ['/content/default/offense-scenario'], 'visible' => !Yii::$app->user->isGuest,],
['label' => 'Defense Scenario', 'url' => ['/content/default/defense-scenario'], 'visible' => !Yii::$app->user->isGuest,],
Expand Down
2 changes: 1 addition & 1 deletion docs/Sysconfig-Keys.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@
* `registrations_start`/`registrations_end` (optional): When the registrations starts and stops (timestamp)
* `profile_visibility`: Set the default player profile visibility (users can still change settings)
* `profile_settings_fields`: Comma separated list of field names that the users are allowed to change (avatar,bio,country,discord,echoctf,email,fullname,github,htb,pending_progress,twitch,twitter,username,visibility,youtube)
* `discord_invite_url` (optional): discord server invite url on show on left sidebar
* `admin_ids` (optional): Comma separated list of admin player IDs
* `admin_player:<PLAYER_ID>` (optional): Set a specific player ID as admin
* `target_new_days`: How many days is target considered as `new` on the frontend after creation
* `target_updated_days`: How many days the target is considered as `updated` on the frontend after update
* `discord_news_webhook`: A discord webhook url to send news and announcements to
* `pf_state_limits`: The content to be appended to the pass rules that enforces limits (default: `(max 10000, source-track rule, max-src-nodes 5, max-src-states 2000, max-src-conn 50)`)
* `force_https_urls`: Force URL generation to always be https (sets `_SERVER['HTTPS']=on`)
* `menu_items`: JSON encoded string of items to append to the frontend menu

## mail configuration
* `mail_from` Email address used to send registration and password reset mails from
Expand Down
16 changes: 14 additions & 2 deletions frontend/widgets/Menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,20 @@ protected function renderItems($items)
$lines[]=Html::tag($tag, $menu, $options);
}

if(Yii::$app->sys->discord_invite_url!==false)
$lines[]='<li class="nav-item"><b>'.Html::a('<i class="fab fa-discord text-discord"></i><p class="text-discord">Join our Discord!</p>', Yii::$app->sys->discord_invite_url, ['target'=>'_blank','class'=>'nav-link']).'</b></li>';
// Add support for more menu items
if(Yii::$app->sys->menu_items!==false && Yii::$app->sys->menu_items!=="")
{
// use try so that if json decode fails we dont crash.
try {
foreach (json_decode(Yii::$app->sys->menu_items,true) as $item)
{
$lines[]='<li class="nav-item"><b>'.Html::a($item['name'], $item['link'], ['target'=>'_blank','class'=>'nav-link']).'</b></li>';
}
}
catch (\Exception $e) {} // avoid exceptions here
catch(\TypeError $e) {} // dont fail on offset errors
}


return implode("\n", $lines);
}
Expand Down

0 comments on commit f105c5a

Please sign in to comment.