Browse Source

First commit

master
Chimo 2 years ago
commit
b7d03164c5
No known key found for this signature in database
6 changed files with 289 additions and 0 deletions
  1. 1
    0
      .gitignore
  2. 66
    0
      FileManagerPlugin.php
  3. 48
    0
      actions/deletefileadminpanel.php
  4. 89
    0
      actions/filesadminpanel.php
  5. 30
    0
      css/file-manager.css
  6. 55
    0
      forms/deletefile.php

+ 1
- 0
.gitignore View File

@@ -0,0 +1 @@
1
+*.swp

+ 66
- 0
FileManagerPlugin.php View File

@@ -0,0 +1,66 @@
1
+<?php
2
+
3
+if (!defined('GNUSOCIAL')) {
4
+    exit(1);
5
+}
6
+
7
+class FileManagerPlugin extends Plugin
8
+{
9
+    const VERSION = '0.0.1';
10
+
11
+    function onRouterInitialized($m)
12
+    {
13
+        $m->connect(
14
+            'panel/files', array(
15
+                'action' => 'filesadminpanel'
16
+            )
17
+        );
18
+
19
+        $m->connect(
20
+            'panel/file/:file/delete',
21
+            array('action' => 'deletefileadminpanel'),
22
+            array('file' => '[0-9]+')
23
+        );
24
+
25
+        return true;
26
+    }
27
+
28
+    function onEndShowStyles($action) {
29
+        $action->cssLink($this->path('css/file-manager.css'));
30
+    }
31
+
32
+    /**
33
+     * If the plugin's installed, this should be accessible to admins
34
+     */
35
+    function onAdminPanelCheck($name, &$isOK)
36
+    {
37
+        if ($name === 'files' || $name === 'deletefile') {
38
+            $isOK = true;
39
+            return false;
40
+        }
41
+
42
+        return true;
43
+    }
44
+
45
+    function onEndAdminPanelNav($nav) {
46
+        if (AdminPanelAction::canAdmin('user')) {
47
+            $menu_title = _('File Manager');
48
+            $action_name = $nav->action->trimmed('action');
49
+
50
+            $nav->out->menuItem(common_local_url('filesadminpanel'), _m('MENU','File Manager'),
51
+                                 $menu_title, $action_name == 'filesadminpanel', 'files_admin_panel');
52
+        }
53
+    }
54
+
55
+    function onPluginVersion(array &$versions)
56
+    {
57
+        $versions[] = array('name' => 'File Manager',
58
+                            'version' => self::VERSION,
59
+                            'author' => 'chimo',
60
+                            'homepage' => 'https://github.com/chimo/gs-fileManager',
61
+                            'description' =>
62
+                            // TRANS: Plugin description.
63
+                            _m('Manage files (attachments)'));
64
+        return true;
65
+    }
66
+}

+ 48
- 0
actions/deletefileadminpanel.php View File

@@ -0,0 +1,48 @@
1
+<?php
2
+if (!defined('GNUSOCIAL')) {
3
+    exit(1);
4
+}
5
+
6
+class DeletefileadminpanelAction extends AdminPanelAction
7
+{
8
+    protected $file = null;
9
+
10
+    function title() {
11
+        return 'Confirm Deletion';
12
+    }
13
+
14
+    function prepare(array $args=array()) {
15
+        parent::prepare($args);
16
+
17
+        // TODO: handle cases where we don't have a fileId
18
+
19
+        $fileId = $args['file'];
20
+
21
+        $this->file = File::getKV('id', $fileId);
22
+
23
+        return true;
24
+    }
25
+
26
+    // FIXME: This doesn't get called(??)
27
+    protected function doPost() {
28
+        // TODO: Actually delete the notice
29
+
30
+        return _('File deleted.');
31
+    }
32
+
33
+    function showForm() {
34
+        $form = new DeletefileForm($this, array('file' => $this->file));
35
+
36
+        $form->show();
37
+    }
38
+
39
+    function saveSettings() {
40
+        if ($this->arg('yes')) {
41
+            // TODO: handle errors
42
+            $this->file->delete();
43
+        }
44
+
45
+        common_redirect(common_local_url('filesadminpanel'), 303);
46
+    }
47
+}
48
+

+ 89
- 0
actions/filesadminpanel.php View File

@@ -0,0 +1,89 @@
1
+<?php
2
+if (!defined('GNUSOCIAL')) {
3
+    exit(1);
4
+}
5
+
6
+class FilesadminpanelAction extends AdminPanelAction
7
+{
8
+    function title() {
9
+        return 'File Manager';
10
+    }
11
+
12
+    function prepare(array $args=array()) {
13
+        parent::prepare($args);
14
+
15
+        $this->page = isset($args['page']) ? $args['page'] + 0 : 1;
16
+        $this->args = $args;
17
+
18
+        $offset = ($this->page - 1) * 10;
19
+        $limit = 10;
20
+
21
+        $file = new File();
22
+
23
+        $file->whereAdd('filename IS NOT NULL'); // Only local files
24
+        $file->limit($offset, $limit);
25
+
26
+        $this->files = $file->fetchAll();
27
+
28
+        return true;
29
+    }
30
+
31
+    function formatBytes($size, $precision = 2)
32
+    {
33
+        $base = log($size, 1024);
34
+        $suffixes = array('', 'K', 'M', 'G', 'T');
35
+
36
+        return round(pow(1024, $base - floor($base)), $precision) .' '. $suffixes[floor($base)];
37
+    }
38
+
39
+    function showContent() {
40
+        if (sizeof($this->files) === 0) {
41
+            $this->element('p', null, 'No files found.'); // TODO: Better msg
42
+
43
+            return true;
44
+        }
45
+
46
+        $this->elementStart('table', array('class' => 'chr-files'));
47
+        $this->elementStart('thead');
48
+        $this->elementStart('tr');
49
+        $this->element('th', null, 'Thumbnail');
50
+        $this->element('th', null, 'Size');
51
+        $this->element('th', null, 'Original');
52
+        $this->element('th', null, 'Delete');
53
+        $this->elementEnd('tr');
54
+        $this->elementEnd('thead');
55
+
56
+        $this->elementStart('tbody');
57
+
58
+        foreach($this->files as $file) {
59
+            $deleteurl = common_local_url('deletefileadminpanel', array('file' => $file->getID()));
60
+
61
+            $this->elementStart('tr');
62
+
63
+            // Thumbnail
64
+            $this->elementStart('td');
65
+            $thumbnail = $file->getThumbnail(); // FIXME: Hanlde file types w/ no thumbnails
66
+            $this->element('img', $thumbnail->getHtmlAttrs(array('class' => 'file-thumbnail')));
67
+            $this->elementEnd('td');
68
+
69
+            // Size
70
+            $this->element('td', null, $this->formatBytes($file->getSize()));
71
+
72
+            // Link to original file
73
+            $this->elementStart('td');
74
+            $this->element('a', array('href' => $file->getUrl()), 'Link');
75
+            $this->elementEnd('td');
76
+
77
+            // Delete
78
+            $this->elementStart('td');
79
+            $this->element('a', array('class' => 'file-delete', 'href' => $deleteurl), 'Delete');
80
+            $this->elementEnd('td');
81
+
82
+            $this->elementEnd('tr');
83
+        }
84
+
85
+        $this->elementEnd('tbody');
86
+        $this->elementEnd('table');
87
+    }
88
+}
89
+

+ 30
- 0
css/file-manager.css View File

@@ -0,0 +1,30 @@
1
+.chr-files {
2
+    width: 100%;
3
+}
4
+
5
+.chr-files,
6
+.chr-files td,
7
+.chr-files th {
8
+    border: 1px solid #ddd;
9
+}
10
+
11
+#content .chr-files td,
12
+.chr-files th {
13
+    padding: 5px;
14
+}
15
+
16
+.file-thumbnail {
17
+    max-height: 96px;
18
+    width: auto;
19
+}
20
+
21
+.file-delete {
22
+    /* FIXME: don't make the sprites theme-specific */
23
+    background: url("/theme/base/images/icons/icons-01.gif") no-repeat 0 -658px;
24
+    display: block;
25
+    height: 16px;
26
+    outline: none;
27
+    text-indent: -9999px;
28
+    width: 16px;
29
+}
30
+

+ 55
- 0
forms/deletefile.php View File

@@ -0,0 +1,55 @@
1
+<?php
2
+if (!defined('GNUSOCIAL')) {
3
+    exit(1);
4
+}
5
+
6
+class DeletefileForm extends AdminForm
7
+{
8
+    function __construct(HTMLOutputter $out=null, array $formOpts=array())
9
+    {
10
+        if (!array_key_exists('file', $formOpts) || !$formOpts['file'] instanceof File) {
11
+            throw new ServerException('No file provided to DeletefileForm');
12
+        }
13
+
14
+        parent::__construct($out);
15
+
16
+        $this->file = $formOpts['file'];
17
+    }
18
+
19
+    function id() {
20
+        return 'form_deletefile_admin_panel';
21
+    }
22
+
23
+    function formClass() {
24
+        return 'form_settings';
25
+    }
26
+
27
+    function action() {
28
+        $deleteurl = common_local_url('deletefileadminpanel', array('file' => $this->file->getID()));
29
+
30
+        return $deleteurl;
31
+    }
32
+
33
+    function formData() {
34
+        // TODO
35
+    }
36
+
37
+    function formActions() {
38
+        $this->out->submit('form_action-no',
39
+                      // TRANS: Button label on the delete notice form.
40
+                      _m('BUTTON','No'),
41
+                      'submit form_action-primary',
42
+                      'no',
43
+                      // TRANS: Submit button title for 'No' when deleting a notice.
44
+                      _('Do not delete this file.'));
45
+
46
+        $this->out->submit('form_action-yes',
47
+                      // TRANS: Button label on the delete notice form.
48
+                      _m('BUTTON','Yes'),
49
+                      'submit form_action-secondary',
50
+                      'yes',
51
+                      // TRANS: Submit button title for 'Yes' when deleting a notice.
52
+                      _('Delete this file.'));
53
+    }
54
+}
55
+

Loading…
Cancel
Save