I have a module I am working for on HumHub which is based on Yii Framework.
My question is, how can I raise a global event? In my application, I need to have a check to see if the system is in development mode, and if it is forward to a view/throw a exception. Currently it throws a exception as I am also having issues with rendering a simple view.
I come from days before frameworks, and am really in over my head.
Anyways, here is an example of what I have in place. It does nothing. I think because I am not using the $event variable.
autostart.php
<?php
Yii::app()->moduleManager->register(array(
'id' => 'devmode',
'class' => 'application.modules.devmode.DevModeModule',
'import' => array(
'application.modules.devmode.*',
),
// Events to Catch
'events' => array(
array('class' => 'AdminMenuWidget', 'event' => 'onInit', 'callback' => array('DevModeEvents', 'onAdminMenuInit')),
array('class' => 'TopMenuWidget', 'event' => 'onInit', 'callback' => array('DevModeEvents', 'devBlock')),
array('class' => 'DashboardSidebarWidget', 'event' => 'onInit', 'callback' => array('DevModeModule', 'onSidebarInit')),
),
));
DevModeEvents.php
<?php
/**
* Defines the module events
*
* @package humhub.modules.devmode.events
* @author Jordan Thompson
*/
class DevModeEvents {
public static function onAdminMenuInit($event)
{
$event->sender->addItem(array(
'label' => Yii::t('devmode.base', 'Development Mode'),
'url' => Yii::app()->createUrl('//devmode/config/config'),
'group' => 'settings',
'icon' => '<i class="fa fa-lock"></i>',
'isActive' => (Yii::app()->controller->module && Yii::app()->controller->module->id == 'devmode' && Yii::app()->controller->id == 'admin'),
'sortOrder' => 300,
));
}
public static function devBlock($event) {
// Nothing happens when this code is triggered.
$devMode = HSetting::Get('devMode', 'devmode');
if ($devMode == 1 ) {
if (!Yii::app()->user->isGuest) {
if (!Yii::app()->user->isAdmin()) {
throw new CHttpException('418', Yii::t('devmode.base', Yii::app()->name . ' is currently under maintenance, check back later.'));
}
} else {
throw new CHttpException('418', Yii::t('devmode.base', Yii::app()->name . ' is currently under maintenance, check back later.'));
}
}
}
}