Browse Source

First commit

master
Chimo 3 years ago
commit
b7d03164c5
No known key found for this signature in database GPG Key ID: B0C36EDD0BB35A9C
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 @@
*.swp

+ 66
- 0
FileManagerPlugin.php View File

@@ -0,0 +1,66 @@
<?php

if (!defined('GNUSOCIAL')) {
exit(1);
}

class FileManagerPlugin extends Plugin
{
const VERSION = '0.0.1';

function onRouterInitialized($m)
{
$m->connect(
'panel/files', array(
'action' => 'filesadminpanel'
)
);

$m->connect(
'panel/file/:file/delete',
array('action' => 'deletefileadminpanel'),
array('file' => '[0-9]+')
);

return true;
}

function onEndShowStyles($action) {
$action->cssLink($this->path('css/file-manager.css'));
}

/**
* If the plugin's installed, this should be accessible to admins
*/
function onAdminPanelCheck($name, &$isOK)
{
if ($name === 'files' || $name === 'deletefile') {
$isOK = true;
return false;
}

return true;
}

function onEndAdminPanelNav($nav) {
if (AdminPanelAction::canAdmin('user')) {
$menu_title = _('File Manager');
$action_name = $nav->action->trimmed('action');

$nav->out->menuItem(common_local_url('filesadminpanel'), _m('MENU','File Manager'),
$menu_title, $action_name == 'filesadminpanel', 'files_admin_panel');
}
}

function onPluginVersion(array &$versions)
{
$versions[] = array('name' => 'File Manager',
'version' => self::VERSION,
'author' => 'chimo',
'homepage' => 'https://github.com/chimo/gs-fileManager',
'description' =>
// TRANS: Plugin description.
_m('Manage files (attachments)'));
return true;
}
}

+ 48
- 0
actions/deletefileadminpanel.php View File

@@ -0,0 +1,48 @@
<?php
if (!defined('GNUSOCIAL')) {
exit(1);
}

class DeletefileadminpanelAction extends AdminPanelAction
{
protected $file = null;

function title() {
return 'Confirm Deletion';
}

function prepare(array $args=array()) {
parent::prepare($args);

// TODO: handle cases where we don't have a fileId

$fileId = $args['file'];

$this->file = File::getKV('id', $fileId);

return true;
}

// FIXME: This doesn't get called(??)
protected function doPost() {
// TODO: Actually delete the notice

return _('File deleted.');
}

function showForm() {
$form = new DeletefileForm($this, array('file' => $this->file));

$form->show();
}

function saveSettings() {
if ($this->arg('yes')) {
// TODO: handle errors
$this->file->delete();
}

common_redirect(common_local_url('filesadminpanel'), 303);
}
}


+ 89
- 0
actions/filesadminpanel.php View File

@@ -0,0 +1,89 @@
<?php
if (!defined('GNUSOCIAL')) {
exit(1);
}

class FilesadminpanelAction extends AdminPanelAction
{
function title() {
return 'File Manager';
}

function prepare(array $args=array()) {
parent::prepare($args);

$this->page = isset($args['page']) ? $args['page'] + 0 : 1;
$this->args = $args;

$offset = ($this->page - 1) * 10;
$limit = 10;

$file = new File();

$file->whereAdd('filename IS NOT NULL'); // Only local files
$file->limit($offset, $limit);

$this->files = $file->fetchAll();

return true;
}

function formatBytes($size, $precision = 2)
{
$base = log($size, 1024);
$suffixes = array('', 'K', 'M', 'G', 'T');

return round(pow(1024, $base - floor($base)), $precision) .' '. $suffixes[floor($base)];
}

function showContent() {
if (sizeof($this->files) === 0) {
$this->element('p', null, 'No files found.'); // TODO: Better msg

return true;
}

$this->elementStart('table', array('class' => 'chr-files'));
$this->elementStart('thead');
$this->elementStart('tr');
$this->element('th', null, 'Thumbnail');
$this->element('th', null, 'Size');
$this->element('th', null, 'Original');
$this->element('th', null, 'Delete');
$this->elementEnd('tr');
$this->elementEnd('thead');

$this->elementStart('tbody');

foreach($this->files as $file) {
$deleteurl = common_local_url('deletefileadminpanel', array('file' => $file->getID()));

$this->elementStart('tr');

// Thumbnail
$this->elementStart('td');
$thumbnail = $file->getThumbnail(); // FIXME: Hanlde file types w/ no thumbnails
$this->element('img', $thumbnail->getHtmlAttrs(array('class' => 'file-thumbnail')));
$this->elementEnd('td');

// Size
$this->element('td', null, $this->formatBytes($file->getSize()));

// Link to original file
$this->elementStart('td');
$this->element('a', array('href' => $file->getUrl()), 'Link');
$this->elementEnd('td');

// Delete
$this->elementStart('td');
$this->element('a', array('class' => 'file-delete', 'href' => $deleteurl), 'Delete');
$this->elementEnd('td');

$this->elementEnd('tr');
}

$this->elementEnd('tbody');
$this->elementEnd('table');
}
}


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

@@ -0,0 +1,30 @@
.chr-files {
width: 100%;
}

.chr-files,
.chr-files td,
.chr-files th {
border: 1px solid #ddd;
}

#content .chr-files td,
.chr-files th {
padding: 5px;
}

.file-thumbnail {
max-height: 96px;
width: auto;
}

.file-delete {
/* FIXME: don't make the sprites theme-specific */
background: url("/theme/base/images/icons/icons-01.gif") no-repeat 0 -658px;
display: block;
height: 16px;
outline: none;
text-indent: -9999px;
width: 16px;
}


+ 55
- 0
forms/deletefile.php View File

@@ -0,0 +1,55 @@
<?php
if (!defined('GNUSOCIAL')) {
exit(1);
}

class DeletefileForm extends AdminForm
{
function __construct(HTMLOutputter $out=null, array $formOpts=array())
{
if (!array_key_exists('file', $formOpts) || !$formOpts['file'] instanceof File) {
throw new ServerException('No file provided to DeletefileForm');
}

parent::__construct($out);

$this->file = $formOpts['file'];
}

function id() {
return 'form_deletefile_admin_panel';
}

function formClass() {
return 'form_settings';
}

function action() {
$deleteurl = common_local_url('deletefileadminpanel', array('file' => $this->file->getID()));

return $deleteurl;
}

function formData() {
// TODO
}

function formActions() {
$this->out->submit('form_action-no',
// TRANS: Button label on the delete notice form.
_m('BUTTON','No'),
'submit form_action-primary',
'no',
// TRANS: Submit button title for 'No' when deleting a notice.
_('Do not delete this file.'));

$this->out->submit('form_action-yes',
// TRANS: Button label on the delete notice form.
_m('BUTTON','Yes'),
'submit form_action-secondary',
'yes',
// TRANS: Submit button title for 'Yes' when deleting a notice.
_('Delete this file.'));
}
}


Loading…
Cancel
Save