Revision [73]

This is an old revision of Mod012fDeleteCommentsHandler made by JsnX on 2004-03-11 02:07:10.

 

Wikka Mod 012

Type: Feature Addition

Credit:

GenkiCodeguru
and
TobiasHesselmann
http://www.wakkawiki.com/DeletingComments






Deleting Comments


Hi all,

I figured out a way to have an owner delete his own comments off of his pages.

First you have to edit handlers/page/show.php and add the following lines, all old lines commented out, so only add those lines without the comment between those lines in the original show.php.

<?php
//              // display comments themselves
//              if ($comments)
//              {
                        $allowed_to_delete=0;
                        if ($this->UserIsOwner()) $allowed_to_delete=1;
//                     foreach ($comments as $comment)
//                     {
//                              print("<a name=\"".$comment["tag"]."\"></a>\n");
//                              print("<div class=\"comment\">\n");
//                              print($this->Format($comment["body"])."\n");
//                              print("<div class=\"commentinfo\">\n-- ".$this->Format($comment["user"])." (".$comment["time"].")");
                                if ($allowed_to_delete==1 && $comment["owner"]==$this->GetPageOwner())
                                {
                                        ?>
                                        <?php echo $this->FormOpen("delcomment"); ?>
                                                <input type="hidden" name="comment_number" value="<?php echo $comment["id"] ?>">
                                                <input type="submit" value="Delete Comment" accesskey="d" />
                                        <?php echo $this->FormClose(); ?>
                                        <?php
                                }
//                              print("\n</div>\n");
//                              print("</div>\n");
//                      }
//              }

?>


These above lines add a Delete Comment button to each comment if you are the owner of the page.

Next you have to create the file delcomment.php in handlers/page/

<?php

//print("<xmp>"); print_r($_REQUEST); exit;

if ($this->UserIsOwner())
{
        //select comment and delete it
        $comment_number=intval(trim($_POST["comment_number"]));
        $Commenttodel = $this->LoadSingle("select tag, owner from ".$this->config["table_prefix"]."pages where comment_on != '' AND id = '".$comment_number."' order by id desc limit 1");
        if ($Commenttodel["owner"]==$this->GetPageOwner())
        {
                $deleted = $this->LoadSingle("delete from ".$this->config["table_prefix"]."pages where comment_on != '' AND id = '".$comment_number."' limit 1");
                $deleted = $this->LoadSingle("delete from ".$this->config["table_prefix"]."acls where page_tag = '".$Commenttodel["tag"]."' limit 3");
        }
        // redirect to page
        $this->redirect($this->href());
}
else
{
        print("<div class=\"page\"><em>Sorry, you're not allowed to delete comments on this page.</em></div>\n");
}

?>


The above lines actually delete the Comment entrie in prefix_pages and the entries in prefix_acls.
the only drawback is the "hole" the deleted comments leave in the prefix_pages table because of the auto incremented id but as far as I have tested WakkaWiki has now problems with that.

Please tell me what you think about it or maybe you have even a better sollution, fix, improvment for it.

by GenkiCodeguru



Hi, Thanks for this modification, I've requested it to be added to Wakka 0.1.3. I have an improvement that I've made above - because you can only delete your own comments, I've made the "Delete Comments" button only show up for comments the page owner 'owns'. Also this is great integrated into PsudoAdminUser - If you've got that mod installed then change delcomment.php to:

<?php

//print("<xmp>"); print_r($_REQUEST); exit;

if ($this->IsAdmin() || $this->UserIsOwner())
{
        //select comment and delete it
        $comment_number=intval(trim($_POST["comment_number"]));
        $Commenttodel = $this->LoadSingle("select tag, owner from ".$this->config["table_prefix"]."pages where comment_on != '' AND id = '".$comment_number."' order by id desc limit 1");
        if ($Commenttodel["owner"]==$this->GetPageOwner() || $this->IsAdmin())
        {
                $deleted = $this->LoadSingle("delete from ".$this->config["table_prefix"]."pages where comment_on != '' AND id = '".$comment_number."' limit 1");
                $deleted = $this->LoadSingle("delete from ".$this->config["table_prefix"]."acls where page_tag = '".$Commenttodel["tag"]."' limit 3");
        }
        // redirect to page
        $this->redirect($this->href());
}
else
{
        print("<div class=\"page\"><br /><em>Sorry, you're not allowed to delete comments on this page.</em><br /></div>\n");
}

?>


and the show.php line should be:
if ($allowed_to_delete==1 && $comment["owner"]==$this->GetPageOwner() || $this->IsAdmin())


That way, an admin can delete ANY comment on any page.



If you want to allow users delete their own comments from any page, as well as letting the page owner delete any comment on his page (which IMHO makes the most sense), you can use the following code:

My handlers/page/show.php looks like this:

...
<?php
               // display comments themselves
                if ($comments)
                {
                        foreach ($comments as $comment)
                        {
                                print("<a name=\"".$comment["tag"]."\"></a>\n");
                                print("<div class=\"comment\">\n");
                                print($this->Format($comment["body"])."\n");
                                print("<div class=\"commentinfo\">\n-- ".$this->Format($comment["user"])." (".$comment["time"].")\n</div>\n");
                        $cur_user=$this->GetUser();
                        if ($this->UserIsOwner() || ($cur_user['name'] == $comment["owner"]))
                                {
                                        ?>
                                        <?php echo $this->FormOpen("delcomment"); ?>
                                                <input type="hidden" name="comment_number" value="<?php echo $comment["id"] ?>">
                                                <input type="submit" value="Delete Comment" accesskey="d" />
                                        <?php echo $this->FormClose(); ?>
                                        <?php
                                }
                                print("</div>\n");
                        }
                }
?>
...


and my delcomment.php looks like this:

<?php>
if (1==1)
//if ($this->UserIsOwner())
{
        //select comment and delete it
        $comment_number=intval(trim($_POST["comment_number"]));
        $Commenttodel = $this->LoadSingle("select tag, owner from ".$this->config["table_prefix"]."pages where comment_on != '' AND id = '".$comment_number."' order by id desc limit 1");
        $cur_user=$this->GetUser();
        if ($this->UserIsOwner() || ($Commenttodel["owner"]==$cur_user['name']))
        {
                $deleted = $this->LoadSingle("delete from ".$this->config["table_prefix"]."pages where comment_on != '' AND id = '".$comment_number."' limit 1");
                $deleted = $this->LoadSingle("delete from ".$this->config["table_prefix"]."acls where page_tag = '".$Commenttodel["tag"]."' limit 3");
        }
        // redirect to page
        $this->redirect($this->href());
}
else
{
        print("<div class=\"page\"><em>Sorry, you're not allowed to delete this comment!</em></div>\n");
}

?>
...


Also, i think it would look better to substitute the button with a standard link like in "Edit this page" in the footer.

--TobiasHesselmann
There are no comments on this page.
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki