From 8a125e2e319b48a06ab503ff80c600dcf1b910d0 Mon Sep 17 00:00:00 2001 From: Themroc <7hemroc@gmail.com> Date: Tue, 15 Feb 2022 19:38:30 +0100 Subject: [PATCH] Config-form: make guest url invisible if not needed --- Module.php | 28 +++++++++++++++++++++++----- controllers/AdminController.php | 16 ++++++++++++---- controllers/IndexController.php | 15 ++++++++++----- messages/de/base.php | 6 +++--- models/AdminForm.php | 18 +++++++++++------- views/index/error.php | 7 +++++++ widgets/AdminTabs.php | 2 +- 7 files changed, 67 insertions(+), 25 deletions(-) create mode 100644 views/index/error.php diff --git a/Module.php b/Module.php index e2c3889..e5b1f12 100644 --- a/Module.php +++ b/Module.php @@ -17,7 +17,7 @@ class Module extends \humhub\modules\content\components\ContentContainerModule /** * @inheritdoc */ - public function getConfigUrl() + public function getConfigUrl () { return Url::to(['/iframe/admin']); } @@ -25,18 +25,36 @@ public function getConfigUrl() /** * @inheritdoc */ - public function disable() + public function disable () { // Cleanup all module data, don't remove the parent::disable()!!! parent::disable(); } - public function getSetting($key, $frame=null) + public function update () + { + $ver= $this->getSetting('//version'); + if (empty($ver) || $ver < 1) { + foreach ($this->getFrames() as $frame) { + $url= $this->getSetting('url', $frame); + $url_reg= $this->getSetting('url_reg', $frame); + if (empty($url_reg)) { + $this->settings->set($frame.'/url_reg', $url); + } else { + $this->settings->set($frame.'/url_guest', $url); + } + $this->settings->delete($frame.'/url'); + } + $this->settings->set('//version', 1); + } + } + + public function getSetting ($key, $frame=null) { return $this->settings->get(($frame==null ? '' : $frame.'/') . $key); } - public function getUrl($page, $params=null) + public function getUrl ($page, $params=null) { $url= [ '/iframe/'.$page ]; if (is_array($params)) @@ -46,7 +64,7 @@ public function getUrl($page, $params=null) return Url::to($url); } - public function getFrames() + public function getFrames () { $frames= $this->settings->get('/frames'); if ($frames == null) diff --git a/controllers/AdminController.php b/controllers/AdminController.php index 000b7f9..3a32c6b 100644 --- a/controllers/AdminController.php +++ b/controllers/AdminController.php @@ -8,30 +8,38 @@ class AdminController extends Controller { + const MH_MIN_REL= '0.2.1'; + public $adminOnly= true; public $subLayout= '@iframe/views/layouts/admin'; - public function init() + public function init () { - if (Yii::$app->getModule('mod-helper')===null) + if (null == $mod= Yii::$app->getModule('mod-helper')) + $this->subLayout= null; + if (version_compare($mod->getVersion(), self::MH_MIN_REL) < 0) $this->subLayout= null; return parent::init(); } + /** * Render admin only page * * @return string */ - public function actionIndex() + public function actionIndex () { if ($this->subLayout===null) return $this->render('error', [ - 'msg'=> 'Please install and activate the Mod-Helper plugin.', + 'msg'=> 'Please install and activate the' + .' Mod-Helper plugin,' + .' at least version '.self::MH_MIN_REL.'.', ]); $mod= Yii::$app->getModule('iframe'); + $mod->update(); $frame= Yii::$app->request->get('frame'); if (Yii::$app->request->get('delete') == 1) { diff --git a/controllers/IndexController.php b/controllers/IndexController.php index cb58a70..27e34ca 100644 --- a/controllers/IndexController.php +++ b/controllers/IndexController.php @@ -9,15 +9,20 @@ class IndexController extends Controller { public $subLayout = "@iframe/views/layouts/default"; - public function actionIndex() + public function actionIndex () { $mod= Yii::$app->getModule('iframe'); - $uid= Yii::$app->user->id; + $mod->update(); $frame= Yii::$app->request->get('frame'); + if (Yii::$app->user->isGuest && !$mod->getSetting('guest', $frame)) + return $this->render('error', [ 'msg'=> 'Sorry, guests are not allowed here.']); + + $uid= Yii::$app->user->id; $top_data= $mod->getSetting('top_data', $frame); - $url= $mod->getSetting('url_reg', $frame); - if (empty($url) || Yii::$app->user->isGuest) - $url= $mod->getSetting('url', $frame); + if (Yii::$app->user->isGuest) + $url= $mod->getSetting('url_guest', $frame); + if (empty($url)) + $url= $mod->getSetting('url_reg', $frame); if (Yii::$app->user->isGuest) { $user= Yii::$app->request->userIP; diff --git a/messages/de/base.php b/messages/de/base.php index e510ad2..662a794 100644 --- a/messages/de/base.php +++ b/messages/de/base.php @@ -8,11 +8,11 @@ 'This will show up under the icon'=> 'Dies wird unter dem Symbol angezeigt', 'Sort order'=> 'Sortierreihenfolge', 'Determines topbar menu position'=> 'Legt die topbar Menüposition fest', - 'Visible for guests'=> 'Sichtbar für Gäste', -// 'URL'=> '', + 'URL for registered users'=> 'URL für registrierte Benutzer', + 'URL for guests'=> 'URL für Gäste', 'The webpage to be shown. @UID@, @USER@, @EMAIL@ and @COLOR@ will be replaced by the respective values.'=> 'Die anzuzeigende Webseite. @UID@, @USER@, @EMAIL@ und @COLOR@ werden durch die jeweiligen Werte ersetzt.', - 'URL for registered users'=> 'URL für registrierte Benutzer', + 'Visible for guests'=> 'Sichtbar für Gäste', 'If empty, the above will be used.'=> 'Wenn leer, wird der Wert von oben verwendet.', 'HTML to add above the iframe'=> 'HTML oberhalb des iframes', 'Delete'=> 'Löschen', diff --git a/models/AdminForm.php b/models/AdminForm.php index 39300bc..8a17f83 100644 --- a/models/AdminForm.php +++ b/models/AdminForm.php @@ -17,8 +17,8 @@ class AdminForm extends \themroc\humhub\modules\modhelper\models\AdminForm public $sort; public $size; public $guest; - public $url; public $url_reg; + public $url_guest; public $top_data; protected $vars= [ @@ -43,6 +43,10 @@ class AdminForm extends \themroc\humhub\modules\modhelper\models\AdminForm 'items'=> [self::class, 'sizeModes'] ], ], + 'url_reg'=> [ + 'label'=> 'URL for registered users', + 'hints'=> 'The webpage to be shown. @UID@, @USER@, @EMAIL@ and @COLOR@ will be replaced by the respective values.', + ], 'guest'=> [ 'label'=> 'Visible for guests', 'rules'=> ['in', 'range'=> [0, 1]], @@ -50,13 +54,13 @@ class AdminForm extends \themroc\humhub\modules\modhelper\models\AdminForm 'type'=> 'checkbox', ], ], - 'url'=> [ - 'label'=> 'URL', - 'hints'=> 'The webpage to be shown. @UID@, @USER@, @EMAIL@ and @COLOR@ will be replaced by the respective values.', - ], - 'url_reg'=> [ - 'label'=> 'URL for registered users', + 'url_guest'=> [ +# 'url'=> [ + 'label'=> 'URL for guests', 'hints'=> 'If empty, the above will be used.', + 'form'=> [ + 'visible'=> ['guest'=> 1], + ], ], 'top_data'=> [ 'label'=> 'HTML to add above the iframe', diff --git a/views/index/error.php b/views/index/error.php new file mode 100644 index 0000000..9d1200b --- /dev/null +++ b/views/index/error.php @@ -0,0 +1,7 @@ +'. "\n"; +echo '
' . "\n"; +echo ' '. $msg . "\n"; +echo '
' . "\n"; +echo '' . "\n"; diff --git a/widgets/AdminTabs.php b/widgets/AdminTabs.php index 0c77fee..8c08227 100644 --- a/widgets/AdminTabs.php +++ b/widgets/AdminTabs.php @@ -15,7 +15,7 @@ class AdminTabs extends \humhub\widgets\BaseMenu */ public $template = "@humhub/widgets/views/tabMenu"; - public function init() + public function init () { $controller= Yii::$app->controller; $frame= Yii::$app->request->get('frame');