Recent Topics

[Hack][1.8] Plugin Grouping

Started by on Jul 10, 2006 – Contents updated: Jul 10, 2006

Jul 10, 2006 21:20    

Anyway seeing 1.8 just got released, here are the modifications that let you have grouping for your plugins.

Firstly here is a screenshot;
http://blogs.balupton.com/plugins/plugin_grouping_v1.0.0.0.gif

And heres the modifications;

[ File: /blogs/inc/_misc/_plugins.class.php ]

Add the following anywhere inside the Plugins Class:

Code

/**
    * Will return an array that contents are references to plugins that have the same group, regardless of the sub_group.
    */
   function get_Plugins_in_group ( $group ) {
      $result = array();
      
      foreach( $this->sorted_IDs as $plugin_ID )
      {
         $Plugin = & $this->get_by_ID( $plugin_ID );
         if( $Plugin->group == $group )
            $result[] = & $Plugin;            
      }
      
      return $result;      
      
   }
  
   /**
    * Will return an array that contents are references to plugins that have the same group and sub_group.
    */
   function get_Plugins_in_sub_group ( $group, $sub_group = '' ) {
      $result = array();
      
      foreach( $this->sorted_IDs as $plugin_ID )
      {
         $Plugin = & $this->get_by_ID( $plugin_ID );
         if( $Plugin->group == $group && $Plugin->sub_group == $sub_group )
            $result[] = & $Plugin;            
      }
      
      return $result;      
   }
 
 
   /**
    * Callback function to sort plugins by group.
    *
    * WARNING: do NOT sort by anything else than priority unless you're handling a list of NOT-YET-INSTALLED plugins
    */
   function sort_Plugin_group( & $a_ID, & $b_ID )
   {
  
      $a_Plugin = & $this->get_by_ID( $a_ID );
      $b_Plugin = & $this->get_by_ID( $b_ID );
      
      // Compare Group
      $r = strcasecmp( $a_Plugin->group, $b_Plugin->group );
      if( $r != 0 )
         return $r;
      else {
         // Compare Sub Group
         $r = strcasecmp( $a_Plugin->sub_group, $b_Plugin->sub_group );
         if( $r != 0 )
            return $r;
         else {
            // Compare Name
            return strcasecmp( $a_Plugin->name, $b_Plugin->name );
         }
      }
              
   }

Add the following to the switch statement inside the sort() function inside Plugins Class:

Code

case 'group':
        usort( $this->sorted_IDs, array( & $this, 'sort_Plugin_group') );
        break;

So you should end up having something that looks like:

Code

/**
   * Sort the list of {@link $Plugins}.
   *
   * WARNING: do NOT sort by anything else than priority unless you're handling a list of NOT-YET-INSTALLED plugins!
   *
   * @param string Order: 'priority' (default), 'name'
   */
  function sort( $order = 'priority' )
  {
    $this->load_plugins_table();
 
    foreach( $this->sorted_IDs as $plugin_ID )
    { // Instantiate every plugin, so invalid ones do not get unregistered during sorting (crashes PHP, because $sorted_IDs gets changed etc)
      $this->get_by_ID( $plugin_ID );
    }
 
    switch( $order )
    {
      case 'name':
        usort( $this->sorted_IDs, array( & $this, 'sort_Plugin_name') );
        break;
        
      case 'group':
        usort( $this->sorted_IDs, array( & $this, 'sort_Plugin_group') );
        break;
        
      default:
        // Sort array by priority:
        usort( $this->sorted_IDs, array( & $this, 'sort_Plugin_priority') );
    }
  }

[ File: /blogs/inc/_misc/_plugin.class.php ]

Add the following before the constructor "function Panel()":

Code

/**
  * Grouping
  */
  var $group = '';
  var $sub_group = '';

[ File: /blogs/inc/VIEW/_menutop.php ]

Look for the following around line 80:

Code

global $Debuglog;
  $Debuglog->add( 'Admin-Path: '.var_export($this->path, true) );

And replace with:

Code

global $Debuglog;
  $Debuglog->add( 'Admin-Path: '.var_export($this->path, true) );
      
  if( $GLOBALS['ctrl'] == 'plugins' ) {
    ?>
  <style type="text/css">
  table.grouped tr.PluginsGroup,
  table.grouped tr.PluginsSubGroup {
    text-align:left;
  }
  table.grouped tr.PluginsGroup th,
  table.grouped tr.PluginsSubGroup th {
    border-left:none;
  }
  
  table.grouped tr.PluginsSeperator th {
    height:7px;
    background-color: #f5f3ef;
    border-top: 1px solid #9e9286;
  }
  table.grouped tr.PluginsSubGroup th {
    border-bottom: 1px solid #9e9286;
    border-top: 1px solid #9e9286;
  }
  
  table.grouped tr.PluginsSubGroup th {
    background-color: #efede0;
    color:#666666;
  }
  table.grouped tr.PluginsSubGroup th,
  table.grouped tr.PluginsSubGroup td.firstcol {
    padding-left:15px;
  }
  </style>
    <?php
  }

[ File: /blogs/inc/VIEW/settings/_set_plugins.form.php ]

Look for the following around line 80:

Code

