Classes

File phpboost/cache/CommentsCache.class.php

File phpboost/cache/CommentsCache.class.php

  1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 
<?php
/**
 * @package     PHPBoost
 * @subpackage  Cache
 * @copyright   &copy; 2005-2019 PHPBoost
 * @license     https://www.gnu.org/licenses/gpl-3.0.html GNU/GPL-3.0
 * @author      Kevin MASSY <reidlos@phpboost.com>
 * @version     PHPBoost 5.2 - last update: 2014 12 22
 * @since       PHPBoost 3.0 - 2011 09 24
*/

class CommentsCache implements CacheData
{
    private $comments = array();

    /**
     * {@inheritdoc}
     */
    public function synchronize()
    {
        $this->comments = array();

        $result = PersistenceContext::get_querier()->select("
            SELECT comments.*, topic.*, member.*
            FROM " . DB_TABLE_COMMENTS . " comments
            LEFT JOIN " . DB_TABLE_COMMENTS_TOPIC . " topic ON comments.id_topic = topic.id_topic
            LEFT JOIN " . DB_TABLE_MEMBER . " member ON member.user_id = comments.user_id
            ORDER BY comments.timestamp " . CommentsConfig::load()->get_order_display_comments()
        );

        while ($row = $result->fetch())
        {
            $this->comments[$row['id']] = array(
                'id' => $row['id'],
                'id_topic' => $row['id_topic'],
                'module_id' => $row['module_id'],
                'id_in_module' => $row['id_in_module'],
                'topic_identifier' => $row['topic_identifier'],
                'message' => $row['message'],
                'note' => $row['note'],
                'timestamp' => $row['timestamp'],
                'path' => $row['path'],
                'user_id' => $row['user_id']
            );
        }
    }

    public function get_comments()
    {
        return $this->comments;
    }

    public function comment_exists($id)
    {
        return array_key_exists($id, $this->comments);
    }

    public function comment_exists_by_module($module_id, $id_in_module)
    {
        $comments = $this->get_comments_by_module($module_id, $id_in_module);
        return !empty($comments);
    }

    public function get_comment($id)
    {
        if ($this->comment_exists($id))
        {
            return $this->comments[$id];
        }
        return null;
    }

    public function get_comments_by_module($module_id, $id_in_module, $topic_identifier)
    {
        $comments = array();
        foreach ($this->comments as $id_comment => $informations)
        {
            if ($informations['module_id'] == $module_id && $informations['id_in_module'] == $id_in_module && $informations['topic_identifier'] == $topic_identifier)
            {
                $comments[$id_comment] = $informations;
            }
        }
        return $comments;
    }

    public function get_count_comments_by_module($module_id, $id_in_module, $topic_identifier)
    {
        return count($this->get_comments_by_module($module_id, $id_in_module, $topic_identifier));
    }

    public function get_count_comments()
    {
        return count($this->comments);
    }

    /**
     * Loads and returns the comments cached data.
     * @return CommentsCache The cached data
     */
    public static function load()
    {
        return CacheManager::load(__CLASS__, 'kernel', 'comments');
    }

    /**
     * Invalidates the current comments cached data.
     */
    public static function invalidate()
    {
        CacheManager::invalidate('kernel', 'comments');
    }

    private function slice_comments(Array $comments, $offset, $lenght = 0)
    {
        if (empty($lenght))
        {
            return array_slice($comments, $offset, count($comments), true);
        }
        else
        {
            return array_slice($comments, $offset, $lenght, true);
        }
    }
}
?>