Vanilla 1 is no longer supported or maintained. If you need a copy, you can get it here.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

Programmatically Adding a Comment

lylelyle New
edited December 2007 in Vanilla 1.0 Help
Apologies if this is answered somewhere already but I've been searching and only got more confused.

I'd like to be able to add a comment to a certain discussion from a script on my server and I'm wondering what the best way to accomplish that would be. I'm guessing it might be more complicated that just inserting a row into the comments table. Can I write a php script that uses the vanilla framework to do this easily? What would that look like?



  • Options
    I think I've got it figured out using CommentManager. Please let me know if there's anything I've left out that should be included.

    <?php include("appg/settings.php"); include("appg/init_vanilla.php"); $Context->Session->UserID = 4; $DiscussionID = 312; // Load the selected user $UserManager = $Context->ObjectFactory->NewContextObject($Context, "UserManager"); $Context->Session->User = $UserManager->GetUserByID($Context->Session->UserID); // Ensure the user is allowed to view this page $Context->Session->Check($Context); // Create the discussion and comment objects $NewComment = $Context->ObjectFactory->NewContextObject($Context, 'Comment'); $Discussion = $Context->ObjectFactory->NewContextObject($Context, 'Discussion'); $cm = $Context->ObjectFactory->NewContextObject($Context, 'CommentManager'); $dm = $Context->ObjectFactory->NewContextObject($Context, 'DiscussionManager'); // Set up the new comment $NewComment->Clear(); $NewComment->DiscussionID = $DiscussionID; $NewComment->Body = time(); $NewComment->UserCommentCount = $Context->Session->User->CountComments; // Get the selected discussion $Discussion = $dm->GetDiscussionById($NewComment->DiscussionID); if ($Discussion->Closed && !$Context->Session->User->Permission('PERMISSION_CLOSE_DISCUSSIONS')) { $ResultComment = false; $Context->WarningCollector->Add($Context->GetDefinition('ErrPermissionInsufficient')); } else { $ResultComment = $cm->SaveComment($NewComment); } if ($ResultComment) { echo "Comment added successfully\n"; } else echo "Failed to save comment.\n"; ?>
This discussion has been closed.