<?php
    $apply_rendering_values = $admin_Plugins->get_apply_rendering_values(true); // with descs
 
    $admin_Plugins->restart();   // make sure iterator is at start position
    $count = 0;
    while( $loop_Plugin = & $admin_Plugins->get_next() )
    {
    ?>
    <tr class="<?php echo (($count++ % 2) ? 'odd' : 'even') ?>">

And replace with:

Code

<?php
    $apply_rendering_values = $admin_Plugins->get_apply_rendering_values(true); // with descs
 
    // Sort the plugins by group
    $admin_Plugins->sort('group');
    // Grouping
    $current_group = false; // False so it does the header once
    $current_sub_group = '';
 
    $admin_Plugins->restart();   // make sure iterator is at start position
    $count = 0;
    while( $loop_Plugin = & $admin_Plugins->get_next() )
    {
    
      if( $loop_Plugin->group !== $current_group ) { // Reason why $current_group is false
        $current_group = $loop_Plugin->group;
        $current_sub_group = '';
        $count = 0;
        ?>
        <tr class="PluginsSeperator">
          <th colspan="8"></th>
        </tr>
        <tr class="PluginsGroup">
          <th colspan="8"><?php
          echo T_(
            ( $current_group == '' || $current_group == 'Un-Grouped' ) ? 'Un-Grouped' : $current_group
          ); ?></th>
        </tr>
        <?php
      }
      
      if( $loop_Plugin->sub_group != $current_sub_group ) {
        $current_sub_group = $loop_Plugin->sub_group;
        $count = 0;
        ?>
        <tr class="PluginsSubGroup">
          <th colspan="8"><?php echo T_( $current_sub_group ); ?></th>
        </tr>
        <?php
      }
    
    ?>
    <tr class="<?php echo (($count++ % 2) ? 'odd' : 'even'); if ( $current_sub_group != '' ) echo ' PluginsSubGroup'; else echo ' PluginsGroup'; ?>">

Now look for the following around line 188:

Code

</tr>
    <?php
    }
    ?>
    </tbody>
  </table>
  <p class="center">
    <a href="admin.php?ctrl=plugins&amp;action=reload_plugins"><?php echo T_('Reload events and codes for installed plugins.') ?></a>
  </p>
</fieldset>

And replace with:

Code

</tr>
    <?php
    }
    // Sort the plugins by name again
    $admin_Plugins->sort('name');
    ?>
    </tbody>
  </table>
  <p class="center">
    <a href="admin.php?ctrl=plugins&amp;action=reload_plugins"><?php echo T_('Reload events and codes for installed plugins.') ?></a>
  </p>
</fieldset>

Now find the following around line 210:

Code

if( empty($AvailablePlugins) || ! is_a( $AvailablePlugins, 'Plugins_no_DB' ) ) // may have been instantiated for action='info'
{
  // Discover available plugins:
  $AvailablePlugins = & new Plugins_no_DB(); // do not load registered plugins/events from DB
  $AvailablePlugins->discover();
  $AvailablePlugins->sort('name');
}
?>

And replace with:

Code

if( empty($AvailablePlugins) || ! is_a( $AvailablePlugins, 'Plugins_no_DB' ) ) // may have been instantiated for action='info'
{
  // Discover available plugins:
  $AvailablePlugins = & new Plugins_no_DB(); // do not load registered plugins/events from DB
  $AvailablePlugins->discover();
  // $AvailablePlugins->sort('name'); - Not needed as we sort by group in the next line
}
 
// Sort the plugins by group
$AvailablePlugins->sort('group');
?>

Now find the following around line 238:

Code

<?php
    $AvailablePlugins->restart();   // make sure iterator is at start position
    $count = 0;
    while( $loop_Plugin = & $AvailablePlugins->get_next() )
    {
    ?>
    <tr class="<?php echo (($count++ % 2) ? 'odd' : 'even') ?>">

And replace with:

Code

<?php
    // Grouping
    $current_group = false; // False so it does the header at least once
    $current_sub_group = '';
    //
    $AvailablePlugins->restart();   // make sure iterator is at start position
    $count = 0;
    while( $loop_Plugin = & $AvailablePlugins->get_next() )
    {
    
      if( $loop_Plugin->group !== $current_group ) { // Reason why $current_group is false
        $current_group = $loop_Plugin->group;
        $current_sub_group = '';
        $count = 0;
        ?>
        <tr class="PluginsSeperator">
          <th colspan="5"></th>
        </tr>
        <tr class="PluginsGroup">
          <th colspan="5"><?php
          echo T_(
            ( $current_group == '' || $current_group == 'Un-Grouped' ) ? 'Un-Grouped' : $current_group
          ); ?></th>
        </tr>
        <?php
      }
      
      if( $loop_Plugin->sub_group != $current_sub_group ) {
        $current_sub_group = $loop_Plugin->sub_group;
        $count = 0;
        ?>
        <tr class="PluginsSubGroup">
          <th colspan="5"><?php echo T_( $current_sub_group ); ?></th>
        </tr>
        <?php
      }
    
    ?>
    <tr class="<?php echo (($count++ % 2) ? 'odd' : 'even'); if ( $current_sub_group != '' ) echo ' PluginsSubGroup'; else echo ' PluginsGroup'; ?>">

Then find the following around line 307:

Code

</td>
    </tr>
    <?php
    flush();
    }
    ?>
    </tbody>
  </table>
</fieldset>

And replace with:

Code

</td>
    </tr>
    <?php
    flush();
    }
    // Sort the AvaliablePlugins by Name again
    $AvailablePlugins->sort('name');
    ?>
    </tbody>
  </table>
</fieldset>

To add grouping to your plugins just add the variables; $group, and $sub_group, to your plugin appropriatly.

Enjoy.


Form is loading...

Social CMS software – This forum is powered by b2evolution CMS, a complete engine for your website.