我的目标是确保当用户注册时,将在jos_content表中自动创建一篇文章,并且它将填充从插件管理器上的参数中选择的标题,类别,部分和文本文本。
我的代码,如下所示,注册用户,但不更新文章表:
XML文件
<?xml version="1.0" encoding="iso-8859-1"?>
<install version="1.5" type="plugin" group="user">
<name>Project2</name>
<author>Alexandros</author>
<creationDate>August 2011</creationDate>
<copyright>(C) 2011 Open Source Matters. All rights reserved.</copyright>
<license>GNU/GPL</license>
<authorEmail>admin@joomla.org</authorEmail>
<authorUrl>www.joomla.org</authorUrl>
<version>1.1</version>
<description>Adds an article when register a user</description>
<files>
<filename plugin="adduser">adduser.php</filename>
</files>
<params>
<param name="sectionid" type="text" default="1" label="article section" description="Section of article"/>
<param name="catid" type="text" default="1" label="article category" description="Category of article"/>
<param name="introtext" type="text" default="" label="article text" description="Text of article"/>
</params>
</install>
插件php
<?php
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
// Import library dependencies
jimport('joomla.plugin.plugin');
class plgUserAdduser extends JPlugin
{
/**
* Constructor
*
* For php4 compatability we must not use the __constructor as a constructor for
* plugins because func_get_args ( void ) returns a copy of all passed arguments
* NOT references. This causes problems with cross-referencing necessary for the
* observer design pattern.
*/
function plgUserAdduser( &$subject )
{
parent::__construct( $subject );
// load plugin parameters
$this->_plugin = JPluginHelper::getPlugin( 'User', 'Adduser' );
$this->_params = new JParameter( $this->_plugin->params );
$sectionid = $params->get('sectionid');
$catid = $params->get('catid');
$introtext = $params->get('introtext');
}
/**
* Plugin method with the same name as the event will be called automatically.
*/
function onAfterStoreUser($user, $isnew, $success, $msg)
{
$app = &JFactory::getApplication();
// Plugin code goes here.
if ($isnew)
{
$database =& JFactory::getDBO();
$query = "Insert INTO #__content(title,introtext,sectionid,catid) VALUES (" . $user['id'] . ",'$introtext','$sectionid','$catid')";
$database->setQuery($query);
$result = $database->query();
if (!$result)
print("<font color=\"red\">SQL error: " . $database->stderr(true) . "</font><br />");
}
// return true;
}
}
?>
我做错了什么?
我认为文章ID可能有问题,因为它是jos_content表中的主键,但我不知道如何正确地自动填写这个。
替代代码:
我尝试了这段代码,但它不起作用(更糟糕的是,即使用户注册,我甚至无法获取入队消息):
<?php
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
// Import library dependencies
jimport('joomla.plugin.plugin');
class plgUserAdduser extends JPlugin
{
/**
* Constructor
*
* For php4 compatability we must not use the __constructor as a constructor for
* plugins because func_get_args ( void ) returns a copy of all passed arguments
* NOT references. This causes problems with cross-referencing necessary for the
* observer design pattern.
*/
function plgUserAdduser( &$subject )
{
parent::__construct( $subject );
// load plugin parameters
$this->_plugin = JPluginHelper::getPlugin( 'User', 'Adduser' );
$this->_params = new JParameter( $this->_plugin->params );
$sectionid = $params->get('sectionid');
$catid = $params->get('catid');
$introtext = $params->get('introtext');
$title = $params->get('articletitle');
}
/**
* Plugin method with the same name as the event will be called automatically.
*/
function onAfterStoreUser($user, $isnew, $success, $msg)
{
// Plugin code goes here.
if ($isnew)
{
JFactory::getApplication()->enqueueMessage( 'New User' );
$database =& JFactory::getDBO();
$data = new stdClass;
$data->id = LAST_INSERT_ID();
$data->title= '$title' ;
$data->introtext= '$introtext';
$data->sectionid= '$sectionid' ;
$data->catid='$catid';
if(!$database->insertObject( '#__content', $data, NULL ));
{
JFactory::getApplication()->enqueueMessage( 'Error' ); return;
}
}
// return true;
}
}
?>