diff options
author | Bharat Mediratta <bharat@menalto.com> | 2009-07-02 11:23:40 -0700 |
---|---|---|
committer | Bharat Mediratta <bharat@menalto.com> | 2009-07-02 11:23:40 -0700 |
commit | e5b6193b26cf0f8509a98f7913a1d87fa354da05 (patch) | |
tree | 232a2dacd29acae22795234383140550415dfa63 | |
parent | 495c76f729372e09f7511967c63948e36303e3d7 (diff) |
Partial pass of server_add cleanup. It's broken at this stage since
I've redone the browsing code but I have not implemented the adding
code.
1) Rename index() to browse() since index is too generic.
2) Simplify the data that we pass to _dialog and _tree
3) Change _tree to return list items only, so that the outer dialog
can be a <ul> for consistency.
4) Simplify the data structures so that we're not tracking checked vs.
unchecked status in the PHP code, it's all done in jquery where we
can do it with just a line or two of JS
5) use glob() which pretty much entirely replaces _get_children
-rw-r--r-- | modules/server_add/controllers/server_add.php | 103 | ||||
-rw-r--r-- | modules/server_add/helpers/server_add_menu.php | 2 | ||||
-rw-r--r-- | modules/server_add/js/server_add.js | 52 | ||||
-rw-r--r-- | modules/server_add/views/server_add_tree.html.php | 29 | ||||
-rw-r--r-- | modules/server_add/views/server_add_tree_dialog.html.php | 25 |
5 files changed, 117 insertions, 94 deletions
diff --git a/modules/server_add/controllers/server_add.php b/modules/server_add/controllers/server_add.php index 4cd3cec7..45ec0a2e 100644 --- a/modules/server_add/controllers/server_add.php +++ b/modules/server_add/controllers/server_add.php @@ -18,26 +18,38 @@ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ class Server_Add_Controller extends Controller { - public function index($id) { - $paths = unserialize(module::get_var("server_add", "authorized_paths")); - + public function browse($id) { if (!user::active()->admin) { access::forbidden(); } + + $paths = unserialize(module::get_var("server_add", "authorized_paths")); + foreach (array_keys($paths) as $path) { + $files[$path] = basename($path); + } + $item = ORM::factory("item", $id); $view = new View("server_add_tree_dialog.html"); $view->item = $item; + $view->tree = new View("server_add_tree.html"); + $view->tree->files = $files; + print $view; + } - $tree = new View("server_add_tree.html"); - $tree->data = array(); - $tree->checked = false; - $tree->tree_id = "tree_$id"; - foreach (array_keys($paths) as $path) { - $tree->data[$path] = array("path" => $path, "is_dir" => true); + private function _validate_path($path) { + if (!is_readable($path) || is_link($path)) { + throw new Exception("@todo BAD_PATH"); } - $view->tree = $tree->__toString(); - print $view; + + $authorized_paths = unserialize(module::get_var("server_add", "authorized_paths")); + foreach (array_keys($authorized_paths) as $valid_path) { + if (strpos($path, $valid_path) === 0) { + return; + } + } + + throw new Exception("@todo BAD_PATH"); } public function children() { @@ -45,31 +57,32 @@ class Server_Add_Controller extends Controller { access::forbidden(); } - $paths = unserialize(module::get_var("server_add", "authorized_paths")); - $path_valid = false; - $path = $this->input->post("path"); - $checked = $this->input->post("checked") == "true"; + $path = $this->input->get("path"); + $this->_validate_path($path); - foreach (array_keys($paths) as $valid_path) { - if ($path_valid = strpos($path, $valid_path) === 0) { - break; + $tree = new View("server_add_tree.html"); + $tree->files = array(); + $tree->tree_id = substr(md5($path), 10); + + foreach (glob("$path/*") as $file) { + if (!is_readable($file)) { + continue; } - } - if (empty($path_valid)) { - throw new Exception("@todo BAD_PATH"); - } - if (!is_readable($path) || is_link($path)) { - kohana::show_404(); - } + if (!is_dir($file)) { + $ext = strtolower(pathinfo($file, PATHINFO_EXTENSION)); + if (!in_array($ext, array("gif", "jpeg", "jpg", "png", "flv", "mp4"))) { + continue; + } + } - $tree = new View("server_add_tree.html"); - $tree->data = $this->_get_children($path); - $tree->checked = $checked; - $tree->tree_id = "tree_" . md5($path); + $tree->files[$file] = basename($file); + } print $tree; } + /* ================================================================================ */ + function start($id) { if (!user::active()->admin) { access::forbidden(); @@ -237,36 +250,4 @@ class Server_Add_Controller extends Controller { return $count; } - - private function _get_children($path) { - $directory_list = $file_list = array(); - $files = new DirectoryIterator($path); - foreach ($files as $file) { - if ($file->isDot() || $file->isLink()) { - continue; - } - $filename = $file->getFilename(); - if ($filename[0] != ".") { - if ($file->isDir()) { - $directory_list[$filename] = array("path" => $file->getPathname(), "is_dir" => true); - } else { - $extension = strtolower(substr(strrchr($filename, '.'), 1)); - if ($file->isReadable() && - in_array($extension, array("gif", "jpeg", "jpg", "png", "flv", "mp4"))) { - $file_list[$filename] = array("path" => $file->getPathname(), "is_dir" => false); - } - } - } - } - - ksort($directory_list); - ksort($file_list); - - // We can't use array_merge here because if a file name is numeric, it will - // get renumbered, so lets do it ourselves - foreach ($file_list as $file => $fileinfo) { - $directory_list[$file] = $fileinfo; - } - return $directory_list; - } }
\ No newline at end of file diff --git a/modules/server_add/helpers/server_add_menu.php b/modules/server_add/helpers/server_add_menu.php index 23878913..0f01eb64 100644 --- a/modules/server_add/helpers/server_add_menu.php +++ b/modules/server_add/helpers/server_add_menu.php @@ -38,7 +38,7 @@ class server_add_menu_Core { $server_add = Menu::factory("dialog") ->id("server_add") ->label(t("Add from server")) - ->url(url::site("server_add/index/$item->id")); + ->url(url::site("server_add/browse/$item->id")); $add_photos_item = $menu->get("add_photos_item"); $add_photos_menu = $menu->get("add_photos_menu"); diff --git a/modules/server_add/js/server_add.js b/modules/server_add/js/server_add.js index e2526dbe..47340c45 100644 --- a/modules/server_add/js/server_add.js +++ b/modules/server_add/js/server_add.js @@ -1,3 +1,44 @@ +function open_close_branch(path, id) { + var parent = $("#file_" + id); + var children = $("#tree_" + id); + var icon = parent.find(".ui-icon:first"); + + if (!children.html()) { + parent.addClass("gLoadingSmall"); + $.ajax({ + url: GET_CHILDREN_URL.replace("__PATH__", path), + success: function(data, textStatus) { + children.html(data); + parent.removeClass("gLoadingSmall"); + + // Propagate checkbox value + children.find("input[type=checkbox]").attr( + "checked", parent.find("input[type=checkbox]:first").attr("checked")); + }, + }); + } + + children.slideToggle("fast", function() { + if (children.is(":hidden")) { + icon.addClass("ui-icon-plus"); + icon.removeClass("ui-icon-minus"); + } else { + icon.addClass("ui-icon-minus"); + icon.removeClass("ui-icon-plus"); + parent.removeClass("gCollapsed"); + } + }); +} + +function click_node(checkbox) { + var parent = $(checkbox).parents("li").get(0); + var checked = $(checkbox).attr("checked"); + $(parent).find("input[type=checkbox]").attr("checked", checked); +} + +/* ================================================================================ */ + +/* var paused = false; var task = null; @@ -82,16 +123,6 @@ function get_url(uri, task_id) { return url; } -function checkbox_click(checkbox) { - var parent = $(checkbox).parents("li").get(0); - var checked = $(checkbox).attr("checked"); - if (!$(parent).hasClass("gCollapsed")) { - $(parent).find(".gCheckboxTree input[type=checkbox]").attr("checked", checked); - } - var checkboxes = $("#gServerAdd :checkbox[checked]"); - $("#gServerAdd form :submit").attr("disabled", checkboxes.length == 0); -} - function load_children(icon) { $("#gDialog").addClass("gDialogLoadingLarge"); var parent = icon.parentNode; @@ -203,3 +234,4 @@ function display_upload_error(error) { }); } +*/ diff --git a/modules/server_add/views/server_add_tree.html.php b/modules/server_add/views/server_add_tree.html.php index 44f6bfbc..f8205a8b 100644 --- a/modules/server_add/views/server_add_tree.html.php +++ b/modules/server_add/views/server_add_tree.html.php @@ -1,12 +1,19 @@ <?php defined("SYSPATH") or die("No direct script access.") ?> -<ul id="<?= $tree_id ?>" class="gCheckboxTree"> - <? foreach ($data as $file => $file_info): ?> - <li class="<?= empty($file_info["is_dir"]) ? "gFile" : "gDirectory gCollapsed ui-icon-left" ?>"> - <? if (!empty($file_info["is_dir"])): ?> - <span class="ui-icon ui-icon-plus"></span> - <? endif ?> - <label> <?= form::checkbox("checkbox[]", p::clean($file_info["path"]), $checked) . " " . p::clean($file) ?> </label> - <div class="gServerAddChildren" style="display: none"></div> - </li> - <? endforeach ?> -</ul> +<? foreach ($files as $file => $name): ?> +<? $id = substr(md5($file), 10) ?> +<li id="file_<?= $id ?>" class="<?= is_file($file) ? "gFile" : "gDirectory gCollapsed ui-icon-left" ?>"> + <? if (is_dir($file)): ?> + <span onclick="open_close_branch('<?=$file?>', '<?=$id?>')" class="ui-icon ui-icon-plus"></span> + <? endif ?> + <label> + <?= form::checkbox("path[]", p::clean($file), false, "onclick=click_node(this)") ?> + <?= p::clean($name) ?> + </label> + <? if (is_dir($file)): ?> + <ul id="tree_<?= $id ?>" style="display: none"></ul> + <? endif ?> +</li> +<? endforeach ?> +<? if (!$files): ?> +<li class="gFile"> <i> <?= t("empty") ?> </i> </li> +<? endif ?> diff --git a/modules/server_add/views/server_add_tree_dialog.html.php b/modules/server_add/views/server_add_tree_dialog.html.php index e2aa5d44..6e4db620 100644 --- a/modules/server_add/views/server_add_tree_dialog.html.php +++ b/modules/server_add/views/server_add_tree_dialog.html.php @@ -1,26 +1,29 @@ <?php defined("SYSPATH") or die("No direct script access.") ?> -<script> - var FATAL_ERROR = "<?= t("Fatal Error") ?>"; - var FILE_IMPORT_WARNING = "<?= t("Add from server warning") ?>"; - $("#gServerAdd").ready(function() { - init_server_add_form(); - }); +<script type="text/javascript"> + var GET_CHILDREN_URL = "<?= url::site("server_add/children?path=__PATH__") ?>"; </script> + <div id="gServerAdd"> <h1 style="display: none;"><?= t("Add Photos to '%title'", array("title" => p::clean($item->title))) ?></h1> <p id="gDescription"><?= t("Photos will be added to album:") ?></p> <ul class="gBreadcrumbs"> <? foreach ($item->parents() as $parent): ?> - <li><?= p::clean($parent->title) ?></li> + <li> + <?= p::clean($parent->title) ?> + </li> <? endforeach ?> - <li class="active"><?= p::clean($item->title) ?></li> + <li class="active"> + <?= p::clean($item->title) ?> + </li> </ul> - <?= form::open(url::abs_site("__ARGS__/{$id}__TASK_ID__?csrf=$csrf"), array("method" => "post")) ?> - <div id="gServerAddTree" > + <?= form::open(url::abs_site("server_add/add"), array("method" => "post")) ?> + <?= access::csrf_form_field(); ?> + <ul id="gServerAddTree" class="gCheckboxTree"> <?= $tree ?> - </div> + </ul> + <span> <?= form::submit(array("id" => "gServerPauseButton", "name" => "add", "disabled" => true, "class" => "submit", "style" => "display:none"), t("Pause")) ?> <?= form::submit(array("id" => "gServerAddButton", "name" => "add", "disabled" => true, "class" => "submit"), t("Add")) ?> |