1 | <?php |
||||
2 | defined('BASEPATH') OR exit('No direct script access allowed'); |
||||
3 | |||||
4 | class BlogManager extends CI_Model { |
||||
5 | |||||
6 | const TABLE_PREFIX = "blogger_posts"; |
||||
7 | |||||
8 | private $ci; |
||||
9 | |||||
10 | private $table_name; |
||||
11 | |||||
12 | function __construct() { |
||||
13 | parent::__construct(); |
||||
14 | $this->ci =& get_instance(); |
||||
15 | $this->ci->load->database(); |
||||
16 | } |
||||
17 | /** |
||||
18 | * [setBlogName description] |
||||
19 | * @param [type] $name [description] |
||||
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||||
20 | */ |
||||
21 | function setBlogName($name) { |
||||
0 ignored issues
–
show
|
|||||
22 | $this->table_name = self::TABLE_PREFIX . ($name != null ? "_" . $name : ""); |
||||
23 | } |
||||
24 | /** |
||||
25 | * [getBlogName description] |
||||
26 | * @return [type] [description] |
||||
0 ignored issues
–
show
|
|||||
27 | */ |
||||
28 | function getBlogName() { |
||||
0 ignored issues
–
show
|
|||||
29 | return $this->table_name; |
||||
30 | } |
||||
31 | /** |
||||
32 | * [createPost creates a post with the given $title and $content in the |
||||
33 | * database.] |
||||
34 | * |
||||
35 | * @param string $title The title of the post. |
||||
36 | * |
||||
37 | * @param string $content The content of the post. |
||||
38 | * |
||||
39 | * @param int $adminId Optional, the id of the poster. this is needed if |
||||
40 | * you provided an admins table name during blog |
||||
41 | * installation. |
||||
42 | * |
||||
43 | * @return 0 Returns the id of the newly created post in the |
||||
0 ignored issues
–
show
|
|||||
44 | * database. Returns 0 if the post couldn't be |
||||
45 | * created. |
||||
46 | */ |
||||
47 | function createPost(string $title, string $content, int $adminId=null): int { |
||||
0 ignored issues
–
show
|
|||||
48 | $this->ci->load->helper("url"); |
||||
49 | $data = array( |
||||
50 | "title" => $title, |
||||
51 | "content" => $content, |
||||
52 | "slug" => url_title($title) |
||||
0 ignored issues
–
show
The function
url_title was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
53 | ); |
||||
54 | if (is_numeric($title)) $data["slug"] = "_" . $data["slug"]; |
||||
55 | if ($adminId !== null) $data["poster_id"] = $adminId; |
||||
56 | if ($this->db->insert($this->table_name, $data)) return $this->db->insert_id(); |
||||
57 | return 0; |
||||
58 | } |
||||
59 | /** |
||||
60 | * [createAndPublish description] |
||||
61 | * @param [type] $title [description] |
||||
0 ignored issues
–
show
|
|||||
62 | * @param [type] $content [description] |
||||
63 | * @param [type] $adminId [description] |
||||
64 | * @return [type] [description] |
||||
0 ignored issues
–
show
|
|||||
65 | */ |
||||
66 | function createAndPublishPost($title, $content, $adminId=null) { |
||||
0 ignored issues
–
show
|
|||||
67 | $data = array( |
||||
68 | "title" => $title, |
||||
69 | "content" => $content, |
||||
70 | "slug" => url_title($title), |
||||
0 ignored issues
–
show
The function
url_title was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
71 | "published" => 1, |
||||
72 | "date_published" => date("Y-m-d H:i:s") |
||||
73 | ); |
||||
74 | if (is_numeric($title)) $data["slug"] = "_" . $data["slug"]; |
||||
75 | if ($adminId != null) $data["poster_id"] = $adminId; |
||||
76 | if ($this->db->insert($this->table_name, $data)) return $this->db->insert_id(); |
||||
77 | return false; |
||||
78 | } |
||||
79 | /** |
||||
80 | * [getPosts get posts from the database by the given $page starting from the |
||||
81 | * value of 1 and returns $limit number of rows.] |
||||
82 | * |
||||
83 | * @param int $page Page number starting from 1. |
||||
84 | * |
||||
85 | * @param int $limit Number of posts to return. |
||||
86 | * |
||||
87 | * @param boolean $filter if true, returns only published posts, if false |
||||
88 | * return all posts. false by default. |
||||
89 | * |
||||
90 | * @param boolean $hits order by hits. |
||||
91 | * |
||||
92 | * @return array Array of posts for a given page. |
||||
93 | */ |
||||
94 | function getPosts(int $page=1, int $limit=5, bool $filter=false, bool $hits=false): array { |
||||
0 ignored issues
–
show
|
|||||
95 | if ($limit != 0) $this->db->limit($limit, ($page * $limit) - $limit); |
||||
96 | if ($filter) $this->db->where("published", 1); |
||||
97 | if ($hits) { |
||||
98 | $this->db->order_by("hits", "DESC"); |
||||
99 | } else { |
||||
100 | $this->db->order_by("id", "DESC"); |
||||
101 | } |
||||
102 | return $this->db->get($this->table_name)->result_array(); |
||||
103 | } |
||||
104 | /** |
||||
105 | * [getRecentPosts description] |
||||
106 | * @param integer $limit [description] |
||||
107 | * @param boolean $filter [description] |
||||
108 | * @return [type] [description] |
||||
0 ignored issues
–
show
|
|||||
109 | */ |
||||
110 | function getRecentPosts($limit=5, $filter=false) { |
||||
0 ignored issues
–
show
|
|||||
111 | if ($limit != null && $limit != null) $this->db->limit($limit); |
||||
112 | if ($filter) $this->db->where("published", 1); |
||||
113 | $this->db->order_by("id", "DESC"); |
||||
114 | return $this->db->get($this->table_name)->result_array(); |
||||
115 | } |
||||
116 | /** |
||||
117 | * [savePost saves or midfies the content of a post record given by $postId |
||||
118 | * in the database.] |
||||
119 | * @param int $postId ID of the post to modify. |
||||
120 | * @param string $title New title of the post. |
||||
121 | * @param string $content New content of the post. |
||||
122 | * @return bool True on success, False if not. |
||||
123 | */ |
||||
124 | function savePost($postId, $title, $content): bool { |
||||
0 ignored issues
–
show
|
|||||
125 | $data = array ( |
||||
126 | "title" => $title, |
||||
127 | "content" => $content, |
||||
128 | "slug" => url_title($title) |
||||
0 ignored issues
–
show
The function
url_title was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
129 | ); |
||||
130 | $this->db->where("id", $postId); |
||||
131 | return $this->db->update($this->table_name, $data); |
||||
132 | } |
||||
133 | /** |
||||
134 | * [getPost Gets a specific post by the given value of $postId. NB: This will |
||||
135 | * increment the hit count of the particularpost in the database.] |
||||
136 | * @param int $postId ID of the post to retrieve. |
||||
137 | * @return array An associative array for the Posts's data. |
||||
138 | */ |
||||
139 | function getPost($postId, $hit=true) { |
||||
0 ignored issues
–
show
|
|||||
140 | if (is_numeric($postId)) { |
||||
0 ignored issues
–
show
|
|||||
141 | $this->db->where("id", $postId); |
||||
142 | } else { |
||||
143 | $this->db->where("slug", $postId); |
||||
144 | } |
||||
145 | $query = $this->db->get($this->table_name); |
||||
146 | if ($query->num_rows() > 0) { |
||||
147 | if ($hit) { |
||||
148 | if ($this->ci->config->item("blogger_hits") === null || |
||||
149 | $this->ci->config->item("blogger_hits") === true) { |
||||
150 | if (!is_numeric($postId)) $postId = $query->result()[0]->id; |
||||
0 ignored issues
–
show
|
|||||
151 | $this->db->where("id", $postId); |
||||
152 | $this->db->set("hits", "hits+1", FALSE); |
||||
153 | $this->db->update($this->table_name); |
||||
154 | } |
||||
155 | } |
||||
156 | $post = $query->result_array()[0]; |
||||
157 | $images = array(); |
||||
158 | // Fetch all images in post. |
||||
159 | preg_match("/<img\s[^>]*?src\s*=\s*['\"]([^'\"]*?)['\"][^>]*?>/", $post["content"], $images); |
||||
160 | $share_image = count($images) == 0 ? null : $images[0]; |
||||
161 | unset($images); |
||||
162 | $src = array(); |
||||
163 | // Get the contents of the src tag. |
||||
164 | preg_match("/(http|https):\/\/[a-zA-Z0-9-._\/]+/", $share_image, $src); |
||||
165 | if (count($src) == 0) return $post; |
||||
166 | $post["share_image"] = $src[0]; |
||||
167 | return $post; |
||||
168 | } |
||||
169 | return false; |
||||
0 ignored issues
–
show
|
|||||
170 | } |
||||
171 | /** |
||||
172 | * [getHits description] |
||||
173 | * @param [type] $postId [description] |
||||
0 ignored issues
–
show
|
|||||
174 | * @return [type] [description] |
||||
0 ignored issues
–
show
|
|||||
175 | */ |
||||
176 | function getHits(int $postId): int { |
||||
0 ignored issues
–
show
|
|||||
177 | $this->db->where("id", $postId); |
||||
178 | $query = $this->db->get($this->table_name); |
||||
179 | if ($query->num_rows() > 0) return $query->result()[0]->hits; |
||||
180 | return 0; |
||||
181 | } |
||||
182 | /** |
||||
183 | * [publishPost description] |
||||
184 | * @param [type] $postId [description] |
||||
0 ignored issues
–
show
|
|||||
185 | * @param [type] $publish [description] |
||||
186 | * @return [type] [description] |
||||
0 ignored issues
–
show
|
|||||
187 | */ |
||||
188 | function publishPost($postId, $publish) { |
||||
0 ignored issues
–
show
|
|||||
189 | $this->db->where("id", $postId); |
||||
190 | $this->db->set("published", $publish ? 1 : 0); |
||||
191 | $this->db->set("date_published", date("Y-m-d H:i:s")); |
||||
192 | return $this->db->update($this->table_name); |
||||
193 | } |
||||
194 | /** |
||||
195 | * [getPostsCount get the total number of posts in the database.] |
||||
196 | * @return [type] [description] |
||||
0 ignored issues
–
show
|
|||||
197 | */ |
||||
198 | function getPostsCount(bool $filter=true): int { |
||||
0 ignored issues
–
show
|
|||||
199 | if ($filter) $this->db->where("published", 1); |
||||
200 | $this->db->select("COUNT(title) as posts"); |
||||
201 | return (int) $this->db->get($this->table_name)->result()[0]->posts; |
||||
202 | } |
||||
203 | /** |
||||
204 | * [deletePost description] |
||||
205 | * @param [type] $postId [description] |
||||
0 ignored issues
–
show
|
|||||
206 | * @return [type] [description] |
||||
0 ignored issues
–
show
|
|||||
207 | */ |
||||
208 | function deletePost($postId) { |
||||
0 ignored issues
–
show
|
|||||
209 | $this->db->where("id", $postId); |
||||
210 | return $this->db->delete($this->table_name); |
||||
211 | } |
||||
212 | /** |
||||
213 | * [searchPosts description] |
||||
214 | * @param [type] $words [description] |
||||
0 ignored issues
–
show
|
|||||
215 | * @param [type] $page [description] |
||||
216 | * @param integer $limit [description] |
||||
217 | * @param boolean $filter [description] |
||||
218 | * @return [type] [description] |
||||
0 ignored issues
–
show
|
|||||
219 | */ |
||||
220 | function searchPosts($words, $page=1, $limit=5, $filter=false) { |
||||
0 ignored issues
–
show
|
|||||
221 | if ($limit != 0) $this->db->limit($limit, ($page * $limit) - $limit); |
||||
222 | if ($filter) $this->db->where("published", 1); |
||||
223 | $this->db->like("title", $words); |
||||
224 | $this->db->or_like("content", $words); |
||||
225 | return $this->db->get($this->table_name)->result_array(); |
||||
226 | } |
||||
227 | } |
||||
228 |