Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split plugin per jurnal (OJS 33) #57

Open
wants to merge 3 commits into
base: 33
Choose a base branch
from
Open
Show file tree
Hide file tree
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
40 changes: 24 additions & 16 deletions OjtPageHandler.inc.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use GuzzleHttp\Exception\BadResponseException;
use Openjournalteam\OjtPlugin\Classes\ModuleProtection;

import('classes.handler.Handler');
import('plugins.generic.ojtPlugin.helpers.OJTHelper');
Expand Down Expand Up @@ -53,12 +54,9 @@ public function index($args, $request)
$baseUrl = $request->getBaseUrl() . '/';
$pluginFullUrl = $baseUrl . $plugin->getPluginPath();
$templateMgr = TemplateManager::getManager($request);
$journal = $this->contextId ? $request->getContext() : $request->getSite();

$publicFileManager = new PublicFileManager();

$publicFolder = ($plugin->getJournalVersion() > '31')
? $baseUrl . $publicFileManager->getContextFilesPath($this->contextId) . '/'
: $baseUrl . $publicFileManager->getContextFilesPath(ASSOC_TYPE_JOURNAL, $this->contextId) . '/';
$publicFolder = $plugin->getPublicFilesJournalUrl();

$ojtPlugin = new \stdClass;
$ojtPlugin->api = $plugin->apiUrl() . '/product/';
Expand Down Expand Up @@ -90,7 +88,8 @@ public function index($args, $request)
];

$templateMgr->assign('ojtPlugin', $ojtPlugin);
$templateMgr->assign('journal', $this->contextId ? $request->getContext() : $request->getSite());
$templateMgr->assign('journal', $journal);
$templateMgr->assign('logoImage', $this->contextId ? $journal->getLocalizedData('pageHeaderLogoImage') : $journal->getLocalizedData('pageHeaderTitleImage'));
$templateMgr->assign('pluginGalleryHtml', $templateMgr->fetch($this->ojtPlugin->getTemplateResource('plugingallery.tpl')));
$templateMgr->assign('pluginInstalledHtml', $templateMgr->fetch($this->ojtPlugin->getTemplateResource('plugininstalled.tpl')));

Expand Down Expand Up @@ -274,8 +273,8 @@ public function getPluginGalleryList($args, $request)
$pluginVersion = $plugin['version'];
$targetPlugin = @include($ojtplugin->getModulesPath($pluginFolder . DIRECTORY_SEPARATOR . "index.php"));

$plugin['update'] = false;
$plugin['license'] = $pluginSettingsDao->getSetting($this->ojtPlugin->getCurrentContextId(), $plugin['class'], 'license') ?? null;
$plugin['update'] = false;
$plugin['license'] = $pluginSettingsDao->getSetting($this->ojtPlugin->getCurrentContextId(), $plugin['class'], 'license') ?? null;

