1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Apps\Controller\Admin\Feedback; |
4
|
|
|
|
5
|
|
|
use Apps\ActiveRecord\FeedbackAnswer; |
6
|
|
|
use Apps\ActiveRecord\FeedbackPost; |
7
|
|
|
use Ffcms\Core\App; |
8
|
|
|
use Ffcms\Core\Arch\View; |
9
|
|
|
use Ffcms\Core\Exception\NotFoundException; |
10
|
|
|
use Ffcms\Core\Helper\Type\Any; |
11
|
|
|
use Ffcms\Core\Network\Request; |
12
|
|
|
use Ffcms\Core\Network\Response; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Trait ActionDelete |
16
|
|
|
* @package Apps\Controller\Admin\Feedback |
17
|
|
|
* @property Request $request |
18
|
|
|
* @property Response $response |
19
|
|
|
* @property View $view |
20
|
|
|
*/ |
21
|
|
|
trait ActionDelete |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Delete feedback post or answer |
25
|
|
|
* @param string $type |
26
|
|
|
* @param string $id |
27
|
|
|
* @return string |
28
|
|
|
* @throws \Exception |
29
|
|
|
*/ |
30
|
|
|
public function delete(string $type, string $id): ?string |
31
|
|
|
{ |
32
|
|
|
if (!Any::isInt($id) || $id < 1) { |
33
|
|
|
throw new NotFoundException('Bad id format'); |
34
|
|
|
} |
35
|
|
|
// try to get active record by type |
36
|
|
|
$record = null; |
37
|
|
|
switch ($type) { |
38
|
|
|
case 'post': |
|
|
|
|
39
|
|
|
$record = FeedbackPost::find($id); |
40
|
|
|
break; |
41
|
|
|
case 'answer': |
42
|
|
|
$record = FeedbackAnswer::find($id); |
43
|
|
|
break; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
// check if we get the row |
47
|
|
|
if ($record === null || $record === false) { |
48
|
|
|
throw new NotFoundException(__('Feedback item is not founded')); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
// if delete is submited |
52
|
|
|
if ($this->request->request->get('deleteFeedback')) { |
53
|
|
|
// remove all answers |
54
|
|
|
if ($type === 'post') { |
55
|
|
|
FeedbackAnswer::where('feedback_id', '=', $record->id)->delete(); |
56
|
|
|
// remove item |
57
|
|
|
$record->delete(); |
58
|
|
|
App::$Session->getFlashBag()->add('success', __('Feedback record is successful removed')); |
59
|
|
|
$this->response->redirect('feedback/index'); |
60
|
|
|
} else { |
61
|
|
|
// its a answer, lets remove it and redirect back in post |
62
|
|
|
$postId = $record->feedback_id; |
63
|
|
|
$record->delete(); |
64
|
|
|
$this->response->redirect('feedback/read/' . $postId); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
// render view |
69
|
|
|
return $this->view->render('delete', [ |
70
|
|
|
'type' => $type, |
71
|
|
|
'record' => $record |
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.