Jump to content

Send mail to administrator if there is only 7 children?


Roych
 Share

Recommended Posts

Hello,

I'm working on a restaurant site that deletes (puts in a trash) one children every day (daily menu). And I would like to send mail to administrator (of choice) that he has to add new children when there is only 7 left. As a reminder so that he doesn't forget about adding some new ones. We always show seven daily menus on frontend and we don't want go below this as it would look weird.

Everything works great so far, I only need help with the mail sending as I never worked on something like this.

Can this be done somehow? (I have FormBuilder, but maybe some hook would be better for something like this.)

Im using Templates

Daily menu (template=calendar-list)
  - day 1 (template=calendar-post)
  - day 2 (template=calendar-post)
  - .....

Thank you
R

Link to comment
Share on other sites

Hi,

you can add a hook to execute code whenever a page is moved to the trash. You can use this to check how many children are left and send an e-mail using WireMail. Put this in your ready.php:

$this->addHookAfter('Pages::trashed(template=calendar-post)', function(HookEvent $event) {
	$numberOfItems = wire('pages')->count('parent=/daily-menu/');
    if ($numberOfItems <= 7) {
		$m = new WireMail();
        $m->to('chefdecuisine@example.com'); // specify CSV string or array for multiple addresses
        $m->from('noreply@example.com');
        $m->subject("Uh-oh, there are only {$numberOfItems} dishes left on the menu");
      	$m->bodyHTML("<html><marquee><blink><a href='" . wire('config')->urls->admin . "'>DO SOMETHING QUICK!!!</a></blink></marquee></html>");
        $m->send();
	}
});

Obviously if you trash items one by one, you’ll get one e-mail for every item as long as the total is under 8. A more proper way to do this may be a cron job that only checks once a day. Alternatively you may not need to send an e-mail at all? Perhaps it will suffice to show a message to the user who did the trashing?

wire()->message('Please remember to complete the daily menu! There are currently only {$numberOfItems} dishes.');

You could show this just after trashing or simply all the time whenever there are too few items.

  • Like 3
Link to comment
Share on other sites

Hi,

nice, this one works and sends mails exactly what I need. But is it possible to only get one mail if the number of remaining events is 7or lower, not for every event that goes to trash?
maybe something like 

if ($numberOfItems = 7) {

Just thinking here, I tried it and it still sends mails for every event that was put in a trash.

And 

 {$numberOfItems}

doesn't work in the sent mail, but don't really need it if the mail would come just once when the number is 7.

Thank you

R

Link to comment
Share on other sites

32 minutes ago, zoeck said:

Use " instead of ' ?  (in wire()->message)

whoops, sorry, I typed that straight into the forum textbox, pretty happy anything works at all ? I’ve changed the quotes above, so you should be able to copy it, @Roych

 

1 hour ago, Roych said:
if ($numberOfItems = 7) {

Just thinking here, I tried it and it still sends mails for every event that was put in a trash.

PHP’s equality operators are == and ===. This is a classic foot gun, there’s probably even a name for it… Your idea should be fine unless there’s some sort of bulk-trash feature that I don’t know about where the number of children could go from 8 to 6 in one go.

  • Like 1
Link to comment
Share on other sites

43 minutes ago, Jan Romero said:

I’ve changed the quotes above, so you should be able to copy it

Thanks for the update ? Just noticed it always gives number 1, Right now I have 12 events all together if I add some new ones with older date (just for testing) it deletes them but the message in mail still says "there are only 1 ...."

Link to comment
Share on other sites

13 hours ago, Jan Romero said:

Your idea should be fine unless there’s some sort of bulk-trash feature that I don’t know about where the number of children could go from 8 to 6 in one go.

Im trashin pages every day automaticaly with:

<?php
$theDayBeforeYesterday = strtotime("-1 day");
// get our pages
$reallyOldEvents = $pages->find("template=calendar-post, Start_date<=$theDayBeforeYesterday");
foreach($reallyOldEvents as $oldEvent){
    $pages->trash($oldEvent);
}
?>

Maybe this helps here. I still get mail for every trashed item, no matter how many is there.

Thank you

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...