Plugin Example

The document description will contain keywords for special tagging (year of publishing, author, etc). When the document is saved or modified the keywords are extracted from the description and stored in a custom database table.

Events to respond to:

  • When: After - we do this after the action has run so that if the action were to fail, our event listener won't run.
  • Component: Docman - This is to only affect the DOCman extension.
  • Name: Document - We're only interested in documents
  • Action: Add & Edit - Save/modify events

Therefore the event method names we need are:

<?php
class PlgDocmanDocument extends PlgKoowaSubscriber
{
    onAfterDocmanDocumentControllerAdd(KEventInterface $event){}
    onAfterDocmanDocumentControllerEdit(KEventInterface $event){}
}

Save event

Save events cover both Add and Edit. The simplest way to have both of these methods run the same code is have one call the other.

<?php
class PlgKoowaDocman extends PlgKoowaSubscriber
{
    onAfterDocmanDocumentControllerAdd(KEventInterface $event)
    {
        return $this->onAfterDocmanDocumentControllerEdit($event);
    }
    onAfterDocmanDocumentControllerEdit(KEventInterface $event){}
}

Edit event

We want to react to certain keywords in the description for special tagging, like:

  • {year:2015}
  • {author:Stephen Hawking}

The description field of the document that was added or edited is contained within the result property of the $event object. It's the result of the action performed.

<?php
public function onAfterDocmanDocumentControllerEdit(KEventInterface $event)
{
    //The result of the controller action is stored in the "result" property
    $row = $event->result;
    //The row contains properties that map to the database table columns
    $description = $row->description;
}

Extract tags

Some simple regular expression matches will extract the year and the author:

<?php
public function onAfterDocmanDocumentControllerEdit(KEventInterface $event)
{
    //The result of the controller action is stored in the "result" property
    $row = $event->result;
    //The row contains properties that map to the database table columns
    $description = $row->description;
    //Get the year
    $year = $author = null;
    if(preg_match('#{year:([\s0-9]*)}#', $description, $match)){
        $year = trim($match[1]);
    }
    //Get the author
    if(preg_match('#{author:([\s\w]*)}#', $description, $match)){
        $author = trim($match[1]);
    }
    //Now do some custom query to store these values, perhaps store in a table using $row->id as an index?
    if($year){
        //Do something
    }
    if($author){
        //Do something
    }
}

This example plugin can be found on GitHub. You can even install it with Composer.