Completed
Push — master ( 00ca69...f76e7b )
by Rasmus
01:56
created

BlogController::preparePost()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 33
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 33
ccs 20
cts 20
cp 1
rs 8.439
c 0
b 0
f 0
cc 5
eloc 17
nc 2
nop 1
crap 5
1
<?php
2
namespace Chp\TextContent;
3
4 1
include_once(__DIR__ . '/../../app/config/text-content.php');
5
6
/**
7
 * A blog controller
8
 * Made by Rasmus Berg (c) 2014-2017
9
 *
10
 * @property  object  $di         Anax-MVC class handler
11
 * @property  object  $response   Anax-MVC Php Header class
12
 * @property  object  $url        Anax-MVC url-handler class
13
 * @property  object  $theme      Anax-MVC theme-handler class
14
 * @property  object  $views      Anax-MVC views-handler class
15
 * @property  object  $textFilter Anax-MVC textformat-handler class
16
 */
17
class BlogController implements \Anax\DI\IInjectionAware
18
{
19
  use \Anax\DI\TInjectable;
20
	
21
  /**
22
	 * Properties
23
	 */
24
  private $content = null;
25
  private $postsPerPage;
26
  private $urlPrefix;
27
  
28
  /**
29
   * Initialize the controller
30
   *
31
   * @return    void
32
   */
33 2 View Code Duplication
  public function initialize(){
34 2
    if(!is_object($this->di))
35 2
      throw new \Anax\Exception\InternalServerErrorException('"$this->di" is not valid!');
36
    
37 2
    $this->content = new \Chp\TextContent\Content();
38 2
    $this->content->setDI($this->di);
39
    
40 2
    $this->postsPerPage = CHP_TC_POSTSPERPAGE;
41 2
    $this->urlPrefix = CHP_TC_URLPREFIX;
42 2
  }
43
  
44
   /**
45
   * Index action - uses tagAction whitout tag
46
   *
47
   * @return   void
48
   */
49
  public function indexAction($page = null){
50
    $posts = $this->getBlogContent($page);
51
    
52
    if(count($posts) > 0){
53
      foreach($posts AS $key => $post){
54
        // Prepare blog post for show in view
55
        $posts[$key] = $this->preparePost($post);
56
      }
57
    }
58
    
59
    // Show blog posts in view
60
    $this->postsToView($posts, null);
61
  }
62
  
63
  /**
64
   * Get blog posts by tag
65
   *
66
   * @param   string  $tag    Blog-tag
67
   * @param   int     $page   Page on paging
68
   * @return  void
69
   */
70
  public function tagAction($tag = null, $page = null){    
71
    $posts = $this->getBlogContent($page, $tag);
72
    
73
    if(count($posts) == 0 && !is_null($tag))
74
      $this->response->redirect($this->url->create("{$this->urlPrefix}blog/"));
75
76
    foreach($posts AS $key => $post){
77
      // Prepare blog post for show in view
78
      $posts[$key] = $this->preparePost($post);
79
    }
80
    
81
    $this->postsToView($posts, $tag);
82
  }
83
  
84
  /**
85
   * Get blog post by slug
86
   *
87
   * @param   string    $slug    Blog-tag
88
   * @return  void
89
   */
90 1
  public function readAction($slug = null){
91
    
92
    if(is_null($slug)){
93
      $this->response->redirect($this->url->create("{$this->urlPrefix}blog/"));
94
    }
95
    
96
    $post = $this->content->getContentBySlug($slug, 'blog-post');
97
    
98
    if(empty($post)){
99
      $this->response->redirect($this->url->create("{$this->urlPrefix}blog/"));
100
    }
101
    
102
    // Prepare blog post for show in view
103
    $post = $this->preparePost($post);
104
    
105
    $title = "Blog -  {$post->title}";
106
    
107
    $this->theme->setTitle($title);
108
    $this->views->add('text-content/blog-post', 
109 1
      [
110
        'title'         => $title,
111
        'post' 	        => $post,
112
        'blogIndexUrl'  => $this->url->create("{$this->urlPrefix}blog/")
113
      ]
114
    );
115
    
116
    /*$this->dispatcher->forward([
117
      'controller' => 'comment',
118
      'action'     => 'view',
119
      'params'	   =>	["{$this->urlPrefix}blog/read/{$post->slug}"]
120
    ]);*/
121
  }
122
  
123
  /**
124
   * Get blog posts by tag or type
125
   *
126
   * @param   int     $page    Page asked for
127
   * @param   string  $tag     Tag-slug to search for
128
   * @return  object  $posts   A object with post objects
129
   */
130 1
  public function getBlogContent($page = null, $tag = null){
131 1
    if(!is_null($tag))
132 1
      return $this->content->getAllContentOfTag($tag, 'blog-post', $page, $this->postsPerPage);
133
    else
134 1
      return $this->content->getAllContentOfType('blog-post', $page, $this->postsPerPage);
135
  }
136
  
137
  /**
138
   * Prepare blog post to show in view 
139
   *
140
   * @param   object  $post   Blog post object
141
   * @return  object  $result Prepared blog post object
142
   */
143 1
  public function preparePost($post){    
144 1
    $result = null;
145
    
146 1
    if(is_object($post) && property_exists($post, 'title')){
147 1
      $result = (object)[];
148
      
149 1
      foreach($post as $key => $value){
150 1
        $result->{$key} = $value;
151 1
      }
152
      
153 1
      $result->title        = htmlspecialchars($post->title, ENT_QUOTES);
154 1
      $result->ingress      = htmlspecialchars($post->ingress, ENT_QUOTES);
155 1
      $result->text         = $this->textFilter->doFilter(htmlspecialchars($post->text, ENT_QUOTES), $post->filters);
156 1
      $result->editUrl      = $this->url->create("{$this->urlPrefix}content/edit/{$post->id}");
157 1
      $result->showUrl      = $this->url->create("{$this->urlPrefix}blog/read/" . $post->slug);
158
      //$result->authorId     = $post->author;
159
      //$result->authorName   = htmlspecialchars($post->name, ENT_QUOTES);
160
      //$result->authorUrl    = $this->url->create("{$this->urlPrefix}users/id/{$post->author}");
161
      
162
      //unset($result->author);
163
      
164 1
      $tags = $this->content->getTagsForContent($post->id);
165
      
166 1
      foreach($tags AS $item_key => $item){
167 1
        $tags[$item_key]->tag  = htmlspecialchars($item->tag, ENT_QUOTES);
168 1
        $tags[$item_key]->url  = $this->url->create($this->urlPrefix . "blog/tag/{$item->slug}");
169 1
      }
170
      
171 1
      $result->tags = $tags;
172 1
    }
173
174 1
    return $result;
175
  }
176
  
177
  /**
178
   * Show blog posts in view
179
   *
180
   * @param   object  $posts   Object of blog post objects
181
   * @param   string  $tag     Tag-slug which has give this result
182
   * @return  void
183
   */
184
  private function postsToView($posts, $tag = null){
185
    $tag_title = null;
186
    
187
    if(!is_null($tag))
188
      $tag_title = $this->content->getTagBySlug($tag);
189
    
190
    $title = "Blog" . ((!is_null($tag_title)) ? " - tag: {$tag_title}" : NULL);
191
  
192
    $this->theme->setTitle($title);
193
    $this->views->add('text-content/blog-index', 
194
      [
195
        'title'     => $title,
196
        'posts' 	  => $posts,
197
        'tag'       => $tag_title
198
      ]
199
    );
200
  }
201
}