-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJournalCategoriesSettingsForm.php
More file actions
102 lines (85 loc) · 3.07 KB
/
Copy pathJournalCategoriesSettingsForm.php
File metadata and controls
102 lines (85 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
namespace APP\plugins\generic\JournalCategories;
use PKP\form\Form;
use PKP\form\validation\FormValidatorPost;
use PKP\form\validation\FormValidatorCSRF;
class JournalCategoriesSettingsForm extends Form
{
private JournalCategoriesPlugin $plugin;
public function __construct(JournalCategoriesPlugin $plugin)
{
parent::__construct($plugin->getTemplateResource('settingsForm.tpl'));
$this->plugin = $plugin;
$this->addCheck(new FormValidatorPost($this));
$this->addCheck(new FormValidatorCSRF($this));
}
/**
* Load saved settings into the form
*/
public function initData()
{
$categoriesJson = $this->plugin->getSetting(CONTEXT_SITE, 'categories');
$categories = $categoriesJson ? json_decode($categoriesJson, true) : [];
// Convert internal structure to the editable textarea format:
// Each line: CategoryName | journal_id1, journal_id2 | Description
$lines = [];
foreach ($categories as $name => $info) {
$ids = is_array($info['journal_ids'])
? implode(', ', $info['journal_ids'])
: $info['journal_ids'];
$lines[] = $name . ' | ' . $ids . ' | ' . ($info['description'] ?? '');
}
$this->setData('categoriesText', implode("\n", $lines));
}
/**
* Read form input
*/
public function readInputData()
{
$this->readUserVars(['categoriesText']);
}
/**
* Assign template variables and fetch the form
*/
public function fetch($request, $template = null, $display = false)
{
$templateMgr = \PKP\template\PKPTemplateManager::getManager($request);
$templateMgr->assign('pluginName', $this->plugin->getName());
return parent::fetch($request, $template, $display);
}
/**
* Parse and save the settings
*/
public function execute(...$functionArgs)
{
$rawText = trim($this->getData('categoriesText') ?? '');
$categories = [];
foreach (explode("\n", $rawText) as $line) {
$line = trim($line);
if ($line === '' || str_starts_with($line, '#')) {
continue; // skip blank lines and comments
}
$parts = array_map('trim', explode('|', $line));
if (count($parts) < 2) {
continue; // skip malformed lines
}
$name = $parts[0];
$idsRaw = $parts[1];
$description = $parts[2] ?? '';
// Parse IDs — accept comma-separated integers
$ids = array_values(
array_filter(
array_map('intval', array_map('trim', explode(',', $idsRaw)))
)
);
if ($name !== '') {
$categories[$name] = [
'journal_ids' => $ids,
'description' => $description,
];
}
}
$this->plugin->updateSetting(CONTEXT_SITE, 'categories', json_encode($categories), 'string');
return parent::execute(...$functionArgs);
}
}