Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 46 additions & 5 deletions PlnPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use APP\core\Application;
use APP\core\PageRouter;
use APP\facades\Repo;
use APP\notification\Notification;
use PKP\notification\Notification;
use APP\notification\NotificationManager;
use APP\plugins\generic\pln\classes\deposit\Repository;
use APP\plugins\generic\pln\classes\deposit\Schema as DepositSchema;
Expand All @@ -28,10 +28,13 @@
use APP\plugins\generic\pln\classes\PLNGatewayPlugin;
use APP\plugins\generic\pln\classes\tasks\Depositor;
use APP\plugins\generic\pln\pages\PageHandler;
use Carbon\Carbon;
use DateTimeImmutable;
use DOMDocument;
use DOMElement;
use Exception;
use GuzzleHttp\Exception\RequestException;
use Illuminate\Support\Facades\DB;
use PKP\config\Config;
use PKP\core\JSONMessage;
use PKP\core\PKPString;
Expand Down Expand Up @@ -277,7 +280,7 @@ public function callbackLoadCategory(string $hookName, array $args): bool
public function registerSchedules(PKPScheduler $scheduler): void
{
$scheduler
->addSchedule(new Depositor())
->addSchedule(new Depositor([]))
->daily()
->name(Depositor::class)
->withoutOverlapping();
Expand Down Expand Up @@ -345,7 +348,7 @@ public function manage($args, $request): JSONMessage
$notificationContent = __('plugins.generic.pln.settings.saved');
$currentUser = $request->getUser();
$notificationMgr = new NotificationManager();
$notificationMgr->createTrivialNotification($currentUser->getId(), PKPNotification::NOTIFICATION_TYPE_SUCCESS, ['contents' => $notificationContent]);
$notificationMgr->createTrivialNotification($currentUser->getId(), Notification::NOTIFICATION_TYPE_SUCCESS, ['contents' => $notificationContent]);

return new JSONMessage(true);
}
Expand Down Expand Up @@ -509,6 +512,43 @@ public function hasZipArchive(): bool
return class_exists('ZipArchive');
}

/**
* Check if the Acron plugin is enabled, or if the scheduled task has been running lately.
*/
public function hasScheduledTasks(): bool
{
try {
$application = Application::get();
$products = $application->getEnabledProducts('plugins.generic');
return isset($products['acron']) || (($lastRun = $this->getLastExecutionDate()) && $lastRun && Carbon::now()->diffInWeeks($lastRun) < 1);
} catch (\Exception $e) {
// If there's an error checking, assume no scheduled tasks are available
return false;
}
}

/**
* Retrieves the last time the depositor task was executed
*/
public function getLastExecutionDate(): ?DateTimeImmutable
{
try {
// Check if the scheduled_tasks table exists first
if (!DB::getSchemaBuilder()->hasTable('scheduled_tasks')) {
return null;
}

$lastRun = DB::table('scheduled_tasks')
->where('class_name', Depositor::class)
->value('last_run');

return $lastRun ? new DateTimeImmutable($lastRun) : null;
} catch (\Exception $e) {
// Table doesn't exist or query failed, return null
return null;
}
}

/**
* Get resource
*
Expand Down Expand Up @@ -614,7 +654,7 @@ public function setEnabled($enabled): void
if ($enabled) {
(new NotificationManager())->createTrivialNotification(
Application::get()->getRequest()->getUser()->getId(),
PKPNotification::NOTIFICATION_TYPE_SUCCESS,
Notification::NOTIFICATION_TYPE_SUCCESS,
['contents' => __('plugins.generic.pln.onPluginEnabledNotification')]
);
}
Expand All @@ -625,7 +665,8 @@ public function setEnabled($enabled): void
*/
public function getName(): string
{
return substr(static::class, strlen(__NAMESPACE__) + 1);
// Return the canonical plugin name used by PluginRegistry lookups
return 'plnplugin';
}

/**
Expand Down