MyBB Hacks

Full Version: Bulk loading threads with thread fields
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm working on a plugin for bulk loading data into a forum that allows XThreads.

So far, I've got the package to read a CSV file and upload the threads into the forum with the thread subject and body. This was done by the following code, which invokes the posthandler for ordinary threads, $posthandler->validate_thread:

$new_thread = array(
"fid" => 21,
"subject" => $finalSubject,
"icon" => ' ',
"uid" => '1',
"username" => $mybb->user['username'], /* Screened for Super Admin */
"message" => $finalBody,
"ipaddress" => '127.0.0.1',
"posthash" => ''
);

$posthandler->set_data($new_thread);
$valid_thread = $posthandler->validate_thread();

My question is:  Is there a corresponding posthandler->validate_thread for XThreads?   I was unable to find one.

How do I modify the above code so that an XThread with thread field data is created instead of an ordinary thread?
Should "xthreads_input_posthandler_insert" be called instead?
XThreads runs off the MyBB posthandler validation, so you don't need to do anything special there.

You probably do have to set your XThreads values into the $mybb->input array though because there's not really an easy way for a plugin to modify the array that gets sent into the posthandler.

It would probably look something like:

PHP Code:
 $new_thread = array(
"fid" => 21,
"subject" => $finalSubject,
"icon" => ' ',
"uid" => '1',
"username" => $mybb->user['username'], /* Screened for Super Admin */
"message" => $finalBody,
"ipaddress" => '127.0.0.1',
"posthash" => ''
);

is_array($mybb->input) or $mybb->input = array();
$mybb->input['xthreads_myfield'] = 'value'; // sets value for the custom thread field named "myfield"

$posthandler->set_data($new_thread);
$valid_thread = $posthandler->validate_thread();

> $mybb->input['xthreads_myfield'] = 'value'; // sets value for the custom thread field named "myfield"

Is there any way I can loop through all xthreads_myfields and set the value in this array? In other words, can I use xthreads metadata to set up a generic loop to set values for each custom thread field?  Something like

for (i=0 ; i<= xthreads_numfields; i++)
    $mybb->input[xthreads_myfield[i]] = 'value_read_from_csv_file [i]'
  
Otherwise, the plug in will require extensive modifications by each user of the plugin.
(07-25-2013 08:37 PM)Anil Gidwani Wrote: [ -> ]Otherwise, the plug in will require extensive modifications by each user of the plugin.
I think this will be required anyway since you don't exactly know what fields a user has.

You probably should be using the CSV header for determining field names, as there's no guarantee what order you'll receive the fields from both the CSV or the database.

But if you're still adamant on your solution, you can read the threadfields cache (see the MyBB cache object) for a list of thread fields.  Note that this contains all thread fields across all forums.
Reference URL's