Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

Post a new discussion for a user

Hi! We've included Vanilla forum on our site and we're using single-sign-on, so when they log in to our site they're also logged in to the forum.

We have a contact form on the site (outside the forum) and would like to give the option to post to the forum whatever they've entered in the input box, as a new discussion in a certain category. What would be the best way to do this?

We've tried inserting the message directly to the DB, but the new discussions are always shown below all the ones that were manually inserted by users themselves. And we're not sure if there could be other issues with this.

There's also the option of using DiscussionModel, etc. But how do we go about using the model from some other page in the site?

Any guidance is appreciated.

Comments

  • What do you mean with underneath the ones inserted manually? Did you set the date when adding to discussions? That could be the issue

  • Yeah, the date seems to be correct. But, when going to the category, the discussions that were actually inserted by users appear first (ordered from most recent to oldest), and then the ones we had inserted directly to the DB appear after that (also ordered from most recent to oldest). And these don't even show up on Recent Discussions.

    But if you, for example, comment in that discussion, it gets fixed and shows up right at the top, and in the Recent Discussions as well.

    We're just inserting to the GDN_Discussion table, so I thought that maybe some other table needs updating as well. I saw that GDN_Log also gets updated when adding or editing a discussion, so maybe that table is used somehow to order the posts?

  • You explained the issue yourself quite well :+1: if this gets fixed when posting into it, it's clear that some other table is needing an update.

    Find the table you need and your problem will be solved :smile:

    If you can't find it, post here again and I'll take a look tomorrow when I get home from work

  • R_JR_J Admin
    edited October 2017

    What you describe sounds quite strange and I don't think that including any other table would change that behavior.
    At least GDN_Category and GDN_User are also touched when writing a discussion to the database. I bet that there is some malformed data in your discussion table.

    Normally you should write a plugin for something like that. But if you don't like to do that, you should be able to use a simple curl request: https://open.vanillaforums.com/discussion/comment/240977/#Comment_240977

  • I had a second thought about it. I would save contact Form info into a table and call a custom vanilla plugin.
    That plugins job would be to scan that contact form table and create a discussion for each entry with no discussion id. Plugin should return discussion id so that the table can be updated

  • KryptixKryptix
    edited October 2017

    @R_J zei:
    I had a second thought about it. I would save contact Form info into a table and call a custom vanilla plugin.
    That plugins job would be to scan that contact form table and create a discussion for each entry with no discussion id. Plugin should return discussion id so that the table can be updated

    Isn't the discussion table using auto-increment for discussion id's? Wouldn't writing a plugin give the same result, since it'll probably just write to the database aswell.

  • R_JR_J Admin
    edited October 2017

    I wrote that down from my mobile, so I wasn't very expressive. I meant to use the DiscussionModel to avoid any problems that may arise when you trick around and avoid using Vanilla when it would be appropriate. Saving a discussion does not only include some more tables, it also should fire some Events so that additionally provided functionality by other plugins can work.

    What I was talking about was a) changing the way the contact form acts and b) write a Vanilla plugin.

    Contact Form

    The contact form should be extended to add the content to a db table that acts like a queue. Following columns would be needed:
    1) Content: the text of the contact form
    2) UserID: the user who is using the contact form

    As I've said, that table should act as a queue. For a finished task you could either delete the row or mark it as finished. If you prefer having a flag instead of deleting something (I would prefer that), you would need the following column:

    3) DiscussionID: null by default, getting updated with the Vanillas DiscussionID after the discussion has been created. Serves as a flag for the state of this task and as a reference

    Vanilla has a column ForeignTable and ForeignID so it would be a nice-to-have if there is a lso the following column in the table:
    4) ID: the PrimaryKey which can be used in Discussion.ForeignID as a back reference.

    After saving that info to the db, the contact form must make a request to the Vanilla plugin in a form like that: example.com/forum/plugin/processMyContactForm

    Plugin

    The Vanilla plugin must create that endpoint and what is going on behind the scenes would be

    $contactFormContents = SELECT * FROM ContactFormTable WHERE DiscussionID IS NULL
    foreach $contactFormContents as $contact
    $discussionID = $discussionModel->save($contact)
    UPDATE ContactFormTable SET DiscussionID = $discussionID WHERE ID = $contact['ID']

  • Thank you both for your help!

    Last night I tried something that I think finally got working. I basically used the info in these two posts:
    1. https://open.vanillaforums.com/discussion/32757/create-vanilla-forum-discussions-programmatically
    2. https://open.vanillaforums.com/discussion/25199/using-vanilla-session-outside-of-vanilla-external-session

    So in a single script I just manually start the vanilla session and then use DiscussionModel to add the new discussion. And it seems to be working ok!

    Before, I had tried using DiscussionModel from a plugin but wasn't able to get it to work completely. How do you set permissions for a plugin so any user can call into a url like the one you suggested (example.com/forum/plugin/processMyContactForm) ?

  • The important thing is, that there is no information passed to that plugin so noone can abuse that endpoint.

    In a plugin you simply add that method:

    public function pluginController_processMyContactForm_create($sender) {
        // If you want to exclude guests, uncomment the following line.
        // $sender->permission('Garden.SignIn.Allow');
    
        // decho's output is only visible for admins.
        decho($sender->RequestArgs);
    
        echo 'Nothing to see';
    }
    
  • Great, thanks!

  • I ran into another interesting problem. Everything seems to be working fine, except that the links of the newly created discussions that are sent in notification emails are not correct.

    That is, if a discussion is added the way I specified above, I receive a notification email as always, but the url of the discussion that is shown in the email is missing some directories. So instead of being http://www.mysite.com/dir/forum/discussion/123 it's just http://www.mysite.com/discussion/123, so it's missing "/dir/forum" form its path.

    The script that starts the vanilla session and inserts the new discussion is located at http://www.mysite.com/dir/myscript.php.

    Not sure what needs to be set to show the right url in the email.

  • You've said you are using parts of the forum classes. But the forum is based on a framework. In order to be sure that everything is handled the right way, you would have to load the complete framework in your application, otherwise you can never be sure that everything will work as expected.
    Loading one framework from inside another is a bad idea.

    You should rethink the option to put the Vanilla task in a Vanilla plugin and do the contact form task in your contact form. That way you at least wouldn't get unwanted results.

    If you need advice for the Vanilla plugin, just ask!

  • Ok, I'll give the plugin option another try. Thanks again!

  • There's an existing example of creating a discussion from a plugin. See feeddiscussion (github) and look for the word "minion". You'll see that under certain conditions a discussion is created as if it was created by another user. The plugin uses the discussion model which is the right way to do it.

  • I know this is quite off topic but i am new here and haven't found an "introduction thread". Do we have one of those here?

  • Totally off topic, yes. No, there is no such discussion. But you can start one if you like :wink:

  • @R_J said:
    Totally off topic, yes. No, there is no such discussion. But you can start one if you like :wink:

    Haha. Okay. I just needed a go ahead from one of the bosses B)

Sign In or Register to comment.