Auto-notification emails pulling wrong page author
Permalink
Hi, folks.
I've got my c5 installation set up to notify the admins whenever a user adds/updates a page (admins are required for approval of user edits). The emails are triggered by the on_page_version_add event, and they get sent out properly.
But one of the things I want to include in the email is the username of the user that performed the most recent edits. And for some reason, the code seems to be pulling the name of the user that performed the edits on the previous version, not the updated version. The relevant code follows:
I'm using the Event Tester add-on to log the on_page_version_add event, and I noticed that on_page_version_add seems to have two arguments... Page Object and Collection Version Object. For some reason, that code is getting the username from the Page Object (i.e., the existing author's name) instead of getting it from the Collection Version Object (i.e., the name of the person modifying the page and creating a new version). Is there a way to modify that code to check that the version is most recent before getting the username? Or maybe the code could get the name of the user that checked the collection out?
The Event Tester log is attached to hopefully illustrate the issue.
I've got my c5 installation set up to notify the admins whenever a user adds/updates a page (admins are required for approval of user edits). The emails are triggered by the on_page_version_add event, and they get sent out properly.
But one of the things I want to include in the email is the username of the user that performed the most recent edits. And for some reason, the code seems to be pulling the name of the user that performed the edits on the previous version, not the updated version. The relevant code follows:
function on_page_version_add($page) { $name = $page->getCollectionName(); $parentid = Page::getByID($page->getCollectionParentID()); $parentname = $parentid->getCollectionName(); $author = $page->getVersionObject()->getVersionAuthorUserName(); $location = View::url($page->getCollectionPath()); if ($location=="/concrete/") { ChangeNotifications::send($page, " The $name document in the Document Library has been added by $author and is awaiting review. Please login to the document library and complete the following steps to review the new document:\n\n1. Go to the Sitemap in the library Dashboard athttp://localhost/concrete/index.php/dashboard/sitemap/full/...\n\n2. Find the $name document under the $parentname node in the Sitemap.\n\n3. Click the document name and select the Visit option from the menu to open the document.\n\nThanks,\nDocument Library Admin", 'admins@email', 'system@email', "New $name Document Ready for Review"); } else { ChangeNotifications::send($page, " The $name document in the Document Library has been updated by $author and is awaiting review. Please login to the document library and review the Document athttp://localhost$location.\n\nThanks,\nDocument Library Admin", 'admins@email', 'system@email', "Updated $name Document Ready for Review"); } }
I'm using the Event Tester add-on to log the on_page_version_add event, and I noticed that on_page_version_add seems to have two arguments... Page Object and Collection Version Object. For some reason, that code is getting the username from the Page Object (i.e., the existing author's name) instead of getting it from the Collection Version Object (i.e., the name of the person modifying the page and creating a new version). Is there a way to modify that code to check that the version is most recent before getting the username? Or maybe the code could get the name of the user that checked the collection out?
The Event Tester log is attached to hopefully illustrate the issue.
Try changing the code to use the new version that gets passed to the event:
Hi, Ryan.
Thanks for taking the time to try and help me on this. I changed the code as you suggested, but it still pulled the author name from the previous version. I'm not really a coder (I'm a tech writer), so maybe I don't understand how this is supposed to work. Where does the $nv variable pull its value from?
EDIT: Wait, actually, I'm an idiot. I was using the same login as the previous version, so I was going to get the same result regardless. After changing logins, it worked beautifully. And I understand now that there are two arguments (as if the log didn't make that clear enough) and that the $nv element correlates to the second argument that contains the new version info. Thanks again, Ryan.
Thanks for taking the time to try and help me on this. I changed the code as you suggested, but it still pulled the author name from the previous version. I'm not really a coder (I'm a tech writer), so maybe I don't understand how this is supposed to work. Where does the $nv variable pull its value from?
EDIT: Wait, actually, I'm an idiot. I was using the same login as the previous version, so I was going to get the same result regardless. After changing logins, it worked beautifully. And I understand now that there are two arguments (as if the log didn't make that clear enough) and that the $nv element correlates to the second argument that contains the new version info. Thanks again, Ryan.
The following code is working on my local test site:
config/site.php
config/site_events.php
/models/version_approve.php
- I've tried with caching on and off, both times it records the proper usernames in the dashboard > reports > Logs
config/site.php
define('ENABLE_APPLICATION_EVENTS', true);
config/site_events.php
Events::extend('on_page_version_add', 'VersionNotifier', 'log_values', 'models/version_approve.php');
/models/version_approve.php
class VersionNotifier { public function log_values($c, $nv) { //Log::addEntry(var_export($nv,true)); $author = $nv->getVersionAuthorUserName(); $vID = $nv->getVersionID(); Log::addEntry($author." | ".$vID); } }
- I've tried with caching on and off, both times it records the proper usernames in the dashboard > reports > Logs
Thanks for trouble shooting this Ryan,
However, when I use your code, I get an warning:
Line 27 is $author = $nv->getVersionAuthorUserName for function on_page_add($page,$nv) {
I think I'm not loading a helper or something for $nv.
Everything else works fine, but it doesn't get the $nv or it's author or additional info, so I'm sure I'm missing some little detail.
Any advice would be much appreciated!
However, when I use your code, I get an warning:
Warning: Missing argument 2 for ChangeNotifications::on_page_add() in /var/www/vhosts/httpdocs...change_notifications.php on line 27
Line 27 is $author = $nv->getVersionAuthorUserName for function on_page_add($page,$nv) {
I think I'm not loading a helper or something for $nv.
Everything else works fine, but it doesn't get the $nv or it's author or additional info, so I'm sure I'm missing some little detail.
Any advice would be much appreciated!