CFormUpdatePost::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 48
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

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