Commit dd3ef6219b413b33af18d3fa8cd55fed882bfdc9
1 parent
f51c0b7c
- delete books
Showing
1 changed file
with
21 additions
and
0 deletions
 
Show diff stats
backend/controllers/BookController.php
| ... | ... | @@ -103,4 +103,25 @@ | 
| 103 | 103 | throw new NotFoundHttpException('The requested page does not exist.'); | 
| 104 | 104 | } | 
| 105 | 105 | } | 
| 106 | + | |
| 107 | + public function actionDelete($id) | |
| 108 | + { | |
| 109 | + if (file_exists(\Yii::getAlias('@storage/books/') . $id)) { | |
| 110 | + $this->removeDirectory(\Yii::getAlias('@storage/books/') . $id); | |
| 111 | + } | |
| 112 | + $this->findModel($id) | |
| 113 | + ->delete(); | |
| 114 | + | |
| 115 | + return $this->redirect([ 'index' ]); | |
| 116 | + } | |
| 117 | + | |
| 118 | + public static function removeDirectory($dir) | |
| 119 | + { | |
| 120 | + if ($objs = glob($dir . "/*")) { | |
| 121 | + foreach ($objs as $obj) { | |
| 122 | + is_dir($obj) ? self::removeDirectory($obj) : unlink($obj); | |
| 123 | + } | |
| 124 | + } | |
| 125 | + rmdir($dir); | |
| 126 | + } | |
| 106 | 127 | } | 
| 107 | 128 | \ No newline at end of file | ... | ... |