Passed
Push — master ( 68c7df...152166 )
by Francis
01:20
created

Blogger::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
nc 2
nop 1
dl 0
loc 9
rs 10
c 1
b 0
f 0
1
<?php
2
defined('BASEPATH') OR exit('No direct script access allowed');
3
4
class Blogger {
5
6
  private $ci;
7
8
  private $dbforge;
9
10
  private $table_name;
11
12
  const TABLE_PREFIX = "blogger_posts";
13
14
  const PACKAGE = "francis94c/blog";
15
16
  const MARKDOWN_PACKAGE = "francis94c/ci-parsedown";
17
18
  const CREATE = "create";
19
20
  const CREATE_AND_PUBLISH = "createAndPublish";
21
22
  const EDIT = "edit";
23
24
  const PUBLISH = "publish";
25
26
  const DELETE = "delete";
27
28
  const ABORT = "abortAction";
29
30
  function __construct($params=null) {
31
    $this->ci =& get_instance();
1 ignored issue
show
Bug introduced by
The function get_instance 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

31
    $this->ci =& /** @scrutinizer ignore-call */ get_instance();
Loading history...
32
    $this->ci->load->database();
33
    $this->table_name = self::TABLE_PREFIX . (isset($params["name"]) ? "_" . $params["name"] : "");
34
    $this->ci->load->database();
35
    $this->ci->load->splint(self::PACKAGE, "*BlogManager", "bmanager");
36
    $this->ci->load->splint(self::MARKDOWN_PACKAGE, "+Parsedown", null, "parsedown");
37
    $this->ci->bmanager->setBlogName(isset($params["name"]) ? $params["name"] : null);
38
    $this->ci->load->helper("url");
39
  }
40
  /**
41
   * [install description]
42
   * @param  [type] $adminTableName          [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...
43
   * @param  [type] $adminTableName          [description]
44
   * @param  [type] $adminIdColumnName       [description]
45
   * @param  [type] $adminIdColumnConstraint [description]
46
   * @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...
47
   */
48
  public function install($blogName=null, $adminTableName = null, $adminIdColumnName = null, $adminIdColumnConstraint = null) {
49
    $blogName = $blogName == null ? $this->table_name : self::TABLE_PREFIX . "_" . $blogName;
50
    $this->ci->load->dbforge();
51
    $this->ci->dbforge->add_field("id");
52
    $fields = array(
53
      "title" => array(
54
        "type"       => "VARCHAR",
55
        "constraint" => 70,
56
      ),
57
      "content" => array(
58
        "type" => "TEXT"
59
      ),
60
      "date_published" => array(
61
        "type" => "TIMESTAMP",
62
        "null" => true
63
      ),
64
      "published" => array(
65
        "type" => "TINYINT"
66
      ),
67
      "hits"      => array(
68
        "type"       => "INT",
69
        "constraint" => 7,
70
        "default"    => 0
71
      ),
72
      "slug"      => array(
73
        "type"       => "VARCHAR",
74
        "constraint" => 80,
75
        "unique"     => true
76
      )
77
    );
78
    $constrain = $adminTableName !== null && $adminIdColumnName !== null &&
79
    $adminIdColumnConstraint !== null;
80
    if ($constrain) {
81
      $fields["poster_id"] = array(
82
        "type"       => "INT",
83
        "constraint" => $adminIdColumnConstraint
84
      );
85
      $this->ci->dbforge->add_field($fields);
86
      $this->ci->dbforge->add_field("date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP");
87
      $this->ci->dbforge->add_field(
88
        "FOREIGN KEY (poster_id) REFERENCES $adminTableName($adminIdColumnName)");
89
    }
90
    $attributes = array('ENGINE' => 'InnoDB');
91
    if (!$this->ci->dbforge->create_table($blogName, true, $attributes)) return false;
92
    return true;
93
  }
94
  /**
95
   * [setBlog description]
96
   * @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...
97
   * @deprecated
98
   */
99
  public function setName($name) {
100
    $this->table_name = self::TABLE_PREFIX . "_" . $name;
101
    $this->ci->bmanager->setBlogName($name != "" ? $name : null);
102
  }
103
  /**
104
   * [setBlog description]
105
   * @param [type] $blog [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...
106
   */
107
  public function setBlog($blog) {
108
    $this->table_name = self::TABLE_PREFIX . "_" . $blog;
109
    $this->ci->bmanager->setBlogName($blog != "" ? $blog : null);
110
  }
111
  /**
112
   * [getBlog description]
113
   * @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...
114
   */
115
  public function getName() {
116
    return $this->table_name;
117
  }
118
  /**
119
   * [loadHeaderScripts description]
120
   * @param  boolean $w3css [description]
121
   * @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...
122
   */
123
  private function loadScripts($w3css) {
124
    $this->ci->load->splint(self::PACKAGE, "-header_scripts", array(
125
      "w3css" => $w3css
126
    ));
127
  }
128
  /**
129
   * [w3css description]
130
   * @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...
131
   */
132
  public function w3css() {
133
    return "<link rel=\"stylesheet\" href=\"https://www.w3schools.com/w3css/4/w3.css\">";
134
  }
135
  /**
136
   * [fontsAwesome description]
137
   * @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...
138
   */
139
  public function fontsAwesome() {
140
    return "<link rel=\"stylesheet\" href=\"https://use.fontawesome.com/releases/v5.3.1/css/all.css\"";
141
  }
142
  /**
143
   * [loadEditor description]
144
   * @param  [type]  $callback [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...
145
   * @param  [type]  $postId   [description]
146
   * @param  boolean $w3css    [description]
147
   * @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...
148
   */
149
  public function loadEditor($callback, $postId=null, $w3css=true) {
150
    $this->loadScripts($w3css);
151
    $this->ci->load->helper("form");
152
    $data = array(
153
      "callback" => "Admin/token",
154
      "type"     => $postId == null ? "create" : "edit",
155
      "callback" => $callback
156
    );
157
    if ($postId != null) {
158
      $data["id"] = $postId;
159
      $post = $this->getPost($postId, false);
160
      $data["title"] = $post["title"];
161
      $data["content"] = $post["content"];
162
    }
163
    $this->ci->load->splint("francis94c/blog", "-post_edit", $data);
164
    return true;
165
  }
166
  /**
167
   * [savePost description]
168
   * @param  [type] $posterId [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
   * @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...
170
   */
171
  public function savePost($posterId=null) {
172
    $action = $this->ci->security->xss_clean($this->ci->input->post("action"));
173
    if ($action == "save") {
174
      $id = $this->ci->security->xss_clean($this->ci->input->post("id"));
175
      if ($id != "") {
176
        $this->ci->bmanager->savePost($this->ci->input->post("id"), $this->ci->security->xss_clean($this->ci->input->post("title")), $this->ci->security->xss_clean($this->ci->input->post("editor")), $posterId);
177
        return self::EDIT;
178
      } else {
179
        $this->ci->bmanager->createPost($this->ci->security->xss_clean($this->ci->input->post("title")), $this->ci->security->xss_clean($this->ci->input->post("editor")), $posterId);
180
        return self::CREATE;
181
      }
182
    } elseif ($action == "publish" || $action == "createAndPublish") {
183
      if ($action == "publish") {
184
        $id = $this->ci->security->xss_clean($this->ci->input->post("id"));
185
        if ($id == "") return self::ABORT;
186
        $this->ci->bmanager->savePost($id, $this->ci->security->xss_clean($this->ci->input->post("title")), $this->ci->security->xss_clean($this->ci->input->post("editor")), $posterId);
187
        $this->ci->bmanager->publishPost($id, true);
188
        return self::PUBLISH;
189
      } else {
190
        $this->ci->bmanager->createAndPublishPost($this->ci->security->xss_clean($this->ci->input->post("title")), $this->ci->security->xss_clean($this->ci->input->post("editor")), $posterId);
191
        return self::CREATE_AND_PUBLISH;
192
      }
193
    } elseif ($action == "delete") {
194
      if ($this->ci->bmanager->deletePost($this->ci->security->xss_clean($this->ci->input->post("id")))) return self::DELETE;
195
    }
196
    return false;
197
  }
198
  /**
199
   * [getPosts get posts from the database by the given $page starting from the
200
   * value of 1 and returns $limit number of rows.]
201
   * @param  int     $page   Page number starting from 1.
202
   * @param  int     $limit  Number of posts to return.
203
   * @param  boolean $filter if true, returns only published posts, if false
204
   *                         return all posts. false by default.
205
   * @return array Array of posts for a given page.
206
   */
207
  public function getPosts($page, $limit, $filter=false, $hits=false) {
208
    return $this->ci->bmanager->getPosts($page, $limit, $filter, $hits);
209
  }
210
  /**
211
   * [renderPosts description]
212
   * @param  [type]  $view       [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...
213
   * @param  [type]  $empty_view [description]
214
   * @param  [type]  $page       [description]
215
   * @param  [type]  $limit      [description]
216
   * @param  boolean $filter     [description]
217
   * @param  boolean $hits       [description]
218
   * @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...
219
   */
220
  public function renderPostItems($view=null, $callback=null, $empty_view=null, $page=1, $limit=5, $filter=false, $hits=false, $slug=true) {
221
    if ($view == null || $empty_view == null) $this->ci->load->bind("francis94c/blog", $blogger);
1 ignored issue
show
Comprehensibility Best Practice introduced by
The variable $blogger seems to be never defined.
Loading history...
222
    $posts = $this->getPosts($page, $limit, $filter, $hits);
223
    if (count($posts) == 0) {
224
      if ($empty_view == null) { $blogger->load->view("empty"); } else {
225
        $this->ci->load->view($empty_view);
226
        return true;
227
      }
228
    }
229
    $this->ci->load->helper("text");
230
    foreach ($posts as $post) {
231
      $post["callback"] = $callback != null ? trim($callback, "/") . "/" . ($slug ? $post["slug"] : $post["id"]) : "";
232
      $post["filter"] = $filter;
233
      $post["content"] = $this->ci->parsedown->text(ellipsize($post["content"], 300));
1 ignored issue
show
Bug introduced by
The function ellipsize 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

233
      $post["content"] = $this->ci->parsedown->text(/** @scrutinizer ignore-call */ ellipsize($post["content"], 300));
Loading history...
234
      if ($view == null) {$blogger->load->view("post_list_item", $post); } else {
235
        $this->ci->load->view($view, $post);
236
      }
237
    }
238
    return true;
239
  }
240
  /**
241
   * [getRecentPosts description]
242
   * @param  integer $limit  [description]
243
   * @param  boolean $filter [description]
244
   * @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...
245
   */
246
  public function getRecentPosts($limit=5, $filter=false) {
247
    return $this->ci->bmanager->getRecentPosts($limit, $filter);
248
  }
249
  /**
250
   * [renderPost description]
251
   * @param  [type] $post [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...
252
   * @param  [type] $view [description]
253
   * @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...
254
   */
255
  public function renderPost($post, $view=null) {
256
    if (!is_array($post)) $post = $this->ci->bmanager->getPost($post);
257
    $post["content"] = $this->ci->parsedown->text($post["content"]);
258
    if ($view == null) {
259
      $this->ci->load->splint("francis94c/blog", "-post_item", $post);
260
    } else {
261
      $this->ci->load->view($view, $post);
262
    }
263
  }
264
  /**
265
   * [metaOg description]
266
   * @param  [type] $post [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...
267
   * @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...
268
   */
269
  public function metaOg($post) {
270
    $data = array();
271
    $data["title"] = $post["title"];
272
    $data["description"] = substr($post["content"], 0, 154);
273
    if (isset($post["share_image"])) $data["image_link"] = $post["share_image"];
274
    $data["url"] = current_url();
1 ignored issue
show
Bug introduced by
The function current_url 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

274
    $data["url"] = /** @scrutinizer ignore-call */ current_url();
Loading history...
275
    return $this->ci->load->splint(self::PACKAGE, "-meta_og", $data, true);
276
  }
277
  /**
278
   * [getPostsCount description]
279
   * @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...
280
   */
281
  public function getPostsCount() {
282
    return $this->ci->bmanager->getPostsCount();
283
  }
284
  /**
285
   * [getPost description]
286
   * @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...
287
   * @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...
288
   */
289
  public function getPost($postId, $hit=true) {
290
    return $this->ci->bmanager->getPost($postId, $hit);
291
  }
292
  /**
293
   * [getHits description]
294
   * @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...
295
   * @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...
296
   */
297
  public function getHits($postId) {
298
    return $this->ci->bmanager->getHits($postId);
299
  }
300
  /**
301
   * [publishPost description]
302
   * @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...
303
   * @param  [type] $publish [description]
304
   * @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...
305
   */
306
  public function publishPost($postId, $publish) {
307
    return $this->ci->bmanager->publishPost($postId, $publish);
308
  }
309
  /**
310
   * [deletePost description]
311
   * @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...
312
   * @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...
313
   */
314
  public function deletePost($postId) {
315
    return $this->ci->bmanager->deletePost($postId);
316
  }
317
  /**
318
   * [searchPosts description]
319
   * @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...
320
   * @param  [type]  $page   [description]
321
   * @param  integer $limit  [description]
322
   * @param  boolean $filter [description]
323
   * @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...
324
   */
325
  public function searchPosts($words, $page, $limit=0, $filter=false) {
326
    return $this->ci->bmanager->searchPosts($words, $page, $limit, $filter);
327
  }
328
}
329