Completed
Push — master ( 2387f6...83bda3 )
by Rasmus
02:08
created

ContentController   C

Complexity

Total Complexity 62

Size/Duplication

Total Lines 673
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 16.58%

Importance

Changes 0
Metric Value
wmc 62
lcom 1
cbo 4
dl 0
loc 673
ccs 64
cts 386
cp 0.1658
rs 5.2975
c 0
b 0
f 0

20 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 11 1
A indexAction() 0 3 1
B setupAction() 0 108 2
B listAction() 0 26 3
A addAction() 0 15 1
B editAction() 0 26 4
B removeAction() 0 36 5
B listForm() 0 31 3
D contentForm() 0 125 14
B confirmForm() 0 24 2
A saveContent() 0 16 3
B saveTags() 0 24 4
B prepareListContent() 0 22 5
B prepareSaveContent() 0 30 6
A prepareNewSlug() 0 8 3
A minimumLength() 0 3 1
A validateSlug() 0 3 1
A checkType() 0 3 1
A checkFilter() 0 3 1
A checkDatetime() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like ContentController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ContentController, and based on these observations, apply Extract Interface, too.

1
<?php
2
namespace Chp\TextContent;
3
4 1
include_once(__DIR__ . '/../../app/config/text-content.php');
5
6
/**
7
 * Text content 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  $response   Anax-MVC Php Header class
13
 * @Property  Object  $url        Anax-MVC url-handler class
14
 * @Property  Object  $theme      Anax-MVC theme-handler class
15
 * @Property  Object  $views      Anax-MVC views-handler class
16
 * @Property  Object  $textFilter Anax-MVC textformat-handler class
17
 * @Property  Object  $db         PDO database class
18
 */
