Forum for discussing general topics related to Couch.
5 posts Page 1 of 1
I'm in the process of setting up my second CouchCMS site and I'm loving how easy it makes it for me to give my site-owners an easy interface.

One thing I'd like to be able to do would be to trigger an external script when a particular page is saved. I have a set of images on the page that the site-owner can update (uploading new ones and adding them to or removing them from the page). Ideally, when the page is saved I'd like to trigger a script which automatically checks which pictures are no longer in use and removes them from the server, to keep it tidy.

Is this possible or do I need to work out a different solution?

Thanks.
Hi :)

It is possible but will require using PHP (going by your question, I'll assume you are conversant with PHP so that is not going to be a problem).

Couch v1.4.5RC1 and above (RC2 can be downloaded from viewtopic.php?f=5&t=8981) throws 'events' at most of the important points while processing a page.

We can then 'hook' on to the particular event we are interested in and then add our routine at that place.

For your case, the 'page_saved' event (the last event before a page is saved) seems to be ideal.
Place the following code in 'addons/kfunctions.php' file and then do your stuff at the indicated place (make sure to use the correct template name in the code). Place a breakpoint using your debugger to see all the data available to you at that point.
Code: Select all
$FUNCS->add_event_listener( 'page_saved', 'my_page_save_handler' );

function my_page_save_handler( &$page, &$error_count ){
   
    if( $page->tpl_name=='example.php' ){ // if this is the template we are interested in
        if( !$error_count ){// no errors at this point so the page is definitely going to be saved
           
            // this is where you can take your action
            // increase the $error_count if you want to prevent the page form being saved
            // DO SOMETHING

        }
    }
   
}

Hope it helps.
Excellent!

Many thanks for a remarkably quick reply as well.

Cheers!
This is truly useful. It means that post-processing can happen after a data-bound form has been submitted. For me, this is a very big plus!

David
dmore54 wrote: This is truly useful. It means that post-processing can happen after a data-bound form has been submitted. For me, this is a very big plus!

And if, like me, your PHP isn't particularly good, it's dead easy to break out from it to a script in another language such as Perl. Which is a big bonus.
5 posts Page 1 of 1