Passed
Push — Showing-Posts ( 91b09f...1f1a70 )
by Stone
03:15
created

PostModel::getSinglePost()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 1
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Models;
4
5
use Core\Constant;
6
use Core\Container;
7
use Core\Model;
8
use Core\Traits\StringFunctions;
9
10
class PostModel extends Model
11
{
12
    use StringFunctions;
13
14
    private $postsTbl;
15
    private $categoriesTbl;
16
    private $usersTbl;
17
18
    public function __construct(Container $container)
19
    {
20
        parent::__construct($container);
21
        $this->postsTbl = $this->getTablePrefix('posts');
22
        $this->categoriesTbl = $this->getTablePrefix('categories');
23
        $this->usersTbl = $this->getTablePrefix('users');
24
    }
25
26
    /**
27
     * get all the posts with details
28
     * @param int $offset where to start (for pagination)
29
     * @param int $limit the number of posts
30
     * @param bool $isFrontPage extract only front page posts
31
     * @return array list of posts
32
     * @throws \ErrorException
33
     */
34
    private function getAllPosts(int $offset, int $limit, bool $isFrontPage = false):array
35
    {
36
        $sql = "SELECT title, post_image,article,$this->postsTbl.last_update, posts_slug, category_name, categories_slug, pseudo as author, idusers
37
                FROM $this->postsTbl INNER JOIN $this->categoriesTbl ON $this->postsTbl.categories_idcategories = $this->categoriesTbl.idcategories
38
                INNER JOIN $this->usersTbl ON $this->postsTbl.author_iduser = $this->usersTbl.idusers";
39
        if ($isFrontPage) {
40
            $sql .= " WHERE on_front_page = 1";
41
        }
42
        $sql .= " ORDER BY $this->postsTbl.creation_date DESC";
43
        $sql .= " LIMIT :limit OFFSET :offset";
44
        $this->query($sql);
45
        $this->bind(":limit", $limit);
46
        $this->bind(":offset", $offset);
47
        $this->execute();
48
        $results = $this->fetchAll();
49
        $sendResults = [];
50
        //we create the excerpt for the text and add it to the object
51
        foreach ($results as $result) {
52
            $result->{'excerpt'} = $this->getExcerpt($result->article);
53
            $sendResults[] = $result;
54
        }
55
        return $sendResults;
56
    }
57
58
    /**
59
     * get the list of front posts
60
     * @param int $offset
61
     * @param int $limit
62
     * @return array
63
     * @throws \ErrorException
64
     */
65
    public function getFrontPosts(int $offset = 0, int $limit = Constant::FRONT_PAGE_POSTS)
66
    {
67
        return $this->getAllPosts($offset, $limit, true);
68
    }
69
70
    /**
71
     * get the list of all the posts.
72
     * @param int $offset
73
     * @param int $limit
74
     * @return array
75
     * @throws \ErrorException
76
     */
77
    public function getPosts(int $offset = 0, int $limit = Constant::POSTS_PER_PAGE)
78
    {
79
        return $this->getAllPosts($offset, $limit, false);
80
    }
81
82
83
    /**
84
     * Create a new post
85
     * @param string $title
86
     * @param string $postImage
87
     * @param int $idCategory
88
     * @param string $article
89
     * @param int $idUser
90
     * @param int $published
91
     * @param int $onFrontPage
92
     * @param string $postSlug
93
     * @return string
94
     * @throws \Exception
95
     */
96
    public function newPost(
97
        string $title,
98
        string $postImage,
99
        int $idCategory,
100
        string $article,
101
        int $idUser,
102
        int $published,
103
        int $onFrontPage,
104
        string $postSlug
105
    ) {
106
        $sql = "
107
          INSERT INTO $this->postsTbl (title, post_image, categories_idcategories, article, author_iduser, creation_date, published, on_front_page, posts_slug)
108
          VALUES (:title, :post_image, :categories_idcategories, :article, :author_iduser, NOW(), :published, :on_front_page, :posts_slug)
109
        ";
110
        $this->query($sql);
111
        $this->bind(':title', $title);
112
        $this->bind(':post_image', $postImage);
113
        $this->bind(':categories_idcategories', $idCategory);
114
        $this->bind(':article', $article);
115
        $this->bind(':author_iduser', $idUser);
116
        $this->bind(':published', $published);
117
        $this->bind(':on_front_page', $onFrontPage);
118
        $this->bind(':posts_slug', $postSlug);
119
120
        $this->execute();
121
122
        return $this->dbh->lastInsertId();
123
124
    }
125
126
    /**
127
     * get all the posts from a certain category
128
     * @param int $categoryId the id of the category
129
     * @param int $offset the offset for pagination
130
     * @param int $limit the limit to display
131
     * @return array list of posts in set category
132
     * @throws \Exception
133
     */
134
    public function getPostsInCategory(int $categoryId, int $offset = 0, int $limit = Constant::POSTS_PER_PAGE): array
135
    {
136
        $sql = "SELECT title, post_image,article,$this->postsTbl.last_update, posts_slug, category_name, categories_slug, pseudo as author, idusers
137
                FROM $this->postsTbl INNER JOIN $this->categoriesTbl ON $this->postsTbl.categories_idcategories = $this->categoriesTbl.idcategories
138
                INNER JOIN $this->usersTbl ON $this->postsTbl.author_iduser = $this->usersTbl.idusers
139
                WHERE categories_idcategories = :categoryId 
140
                ORDER BY $this->postsTbl.creation_date DESC
141
                LIMIT :limit OFFSET :offset
142
                ";
143
        $this->query($sql);
144
        $this->bind(":categoryId", $categoryId, \PDO::PARAM_INT);
145
        $this->bind(":limit", $limit);
146
        $this->bind(":offset", $offset);
147
        $this->execute();
148
149
        $results = $this->fetchAll();
150
        $sendResults = [];
151
        //we create the excerpt for the text and add it to the object
152
        foreach ($results as $result) {
153
            $result->{'excerpt'} = $this->getExcerpt($result->article);
154
            $sendResults[] = $result;
155
        }
156
        return $sendResults;
157
    }
158
159
160
    /**
161
     * get a single post from it's ID
162
     * @param int $postid the post ID to get
163
     * @return array the single post details
164
     * @throws \Exception
165
     */
166
    public function getSinglePost(int $postid) //TODO Add this return type to others, although the return false might be an issue, update the fetch and fetchall methods ?
167
    {
168
        $sql = "SELECT title, post_image,article,$this->postsTbl.last_update, posts_slug, categories_idcategories, category_name, published, on_front_page, categories_slug, pseudo as author, idusers
169
                FROM $this->postsTbl INNER JOIN $this->categoriesTbl ON $this->postsTbl.categories_idcategories = $this->categoriesTbl.idcategories
170
                INNER JOIN $this->usersTbl ON $this->postsTbl.author_iduser = $this->usersTbl.idusers
171
                WHERE idposts = :postId 
172
                ;";
173
        $this->query($sql);
174
        $this->bind(":postId", $postid, \PDO::PARAM_INT);
175
        $this->execute();
176
177
        return $this->fetch();
178
    }
179
}