diff options
-rw-r--r-- | modules/gallery/helpers/movie.php | 2 | ||||
-rw-r--r-- | modules/gallery/helpers/photo.php | 2 | ||||
-rw-r--r-- | modules/server_add/controllers/admin_server_add.php | 14 | ||||
-rw-r--r-- | modules/server_add/controllers/server_add.php | 126 | ||||
-rw-r--r-- | modules/server_add/helpers/server_add_menu.php | 2 | ||||
-rw-r--r-- | modules/server_add/js/server_add.js | 70 | ||||
-rw-r--r-- | modules/server_add/views/server_add_tree.html.php | 31 | ||||
-rw-r--r-- | modules/server_add/views/server_add_tree_dialog.html.php | 35 |
8 files changed, 171 insertions, 111 deletions
diff --git a/modules/gallery/helpers/movie.php b/modules/gallery/helpers/movie.php index fcf1cc54..54159294 100644 --- a/modules/gallery/helpers/movie.php +++ b/modules/gallery/helpers/movie.php @@ -82,7 +82,7 @@ class movie_Core { $movie->rand_key = ((float)mt_rand()) / (float)mt_getrandmax(); // Randomize the name if there's a conflict - while (ORM::Factory("item") + while (ORM::factory("item") ->where("parent_id", $parent->id) ->where("name", $movie->name) ->find()->id) { diff --git a/modules/gallery/helpers/photo.php b/modules/gallery/helpers/photo.php index a4bc853b..e8a4f357 100644 --- a/modules/gallery/helpers/photo.php +++ b/modules/gallery/helpers/photo.php @@ -81,7 +81,7 @@ class photo_Core { $photo->rand_key = ((float)mt_rand()) / (float)mt_getrandmax(); // Randomize the name if there's a conflict - while (ORM::Factory("item") + while (ORM::factory("item") ->where("parent_id", $parent->id) ->where("name", $photo->name) ->find()->id) { diff --git a/modules/server_add/controllers/admin_server_add.php b/modules/server_add/controllers/admin_server_add.php index a30215b8..30109f42 100644 --- a/modules/server_add/controllers/admin_server_add.php +++ b/modules/server_add/controllers/admin_server_add.php @@ -38,10 +38,7 @@ class Admin_Server_Add_Controller extends Admin_Controller { $path = $form->add_path->path->value; $paths[$path] = 1; module::set_var("server_add", "authorized_paths", serialize($paths)); - $form->add_path->inputs->path->value = ""; - message::success(t("Added path %path", array("path" => p::clean($path)))); - server_add::check_config($paths); url::redirect("admin/server_add"); } else { @@ -61,11 +58,12 @@ class Admin_Server_Add_Controller extends Admin_Controller { $path = $this->input->get("path"); $paths = unserialize(module::get_var("server_add", "authorized_paths")); - unset($paths[$path]); - message::success(t("Removed path %path", array("path" => p::clean($path)))); - module::set_var("server_add", "authorized_paths", serialize($paths)); - server_add::check_config($paths); - + if (isset($paths[$path])) { + unset($paths[$path]); + message::success(t("Removed path %path", array("path" => p::clean($path)))); + module::set_var("server_add", "authorized_paths", serialize($paths)); + server_add::check_config($paths); + } url::redirect("admin/server_add"); } diff --git a/modules/server_add/controllers/server_add.php b/modules/server_add/controllers/server_add.php index 05ea5058..e2b1b01a 100644 --- a/modules/server_add/controllers/server_add.php +++ b/modules/server_add/controllers/server_add.php @@ -18,60 +18,94 @@ * 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(); } - $item = ORM::factory("item", $id); - $view = new View("server_add_tree_dialog.html"); - $view->action = url::abs_site("__ARGS__/{$id}__TASK_ID__?csrf=" . access::csrf_token()); - $view->parents = $item->parents(); - $view->album_title = $item->title; - $tree = new View("server_add_tree.html"); - $tree->data = array(); - $tree->checked = false; - $tree->tree_id = "tree_$id"; + $paths = unserialize(module::get_var("server_add", "authorized_paths")); foreach (array_keys($paths) as $path) { - $tree->data[$path] = array("path" => $path, "is_dir" => true); + $files[$path] = basename($path); } - $view->tree = $tree->__toString(); + + $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; } + private function _validate_path($path) { + if (!is_readable($path) || is_link($path)) { + throw new Exception("@todo BAD_PATH"); + } + + $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() { if (!user::active()->admin) { 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 (!is_dir($file)) { + $ext = strtolower(pathinfo($file, PATHINFO_EXTENSION)); + if (!in_array($ext, array("gif", "jpeg", "jpg", "png", "flv", "mp4"))) { + continue; + } } + + $tree->files[$file] = basename($file); } - if (empty($path_valid)) { - throw new Exception("@todo BAD_PATH"); + print $tree; + } + + public function add() { + if (!user::active()->admin) { + access::forbidden(); } + access::verify_csrf(); - if (!is_readable($path) || is_link($path)) { - kohana::show_404(); + $authorized_paths = unserialize(module::get_var("server_add", "authorized_paths")); + + // The paths we receive are full pathnames. Convert that into a tree structure to save space + // in our task. + foreach (Input::instance()->post("path") as $path) { + if (is_dir($path)) { + $dirs[$path] = array(); + } else if (is_file($path)) { + $dir = dirname($path); + $file = basename($path); + $dirs[$dir][] = $file; + } } - $tree = new View("server_add_tree.html"); - $tree->data = $this->_get_children($path); - $tree->checked = $checked; - $tree->tree_id = "tree_" . md5($path); - print $tree; + Kohana::log("alert",print_r($dirs,1)); } + /* ================================================================================ */ + function start($id) { if (!user::active()->admin) { access::forbidden(); @@ -239,36 +273,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..568ef91f 100644 --- a/modules/server_add/js/server_add.js +++ b/modules/server_add/js/server_add.js @@ -1,3 +1,62 @@ +/** + * We've clicked the + icon next to a directory. Load up children of this + * directory from the server and display them. + */ +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"); + } + }); +} + +/** + * We've clicked a checkbox. Propagate the value downwards as necessary. + */ +function click_node(checkbox) { + var parent = $(checkbox).parents("li").get(0); + var checked = $(checkbox).attr("checked"); + $(parent).find("input[type=checkbox]").attr("checked", checked); + + // @todo if we uncheck all the children for a parent, we should uncheck the + // parent itself, otherwise in the code we'll add the entire parent since if + // we find an album as a leaf, we assume that it's never been expanded in the UI. + if ($("#gServerAddTree").find("input[type=checkbox]").is(":checked")) { + $("#gServerAddAddButton").enable(true); + $("#gServerAddAddButton").removeClass("ui-state-disabled"); + } else { + $("#gServerAddAddButton").enable(false); + $("#gServerAddAddButton").addClass("ui-state-disabled"); + } +} + +/* ================================================================================ */ + +/* var paused = false; var task = null; @@ -82,16 +141,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 +252,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 33047fb3..f8205a8b 100644 --- a/modules/server_add/views/server_add_tree.html.php +++ b/modules/server_add/views/server_add_tree.html.php @@ -1,14 +1,19 @@ <?php defined("SYSPATH") or die("No direct script access.") ?> -<script type="text/javascript"> -</script> -<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 8b296987..723d388e 100644 --- a/modules/server_add/views/server_add_tree_dialog.html.php +++ b/modules/server_add/views/server_add_tree_dialog.html.php @@ -1,29 +1,34 @@ <?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($album_title))) ?></h1> + <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 ($parents as $parent): ?> - <li><?= p::clean($parent->title) ?></li> + <? foreach ($item->parents() as $parent): ?> + <li> + <?= p::clean($parent->title) ?> + </li> <? endforeach ?> - <li class="active"><?= p::clean($album_title) ?></li> + <li class="active"> + <?= p::clean($item->title) ?> + </li> </ul> - <?= form::open($action, 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")) ?> + <input id="gServerAddPauseButton" class="submit ui-state-disabled" disabled="disabled" type="submit" + value="<?= t("Pause") ?>" style="display: none"> + <input id="gServerAddAddButton" class="submit ui-state-disabled" disabled="disabled" type="submit" + value="<?= t("Add") ?>"> </span> <?= form::close() ?> <div class="gProgressBar" style="visibility: hidden" ></div> |