My goal is to ensure that when a user registers, an article will automatically be created in the jos_content table, and that it will be filled with the title, category, section, and text text chosen from parameters on the Plugin Manager.
My code, as shown below, registers the user, but does not update the table of articles:
XML FILE
<?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>
Plugin 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;
}
}
?>
What am I doing wrong?
I believe there could be something wrong with the article ID as it is the primary key in the jos_content table, but I don't know how to automatically fill in this one correctly.
Alternate Code:
I tried this code but it doesn't work (and worse is that I can't even get the enqueue messages even when the user is registered):
<?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;
}
}
?>