Skip to content

Commit d4d466f

Browse files
authored
Merge pull request #8 from GreenMeteor/develop
Enh: Support adding multiple codeboxes
2 parents 6656a86 + f640d26 commit d4d466f

21 files changed

+532
-288
lines changed

Assets.php

Lines changed: 0 additions & 16 deletions
This file was deleted.

Events.php

Lines changed: 55 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,55 @@
1-
<?php
2-
3-
namespace humhub\modules\codebox;
4-
5-
use Yii;
6-
use yii\helpers\Url;
7-
use yii\base\BaseObject;
8-
use humhub\modules\ui\menu\MenuLink;
9-
use humhub\modules\ui\icon\widgets\Icon;
10-
use humhub\modules\admin\widgets\AdminMenu;
11-
use humhub\modules\admin\permissions\ManageModules;
12-
13-
class Events extends BaseObject
14-
{
15-
16-
public static function onAdminMenuInit($event)
17-
{
18-
if (!Yii::$app->user->can(ManageModules::class)) {
19-
return;
20-
}
21-
22-
/** @var AdminMenu $menu */
23-
$menu = $event->sender;
24-
25-
$menu->addEntry(new MenuLink([
26-
'label' => Yii::t('CodeboxModule.base', 'Codebox Settings'),
27-
'url' => Url::toRoute('/codebox/admin/index'),
28-
'icon' => Icon::get('code'),
29-
'isActive' => Yii::$app->controller->module && Yii::$app->controller->module->id == 'codebox' && Yii::$app->controller->id == 'admin',
30-
'sortOrder' => 600,
31-
'isVisible' => true,
32-
]));
33-
}
34-
35-
public static function addCodeboxFrame($event)
36-
{
37-
$module = Yii::$app->getModule('codebox');
38-
$settings = $module->settings;
39-
40-
if (Yii::$app->user->isGuest) {
41-
return;
42-
} else {
43-
Yii::$app->user;
44-
}
45-
46-
$event->sender->addWidget(widgets\CodeboxFrame::class, [], ['sortOrder' => $settings->get('sortOrder')]);
47-
}
48-
}
1+
<?php
2+
3+
namespace humhub\modules\codebox;
4+
5+
use Yii;
6+
use yii\helpers\Url;
7+
use yii\base\BaseObject;
8+
use humhub\modules\ui\menu\MenuLink;
9+
use humhub\modules\ui\icon\widgets\Icon;
10+
use humhub\modules\admin\widgets\AdminMenu;
11+
use humhub\modules\admin\permissions\ManageModules;
12+
use humhub\modules\codebox\models\ConfigureForm;
13+
14+
class Events extends BaseObject
15+
{
16+
17+
public static function onAdminMenuInit($event)
18+
{
19+
if (!Yii::$app->user->can(ManageModules::class)) {
20+
return;
21+
}
22+
23+
/** @var AdminMenu $menu */
24+
$menu = $event->sender;
25+
26+
$menu->addEntry(new MenuLink([
27+
'label' => Yii::t('CodeboxModule.base', 'Codebox Settings'),
28+
'url' => Url::toRoute('/codebox/admin/index'),
29+
'icon' => Icon::get('code'),
30+
'isActive' => Yii::$app->controller->module && Yii::$app->controller->module->id == 'codebox' && Yii::$app->controller->id == 'admin',
31+
'sortOrder' => 600,
32+
'isVisible' => true,
33+
]));
34+
}
35+
36+
public static function addCodeboxFrame($event)
37+
{
38+
// Retrieve the Codebox module
39+
$module = Yii::$app->getModule('codebox');
40+
41+
// Check if the module is enabled
42+
if ($module !== null && $module->isEnabled) {
43+
// Retrieve the settings from the database
44+
$entries = ConfigureForm::find()->asArray()->all();
45+
46+
// Add the CodeboxFrame widget with the entries
47+
$event->sender->addWidget(
48+
\humhub\modules\codebox\widgets\CodeboxFrame::class,
49+
['entries' => $entries],
50+
['sortOrder' => $module->getOrder()]
51+
);
52+
}
53+
}
54+
55+
}

Module.php

