Skip to content

Instantly share code, notes, and snippets.

@rtripault
Created June 23, 2017 08:58
Show Gist options
  • Save rtripault/f948caecd98d01ea59ab9cfe4e729725 to your computer and use it in GitHub Desktop.
Save rtripault/f948caecd98d01ea59ab9cfe4e729725 to your computer and use it in GitHub Desktop.
Sample plugin to display the welcome screen to any new member login for the first time in MODX Revolution manager
<?php
/**
* A sample plugin to display the "welcome message" to all users, login for the first time in Revo manager
*
* @var modX $modx
* @var array $scriptProperties
* @var modPlugin $this
*
* @see modPlugin::process()
*
* @event OnManagerPageBeforeRender
* @event OnUserFormSave
*/
switch ($modx->event->name) {
case 'OnManagerPageBeforeRender':
// Here we need to check if we are displaying the "welcome" controller
if (!$modx->event->params['controller'] instanceof WelcomeManagerController) {
return;
}
// Assume we always want to display the welcome message, no matter how global welcome_screen setting is configured
$show = true;
// Check the current user "preference"
$setting = $modx->getObject('modUserSetting', [
'key' => 'welcome_screen',
'user' => $modx->user->get('id'),
]);
if ($setting) {
// user option found, let's use it instead
$show = (bool) $setting->get('value');
}
// Set the option temporarily so the welcome controller could do whatever it takes
$modx->setOption('welcome_screen', $show);
break;
case 'OnUserFormSave':
// A user is saved, let's make sure to process only when a user is created
if ($modx->event->params['mode'] !== modSystemEvent::MODE_NEW) {
return;
}
// User has just been created, assume we want to create a user setting
/** @var modUser $user */
$user = $modx->event->params['object'];
// First make sure the setting does not already exist
/** @var modUserSetting||null $setting */
$setting = $modx->getObject('modUserSetting', [
'key' => 'welcome_screen',
'user' => $user->get('id'),
]);
if (!$setting) {
// No user setting found, let's create it!
/** @var modUserSetting $setting */
$setting = $modx->newObject('modUserSetting');
$setting->fromArray([
'key' => 'welcome_screen',
'user' => $user->get('id'),
'xtype' => 'combo-boolean',
'area' => 'manager',
'namespace' => 'core',
], '', true);
}
$setting->set('value', true);
$setting->save();
break;
}
return;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment