Jump to content

Two different cases where I'd like to email the page's creator


creativejay
 Share

Recommended Posts

In my ProcessBlog project, I have multiple authors and would like to send notifications on a couple different kinds of events.

Situation 1:

First, for blog_comments (a FieldtypeComments field) on the blog posts, I'd like to email the page's creator when there is a comment posted. Since the field's settings ask for a constant value for emails to send notifications to, I'm wondering if I should hook onto this field somehow, or if there's a syntax by which I can specify the superuser(s) and the person whose post it is (their email address is stored in their user page). I looked in the FieldtypeComments module for this and didn't see an obvious place where I could tack in code to append $createduser->email

Situation 2 (& 2b):

Second, I created a system of posts, sort of like a ticket system, by which users can communicate with each other (without having to know personal contact info). A user would issue one of these messages (by creating a page) and select the user from a Page field of qualified users. When they publish this page, I'd like to send an email to the user that was selected ($targeteduser->email).

This page also has a comments field, and I'd like one user to get an email when the other user comments (similar to the first situation, except slightly more aware of who is posting and who is referenced in the page).

Since both situations deal with page saves (saves generated by FieldtypeComments in situations 1 and 2b, and general page saves with publish in situation 2), I don't want to hook this to the page save or there might be excess emails going out. Not really familiar enough with hooks to know what else to consider, and nothing jumped out at me when I browsed the Captain Hook list.

I imagine I'd create a new module, possibly 2, that looks at the page template and draws information from the fields in it, but I am a little stuck beyond that and would appreciate help with as much detail as anyone is willing to go into.

Thanks in advance!

Link to comment
Share on other sites

Hard to know the exact use case you are talking about. I'm not really clear of the method you are using to create new comments on a page. But in case I understand your scenario and you don't want to hook into page->save via modules or something:

You can create a separate template or two that just process forms for comment creation and ticket creation. So for example you have a user-facing page which has a form that submits to a hidden page called "/createticket/", and all the necessary data - the user-id of the selected user, the subject and message etc - are sent to the template. That template can create a new page for the user and while saving the page (using the API of course), do something like:

$newpage = new Page();

...//all the page related necessary data like template, title, parent and your own field data

if($newpage->save()) {
     notifyByMail($user,$selected-user);
}
and have the notifyByMail function right there in that template file if you like, and if the mail is sent, you can redirect user to the newly created page with a success message.

For comments too, if you are using Repeater fields to create comments on the page (e.g some $mypage), just send the input of the comment form to some other page whose template will process the input, create the repeater item, and on $mypage->save($comment) you can again hook in a notifyByMail function. (Not sure if that's how you save repeaters, I haven't used them in a while...)

Basically it sounds like you only want to hook into the first page/repeater save, so it is best to do it when you are creating the page/repeater. Doing that via API makes it pretty easy.

Link to comment
Share on other sites

Thanks for your reply and suggestions, nickie. I'm not sure this is what I'm trying to do.

The comments are created using a FieldtypeComments field called challenge_comments. This is, I believe, one of Ryan's core modules.

I can use FormBuilder to set up comments, but the function is already largely built-in. And I don't mind hooking to a page, but save doesn't seem the right place to do it in the first cast. The targeted user should get an email when a page is published meant for them (they are selected in a dropdown in the page creation template). This would happen in the admin back end.

Then both the page creator and the targeted user should be notified when the other posts a comment to that published page (front end).

Link to comment
Share on other sites

The built-in comment field is probably even easier to handle, since all its saving functionality is built in. (https://processwire.com/api/fieldtypes/comments/)

I was only mentioning "save" in the context of the first time a page or a comment is saved - if you do it via the API and all the fields required to publish the page are provided, the page IS published as well as saved then. So it would only happen that one time (e.g. in my code snippet in the earlier comment).

So the forms for ticket creation and/or comment creation can have their 'action' attribute link to a page whose template processes the input va API. And yes, this would require some customization of the form, to send all the information you require (the other user's email address etc).

I think either this way or hooking in the back-end with a module (and having both user emails as fields on the page in that case) might be the only two ways to achieve what you are needing, but I'll let others confirm that.

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

×
×
  • Create New...