MODX Community

Системное событие на создание ресурса из фронта

Requires the administrator’s notice when a user creates a resource from the frontend. The resource is automatically created with the unpublished status.
I am making a plugin like this:

<?php if ($modx->event->name == 'OnDocFormSave' ) { if ($resource->get('parent') == 56 && $resource->published == 0) { $message = $modx->getChunk('notificatiob.tpl'); $modx->getService('mail', 'mail.modPHPMailer'); $modx->mail->set(modMail::MAIL_BODY,$message); $modx->mail->set(modMail::MAIL_FROM,'mail@domen.ru'); $modx->mail->set(modMail::MAIL_FROM_NAME,'От кого'); $modx->mail->set(modMail::MAIL_SUBJECT,'Тема'); $modx->mail->address('to','mymail@yandex.ru'); $modx->mail->setHTML(true); if (!$modx->mail->send()) { $modx->log(modX::LOG_LEVEL_ERROR,'Произошла ошибка при отправке сообщения: '.$modx->mail->mailer->ErrorInfo); } $modx->mail->reset(); } }

Nothing works. Tell me how best to do it?

What extra (пакет) or method do you use to create a resource from the frontend?

I am use tickets.
If you write the code like this:
<?php
switch ($modx->event->name) {
case ‘OnBeforeDocFormSave’:

        if ($mode == 'new' || $mode == 'upd' && $resource->class_key == "Ticket") {  
            $resource->set('published', 0);                               
            $modx->getService('mail', 'mail.modPHPMailer');
            $modx->mail->set(modMail::MAIL_FROM, $modx->getOption('emailsender'));
            $modx->mail->set(modMail::MAIL_FROM_NAME, $modx->getOption('site_name'));
            
            $user_vo = $resource->get('createdby'); 
			$theme_vo = $resource->get('pagetitle'); 
			$user = $modx->getUser();
			$username = $user->username;
            
            $modx->mail->address('to', 'mail@mail.ru');
            if ($mode == 'new') {
                $modx->mail->set(modMail::MAIL_SUBJECT, 'New profile on the site');
                $modx->mail->set(modMail::MAIL_BODY, $modx->getChunk('dino__new_ticket.tpl', array('pagetitle'=>$theme_vo, 'createdby' => $user_vo, 'username' => $username)));
                }
            if ($mode == 'upd') {
                $modx->mail->set(modMail::MAIL_SUBJECT, 'User edited the profile');
                $modx->mail->set(modMail::MAIL_BODY, $modx->getChunk('dino__edit_ticket.tpl', array('pagetitle'=>$theme_vo, 'createdby' => $user_vo, 'username' => $username)));
                }
            $modx->mail->setHTML(true);
            if (!$modx->mail->send()) {
                $modx->log(modX::LOG_LEVEL_ERROR,'An error occurred while trying to send an email: '.$modx->mail->mailer->ErrorInfo);
            }
            $modx->mail->reset();
 
        }
 
        $resource->save();
 
    break;
}

A notification is received both when creating a new record and when the user updates the record. But there is one problem: when the administrator changes the resource in the panel, he does not want to acquire status 1. After saving, status 0 is always assigned. How can this be solved?

You are already reading the user:

$user = $modx->getUser();

Now you should be able to determine, if this user is an administrator or not:

if ($user->isMember('Administrator')){
    $resource->set('published', 1);
} else {
    $resource->set('published', 0);
}

Does not work.
Prevents a regular user from submitting an edited resource from the frontend.
Hangs in the admin area when the administrator changes the resource.