Lines changed: 84 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,84 @@
1-
<?php
2-
3-
namespace humhub\modules\codebox;
4-
5-
use Yii;
6-
use yii\helpers\Url;
7-
use humhub\libs\Html;
8-
use humhub\components\Module as BaseModule;
9-
10-
class Module extends BaseModule
11-
{
12-
13-
public $resourcesPath = 'resources';
14-
15-
/**
16-
* @inheritdoc
17-
*/
18-
public function getConfigUrl()
19-
{
20-
return Url::to(['/codebox/admin']);
21-
}
22-
23-
public function getTitle()
24-
{
25-
$title = $this->settings->get('title');
26-
if (empty($title)) {
27-
return '';
28-
}
29-
return $title;
30-
}
31-
32-
public function getHtmlCode()
33-
{
34-
$htmlCode = $this->settings->get('htmlCode');
35-
36-
if (empty($htmlCode)) {
37-
return '';
38-
}
39-
40-
return $htmlCode;
41-
}
42-
43-
44-
public function getOrder()
45-
{
46-
$sortOrder = $this->settings->get('sortOrder');
47-
if (empty($sortOrder)) {
48-
return '100';
49-
}
50-
return $sortOrder;
51-
}
52-
}
1+
<?php
2+
3+
namespace humhub\modules\codebox;
4+
5+
use Yii;
6+
use yii\helpers\Url;
7+
use humhub\components\Module as BaseModule;
8+
use humhub\modules\codebox\models\ConfigureForm;
9+
10+
class Module extends BaseModule
11+
{
12+
public $resourcesPath = 'resources';
13+
14+
/**
15+
* Returns the URL to the configuration page.
16+
*
17+
* @return string the URL to the configuration page
18+
*/
19+
public function getConfigUrl()
20+
{
21+
return Url::to(['/codebox/admin']);
22+
}
23+
24+
/**
25+
* Retrieves all settings from the database.
26+
*
27+
* @return array the settings
28+
*/
29+
protected function getSettings()
30+
{
31+
$models = ConfigureForm::find()->all();
32+
$settings = [];
33+
34+
foreach ($models as $model) {
35+
$settings[$model->getAttribute('name')] = $model->getAttribute('value');
36+
}
37+
38+
return $settings;
39+
}
40+
41+
/**
42+
* Retrieves a setting from the database.
43+
*
44+
* @param string $name the name of the setting attribute
45+
* @param mixed $defaultValue the default value if the setting is not found
46+
* @return mixed the value of the setting
47+
*/
48+
protected function getSetting($name, $defaultValue = null)
49+
{
50+
$settings = $this->getSettings();
51+
52+
return isset($settings[$name]) ? $settings[$name] : $defaultValue;
53+
}
54+
55+
/**
56+
* Retrieves the title from the module settings.
57+
*
58+
* @return string the title
59+
*/
60+
public function getTitle()
61+
{
62+
return $this->getSetting('title');
63+
}
64+
65+
/**
66+
* Retrieves the HTML code snippet from the module settings.
67+
*
68+
* @return string the HTML code snippet
69+
*/
70+
public function getHtmlCode()
71+
{
72+
return $this->getSetting('htmlCode');
73+
}
74+
75+
/**
76+
* Retrieves the sort order from the module settings.
77+
*
78+
* @return int the sort order
79+
*/
80+
public function getOrder()
81+
{
82+
return $this->getSetting('sortOrder', 100);
83+
}
84+
}

README.md

Lines changed: 0 additions & 8 deletions
This file was deleted.

controllers/AdminController.php

Lines changed: 67 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,67 @@
1-
<?php
2-
3-
namespace humhub\modules\codebox\controllers;
4-
5-
use Yii;
6-
use humhub\modules\admin\components\Controller;
7-
use humhub\modules\codebox\models\ConfigureForm;
8-
9-
class AdminController extends Controller
10-
{
11-
12-
public function actionIndex()
13-
{
14-
$model = new ConfigureForm();
15-
$model->loadSettings();
16-
17-
if ($model->load(Yii::$app->request->post()) && $model->save()) {
18-
$this->view->saved();
19-
}
20-
21-
return $this->render('index', ['model' => $model]);
22-
}
23-
24-
}
1+
<?php
2+
3+
namespace humhub\modules\codebox\controllers;
4+
5+
use Yii;
6+
use yii\web\NotFoundHttpException;
7+
use humhub\modules\admin\components\Controller;
8+
use humhub\modules\codebox\models\ConfigureForm;
9+
10+
class AdminController extends Controller
11+
{
12+
13+
public function actionIndex()
14+
{
15+
$models = ConfigureForm::find()->all();
16+
17+
return $this->render('index', ['models' => $models]);
18+
}
19+
20+
public function actionCreate()
21+
{
22+
$model = new ConfigureForm();
23+
24+
if ($model->load(Yii::$app->request->post()) && $model->save()) {
25+
return $this->redirect(['index']);
26+
}
27+
28+
return $this->renderAjax('create', ['model' => $model]);
29+
}
30+
31+
/**
32+
* Updates an existing ticket.
33+
*
34+
* @param int $id
35+
* @return string|\yii\web\Response
36+
* @throws NotFoundHttpException
37+
*/
38+
public function actionUpdate($id)
39+
{
40+
$model = $this->findModel($id);
41+
42+
if ($model->load(Yii::$app->request->post()) && $model->save()) {
43+
return $this->redirect(['index']);
44+
}
45+
46+
return $this->renderAjax('update', [
47+
'model' => $model,
48+
]);
49+
}
50+
51+
public function actionDelete($id)
52+
{
53+
$model = $this->findModel($id);
54+
$model->delete();
55+
56+
return $this->redirect(['index']);
57+
}
58+
59+
protected function findModel($id)
60+
{
61+
if (($model = ConfigureForm::findOne($id)) !== null) {
62+
return $model;
63+
}
64+
65+
throw new NotFoundHttpException(Yii::t('CodeboxModule.base', 'The requested entry does not exist.'));
66+
}
67+
}

docs/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelogs
22

3+
### Release v1.2.0 (*5/12/2024*)
4+
- Support adding multiple codeboxes
5+
- Refactor complete module
6+
37
### Release v1.1.1 (*3/13/2024*)
48
- Fix: Nonce
59

docs/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## [HumHub](https://www.humhub.org/en) Codebox Module
2-
Allows you to use HTML snippets to your sidebar.
2+
Allows you to add and use HTML snippets on your Dashboard sidebar.
33

44
### __Product Distributors:__
55
[@GreenMeteor](https://github.com/GreenMeteor)
6+
7+
> If you enjoy our work please consider [donating](https://donate.stripe.com/00g7uJ4gb7UZePu8wM).

0 commit comments

Comments
 (0)