1 | <?php |
||
9 | class CFormUpdatePage 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($page) |
||
21 | { |
||
22 | if(!$page) { |
||
23 | die('Not a valid page!'); |
||
24 | } |
||
25 | |||
26 | $this->page = $page; |
||
27 | |||
28 | parent::__construct([], [ |
||
29 | 'id' => [ |
||
30 | 'type' => 'hidden', |
||
31 | 'value' => $page->id, |
||
32 | 'required' => true, |
||
33 | 'validation' => ['not_empty'], |
||
34 | ], |
||
35 | 'title' => [ |
||
36 | 'type' => 'text', |
||
37 | 'label' => 'Title:', |
||
38 | 'value' => $page->title, |
||
39 | 'required' => true, |
||
40 | 'validation' => ['not_empty'], |
||
41 | ], |
||
42 | 'content' => [ |
||
43 | 'type' => 'textarea', |
||
44 | 'label' => 'Content:', |
||
45 | 'value' => $page->content, |
||
46 | 'required' => true, |
||
47 | 'validation' => ['not_empty'], |
||
48 | ], |
||
49 | 'filter' => [ |
||
50 | 'type' => 'text', |
||
51 | 'label' => 'Filter:', |
||
52 | 'description' => 'separated by comma, no spaces', |
||
53 | 'value' => $page->filter, |
||
54 | ], |
||
55 | 'submit' => [ |
||
56 | 'type' => 'submit', |
||
57 | 'callback' => [$this, 'callbackSubmit'], |
||
58 | ], |
||
59 | ]); |
||
60 | } |
||
61 | |||
62 | |||
63 | |||
64 | /** |
||
65 | * Customise the check() method. |
||
66 | * |
||
67 | * @param callable $callIfSuccess handler to call if function returns true. |
||
68 | * @param callable $callIfFail handler to call if function returns true. |
||
69 | */ |
||
70 | public function check($callIfSuccess = null, $callIfFail = null) |
||
71 | { |
||
72 | return parent::check([$this, 'callbackSuccess'], [$this, 'callbackFail']); |
||
73 | } |
||
74 | |||
75 | |||
76 | |||
77 | /** |
||
78 | * Callback for submit-button. |
||
79 | * |
||
80 | */ |
||
81 | public function callbackSubmit() |
||
|
|||
82 | { |
||
83 | $this->page = new \Anax\Page\Page(); |
||
84 | $this->page->setDI($this->di); |
||
85 | |||
86 | $now = gmdate('Y-m-d H:i:s'); |
||
87 | |||
88 | $this->page->save([ |
||
89 | 'id' => $this->Value('id'), |
||
90 | 'title' => htmlentities($this->Value('title'), null, 'UTF-8'), |
||
91 | 'content' => htmlentities($this->Value('content'), null, 'UTF-8'), |
||
92 | 'slug' => $this->page->slugify($this->Value('title')), |
||
93 | 'filter' => $this->Value('filter') ? htmlentities($this->Value('filter')) : '', |
||
94 | 'updated' => $now, |
||
95 | ]); |
||
96 | |||
97 | return true; |
||
98 | } |
||
99 | |||
100 | |||
101 | |||
102 | /** |
||
103 | * Callback What to do if the form was submitted? |
||
104 | * |
||
105 | */ |
||
106 | public function callbackSuccess() |
||
111 | |||
112 | |||
113 | |||
114 | /** |
||
115 | * Callback What to do when form could not be processed? |
||
116 | * |
||
117 | */ |
||
118 | public function callbackFail() |
||
122 | } |
||
123 |
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.