Not signed in (Sign In)

Categories

Vanilla 1.1.4 is a product of Lussumo. More Information: Documentation, Community Support.

Help keep Vanilla free:
Welcome Guest!
Want to take part in these discussions? If you have an account, sign in now.
If you don't have an account, apply for one now.
    • CommentAuthorlyle
    • CommentTimeDec 13th 2007 edited
     # 1
    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?

    Thanks!
    • CommentAuthorlyle
    • CommentTimeDec 14th 2007
     # 2
    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";

    ?>
Add your comments
    Username Password
  • Format comments as