CFormAddPost::callbackFail()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace Anax\Blog;
4
5
/**
6
 * Anax base class for wrapping sessions.
7
 *
8
 */
9
class CFormAddPost extends \Mos\HTMLForm\CForm
10
{
11
    use \Anax\DI\TInjectionaware,
12
        \Anax\MVC\TRedirectHelpers;
13
	
14
	public $blog;
15
16
    /**
17
     * Constructor
18
     *
19
     */
20
    public function __construct()
21
    {
22
        parent::__construct([], [
23
			'author' => [
24
                'type'        => 'text',
25
                'label'       => 'Author:',
26
                'required'    => true,
27
                'validation'  => ['not_empty'],
28
            ],
29
			'title' => [
30
                'type'        => 'text',
31
                'label'       => 'Title:',
32
                'required'    => true,
33
                'validation'  => ['not_empty'],
34
            ],
35
            'content' => [
36
                'type'        => 'textarea',
37
                'label'       => 'Content:',
38
                'required'    => true,
39
                'validation'  => ['not_empty'],
40
            ],
41
			'filter' => [
42
                'type'        => 'text',
43
                'label'       => 'Filter:',
44
				'description' => 'separated by comma, no spaces',
45
            ],
46
            'submit' => [
47
                'type'      => 'submit',
48
                'callback'  => [$this, 'callbackSubmit'],
49
            ],
50
        ]);
51
    }
52
53
54
55
    /**
56
     * Customise the check() method.
57
     *
58
     * @param callable $callIfSuccess handler to call if function returns true.
59
     * @param callable $callIfFail    handler to call if function returns true.
60
     */
61
    public function check($callIfSuccess = null, $callIfFail = null)
62
    {
63
        return parent::check([$this, 'callbackSuccess'], [$this, 'callbackFail']);
64
    }
65
66
67
68
    /**
69
     * Callback for submit-button.
70
     *
71
     */
72
    public function callbackSubmit()
0 ignored issues
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...
73
    {
74
		$this->blog = new \Anax\Blog\Blog();
75
        $this->blog->setDI($this->di);
76
		
77
		$now = gmdate('Y-m-d H:i:s');
78
		
79
		$this->blog->save([
80
			'author' 	=> htmlentities($this->Value('author'), null, 'UTF-8'),
81
			'title' 	=> htmlentities($this->Value('title'), null, 'UTF-8'),
82
            'content'	=> htmlentities($this->Value('content'), null, 'UTF-8'),
83
			'slug'		=> $this->blog->slugify($this->Value('title')),
84
			'filter' 	=> $this->Value('filter') ? htmlentities($this->Value('filter')) : '',
85
			'created'	=> $now,
86
		]);
87
		
88
        return true;
89
    }
90
91
92
93
    /**
94
     * Callback What to do if the form was submitted?
95
     *
96
     */
97
    public function callbackSuccess()
98
    {
99
        $this->AddOUtput("<p><i>Form was submitted and the post was added successfully.</i></p>");
100
        $this->redirectTo('blog/list');
101
    }
102
103
104
105
    /**
106
     * Callback What to do when form could not be processed?
107
     *
108
     */
109
    public function callbackFail()
110
    {
111
        $this->AddOutput("<p><i>Form was submitted and the Check() method returned false.</i></p>");
112
        $this->redirectTo();
113
    }
114
}
115