Module function calls can now be passed multiple parameters by GET/POST requests. FileBrowser Module now filters directory and file listing properly via filterFiles().

master
kts 2014-05-19 19:14:05 -07:00
parent 4b4e71279c
commit c854c8ab76
4 changed files with 81 additions and 15 deletions

View File

@ -67,6 +67,8 @@ class MediaServe {
static function processProgram() static function processProgram()
This function first checks for POST or GET Module commands, and if found, attempts to call them, passing parameters as needed. Thereafter, it runs through every Module loaded and executes the Module's onProcess method if it exists. This function first checks for POST or GET Module commands, and if found, attempts to call them, passing parameters as needed. Thereafter, it runs through every Module loaded and executes the Module's onProcess method if it exists.
POST/GET parameter(s) can be declared in a single variable syntax, e.g., "p=value", or in array style syntax, e.g., "p[]=value1&p[]=value2". Parameters are passed to functions in the order they are provided.
================================ ================================
*/ */
static function processProgram() { static function processProgram() {
@ -74,13 +76,21 @@ class MediaServe {
if (isset($_POST['m'])) { if (isset($_POST['m'])) {
if (isset($_POST['c'])) { if (isset($_POST['c'])) {
if (method_exists('ktk\\MediaServe\\'.$_POST['m'], $_POST['c'])) { if (method_exists('ktk\\MediaServe\\'.$_POST['m'], $_POST['c'])) {
call_user_func('ktk\\MediaServe\\'.$_POST['m'].'::'.$_POST['c'], $_POST['p1']); if (is_array($_POST['p'])) {
call_user_func_array('ktk\\MediaServe\\'.$_POST['m'].'::'.$_POST['c'], $_POST['p']);
} else {
call_user_func('ktk\\MediaServe\\'.$_POST['m'].'::'.$_POST['c'], $_POST['p']);
}
} }
} }
} else if (isset($_GET['m'])) { } else if (isset($_GET['m'])) {
if (isset($_GET['c'])) { if (isset($_GET['c'])) {
if (method_exists('ktk\\MediaServe\\'.$_GET['m'], $_GET['c'])) { if (method_exists('ktk\\MediaServe\\'.$_GET['m'], $_GET['c'])) {
call_user_func('ktk\\MediaServe\\'.$_GET['m'].'::'.$_GET['c'], $_GET['p1']); if (is_array($_GET['p'])) {
call_user_func_array('ktk\\MediaServe\\'.$_GET['m'].'::'.$_GET['c'], $_GET['p']);
} else {
call_user_func('ktk\\MediaServe\\'.$_GET['m'].'::'.$_GET['c'], $_GET['p']);
}
} }
} }
} }

View File

@ -14,7 +14,7 @@ class Clock {
static function onRender() { static function onRender() {
echo '<div id="ktk_MediaServe_Clock">'.PHP_EOL; echo '<div id="ktk_MediaServe_Clock">'.PHP_EOL;
echo self::$time.PHP_EOL; echo self::$time.PHP_EOL;
echo '<a href="?m=Clock&c=changeTime&p1=g:i:s a">12-hour</a>'.PHP_EOL; echo '<a href="?m=Clock&c=changeTime&p=g:i:s a">12-hour</a>'.PHP_EOL;
echo '</div>'.PHP_EOL; echo '</div>'.PHP_EOL;
} }
static function changeTime($format) { static function changeTime($format) {

View File

@ -51,7 +51,21 @@
color: #CCC; color: #CCC;
} }
#ktk_MediaServe_FileBrowser button:hover,
#ktk_MediaServe_FileBrowser input:hover { #ktk_MediaServe_FileBrowser input:hover {
background-color: #777; background-color: #777;
cursor: pointer; cursor: pointer;
} }
#ktk_MediaServe_FileBrowser button {
background-color: #777;
color: #CCC;
padding: 1px;
}
#ktk_MediaServe_FileBrowser_FilterInput {
border: 1px solid #777;
margin-left: 2px;
width: 115px;
}

View File

@ -1,6 +1,10 @@
<?php <?php
namespace ktk\MediaServe; namespace ktk\MediaServe;
/*
In FileBrowser, we store our lists of files(files), directories(dirs), and everything(dir_list) within MediaServe's storage. This is to improve performance and reduce disk access if we are only doing simple operations such as sorting.
*/
class FileBrowser { class FileBrowser {
const ROOT = "media/"; const ROOT = "media/";
@ -17,6 +21,7 @@ class FileBrowser {
MediaServe::storeData('cwd', FileBrowser::ROOT); MediaServe::storeData('cwd', FileBrowser::ROOT);
MediaServe::storeData('sort_order', SORT_ASC); MediaServe::storeData('sort_order', SORT_ASC);
MediaServe::storeData('sort_by', "name"); MediaServe::storeData('sort_by', "name");
MediaServe::storeData('filter', '');
self::getFiles(); self::getFiles();
self::sortFiles(); self::sortFiles();
} }
@ -32,18 +37,27 @@ class FileBrowser {
echo ' <input type="submit" name="" value="add all to playlist" />'; echo ' <input type="submit" name="" value="add all to playlist" />';
echo ' </form>'; echo ' </form>';
} }
echo ' <form action="" method="POST" autocomplete="off">';
echo ' <input type="hidden" name="m" value="FileBrowser" />';
echo ' <input id="ktk_MediaServe_FileBrowser_FilterInput" type="text" name="p[]" value="'.MediaServe::getData('filter').'" autocomplete="off" />';
//echo ' <input type="hidden" name="c" value="changeFilter" />';
//echo ' <input id="ktk_MediaServe_FileBrowser_FilterSubmit" type="submit" value="Filter" />';
// Using button may break IE6 and below...
echo ' <button type="submit" name="c" value="changeFilter">Filter</button>';
echo ' <button type="submit" name="c" value="resetFilter">Reset</button>';
echo ' </form>';
echo ' <table>'.PHP_EOL; echo ' <table>'.PHP_EOL;
echo ' <form action="" method="POST">'; echo ' <form action="" method="POST">';
echo ' <input type="hidden" name="m" value="FileBrowser" />'; echo ' <input type="hidden" name="m" value="FileBrowser" />';
echo ' <input type="hidden" name="c" value="changeSort" />'; echo ' <input type="hidden" name="c" value="changeSort" />';
echo ' <tr><th><input type="submit" name="p1" value="name" /></th><th><input type="submit" name="p1" value="ext" /></th></tr>'.PHP_EOL; echo ' <tr><th><input type="submit" name="p[]" value="name" /></th><th><input type="submit" name="p[]" value="ext" /></th></tr>'.PHP_EOL;
echo ' </form>'.PHP_EOL; echo ' </form>'.PHP_EOL;
echo ' <form action="" method="POST">'; echo ' <form action="" method="POST">';
echo ' <input type="hidden" name="m" value="FileBrowser" />'; echo ' <input type="hidden" name="m" value="FileBrowser" />';
echo ' <input type="hidden" name="c" value="changeCwd" />'; echo ' <input type="hidden" name="c" value="changeCwd" />';
echo ' <tr><td><input type="submit" name="p1" value="../"></td><td></td></tr>'.PHP_EOL; echo ' <tr><td><input type="submit" name="p[]" value="../"></td><td></td></tr>'.PHP_EOL;
foreach(MediaServe::getData('dirs') as $dir) { foreach(MediaServe::getData('dirs') as $dir) {
echo ' <tr><td><input type="submit" name="p1" value="'.$dir[FileBrowser::F_NAME].'/"></td><td></td></tr>'.PHP_EOL; echo ' <tr><td><input type="submit" name="p[]" value="'.$dir[FileBrowser::F_NAME].'/"></td><td></td></tr>'.PHP_EOL;
} }
echo ' </form>'.PHP_EOL; echo ' </form>'.PHP_EOL;
@ -51,7 +65,7 @@ class FileBrowser {
echo ' <input type="hidden" name="m" value="FileBrowser" />'; echo ' <input type="hidden" name="m" value="FileBrowser" />';
echo ' <input type="hidden" name="c" value="openFile" />'; echo ' <input type="hidden" name="c" value="openFile" />';
foreach(MediaServe::getData('files') as $file) { foreach(MediaServe::getData('files') as $file) {
echo ' <tr><td><input type="submit" name="p1" value="'.$file[FileBrowser::F_NAME].'"/></td><td>'.$file[FileBrowser::F_EXT].'</td></tr>'.PHP_EOL; echo ' <tr><td><input type="submit" name="p[]" value="'.$file[FileBrowser::F_NAME].'"/></td><td>'.$file[FileBrowser::F_EXT].'</td></tr>'.PHP_EOL;
} }
echo ' </form>'.PHP_EOL; echo ' </form>'.PHP_EOL;
echo ' </table>'.PHP_EOL; echo ' </table>'.PHP_EOL;
@ -76,8 +90,9 @@ class FileBrowser {
return NULL; return NULL;
} }
MediaServe::storeData('cwd', $directory); MediaServe::storeData('cwd', $directory);
if (self::getFiles()) self::getFiles();
self::sortFiles(); self::sortFiles();
self::filterFiles();
} }
static function changeSort($type) { static function changeSort($type) {
@ -88,6 +103,17 @@ class FileBrowser {
MediaServe::storeData('sort_order', SORT_DESC); MediaServe::storeData('sort_order', SORT_DESC);
} }
self::sortFiles(); self::sortFiles();
self::filterFiles();
}
static function changeFilter($filter) {
MediaServe::storeData('filter', $filter);
self::filterFiles();
}
static function resetFilter() {
self::sortFiles();
self::changeFilter("");
} }
static function openFile($file) { static function openFile($file) {
@ -123,6 +149,7 @@ class FileBrowser {
return NULL; return NULL;
} }
$i = 0; $i = 0;
$files = array();
while (false !== ($filename = readdir($directory_handle))) { while (false !== ($filename = readdir($directory_handle))) {
if ($filename[0] != '.' && $filename != "..") { if ($filename[0] != '.' && $filename != "..") {
$files[$i] = array(); $files[$i] = array();
@ -141,11 +168,6 @@ class FileBrowser {
/* INTERNAL FUNCTIONS */ /* INTERNAL FUNCTIONS */
static function sortFiles() { static function sortFiles() {
if (!MediaServe::getData('dir_list')) {
MediaServe::storeData('dirs', array());
MediaServe::storeData('files', array());
return NULL;
}
$sort = array(); $sort = array();
$sort_by = MediaServe::getData('sort_by'); $sort_by = MediaServe::getData('sort_by');
$sort_order = MediaServe::getData('sort_order'); $sort_order = MediaServe::getData('sort_order');
@ -160,7 +182,7 @@ class FileBrowser {
} }
} }
array_multisort($sort, $sort_order, MediaServe::getData('dir_list')); array_multisort($sort, $sort_order, MediaServe::getData('dir_list'));
// Wasteful, but we want directories always on top // Wasteful, but we want directories always on top
$dirs = array(); $dirs = array();
$files = array(); $files = array();
foreach (MediaServe::getData('dir_list') as $file) { foreach (MediaServe::getData('dir_list') as $file) {
@ -173,5 +195,25 @@ class FileBrowser {
MediaServe::storeData('files', $files); MediaServe::storeData('files', $files);
MediaServe::storeData('dirs', $dirs); MediaServe::storeData('dirs', $dirs);
} }
static function filterFiles() {
// TODO: recreating an array seems inefficient, should change
if (($filter = MediaServe::getData('filter')) == "")
return NULL;
$files = array();
$dirs = array();
foreach(MediaServe::getData('files') as $file) {
if (strpos($file[FileBrowser::F_NAME], $filter) !== FALSE) {
$files[] = $file;
}
}
foreach(MediaServe::getData('dirs') as $dir) {
if (strpos($dir[FileBrowser::F_NAME], $filter) !== FALSE) {
$dirs[] = $dir;
}
}
MediaServe::storeData('files', $files);
MediaServe::storeData('dirs', $dirs);
}
} }
?> ?>