Email notification on new article

Get help with installation and running PivotX 2.0.x here. Please do not post Extension or Theme related questions here.

Re: Email notification on new article

Postby hansfn » Thu Aug 02, 2012 11:35 am

Of course, the code is set when we use the "after save" hook. Sorry. Here is a better, more robust, version. (It also doesn't send mail if the page/entry isn't published.)

Code: Select all
<?php
// - Extension: The saved entry/page notification hook
// - Version: 0.3
// - Author: PivotX Team
// - Email: admin@pivotx.net
// - Site: http://www.pivotx.net
// - Description: This hook will send an e-mail notification each time an entry or page is saved.
// - Date: 2012-08-02

// NB! Edit the email address...
global $saved_notification_recipient;
$saved_notification_recipient = "you@example.org";

$this->addHook(
    'entry_edit_beforesave',
    'callback',
    'beforeSaveNotification'
);

$this->addHook(
    'page_edit_beforesave',
    'callback',
    'beforeSaveNotification'
);


function beforeSaveNotification(&$item) {
    global $PIVOTX, $saved_notification_recipient;

    // Do nothing if the item isn't published.
    if ($item['status']!="publish") {
        return;
    }
    $text = "The item '{$item['title']}' was ";
    $subject = "item ";
    if (!empty($item['code']) && $PIVOTX['db']->entry_exists($item['code'])) {
        $text .= "updated.";
        $subject .= "updated";
    } else {
        $text .= "created.";
        $subject .= "created";
    }
    // Uncomment the line below if you want all details about the entry/page
    // $text .= "\n\n" . var_export($item, TRUE);
    pivotxMail($saved_notification_recipient, $subject, $text);
}

PS! There is one drawback using the "before save" hook: The email is sent even if the saving fails...
hansfn
Developer
 
Posts: 3072
Joined: Sun Nov 25, 2007 7:48 pm
Location: Molde, Norway

Re: Email notification on new article

Postby hansi999 » Thu Aug 02, 2012 6:05 pm

Thanks a lot, this works like a charm!
Best regards
hansi
hansi999
 
Posts: 7
Joined: Mon Jul 30, 2012 3:36 pm

Re: Email notification on new article

Postby hansi999 » Sun Aug 05, 2012 12:08 pm

Hi,

finally i got the notification working. I'd like to include a link to the article, but both methods ( $PIVOTX['db']->entry and $entry) lead me to an empty link:
xxx/?e=

Is there a way to get the link at this point?
hansi999
 
Posts: 7
Joined: Mon Jul 30, 2012 3:36 pm

Re: Email notification on new article

Postby hansfn » Sun Aug 05, 2012 3:34 pm

(Removed original wrong reply.)

Since PivotX doesn't know the code before the entry is saved, PivotX can't determine any code based links. Yep, that is another drawback of using the "before save" hook. (If you switch to date based mod_rewritten entry links, I think $item['link'] works as expected even in the "before save" hook.)
hansfn
Developer
 
Posts: 3072
Joined: Sun Nov 25, 2007 7:48 pm
Location: Molde, Norway

Previous

Return to 2.x Support Forum

Who is online

Users browsing this forum: Google [Bot] and 1 guest