<< Back to main page

Adding support for Add-ons or Plugins

This is a question I received in the comments section for PHP Setup Wizard:
Hi,
Very nice script, cod be very useful. From what I have seen in the demo, it can do the simple tasks as creating databases and importing and checking etc. Is it possible to copy files from ex. a resources folder. Based on the selection in one of the steps. Let’s say the person installing weed like to install a statistics page from a add-on list, and dose coping the files for the statistics page to the add-ons directory?

Sure, you can easily add support for addons with a custom step. I have written a simple example on how you can implement a custom step that supports installation and removal of addons or plugins.

First step

You need to define new constant in configuration.php file :

define('STEP_COPYADDON', 'copyaddon');

And then you need to give this constant a place in the setup process and give it some basic settings. I added this step totally last, right before the STEP_WRITECONFIG settings.

STEP_COPYADDON => array(
   'title' => 'Add-on Selection',
   'enabled' => true,
   ),

Now, the step will need some custom functions that are related to the plugin process. We need a function that can give us total list of available add-ons and list of installed add-ons (which have been copied).

We do not want to make the step too complicated so we create new helper file and we name it helper.addons.php and put it in the assets folder of the installer

This new helper could have functions like these, and return these arrays:

function GetAvailableAddonsList_YourCustomMethod()
{
   // GET ALL THE ADDONS! This list contains ALL available addons 
   // in the source folder where all addons are kept 
   return array( 
      'aaa' => array('name'=>'A-addon', 'file'=>'addon.AAA.php'), 
      'bbb' => array('name'=>'B-addon', 'file'=>'addon.BBB.php'),
      'ccc' => array('name'=>'C-addon', 'file'=>'addon.CCC.php'), 
      'ddd' => array('name'=>'D-addon', 'file'=>'addon.DDD.php')); 
}
function GetInstalledAddonsList_YourCustomMethod()
{
   // GET ONLY INSTALLED ADDONS! This list contains ONLY the addons
   // that have been copied to some folder for the system being installed 
   return array(
      'ccc' => array('name'=>'C-addon', 'file'=>'addon.CCC.php'), 
      'ddd' => array('name'=>'D-addon', 'file'=>'addon.DDD.php') 
      );
}

Now we have done all the prep-work for adding this custom step, the next thing to do is writing some code to installer.php

Put the following code before the comment
/* ==[ CREATE FINAL-OUTPUT CONFIGURATION FILE ]== */

...

/* ==================[ INTALL/REMOVE  ADDONS ]================== */

// Only show this step if enabled and currently selected!
if($steps[STEP_COPYADDON]['enabled'] && $step == STEP_COPYADDON)
{
   $page->MainTitle($steps[STEP_COPYADDON]['title'], 'custom');

   // You will need some custom functions that check addons available and
   // addons that have been copied - create new "helper" and put it in "assets"
   require('assets'.DIRECTORY_SEPARATOR.'helper.addons.php');

   // Get all available addons, and addons that are in the addons folder
   // already - this is to enable the user to copy more than one addon
   $availableAddons = GetAvailableAddonsList_YourCustomMethod();
   $installedAddons = GetInstalledAddonsList_YourCustomMethod();

   // Set these paths to where the addons are located (this could also go
   // in the configuration.php, but it is up to you)
   $availPath = " *the folder containing AVAILABLE addons* ";
   $installPath = " *the folder containing INSTALLED addons* ";

   // Request has been made to copy some available addon, 
   // and it has not been copied before - "install it"!
   if(isset($_REQUEST['installaddon']) &&
      isset($availableAddons[$_REQUEST['installaddon']]) && 
      !isset($installedAddons[$_REQUEST['installaddon']]))
   {               
      $addonFile = $availableAddons[$_REQUEST['installaddon']]['file'];
      $addonName = $availableAddons[$_REQUEST['installaddon']]['name'];
      $slash = DIRECTORY_SEPARATOR;

      if(copy($availPath.$slash.$addonFile, $installPath.$slash.$addonFile))
          $page->SuccessBox('The addon '.$addonName.' was installed successfully');
      else $page->ErrorBox('Unable to install '.$addonName.' addon');
   }

   // Request has been made to remove some addon, and it exists BOTH
   // in available addons and installed addons - "delete from installed"!
   if(isset($_REQUEST['removeaddon']) &&
      isset($availableAddons[$_REQUEST['removeaddon']]) && 
      isset($installedAddons[$_REQUEST['removeaddon']]))
   {               
      $addonFile = $availableAddons[$_REQUEST['removeaddon']]['file'];
      $addonName = $availableAddons[$_REQUEST['removeaddon']]['name'];

      if(unlink($installPath.DIRECTORY_SEPARATOR.$addonFile))
          $page->SuccessBox('The addon '.$addonName.' has been removed successfully');
      else $page->ErrorBox('Unable to remove '.$addonName.' addon');
   }

   // Show AVAILABLE addons for install ONLY
   $page->Paragraph('List of available addons to install:');
   $page->FormStart();
   $count = 0;
   foreach($availableAddons as $addonkey=>$info)
   {
      // Do not show already installed addons here, 
      // you do not want to "re-install" anything
      if(!isset($installedAddons[$addonkey]))
      {
         $count++;
         $page->FormRadiobox($addonkey, 'installaddon', $info['name']);
      }
   }

   // If there are addons to install, include submit button for the form
   // othervice just show a quotation saying "nothing is available"
   if($count > 0)
       $page->FormSubmit('Install Addon');
   else $page->Quotation('All addons have been installed');
   $page->FormClose();

   // Show INSTALLED addons ONLY
   if(count($installedAddons) > 0)
   {
      $page->Paragraph('List of installed addons you can remove:');
      $page->FormStart();
      foreach($installedAddons as $addonkey=>$info)
      {
         $page->FormRadiobox($addonkey, 'removeaddon', $info['name']);
      }
      $page->FormSubmit('Remove selected');
      $page->FormClose();
   }

   // Offer the user to continue to next step
   $page->Paragraph('If you are done installing addons, continue to the next step');
   $page->FormStart(array('step'=>GetNextStep(STEP_COPYADDON)));
   $page->FormButton('Back', array('step'=>GetPrevStep(STEP_COPYADDON)));   
   $page->FormSubmit('Next');
   $page->FormClose();

   // Show addons page and die
   $page->ShowPage(STEP_COPYADDON);
}

/* ==================[ CREATE FINAL-OUTPUT CONFIGURATION FILE ]================== */

...
And that's all folks! ;)