Initial import
This commit is contained in:
66
app/controllers/CoverController.php
Normal file
66
app/controllers/CoverController.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
class CoverController {
|
||||
|
||||
static function get_cover($_request, $id, $filename, $size = false) {
|
||||
|
||||
$rev = new Revision($id);
|
||||
if ($rev === false) return "";
|
||||
|
||||
$path = $rev->path();
|
||||
|
||||
$pdf = new PDF($path . "/doc.pdf");
|
||||
if (!$pdf->exists()) {
|
||||
return [404, "Not Found"];
|
||||
}
|
||||
|
||||
if (!$size) {
|
||||
$imgpath = sprintf("%s/cover.jpg", $path);
|
||||
} else {
|
||||
$imgpath = sprintf("%s/cover-%d.jpg", $path, $size);
|
||||
}
|
||||
|
||||
$file = new File($imgpath);
|
||||
|
||||
if ($file->exists()) {
|
||||
$h = $file->hash();
|
||||
$h = "\"$h\"";
|
||||
if ($h == $_request->header("If-None-Match")) {
|
||||
return [304, "Not Modified", [
|
||||
"ETag" => $h,
|
||||
"Cache-Control" => "public, max-age=86400, must-revalidate",
|
||||
]];
|
||||
}
|
||||
return new Image($file);
|
||||
}
|
||||
|
||||
$cover = new Image($path . "/cover.jpg");
|
||||
if (!$cover->exists()) {
|
||||
$pdf->extract_page(0, $cover->path());
|
||||
$cover = new Image($path . "/cover.jpg");
|
||||
}
|
||||
|
||||
if (!$cover->exists()) {
|
||||
return [404, "Not Found"];
|
||||
}
|
||||
|
||||
if ($size === false) $size = $cover->width();
|
||||
|
||||
if ($size >= $cover->width()) {
|
||||
$h = $cover->hash();
|
||||
$h = "\"$h\"";
|
||||
if ($h == $_request->header("If-None-Match")) {
|
||||
return [304, "Not Modified", [
|
||||
"ETag" => $h,
|
||||
"Cache-Control" => "public, max-age=86400, must-revalidate",
|
||||
]];
|
||||
}
|
||||
return $cover;
|
||||
}
|
||||
|
||||
$cover->scale($size);
|
||||
$img = $cover->save($imgpath, "image/jpeg");
|
||||
return $img;
|
||||
}
|
||||
|
||||
}
|
||||
379
app/controllers/DocumentController.php
Normal file
379
app/controllers/DocumentController.php
Normal file
@@ -0,0 +1,379 @@
|
||||
<?php
|
||||
|
||||
class DocumentController {
|
||||
|
||||
public static function browse($pid = 0) {
|
||||
|
||||
$product = new Product($pid);
|
||||
|
||||
return blade("documents", ["product" => $product]);
|
||||
}
|
||||
|
||||
public static function show($id) {
|
||||
$doc = new Document($id);
|
||||
$doc->load();
|
||||
|
||||
return blade("document", [
|
||||
"doc" => $doc
|
||||
]);
|
||||
}
|
||||
|
||||
public static function api_get($id) {
|
||||
return new Document($id);
|
||||
}
|
||||
|
||||
public static function api_set($id) {
|
||||
$doc = new Document($id);
|
||||
if ($doc->valid()) {
|
||||
foreach ($_POST as $k=>$v) {
|
||||
if ($k != "products") {
|
||||
$doc->$k = trim($v);
|
||||
}
|
||||
}
|
||||
$doc->save();
|
||||
|
||||
$pids = explode(",", $_POST['products']);
|
||||
|
||||
$dpl = DocProduct::find([["document", "=", $id]])->all();;
|
||||
|
||||
foreach ($dpl as $dp) {
|
||||
$e = array_search($dp->product, $pids);
|
||||
if ($e !== false) {
|
||||
unset($pids[$e]);
|
||||
continue;
|
||||
}
|
||||
$dp->delete();
|
||||
}
|
||||
|
||||
foreach ($pids as $pid) {
|
||||
$dp = new DocProduct;
|
||||
$dp->document = $id;
|
||||
$dp->product = $pid;
|
||||
$dp->save();
|
||||
}
|
||||
|
||||
$doc->cache_invalidate("products");
|
||||
|
||||
}
|
||||
return $doc;
|
||||
}
|
||||
|
||||
public static function api_get_products($id) {
|
||||
$doc = new Document($id);
|
||||
return $doc->products;
|
||||
}
|
||||
|
||||
public static function api_move($id) {
|
||||
$doc = new Document($id);
|
||||
$from = $_POST['from'];
|
||||
$to = $_POST['to'];
|
||||
|
||||
$dp = DocProduct([["document", "=", $id], ["product", "=", $from]])->first();
|
||||
if ($dp) {
|
||||
$dp->product = $to;
|
||||
$dp->save();
|
||||
}
|
||||
|
||||
$doc->cache_invalidate("products");
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
public static function del_docproduct($doc, $prod) {
|
||||
$dp = DocProduct::find([["document", "=", $doc], ["product", "=", $prod]])->first();
|
||||
if ($dp) {
|
||||
$dp->delete();
|
||||
}
|
||||
return back();
|
||||
}
|
||||
|
||||
public static function get_by_id($id) {
|
||||
return Document::find([["internal_id", "=", trim($id)]])->first();
|
||||
}
|
||||
|
||||
public static function api_merge($id) {
|
||||
$from = new Document($id);
|
||||
$to = new Document($_POST['to']);
|
||||
|
||||
if ($from->id == $to->id) return [];
|
||||
|
||||
if ($from && $to) {
|
||||
$revs = Revision::find([["document", "=", $from->id]]);
|
||||
for ($rev = $revs->first(); $rev = $revs->next(); ) {
|
||||
$rev->document = $to->id;
|
||||
$rev->save();
|
||||
}
|
||||
|
||||
$from->delete();
|
||||
}
|
||||
return new Document($to->id); // Force refresh
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static function api_drag_drop($_request) {
|
||||
$src_id = $_request->post("src_id");
|
||||
$dst_id = $_request->post("dst_id");
|
||||
|
||||
$src_type = $_request->post("src_type");
|
||||
$dst_type = $_request->post("dst_type");
|
||||
|
||||
$src_extra = $_request->post("src_extra");
|
||||
$dst_extra = $_request->post("dst_extra");
|
||||
|
||||
$copy = $_request->post("copy") == "true";
|
||||
|
||||
if (($src_type == "document") && ($dst_type == "document")) {
|
||||
// Merge src into dst
|
||||
$src = new Document($src_id);
|
||||
$dst = new Document($dst_id);
|
||||
|
||||
if ($src->id == $dst->id) return [];
|
||||
|
||||
foreach ($src->revisions as $r) {
|
||||
$r->document = $dst->id;
|
||||
$r->save();
|
||||
}
|
||||
|
||||
$src->delete();
|
||||
$dst->cache_invalidate("revisions");
|
||||
}
|
||||
|
||||
if (($src_type == "document") && ($dst_type == "product")) {
|
||||
|
||||
if ($copy) {
|
||||
$dp = new DocProduct();
|
||||
$dp->document = $src_id;
|
||||
$dp->product = $dst_id;
|
||||
$dp->save();
|
||||
} else {
|
||||
|
||||
// Move document into product
|
||||
$dpl = DocProduct::find([["document", "=", $src_id], ["product", "=", $src_extra]])->all();
|
||||
foreach ($dpl as $dp) {
|
||||
$dp->product = $dst_id;
|
||||
$dp->save();
|
||||
}
|
||||
$prod = new Product($src_extra);
|
||||
$prod->cache_invalidate("documents");
|
||||
}
|
||||
$doc = new Document($src_id);
|
||||
$doc->cache_invalidate("products");
|
||||
$prod = new Product($dst_id);
|
||||
$prod->cache_invalidate("documents");
|
||||
}
|
||||
|
||||
if (($src_type == "product") && ($dst_type == "product")) {
|
||||
|
||||
if ($src_id == $dst_id) return [];
|
||||
// Move product into product
|
||||
$sp = new Product($src_id);
|
||||
$dp = new Product($dst_id);
|
||||
$sp->parent = $dp->id;
|
||||
$sp->save();
|
||||
$sp->cache_invalidate("parent");
|
||||
$sp->cache_invalidate("children");
|
||||
$dp->cache_invalidate("parent");
|
||||
$dp->cache_invalidate("children");
|
||||
}
|
||||
|
||||
|
||||
return [200, ["didit" => "true"]];
|
||||
}
|
||||
|
||||
public static function merge($id) {
|
||||
|
||||
$doc = new Document($id);
|
||||
|
||||
$proc = new Process("pdftk");
|
||||
|
||||
foreach ($doc->revisions as $rev) {
|
||||
$proc->arg($rev->path() . "/doc.pdf");
|
||||
}
|
||||
|
||||
$nr = new Revision;
|
||||
$nr->document = $doc->id;
|
||||
$nr->revno = "NEW";
|
||||
$nr->save();
|
||||
|
||||
|
||||
$out = new File($nr->path() . "/doc.pdf");
|
||||
$out->parent()->mkdir();
|
||||
|
||||
$proc->arg("output");
|
||||
$proc->arg((string)$out);
|
||||
$r = $proc->execute();
|
||||
|
||||
if ($r != 0) {
|
||||
print("<pre>");
|
||||
print_r($proc->stderr());
|
||||
print("</pre>");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
return redirect("/document/" . $doc->id);
|
||||
}
|
||||
|
||||
public static function create_overview($id) {
|
||||
|
||||
$job = new GeminiJob($id, "document:$id");
|
||||
$jobid = $job->queue();
|
||||
flash("success", "Job queued as ID " . $jobid);
|
||||
|
||||
return redirect("/document/" . $id);
|
||||
}
|
||||
|
||||
public static function api_get_title_fragment($_request) {
|
||||
$q = $_request->post("title");
|
||||
$db = DB::getInstance();
|
||||
$q1 = $db->query("
|
||||
SELECT
|
||||
DISTINCT title
|
||||
FROM (
|
||||
SELECT
|
||||
DISTINCT title
|
||||
FROM
|
||||
document
|
||||
WHERE
|
||||
title LIKE :s
|
||||
UNION SELECT
|
||||
DISTINCT subtitle AS title
|
||||
FROM
|
||||
document
|
||||
WHERE
|
||||
subtitle LIKE :s
|
||||
UNION SELECT
|
||||
DISTINCT subsubtitle AS title
|
||||
FROM
|
||||
document
|
||||
WHERE subsubtitle LIKE :s
|
||||
) AS DERIVED", ["s" => $q . "%"]);
|
||||
$o = $db->all($q1);
|
||||
|
||||
if ($o->count() == 0) {
|
||||
return [404, "Not Found"];
|
||||
}
|
||||
if ($o->count() != 1) {
|
||||
return [413, "Content Too Large"];
|
||||
}
|
||||
return $o[0]->title;
|
||||
}
|
||||
|
||||
public static function separate($id) {
|
||||
|
||||
$doc = new Document($id);
|
||||
$firstprod = $doc->products[0];
|
||||
|
||||
$count = 0;
|
||||
|
||||
foreach ($doc->revisions as $rev) {
|
||||
$count++;
|
||||
if ($count == 1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$newdoc = $doc->duplicate();
|
||||
$newdoc->subsubtitle .= " - $count";
|
||||
$rev->document = $newdoc->id;
|
||||
$rev->save();
|
||||
}
|
||||
|
||||
|
||||
return redirect("/documents/" . $firstprod);
|
||||
}
|
||||
|
||||
public static function api_get_metadata($id) {
|
||||
return DocMeta::find([["document", "=", $id]])->orderBy("metadata")->all();
|
||||
}
|
||||
|
||||
public static function api_new_metadata($_request, $id) {
|
||||
$doc = new Document($id);
|
||||
$doc->set_metadata($_request->put("item_id"), "");
|
||||
return DocMeta::find([["document", "=", $id]])->orderBy("metadata")->all();
|
||||
}
|
||||
|
||||
public static function api_set_metadata($_request, $id, $metadata) {
|
||||
$doc = new Document($id);
|
||||
$doc->set_metadata($metadata, $_request->post('data'));
|
||||
return DocMeta::find([["document", "=", $id]])->orderBy("metadata")->all();
|
||||
}
|
||||
|
||||
public static function api_delete_metadata($id, $metadata) {
|
||||
$doc = new Document($id);
|
||||
$doc->remove_metadata($metadata);
|
||||
return DocMeta::find([["document", "=", $id]])->orderBy("metadata")->all();
|
||||
}
|
||||
|
||||
public static function api_available_metadata($id) {
|
||||
$exist = DocMeta::find([["document", "=", $id]])->orderBy("metadata")->all();
|
||||
$metas = MetaType::find()->all();
|
||||
|
||||
|
||||
$out = new Collection();
|
||||
|
||||
foreach ($metas as $meta) {
|
||||
$e = false;
|
||||
foreach ($exist as $ex) {
|
||||
if ($ex->metadata == $meta->id) {
|
||||
$e = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!$e) {
|
||||
$cl = new stdClass;
|
||||
$cl->key = $meta->id;
|
||||
$cl->value = $meta->name;
|
||||
$out->push($cl);
|
||||
}
|
||||
}
|
||||
|
||||
$out->sort("value");
|
||||
|
||||
return $out;
|
||||
}
|
||||
|
||||
public static function delete_metadata($id, $metadata) {
|
||||
$doc = new Document($id);
|
||||
$doc->remove_metadata($metadata);
|
||||
return redirect("/document/" . $id);
|
||||
}
|
||||
|
||||
public static function api_guess_docid($id) {
|
||||
$doc = new Document($id);
|
||||
$docid = $doc->guess_docid();
|
||||
|
||||
return new Collection(["id" => $id, "docid" => $docid]);
|
||||
}
|
||||
|
||||
public static function download_attachment($id, $filename) {
|
||||
$doc = new Document($id);
|
||||
$atts = $doc->get_attachments();
|
||||
foreach ($atts as $f) {
|
||||
if ($f->basename() == $filename) {
|
||||
$f->set_header("Content-Disposition", "attachment; filename=\"$filename\"");
|
||||
|
||||
return $f;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function upload_attachment($id) {
|
||||
$doc = new Document($id);
|
||||
return blade("upload.attachment", ["doc" => $doc]);
|
||||
}
|
||||
|
||||
public static function do_upload_attachment($_request, $id) {
|
||||
$doc = new Document($id);
|
||||
mkdir(ROOT . "/attachments/" . $doc->id, 0777);
|
||||
|
||||
$f = 0;
|
||||
while (($file = $_request->file("file", $f)) !== false) {
|
||||
$nf = new File($file['tmp_name']);
|
||||
$nf->rename(ROOT . "/attachments/" . $doc->id . "/" . $file['name']);
|
||||
$f++;
|
||||
}
|
||||
return redirect("/document/" . $doc->id);
|
||||
}
|
||||
}
|
||||
31
app/controllers/DownloadController.php
Normal file
31
app/controllers/DownloadController.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
class DownloadController {
|
||||
public static function get_download($id) {
|
||||
return new DownloadJob($id);
|
||||
}
|
||||
|
||||
public static function start_download() {
|
||||
}
|
||||
|
||||
public static function api_downloads() {
|
||||
$s = DownloadJob::find([["processed", "=", 0], ["owner", "=", get_user()->id]])->orderBy("queued")->limit(20);
|
||||
return $s->all();
|
||||
}
|
||||
|
||||
public static function api_add_download($_request) {
|
||||
$url = $_request->put("url");
|
||||
$d = new DownloadJob;
|
||||
$d->url = $url;
|
||||
$d->queued = time();
|
||||
$d->started = 0;
|
||||
$d->finished = 0;
|
||||
$d->processed = 0;
|
||||
$d->owner = get_user()->id;
|
||||
$d->file = sprintf("download/file-%08X-%08X", rand(), time());
|
||||
|
||||
$d->save();
|
||||
return DownloadController::api_downloads();
|
||||
}
|
||||
|
||||
}
|
||||
7
app/controllers/HomeController.php
Normal file
7
app/controllers/HomeController.php
Normal file
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
class HomeController {
|
||||
static function index() {
|
||||
return blade("index");
|
||||
}
|
||||
}
|
||||
78
app/controllers/ImportController.php
Normal file
78
app/controllers/ImportController.php
Normal file
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
class ImportController {
|
||||
public static function downloads() {
|
||||
return blade("downloads");
|
||||
}
|
||||
|
||||
public static function imports() {
|
||||
return blade("imports");
|
||||
}
|
||||
|
||||
public static function api_imports() {
|
||||
$is = ProcessJob::find([["imported", "=", 0]])->orderBy("queued")->limit(50)->all();
|
||||
foreach ($is as $i) {
|
||||
$i->load("revision");
|
||||
}
|
||||
return $is;
|
||||
}
|
||||
|
||||
public static function api_delete_import($id) {
|
||||
$d = new ProcessJob($id);
|
||||
if ($d->valid()) {
|
||||
$d->delete();
|
||||
}
|
||||
return ImportController::api_imports();
|
||||
}
|
||||
|
||||
public static function api_set_import($_request, $id) {
|
||||
$d = new ProcessJob($id);
|
||||
if ($d->valid()) {
|
||||
foreach ($_POST as $k=>$v) {
|
||||
$d->$k = $v;
|
||||
}
|
||||
$d->save();
|
||||
}
|
||||
return ImportController::api_imports();
|
||||
}
|
||||
|
||||
public static function api_add_document($_request, $id) {
|
||||
|
||||
$job = new ProcessJob($id);
|
||||
|
||||
$doc = Document::find([["internal_id", "=", $_request->put('internal_id')]])->first();
|
||||
if (!$doc) {
|
||||
$doc = new Document;
|
||||
$doc->internal_id = trim($_request->put('internal_id'));
|
||||
$doc->title = trim($_request->put('title'));
|
||||
$doc->subtitle = trim($_request->put('subtitle'));
|
||||
$doc->subsubtitle = trim($_request->put('subsubtitle'));
|
||||
$doc->overview = trim($_request->put('overview'));
|
||||
$doc->owner = get_user()->id;
|
||||
$doc->save();
|
||||
|
||||
$prods = explode(",", $_request->put('products'));
|
||||
foreach ($prods as $product) {
|
||||
$dp = new DocProduct;
|
||||
$dp->document = $doc->id;
|
||||
$dp->product = $product;
|
||||
$dp->save();
|
||||
}
|
||||
}
|
||||
|
||||
$job->document = $doc->id;
|
||||
|
||||
$rev = new Revision($job->revision);
|
||||
$rev->document = $doc->id;
|
||||
$rev->revno = trim($_request->put('revno'));
|
||||
$rev->month = $_request->put('month');
|
||||
$rev->year = $_request->put('year');
|
||||
$rev->owner = get_user()->id;
|
||||
$rev->save();
|
||||
|
||||
$job->imported = time();
|
||||
$job->save();
|
||||
|
||||
return ImportController::api_imports();
|
||||
}
|
||||
}
|
||||
23
app/controllers/JobController.php
Normal file
23
app/controllers/JobController.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
class JobController {
|
||||
public static function api_get_jobs($source) {
|
||||
$db = DB::getInstance();
|
||||
$q = $db->query("select * from job where source=:source order by queued", ["source" => $source]);
|
||||
|
||||
$c = new Collection();
|
||||
|
||||
while ($r = $db->nextRecord($q)) {
|
||||
$c->push($r);
|
||||
}
|
||||
return $c;
|
||||
}
|
||||
|
||||
public static function api_delete_job($id) {
|
||||
$db = DB::getInstance();
|
||||
$q = $db->query("select * from job where id=:id", ["id" => $id]);
|
||||
$r = $db->nextRecord($q);
|
||||
$q = $db->query("delete from job where id=:id", ["id" => $id]);
|
||||
return JobController::api_get_jobs($r->source);
|
||||
}
|
||||
}
|
||||
55
app/controllers/PDFController.php
Normal file
55
app/controllers/PDFController.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
class PDFController {
|
||||
public static function download($id, $type, $filename) {
|
||||
|
||||
$rev = new Revision($id);
|
||||
$path = $rev->path();
|
||||
|
||||
$rev->downloads++;
|
||||
$rev->last_download = time();
|
||||
$rev->save();
|
||||
|
||||
$disp = "attachment";
|
||||
$mime = "application/octet-stream";
|
||||
|
||||
$file = null;
|
||||
|
||||
switch ($type) {
|
||||
case "view":
|
||||
$file = new PDF($path . "/doc.pdf");
|
||||
break;
|
||||
case "download":
|
||||
$file = new PDF($path . "/doc.pdf");
|
||||
$file->force_download();
|
||||
break;
|
||||
case "viewocr":
|
||||
$file = new PDF($path . "/ocr.pdf");
|
||||
break;
|
||||
case "downloadocr":
|
||||
$file = new PDF($path . "/ocr.pdf");
|
||||
$file->force_download();
|
||||
break;
|
||||
}
|
||||
$file->fake_filename($filename);
|
||||
|
||||
if ($file == null) {
|
||||
return [404, "Not Found 1"];
|
||||
}
|
||||
|
||||
if (!$file->exists()) {
|
||||
return [404, "Not Found 2"];
|
||||
}
|
||||
|
||||
return $file;
|
||||
}
|
||||
|
||||
public static function get_page($id, $page) {
|
||||
$rev = new Revision($id);
|
||||
$page = $rev->get_page($page, 150);
|
||||
if ($page) {
|
||||
return $page;
|
||||
}
|
||||
return [404, "Not Found"];
|
||||
}
|
||||
}
|
||||
161
app/controllers/ProductController.php
Normal file
161
app/controllers/ProductController.php
Normal file
@@ -0,0 +1,161 @@
|
||||
<?php
|
||||
|
||||
class ProductController {
|
||||
public static function api_get_list($list) {
|
||||
$db = DB::getInstance();
|
||||
$pids = explode(",", $list);
|
||||
$out = new Collection();
|
||||
foreach ($pids as $pid) {
|
||||
$q = $db->query("select * from product where id=:id", ["id" => $pid]);
|
||||
if ($r = $db->nextRecord($q)) {
|
||||
$out->push($r);
|
||||
}
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
public static function api_get_mru() {
|
||||
$out = new Collection;
|
||||
$mru = Session::get("mru");
|
||||
if (!$mru) {
|
||||
$mru = [];
|
||||
}
|
||||
foreach ($mru as $p) {
|
||||
$prod = new Product($p);
|
||||
if ($prod) {
|
||||
$out->push($prod);
|
||||
}
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
public static function api_add_mru($id) {
|
||||
$mru = Session::get("mru");
|
||||
if (!$mru) {
|
||||
$mru = [];
|
||||
}
|
||||
array_unshift($mru, $id);
|
||||
$mru = array_unique($mru);
|
||||
while (count($mru) > 5) {
|
||||
array_pop($mru);
|
||||
}
|
||||
Session::set("mru", $mru);
|
||||
return ProductController::api_get_mru();
|
||||
}
|
||||
|
||||
public static function api_search() {
|
||||
|
||||
return Product::find([["full_path", "like", "%" . $_POST['search'] . "%"]])->orderBy("full_path")->all();
|
||||
}
|
||||
|
||||
public static function api_add_child($_request, $id) {
|
||||
$p = new Product;
|
||||
$p->parent = $id;
|
||||
$p->title = $_request->put("title");
|
||||
$p->save();
|
||||
return $p;
|
||||
}
|
||||
|
||||
public static function api_set($id) {
|
||||
$p = new Product($id);
|
||||
|
||||
$data = [];
|
||||
foreach ($_POST as $k=>$v) {
|
||||
$p->$k = $v;
|
||||
}
|
||||
$p->save();
|
||||
|
||||
return $p;
|
||||
}
|
||||
|
||||
public static function api_move($id) {
|
||||
$p = new Product($id);
|
||||
$p->parent = $_POST['to'];
|
||||
$p->save();
|
||||
return $p;
|
||||
}
|
||||
|
||||
public static function api_delete($id) {
|
||||
$product = new Product($id);
|
||||
if ($product->valid()) {
|
||||
$laf = Product::find([["title", "=", "Lost and Found"]])->first();
|
||||
|
||||
DB::getInstance()->query("delete from docproduct where product=:pid", ["pid" => $product->id]);
|
||||
|
||||
foreach ($product->children as $c) {
|
||||
$c->parent = $laf;
|
||||
$c->save();
|
||||
}
|
||||
$product->load("parent");
|
||||
$par = $product->parent;
|
||||
$product->delete();
|
||||
return $par;
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
public static function api_empty_trash($id) {
|
||||
$prod = new Product($id);
|
||||
$docs = $prod->documents;
|
||||
|
||||
foreach ($docs as $doc) {
|
||||
$revs = $doc->revisions;
|
||||
foreach ($revs as $rev) {
|
||||
$rev->delete();
|
||||
}
|
||||
|
||||
$dpl = DocProduct::find([["document", "=", $doc->id]])->all();
|
||||
foreach ($dpl as $dp) {
|
||||
$dp->delete();
|
||||
}
|
||||
|
||||
$doc->delete();
|
||||
}
|
||||
|
||||
$prod->invalidate("documents");
|
||||
|
||||
|
||||
return [];
|
||||
|
||||
}
|
||||
|
||||
public static function api_available_metadata($id) {
|
||||
$prod = new Product($id);
|
||||
$exist = $prod->meta();
|
||||
$metas = MetaType::find()->all();
|
||||
|
||||
$out = new Collection();
|
||||
|
||||
foreach ($metas as $meta) {
|
||||
if (!in_array($meta->id, $exist)) {
|
||||
$cl = new stdClass;
|
||||
$cl->key = $meta->id;
|
||||
$cl->value = $meta->name;
|
||||
$out->push($cl);
|
||||
}
|
||||
}
|
||||
|
||||
$out->sort("value");
|
||||
|
||||
return $out;
|
||||
}
|
||||
|
||||
public static function api_add_metadata($_request, $id) {
|
||||
$prod = new Product($id);
|
||||
$prod->add_meta($_request->put('item_id'));
|
||||
return Collection::from_array($prod->meta());
|
||||
}
|
||||
|
||||
public static function api_gemini_all($id) {
|
||||
$prod = new Product($id);
|
||||
|
||||
$c = new Collection();
|
||||
|
||||
foreach ($prod->documents as $doc) {
|
||||
$job = new GeminiJob($doc->id, "document:" . $doc->id);
|
||||
$jobid = $job->queue();
|
||||
$c->push(["document" => $doc->id, "job" => $jobid]);
|
||||
}
|
||||
return $c;
|
||||
}
|
||||
}
|
||||
46
app/controllers/RevisionController.php
Normal file
46
app/controllers/RevisionController.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
class RevisionController {
|
||||
|
||||
public static function show($id) {
|
||||
|
||||
$rev = new Revision($id);
|
||||
$rev->load("document");
|
||||
|
||||
return blade("revision", ["rev" => $rev]);
|
||||
}
|
||||
|
||||
public static function api_set($id) {
|
||||
$r = new Revision($id);
|
||||
if ($r !== false) {
|
||||
foreach ($_POST as $k=>$v) {
|
||||
$r->$k = trim($v);
|
||||
}
|
||||
$r->save();
|
||||
}
|
||||
|
||||
return $r;
|
||||
}
|
||||
|
||||
public static function delete($id) {
|
||||
$r = new Revision($id);
|
||||
$r->load("document");
|
||||
$doc = $r->document;
|
||||
$r->delete();
|
||||
return redirect("/document/" . $doc->id);
|
||||
}
|
||||
|
||||
public static function redownload($id) {
|
||||
$r = new Revision($id);
|
||||
$j = new DownloadJob($r->id, $r->origtitle, $r->path() . "/doc.pdf");
|
||||
$jobid = $j->queue();
|
||||
flash("success", "Job queued as ID " . $jobid);
|
||||
return redirect("/revision/" . $id);
|
||||
}
|
||||
|
||||
public static function purge($id) {
|
||||
$r = new Revision($id);
|
||||
$r->purge();
|
||||
return redirect("/revision/" . $id);
|
||||
}
|
||||
}
|
||||
76
app/controllers/SearchController.php
Normal file
76
app/controllers/SearchController.php
Normal file
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
class SearchController {
|
||||
static public function api_title_search() {
|
||||
$out = new Collection;
|
||||
|
||||
$q = DB::getInstance()->query("
|
||||
select
|
||||
id, internal_id, title, subtitle, subsubtitle,
|
||||
match (internal_id, title, subtitle, subsubtitle, overview)
|
||||
against (:search in boolean mode)
|
||||
as rel
|
||||
from
|
||||
document
|
||||
where
|
||||
match (internal_id, title, subtitle, subsubtitle, overview)
|
||||
against (:search in boolean mode)
|
||||
order by
|
||||
rel desc
|
||||
limit
|
||||
10
|
||||
", ["search" => $_POST['search']]);
|
||||
while ($r = DB::getInstance()->nextRecord($q)) {
|
||||
$o = new Document($r->id);
|
||||
$out->push($o);
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
static public function search($page = 0) {
|
||||
if (array_key_exists("search", $_POST)) {
|
||||
$q = DB::getInstance()->query("
|
||||
select
|
||||
revision.id as id,
|
||||
match(ocr.body) against (:search) as relevance
|
||||
from
|
||||
revision,ocr
|
||||
where
|
||||
match(ocr.body) against (:search) and
|
||||
ocr.revision = revision.id and
|
||||
not revision.document is null
|
||||
order by
|
||||
relevance desc
|
||||
|
||||
", ["search" => $_POST['search']]);
|
||||
|
||||
$slog = [];
|
||||
|
||||
while ($r = DB::getInstance()->nextRecord($q)) {
|
||||
$slog[] = $r->id;
|
||||
}
|
||||
|
||||
Session::set("search", json_encode($slog));
|
||||
}
|
||||
|
||||
$rpp = 8;
|
||||
|
||||
$offset = $page * $rpp;
|
||||
|
||||
$out = [];
|
||||
|
||||
$slog = json_decode(Session::get("search"));
|
||||
|
||||
for ($i = 0; $i < $rpp; $i++) {
|
||||
if ($offset + $i < count($slog)) {
|
||||
$rev = new Revision($slog[$offset + $i]);
|
||||
if ($rev) {
|
||||
$out[] = $rev;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return blade("search", ["page" => $page, "count" => count($slog), "results" => $out, "pages" => ceil(count($slog) / $rpp)]);
|
||||
}
|
||||
}
|
||||
|
||||
92
app/controllers/SpiderController.php
Normal file
92
app/controllers/SpiderController.php
Normal file
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
class SpiderController {
|
||||
|
||||
public static function spider_pdfs() {
|
||||
return blade("spider_pdfs");
|
||||
}
|
||||
|
||||
public static function spider_pages() {
|
||||
return blade("spider_pages");
|
||||
}
|
||||
|
||||
public static function api_pdfs() {
|
||||
$pdfs = Spider::find([["status", "=", "N"]])->orderBy("id")->limit(20)->all();
|
||||
|
||||
return $pdfs;
|
||||
}
|
||||
|
||||
public static function api_pages() {
|
||||
$pages = SpiderPage::find([["status", "=", "O"], ["title", "!=", ""]])->orderBy("title")->limit(20)->all();
|
||||
|
||||
return $pages;
|
||||
}
|
||||
|
||||
public static function api_reject_pdf($id = null) {
|
||||
if ($id == null) return [];
|
||||
|
||||
$i = explode(",", $id);
|
||||
foreach ($i as $id) {
|
||||
$pdf = new Spider($id);
|
||||
if ($pdf) {
|
||||
$pdf->status = "B";
|
||||
$pdf->save();
|
||||
}
|
||||
}
|
||||
return SpiderController::api_pdfs();
|
||||
}
|
||||
|
||||
public static function api_accept_pdf($_request, $id = null) {
|
||||
if ($id == null) return [];
|
||||
|
||||
$i = explode(",", $id);
|
||||
foreach ($i as $id) {
|
||||
$pdf = new Spider($id);
|
||||
if ($pdf) {
|
||||
$pdf->status = "D";
|
||||
$pdf->save();
|
||||
|
||||
$job = new DownloadJob;
|
||||
|
||||
$job->queued = time();
|
||||
$job->started = 0;
|
||||
$job->finished = 0;
|
||||
$job->processed = 0;
|
||||
$job->url = $pdf->url;
|
||||
$job->owner = get_user()->id;
|
||||
$job->file = sprintf("download/file-%08X-%08X", rand(), time());
|
||||
|
||||
$job->save();
|
||||
}
|
||||
}
|
||||
return SpiderController::api_pdfs();
|
||||
}
|
||||
|
||||
public static function api_reject_page($_request, $id) {
|
||||
$i = explode(",", $id);
|
||||
foreach ($i as $id) {
|
||||
$page = new SpiderPage($id);
|
||||
if ($page) {
|
||||
$page->status = "B";
|
||||
$page->save();
|
||||
}
|
||||
}
|
||||
return SpiderController::api_pages();
|
||||
}
|
||||
|
||||
public static function api_accept_page($id) {
|
||||
|
||||
$i = explode(",", $id);
|
||||
foreach ($i as $id) {
|
||||
$page = new SpiderPage($id);
|
||||
if ($page) {
|
||||
$page->status = "N";
|
||||
$page->save();
|
||||
}
|
||||
}
|
||||
return SpiderController::api_pages();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
79
app/controllers/SystemController.php
Normal file
79
app/controllers/SystemController.php
Normal file
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
class SystemController {
|
||||
public static function status() {
|
||||
$status = [
|
||||
"B" => "Blacklisted",
|
||||
"N" => "Pending",
|
||||
"F" => "Failed",
|
||||
"D" => "Done",
|
||||
"Y" => "Done",
|
||||
"W" => "Postponed",
|
||||
"P" => "Processing",
|
||||
"X" => "Deleted",
|
||||
"O" => "Off-site",
|
||||
"Q" => "Postponed",
|
||||
"R" => "Redirect",
|
||||
];
|
||||
|
||||
$q = DB::getInstance()->query("select count(id) as c, status from pages group by status order by status");
|
||||
$spider = [];
|
||||
while ($r = DB::getInstance()->nextRecord($q)) {
|
||||
$spider[@$status[$r->status]] = $r->c;
|
||||
}
|
||||
|
||||
$q = DB::getInstance()->query("select count(id) as c, status from spider group by status order by status");
|
||||
$pdf = [];
|
||||
while ($r = DB::getInstance()->nextRecord($q)) {
|
||||
$pdf[@$status[$r->status]] = $r->c;
|
||||
}
|
||||
|
||||
$q = DB::getInstance()->query("select count(id) as c, ocr from revision group by ocr order by ocr");
|
||||
$ocr = [];
|
||||
while ($r = DB::getInstance()->nextRecord($q)) {
|
||||
$ocr[@$status[$r->ocr]] = $r->c;
|
||||
}
|
||||
|
||||
$q = DB::getInstance()->query("select count(id) as c, idx from revision group by idx order by idx");
|
||||
$idx = [];
|
||||
while ($r = DB::getInstance()->nextRecord($q)) {
|
||||
$idx[@$status[$r->idx]] = $r->c;
|
||||
}
|
||||
|
||||
return blade("status", ["spider" => $spider, "pdf" => $pdf, "ocr" => $ocr, "idx" => $idx]);
|
||||
}
|
||||
|
||||
|
||||
public static function api_get_idmatches() {
|
||||
return IDMatch::find([["id", ">=", 0]])->orderBy("weight")->all();
|
||||
}
|
||||
|
||||
public static function api_add_idmatch($_request) {
|
||||
$i = new IDMatch;
|
||||
$i->example = $_request->put('example');
|
||||
$i->regex = $_request->put('regex');
|
||||
$i->weight = $_request->put('weight');
|
||||
$i->save();
|
||||
return SystemController::api_get_idmatches();
|
||||
}
|
||||
|
||||
public static function api_set_idmatch($id) {
|
||||
$i = new IDMatch($id);
|
||||
if ($i) {
|
||||
$i->example = $_POST['example'];
|
||||
$i->regex = $_POST['regex'];
|
||||
$i->weight = $_POST['weight'];
|
||||
$i->save();
|
||||
}
|
||||
return SystemController::api_get_idmatches();
|
||||
}
|
||||
|
||||
public static function api_del_idmatch($id) {
|
||||
$i = new IDMatch($id);
|
||||
if ($i) {
|
||||
$i->delete();
|
||||
}
|
||||
return SystemController::api_get_idmatches();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user