Homepage of www.easymagazine.net | Wem site map of www.easymagazine.net | Email the author of www.easymagazine.net

 

How to create a Filter

A filter is an implementation of the command pattern.
You can download the filter sandbox in order to have a skeleton to develop your plug-in but you'll see how simple it is!

As software developer we like to start with some code:

require_once(FILTERPATH.'/filtercommand.php');
require_once(FILTERPATH.'/articlefilterremote.php');

class ProofFilter implements FilterCommand {

function execute($string){
    return $string." filter executed";
}

}

$filter= new ProofFilter();

$articleRemote = ArticleFilterRemote::getInstance();

$articleRemote->addFiltersGetTitle($filter);

This is just a little example, this filter add the string " filter executed" to every Article title. It is not really useful but is good to give you an idea.
After you understand this you can try to do someting more interesting, maybe with some table of words to filter saved in a database.

Ad the start of the script we include the filtercommand interface and the articlefilterremote singleton.
If you are curious about this script just check the files filtercommand.php and articlefilterremote.php in the admin/filters directory in your EasyMagazine installation.

Then we create the class ProofFilter that implements the interface FilterCommand. This means that we must create a method execute($string). This method will be executed every time the trigger is pulled.
Be careful a filter needs an input and an output so you must implement a String parameter and you must return a String output.

That's it, the filter is ready! :-)

Now we need to decide to what of database fileds the filter has to be connected.
We decide to let it execute everytime the Title of an Article is showed in the magazine!

So we create an instance of the filter ProofFilter, we get the instance of the ArticleFilterRemote singleton and we add the $filter instance to the filter array calling $remote->addFiltersGetTitle.

Now we finished, the filter will be executed as we desire, every time the Article title is showed in the website.

Now a list of what Entity you can attach a filter to:

Article:
(you need to include articlefilterremote.php)

  • addFiltersGetTitle($filter)
  • Add a filter to the $article->getTile() method.
  • addFiltersGetSubTitle($filter)
  • Add a filter to the $article->getSubTile() method.
  • addFiltersGetSummary($filter)
  • Add a filter to the $article->getSummary() method.
  • addFiltersGetBody($filter)
  • Add a filter to the $article->getBody() method.
  • addFiltersGetTag($filter)
  • Add a filter to the $article->getTag() method.

Comment:
(you need to include commentfilterremote.php)

  • addFiltersGetTitle($filter)
  • Add a filter to the $comment->getTile() method.
  • addFiltersGetBody($filter)
  • Add a filter to the $comment->getBody() method.
  • addFiltersGetSignature($filter)
  • Add a filter to the $comment->getTag() method.

Number:
(you need to include numberfilterremote.php)

  • addFiltersGetTitle($filter)
  • Add a filter to the $number->getTile() method.
  • addFiltersGetSubTitle($filter)
  • Add a filter to the $number->getSubTile() method.
  • addFiltersGetSummary($filter)
  • Add a filter to the $number->getSummary() method.

Page:
(you need to include pagefilterremote.php)

  • addFiltersGetTitle($filter)
  • Add a filter to the $page->getTile() method.
  • addFiltersGetSubTitle($filter)
  • Add a filter to the $page->getSubTile() method.
  • addFiltersGetSummary($filter)
  • Add a filter to the $page->getSummary() method.
  • addFiltersGetBody($filter)
  • Add a filter to the $page->getBody() method.
  • addFiltersGetTag($filter)
  • Add a filter to the $page->getTag() method.

 


Copyright © 2009, 2010, 2011 Fabio Mattei, designed by zuudesign.sk