Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a Property manager. #207

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions apps/filemanager/handlers/properties.php
Original file line number Diff line number Diff line change
@@ -1,26 +1,36 @@
<?php

/**
* Fetches the properties edit form for the file manager.
*/

$page->layout = false;

header ('Content-Type: application/json');

$file = urldecode (join ('/', $this->params));


$res = PropManager::all();
$props = array();

foreach($res as $k => $row) {
$props[$k]['id'] = $row->id;
$props[$k]['value'] = FileManager::prop ($file, $row->id);
$props[$k]['type'] = $row->type;
$props[$k]['label'] = $row->label;
}

$out = array (
'title' => __ ('Properties'),
'body' => $tpl->render (
'filemanager/properties',
array (
'file' => $file,
'desc' => FileManager::prop ($file, 'desc')
'props' => $props
)
)
);

echo json_encode ($out);

?>
18 changes: 18 additions & 0 deletions apps/filemanager/handlers/propman/add.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/**
* Fetches the properties add form for the property manager.
*/

$page->layout = false;

header ('Content-Type: application/json');

$out = array (
'title' => __ ('Add Property'),
'body' => $tpl->render ('filemanager/propman/add')
);

echo json_encode ($out);

?>
28 changes: 28 additions & 0 deletions apps/filemanager/handlers/propman/edit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/**
* Fetches the properties edit form for the property manager.
*/

$page->layout = false;

header ('Content-Type: application/json');

$res = PropManager::all();
$prop = array();

foreach ($res as $row) {
if ($_GET['id'] == $row->id) {
$prop['id'] = $row->id;
$prop['type'] = $row->type;
$prop['label'] = $row->label;
}
}

$out = array (
'title' => __ ('Edit Property'),
'body' => $tpl->render ('filemanager/propman/edit', $prop)
);
echo json_encode ($out);

?>
26 changes: 26 additions & 0 deletions apps/filemanager/handlers/propmanager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/**
* Fetches the properties manager for the file manager.
*/

$page->layout = 'admin';

$this->require_acl ('admin', 'user');

$res = PropManager::all();
$props = array();

foreach ($res as $k => $row) {
$props[$k]['id'] = $row->id;
$props[$k]['type'] = $row->type;
$props[$k]['label'] = $row->label;
}


$page->title = __ ('Properties Manager');
$page->add_script ('/js/jquery-ui/jquery-ui.min.js');
$page->add_script ('/apps/filemanager/js/jquery.filemanager.js');
echo $tpl->render ('filemanager/propmanager', array('props' => $props, 'redirect' => $_GET['redirect']));

?>
59 changes: 51 additions & 8 deletions apps/filemanager/js/jquery.filemanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,16 @@
res.body
);
});
break;
break;
case 'propman':
// display properties dialogue
$.get ('/filemanager/propman/' + options.action + '?id=' + options.id, function (res) {
$.open_dialog (
res.title,
res.body
);
});
break;
case 'img':
// edit an image
var url = window.location.href.split ('/filemanager')[0] + '/' + conf_root + '/' + options.file;
Expand Down Expand Up @@ -274,13 +283,47 @@
}

$.filemanager_prop = function (form) {
var file = form.elements.file.value,
desc = form.elements.desc.value;

$.get ('/filemanager/api/prop/' + file + '?prop=desc&value=' + encodeURIComponent (desc), function (res) {
$.close_dialog ();
$.add_notification (res.data.msg);
});
var file = form.elements.file.value, result = '', response = '';
$('#properties').children('.prop').each( function() {
$.get ('/filemanager/api/prop/' + file + '?prop=' + $(this).attr('id') + '&value=' + encodeURIComponent ($(this).attr('value')), function(res) {
result = res.data.value;
response = res.data.msg;
});
});
$.close_dialog ();
$.add_notification (response);
return false;
};

$.propmanager = function (action, form) {
if (action != 'delete') {
var id = form.elements.id.value;
var type = form.elements.type.value;
var label = form.elements.label.value;
$.get ('/filemanager/api/propman/' + action + '?id=' + encodeURIComponent (id) + '&type=' + type + '&label=' + encodeURIComponent (label), function(res) {
if (!res) {
return false;
}
$.close_dialog ();
if (action == 'add') {
$('#prop-table').append("<tr id=" + id + "><td id=\"prop-id\">" + id + "</td><td id=\"prop-type\">" + type + "</td><td id=\"prop-label\">" + label + "</td><td style=\"text-align: right\"><a href=\"#\" onclick=\"return $.filemanager ('propman', {action: 'edit', id: '" + id + "'})\">Edit</a> | <a href=\"#\" onclick=\"if (confirm('Are you sure you want to delete this property?')) { return $.propmanager('delete', '" + id + "') };\">Delete</a></td></tr>");
$.add_notification ('Property added.');
}
if (action == 'edit') {
$('tr#' + id + ' td#prop-type').text(type);
$('tr#' + id + ' td#prop-label').text(label);
$.add_notification ('Property saved.');
}
});
} else {
$.get ('/filemanager/api/propman/delete?id=' + encodeURIComponent (form), function(res) {
if (!res) {
return false;
}
$('tr#' + form).remove();
$.add_notification ('Property removed.');
});
}
return false;
};

Expand Down
28 changes: 26 additions & 2 deletions apps/filemanager/lib/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace filemanager;

use DB, FileManager, I18n, Restful;
//added PropManager
use DB, FileManager, PropManager, I18n, Restful;

