Skip to content

Commit

Permalink
register handler bug
Browse files Browse the repository at this point in the history
  • Loading branch information
GaziYucel committed May 30, 2024
1 parent 5a735b9 commit 7a26503
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 119 deletions.
41 changes: 23 additions & 18 deletions LatexConverterPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
require_once(__DIR__ . '/vendor/autoload.php');

use APP\plugins\generic\latexConverter\classes\Constants;
use APP\plugins\generic\latexConverter\classes\Handler\PluginHandler;
use APP\plugins\generic\latexConverter\classes\Settings\Actions;
use APP\plugins\generic\latexConverter\classes\Settings\Manage;
use APP\plugins\generic\latexConverter\classes\Settings\MimeTypes;
Expand All @@ -36,10 +35,9 @@ function register($category, $path, $mainContextId = null): bool
{
if (parent::register($category, $path, $mainContextId)) {
if ($this->getEnabled()) {
$pluginHandler = new PluginHandler();
$links = new Links($this);
$mimeTypes = new MimeTypes($this);
Hook::add('LoadHandler', [$pluginHandler, 'register']);
Hook::add('LoadHandler', [$this, 'registerHandler']);
Hook::add('TemplateManager::fetch', [$links, 'execute']);
Hook::add('SubmissionFile::supportsDependentFiles', [$mimeTypes, 'execute']);

Expand All @@ -52,6 +50,25 @@ function register($category, $path, $mainContextId = null): bool
return false;
}

/** Register PluginHandler */
public function registerHandler(string $hookName, array $args): bool
{
$page = $args[0];
$op = $args[1];

switch ("$page/$op") {
case "latexConverter/extractShow":
case "latexConverter/extractExecute":
case "latexConverter/convert":
define('HANDLER_CLASS', '\APP\plugins\generic\latexConverter\classes\Handler\PluginHandler');
return true;
default:
break;
}

return false;
}

/** @copydoc Plugin::getActions() */
public function getActions($request, $actionArgs): array
{
Expand All @@ -68,31 +85,19 @@ public function manage($args, $request): JSONMessage
return $manage->execute($args, $request);
}

/* Plugin required methods */

/**
* @copydoc PKPPlugin::getDisplayName
*/
/** @copydoc PKPPlugin::getDisplayName */
public function getDisplayName(): string
{
return __('plugins.generic.latexConverter.displayName');
}

/**
* @copydoc PKPPlugin::getDescription
*/
/** @copydoc PKPPlugin::getDescription */
public function getDescription(): string
{
return __('plugins.generic.latexConverter.description');
}

/**
* Overrides parent getSetting
*
* @param $contextId
* @param $name
* @return mixed|null
*/
/** @copydoc PKPPlugin::getSetting */
public function getSetting($contextId, $name): mixed
{
switch ($name) {
Expand Down
Empty file added assets/index.php
Empty file.
76 changes: 16 additions & 60 deletions classes/Handler/PluginHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,10 @@

class PluginHandler extends Handler
{
/**
* @var LatexConverterPlugin
*/
/** @var LatexConverterPlugin */
protected LatexConverterPlugin $plugin;

/**
* List of methods allowed
*
* @var array|string[]
*/
/** @var array|string[] List of methods allowed */
protected array $allowedMethods = ['extractShow', 'extractExecute', 'convert'];

function __construct()
Expand All @@ -44,56 +38,24 @@ function __construct()

/* @var LatexConverterPlugin $plugin */
$plugin = PluginRegistry::getPlugin('generic', strtolower(LATEX_CONVERTER_PLUGIN_NAME));
$this->plugin = $plugin;

$this->addRoleAssignment(
Constants::authorizedRoles,
$this->allowedMethods
);
}

/**
* Register PluginHandler
*
* @param $hookName string
* @param $args array Hook arguments [&$page, &$op, &$sourceFile]
* @return bool
*/
public function register(string $hookName, array $args): bool
{
$page = $args[0];
$op = $args[1];
$this->plugin = &$plugin;

switch ("$page/$op") {
case "latexConverter/extractShow":
case "latexConverter/extractExecute":
case "latexConverter/convert":
define('HANDLER_CLASS', '\APP\plugins\generic\latexConverter\classes\Handler\PluginHandler');
return true;
default:
break;
}

return false;
$this->addRoleAssignment(Constants::authorizedRoles, $this->allowedMethods);
}

/**
* Overridden method from Handler
*
* @copydoc PKPHandler::authorize()
*/
/** @copydoc PKPHandler::authorize() */
function authorize($request, &$args, $roleAssignments): bool
{
$this->addPolicy(
new WorkflowStageAccessPolicy(
$request,
$args,
$roleAssignments,
'submissionId',
(int)$request->getUserVar('stageId')
)
$policy = new WorkflowStageAccessPolicy(
$request,
$args,
$roleAssignments,
'submissionId',
(int)$request->getUserVar('stageId')
);

$this->addPolicy($policy);

return parent::authorize($request, $args, $roleAssignments);
}

Expand All @@ -106,10 +68,8 @@ function authorize($request, &$args, $roleAssignments): bool
*/
public function extractShow($args, $request): JSONMessage
{
$action = new Extract($this->plugin, $request, $args);

$action = new Extract($this->plugin);
$action->initData();

return new JSONMessage(true, $action->fetch($request));
}

Expand All @@ -122,12 +82,9 @@ public function extractShow($args, $request): JSONMessage
*/
public function extractExecute($args, $request): JSONMessage
{
$action = new Extract($this->plugin, $request, $args);

$action = new Extract($this->plugin);
$action->readInputData();

$action->process();

return $request->redirectUrlJson(
$request->getDispatcher()->url(
$request,
Expand All @@ -153,8 +110,7 @@ public function extractExecute($args, $request): JSONMessage
*/
public function convert($args, $request): JSONMessage
{
$action = new Convert($this->plugin, $request, $args);

$action = new Convert($this->plugin);
return $action->process();
}
}
8 changes: 6 additions & 2 deletions classes/Helpers/FileSystemHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

namespace APP\plugins\generic\latexConverter\classes\Helpers;

use FilesystemIterator;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;

class FileSystemHelper
{
/**
Expand Down Expand Up @@ -59,8 +63,8 @@ public static function getDirectoryFilesRecursively(string $path): array

if (empty($path) || !file_exists($path)) return $files;

$iterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($path, \FilesystemIterator::SKIP_DOTS));
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS));

foreach ($iterator as $file) {
if (!is_dir($file)) {
Expand Down
6 changes: 3 additions & 3 deletions classes/Workflow/Convert.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,15 @@ class Convert
*/
protected string $latexExe = '';

function __construct(LatexConverterPlugin $plugin, PKPRequest $request, $args)
function __construct(LatexConverterPlugin &$plugin)
{
$this->timeStamp = date('Ymd_His');

$this->plugin = $plugin;
$this->plugin = &$plugin;

$this->notificationManager = new NotificationManager();

$this->request = $request;
$this->request = $this->plugin->getRequest();

$this->submissionFileId = (int)$this->request->getUserVar('submissionFileId');
$this->submissionFile = Repo::submissionFile()->get($this->submissionFileId);
Expand Down
6 changes: 3 additions & 3 deletions classes/Workflow/Extract.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,17 @@ class Extract extends Form
*/
protected string $latexConverterSelectedFilenameKey = 'latexConverter_SelectedFilename';

function __construct(LatexConverterPlugin $plugin, PKPRequest $request, $args)
function __construct(LatexConverterPlugin &$plugin)
{
$this->timeStamp = date('Ymd_His');

$this->plugin = $plugin;
$this->plugin = &$plugin;

$this->fileManager = new PrivateFileManager();

$this->notificationManager = new NotificationManager();

$this->request = $request;
$this->request = $this->plugin->getRequest();

$this->submissionFileId = (int)$this->request->getUserVar('submissionFileId');
$this->submissionFile = Repo::submissionFile()->get($this->submissionFileId);
Expand Down
42 changes: 14 additions & 28 deletions classes/Workflow/Links.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,22 +100,27 @@ public function execute(string $hookName, array $args): void
$row->addAction(
new LinkAction(
'latexconverter_extract_zip',
new AjaxModal(
$path,
__('plugins.generic.latexConverter.modal.extract.title')
),
new AjaxModal($path, __('plugins.generic.latexConverter.modal.extract.title')),
__('plugins.generic.latexConverter.button.extract'),
null
)
);

} // only show link if file is tex and is not dependent file (assocId is null)
elseif (strtolower($fileExtension) == Constants::texFileType
&& empty($submissionFile->getData('assocId'))) {

$disableLink = true;
$linkText = $this->getConvertButton();
if ($linkText === 'plugins.generic.latexConverter.button.convert') $disableLink = false;
$disableLink = false;
$latexExe = $this->plugin->getSetting(
$this->plugin->getRequest()->getContext()->getId(),
Constants::settingKeyPathExecutable);

$linkText = 'plugins.generic.latexConverter.button.convert';
if (strlen($latexExe) == 0) {
$linkText = 'plugins.generic.latexConverter.executable.notConfigured';
$disableLink = true;
} elseif (!is_executable($latexExe)) {
$linkText = 'plugins.generic.latexConverter.executable.notFound';
$disableLink = true;
}

$path = $dispatcher->url(
$request,
Expand All @@ -141,23 +146,4 @@ public function execute(string $hookName, array $args): void
}
}
}

/**
* Converter button depending on the latex executable status
*
* @return string
*/
public function getConvertButton(): string
{
$latexExe = $this->plugin->getSetting($this->plugin->getRequest()->getContext()->getId(),
Constants::settingKeyPathExecutable);

if (strlen($latexExe) == 0) {
return 'plugins.generic.latexConverter.executable.notConfigured';
} elseif (!is_executable($latexExe)) {
return 'plugins.generic.latexConverter.executable.notFound';
}

return 'plugins.generic.latexConverter.button.convert';
}
}
2 changes: 0 additions & 2 deletions vendor/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@

return array(
'APP\\plugins\\generic\\latexConverter\\classes\\Constants' => $baseDir . '/classes/Constants.php',
'APP\\plugins\\generic\\latexConverter\\classes\\Handler\\LoadHandler' => $baseDir . '/classes/Handler/LoadHandler.php',
'APP\\plugins\\generic\\latexConverter\\classes\\Handler\\PluginHandler' => $baseDir . '/classes/Handler/PluginHandler.php',
'APP\\plugins\\generic\\latexConverter\\classes\\Helpers\\FileSystemHelper' => $baseDir . '/classes/Helpers/FileSystemHelper.php',
'APP\\plugins\\generic\\latexConverter\\classes\\Helpers\\LogHelper' => $baseDir . '/classes/Helpers/LogHelper.php',
'APP\\plugins\\generic\\latexConverter\\classes\\Helpers\\SubmissionFileHelper' => $baseDir . '/classes/Helpers/SubmissionFileHelper.php',
'APP\\plugins\\generic\\latexConverter\\classes\\Helpers\\ZipHelper' => $baseDir . '/classes/Helpers/ZipHelper.php',
'APP\\plugins\\generic\\latexConverter\\classes\\Settings\\Actions' => $baseDir . '/classes/Settings/Actions.php',
Expand Down
2 changes: 0 additions & 2 deletions vendor/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ class ComposerStaticInit1b66bd88bb6dab52bf3af9b9a16ca7a1
{
public static $classMap = array (
'APP\\plugins\\generic\\latexConverter\\classes\\Constants' => __DIR__ . '/../..' . '/classes/Constants.php',
'APP\\plugins\\generic\\latexConverter\\classes\\Handler\\LoadHandler' => __DIR__ . '/../..' . '/classes/Handler/LoadHandler.php',
'APP\\plugins\\generic\\latexConverter\\classes\\Handler\\PluginHandler' => __DIR__ . '/../..' . '/classes/Handler/PluginHandler.php',
'APP\\plugins\\generic\\latexConverter\\classes\\Helpers\\FileSystemHelper' => __DIR__ . '/../..' . '/classes/Helpers/FileSystemHelper.php',
'APP\\plugins\\generic\\latexConverter\\classes\\Helpers\\LogHelper' => __DIR__ . '/../..' . '/classes/Helpers/LogHelper.php',
'APP\\plugins\\generic\\latexConverter\\classes\\Helpers\\SubmissionFileHelper' => __DIR__ . '/../..' . '/classes/Helpers/SubmissionFileHelper.php',
'APP\\plugins\\generic\\latexConverter\\classes\\Helpers\\ZipHelper' => __DIR__ . '/../..' . '/classes/Helpers/ZipHelper.php',
'APP\\plugins\\generic\\latexConverter\\classes\\Settings\\Actions' => __DIR__ . '/../..' . '/classes/Settings/Actions.php',
Expand Down
2 changes: 1 addition & 1 deletion version.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<version>
<application>latexConverter</application>
<type>plugins.generic</type>
<release>3.5.0.3</release>
<release>3.5.0.4</release>
<date>2024-06-01</date>
<lazy-load>1</lazy-load>
<class>LatexConverterPlugin</class>
Expand Down

0 comments on commit 7a26503

Please sign in to comment.