Jump to content
View in the app

A better way to browse. Learn more.

Дизайн и модификация Invision Community

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Создать новую тему в форуме API

Собственно не получается, используя API форума, создать новую тему в отдельной ветке форума. Создаю следующим кодом:

require_once( 'forums/initdata.php' );
 require_once( IPS_ROOT_PATH . 'sources/base/ipsRegistry.php' );

 $registry = ipsRegistry::instance();
 $registry->init();

 $forumID = '2';
 $memberID = '109';

 require_once( IPSLib::getAppDir( 'forums' ) . '/sources/classes/post/classPost.php' );

 $postClass = new classPost( $registry );
 $postClass->setForumID( $forumID );
 $postClass->setTopicTitle( "My Topic" );
 $postClass->setPostContent( "Hello, I am post content!" );
 $postClass->setAuthor( $memberID );

 try
 {
     $postClass->addTopic();
 }
 catch( Exception $error )
 {
     print $error->getMessage();
 }

В чём проблема? Ошибок код не выдаёт, но и тема на форуме не создаётся. IPB 3.2.3

Featured Replies

До боли знакомый кусок кода. Где взяли? :)

 

Нашел тут на форуме свой старый пример, он работает?

 

<?php

define('IPS_ENFORCE_ACCESS', true);
require_once( 'initdata.php' );
require_once( IPS_ROOT_PATH . 'sources/base/ipsRegistry.php' );
require_once( IPS_ROOT_PATH . 'sources/base/ipsController.php' );

$registry = ipsRegistry::instance();
$registry->init();

$classToLoad = IPSLib::loadLibrary( IPSLib::getAppDir( 'forums' ) . '/sources/classes/post/classPost.php', 'classPost', 'forums' );
$post = new $classToLoad( $registry );

$post->setForumID( 1 ); 
$post->setAuthor( 1 );
$post->setPostContent( "[i]Hello[/i] [b]there![/b]" );
$post->setTopicTitle('Hi!');

if($post->addTopic()) {
   echo 'Тема успешно создана';
} else {
   echo 'Ошибка: '.$post->_postErrors;
}

?>

 

Если нет, то какую ошибку выводит? Что-то вполне могло поменяться с тех пор.

Помню тоже сталкивался с такой проблемы, что темы не хотели создаваться. вот мой - вроде работал.

 

$post->setForumID( $forum_id );
$post->setForumData( $registry->class_forums->forum_by_id[ $forum_id ] );
$post->setAuthor( $author_id );
$post->setPostContent( "[i]Hello[/i] [b]there![/b]" );
$post->setTopicTitle( 'Hi!' );
$post->setPublished( true );
$post->setSettings( array(
   'enableSignature' => 1,
   'enableEmoticons' => 1,
   'post_htmlstatus' => 0,
   'enableTracker'   => 0,
) );

if( ! $post->addTopic() )
{
die( 'Error addTopic: '.$post->_postErrors );
}

$topic_data = $post->getTopicData();

  • Author

Проблему решил! Использую этот класс:

<?php
class AddTopic {
   /**
    * @var classPost
    */
   private $post;

   function __construct() {
       if (!defined('IPB3_ROOT')) {
           define('IPB3_ROOT', 'forums');
       }

       define('IPB_THIS_SCRIPT', 'public');
       define('IPS_PUBLIC_SCRIPT', 'index.php');
       define('IPS_ENFORCE_ACCESS', true);

       require_once IPB3_ROOT.'/initdata.php';
       require_once IPS_ROOT_PATH.'sources/base/ipsRegistry.php';
       require_once IPS_ROOT_PATH.'sources/base/ipsController.php';

       ipsRegistry::init();

       // Нет смысла каждый раз заново создавать
       require_once IPSLib::getAppDir('forums').'/sources/classes/post/classPost.php';

       $this->post = new classPost(ipsRegistry::instance());
   }

   public function createTopic($memberId=1, $forumId=2, $topicTitle='Новая тема',
           $postContent='Тело сообщения', $timestamp=null) {
       if (!$timestamp) {
           $timestamp = time();
       }

       if (!isset(ipsRegistry::getClass('class_forums')->forum_by_id[$forumId])) {
           return false;
       }

       // Настраиваем
       $this->post->_timestamp=$timestamp;
       $this->post->setIsPreview(false);
       $this->post->setForumData(ipsRegistry::getClass('class_forums')->forum_by_id[$forumId]);
       $this->post->setForumID($forumId);
       $this->post->setAuthor($memberId);
       $this->post->setPublished(true);
       $this->post->setSettings(array(
           'enableSignature' => 1,
           'enableEmoticons' => 1,
           'post_htmlstatus' => 1,
           'enableTracker'   => 0,
           ));

       $this->post->setTopicTitle($topicTitle);

       // Проблема в том, что контент кешируется при первом вызове
       // чтобы работало - его нужно сбросить
       $this->post->setPostContentPreFormatted(null);
       $this->post->setPostContent($postContent);

       // Создаем тему
       try {
           if ($this->post->addTopic() === false) {
               // А тут все наши ошибки...
               // $this->post->_postErrors;

               echo 'Тема не может быть создана<br />';
               return false;
           }
       } catch(Exception $exc) {
           echo 'Ошибка при создании темы: '.$exc->getMessage().'<br />';
           return false;
       }

       // Получаем свойства новой темы и её первого сообщения
       return array(
           $this->post->getTopicData(),
           $this->post->getPostData()
       );
   }
}

?>

 

$topic = new AddTopic();
$Result = $topic->createTopic(1, 1, 'Название темы', 'Контент');
print_r($Result);

Опять тот же код, но в обертке класса. Ну ок, для коллекции отлично будет :)

Create an account or sign in to comment

Recently Browsing 0

  • No registered users viewing this page.

Account

Navigation

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.