162 lines
5.8 KiB
PHP
162 lines
5.8 KiB
PHP
<?php
|
|
|
|
require_once 'lib/files.php';
|
|
|
|
class FilesHandler extends request_handler {
|
|
|
|
const SEARCH_RESULTS_PER_PAGE = 50;
|
|
const SEARCH_MIN_QUERY_LENGTH = 3;
|
|
|
|
function GET_files() {
|
|
add_meta([
|
|
'$title' => '$meta_files_title',
|
|
'$description' => '$meta_files_description'
|
|
]);
|
|
set_title('$files');
|
|
set_skin_opts(['head_section' => 'files']);
|
|
$collections = array_map(fn(FilesCollection $c) => new CollectionItem($c), FilesCollection::cases());
|
|
$books = books_get();
|
|
$misc = books_get(category: BookCategory::MISC);
|
|
render('files/index',
|
|
collections: $collections,
|
|
books: $books,
|
|
misc: $misc);
|
|
}
|
|
|
|
function GET_folder() {
|
|
list($folder_id) = input('i:folder_id');
|
|
$folder = books_get_folder($folder_id);
|
|
if (!$folder)
|
|
not_found();
|
|
$files = books_get($folder_id);
|
|
add_meta([
|
|
'$title' => lang('meta_files_book_folder_title', $folder->getTitle()),
|
|
'$description' => lang('meta_files_book_folder_description', $folder->getTitle())
|
|
]);
|
|
set_title(lang('files').' - '.$folder->title);
|
|
render('files/folder',
|
|
folder: $folder,
|
|
files: $files);
|
|
}
|
|
|
|
function GET_collection() {
|
|
list($collection, $folder_id, $query, $offset) = input('collection, i:folder_id, q, i:offset');
|
|
$collection = FilesCollection::from($collection);
|
|
$parents = null;
|
|
|
|
$query = trim($query);
|
|
if (!$query)
|
|
$query = null;
|
|
|
|
add_skin_strings_re('/^files_(.*?)_collection$/');
|
|
add_skin_strings([
|
|
'files_search_results_count'
|
|
]);
|
|
|
|
$vars = [];
|
|
$text_excerpts = null;
|
|
$func_prefix = $collection->value;
|
|
|
|
if ($query !== null) {
|
|
$files = call_user_func("{$func_prefix}_search", $query, $offset, self::SEARCH_RESULTS_PER_PAGE);
|
|
$vars += [
|
|
'search_count' => $files['count'],
|
|
'search_query' => $query
|
|
];
|
|
|
|
/** @var WFFCollectionItem[]|MDFCollectionItem[]|BaconianaCollectionItem[] $files */
|
|
$files = $files['items'];
|
|
|
|
$query_words = array_map('mb_strtolower', preg_split('/\s+/', $query));
|
|
$found = [];
|
|
$result_ids = [];
|
|
foreach ($files as $file) {
|
|
if ($file->isFolder())
|
|
continue;
|
|
$result_ids[] = $file->id;
|
|
|
|
switch ($collection) {
|
|
case FilesCollection::MercureDeFrance:
|
|
$candidates = [
|
|
$file->date,
|
|
(string)$file->issue
|
|
];
|
|
break;
|
|
case FilesCollection::WilliamFriedman:
|
|
$candidates = [
|
|
mb_strtolower($file->getTitle()),
|
|
strtolower($file->documentId)
|
|
];
|
|
break;
|
|
case FilesCollection::Baconiana:
|
|
$candidates = [
|
|
// TODO
|
|
];
|
|
break;
|
|
}
|
|
|
|
foreach ($candidates as $haystack) {
|
|
foreach ($query_words as $qw) {
|
|
if (mb_strpos($haystack, $qw) !== false) {
|
|
$found[$file->id] = true;
|
|
continue 2;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
$found = array_map('intval', array_keys($found));
|
|
$not_found = array_diff($result_ids, $found);
|
|
if (!empty($not_found))
|
|
$text_excerpts = call_user_func("{$func_prefix}_get_text_excerpts", $not_found, $query_words);
|
|
|
|
if (is_xhr_request()) {
|
|
ajax_ok([
|
|
...$vars,
|
|
'new_offset' => $offset + count($files),
|
|
'html' => skin('files')->collection_files($files, $query, $text_excerpts)
|
|
]);
|
|
}
|
|
} else {
|
|
if (in_array($collection, [FilesCollection::WilliamFriedman, FilesCollection::Baconiana]) && $folder_id) {
|
|
$parents = call_user_func("{$func_prefix}_get_folder", $folder_id, true);
|
|
if (!$parents)
|
|
not_found();
|
|
if (count($parents) > 1)
|
|
$parents = array_reverse($parents);
|
|
}
|
|
$files = call_user_func("{$func_prefix}_get", $folder_id);
|
|
}
|
|
|
|
$title = lang('files_'.$collection->value.'_collection');
|
|
if ($folder_id && $parents)
|
|
$title .= ' - '.htmlescape($parents[count($parents)-1]->getTitle());
|
|
if ($query)
|
|
$title .= ' - '.htmlescape($query);
|
|
set_title($title);
|
|
|
|
if (!$folder_id && !$query)
|
|
add_meta([
|
|
'$title' => lang('4in1').' - '.lang('meta_files_collection_title', lang('files_'.$collection->value.'_collection')),
|
|
'$description' => lang('meta_files_'.$collection->value.'_description')
|
|
]);
|
|
else if ($query || $parents) {
|
|
add_meta([
|
|
'$title' => lang('4in1').' - '.$title,
|
|
'$description' => lang('meta_files_'.($query ? 'search' : 'folder').'_description',
|
|
$query ?: $parents[count($parents)-1]->getTitle(),
|
|
lang('files_'.$collection->value.'_collection'))
|
|
]);
|
|
}
|
|
|
|
render('files/collection',
|
|
...$vars,
|
|
collection: $collection,
|
|
files: $files,
|
|
parents: $parents,
|
|
search_results_per_page: self::SEARCH_RESULTS_PER_PAGE,
|
|
search_min_query_length: self::SEARCH_MIN_QUERY_LENGTH,
|
|
text_excerpts: $text_excerpts);
|
|
}
|
|
|
|
} |