if ($targetPlugin) {
import('lib.pkp.classes.site.VersionCheck');
Expand Down Expand Up @@ -348,12 +347,14 @@ public function toggleInstalledPlugin($args, $request)
public function installPlugin($args, $request)
{
try {
$ojtPlugin = $this->ojtPlugin;
$fileManager = new FileManager();
$pluginToInstall = json_decode($request->getUserVar('plugin'));
$indexFile = $ojtPlugin->getModulesPath(DIRECTORY_SEPARATOR . $pluginToInstall->folder . DIRECTORY_SEPARATOR . "index.php");
$license = $request->getUserVar('license') ?? false;
$update = $request->getUserVar('update');
$ojtPlugin = $this->ojtPlugin;
$fileManager = new FileManager();
$pluginToInstall = json_decode($request->getUserVar('plugin'));
$pluginFolder = $ojtPlugin->getModulesPath($pluginToInstall->folder . DIRECTORY_SEPARATOR);
$indexFile = $pluginFolder . "index.php";
$license = $request->getUserVar('license') ?? false;
$update = $request->getUserVar('update');

if ($update && $fileManager->fileExists($indexFile)) {
$pluginInstance = include($indexFile);

Expand All @@ -365,7 +366,7 @@ public function installPlugin($args, $request)

// trying to install dependencies
foreach ($downloadLink['dependencies'] as $dependency) {
$indexDependency = $ojtPlugin->getModulesPath(DIRECTORY_SEPARATOR . $dependency['folder'] . DIRECTORY_SEPARATOR . "index.php");
$indexDependency = $ojtPlugin->getModulesPath($dependency['folder'] . DIRECTORY_SEPARATOR . "index.php");

if (!$fileManager->fileExists($indexDependency)) {
$ojtPlugin->installPlugin($dependency['link']);
Expand All @@ -387,9 +388,13 @@ public function installPlugin($args, $request)
$pluginInstance->updateSetting($this->contextId, 'licenseMain', $license);
}

$protection = new ModuleProtection($pluginInstance, $pluginFolder);
$protection->generateProtectionFile();


$json['error'] = 0;
$json['msg'] = !$update ? 'Plugin Installed' : 'Plugin Updated';
$json['msg'] = !$update ? 'Plugin Installed' : 'Plugin Updated';

return showJson($json);
} catch (Exception $e) {
$json['error'] = 1;
Expand All @@ -411,6 +416,9 @@ protected function simulateRegisterModules($pluginToInstall)
// delete plugin when error occured
register_shutdown_function(function () use ($ojtPlugin, $pluginToInstall) {
$error = error_get_last();

if(!$error) return;

if (!in_array($error['type'], [E_COMPILE_ERROR, E_ERROR])) return;

// Working directory berubah ketika callback ini berjalan, jadi harus mendapatkan fullpath
Expand Down
63 changes: 46 additions & 17 deletions OjtPlugin.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Monolog\Logger;
use Monolog\Utils;
use Openjournalteam\OjtPlugin\Classes\ErrorHandler;
use Openjournalteam\OjtPlugin\Classes\ModuleProtection;
use Openjournalteam\OjtPlugin\Classes\ParamHandler;
use Openjournalteam\OjtPlugin\Classes\ServiceHandler;
use Psr\Log\LogLevel;
Expand All @@ -23,10 +24,8 @@ public function register($category, $path, $mainContextId = null)
{
if (parent::register($category, $path, $mainContextId)) {
if ($this->getEnabled()) {
register_shutdown_function([$this, 'fatalHandler']);
$this->init();
$this->setLogger();
$this->createModulesFolder();
$this->registerModules();
// HookRegistry::register('Template::Settings::website', array($this, 'settingsWebsite'));
HookRegistry::register('LoadHandler', [$this, 'setPageHandler']);
Expand All @@ -35,14 +34,15 @@ public function register($category, $path, $mainContextId = null)
HookRegistry::register('TemplateManager::display', [$this, 'addHeader']);
}


return true;
}
return false;
}

public function init()
{
register_shutdown_function([$this, 'fatalHandler']);

$paramHandler = new ParamHandler($this);
$paramHandler->handle();
}
Expand Down Expand Up @@ -109,6 +109,8 @@ function fatalHandler()
{
$error = error_get_last();
// Fatal error, E_ERROR === 1
if($error === null) return;

if (!in_array($error['type'], [E_COMPILE_ERROR, E_ERROR])) return;
if (!str_contains($error['file'], 'ojtPlugin')) {
return;
Expand Down Expand Up @@ -267,13 +269,12 @@ public function setupBackendPage($hookName, $args)

public function getModulesPath($path = '')
{
return $this->getPluginPath() . DIRECTORY_SEPARATOR . 'modules' . DIRECTORY_SEPARATOR . $path;
return $this->getPluginPath() . DIRECTORY_SEPARATOR . 'modules' . DIRECTORY_SEPARATOR . $this->getCurrentContextId() . DIRECTORY_SEPARATOR . $path;
}

public function registerModules()
{
$modulesFolder = $this->getDirs($this->getModulesPath());

import('lib.pkp.classes.site.VersionCheck');

$plugins = [];
Expand All @@ -297,8 +298,10 @@ public function registerModules()

$categoryPlugin = explode('.', $version->getData('productType'))[1];
$categoryDir = $this->getModulesPath();
$pluginDir = $categoryDir . $moduleFolder;
$pluginDir = $categoryDir . $moduleFolder . DIRECTORY_SEPARATOR;


$protection = new ModuleProtection($plugin, $pluginDir);
PluginRegistry::register($categoryPlugin, $plugin, $pluginDir);

if ($plugin instanceof ThemePlugin) {
Expand All @@ -315,15 +318,13 @@ public function registerModules()
$data['icon'] = method_exists($plugin, 'getPageIcon') ? $plugin->getPageIcon() : $this->getDefaultPluginIcon();
$data['documentation'] = method_exists($plugin, 'getDocumentation') ? $plugin->getDocumentation() : null;
$data['page'] = method_exists($plugin, 'getPage') ? $plugin->getPage() : null;
$data['isInstalledFromOjt'] = $protection->check();

$plugins[] = $data;
}

// HookRegistry::call('PluginRegistry::categoryLoaded::themes');


$this->registeredModule = $plugins;

return $plugins;
}

Expand All @@ -349,7 +350,7 @@ public function createModulesFolder()
return;
}

mkdir(getcwd() . DIRECTORY_SEPARATOR . $this->getModulesPath());
mkdir(getcwd() . DIRECTORY_SEPARATOR . $this->getModulesPath(), 0755, true);
}

// Show available update on Setting -> Website
Expand Down Expand Up @@ -478,13 +479,12 @@ public function getPluginFullUrl($path = '', $withVersion = true)

public function setPageHandler($hookName, $params)
{
if ($this->getCurrentContextId() == 0) {
// Panel tidak support untuk sitewide
return false;
}
// if ($this->getCurrentContextId() == 0) {
// // Panel tidak support untuk sitewide
// return false;
// }

$page = $params[0];

switch ($page) {
case 'ojt':
define('HANDLER_CLASS', 'OjtPageHandler');
Expand Down Expand Up @@ -514,10 +514,12 @@ public function getActions($request, $actionArgs)
return $actions;
}

$path = $this->getCurrentContextId() ? $request->getContext()->getPath() : 'index';

import('lib.pkp.classes.linkAction.request.OpenWindowAction');
$linkAction = new LinkAction(
'ojt_control_panel',
new OpenWindowAction($request->getDispatcher()->url($request, ROUTE_PAGE, $request->getContext()->getPath()) . '/ojt?PageSpeed=off'),
new OpenWindowAction($request->getDispatcher()->url($request, ROUTE_PAGE, $path) . '/ojt?PageSpeed=off'),
'Control Panel',
null
);
Expand Down Expand Up @@ -587,7 +589,12 @@ public function recursiveDelete($dirPath, $deleteParent = true)
public function getJournalURL()
{
$request = $this->getRequest();
return $request->getDispatcher()->url($request, ROUTE_PAGE, $request->getContext()->getPath());

if($this->getCurrentContextId()){
return $request->getDispatcher()->url($request, ROUTE_PAGE, $request->getContext()->getPath());
}

return $request->getDispatcher()->url($request, ROUTE_PAGE, 'index');
}

public function getPluginDownloadLink($pluginToken, $license = false, $journalUrl)
Expand Down Expand Up @@ -721,4 +728,26 @@ public function isDiagnosticEnabled()
{
return $this->getSetting(CONTEXT_SITE, 'enable_diagnostic') ?? true;
}

public function getPublicFilesJournalUrl()
{
$publicFileManager = new PublicFileManager();
$baseUrl = $this->getRequest()->getBaseUrl() . '/';
$contextId = $this->getCurrentContextId();

if ($this->getJournalVersion() == '31') {
return $baseUrl . $publicFileManager->getContextFilesPath(ASSOC_TYPE_JOURNAL, $this->getCurrentContextId()) . '/';
}

if($contextId == CONTEXT_SITE){
return $baseUrl . $publicFileManager->getSiteFilesPath() . '/';
}

return $baseUrl . $publicFileManager->getContextFilesPath($this->getCurrentContextId()) . '/';
}

function isSitePlugin() {
return !Application::get()->getRequest()->getContext();
}
}

2 changes: 1 addition & 1 deletion assets/stylesheets/tailwind.css

Large diffs are not rendered by default.

Loading