Completed
Push — master ( 83bda3...00ca69 )
by Rasmus
02:04
created

PageController::preparePage()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 13
cts 13
cp 1
rs 9.0856
c 0
b 0
f 0
cc 3
eloc 11
nc 2
nop 1
crap 3
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 = null;
25
  private $urlPrefix = CHP_TC_URLPREFIX;
26
  
27
  /**
28
   * Initialize the controller
29
   *
30
   * @Return    Void
31
   */
32 View Code Duplication
  public function initialize(){
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
  
40
  /**
41
   * Index content - Redirect to startpage
42
   *
43
   * @Return    Void
44
   */
45
  public function indexAction(){
46
    $url = $this->request->getGet('url');
47
    $this->pageAction($url);
48
  }
49
  
50
  /**
51
   * Visit page made by database content 
52
   *
53
   * @Param   String    $url    Select url to content  
54
   * @Return  Void
55
   */
56
  public function pageAction($url = null){
57
    if(is_null($url))
58
      throw new \Anax\Exception\NotFoundException();
59
    
60
    $page = $this->content->getContentByUrl($url, 'page');
61
    
62
    if(empty($page))
63
      throw new \Anax\Exception\NotFoundException();
64
    
65
    // Prepare page for show in view
66
    $page = $this->preparePage($page);
67
    
68
    $title = $page->title;
69
    
70
    $this->theme->setTitle($title);
71
    $this->views->addString($title, 'page-title');
72
    $this->views->add('text-content/page', 
73
      [
74
        'title'         => $title,
75
        'page' 	        => $page
76
      ]
77
    );
78
  }
79
  
80
  /**
81
   * Prepare page to show in view 
82
   *
83
   * @Param   Object    $page    Page information  
84
   * @Return  Object    $result  Prepared page information
85
   */
86 1
  public function preparePage($page = null){
87 1
    $result = null;
88
    
89 1
    if(!is_null($page)){
90 1
      $result = (object)[];
91
      
92 1
      foreach($page as $key => $value){
93 1
        $result->{$key} = $value;
94 1
      }
95
      
96 1
      $result->title         = htmlspecialchars($page->title, ENT_QUOTES);
97 1
      $result->ingress       = htmlspecialchars($page->ingress, ENT_QUOTES);
98 1
      $result->text          = $this->textFilter->doFilter(htmlentities($page->text, ENT_QUOTES), $page->filters);
99 1
      $result->editUrl       = $this->url->create("content/edit/{$page->id}");
100
      //$result->authorId      = $page->author;
101
      //$result->authorName    = htmlentities($page->name, ENT_QUOTES);
102
      //$result->authorUrl     = $this->url->create('users/id/' . $page->author);
103
      
104
      //unset($result->author);
105 1
    }
106
    
107 1
    return $result;
108
  }
109
}