Completed
Push — master ( 7714d3...c690da )
by Francis
01:35
created

models/BlogManager.php (33 issues)

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
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
20
   */
21
  function setBlogName($name) {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
22
    $this->table_name = self::TABLE_PREFIX . ($name != null ? "_" . $name : "");
23
  }
24
  /**
25
   * [getBlogName description]
26
   * @return [type] [description]
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
27
   */
28
  function getBlogName() {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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
Documentation Bug introduced by
The doc comment 0 at position 0 could not be parsed: Unknown type name '0' at position 0 in 0.
Loading history...
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
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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 ignore-call  annotation

52
      "slug"    => /** @scrutinizer ignore-call */ url_title($title)
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
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
62
   * @param  [type] $content [description]
63
   * @param  [type] $adminId [description]
64
   * @return [type]          [description]
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
65
   */
66
  function createAndPublishPost($title, $content, $adminId=null) {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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 ignore-call  annotation

70
      "slug"           => /** @scrutinizer ignore-call */ url_title($title),
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
   * @param  int     $page   Page number starting from 1.
83
   * @param  int     $limit  Number of posts to return.
84
   * @param  boolean $filter if true, returns only published posts, if false
85
   *                         return all posts. false by default.
86
   * @param  boolean $hits   order by hits.
87
   * @return array           Array of posts for a given page.
88
   */
89
  function getPosts($page=1, $limit=5, $filter=false, $hits=false) {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
90
    if ($limit != 0) $this->db->limit($limit, ($page * $limit) - $limit);
91
    if ($filter) $this->db->where("published", 1);
92
    if ($hits) {
93
      $this->db->order_by("hits", "DESC");
94
    } else {
95
      $this->db->order_by("id", "DESC");
96
    }
97
    return $this->db->get($this->table_name)->result_array();
98
  }
99
  /**
100
   * [getRecentPosts description]
101
   * @param  integer $limit  [description]
102
   * @param  boolean $filter [description]
103
   * @return [type]          [description]
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
104
   */
105
  function getRecentPosts($limit=5, $filter=false) {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
106
    if ($limit != null && $limit != null) $this->db->limit($limit);
107
    if ($filter) $this->db->where("published", 1);
108
    $this->db->order_by("id", "DESC");
109
    return $this->db->get($this->table_name)->result_array();
110
  }
111
  /**
112
   * [savePost saves or midfies the content of a post record given by $postId
113
   * in the database.]
114
   * @param  int    $postId  ID of the post to modify.
115
   * @param  string $title   New title of the post.
116
   * @param  string $content New content of the post.
117
   * @return bool            True on success, False if not.
118
   */
119
  function savePost($postId, $title, $content): bool {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
120
    $data = array (
121
      "title"   => $title,
122
      "content" => $content,
123
      "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 ignore-call  annotation

123
      "slug"    => /** @scrutinizer ignore-call */ url_title($title)
Loading history...
124
    );
125
    $this->db->where("id", $postId);
126
    return $this->db->update($this->table_name, $data);
127
  }
128
  /**
129
   * [getPost Gets a specific post by the given value of $postId. NB: This will
130
   * increment the hit count of the particularpost in the database.]
131
   * @param  int   $postId ID of the post to retrieve.
132
   * @return array An associative array for the Posts's data.
133
   */
134
  function getPost($postId, $hit=true) {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
135
    if (is_numeric($postId)) {
0 ignored issues
show
The condition is_numeric($postId) is always true.
Loading history...
136
      $this->db->where("id", $postId);
137
    } else {
138
      $this->db->where("slug", $postId);
139
    }
140
    $query = $this->db->get($this->table_name);
141
    if ($query->num_rows() > 0) {
142
      if ($hit) {
143
        if ($this->ci->config->item("blogger_hits") === null ||
144
        $this->ci->config->item("blogger_hits") === true) {
145
          $this->db->where("id", $postId);
146
          $this->db->set("hits", "hits+1", FALSE);
147
          $this->db->update($this->table_name);
148
        }
149
      }
150
      $post =  $query->result_array()[0];
151
      $images = array();
152
      // Fetch all images in post.
153
      preg_match("/<img\s[^>]*?src\s*=\s*['\"]([^'\"]*?)['\"][^>]*?>/", $post["content"], $images);
154
      $share_image = count($images) == 0 ? null : $images[0];
155
      unset($images);
156
      $src = array();
157
      // Get the contents of the src tag.
158
      preg_match("/(http|https):\/\/[a-zA-Z0-9-._\/]+/", $share_image, $src);
159
      if (count($src) == 0) return $post;
160
      $post["share_image"] = $src[0];
161
      return $post;
162
    }
163
    return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type array.
Loading history...
164
  }
165
  /**
166
   * [getHits description]
167
   * @param  [type] $postId [description]
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
168
   * @return [type]         [description]
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
169
   */
170
  function getHits($postId) {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
171
    $this->db->where("id", $postId);
172
    $query = $this->db->get($this->table_name);
173
    if ($query->num_rows() > 0) return $query->result()[0]->hits;
174
    return 0;
175
  }
176
  /**
177
   * [publishPost description]
178
   * @param  [type] $postId  [description]
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
179
   * @param  [type] $publish [description]
180
   * @return [type]          [description]
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
181
   */
182
  function publishPost($postId, $publish) {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
183
    $this->db->where("id", $postId);
184
    $this->db->set("published", $publish ? 1 : 0);
185
    $this->db->set("date_published", date("Y-m-d H:i:s"));
186
    return $this->db->update($this->table_name);
187
  }
188
  /**
189
   * [getPostsCount get the total number of posts in the database.]
190
   * @return [type] [description]
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
191
   */
192
  function getPostsCount() {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
193
    $this->db->select("COUNT(title) as posts");
194
    return $this->db->get($this->table_name)->result()[0]->posts;
195
  }
196
  /**
197
   * [deletePost description]
198
   * @param  [type] $postId [description]
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
199
   * @return [type]         [description]
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
200
   */
201
  function deletePost($postId) {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
202
    $this->db->where("id", $postId);
203
    return $this->db->delete($this->table_name);
204
  }
205
  /**
206
   * [searchPosts description]
207
   * @param  [type]  $words  [description]
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
208
   * @param  [type]  $page   [description]
209
   * @param  integer $limit  [description]
210
   * @param  boolean $filter [description]
211
   * @return [type]          [description]
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
212
   */
213
  function searchPosts($words, $page, $limit=0, $filter=false) {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
214
    if ($limit != 0) $this->db->limit($limit, ($page * $limit) - $limit);
215
    if ($filter) $this->db->where("published", 1);
216
    $this->db->like("title", $words);
217
    $this->db->or_like("content", $words);
218
    return $this->db->get($this->table_name)->result_array();
219
  }
220
}
221