Plugins are extra components you as the developer can to add to Netharbour to implement new functionality or help make certain items more efficient.
To make a plugin, you need to have 2 files.
- plugin.php
- config.xml
Below will be an explanation on each of these files.
config.xml
This is your configuration file. In here you will need to specify:
- the name of the class your plugin will be using <className>
- the name of the plugin itself <name>
- the location in which you want your plugin to be displayed <location>
- the sublocation if you want it to be a page within an existing page <subLocation>
- the order in which you want your plugin to be displayed <order>
- the description for your plugin <description>
- the version of your plugin <version>
Example:
1 2 3 4 5 6 7 8 9 10 |
<?xml version="1.0" encoding="ISO-8859-1"?> <Plugin> <className>HelloWorld</className> <name>Hello My World2!</name> <location></location> <subLocation></subLocation> <order>default</order> <description>This is my Hellow World</description> <version>1.1.1.1</version> </Plugin> |
In here I’ve created a Hello World plugin. As you can see location is blanked. There are a few options you can put in there: home, services, devices, clients, statistics. If you put anything else in there, it will be set to default.
Now that config.xml is done, we can start on plugin.php.
plugin.php
This is where you write your actual plugin.
You need to put 1 function in here – get_content($post, $get), and 2 optional function – get_config($id), update_config($value).
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
<? class HelloWorld { //default content private $content = "<h1>HELLO WORLD!</h1>"; //renders the content function get_content($post='', $get='') { //use the property class to get content from db include_once("classes/Property.php"); //name of the property $name = "Plugin_HelloWorld_greetings"; //create the property to get information $property = new Property($name); //if this property gets the information, show the information out if($propertyInfo = $property->get_property($name)) {return "<h1>".addslashes($propertyInfo)."</h1>";} //if not give a default content else {return $this->content;} } //renders the configuration function get_config($id) { // MUST HAVE<input type='hidden' name='id' value=".$id."></input> // the name of the property must follow the conventions Widget_<Classname>_<propertyName> // have the form post and make sure the submit button is named widget_update // make sure there is also a hidden value giving the name of this Class file return "<form id='configForm' method='post'> <input type='hidden' name='class' value='HelloWorld'></input> <input type='hidden' name='id' value=".$id."></input> <select name='Plugin_HelloWorld_greetings'> <option value='Hello World!'>Hello World!</option> <option value='Goodbye World!'>Goodbye World!</option> </select> <input type='submit' class='submitBut' name='plugin_update' value='Update config'/> </form>"; } //updates the configuration, needs to return a true or false value. function update_config($values) { //calls on for the database class include_once("classes/Property.php"); $property = new Property(); //sets the properties to store them, use a switch statement to store different description based on different properties if($property->set_property($values['name'], $values['value'], "Hello World description")) { return true; } else { return false; } } } ?> |
First let’s take a look at get_content.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
//default content private $content = "<h1>HELLO WORLD!</h1>"; //renders the content function get_content($post='', $get='') { //use the property class to get content from db include_once("classes/Property.php"); //name of the property $name = "Plugin_HelloWorld_greetings"; //create the property to get information $property = new Property($name); //if this property gets the information, show the information out if($propertyInfo = $property->get_property($name)) {return "<h1>".addslashes($propertyInfo)."</h1>";} //if not give a default content else {return $this->content;} } |
In here are the post and get methods just incase if the user wants to grab his own post and get values. For this Hello World, we have set the greeting property into the property table in MySQL. Therefore, in the get_content() function, we programmed for it to retrieve the data from the table using our existing property class.
The developer may choose to make his own database for his class, but there is a default if the developer wishes to use it.
Afterwards, the developer must make sure to return some kind of content for the html.
Now let’s take a look at get config.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
//renders the configuration function get_config($id) { // MUST HAVE<input type='hidden' name='id' value=".$id."></input> // the name of the property must follow the conventions Plugin_<Classname>_<propertyName> // have the form post and make sure the submit button is named Plugin_update // make sure there is also a hidden value giving the name of this Class file return "<form id='configForm' method='post'> <input type='hidden' name='class' value='HelloWorld'></input> <input type='hidden' name='id' value=".$id."></input> <select name='Plugin_HelloWorld_greetings'> <option value='Hello World!'>Hello World!</option> <option value='Goodbye World!'>Goodbye World!</option> </select> <input type='submit' class='submitBut' name='plugin_update' value='Update config'/> </form>"; } |
In here is where you make your configuration data. The developer must make sure to have 3 things, the $id as a hidden value so that the application can recognize which plugin is being configured, the naming convention of the value name (Plugin_) so that the application recognizes that this is a plugin configuration, and a plugin_update submit button so that the application can recognize the submission.
In the example, I gave 2 options for the user to choose
Goodbye World!
Once the user submits it will go on to the next function, update_config()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
//updates the configuration, needs to return a true or false value. function update_config($values) { //calls on for the database class include_once("classes/Property.php"); $property = new Property(); //sets the properties to store them, use a switch statement to store different description based on different properties if($property->set_property($values['name'], $values['value'], "Hello World description")) { return true; } else { return false; } } |
Here is the final place where you can take the post values and update it into either the Netharbour’s property database, or your own database. Afterwards, the user can return a true value back or else Netharbour would not recognize it’s update.
And that’s in for the basic build of the plugin. For more advance builds, you can visit later as I write them.