/**
* Provides the JSON API for the admin file manager/browser, as well as functions
Expand Down Expand Up @@ -125,7 +126,7 @@ public function get_prop () {
if (! isset ($_GET['prop'])) {
return $this->error (__ ('Missing property name'));
}
if (isset ($_GET['value'])) {
if ((!FileManager::prop($file, $_GET['prop']) && $_GET['value'] != '') || $_GET['value'] != Filemanager::prop ($file, $_GET['prop'])) {
// update and fetch
$res = FileManager::prop ($file, $_GET['prop'], $_GET['value']);
} else {
Expand All @@ -139,6 +140,29 @@ public function get_prop () {
'msg' => __ ('Properties saved.')
);
}

/**
* Handle property manager update requests (/filemanager/api/propman).
*/
public function get_propman () {
$action = urldecode (join ('/', func_get_args ()));
if (! isset ($_GET['id'])) {
return $this->error (__ ('Missing property name.'));
}
if (! preg_match ('/^[a-zA-Z0-9_]+$/', $_GET['id'])) {
return $this->error (__ ('Invalid property name.'));
}
if (! $action == 'delete' && ! preg_match ('/^[a-zA-Z0-9_]+$/', $_GET['label'])) {
return $this->error (__ ('Invalid label.'));
}
if ($action == 'add' || $action == 'edit') {
// update and fetch
$res = PropManager::set ($_GET['id'], $_GET['type'], $_GET['label']);
} elseif ($action == 'delete') {
$res = PropManager::delete ($_GET['id']);
}
return $res;
}
}

?>
21 changes: 9 additions & 12 deletions apps/filemanager/lib/FileManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ public static function fsort ($a, $b) {
}

/**
* Fetch all of the properties for the specified file.
* Fetch all of the properties for the specified file. (not used anymore)
*/
public static function props ($file) {
return DB::pairs ('select prop, value from #prefix#filemanager_prop where file = ?', $file);
Expand All @@ -435,9 +435,8 @@ public static function prop ($file, $prop, $value = null) {
} elseif ($res === false || $res === null) {
// doesn't exist yet
if (! DB::execute (
'insert into #prefix#filemanager_prop (file, prop, value) values (?, ?, ?)',
'insert into #prefix#filemanager_prop (file, `'.$prop.'`) values (?, ?)',
$file,
$prop,
$value
)) {
self::$error = DB::error ();
Expand All @@ -446,10 +445,9 @@ public static function prop ($file, $prop, $value = null) {
} else {
// already exists, update
if (! DB::execute (
'update #prefix#filemanager_prop set value = ? where file = ? and prop = ?',
'update #prefix#filemanager_prop set `'.$prop.'` = ? where file = ?',
$value,
$file,
$prop
$file
)) {
self::$error = DB::error ();
return false;
Expand All @@ -461,16 +459,15 @@ public static function prop ($file, $prop, $value = null) {
// get as a list
$qmarks = array_fill (0, count ($file), '?');
$file[] = $prop;
return DB::pairs (
'select file, value from #prefix#filemanager_prop where file in(' . join (', ', $qmarks) . ') and prop = ?',
$file
return DB::fetch (
'select `'.$prop.'` from #prefix#filemanager_prop where file in(' . join (', ', $qmarks) . ')',
$file
);
}
// get a single value
return DB::shift (
'select value from #prefix#filemanager_prop where file = ? and prop = ?',
$file,
$prop
'select `'.$prop.'` from #prefix#filemanager_prop where file = ?',
$file
);
}

Expand Down
66 changes: 66 additions & 0 deletions apps/filemanager/lib/PropManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
class PropManager {
// Stores last error occurance
public static $error;
// Returns last error occurance
public static function error () {
return self::$error;
}
// gets the data of the requested property
public static function get ($id) {
return DB::single ('select * from #prefix#filemanager_propman where id = ?', $id);
}
// gets the data of each property that exists
public static function all () {
return DB::fetch ('select * from #prefix#filemanager_propman');
}
// sets and updates any new changes to the property data
public static function set ($id, $type, $label) {
// takes an extra select query, but works cross-database
$res = self::get ($id);
if ($res->type === $type && $res->label === $label) {
return 'No changes made';
} elseif (!$res) {
// doesn't exist yet
if (DB::execute (
'insert into #prefix#filemanager_propman (id, type, label) values (?, ?, ?)',
$id,
$type,
$label
)) {
DB::execute ('alter table #prefix#filemanager_prop add '.$id.' char(255) not null');
} else {
self::$error = DB::error ();
return false;
}
} else {
// already exists, update
if (! DB::execute (
'update #prefix#filemanager_propman set type = ?, label = ? where id = ?',
$type,
$label,
$id
)) {
self::$error = DB::error ();
return false;
}
}
return self::get($id);
}
public static function delete ($id) {
//check if exists
$res = self::get ($id);
//return if doesn't exist
if (! $res->id === $id) {
return 'Property does not exist.';
//if exists, execute removal
} elseif (DB::execute ('delete from #prefix#filemanager_propman where id = ?', $id)) {
DB::execute ('alter table #prefix#filemanager_prop drop column '.$id);
return true;
} else {
self::$error = DB::error ();
return self::$error;
}
}
}
?>
5 changes: 3 additions & 2 deletions apps/filemanager/views/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,16 @@ <h2>
</div>

<p>
<table width="100%">
<tr>
<table width="100%" style="margin-bottom:5px;">
<tr>
<th width="45%">{" Name "}</th>
<th width="22%">{" Last modified "}</th>
<th width="8%">{" File size "}</th>
<th width="25%">&nbsp;</th>
</tr>
<tbody id="file-list"></tbody>
</table>
<a style="float:right;" href="/filemanager/propmanager?redirect={{path}}">Properties Manager</a>
</p>

{% if aviary_key %}
Expand Down
Loading