Create a Plugin

Open Source CMS

 

Toasted Site of the Week

network consultant stamford

Another professional technology services firm standardizing on seotoaster. Tarrytech is located in Stamford, CT  and provide network support and IT consulting services in Stamford, CT and White Plains.

 

Stay in Touch

 

facebook

Become a fan and get all latest updates as soon as we push them out of the toaster labs

 

SEO twitter

 

Questions?
Post on our forum

 

Why wait? try seotoaster right now: Access our online demo

Note to users: The menu above is created by adding {%main_menu} either in the template or through editor

 

A live training session (Free, as in no money, as in you gotta be there!) is scheduled every Thursdays at 10.30 AM PST. Enroll now and get a chance to toast websites like a pro in no time!

With new plug-in system you will be able to:

    1. Add custom widgets
    2. Add new action in the Seotoaster's right-side admin panel
    3. Add new custom routes (one route per plugin)

 

A. All plug-ins should be in the "plugins" directory in the root folder of seotoaster

Code:
unzipped_seotoaster_package_dir /
|_...
|_plugins /
|_plugin1
|_...
|_pluginN
|_...

B. Plugin structureedit header

PLUG-IN DIRECTORY SHOULD BE NAMED IN LOWERCASE

There are couple of files that your plug-in should have:

  1. Main plug-in class file. This file should be named exactly as plug-in directory The name of this file should start with capital first letter. All other letters should be in lowercase.
  2. readme.txt file. This file should contain plug-in description and/or some important information about your plug-in if needed
  3. "system" folder.
  4. SYSTEM FOLDER should contain 2 files:
    1. install.sql - Might contain sql queries that will be executed during the plug-in installation. Each query in this file should be separated by /*~query~*/ delimiter
    2. uninstall.sql - Might contain sql queries that will be executed during the plug-in deinstallation. Each query in this file should be separated by /*~query~*/ delimiter.
  5. [optional but recommended if you want to add new items in the admin panel or add new routes] "config" folder.
    Config folder should contain "config.ini" file

