2021-12-14 18:32:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2007-04-16 11:58:25 +00:00
|
|
|
|
2023-01-10 22:44:09 +00:00
|
|
|
namespace Shimmie2;
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
class BulkAddEvent extends Event
|
|
|
|
{
|
2021-03-14 23:43:50 +00:00
|
|
|
public string $dir;
|
2024-01-05 13:53:21 +00:00
|
|
|
/** @var UploadResult[] */
|
2021-03-14 23:43:50 +00:00
|
|
|
public array $results;
|
2015-08-23 15:09:52 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
public function __construct(string $dir)
|
|
|
|
{
|
2020-01-26 13:19:35 +00:00
|
|
|
parent::__construct();
|
2019-05-28 16:59:38 +00:00
|
|
|
$this->dir = $dir;
|
|
|
|
$this->results = [];
|
|
|
|
}
|
2015-08-23 15:09:52 +00:00
|
|
|
}
|
|
|
|
|
2019-05-28 16:59:38 +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
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
public function onPageRequest(PageRequestEvent $event)
|
|
|
|
{
|
2023-06-25 13:19:02 +00:00
|
|
|
global $page, $user;
|
2019-05-28 16:59:38 +00:00
|
|
|
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);
|
2020-01-28 21:19:59 +00:00
|
|
|
$bae = send_event(new BulkAddEvent($_POST['dir']));
|
2024-01-05 13:53:21 +00:00
|
|
|
$this->theme->display_upload_results($page, $bae->results);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
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) {
|
2020-01-28 21:19:59 +00:00
|
|
|
$bae = send_event(new BulkAddEvent($event->args[0]));
|
2024-01-05 13:53:21 +00:00
|
|
|
foreach ($bae->results as $r) {
|
|
|
|
if(is_a($r, UploadError::class)) {
|
|
|
|
print($r->name." failed: ".$r->error."\n");
|
|
|
|
} else {
|
|
|
|
print($r->name." ok\n");
|
|
|
|
}
|
|
|
|
}
|
2019-05-28 16:59:38 +00:00
|
|
|
print(implode("\n", $bae->results));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-06-17 19:05:16 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
public function onAdminBuilding(AdminBuildingEvent $event)
|
|
|
|
{
|
|
|
|
$this->theme->display_admin_block();
|
|
|
|
}
|
2014-04-26 02:54:51 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
public function onBulkAdd(BulkAddEvent $event)
|
|
|
|
{
|
|
|
|
if (is_dir($event->dir) && is_readable($event->dir)) {
|
|
|
|
$event->results = add_dir($event->dir);
|
|
|
|
} else {
|
2024-01-05 13:53:21 +00:00
|
|
|
$event->results = [new UploadError($event->dir, "is not a readable directory")];
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
2015-08-23 15:09:52 +00:00
|
|
|
}
|