Skip to content

Instantly share code, notes, and snippets.

@oligriffiths
Last active December 19, 2015 10:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oligriffiths/5940659 to your computer and use it in GitHub Desktop.
Save oligriffiths/5940659 to your computer and use it in GitHub Desktop.
Docman example plugin
<?php
/**
* @package Koowa.Plugin
* @subpackage Docman
*
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
defined('KOOWA') or die;
class PlgKoowaDocman extends PlgKoowaDefault
{
/**
* Plugin name, usually the component name
* @var string
*/
protected $_name = 'docman';
/**
* After document is saved, look for certain keywords and do something
*
* @param KEvent $event
*/
public function onAfterDocumentControllerEdit(KEvent $event)
{
//The result of the controller action is stored in the "result" property
//In this case, the result is a KDatabaseRowDefault object
$row = $event->result;
//The row contains properties that map to the database table columns
$description = $row->description;
//You can now do anything you want with the data, for example look for certain keywords
$year = $author = null;
if(preg_match('#{year:([\s0-9]*)}#', $description, $match)){
$year = trim($match[1]);
}
//Or 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
}
}
/**
* Add events are processed the same as edit events
*
* @calls $this->onAfterDocumentControllerEdit($event);
* @param KEvent $event
*/
public function onAfterDocumentControllerAdd(KEvent $event)
{
$this->onAfterDocumentControllerEdit($event);
}
/**
* On delete, remove the custom values stored
* @param KEvent $event
*/
public function onAfterDocumentControllerDelete(KEvent $event)
{
$row = $event->result;
//Do something to delete the stored values using $row->id as the key
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment