Passed
Push — Showing-Posts ( ca7424...b80001 )
by Stone
02:25
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, last_update, published, on_front_page, posts_slug)
108
          VALUES (:title, :post_image, :categories_idcategories, :article, :author_iduser, NOW(), 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
    public function modifyPost(
127
        int $postId,
128
        string $title,
129
        string $postImage,
130
        int $idCategory,
131
        string $article,
132
        int $idUser,
133
        int $published,
134
        int $onFrontPage,
135
        string $postSlug
136
    )
137
    {
138
        $sql="
139
            UPDATE $this->postsTbl 
140
            SET 
141
                title = :title,
142
                post_image = :postImage,
143
                categories_idcategories = :idCategory,
144
                article = :article,
145
                author_iduser = :idUser,
146
                last_update = NOW(),
147
                published = :published,
148
                on_front_page = :onFrontPage,
149
                posts_slug = :postSlug
150
            WHERE
151
              idposts = :postId
152
        ;";
153
        $this->query($sql);
154
        $this->bind(":title",$title);
155
        $this->bind(":postImage",$postImage);
156
        $this->bind(":idCategory",$idCategory);
157
        $this->bind(":article",$article);
158
        $this->bind(":idUser",$idUser);
159
        $this->bind(":published",$published);
160
        $this->bind(":onFrontPage",$onFrontPage);
161
        $this->bind(":postSlug",$postSlug);
162
        $this->bind(":postId",$postId);
163
164
        return $this->execute();
165
    }
166
167
    /**
168
     * get all the posts from a certain category
169
     * @param int $categoryId the id of the category
170
     * @param int $offset the offset for pagination
171
     * @param int $limit the limit to display
172
     * @return array list of posts in set category
173
     * @throws \Exception
174
     */
175
    public function getPostsInCategory(int $categoryId, int $offset = 0, int $limit = Constant::POSTS_PER_PAGE): array
176
    {
177
        $sql = "SELECT title, post_image,article,$this->postsTbl.last_update, posts_slug, category_name, categories_slug, pseudo as author, idusers
178
                FROM $this->postsTbl INNER JOIN $this->categoriesTbl ON $this->postsTbl.categories_idcategories = $this->categoriesTbl.idcategories
179
                INNER JOIN $this->usersTbl ON $this->postsTbl.author_iduser = $this->usersTbl.idusers
180
                WHERE categories_idcategories = :categoryId 
181
                ORDER BY $this->postsTbl.creation_date DESC
182
                LIMIT :limit OFFSET :offset
183
                ";
184
        $this->query($sql);
185
        $this->bind(":categoryId", $categoryId, \PDO::PARAM_INT);
186
        $this->bind(":limit", $limit);
187
        $this->bind(":offset", $offset);
188
        $this->execute();
189
190
        $results = $this->fetchAll();
191
        $sendResults = [];
192
        //we create the excerpt for the text and add it to the object
193
        foreach ($results as $result) {
194
            $result->{'excerpt'} = $this->getExcerpt($result->article);
195
            $sendResults[] = $result;
196
        }
197
        return $sendResults;
198
    }
199
200
201
    /**
202
     * get a single post from it's ID
203
     * @param int $postid the post ID to get
204
     * @return array the single post details
205
     * @throws \Exception
206
     */
207
    public function getSinglePost(int $postid)
208
    {
209
        $sql = "SELECT idposts, title, post_image,article,$this->postsTbl.last_update, posts_slug, categories_idcategories, category_name, published, on_front_page, categories_slug, pseudo as author, idusers
210
                FROM $this->postsTbl INNER JOIN $this->categoriesTbl ON $this->postsTbl.categories_idcategories = $this->categoriesTbl.idcategories
211
                INNER JOIN $this->usersTbl ON $this->postsTbl.author_iduser = $this->usersTbl.idusers
212
                WHERE idposts = :postId 
213
                ;";
214
        $this->query($sql);
215
        $this->bind(":postId", $postid, \PDO::PARAM_INT);
216
        $this->execute();
217
218
        return $this->fetch();
219
    }
220
}