Implementation of local import module. Still need work on better

status messages.
This commit is contained in:
Tim Almdal
2009-02-21 02:28:19 +00:00
parent af2f7f2c56
commit c2fa42cf6c
18 changed files with 706 additions and 0 deletions
@@ -0,0 +1,82 @@
<?php defined("SYSPATH") or die("No direct script access.");
/**
* Gallery - a web based photo album viewer and editor
* Copyright (C) 2000-2008 Bharat Mediratta
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
class Admin_Local_Import_Controller extends Admin_Controller {
public function index() {
$view = new Admin_View("admin.html");
$view->content = local_import::get_admin_page();
print $view;
}
public function add_path() {
access::verify_csrf();
$form = local_import::get_admin_form();
$paths = unserialize(module::get_var("local_import", "authorized_paths", "a:0:{}"));
if ($form->validate()) {
if (is_readable($form->add_path->path->value)) {
$paths[$form->add_path->path->value] = 1;
module::set_var("local_import", "authorized_paths", serialize($paths));
$path_count = count($paths) - 1;
$path_view = new View("local_import_dir_list.html");
$path_view->paths = array_keys($paths);
$form->add_path->inputs["path"]->value("");
print json_encode(
array("result" => "success",
"paths" => $path_view->__toString(),
"form" => $form->__toString()));
} else {
$form->add_path->inputs["path"]->error("not_readable");
print json_encode(array("result" => "error", "form" => $form->__toString()));
}
} else {
print json_encode(array("result" => "error", "form" => $form->__toString()));
}
}
public function remove() {
access::verify_csrf();
$path = $this->input->post("path");
$paths = unserialize(module::get_var("local_import", "authorized_paths"));
unset($paths[$path]);
module::set_var("local_import", "authorized_paths", serialize($paths));
$view = new View("local_import_dir_list.html");
$view->paths = array_keys($paths);
print $view->render();
}
public function autocomplete() {
access::verify_csrf();
$directories = array();
$path_prefix = $this->input->get("q");
foreach (glob("{$path_prefix}*") as $file) {
if (is_dir($file)) {
$directories[] = $file;
}
}
print implode("\n", $directories);
}
}
@@ -0,0 +1,96 @@
<?php defined("SYSPATH") or die("No direct script access.");
/**
* Gallery - a web based photo album viewer and editor
* Copyright (C) 2000-2008 Bharat Mediratta
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
class Local_Import_Controller extends Controller {
public function index($id) {
$paths = unserialize(module::get_var("local_import", "authorized_paths"));
$item = ORM::factory("item", $id);
access::can("local_import", $item);
$view = new View("local_import_tree_dialog.html");
$view->action = url::site("local_import/add/$id");
$view->hidden = array("csrf" => access::csrf_token(), "base_url" => url::base(true));
$view->parents = $item->parents();
$view->album_title = $item->title;
$tree = new View("local_import_tree.html");
$tree->data = array();
$tree->uid = "tree_$id";
foreach (array_keys($paths) as $path) {
$tree->data[$path] = array("path" => $path, "is_dir" => true);
}
$view->tree = $tree->__toString();
print $view;
}
public function children() {
access::verify_csrf();
$path = $this->input->post("path");
if (!is_readable($path)) {
kohana::show_404();
}
$tree = new View("local_import_tree.html");
$tree->data = $this->_get_children($path);
$tree->uid = "tree_" . md5($path);
print $tree;
}
function add($id) {
access::verify_csrf();
$parent = ORM::factory("item", $id);
access::can("local_import", $item);
if (!$parent->loaded) {
throw new Exception("@todo BAD_ALBUM");
}
$path = $this->input->post("path");
set_time_limit(30);
$photo = photo::create($parent, $path, basename($path), basename($path));
log::success("content", t("Added photo"),
html::anchor("photos/{$photo->id}", "View photo"));
message::success(t("Added photo %photo_title", array("photo_title" => $photo->title)));
}
private function _get_children($path) {
$file_list = array();
$files = scandir($path);
foreach ($files as $file) {
if ($file[0] != ".") {
$full_path = "$path/$file";
if (is_dir($full_path)) {
$file_list[$file] =
array("path" => $full_path, "is_dir" => true);
} else {
$extension = strtolower(substr(strrchr($file, '.'), 1));
// Make sure the file is readable
if (is_readable($full_path) &&
in_array($extension, Image::$allowed_types)) {
$file_list[$file] = array("path" => $full_path);
}
}
}
}
return $file_list;
}
}
+29
View File
@@ -0,0 +1,29 @@
#gLocalImportAdmin {
margin:auto;
text-align: left;
}
#gLocalImportAdmin form fieldset {
border: medium none;
}
#gLocalImportAdmin legend {
display: none;
}
#gLocalImportAdmin .gWarning {
background-color: #FFFF99;
}
#gAuthorizedPath {
margin: 0 !important;
padding: 0.3em 1.5em 0.3em 1em;
}
#gLocalImportAdmin #path {
width: 80%;
}
.gRemoveDir:hover {
cursor: pointer;
}
@@ -0,0 +1,49 @@
.ac_results {
padding: 0px;
border: 1px solid black;
background-color: white;
overflow: hidden;
text-align: left;
z-index: 99999;
}
.ac_results ul {
width: 100%;
list-style-position: outside;
list-style: none;
padding: 0;
margin: 0;
}
.ac_results li {
margin: 0px;
padding: 2px 5px;
cursor: default;
display: block;
/*
if width will be 100% horizontal scrollbar will apear
when scroll mode will be used
*/
/*width: 100%;*/
font: menu;
font-size: 12px;
/*
it is very important, if line-height not setted or setted
in relative units scroll will be broken in firefox
*/
line-height: 16px;
overflow: hidden;
}
.ac_loading {
background: white url('indicator.gif') right center no-repeat;
}
.ac_odd {
background-color: #eee;
}
.ac_over {
background-color: #0A246A;
color: white;
}
+53
View File
@@ -0,0 +1,53 @@
.gCheckboxTree input {
display: inline;
}
.gCheckboxTree li {
padding: 0;
float: none;
}
.gCheckboxTree .ui-icon {
cursor: pointer;
}
.gFile {
padding-left: 2.5em;
}
#gImportProgress {
visibility: hidden;
height: 1em;
}
#gLocalImport #gLocalImportTree {
border: 1px solid #CCCCCC;
height: 25em;
overflow: auto;
margin-bottom: .5em;
padding-top: .5em;
padding-bottom: .5em;
}
#gLocalImport ul ul li {
padding-left: 1.2em;
}
#gLocalImport ul li .gFile {
padding-left: 2.5em;
}
#gLocalImport .gBreadcrumbs {
font-size: 1em;
padding: 0;
margin: 0;
border-top-width: 0px;
}
#gLocalImport p {
margin: 0;
}
#gLocalImport .gBreadcrumbs li {
padding: 10px 6px 10px 16px;
}
@@ -0,0 +1,42 @@
<?php defined("SYSPATH") or die("No direct script access.");
/**
* Gallery - a web based photo album viewer and editor
* Copyright (C) 2000-2008 Bharat Mediratta
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
class local_import {
public static function get_admin_page() {
$template = new View("local_import_admin.html");
$paths = unserialize(module::get_var("local_import", "authorized_paths", "a:0:{}"));
$path_list = new View("local_import_dir_list.html");
$path_list->paths = array_keys($paths);
$template->path_list = $path_list->render();
$template->add_form = self::get_admin_form()->render();
return $template;
}
public static function get_admin_form() {
$form = new Forge("admin/local_import/add_path", "", "post", array("id" => "gLocalImportAdminForm"));
$add_path = $form->group("add_path");
$add_path->input("path")->label(t("Path"))->rules("required")
->error_messages("not_readable", t("The directory is not readable by the webserver"));
$add_path->submit("add")->value(t("Add Path"));
return $form;
}
}
@@ -0,0 +1,30 @@
<?php defined("SYSPATH") or die("No direct script access.");
/**
* Gallery - a web based photo album viewer and editor
* Copyright (C) 2000-2008 Bharat Mediratta
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
class local_import_block_Core {
public static function head($theme) {
$head[] = html::script("modules/local_import/js/local_import.js");
$url = url::file("modules/local_import/css/local_import.css");
$head[] = "<link rel=\"stylesheet\" type=\"text/css\" href=\"$url\" " .
"media=\"screen,print,projection\" />";
return implode("\n", $head);
}
}
@@ -0,0 +1,43 @@
<?php defined("SYSPATH") or die("No direct script access.");
/**
* Gallery - a web based photo album viewer and editor
* Copyright (C) 2000-2008 Bharat Mediratta
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
class local_import_installer {
public static function install() {
$db = Database::instance();
$version = module::get_version("local_import");
if ($version == 0) {
access::register_permission("local_import", t("Import Local Files"));
access::allow(user::lookup(2), "view", ORM::factory("item", 1));
module::set_version("local_import", 1);
module::set_var("local_import", "authorized_paths", serialize(array()));
}
}
public static function uninstall() {
access::delete_permission("local_import");
$module = module::get("local_import");
$db = Database::instance();
$db->delete("vars", array("module_name" => $module->name));
module::delete("local_import");
}
}
@@ -0,0 +1,42 @@
<?php defined("SYSPATH") or die("No direct script access.");
/**
* Gallery - a web based photo album viewer and editor
* Copyright (C) 2000-2008 Bharat Mediratta
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
class local_import_menu_Core {
static function admin($menu, $theme) {
$menu->get("content_menu")
->append(Menu::factory("link")
->id("local_import")
->label(t("Local Import"))
->url(url::site("admin/local_import")));
}
static function site($menu, $theme) {
$item = $theme->item();
$paths = unserialize(module::get_var("local_import", "authorized_paths"));
if ($item && access::can("local_import", $item) && $item->is_album() &&!empty($paths)) {
$menu->get("options_menu")
->append(Menu::factory("dialog")
->id("local_import")
->label(t("Import from server"))
->url(url::site("local_import/index/$item->id")));
}
}
}
@@ -0,0 +1,44 @@
<?php defined("SYSPATH") or die("No direct script access.");
/**
* Gallery - a web based photo album viewer and editor
* Copyright (C) 2000-2008 Bharat Mediratta
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
class local_import_theme_Core {
static function admin_head($theme) {
$head[] = "<link media=\"screen, projection\" rel=\"stylesheet\" type=\"text/css\" href=\"" .
url::file("modules/local_import/css/jquery.autocomplete.css") . "\" />";
$head[] = "<link media=\"screen, projection\" rel=\"stylesheet\" type=\"text/css\" href=\"" .
url::file("modules/local_import/css/admin.css") . "\" />";
$base = url::base(true);
$csrf = access::csrf_token();
$head[] = "<script> var base_url = \"$base\"; var csrf = \"$csrf\";</script>";
$head[] = html::script("modules/local_import/js/jquery.autocomplete.pack.js");
$head[] = html::script("modules/local_import/js/admin.js");
return implode("\n", $head);
}
static function head($theme) {
$head[] = "<link media=\"screen, projection\" rel=\"stylesheet\" type=\"text/css\" href=\"" .
url::file("modules/local_import/css/local_import.css") . "\" />";
$head[] = html::script("modules/local_import/js/local_import.js");
return implode("\n", $head);
}
}
+47
View File
@@ -0,0 +1,47 @@
/**
* Set up autocomplete on the server path list
*
*/
$("document").ready(function() {
add_autocomplete();
ajaxify_import_form();
add_onclick();
});
function add_autocomplete() {
$("#gLocalImportAdmin input:text").autocomplete(base_url + "admin/local_import/autocomplete", {
extraParams: {csrf: csrf},
mustMatch: true,
max: 256});
}
function ajaxify_import_form(options) {
$("#gLocalImportAdmin form").ajaxForm({
dataType: "json",
success: function(data) {
if (data.form) {
$("#gLocalImportAdmin form").replaceWith(data.form);
ajaxify_import_form();
add_autocomplete();
}
if (data.result == "success") {
$("#gNoImportPaths").css("display", "none");
$("#gAuthorizedPath").html(data.paths);
add_onclick();
}
}
});
}
function add_onclick() {
$(".gRemoveDir").click(function() {
var parent = $(this).parent();
$.post(
base_url + "admin/local_import/remove",
{csrf: csrf,
path: parent.text().replace(/^\s\s*/, "").replace(/\s\s*$/, "")},
function(data, textStatus) {
$("#gAuthorizedPath").html(data);
add_onclick();
});
});
}
File diff suppressed because one or more lines are too long
+60
View File
@@ -0,0 +1,60 @@
function open_close_branch(icon, event) {
var parent = icon.parentNode;
//var label = $(parent).find("label");
var children = $(parent).find(".gCheckboxTree");
var closed = $(icon).hasClass("ui-icon-plus");
if (closed) {
if (children.length == 0) {
load_children( $(icon).attr("ref"), function(data, textStatus) {
$(parent).append(data);
$(icon).addClass("ui-icon-minus");
$(icon).removeClass("ui-icon-plus");
var checkbox = $(parent).find(":checkbox")[0];
checkbox_click(checkbox, null);
});
} else {
$(icon).addClass("ui-icon-minus");
$(icon).removeClass("ui-icon-plus");
$(parent).children("ul").slideDown("fast");
}
} else {
$(icon).addClass("ui-icon-plus");
$(icon).removeClass("ui-icon-minus");
$(parent).children("ul").slideUp("fast");
}
}
function checkbox_click(checkbox, event) {
var parents = $(checkbox).parents("li");
var parent = parents.get(0);
$(parent).find(".gCheckboxTree :checkbox").attr("checked", checkbox.checked);
var checked = $("#gLocalImport .gFile :checkbox[checked]");
$("#gLocalImport form :submit").attr("disabled", checked.length == 0);
}
function load_children(path, callback) {
var csrf = $("#gLocalImport form :hidden[name='csrf']")[0].value;
var base_url = $("#gLocalImport form :hidden[name='base_url']")[0].value;
$.post(base_url + "local_import/children",
{csrf: csrf, path: path}, callback);
}
function do_import(submit, event) {
event.preventDefault();
$("#gImportProgress").progressbar('value', 0);
$("#gImportProgress").css("visibility", "visible");
var check_list = $("#gLocalImport .gFile :checkbox[checked]");
var current = 0;
var csrf = $("#gLocalImport form :hidden[name='csrf']")[0].value;
var url = $("#gLocalImport form").attr("action");
$.each(check_list, function () {
var path = $(this).val();
$.post(url, {csrf: csrf, path: path}, function(data, status) {
});
current++;
$("#gImportProgress").progressbar('value', current / check_list.length * 100);
});
document.location.reload();
return false;
}
+3
View File
@@ -0,0 +1,3 @@
name = Local Import
description = Allows authorized users to load images from the local server
version = 1
@@ -0,0 +1,10 @@
<? defined("SYSPATH") or die("No direct script access."); ?>
<div id="gLocalImportAdmin">
<h2>
<?= t("Local Import Admininstration") ?>
</h2>
<div id="gAuthorizedPath">
<?= $path_list ?>
</div>
<?= $add_form ?>
</div>
@@ -0,0 +1,13 @@
<? defined("SYSPATH") or die("No direct script access."); ?>
<span><?= t("Authorized Paths") ?></span>
<ul id="gPathList">
<? foreach ($paths as $id => $path): ?>
<li class="ui-icon-left">
<span id="icon_<?= $id?>" class="gRemoveDir ui-icon ui-icon-trash"></span>
<?= $path ?>
</li>
<? endforeach ?>
</ul>
<div id="gNoImportPaths" <? if (!empty($paths)): ?>style="display:none"<? endif ?>>
<span class="gWarning"><?= t("No Authorized upload paths defined") ?></span>
</div>
@@ -0,0 +1,22 @@
<? defined("SYSPATH") or die("No direct script access."); ?>
<script type="text/javascript">
$("#<?= $uid ?>").ready(function() {
$("#<?= $uid ?> span.ui-icon").click(function(event) {
open_close_branch(this, event);
});
$("#<?= $uid ?> :checkbox").click(function(event) {
checkbox_click(this, event);
});
});
</script>
<ul id="<?= $uid ?>" 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" ref="<?= $file_info["path"] ?>"></span>
<? endif ?>
<?= "<label>" . form::checkbox("checkbox", $file_info["path"]) . " $file</label>" ?>
</li>
<? endforeach ?>
</ul>
@@ -0,0 +1,28 @@
<? defined("SYSPATH") or die("No direct script access."); ?>
<script type="text/javascript">
$("#gLocalImport").ready(function() {
$("#gLocalImport :submit").click(function(event) {
do_import(this, event);
});
$("#gImportProgress").progressbar({value:50});
});</script>
<div id="gLocalImport">
<h1 style="display: none;"><?= sprintf(t("Import Photos to '%s'"), $album_title) ?></h1>
<p id="gDescription"><?= t("Photos will be imported to album:") ?></p>
<ul class="gBreadcrumbs">
<? foreach ($parents as $parent): ?>
<li><?= $parent->title ?></li>
<? endforeach ?>
<li class="active"><?= $album_title ?></li>
</ul>
<?= form::open($action, array("method" => "post"), $hidden) ?>
<div id="gLocalImportTree" >
<?= $tree ?>
</div>
<?= form::submit(array("id" => "gImportButton", "name" => "import", "disabled" => true),
t("Import")) ?>
<div id="gImportProgress"></div>
<?= form::close() ?>
</div>