1 | <?php |
||
9 | class CFormAddPage extends \Mos\HTMLForm\CForm |
||
10 | { |
||
11 | use \Anax\DI\TInjectionaware, |
||
12 | \Anax\MVC\TRedirectHelpers; |
||
13 | |||
14 | public $page; |
||
15 | |||
16 | /** |
||
17 | * Constructor |
||
18 | * |
||
19 | */ |
||
20 | public function __construct() |
||
21 | { |
||
22 | parent::__construct([], [ |
||
23 | 'title' => [ |
||
24 | 'type' => 'text', |
||
25 | 'label' => 'Title:', |
||
26 | 'required' => true, |
||
27 | 'validation' => ['not_empty'], |
||
28 | ], |
||
29 | 'content' => [ |
||
30 | 'type' => 'textarea', |
||
31 | 'label' => 'Content:', |
||
32 | 'required' => true, |
||
33 | 'validation' => ['not_empty'], |
||
34 | ], |
||
35 | 'filter' => [ |
||
36 | 'type' => 'text', |
||
37 | 'label' => 'Filter:', |
||
38 | 'description' => 'separated by comma, no spaces', |
||
39 | ], |
||
40 | 'submit' => [ |
||
41 | 'type' => 'submit', |
||
42 | 'callback' => [$this, 'callbackSubmit'], |
||
43 | ], |
||
44 | ]); |
||
45 | } |
||
46 | |||
47 | |||
48 | |||
49 | /** |
||
50 | * Customise the check() method. |
||
51 | * |
||
52 | * @param callable $callIfSuccess handler to call if function returns true. |
||
53 | * @param callable $callIfFail handler to call if function returns true. |
||
54 | */ |
||
55 | public function check($callIfSuccess = null, $callIfFail = null) |
||
56 | { |
||
57 | return parent::check([$this, 'callbackSuccess'], [$this, 'callbackFail']); |
||
58 | } |
||
59 | |||
60 | |||
61 | |||
62 | /** |
||
63 | * Callback for submit-button. |
||
64 | * |
||
65 | */ |
||
66 | public function callbackSubmit() |
||
67 | { |
||
68 | $this->page = new \Anax\Page\Page(); |
||
69 | $this->page->setDI($this->di); |
||
70 | |||
71 | $now = gmdate('Y-m-d H:i:s'); |
||
72 | |||
73 | $this->page->save([ |
||
74 | 'title' => htmlentities($this->Value('title'), null, 'UTF-8'), |
||
75 | 'content' => htmlentities($this->Value('content'), null, 'UTF-8'), |
||
76 | 'slug' => $this->page->slugify($this->Value('title')), |
||
77 | 'filter' => $this->Value('filter') ? htmlentities($this->Value('filter')) : '', |
||
78 | 'created' => $now, |
||
79 | ]); |
||
80 | |||
81 | return true; |
||
82 | } |
||
83 | |||
84 | |||
85 | |||
86 | /** |
||
87 | * Callback What to do if the form was submitted? |
||
88 | * |
||
89 | */ |
||
90 | public function callbackSuccess() |
||
95 | |||
96 | |||
97 | |||
98 | /** |
||
99 | * Callback What to do when form could not be processed? |
||
100 | * |
||
101 | */ |
||
102 | public function callbackFail() |
||
107 | } |
||
108 |