Joomla: `onAfterStoreUser` ничего не делает - PullRequest
0 голосов
/ 31 августа 2011

Моя цель - обеспечить, чтобы при регистрации пользователя статья автоматически создавалась в таблице 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;
 }
}
?>

Что я делаю не так?

Я полагаю, что с идентификатором статьи может быть что-то не так, поскольку он является первичным ключом в таблице 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;
 }
}
?>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...