1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Apps\Controller\Admin\Feedback; |
4
|
|
|
|
5
|
|
|
use Apps\ActiveRecord\FeedbackAnswer; |
6
|
|
|
use Apps\ActiveRecord\FeedbackPost; |
7
|
|
|
use Apps\Model\Admin\Feedback\FormUpdate; |
8
|
|
|
use Ffcms\Core\App; |
9
|
|
|
use Ffcms\Core\Arch\View; |
10
|
|
|
use Ffcms\Core\Exception\NotFoundException; |
11
|
|
|
use Ffcms\Core\Helper\Type\Any; |
12
|
|
|
use Ffcms\Core\Network\Request; |
13
|
|
|
use Ffcms\Core\Network\Response; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Trait ActionUpdate |
17
|
|
|
* @package Apps\Controller\Admin\Feedback |
18
|
|
|
* @property Request $request |
19
|
|
|
* @property Response $response |
20
|
|
|
* @property View $view |
21
|
|
|
*/ |
22
|
|
|
trait ActionUpdate |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Edit feedback post or answer |
26
|
|
|
* @param string $type |
27
|
|
|
* @param string $id |
28
|
|
|
* @return string |
29
|
|
|
* @throws NotFoundException |
30
|
|
|
* @throws \Ffcms\Core\Exception\SyntaxException |
31
|
|
|
*/ |
32
|
|
|
public function update(string $type, string $id): ?string |
33
|
|
|
{ |
34
|
|
|
if (!Any::isInt($id) || $id < 1) { |
35
|
|
|
throw new NotFoundException('Bad id format'); |
36
|
|
|
} |
37
|
|
|
// get active record based on type (post or answer for post) |
38
|
|
|
$record = null; |
39
|
|
|
$postId = $id; |
40
|
|
|
switch ($type) { |
41
|
|
|
case 'post': |
|
|
|
|
42
|
|
|
$record = FeedbackPost::find($id); |
43
|
|
|
break; |
44
|
|
|
case 'answer': |
45
|
|
|
$record = FeedbackAnswer::find($id); |
46
|
|
|
if ($record !== null && $record !== false) { |
47
|
|
|
$postId = (int)$record->getFeedbackPost()->id; |
48
|
|
|
} |
49
|
|
|
break; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
// try what we got |
53
|
|
|
if ($record === null || $record === false) { |
54
|
|
|
throw new NotFoundException(__('Feedback item is not founded')); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// initialize model |
58
|
|
|
$model = new FormUpdate($record); |
59
|
|
|
if ($model->send()) { |
60
|
|
|
if ($model->validate()) { |
61
|
|
|
$model->make(); |
62
|
|
|
App::$Session->getFlashBag()->add('success', __('Feedback item are successful changed')); |
63
|
|
|
$this->response->redirect('feedback/read/' . $postId); |
64
|
|
|
} else { |
65
|
|
|
App::$Session->getFlashBag()->add('danger', __('Updating is failed')); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
// render output view |
70
|
|
|
return $this->view->render('update', [ |
71
|
|
|
'model' => $model |
72
|
|
|
]); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next
break
.There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.