Passed
Push — master ( 488f68...c32df4 )
by Francis
01:18
created

Blogger::renderPostItems()   B

Complexity

Conditions 9
Paths 38

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 9
eloc 14
c 3
b 0
f 0
nc 38
nop 8
dl 0
loc 19
rs 8.0555

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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

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

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