19
class ContentController implements \Anax\DI\IInjectionAware
20
{
21
  use \Anax\DI\TInjectable;
22
  
23
  /**
24
	 * Properties
25
	 */
26
  private $content        = null;
27
  private $contentPerPage; // Limit contents on list page
28
  private $urlPrefix;
29
  private $miniLength;
30
  
31
  /**
32
   * Initialize the controller
33
   *
34
   * @Return    Void
35
   */
36 7
  public function initialize(){
37 7
    $this->content = new \Chp\TextContent\Content();
38 7
    $this->content->setDI($this->di);
39 7
    $this->valid = new \Chp\TextContent\ValidContent();
0 ignored issues
show
Bug introduced by
The property valid does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
40 7
    $this->valid->setDI($this->di);
0 ignored issues
show
Bug introduced by
It seems like $this->di can also be of type array or null; however, Anax\DI\TInjectable::setDI() does only seem to accept object<Anax\DI\class>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
41 7
    $this->valid->initialize();
42
    
43 7
    $this->contentPerPage = CHP_TC_CONTENTPERPAGE;
44 7
    $this->urlPrefix      = CHP_TC_URLPREFIX;
45 7
    $this->miniLength     = CHP_TC_MINILENGTH;
46 7
  }
47
  
48
  /**
49
	 *  Index content - use listAction 
50
   *
51
	 * @Returns		Void
52
	 */
53
	public function indexAction(){
54
    $this->listAction();
55
  }
56
  
57
  /**
58
   * Setup content (If it allready exist it will restore database to begining)
59
   *
60
   * @Return    Void
61
   */
62
  public function setupAction(){
63
    $toDo   = "Restore or setup";
64
    $toWhat = "content database tables";
65
    $title  = "{$toDo} {$toWhat}";
66
    $form   = $this->confirmForm($this->url->create($this->urlPrefix . 'content/'));
67
    $status = $form->check();
68
 
69
    if($status === true){
70
    
71
      $this->db->dropTableIfExists('content')->execute();
72
73
      $this->db->createTable(
74
        'content',
75
        [
76
          'id'        => ['integer', 'primary key', 'not null', 'auto_increment'],
77
          'slug'      => ['char(80)'],
78
          'url'       => ['char(80)'],
79
          'type'      => ['char(80)'],
80
          'title'     => ['varchar(80)'],
81
          'ingress'   => ['text'],
82
          'text'      => ['text'],
83
          'filters'   => ['char(80)'],
84
          'author'    => ['integer', 'not null'],
85
          'published' => ['datetime'],
86
          'created'   => ['datetime'],
87
          'updated'   => ['datetime'],
88
          'deleted'   => ['datetime']
89
        ]
90
      )->execute();
91
      
92
      $this->db->insert(
93
        'content',
94
        ['slug', 'url', 'type', 'title', 'ingress', 'text', 'filters', 'author', 'published', 'created']
95
      );
96
97
      $now = date('Y-m-d H:i:s');
98
99
      $this->db->execute([
0 ignored issues
show
Documentation introduced by
The property db does not exist on object<Chp\TextContent\ContentController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
100
        'welcome_to_your_new_blog',
101
        NULL,
102
        'blog-post',
103
        'Welcome to your new blog',
104
        'This is example-blogg to show your new blog system!',
105
        'You can start blog by visit [url=' . $this->url->create($this->urlPrefix . "content/add/") . ']Add content[/url]',
106
        'bbcode',
107
        3,
108
        $now,
109
        $now
110
      ]);
111
      
112
      $this->db->execute([
0 ignored issues
show
Documentation introduced by
The property db does not exist on object<Chp\TextContent\ContentController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
113
        'welcome_to_your_new_page',
114
        'example-page',
115
        'page',
116
        'Welcome to your new page',
117
        'This is example-page to show your new page system!',
118
        'You can start makeing pages by visit [url=' . $this->url->create($this->urlPrefix . "content/add/") . ']Add content[/url]',
119
        'bbcode',
120
        3,
121
        $now,
122
        $now
123
      ]);
124
      
125
      $this->db->dropTableIfExists('content_tags')->execute();
126
      
127
      $this->db->createTable(
128
        'content_tags',
129
        [
130
          'idContent' => ['integer', 'not null'],
131
          'tag'       => ['varchar(150)', 'not null'],
132
          'slug'      => ['varchar(150)', 'not null']
133
        ]
134
      )->execute();
135
      
136
      $this->db->insert(
137
        'content_tags',
138
        ['idContent', 'tag', 'slug']
139
      );
140
      
141
      $this->db->execute([
0 ignored issues
show
Documentation introduced by
The property db does not exist on object<Chp\TextContent\ContentController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
142
        1,
143
        'New blog',
144
        'new_blog'
145
      ]);
146
147
      $this->db->execute([
0 ignored issues
show
Documentation introduced by
The property db does not exist on object<Chp\TextContent\ContentController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
148
        1,
149
        'Blog information',
150
        'blog_information'
151
      ]);
152
      
153
      $this->views->add('text-content/action-finish', [
154
          'title'   => $title,
155
          'msg'     => "Content and content tags database tables " . strtolower($toDo). " was successful!"
156
      ], 'main');
157
      
158
    }
159
    else{      
160
      $this->views->add('text-content/action', [
0 ignored issues
show
Documentation introduced by
The property views does not exist on object<Chp\TextContent\ContentController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
161
          'title'   => $title,
162
          'toDo'    => strtolower($toDo),
163
          'toWhat'  => $toWhat,
164
          'form'    => $form->getHTML(['novalidate' => true])
165
      ], 'main');
166
    }
167
    
168
    $this->theme->setTitle($title);
0 ignored issues
show
Documentation introduced by
The property theme does not exist on object<Chp\TextContent\ContentController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
169
  }
170
  
171
  /**
172
	 *  List content (by type)
173
   *
174
	 * @Param     String    $type	  Type to list
175
   * @Param     Integer   $page   Page that paging is on
176
	 * @Return		Void
177
	 */
178
	public function listAction($type = null, $published = false, $page = null){
179
    $type      = ($this->valid->checkType($type)) ? $type : null;
180
    $title     = "All Content {$type} listed";
181
    $published = boolval($published);
182
183
    $form   = $this->listForm($type, $published);
184
    $form->check();
185
    
186
    if(!is_null($type))
187
      $contents = $this->content->getAllContentOfType($type, $page, $this->contentPerPage, $published);
188
    else
189
      $contents = $this->content->getAllContent($page, $this->contentPerPage, $published);
190
    $contents = $this->prepareListContent($contents);
191
    
192
    $this->theme->setTitle($title);
0 ignored issues
show
Documentation introduced by
The property theme does not exist on object<Chp\TextContent\ContentController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
193
    $this->views->add('text-content/list', 
194
      [
195
        'title'     => $title,
196
        'form'      => $form->getHTML(array('novalidate' => true)),
197
        'contents' 	=> $contents,
198
        'addUrl'	  => $this->url->create($this->urlPrefix . 'content/add/'),
199
        'setupUrl'  => $this->url->create($this->urlPrefix . 'content/setup/')
200
      ]
201
    );
202
203
  }
204
  
205
  /**
206
   * Add content to database
207
   *
208
   * @Return    Void
209
   */
210
  public function addAction(){
211
    $action = "Add";
212
    $title = "{$action} content";
213
    
214
    $form = $this->contentForm();
215
    $form->check();
216
    
217
    $this->theme->setTitle($title);
0 ignored issues
show
Documentation introduced by
The property theme does not exist on object<Chp\TextContent\ContentController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
218
    $this->views->add('text-content/post', 
0 ignored issues
show
Documentation introduced by
The property views does not exist on object<Chp\TextContent\ContentController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
219
      [
220
        'title'  => $title,
221
        'action' => $action,
222
        'form'   => $form->getHTML(array('novalidate' => true))
223
      ]);
224
  }
225
  
226
  /**
227
   * Edit content in database
228
   *
229
   * @Param     Integer   $id   Index for content to edit
230
   * @Return    Void
231
   */
232
  public function editAction($id = null){
233
    $action = "Edit";
234
    $title  = "{$action} content";
235
    $url    = $this->url->create($this->urlPrefix . 'content/');
236
    
237
    if(is_null($id) || !is_numeric($id))
238
      $this->response->redirect($url);
0 ignored issues
show
Documentation introduced by
The property response does not exist on object<Chp\TextContent\ContentController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
239
    
240
    $content = $this->content->getContentById($id, false);
241
    
242
    if(is_null($content->id))
243
      $this->response->redirect($url);
0 ignored issues
show
Documentation introduced by
The property response does not exist on object<Chp\TextContent\ContentController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
244
    
245
    $form = $this->contentForm($content);
246
    $form->check();
247
    
248
    $this->theme->setTitle($title);
0 ignored issues
show
Documentation introduced by
The property theme does not exist on object<Chp\TextContent\ContentController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
249
    
250
    $this->views->add('text-content/post', 
0 ignored issues
show
Documentation introduced by
The property views does not exist on object<Chp\TextContent\ContentController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
251
      [
252
        'title'  => $title,
253
        'action' => $action,
254
        'form'   => $form->getHTML(array('novalidate' => true))
255
      ]
256
    );
257
  }
258
	
259
	/**
260
	 * Remove content
261
	 *
262
   * @Param   Integer  $id      Index to content to remove
263
	 * @Return  String   $error  	Database-error msg
264
	 */
265
	public function removeAction($id = null){
266
		$action = "Delete";
267
    $title = "Delete content";
268
    $url = $this->url->create($this->urlPrefix . "content/");
269
    
270
    if(is_null($id) || !is_numeric($id)){
271
			$this->response->redirect($url);
0 ignored issues
show
Documentation introduced by
The property response does not exist on object<Chp\TextContent\ContentController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
272
		}
273
    
274
    $content = $this->content->find($id);
275
    
276
    if(is_null($content->id)){
277
      $this->response->redirect($url);
0 ignored issues
show
Documentation introduced by
The property response does not exist on object<Chp\TextContent\ContentController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
278
    }
279
    
280
    $form = $this->confirmForm($url);
281
    $status = $form->Check();
282
    
283
    if ($status === true) {
284
      $now = date('Y-m-d H:i:s');
285
      
286
      $content->deleted = $now;
287
      $content->save();
288
      
289
      $this->response->redirect($url);
0 ignored issues
show
Documentation introduced by
The property response does not exist on object<Chp\TextContent\ContentController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
290
    }
291
    
292
    $this->theme->setTitle($title);
0 ignored issues
show
Documentation introduced by
The property theme does not exist on object<Chp\TextContent\ContentController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
293
    $this->views->add('text-content/action', [
0 ignored issues
show
Documentation introduced by
The property views does not exist on object<Chp\TextContent\ContentController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
294
      'title'  => $title,
295
      'toDo'    => strtolower($action),
296
      'toWhat'  => strtolower($this->valid->getTypeTitle($content->type)),
297
      'which'   => htmlentities($content->title),
298
      'form'    => $form->getHTML(['novalidate' => true]),
299
    ], 'main');
300
	}
301
  
302
  /**
303
   * Prepare form to add or edit content
304
   *
305
   * @Param   String    $type       Selected content-type
306
   * @Param   Boolean   $published  If content should be published already
307
   * @Return  Object    $form       CForm object
308
   */
309
  private function listForm($type = null, $published = false){
310
    
311
    $type_options      = array_merge([0 => "Select a type of content"], $this->valid->getTypes());
312
    
313
    $form = new \Mos\HTMLForm\CForm([], [
314
        'type' => [
315
          'type'        => 'select',
316
          'label'       => 'Type of content:',
317
          'options'     => $type_options,
318
          'value'       => (isset($type)) ? $type : ''
319
        ],
320
        'published' => [
321
          'type'        => 'checkbox',
322
          'label'       => 'Published',
323
          'checked'     => $published,
324
          'value'       => 1
325
        ],
326
        'submit' => [
327
          'value' 		=> 'Filter',
328
          'type'      => 'submit',
329
          'callback'  => function ($form) {
330
            $published = ($this->request->getPost('published')) ? 1 : 0;
331
            $type      = $form->Value('type');
332
            
333
            $this->response->redirect($this->url->create($this->urlPrefix . "content/list/{$type}/{$published}"));
334
          }
335
        ]
336
    ]);
337
    
338
    return $form;
339
  }
340
  
341
  /**
342
   * Prepare form to add or edit content
343
   *
344
   * @Param   Object    $values     Content values to add form elements
345
   * @Return  Object    $form       CForm object
346
   */
347 1
  private function contentForm($values = null){
348
    if(isset($values) && is_object($values)){
349
      $valArr = get_object_vars($values);
350
      extract($valArr);
351
    }
352
    
353
    $slug = (isset($slug)) ? $slug : NULL;
354
    
355
    $type_options      = $this->valid->getTypes();
356
    
357
    $form = new \Mos\HTMLForm\CForm([], [
358
        'id'      => [
359
          'type'        => 'hidden',
360
          'value'       => (isset($id)) ? $id : 0
361
        ],
362
        'title' => [
363
          'type'        => 'text',
364
          'label'       => 'Title: (Between ' . $this->miniLength . ' to 80 chars)',
365
          'maxlength'   => 80,
366
          'required'    => true,
367
          'value'       => (isset($title)) ? $title : '',
368
          'validation'  => [
369
                              'custom_test' => array(
370
                                'message' => 'Minmum length is ' . $this->miniLength . ' chars.', 
371
                                'test'    => array($this, 'minimumLength')
372
                              )
373
                           ]
374
        ],
375
        'url' => [
376
          'type'        => 'text',
377
          'label'       => 'Url:',
378
          'maxlength'   => 80,
379
          'value'       => (isset($url)) ? $url : '',
380 1
          'required'    => false, 
381
          'validation'  => [
382
                              'custom_test' => array(
383
                                'message' => 'Url is not in accepted-format for url:s (accepted characters is \'a-z0-9-_()\').', 
384
                                'test'    => array($this, 'validateSlug')
385
                              )
386
                           ]
387
        ],
388
        'ingress' => [
389
          'type'        => 'textarea',
390
          'label'       => 'Ingress: (minimum 3 chars)',
391
          'value'       => (isset($ingress)) ? $ingress : '',
392
          'required'    => true,
393
          'validation'  => [
394
                              'custom_test' => array(
395
                                'message' => 'Minmum length is ' . $this->miniLength . ' chars.', 
396
                                'test'    => array($this, 'minimumLength')
397
                              )
398
                           ]
399
        ],
400
        'text' => [
401
          'type'        => 'textarea',
402
        	'label'       => 'Text: (minimum ' . $this->miniLength . ' chars)',
403
          'value'       => (isset($text)) ? $text : '',
404
          'required'    => true,
405
          'validation'  => [
406
                            'custom_test' => array(
407
                              'message' => 'Minmum length is ' . $this->miniLength . ' chars.', 
408
                              'test'    => array($this, 'minimumLength')
409
                            )
410
                           ]
411
        ],
412
        'type' => [
413
          'type'        => 'select',
414
          'label'       => 'Type of content:',
415
          'options'     => $type_options,
416
          'value'       => (isset($type)) ? $type : '',
417
          'required'    => true,
418
          'validation'  => [
419
                              'not_empty',
420
                              'custom_test' => array(
421
                                'message' => 'You need to select a existing type.', 
422
                                'test'    => array($this, 'checkType')
423
                              )
424
                           ]
425
        ],
426
        'tags' => [
427
          'type'        => 'text',
428
          'label'       => 'Tags: (Seperate by \',\')',
429
          'value'       => (isset($tags)) ? $tags : '',
430
          'required'    => false
431
        ],
432
        'filters' => [
433
          'type'        => 'checkbox-multiple',
434
          'label'       => 'Text filter:',
435
          'values'      => $this->valid->getFilters(),
436
          'checked'     => (isset($filters)) ? explode(',', $filters) : array(),
437
          'validation'  => [
438
                              'custom_test' => array(
439
                                'message' => 'You need to select a existing type.', 
440
                                'test'    => array($this, 'checkFilter')
441
                              )
442
                           ]
443
        ],
444
        'published' => [
445
          'type'        => 'datetime',
446
          'label'       => 'Published: (YYYY-MM-DD HH:MM:SS)',
447
          'value'       => (isset($published)) ? $published : '',
448
          'validation'  => [
449
                              'custom_test' => array(
450
                                'message' => 'It need to be in a correct date and time with format: YYYY-MM-DD HH:MM:SS.', 
451
                                'test'    => array($this, 'checkDatetime')
452
                              )
453
                           ]
454
        ],
455
        'publishedNow' => [
456
          'type'        => 'checkbox',
457
          'label'       => 'Publish now:',
458
          'checked'     => (!isset($published) && !isset($id)),
459
          'value'       => 'yes'
460
        ],
461
        'submit' => [
462
          'value' 		=> 'Save',
463
          'type'      => 'submit',
464
          'callback'  => function ($form) use($slug) {
465
            return $this->saveContent($form, $slug);
466
          }
467
        ]
468
    ]);
469
    
470
    return $form;
471
  }
472
  
473
  /**
474
   * Prepare confirmation form
475
   *
476
   * @Param   String   $returnUrl       Return url
477
   * @Return  Object   $form            Form-object
478
   */
479
  public function confirmForm($returnUrl = null){
480
    $returnUrl = (isset($returnUrl)) ? $returnUrl : $this->request->getBaseUrl();
481
    
482
    $form = new \Mos\HTMLForm\CForm([], [
483
        'submit' => [
484
          'type'     => 'submit',
485
          'value'    => 'Yes',
486
          'callback' => function() {
487
            return true;
488
          }
489
        ],
490
        'submit-no' => [
491
          'type'     => "submit",
492
          'value'    => 'No',
493
          'callback' => function() use($returnUrl) {
494
            $this->response->redirect($returnUrl);
495
          }
496
        ]
497
      ]
498
    );
499
		
500
    // Check the status of the form
501
    return $form;
502
  }
503
  
504
  /**
505
   * Save content to database
506
   *
507
   * @Param   Object    $form     Form object
508
   * @Param   String    $oldSlug  Old slug for content to compare
509
   * @Return  Boolean   false     If saving fail, return false
510
   */
511
  private function saveContent($form, $oldSlug = null){
512
    // Prepare content for saving
513
    $content = $this->prepareSaveContent($form, $oldSlug);
514
    
515
    // Save content
516
    $content = $this->content->save($content);
517
    
518
    // Saving fail
519
    if(!$this->content->id)
520
      return false;
521
    // Save tags for content to database
522
    else if($content['id'] != 0)
523
      $this->saveTags($form->Value('tags'), $this->content->id);
524
    
525
    $this->response->redirect($this->url->create($this->urlPrefix . "content/"));
526
  }
527
528
  /**
529
   * Save tags for content to database
530
   *
531
   * @Param   String   $tags     Tags for content
532
   * @Param   Int      $id       Content index
533
   */
534
  private function saveTags($tags, $id){
535
    $this->db->delete(
536
      'content_tags',
537
      'idContent = ?'
538
    )->execute([$id]);
539
    
540
    $this->db->insert(
541
      'content_tags',
542
      ['idContent', 'tag', 'slug']
543
    );
544
    
545
    if(isset($tags) && !is_null($tags)){
546
      $tagsArr = explode(",", $tags);
547
      
548
      foreach($tagsArr as $tag){
549
        $tag = trim($tag);
550
        $this->db->execute([
0 ignored issues
show
Documentation introduced by
The property db does not exist on object<Chp\TextContent\ContentController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
551
          $id,
552
          $tag,
553
          $this->valid->slugify($tag)
554
        ]);
555
      }
556
    }
557
  }
558
  
559
  /**
560
   * Prepare contents for show in list view
561
   *
562
   * @Param   Object  $contents   Object with content objects
563
   * @Return  Array   $results    Array with prepare content objects
564
   */
565 1
  public function prepareListContent($contents){
566 1
    $results = array();
567
    
568 1
    foreach($contents AS $key => $content){
569 1
      $available = $this->valid->checkIfAvailable($content->published);
570 1
      $results[$key] = (object)[];
571
      
572 1
      foreach($content as $key2 => $value){
573 1
        $results[$key]->{$key2} = $value;
574 1
      }
575
      
576 1
      $results[$key]->typeTxt      = $this->valid->getTypeTitle($content->type);
577 1
      $results[$key]->title        = htmlspecialchars($content->title, ENT_QUOTES);
578 1
      $results[$key]->editUrl      = $this->url->create($this->urlPrefix . "content/edit/{$content->id}");
579 1
      $results[$key]->removeUrl    = $this->url->create($this->urlPrefix . "content/remove/{$content->id}");
580 1
      $results[$key]->showUrl      = $this->valid->getUrlToContent($content);
581 1
      $results[$key]->available    = ((!$available) ? "not-" : null) . "published";
582 1
      $results[$key]->publishedTxt  = ($available) ? $contents[$key]->published : "Not published yet";
583 1
    }
584
    
585 1
    return $results;
586
  }
587
  /**
588
   * Prepare save of content to database
589
   *
590
   * @Param   Object    $form     Form object
591
   * @Param   String    $oldSlug  Old slug for content to compare
592
   * @Return  Array     $content  Prepare content array
593
   */  
594 1
  public function prepareSaveContent($form, $oldSlug = null){
595 1
    $now = date('Y-m-d H:i:s');
596
    
597
    // Prepare new slug
598 1
    $newSlug = $this->prepareNewSlug($form->Value('title'), $form->Value('type'), $oldSlug);
599
    
600
    $content = array(
601 1
      'title'     => $form->Value('title'),
602 1
      'slug'      => $newSlug,
603 1
      'url'       => $this->valid->slugify($form->Value('url')),
604 1
      'ingress'   => $form->Value('ingress'),
605 1
      'text'      => $form->Value('text'),
606 1
      'type'      => $form->Value('type'),
607 1
      'filters'   => ($form->Value('filters')) ? implode(",", $form->Value('filters')) : '',
608 1
      'published' => ($form->Value('publishedNow') && $form->Value('publishedNow') == 'yes') ? $now : $form->Value('published')
609 1
    );
610
    
611 1
    $id = ($form->Value('id')) ? intval($form->Value('id')) : 0;
612
    
613 1
    if($id != 0){
614 1
      $content['updated'] = $now;
615 1
      $content['id']      = $id;
616 1
    }
617
    else{
618 1
      $content['created'] = $now;
619 1
      $content['author']  = 0;//$this->user->getUserId();
620
    }
621
    
622 1
    return $content;
623
  }
624
  
625
  /**
626
   * Prepare new slug for content by title
627
   *
628
   * @Param   String    $title      Content title to make slug by
629
   * @Param   String    $type       Content type
630
   * @Param   String    $oldSlug    Old slug for content to compare
631
   * @Return  String    $newSlug    New unique slug for content
632
   */
633 1
  public function prepareNewSlug($title, $type, $oldSlug = null){
634 1
    $newSlug = $this->valid->slugify($title);
635
    
636 1
    if($oldSlug != $newSlug && isset($newSlug))
637 1
      $newSlug = $this->content->makeSlugToContent($newSlug, $type);
638
    
639 1
    return $newSlug;
640
  }
641
  
642
  /**
643
   * Validate minimum length
644
   *
645
   * @Param   String    $text       Text to validate
646
   * @Return  Boolean   True/False  Validate status
647
   */
648 1
  public function minimumLength($text = null){
649 1
    return $this->valid->minimumLength($text);
650
  }
651
  
652
  /**
653
   * Validate slug
654
   *
655
   * @Param   String    $slug       Slug to validate
656
   * @Return  Boolean   True/False  Validate status
657
   */
658 1
  public function validateSlug($slug = null){
659 1
    return $this->valid->validateSlug($slug);
660
  }
661
  
662
  /**
663
   * Validate type
664
   *
665
   * @Param   String    $type   Type to validate
666
   * @Return  Boolean   True/False  Validate status
667
   */
668 1
  public function checkType($type = null){
669 1
    return $this->valid->checkType($type);
670
  }
671
  
672
  /**
673
   * Validate filter
674
   *
675
   * @Param   String    $filter   Filter to validate
676
   * @Return  Boolean   True/False  Validate status
677
   */
678 1
  public function checkFilter($filter = null){
679 1
    return $this->valid->checkFilter($filter);
680
  }
681
  
682
  /**
683
   * Validate date time
684
   *
685
   * @Param   String    $datetime   Date time to validate
686
   * @Return  Boolean   True/False  Validate status
687
   */
688 1
  public function checkDatetime($datetime = null){
689 1
    return $this->valid->checkDatetime($datetime);
690
  }
691
}