Hi I had a look around but couldn’t see what I was after. Does anyone know if there is snippet/plugin that send an automated email to a specific user when a specific document is edited?
Thanks!
Thanks a bunch that is an excellent resource.
I have it working as presented but I am trying to modify to send an email only once when a new document is created and saved from a specific template.
The way I plan to limit the email notification is to allow the site administrator (not designer/coder) to flag whether an email should be sent. I have created a check box template Variable in the specific template.
As per the recommended code, I have created a plugin to run on the event OnDocFormSave.
How do I clear the check box from the plugin before the document is saved?
I note that the recommonded code used $modx->getPageInfo to get TV values.
I have searched the internet and the Modx site for various versions of $modx->setPageInfo;
ie by various versions I mean common functioin names and tried code statementsd from other programming languages that do a similar task.
I’m just shooting from the hip here, but I think the TV you want should be in the $_POST array and you can modify it there.
You’ll have to use OnBeforeDocFormSave though, rather than OnDocFormSave, or it will be too late. OnDocFormSave fires after the doc has been saved to the DB.
BTW, the actual values of the TVs are in the modx_site_templvar_contentvalues table:
The fields are:
id
tmplvarid - The ID of the TV itself
contentid - The ID of a resource the TV is used in
value - The actual value of the TV for that document, based on the most recent save
So you could also use OnDocFormSave and write PHP code to clear the value using the current doc’s ID (which will change from doc to doc) and the TV’s ID (which should always be the same).
BobRay
Thanks for your suggestions.
I spent all day trying variations of your suggestions and tried $_POST, $_REQUEST and request_var()without success.
Using POST, I can obtain the basic page info such as the template ID by using ’template’ as the index.
However, when I use my template variable name, nothing is found.
Are user defined template variable name formatted in some way so that the name available to code is different to the user’s name?
With regard to using php code direct on the database, I had already thought that this approach would be the last resort.
Your comments set me thinking. The way the application will be used is to send the email mainly when a new page is created from the specific template. Hence, I assume that the page ID will not be available until it is saved to the database.
If the OnDocFormSave is used, is there a Modx function that returns the ID of the page just saved?
Yes, the tv names are prefixed with ’tv’ -- here’s some code I found (and modified slightly) that may help:
$tvs=array();
foreach ($_POST as $key => $value) {
//-- cycle through POST vars and pick out the tv values
if (substr($key, 0, 2) == 'tv') {
$tvKeyName = substr($key, 2);
if ($key == "tv" . $tvKeyName . '_prefix')
continue;
}
// -- add TV to array
$tvs[] = $key=>$value;
}
TBH, I’ve been working with MODx Revolution for a while, so I’m pretty rusty on Evolution (even on stuff I used to know).
I think the new document’s ID will be available as $_POST[’id’], but I’m not certain.
BobRay
Thank you again.
Prefixing my template variable with ’tv’ worked a treat for reading the value.
I used the following following structure to try to write a new value but it didn’t update the page. I tried it in the On and Before events.
$_POST[’tvemailSent’] = "yyyyy";
I suspect the $_POST used in the plugin is a copy of what is sent between events. Is this the case?
As for the document id of a new document, I detect that $_POST[’id’] is empty in the OnDocFormSave.
This is making me feel stupid. I’m sure there’s an easy way to do this.
Try $_REQUEST[’id’].
Also, I forgot about these:
$tv = $modx->getTemplateVar(’name_of_TV_’)
$modx->getInsertId();
$id = $modx->getDocumentIdentifier(’id’);
I’m not sure if they are available in the events but worth a try. getInsertId() should definitely give you the document ID in onDocFormSave.
At worst, you can use the Db-API to do everything:
http://wiki.modxcms.com/index.php/API:DBAPI
BobRay
Thank you very much for all your comments and suggestions.
Unfortunately, I still haven’t developed or found a solution.
I’m going to carry on with a slotution where the Content Managed has to indicate whether to send an email or not.
The getInsertId() funtion didn’t seem to work.
I think I need to do more research to try to understand exactly when and how the POST information is available before it is written to the database.
Alternatively, maybe Modx needs a PostDocFormSave event where the id of the document is available.
Another track I thought about was to see if a template variable or an HTML input field could be defined but marked as not to be written to the database. ie the TV is used only as a ’flag’ and would contain a default value every time a document was open.
However, I didn’t spend too long on this as I could find a TV property for this purpose and a chunk (for the HTML input field) didn’t displsy in the doc edit screen.
Thanks again.