Skip to main content

Forums

This topic contains 25 replies, has 4 voices, and was last updated by  chrisburton 4 years, 9 months ago.

Viewing 15 posts - 1 through 15 (of 26 total)
  • Author
    Posts
  • #159446

    chrisburton
    Participant

    So I had a bit of time today to start on a Kirby comment system that I recently requested on their forum.

    I used the Kirby Toolkit as much as possible which made coding much faster and also helped me comprehend a bit more about databases.

    As I thought about how to approach this, I figured it would be difficult to find an ‘article ID’ to only show comments for that particular article. But, Kirby actually prepends a number (03-article-three) to article pages. Therefore, you can set that as the article ID.

    Those of you who are interested can watch a screencast I have made showing a very basic comment form and me publishing comments using a database in the background. Of course this all will change (even the database). I just wanted to grasp the idea by creating a simple demo.

    Basically I am using that ID method I was talking about above to publish comments. I wanted to find out if they output to the correct article page in which they do (thankfully). The last scene is showing the structure of the database and the content that was inserted. As I stated above, the rows will most likely change (except the article_id column).

    Screencast.

    #159447

    Alen
    Participant

    Dude awesome job, Celine Dion would be so proud :)

    #159448

    Alen
    Participant

    Also, Happy New Year!

    #159451

    chrisburton
    Participant

    Fuck. I always forget to add ‘Power of Love’ to my screencasts.

    Thanks, Alen. Happy New Year to you as well.

    #159452

    TheDoc
    Moderator

    Looking forward to testing it out!

    #159457

    chrisburton
    Participant

    @thedoc I’m enjoying this so far. I just got Facebook and Twitter authentication working using Opauth. Exciting!

    #159461

    __
    Participant

    Looks cool. Are you sure about the DB? I thought the whole point of Kirby was flat files. This is straightforward enough that it wouldn’t really be difficult…?

    And congrats on figuring out Opauth!

    Is there any code to peek at?

    #159469

    chrisburton
    Participant

    @traq

    Storing comments in multiple files is certainly possible but I don’t think it’s ideal. I think a DB is more appropriate for this. And there is already a plugin that does that for Kirby which Gray pointed out, I believe, on the Kirby forums. Unfortunately it does not offer what I’m looking for and doesn’t seem that simple to initiate. That’s what I want to solve. This will not be a simple drop into the plugins folder but it should be easy enough for people to follow a really short tutorial.

    Thanks. Although, I’m running into issues with Opauth and having it setup like Bastian when you login to his forums. He did say he wanted to talk to me about what I’m doing. I’ve also asked him to release the code.

    Keep in mind this code is very rough. Just trying to test things out before I condense it down as much as possible. I also haven’t incorporated Opauth with this just yet. I’ll try to work on that today.

        <?php
    
            // INSERT COMMENTS ON PUBLISH/SUBMIT
    
            if($_SERVER["REQUEST_METHOD"] == "POST") {
    
                db::connect('localhost','$username','$password','$table');
    
                $comment = db::escape($_POST['comment']);
                $page_id = ltrim($page->num(), '0');
                $article_id = db::escape($page_id);
    
                if($comment == '') {
                    echo '<div style="color:red;">No blank comments allowed.</div>';
                } else {
                    $insert = db::insert('comments',
                        array(
                            'id' => NULL,
                            'comment' => $comment,
                            'article_id'   => $article_id,
                            'date' => 'NOW()',
                        )
                    );
                }
            }
        ?>
    
    <ul>
        <?php
    
        // OUTPUT COMMENTS
    
            $article_id = ltrim($page->num(), '0');
    
            db::connect('localhost','$username','$password','$table');
            $comments = db::select('comments', 'comment, date', array('article_id' => $article_id), 'id DESC', 0,20);
    
            if($comments) {
                foreach ($comments as $comment) {
                    $text = $comment['comment'];
                    $date = date('M. jS, Y', strtotime($comment['date']));
                    echo "<li><p><small>".$date."</small></p><p>".$text."</p><br /></li>";
                }
            }
        ?>
    </ul>
    
    <?php // COMMENT FORM ?>
    
    <section class="responses" id="reply">
        <form action="#reply" method="post">
          <textarea name="comment" cols="75" rows="10" id="comment" placeholder="Your response?..." /></textarea>
          <br />
          <input type="hidden" name="article_id" id="article_id" value="<?php echo ltrim($page->num(), '0'); ?>" />
          <input type="submit" value="Reply" />  
        </form>
    </section>
    
    #159490

    __
    Participant

    Storing comments in multiple files is certainly possible but I don’t think it’s ideal. I think a DB is more appropriate for this.

    I’d agree, but files wouldn’t be bad. You could even store all comments for a single article in the same file, with an identifying filename.

    #159492

    chrisburton
    Participant

    True. But I’m not sure of the performance of having it like that. I’m not even sure how I could go about achieving that method. However, if someone wants to take this code as a framework for that after it’s cleaned up and the security vulnerabilities are reduced, I’m sure others would be happy to try it.

    I assume it would look something like this:

    User: Adrian
    
    ----
    
    Website: yoursite.com
    
    ----
    
    Twitter: adrian
    
    ----
    
    Text: This is a comment
    

    An issue that I think could arise is how you would gather up all those comments if you need to move to a different CMS?

    #159496

    Alen
    Participant

    Can you use SQLite?

    #159500

    TheDoc
    Moderator

    @traq – I hear what you’re saying but I think using a DB is better suited in this case. While Kirby is a database-free CMS it still provides lots of built-in methods for accessing DBs if required.

    #159503

    chrisburton
    Participant

    @alen If you’re asking personally for this plugin, I’ve never used SQLite. But if you’re asking in general, there’s nothing that limits you here. The toolkit is just a shortcut using classes

    Example

    db::connect($host, $user, $password, $dbname);
    

    Instead of (I think this is right)

    new PDO('mysql:host=$host;dbname=$dbname', $user, $password);
    
    #159510

    Alen
    Participant

    @chrisburton, it was just a suggestion, as I’m not that familiar with Kirby. SQLite seems to fit the file based mentality so not sure how all that fits within Kirby.

    #159512

    chrisburton
    Participant

    @alen

    I would really have to take a look at SQLite to give a definitive answer but I’m sure it could as Kirby is highly flexible.

Viewing 15 posts - 1 through 15 (of 26 total)

You must be logged in to reply to this topic.

icon-link icon-logo-star icon-search icon-star