This repository was archived by the owner on Jul 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 140
/
Copy pathFileDeleteAction.php
141 lines (124 loc) · 4.24 KB
/
FileDeleteAction.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
/**
* @link https://github.com/2amigos/yii2-file-upload-widget
* @copyright Copyright (c) 2013-2019 2amigOS! Consulting Group LLC
* @license http://opensource.org/licenses/BSD-3-Clause
*/
namespace dosamigos\fileupload\actions;
use yii\base\Action;
use yii\base\InvalidConfigException;
use yii\db\ActiveRecord;
use yii\web\NotFoundHttpException;
use yii\web\Response;
use Yii;
/**
* FileDeleteAction handles file deletion
*
* @author Antonio Ramirez <[email protected]>
*/
class FileDeleteAction extends Action
{
/**
* @var string the AR class name that we need to delete
*/
public $className;
/**
* @var string the parameter name in the request. Defaults to 'id'.
*/
public $idParamName = 'id';
/**
* @var string the name of the table handling relations -ie city_picture_assn
*/
public $ownerLinkTable;
/**
* @var string the name of the attribute of the class we are going to delete -ie picture_id
*/
public $ownerLinkTableAttribute;
/**
* @var string|array the route to redirect after successful deletion. Note: If the user is doing and AJAX'ed
* request. Will simply return a JSON with `success` true or false. If `success` it will contain a `message`,
* otherwise will contain also `errors` key so you can display the issues on a notification plugin or whatever.
*/
public $redirectRoute = 'index';
/**
* @inheritdoc
* @throws InvalidConfigException
*/
public function init()
{
if (!isset($this->ownerLinkTable, $this->className)) {
throw new InvalidConfigException('"ownerLinkTable" and "className" attributes cannot be null');
}
parent::init();
}
/**
* @inheritdoc
*
* @return array|Response
* @throws NotFoundHttpException
*/
public function run()
{
$id = Yii::$app->request->get($this->idParamName);
if (!$id) {
throw new NotFoundHttpException(Yii::t('fileupload', 'Page not found'));
}
$model = call_user_func([$this->className, 'findOne'], $id);
$response = ['success' => $model->delete()];
if (Yii::$app->request->isPost) {
$shortClassName = (strpos($this->className, '\\') === false
? $this->className
: substr($this->className, strrpos($this->className, '\\') + 1));
if (Yii::$app->request->isAjax) { // handling AJAX'ed requests
Yii::$app->response->format = Response::FORMAT_JSON;
$response['success'] = $model->delete();
if ($response['success']) {
$this->unlink($id);
$response['message'] = $shortClassName . ' ' . Yii::t('fileupload', 'successfully removed.');
} else {
$response['message'] = Yii::t('fileupload', 'Unable to remove') . $shortClassName;
$response['errors'] = implode("\n", $this->getModelErrors($model));
}
return $response;
} else { // handling not AJAX'ed requests
if ($model->delete()) {
Yii::$app->session->addFlash(
'success',
$shortClassName . ' ' . Yii::t('fileupload', 'successfully removed.')
);
} else {
Yii::$app->session->addFlash('error', Yii::t('fileupload', 'Unable to remove') . $shortClassName);
}
}
}
return $this->controller->redirect($this->redirectRoute);
}
/**
* Removed
*
* @param mixed $id the id of the class to remove from link table
*/
protected function unlink($id)
{
ActiveRecord::getDb()
->createCommand()
->delete($this->ownerLinkTable, [$this->ownerLinkTableAttribute => $id])
->execute();
}
/**
* Helper function
*
* @param \yii\base\Model $model
*
* @return array
* @todo breaks DRY. Think of the best way to implement.
*/
protected function getModelErrors($model)
{
$errors = [];
foreach ($model->getFirstErrors() as $error) {
$errors[] = $error;
}
return $errors;
}
}