Plugin Tutorials 201 - Plugin Aministration

From e107 Wiki
Jump to: navigation, search

e107 Wiki: English | Русский | Deutsch | Français | Magyar | Português | Български | Česky | Nederlands | Ελληνικά | Italiano | Norske | Polska | Slovenščina | Español | Svenska | Translate: Wiki | Page


Current Release 1.0.4 | | Download | Changelog | Forum | Plugins | Themes | RSS Feed of Latest Changes | Atom Feed of Latest Changes

How to write a plugin
Main page > I : First Aid > II : Development > III : Administration > IV : How To's > V : Plugin Writing > VI : Theming & Shortcodes

Tutorial 201 - plugin adminstration

In this part of the tutorial we are going to extend the basic plugin so that we can modify it via the admin tool. We will be able to change all the features; the title, the link and the picture via a new web page in the admin tool.

We are going to store the new values in the database. A cms system relies on the database and using it is easy.

What it will look like

Code: Admin screen for promo
               Data Entry
    promo_title   ________________________
    promo_link    ________________________
    promo_picture ________________________
 This page will update the values for the promo in the database. 
 The picture will be copied into the  promo/images directory.

The code for the data entry page

This is a simple html form. It gathers the new values and passes them to another program that puts them in the database.

This data entry screen is produced by promo_admin.php.

Code: promo_admin.php
 <!--File: promo_admin.php-->
 <!-- this program shows on the admin menu in the plugin area.
      It accepts the input for the promo and calls promo_add.php
      to insert the data into the database.-->
   <body leftmargin="200" rightmargin="200"> 
    <h2>Data Entry</h2>
    <form enctype="multipart/form-data" method="post" action="promo_add.php">
       promo_title <input type="Text" name="promo_title" size="25"><p>
       promo_link  <input type="Text" name="promo_link"  size="25"><p>
       promo_picture <input type="File" name="picture" size="25"><p>
       <!-- promo_picture is the name of the file in the temp directory
       promo_picture_name is the original file name -->
       <input type="submit" name="submit" value="Save">
  This page will update the values for the promo in the database.
  The picture will be copied into the promo/images directory.

Hooking promo_admin.php

To hook promo_admin.php into the admin tool we need to make one change to plugin.php. Change this line;

// Name of the admin configuration file
$eplug_conffile = "promo_admin.php";

Adding Icons

If you want icons for the admin tool also add the following to plugin.php and put the necessary icons in the images directory; e_107_plugins/promo/images

Code: plugin.php
 // Icon image and caption text 
 $eplug_icon = $eplug_folder."/images/promo_32.png";
 $eplug_icon_small = $eplug_folder."/images/promo_16.png";
 $eplug_caption = "Configure promo";

The image does not actually have to be 16 or 32 pixels wide. Any size will do but it will be displayed in that size. Shrinking it yourself will be faster and save space but really... how often is the admin page used. I don't think it is enough to bother with the exercise. You are provided with the file names to use should proper sizing be important to you.


Even though we have not yet written promo_add.php that doesn't mean we can't test.

Uninstall promo and then install it again. At the bottom of the admin tool, near the plugin manager, you should see a new plug icon labeled "promo". Click it and you should see the data entry page.

Test the browse button to get the picture filename. When you click save, you will get a "not found" error... that's ok.

Database Stuff

Modify plugin.php so that it can create the table we need.
Add the following to plugin.php near the bottom

// List of sql requests to create tables 
$eplug_tables = array("
  ID integer UNSIGNED NOT NULL auto_increment,
  title varchar(255),  
  link varchar(255),
  picture varchar(255),

Uninstall promo and install it again. I like the use of "p_" to prefix plugin tables.


Create a new file called promo_add.php containing the following code;

Code: promo_add.php
  //File: promo_add.php
  // this program inserts the passed data 
  // into the database and shows it to the 
  // user for confirmation... 
  // There is no edit or delete yet.


   echo '<body ';
   echo   'leftmargin="200" ';
   echo   'topmargin="0" ';
   echo   'rightmargin="200" ';
   echo   'bottommargin="0">';
   echo '<Center>';
   echo "<H1>promo</H1>"; //watch apostrope's
   echo '<h2>Data Saved</h2>';
   echo '</Center>';

   // browser put the image in the temp directory...
   // move it to our image directory
   copy( $promo_picture_temp, $t_file_path.$promo_picture_name); 

   // --------Update the database.-------------
       //****put your db values here****
   $db = mysql_connect("$dbserver","$dbusername","$dbpassword");
   $currDb = mysql_selectdb($dbname) or die (mysql_error()); 

   $sqlstring="Select * From $tblname WHERE id=1";
   $result=mysql_query($sqlstring) or die(mysql_error());
   $num_rows = mysql_num_rows($result);

      $sqlstring = "INSERT INTO ".$tblname." (id,title, link, picture) 
          VALUES (1,'".$promo_title."', '".$promo_link."', '".$t_file_path.$promo_picture_name."')";
      $sqlstring = "UPDATE ".$tblname." SET ".
                   "title='".$promo_title."', ".
                   "link='".$promo_link."', ".
                   "picture='".$t_file_path.$promo_picture_name."' ".
                   "WHERE id=1";
   //echo "<br>string: ".$sqlstring;
   $result=mysql_query($sqlstring) or die(mysql_error());

   //--------now show them what they did--------
   echo "<h3>".$promo_title."</h3>";
   echo "<img align='Left' src=".$t_file_path.$picture_name."><br><br>\n";
   // let the user get out of here
   echo '<A HREF="promo_admin.php"> Change Data </A>';
   echo '    ';
   echo '<a href="/e107_admin/admin.php">Admin Page</a>';

Hmmm. The lack of integraton with e107 is starting to show... We should be asking for the dbpassword stored in the e107 configuration file and not coding it in the file as we do here. Let's keep pluging anyway.


It would be wrong to continue without mentioning phpMyAdmin. It provides a web interface to the mysql database.

At this point you should check the database to see if the data was entered correctly.
As mentioned you can do this via phpmyadmin or you can check via opening a mysql prompt and doing a sql query
select * from p_promo;
If you don't know either method just continue the tutorial.


And we need to modify promo_menu.php to use the values in the database;

Code: promo_menu.php
   // database stuff
   $dbusername=''; //put your values here.
   $db = mysql_connect("$dbserver","$dbusername","$dbpassword");
   $currDb = mysql_selectdb($dbname) or die (mysql_error()); 
   $sqlstring="Select title, link, picture From $tblname WHERE id=1";
   $result=mysql_query($sqlstring) or die(mysql_error());
   $row = mysql_fetch_array($result);
   $text  = "<a href=".$promo_link.">\n";
   $text .= "<img align='Left' width=175 src=/e107_plugins/promo/".$promo_picture.">";
   $text .= "</a>\n";

Above the space in the code we added database code.
Below the space is mostly the old promo_menu.php except the $promo_ values are now obtained from the database. $text and $ns are the same.

No install necessary

The good news is that no install is necessary. e107 already knows about promo_menu.php and it is read from the directory. Just refresh your browser by pressing F5 or the refresh icon.


Now you have a plugin that can be controlled from the admin tool, stores its values in the database, can be moved to any menu area via the menus tool.

Although it is controlled by e107 it is not that well behaved. It should use a language file to isolate language from code. This allows for easy translation. It should get the database server, user and password by including class2.php.