Jump to content
creativejay

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

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!

Share this post


Link to post
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.

Share this post


Link to post
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).

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Thanks for your help nickie. I'm still not entirely clear on this, but I'll give it a go today when my coffee has kicked in.

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By celfred
      Hello,
      I'm struggling with hooks to try and do this : adding user page to a page field when visiting a particular page on my website. In fact, I want to have a list of connected users on a particular page (called meeting-hall).
      I've tried all kinds of things (which I don't really understand, sorry) but the latest is this :

        $wire->addHookBefore('Page::render', function($e) {     $page = $e->arguments(0);     if ($page->is("name=meeting-hall")) {       $page->connected->add($user); // connected is a Page field       $page->save();     } else {       $meeting = wire("pages")->get("name=meeting-hall");       $meeting->connected->remove($user);       $meeting->save();     }   }); I put this in my _init.php file but nothing works...
      The best I managed was updating my 'connected' field when the user loads the 'meeting hall' page by having this on the page template :

          if ($user->isLoggedin()) {       $page->connected->add($user);       $page->of(false);       $page->save();       $page->of(true);     } But I wanted to remove the user when he or she leaves the page... hence my thought about using hooks...
      If you can give me a hint, I'd greatly appreciate !
    • By Gadgetto
      Hi there,
      I'd like to prevent duplicate values in a specific page field (FieldtypeText) across all pages. The hook should also prevent saving of the page if a duplicate value is detected. Therefore I created two hook methods but I can't get it to work properly.
      Here are both hooks placed in init() method of an autoloader module:
      The first hook is to initially preset the field with the page id. The field value can be customized by the admin before saving.
      The second hook should check if the value is unique.
      If I create a new page and try to save, the WireException is already triggered in first step of page creation, where you enter the page title: SKU [] is already in use
      If I check the value of $sku (with Tracy) it's always empty!
      Any idea what could be wrong?
      EDIT: if I remove the second hook, the field value is correctly preset with the page id!
      $this->addHookAfter('Pages::added', $this, 'presetProductFields', ['priority' => 99]); $this->addHookAfter('Pages::saveReady', $this, 'checkSKUUnique', ['priority' => 101]); And this are the hook methods:
      public function presetProductFields(HookEvent $event) { $snipwire = $this->wire('snipwire'); if (!$snipwire) return; $page = $event->arguments(0); if ($snipwire->isProductTemplate($page->template)) { if ($page->hasfield('snipcart_item_id')) $page->setAndSave('snipcart_item_id', $page->id); } } /** * Check if the SKU value is unique across all product pages. * (Method triggered after Pages saveReady -> just before page is saved) * * @throws WireException * */ public function checkSKUUnique(HookEvent $event) { $snipwire = $this->wire('snipwire'); if (!$snipwire) return; $page = $event->arguments(0); if ($snipwire->isProductTemplate($page->template)) { $field = $page->getField('snipcart_item_id'); $sku = $page->snipcart_item_id; // SKU field value bd($sku); if ($page->isChanged('snipcart_item_id')) { $exists = $this->wire('pages')->get("snipcart_item_id=$sku"); if ($exists->id) { // value is not unique! $error = $this->_('SKU must be unique'); $exception = sprintf( $this->_('SKU [%s] is already in use'), $sku ); $inputfield = $page->getInputfield($field); $inputfield->error($error); throw new WireException($exception); // Prevent saving of non-unique value! } } } }  
    • By t0b1
      Hello there, and thanks for ProcessWire!
      I'm getting to know ProcessWire while doing my first project using it and I really like it so far.
      The challenge I'm facing right now is the following:
      I have a One-Pager using fullpage.js, realized as a single PW-Page containing a Repeater Field where each Repeater Item is one Section.
      Some Sections are supposed to have a little menu at the Top which references/links to different Sections of the Website, so I wanted to use a Checkbox Field "Top Menu" to decide if a Section gets a Menu and a Page Reference Field to choose the different Sections (Repeater Items) it should contain.
      I've already accomplished this by pasting the following code into /site/ready.php:
       
      $wire->addHookAfter('InputfieldPage::getSelectablePages', function($event) { if($event->object->hasField == 'top_menu_entries') { $page = $event->arguments('page'); if($page instanceof RepeaterPage) $page = $page->getForPage(); $event->return = $page->; } });  
      The only problem that still remains is that when I select the Entry of the Repeater Item itself it doesnt save the selection, meaning after I saved it's unselected again.
      On some Sections I do want a Menu-Entry for the Section itself though (which would be styled differently and not link anywhere) for Continuity-Reasons, any ideas on how to achieve that?
    • By Gadgetto
      Hi guys (didn't see any gals here),
      I have a module which has some custom helper classes. One of these classes has methods which are hookable. The class is derived from WireData. An additional complication is that this class has its own namespace. What would be the correct way to provide the hookable methods via API?
      Here is a very simplified code sample of the class:
      <?php namespace SnipWire\Services; use ProcessWire\WireData; use ProcessWire\WireException; class Webhooks extends WireData { ... public function ___handleOrderCompleted() { if ($this->debug) $this->wire('log')->save( self::snipWireWebhooksLogName, '[DEBUG] Webhooks request: handleOrderCompleted' ); $this->responseStatus = 202; // Accepted } public function ___handleOrderStatusChanged() { if ($this->debug) $this->wire('log')->save( self::snipWireWebhooksLogName, '[DEBUG] Webhooks request: handleOrderStatusChanged' ); $this->responseStatus = 202; // Accepted } ... } The class itself is invoked/used by a hook from within the main module:
      Depending of the event, one of the methods of the Webhooks class above is triggered. A developer should now be able to use one of the ___handle* methods to do further things.
      /** * Check for webohook request and process them. * (Method triggered before ProcessPageView::execute) * */ public function checkWebhookRequest(HookEvent $event) { if ($webhooksEndpoint = $this->get('webhooks_endpoint')) { if ($this->sanitizer->url($this->input->url) == $webhooksEndpoint) { /** @var Webhooks $webhooks Custom ProcessWire API variable */ $this->wire('webhooks', new Webhooks()); $this->wire('webhooks')->process(); $event->replace = true; // @note: Tracy Debug won't work from here on as normal page rendering is omitted! } } } Should I provide a custom API variable e.g. $webhooks? Or how is the best way to do this?
    • By Flashmaster82
      Hi, can you guys help a beginner with a problem..
      On my template (profile_page) i have a dropdown (page reference) where i can choose a sports team (team_page) that is related to that profile which is also its parent. Then on my Competition1 page (competition_page) I have page reference field (profiles) a dropdown that i want to display only profiles that has choose a specific sports team (template=team_page) the page parent to be specific.
      Structure/Template
      Sports_team1 (team_page)
           Profile1 (profile_page)
           Profile2 (profile_page)
           Profile3 (profile_page)
           Competition1 (competition_page)
       
      ready.php
      <?php $wire->addHookAfter('InputfieldPage::getSelectablePages', function($event) { if($event->object->hasField == 'profiles') { $relative = $page->parent->name; $event->return = $event->pages->find("template=profile_page, sports_team=$relative"); } }); ?> This returns with no results in the dropdown. If i remove sports_team=$relative then it displays all profiles that have profile_page as template, so it works almost. But i will have more sports teams so this is just an example. I only want to display the profiles that has choosen the parent team on there profile page in admin not front end.
      I hope i was able to explain it so you guys can understand a little bit. Need some help please! /Thanks
       
       
×
×
  • Create New...