PageController::preparePage()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 8.2077

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 2
cts 12
cp 0.1666
rs 9.0856
c 0
b 0
f 0
cc 3
eloc 11
nc 2
nop 1
crap 8.2077
1
<?php
2
namespace Chp\TextContent;
3
4 1
include_once(__DIR__ . '/../../app/config/text-content.php');
5
6
/**
7
 * A page controller
8
 * Made by Rasmus Berg (c) 2014-2017
9
 *
10
 * @property  object  $di         Anax-MVC class handler
11
 * @property  object  $request    Anax-MVC $_POST, $_GET and $_SERVER handler 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 PageController implements \Anax\DI\IInjectionAware
18
{
19
  use \Anax\DI\TInjectable;
20
  
21
  /**
22
   * Properties
23
   */
24
  private $content;
25
  private $urlPrefix;
26
  
27
  /**
28
   * Initialize the controller
29
   *
30
   * @return    void
31
   */
32 View Code Duplication
  public function initialize(){
33
    if(!is_object($this->di))
34
      throw new \Anax\Exception\InternalServerErrorException('"$this->di" is not valid!');
35
    
36
    $this->content = new \Chp\TextContent\Content();
37
    $this->content->setDI($this->di);
38
    
39
    $this->urlPrefix = CHP_TC_URLPREFIX;
40
  }
41
  
42
  /**
43
   * Index content - Redirect to startpage
44
   *
45
   * @return    void
46
   */
47
  public function indexAction(){
48
    $url = $this->request->getGet('url');
49
    $this->pageAction($url);
50
  }
51
  
52
  /**
53
   * Visit page made by database content 
54
   *
55
   * @param   string    $url    Select url to content  
56
   * @return  void
57
   */
58
  public function pageAction($url = null){
59
    if(is_null($url))
60
      throw new \Anax\Exception\NotFoundException();
61
    
62
    $page = $this->content->getContentByUrl($url, 'page');
63
    
64
    if(empty($page))
65
      throw new \Anax\Exception\NotFoundException();
66
    
67
    // Prepare page for show in view
68
    $page = $this->preparePage($page);
69
    
70
    $title = $page->title;
71
    
72
    $this->theme->setTitle($title);
73
    $this->views->addString($title, 'page-title');
74
    $this->views->add('text-content/page', 
75
      [
76
        'title'         => $title,
77
        'page' 	        => $page
78
      ]
79
    );
80
  }
81
  
82
  /**
83
   * Prepare page to show in view 
84
   *
85
   * @param   object    $page    Page information  
86
   * @return  object    $result  Prepared page information
87
   */
88 1
  public function preparePage($page = null){
89
    $result = null;
90
    
91
    if(!is_null($page)){
92
      $result = (object)[];
93
      
94
      foreach($page as $key => $value){
95
        $result->{$key} = $value;
96
      }
97
      
98
      $result->title         = htmlspecialchars($page->title, ENT_QUOTES);
99
      $result->ingress       = htmlspecialchars($page->ingress, ENT_QUOTES);
100
      $result->text          = $this->textFilter->doFilter(htmlspecialchars($page->text, ENT_QUOTES), $page->filters);
101
      $result->editUrl       = $this->url->create("{$this->urlPrefix}content/edit/{$page->id}");
102
      //$result->authorId      = $page->author;
103
      //$result->authorName    = htmlspecialchars($page->name, ENT_QUOTES);
104
      //$result->authorUrl     = $this->url->create("{$this->urlPrefix}users/id/{$page->author}");
105
      
106
      //unset($result->author);
107
    }
108
    
109
    return $result;
110
  }
111
}