After all your basic plugin structure should look like:
(let's assume that we are creating plugin called "Demo"). In this case my plugin folder will looks like:

Code:
/plugins/
|_demo
|_config
|_config.ini
|_system
|_install.sql
|_uninstall.sql
|_readme.txt
|_Demo.php

C. Main plug-in file structure

In this example bellow our main plug-in class file is Demo.php. This class should implement RCMS_Core_PluginInterface According to this file, Demo.php should contain public method run

Code:
public function run($params = array()) {}

(Seotoaster try to call this method each time when it meets the plug-in) The second public method - constructor.

Code:
public function __construct($options, $seotoasterData) { }

Full basic plugin main file code listing

Code:
class Demo implements RCMS_Core_PluginInterface {

public function __construct($options, $seotoasterData) {

}

public function run($params = array()) {

}
}

 

LET"S CREATE A "HELLO WORLD" WITH OUR PLUGIN

Now let's add the following line into the "run" method of our plugin:

Code:
return "Hello world";

Now your plugin code should looks like

Code:
class Demo implements RCMS_Core_PluginInterface {

public function __construct($options, $seotoasterData) {

}

public function run($params = array()) {
return "Hello world";
}
}



Now go to your Seotoaster site. On the admin panel open the "OTHER" section and click "enable" under your plugin. Then go and edit any content of your site and put . Save your content. After a page refresh you will see "Hello world".

In this overview I will use the basic plugin structure that I described in the previous part.

Code: demo /
|_config
|_config.ini
|_system
|_install.sql
|_uninstall.sql
|_readme.txt
|_Demo.php

1. Plugin configuration file config.ini

The plugin configuration file might contain any data you want/need for your plugin. For example:

Code: [section]
foo = "bar"
foo1 = "bar1"
fooarr[] = "baritem1"
fooarr[] = "baritem2"

But you can add special sections in your config.ini and Seotoaster will recognize and process them. This sections are:

 

A. [cpanel] section ---> Allows you to extend Seotoaster's right-side admin panel with your items.
Example:

Code: [cpanel]
title = "Demo plugin" //new menu title
items[] = 'demo item 1' //menu items
items[] = 'demo item 2'

B. [route] section --> Allows you to extend Seotoaster's routes with your plugin's route.
Example:

Code: [route]
name = 'demo' //name of your new route
type = 'Zend_Controller_Router_Route' //route type
route = '/demo' //route
method = 'rundemo' //method of your plugin that will be
lounched

2. Install & uninstall sql files

2.1. install.sql

Seotoaster will try to execute the install.sql file during the installation process. In this file you might add any sql queries you want but with one restriction - queries should be separated by the /*~query~*/ delimiter.
Example:

Code: DROP TABLE IF EXISTS `demo_settings`;
/*~query~*/
CREATE TABLE `demo_settings`(
`name` VARCHAR(255) NOT NULL ,
`value` VARCHAR(255) NOT NULL
);
/*~query~*/
INSERT INTO `demo_settings`(`name`, `value`) VALUES ('title', 'Demo plugin');

2.2. uninstall.sql

Seotoaster will try to execute the uninstall.sql file during the uninstallation process. In this file you might add any sql queries you want but with one restriction - queries should be separated by the /*~query~*/ delimiter. The good way to uninstall your plugin is to remove all additional fields/table/etc... that your plugin created.
Example:

Code: DROP TABLE IF EXISTS `demo_settings`;

Try the first free open source SEO CMS http://www.seotoaster.com

In this part I'll continue working with the basic plugin structure:

Code: demo /
|_config
|_config.ini
|_system
|_install.sql
|_uninstall.sql
|_readme.txt
|_Demo.php

A. Working with views

et's create a new directory and call it views. Inside of this directory create a new file called demo.phtml. Now our plugin folder will look like this:

Code: demo /
|_config
|_config.ini
|_system
|_install.sql
|_uninstall.sql
|_views /
|_demo.phtml
|_readme.txt
|_Demo.php

Edit demo.phtml and put in it follwing lines:

Code: pluginTitle; ?> plugin view demonstration

Now edit your Demo.php. We will create an instance of Zend_View and ask plugin to work with it. Now __construct method of your plugin will look like:

Code: public function __construct($options, $seotoasterData) {
$this->_view = new Zend_View(array(
'scriptPath' => dirname(__FILE__) . '/views'
));
}

Now let's go to the run method and work a bit with our new view object

Code: public function run($params = array()) {
$this->_view->pluginTitle = 'Demo';
return $this->_view->render('demo.phtml');
}

Now (if you didn't do it yet) go to one of your seotoaster pages and in any content put Plugin demo doesn't exists (REMEMBER that you plugin should be enabled in Seotoaster plugin management screen: OTHER -> Plugins)

Refresh your page and you will see the results:

Code: Demo plugin view demostration

Since we use Zend_View as view engine we can use all its powerful features such as view helpers, etc...

B. Including javascript and css files

Let's modify our plugin structure by adding css and js folders. Now plugin structure will look like this:

Code: demo /
|_config
|_config.ini
|_system
|_install.sql
|_uninstall.sql
|_css /
|_demo.css
|_js /
|_demo.js
|_views /
|_demo.phtml
|_readme.txt
|_Demo.php

We will use standart Zend view helpers to add our css and js file to our views: HeadScript and HeadLink. But first we will need to assign a website URL to our view, so, go to the your plugin's __construct method and add:

Code: $this->_view->websiteUrl = $seotoasterData['websiteUrl'];

$seotoasterData - array that Seotoaster pass to your plugin __construct method. This array contain a lot of useful data and we recommend to discover it.

Now your plugin's constructor will look like:

Code: public function __construct($options, $seotoasterData) {
$this->_view = new Zend_View(array(
'scriptPath' => dirname(__FILE__) . '/views'
));
$this->_view->websiteUrl = $seotoasterData['websiteUrl'];
}

Now go and edit your demo.phtml by adding following lines:

Code: headLink()->appendStylesheet($this->websiteUrl . 'plugins/demo/css/demo.css'); ?>
headLink(); ?>
headScript()->appendFile($this->websiteUrl . 'plugins/demo/js/demo.js'); ?>
headScript(); ?>

That's it.

Working with database

In the previous part we created the following plugin structure:

Code: demo /
|_config
|_config.ini
|_system
|_install.sql
|_uninstall.sql
|_views /
|_demo.phtml
|_readme.txt
|_Demo.php

So, now let's teach our plugin to work with the database. Let's create a new directory called models and create a class, which will work with the database. Let's call it DemoModel.php after all actions plugin structure will look like:

Code: demo /
|_config
|_config.ini
|_system
|_install.sql
|_uninstall.sql
|_views /
|_demo.phtml
|_models /
|_DemoModel.php
|_readme.txt
|_Demo.php

Now we will edit DemoModel.php
To make your plugin model work with the seotoaster database you do not need to set up your connetcion or so, Seotoaster will do this for you. One thing you need is to extend your model from the seotoaster base model class RCMS_Core_BaseModel. So, your DemoModel.php should look like this:

Code: <?php
class DemoModel extends RCMS_Core_BaseModel {

}

That's it. Now you can start adding methods into your model, then require your DemoModel.php in your Demo.php. For example let's get all data from the Seotoaster's 'config' table. This table has two fields 'name' and 'value', so our method will be something like this:

Code: class DemoModel extends RCMS_Core_BaseModel {
public function selectSeotoasterConfig() {
$select = $this->getAdapter()->select()->from('config');
return $this->getAdapter()->fetchPairs($select);
}
}

Now lets use our model in Demo.php. Open Demo.php and add require statement at the top

Code: <?php
require_once 'models/DemoModel.php';

then add following lines to the 'run' method

Code: public function  run($params = array()) {

$model = new DemoModel();
$toasterConfig = $model->selectSeotoasterConfig();
$this->_view->seotoasterConfig = $toasterConfig;

$this->_view->pluginTitle = 'Demo';   
return $this->_view->render('demo.phtml');
}

That's it, we read the config data from the database in associative array and assigned it to the view.