天天看點

Joomla!1.5的plug-in開發劄記(一)

Joomla!1.5中的plug-in是事件驅動的,我看了樓主的文章,發現你還是用的舊模式編寫的。現在的Joomla!都是各種類協調工作的。要實作plug-in必須明白plug-in的分類,目前joomla!的plug-in分為八類:

  • authentication
  • content
  • editors
  • editors-xtd
  • search
  • system
  • user
  • xmlrpc

首先你要明白,你要開發哪個方面的plug-in,這樣才有的放矢。下面以joomla!自帶的user方面的example plug-in為例:example.xml(plug-in配置檔案) <?xml version="1.0" encoding="utf-8"?>

<install version="1.5" type="plugin" group="user">

   <name>User - Example</name>

   <author>Joomla! Project</author>

   <creationDate>November 2005</creationDate>

   <copyright>Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.</copyright>

   <license> http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL</license>

   <authorEmail> [email protected]</authorEmail>

   <authorUrl>www.joomla.org</authorUrl>

   <version>1.0</version>

   <description>An example user synchronisation plugin</description>

   <files>

      <filename plugin="example">example.php</filename>

   </files>

   <params/>

</install> 注意這句<install version="1.5" type="plugin" group="user">,其中type表明安裝包為plug-in

group ="user"表明此plug-in為關于user的.接着注意這句<filename   plugin="example">example.php</filename>,其中 plugin="example"非常重要,這個屬性的值一定要跟包含plugin類的檔案名相同。其他的我就不解釋了。下面看example.php: <?php

// Check to ensure this file is included in Joomla!

defined('_JEXEC') or die();

jimport('joomla.plugin.plugin');

class plgUserExample extends JPlugin {

   function plgUserExample(& $subject, $config)

   {

      parent::__construct($subject, $config);

   }

// Method is called before user data is stored in the database

   function onBeforeStoreUser($user, $isnew)

   {

      global $mainframe;

   }

   //Method is called after user data is stored in the database

   function onAfterStoreUser($user, $isnew, $succes, $msg)

   {

    global $mainframe;

      // convert the user parameters passed to the event

      // to a format the external application

      $args = array();

      $args['username']   = $user['username'];

      $args['email']       = $user['email'];

      $args['fullname']   = $user['name'];

      $args['password']   = $user['password'];

      if ($isnew)

      {

         // Call a function in the external app to create the user

         // ThirdPartyApp::createUser($user['id'], $args);

      }

      else

      {

         // Call a function in the external app to update the user

         // ThirdPartyApp::updateUser($user['id'], $args);

      }

   }

   // Method is called before user data is deleted from the database

   function onBeforeDeleteUser($user)

   {

      global $mainframe;

   }

//Method is called after user data is deleted from the database

   function onAfterDeleteUser($user, $succes, $msg)

   {

      global $mainframe;

       // only the $user['id'] exists and carries valid information

      // Call a function in the external app to delete the user

      // ThirdPartyApp::deleteUser($user['id']);

   }

//This method should handle any login logic and report back to the subject

   function onLoginUser($user, $options)

   {

      // Initialize variables

      $success = false;

      // Here you would do whatever you need for a login routine with the credentials

      //

      // Remember, this is not the authentication routine as that is done separately.

      // The most common use of this routine would be logging the user into a third party

      // application.

      //

      // In this example the boolean variable $success would be set to true

      // if the login routine succeeds

      // ThirdPartyApp::loginUser($user['username'], $user['password']);

      return $success;

   }

   function onLogoutUser($user)

   {

      // Initialize variables

      $success = false;

      // Here you would do whatever you need for a logout routine with the credentials

      //

      // In this example the boolean variable $success would be set to true

      // if the logout routine succeeds

      // ThirdPartyApp::loginUser($user['username'], $user['password']);

      return $success;

   }

}

具體的可以看注釋。因為時間的關系。我下次再分析源檔案。見諒! 

繼續閱讀