aboutsummaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authorvincent-peugnet <v.peugnet@free.fr>2019-08-17 19:18:57 +0200
committervincent-peugnet <v.peugnet@free.fr>2019-08-17 19:18:57 +0200
commit463f06ec02ecc02a70147b6494e23ec93ca74420 (patch)
tree3bc470b8ff253dc343ca9b60b0cdf4a19c784236 /app
parent0d863868d69d9e2c9ae3f6f0b22b4d3a1be73ee2 (diff)
downloadwcms-463f06ec02ecc02a70147b6494e23ec93ca74420.tar.gz
wcms-463f06ec02ecc02a70147b6494e23ec93ca74420.zip
Media oragnisation
- Delete folder - move or delete files
Diffstat (limited to 'app')
-rw-r--r--app/class/controllermedia.php33
-rw-r--r--app/class/media.php5
-rw-r--r--app/class/modelmedia.php109
-rw-r--r--app/class/routes.php4
-rw-r--r--app/view/templates/media.php43
5 files changed, 185 insertions, 9 deletions
diff --git a/app/class/controllermedia.php b/app/class/controllermedia.php
index 4200253..93ab218 100644
--- a/app/class/controllermedia.php
+++ b/app/class/controllermedia.php
@@ -41,9 +41,13 @@ class Controllermedia extends Controller
$dirlist = $this->mediamanager->listdir(Model::MEDIA_DIR);
+ $pathlist = [];
+
+ $this->mediamanager->listpath($dirlist, '', $pathlist);
+
$this->mediamanager->medialistsort($medialist, $sortby, $order);
- $this->showtemplate('media', ['medialist' => $medialist, 'faviconlist' => $faviconlist, 'dirlist' => $dirlist, 'dir' => $dir, 'opt' => $opt]);
+ $this->showtemplate('media', ['medialist' => $medialist, 'faviconlist' => $faviconlist, 'dirlist' => $dirlist, 'pathlist' =>$pathlist, 'dir' => $dir, 'opt' => $opt]);
} else {
$this->routedirect('media');
}
@@ -66,7 +70,7 @@ class Controllermedia extends Controller
}
}
- public function folder()
+ public function folderadd()
{
if ($this->user->iseditor()) {
$dir = $_POST['dir'] ?? Model::MEDIA_DIR;
@@ -77,6 +81,31 @@ class Controllermedia extends Controller
}
+ public function folderdelete()
+ {
+ if(isset($_POST['dir'])) {
+ if(isset($_POST['deletefolder']) && intval($_POST['deletefolder']) && $this->user->issupereditor()) {
+ $this->mediamanager->deletedir($_POST['dir']);
+ } else {
+ $this->redirect($this->router->generate('media') . '?path=' . $_POST['dir']);
+ exit;
+ }
+ }
+ $this->redirect($this->router->generate('media'));
+ }
+
+ public function edit()
+ {
+ if($this->user->issupereditor() && isset($_POST['action']) && isset($_POST['id'])) {
+ if($_POST['action'] == 'delete') {
+ $this->mediamanager->multifiledelete($_POST['id']);
+ } elseif ($_POST['action'] == 'move' && isset($_POST['dir'])) {
+ $this->mediamanager->multimovefile($_POST['id'], $_POST['dir']);
+ }
+ }
+ $this->redirect($this->router->generate('media') . '?path=' . $_POST['path']);
+ }
+
}
diff --git a/app/class/media.php b/app/class/media.php
index 4b947fc..eccc58b 100644
--- a/app/class/media.php
+++ b/app/class/media.php
@@ -74,6 +74,11 @@ class Media
return $includepath;
}
+ public function getfulldir()
+ {
+ return $this->path . $this->id . '.' . $this->extension;
+ }
+
// _________________________________________________ G E T ____________________________________________________
diff --git a/app/class/modelmedia.php b/app/class/modelmedia.php
index ead7375..c2c1b47 100644
--- a/app/class/modelmedia.php
+++ b/app/class/modelmedia.php
@@ -154,10 +154,11 @@ class Modelmedia extends Model
}
- public function listdir($dir)
+ /**
+ * Generate an reccursive array where each folder is a array and containing a filecount in each folder
+ */
+ public function listdir(string $dir) : array
{
-
-
$result = array();
$cdir = scandir($dir);
@@ -176,6 +177,25 @@ class Modelmedia extends Model
}
/**
+ * Analyse reccursive array of content to generate list of path
+ *
+ * @param array $dirlist Array generated by the listdir function
+ * @param string $parent used to create the strings
+ * @param array $pathlist used by reference, must be an empty array
+ *
+ * @return array list of path as string
+ */
+ public function listpath(array $dirlist, string $parent = '', array &$pathlist = [])
+ {
+ foreach ($dirlist as $dir => $content) {
+ if(is_array($content)) {
+ $pathlist[] = $parent . $dir . DIRECTORY_SEPARATOR;
+ $this->listpath($content, $parent . $dir . DIRECTORY_SEPARATOR, $pathlist);
+ }
+ }
+ }
+
+ /**
* Upload single file
*
* @param string $index The file id
@@ -238,4 +258,87 @@ class Modelmedia extends Model
return false;
}
}
+
+ /**
+ * Completely delete dir and it's content
+ *
+ * @param string $dir Directory to destroy
+ *
+ * @return bool depending on operation success
+ */
+ public function deletedir(string $dir) : bool
+ {
+ if(substr($dir, -1) !== '/') {
+ $dir .= '/';
+ }
+ if(is_dir($dir)) {
+ return $this->deltree($dir);
+ } else {
+ return false;
+ }
+ }
+
+ /**
+ * Function do reccursively delete a directory
+ */
+ public function deltree(string $dir)
+ {
+ $files = array_diff(scandir($dir), array('.','..'));
+ foreach ($files as $file) {
+ (is_dir("$dir/$file")) ? $this->deltree("$dir/$file") : unlink("$dir/$file");
+ }
+ return rmdir($dir);
+ }
+
+ /**
+ * Delete a file
+ */
+ public function deletefile(string $filedir)
+ {
+ if(is_file($filedir)) {
+ return unlink($filedir);
+ } else {
+ return false;
+ }
+ }
+
+ public function multifiledelete(array $filelist)
+ {
+ foreach ($filelist as $filedir ) {
+ if(is_string($filedir)) {
+ $this->deletefile($filedir);
+ }
+ }
+ }
+
+ public function movefile(string $filedir, string $dir)
+ {
+ if(substr($dir, -1) !== '/') {
+ $dir .= '/';
+ }
+ if(is_file($filedir)) {
+ $newdir = $dir . basename($filedir);
+ return rename($filedir, $newdir);
+ } else {
+ return false;
+ }
+ }
+
+ public function multimovefile(array $filedirlist, string $dir)
+ {
+ $success = [];
+ foreach ($filedirlist as $filedir ) {
+ if(is_string($filedir)) {
+ $success[] = $this->movefile($filedir, $dir);
+ }
+ }
+ if(in_array(false, $success)) {
+ return false;
+ } else {
+ return true;
+ }
+ }
+
+
+
}
diff --git a/app/class/routes.php b/app/class/routes.php
index f960582..16aede2 100644
--- a/app/class/routes.php
+++ b/app/class/routes.php
@@ -23,7 +23,9 @@ class Routes
['POST', '/!search', 'Controllerhome#search', 'search'],
['GET', '/!media', 'Controllermedia#desktop', 'media'],
['POST', '/!media/upload', 'Controllermedia#upload', 'mediaupload'],
- ['POST', '/!media/folder', 'Controllermedia#folder', 'mediafolder'],
+ ['POST', '/!media/folderadd', 'Controllermedia#folderadd', 'mediafolderadd'],
+ ['POST', '/!media/folderdelete', 'Controllermedia#folderdelete', 'mediafolderdelete'],
+ ['POST', '/!media/edit', 'Controllermedia#edit', 'mediaedit'],
['GET', '/!font', 'Controllerfont#desktop', 'font'],
['GET', '/!font/render', 'Controllerfont#render', 'fontrender'],
['POST', '/!font/add', 'Controllerfont#add', 'fontadd'],
diff --git a/app/view/templates/media.php b/app/view/templates/media.php
index c75ce68..cea51fb 100644
--- a/app/view/templates/media.php
+++ b/app/view/templates/media.php
@@ -65,13 +65,25 @@ treecount($dirlist, 'media', 0, 'media', $dir, $opt);
-<form id="addfolder" action="<?= $this->url('mediafolder') ?>" method="post">
+<form id="folderadd" action="<?= $this->url('mediafolderadd') ?>" method="post">
<label for="foldername">📂 New folder</label>
<input type="text" name="foldername" id="foldername" placeholder="folder name" required>
<input type="hidden" name="dir" value="<?= $dir ?>">
<input type="submit" value="create folder">
</form>
+<?php if($user->issupereditor()) { ?>
+
+<form action="<?= $this->url('mediafolderdelete') ?>" id="deletefolder" method="post" class="hidephone">
+ <input type="hidden" name="dir" value="<?= $dir ?>/">
+ <input type="checkbox" name="deletefolder" id="confirmdeletefolder" value="1">
+ <label for="confirmdeletefolder">Delete folder and all it's content</label>
+ <input type="submit" value="delete folder" >
+</form>
+
+
+<?php } ?>
+
<form id=addmedia action="<?= $this->url('mediaupload') ?>" method="post" enctype="multipart/form-data">
<label for="file">🚀 Upload file(s)</label>
<input type='file' id="file" name='file[]' multiple required>
@@ -81,8 +93,32 @@ treecount($dirlist, 'media', 0, 'media', $dir, $opt);
+<?php if($user->issupereditor()) { ?>
+
+<form action="<?= $this->url('mediaedit') ?>" method="post" id="mediaedit">
+ <input type="hidden" name="path" value="<?= $dir ?>">
+ <label for="moveto">Selected medias :</label>
+ <select name="dir" id="moveto" >
+ <option value="" selected>---select destination---</option>
+ <option value="<?= Model::MEDIA_DIR ?>">/</option>
+ <?php
+ foreach ($pathlist as $path) {
+ echo '<option value="' . Model::MEDIA_DIR . $path . '">' . $path . '</option>';
+ }
+ ?>
+ </select>
+ <input type="submit" name="action" value="move" >
+ <input type="submit" name="action" value="delete" >
+</form>
+
+<?php } ?>
+
+
+
+
<table id="medialist">
<tr>
+ <th>x</th>
<th><a href="?path=<?= $dir ?>&sortby=id&order=<?php echo ($opt['order'] * -1); ?>">id</a></th>
<th>ext</th>
<th><a href="?path=<?= $dir ?>&sortby=type&order=<?php echo ($opt['order'] * -1); ?>">type</a></th>
@@ -97,9 +133,10 @@ treecount($dirlist, 'media', 0, 'media', $dir, $opt);
foreach ($medialist as $media) {
?>
<tr>
- <td><a href="<?= $media->getfullpath() ?>" target="_blank"><?= $media->id() ?></a></td>
+ <td><input type="checkbox" name="id[]" value="<?= $media->getfulldir() ?>" form="mediaedit" id="media_<?= $media->id() ?>"></td>
+ <td><label for="media_<?= $media->id() ?>"><?= $media->id() ?></label></td>
<td><?= $media->extension() ?></td>
- <td><?= $media->type() == 'image' ? '<span class="thumbnail">image 👁<img src="' . $media->getfullpath() . '"></span>' : $media->type() ?></td>
+ <td><a href="<?= $media->getfullpath() ?>" target="_blank"><?= $media->type() == 'image' ? '<span class="thumbnail">image 👁<img src="' . $media->getfullpath() . '"></span>' : $media->type() ?></a></td>
<td><?= $media->size('hr') ?></td>
<td><?= $media->width() ?></td>
<td><?= $media->height() ?></td>