Notifications to all users if new block is posted

Hey there.
I would like to notify my users if someone posted a new block.

I have to explain: The template is collecting blocks with content from several users. It would be good, to notify them, if someone added a block, because the next one has to react on it.
So it is not about a new page (article) but a new page element. Is that possible?

How would I build the trigger for that operation?
I just jumped into the hole email engine thing in kirby and I guess it should to be in the template controller, right?

I think this would be a start, right?

In a controller you wouldn’t know if the content has changed. You could use a page.update:after hook and check if the number of blocks has changed. Inside the hook you have access to the old and the new version of the page. This is not possible in a controller.

Ah. I see.

So it has to be in /site/config/config.php?
I will try if I can figure it out with that page.update:after hook.

Hooks can be registered in the config or in a plugin, that’s up to you.

1 Like

Almost.

With this I already achieve that it tries to do something, if I add a block:

return [
'debug' => true,
'hooks' => [
        'page.update:after' => function ($newPage, $oldPage) {
          if ( $oldPage->artwork()->isNotEmpty() ) {
            if ($oldPage->artwork()->toBlocks()->count() < $newPage->artwork()->toBlocks()->count() ) {
               try {
                  $kirby->email([
                    'to' => 'hallo@url.de',
                    'template' => 'new-block',

                  ]);
                } catch (Exception $error) {
                  echo $error;
                }

              // code...
            }
          }
        }
    ]

but it throws me an error in line 25 (here it is $kirby->email([) . More I can’t read from it. :sweat_smile:

The first if is to check, if the toBlocks Element exists and if I am on the right page.
Is there an obvious mistake, I don’t see?

$kirby is not available inside the hook (only in templates/snippets), you can use the kirby() helper instead.

Instead of checking if the field is empty, I would check for the correct intended template of the page.

You mean this:

Ok. no error anymore!
Tried it now on a server, but the mail is not arriving. :frowning:

So I made a template for the mail, and this config code:

return [
'debug' => true,
'hooks' => [
        'page.update:after' => function ($newPage, $oldPage) {
          if ( $oldPage->artwork()->isNotEmpty() ) {
            if ($oldPage->artwork()->toBlocks()->count() < $newPage->artwork()->toBlocks()->count() ) {

              $from = new \Kirby\Cms\User([
                'email' => 'hallo@url.de',
                'name' => 'Gadu Gadu',
              ]);

                try {
                  $this->email([
                    'from' => $from,
                    'to' => 'hallo@url.de',
                    'template' => 'new-block',
                    'subject' => 'Welcome!',

                  ]);
                } catch (Exception $error) {
                  echo $error;
                }



            }
          }
        }
    ]
];

About this:

Sure! You mean like that: ->findBy('intendedTemplate', 'template')
I have been there. With the error, I went back, because I thought it could be the source of the error.

No, not find by, but

if ($oldPage->intendedTemplate()->name() === 'sometemplate') {
  // do stuff
}

Because you probably don’t want to to this for any page but only particular pages.

And as regards sending the email, have you set up your email transport configuration in your config?

I thought that is optional, but as I think about it, it makes sense that it is obligatory. But it does not work though…
I did set it like that:

return [
'debug' => true,
'email' => [
  'transport' => [
    'type' => 'smtp',
    'host' => 'send.one.com',
    'port' => 465,
    'security' => true,
    'auth' => true,
    'username' => 'myusername',
    'password' => 'my-pw',
  ]
],
'hooks' => [
        'page.update:after' => function ($newPage, $oldPage) {
          if ($oldPage->intendedTemplate()->name() === 'channel') {
            if ($oldPage->artwork()->toBlocks()->count() < $newPage->artwork()->toBlocks()->count() ) {

              $from = new \Kirby\Cms\User([
                'email' => 'hallo@url.de',
                'name' => 'Gadu Gadu',
              ]);

                try {
                  $this->email([
                    'from' => $from,
                    'to' => 'hallo@url.de',
                    'template' => 'new-block',
                    'subject' => 'Welcome!',

                  ]);
                } catch (Exception $error) {
                  echo $error;
                }

            }
          }
        }
    ]
];

No idea what I do wrong here.

You are using $this when not in object context. As I already wrote above, you have to use the kirby() helper here:

kirby()->email([

Ah now I got it. Thanks for clarifying.

I could not get it running with the email transport, but removing it and just put an ‘email’ works out of the sudden. Anyway, for documentation here is a working code snippet.

site/config/config.php:

 <?php

return [
'debug' => true,
'hooks' => [
        'page.update:after' => function ($newPage, $oldPage) {
          if ($oldPage->intendedTemplate()->name() === 'channel') {
            if ($oldPage->artwork()->toBlocks()->count() < $newPage->artwork()->toBlocks()->count() ) {
              $lastwork = $newPage->artwork()->toBlocks()->last();
                try {
                  kirby()->email([
                    'template' => 'new-block',
                    'from' => 'hallo@url.de',
                    'to' => 'hallo@url.de',
                    'subject' => 'New work for you to answer',
                    'data' => [
                      'name' => $lastwork->author()->firstname(),
                      'nextname' => $lastwork->nextartist()->name(),
                      'link' => $newPage->url(),
                      'channel' => $newPage->title(),
                      'date' => $newPage->artwork()->toBlocks()->last()->date(),
                      'time' => $newPage->artwork()->toBlocks()->last()->time(),
                    ]
                  ]);
                } catch (Exception $error) {
                  echo $error;
                }

            }
          }
        }
    ]
];