Joomla!1.5的plug-in开发札记(一)

来源:互联网 发布:画设计图的软件手机 编辑:程序博客网 时间:2024/05/22 13:56
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>admin@joomla.org</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;

   }
   /**

    * This method should handle any logout logic and report back to the subject

    *

    * @access public

    * @param array holds the user data

    * @return boolean True on success

    * @since 1.5

    */

   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;
   }

}
具体的可以看注释。因为时间的关系。我下次再分析源文件。见谅! 
原创粉丝点击