This repository has been archived on 2024-09-05. You can view files and clone it, but cannot push or open issues or pull requests.
shimmie2/ext/bulk_add/main.php

73 lines
2 KiB
PHP
Raw Normal View History

2021-12-14 18:32:47 +00:00
<?php
declare(strict_types=1);
namespace Shimmie2;
class BulkAddEvent extends Event
{
public string $dir;
/** @var UploadResult[] */
public array $results;
2015-08-23 15:09:52 +00:00
public function __construct(string $dir)
{
2020-01-26 13:19:35 +00:00
parent::__construct();
$this->dir = $dir;
$this->results = [];
}
2015-08-23 15:09:52 +00:00
}
class BulkAdd extends Extension
{
2020-02-04 00:46:36 +00:00
/** @var BulkAddTheme */
2023-06-27 14:56:49 +00:00
protected Themelet $theme;
2020-02-04 00:46:36 +00:00
public function onPageRequest(PageRequestEvent $event)
{
2023-06-25 13:19:02 +00:00
global $page, $user;
if ($event->page_matches("bulk_add")) {
2019-09-29 18:00:51 +00:00
if ($user->can(Permissions::BULK_ADD) && $user->check_auth_token() && isset($_POST['dir'])) {
2023-06-25 13:19:02 +00:00
shm_set_timeout(null);
$bae = send_event(new BulkAddEvent($_POST['dir']));
$this->theme->display_upload_results($page, $bae->results);
}
}
}
public function onCommand(CommandEvent $event)
{
if ($event->cmd == "help") {
print "\tbulk-add [directory]\n";
print "\t\tImport this directory\n\n";
}
if ($event->cmd == "bulk-add") {
if (count($event->args) == 1) {
$bae = send_event(new BulkAddEvent($event->args[0]));
foreach ($bae->results as $r) {
if(is_a($r, UploadError::class)) {
print($r->name." failed: ".$r->error."\n");
} else {
print($r->name." ok\n");
}
}
print(implode("\n", $bae->results));
}
}
}
public function onAdminBuilding(AdminBuildingEvent $event)
{
$this->theme->display_admin_block();
}
public function onBulkAdd(BulkAddEvent $event)
{
if (is_dir($event->dir) && is_readable($event->dir)) {
$event->results = add_dir($event->dir);
} else {
$event->results = [new UploadError($event->dir, "is not a readable directory")];
}
}
2015-08-23 15:09:52 +00:00
}