Skip to content

Commit

Permalink
pkp/pkp-lib#5523, pkp#17: refactor block names and settings, migrate …
Browse files Browse the repository at this point in the history
…block sidebar contexts
  • Loading branch information
ctgraham committed Mar 3, 2020
1 parent a8dbeb1 commit 41d340f
Show file tree
Hide file tree
Showing 28 changed files with 110 additions and 90 deletions.
80 changes: 79 additions & 1 deletion CustomBlockManagerPlugin.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
*
*/

// No constant name in core 3.2 (!?!), per https://github.com/pkp/pkp-lib/commit/a76bac72ed068a1d1866398d20cdf28c4977249f#diff-70caff5ef9a513397af1833a3e2a3c7c
import('lib.pkp.classes.plugins.BlockPlugin');
if (!defined('BLOCK_CONTEXT_SIDEBAR')) {
define('BLOCK_CONTEXT_SIDEBAR', 1);
}

import('lib.pkp.classes.plugins.GenericPlugin');

class CustomBlockManagerPlugin extends GenericPlugin {
Expand Down Expand Up @@ -62,7 +68,7 @@ function register($category, $path, $mainContextId = null) {
foreach ($blocks as $block) {
PluginRegistry::register(
'blocks',
new CustomBlockPlugin($block, $this),
new CustomBlockPlugin($block, $this, $contextId),
$this->getPluginPath()
);
}
Expand Down Expand Up @@ -146,4 +152,76 @@ function manage($args, $request) {
function isSitePlugin() {
return !Application::get()->getRequest()->getContext();
}

/**
* Create a unique name for a child plugin
*
* @return string
*/
function createUniqueName() {
return str_replace('.', 'x', uniqid($this->getUniqueNamePrefix(), true));
}

/**
* Get the name prefix for a child plugin
*
* @return string
*/
function getUniqueNamePrefix() {
return $this->getName().'__';
}

/**
* We will need to modify data in certain upgrades.
*
* @param $hookName string
* @param $args array
* @return boolean
*/
function installFilters($hookName, $args) {
// There is no opportunity to hook the upgrade event before the new version is written to the versions table.
// The only function automatically called in installPluginVersion::execute() is installFilters(), so we hijack this.
// So, we need to look at the immediately preceeding version, and (re)apply fixes based on guesswork.
$versionDao = DAORegistry::getDAO('VersionDAO');
$contextDao = Application::getContextDAO();
$historicVersions = $versionDao->getVersionHistory('plugins.generic', 'customBlockManager');
if (count($historicVersions) > 1 && $historicVersions[1]->compare('1.3.0') < 0) {
// The last installed version is prior to 1.3.0
// We need up update the plugin_settings names and move any orphaned sidebar contexts
$contexts = $contextDao->getAll();
while ($context = $contexts->next()) {
// Load the custom blocks we have created
$blocks = $this->getSetting($context->getId(), 'blocks');
if (!is_array($blocks)) $blocks = array();
$pluginSettingsDao = DAORegistry::getDAO('PluginSettingsDAO');
$newBlocks = array();
foreach ($blocks as $blockName) {
// Current block uses old naming
if (strpos($blockName, $this->getUniqueNamePrefix()) !== 0) {
$newBlockName = $this->createUniqueName();
// Update plugin_settings
$settings = $pluginSettingsDao->getPluginSettings($context->getId(), $blockName);
foreach ($settings as $setting_name => $setting_value) {
switch ($setting_name) {
case 'context':
$setting_value = BLOCK_CONTEXT_SIDEBAR;
case 'blockContent':
case 'enabled':
case 'seq':
$pluginSettingsDao->deleteSetting($context->getId(), $blockName, $setting_name);
$pluginSettingsDao->updateSetting($context->getId(), $newBlockName, $setting_name, $setting_value);
break;
default:
error_log('found an unrecognized setting "'.$setting_name.'", in custom block "'.$blockName.'"');
}
}
$pluginSettingsDao->updateSetting($context->getId(), $newBlockName, 'blockDisplayName', $blockName);
}
$newBlocks[] = $newBlockName;
}
$this->updateSetting($context->getId(), 'blocks', $newBlocks);
}
}
return parent::installFilters($hookName, $args);
}
}
8 changes: 6 additions & 2 deletions CustomBlockPlugin.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
class CustomBlockPlugin extends BlockPlugin {
/** @var string Name of this block plugin */
var $_blockName;
/** @var int Context ID */
var $_contextId;

/** @var CustomBlockManagerPlugin Parent plugin */
var $_parentPlugin;
Expand All @@ -27,10 +29,12 @@ class CustomBlockPlugin extends BlockPlugin {
* Constructor
* @param $blockName string Name of this block plugin.
* @param $parentPlugin CustomBlockManagerPlugin Custom block plugin management plugin.
* @param $contextId int The context in which this plugin lives
*/
function __construct($blockName, $parentPlugin) {
function __construct($blockName, $parentPlugin, $contextId) {
$this->_blockName = $blockName;
$this->_parentPlugin = $parentPlugin;
$this->_contextId = $contextId;
parent::__construct();
}

Expand Down Expand Up @@ -84,7 +88,7 @@ function getEnabled($contextId = null) {
* @copydoc Plugin::getDisplayName()
*/
function getDisplayName() {
return $this->_blockName . ' ' . __('plugins.generic.customBlock.nameSuffix');
return $this->getSetting($this->_contextId, 'blockDisplayName') . ' ' . __('plugins.generic.customBlock.nameSuffix');
}

/**
Expand Down
13 changes: 7 additions & 6 deletions README
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
=============================================================
=== Custom Block Manager Plugin
=== Version: 1.2
=== Version: 1.3.1
=== Author: Juan Pablo Alperin <[email protected]>
=== Co-Author: Bozana Bokan <[email protected]>
=============================================================
Expand All @@ -18,16 +18,17 @@ LICENSE for the complete terms of this license.
System Requirements
-------------------
This plugin is compatible with...
- OJS 3.0.x
- OMP 1.2.x
- OMP 1.1.x
- OJS 3.2.x
- OMP 3.2.x
- OPS 3.2.x

Installation
------------
To install the plugin:
The Plugin Gallery is the recommended installation method.
To install the plugin manually:
- Unpack the plugin tar.gz file to your plugins/generic directory
- From your application's installation directory, run the upgrade script:
$ php tools/upgrade.php upgrade
$ php lib/pkp/tools/installPluginVersion.php plugins/generic/customBlockManager/version.xml
(NOTE: It is recommended to back up your database first.)
- Enable the plugin by going to:
Management > Website Settings > Plugins > Generic Plugins
Expand Down
9 changes: 5 additions & 4 deletions controllers/grid/CustomBlockGridHandler.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ function initialize($request, $args = null) {
$blocks = $customBlockManagerPlugin->getSetting($contextId, 'blocks');
$gridData = array();
if (is_array($blocks)) foreach ($blocks as $block) {
$plugin = new CustomBlockPlugin($block, $customBlockManagerPlugin, $contextId);
$gridData[$block] = array(
'title' => $block
'title' => $plugin->getSetting($contextId, 'blockDisplayName')
);
}
$this->setGridDataElements($gridData);
Expand Down Expand Up @@ -134,15 +135,15 @@ function addCustomBlock($args, $request) {
function editCustomBlock($args, $request) {
$blockName = $request->getUserVar('blockName');
$context = $request->getContext();
$contextId = $context ? $context->getId() : 0;
$contextId = $context ? $context->getId() : CONTEXT_ID_NONE;
$this->setupTemplate($request);

$customBlockPlugin = null;
// If this is the edit of the existing custom block plugin,
if ($blockName) {
// Create the custom block plugin
import('plugins.generic.customBlockManager.CustomBlockPlugin');
$customBlockPlugin = new CustomBlockPlugin($blockName, CUSTOMBLOCKMANAGER_PLUGIN_NAME);
$customBlockPlugin = new CustomBlockPlugin($blockName, $this->plugin, $contextId);
}

// Create and present the edit form
Expand Down Expand Up @@ -171,7 +172,7 @@ function updateCustomBlock($args, $request) {
if ($pluginName) {
// Create the custom block plugin
import('plugins.generic.customBlockManager.CustomBlockPlugin');
$customBlockPlugin = new CustomBlockPlugin($pluginName, CUSTOMBLOCKMANAGER_PLUGIN_NAME);
$customBlockPlugin = new CustomBlockPlugin($pluginName, $this->plugin, $contextId);
}

// Create and populate the form
Expand Down
17 changes: 9 additions & 8 deletions controllers/grid/form/CustomBlockForm.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ function __construct($template, $contextId, $plugin = null) {
// Add form checks
$this->addCheck(new FormValidatorPost($this));
$this->addCheck(new FormValidatorCSRF($this));
$this->addCheck(new FormValidator($this, 'blockName', 'required', 'plugins.generic.customBlock.nameRequired'));
$this->addCheck(new FormValidatorRegExp($this, 'blockName', 'required', 'plugins.generic.customBlock.nameRegEx', '/^[a-zA-Z0-9_-]+$/'));
$this->addCheck(new FormValidator($this, 'blockDisplayName', 'required', 'plugins.generic.customBlock.nameRequired'));
}

/**
Expand All @@ -49,23 +48,23 @@ function initData() {
$contextId = $this->contextId;
$plugin = $this->plugin;

$templateMgr = TemplateManager::getManager();

$blockName = null;
$blockContent = null;
if ($plugin) {
$blockName = $plugin->getName();
$blockDisplayName = $plugin->getSetting($contextId, 'blockDisplayName');
$blockContent = $plugin->getSetting($contextId, 'blockContent');
}
$this->setData('blockContent', $blockContent);
$this->setData('blockName', $blockName);
$this->setData('blockDisplayName', $blockDisplayName);
}

/**
* Assign form data to user-submitted data.
*/
function readInputData() {
$this->readUserVars(array('blockName', 'blockContent'));
$this->readUserVars(array('blockName', 'blockDisplayName', 'blockContent'));
}

/**
Expand All @@ -74,11 +73,13 @@ function readInputData() {
function execute(...$functionArgs) {
$plugin = $this->plugin;
$contextId = $this->contextId;
$blockName = $this->getData('blockName');
if (!$plugin) {
// Create a new custom block plugin
import('plugins.generic.customBlockManager.CustomBlockPlugin');
$customBlockManagerPlugin = PluginRegistry::getPlugin('generic', CUSTOMBLOCKMANAGER_PLUGIN_NAME);
$plugin = new CustomBlockPlugin($this->getData('blockName'), $customBlockManagerPlugin);
$blockName = $customBlockManagerPlugin->createUniqueName();
$plugin = new CustomBlockPlugin($blockName, $customBlockManagerPlugin, $contextId);
// Default the block to being enabled
$plugin->setEnabled(true);

Expand All @@ -87,13 +88,13 @@ function execute(...$functionArgs) {
$blocks = $customBlockManagerPlugin->getSetting($contextId, 'blocks');
if (!isset($blocks)) $blocks = array();

array_push($blocks, $this->getData('blockName'));
$blocks[] = $blockName;
$customBlockManagerPlugin->updateSetting($contextId, 'blocks', $blocks);
}

// update custom block plugin content
$plugin->updateSetting($contextId, 'blockContent', $this->getData('blockContent'));

$plugin->updateSetting($contextId, 'blockDisplayName', $this->getData('blockDisplayName'));
parent::execute(...$functionArgs);
}
}
Expand Down
3 changes: 0 additions & 3 deletions locale/ar_IQ/locale.po
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,3 @@ msgstr "هذه كتلة مولدة من قبل المستخدم."

msgid "plugins.generic.customBlock.nameRequired"
msgstr "اسم الكتلة المخصصة مطلوب حتماً."

msgid "plugins.generic.customBlock.nameRegEx"
msgstr "اسم الكتلة المخصصة يجب أن يقتصر على الحروف والأرقام وعلامة الطرح والخط التحتاني."
3 changes: 0 additions & 3 deletions locale/bg_BG/locale.po
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,3 @@ msgstr "Това е егенериран от потребителя блок."

msgid "plugins.generic.customBlock.nameRequired"
msgstr "Изисква се име за потребителския блок."

msgid "plugins.generic.customBlock.nameRegEx"
msgstr "Името на потребителския блок трябва да съдърожа само букви, цифри и долно тире."
3 changes: 0 additions & 3 deletions locale/cs_CZ/locale.po
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,3 @@ msgstr "Tento plugin vám umožňuje spravovat (přidávat, upravovat a odstraň

msgid "plugins.generic.customBlock.nameRequired"
msgstr "Je třeba zadat jméno uživatelského bloku."

msgid "plugins.generic.customBlock.nameRegEx"
msgstr "Název uživatelského bloku musí obsahovat pouze písmena, čísla a pomlčky/podtržítka."
5 changes: 0 additions & 5 deletions locale/da_DK/locale.po
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,3 @@ msgstr "Dette er et brugerdefineret blok."

msgid "plugins.generic.customBlock.nameRequired"
msgstr "Et brugerdefineret blok-navn er påkrævet."

msgid "plugins.generic.customBlock.nameRegEx"
msgstr ""
"Det brugerdefinerede blok-navn må kun indeholde bogstaver, numre og "
"bindestreg/understregning."
3 changes: 0 additions & 3 deletions locale/de_DE/locale.po
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,3 @@ msgstr "Dies ist ein selbst angelegter Block."

msgid "plugins.generic.customBlock.nameRequired"
msgstr "Der Name des benutzerdefinierten Blocks wird benötigt."

msgid "plugins.generic.customBlock.nameRegEx"
msgstr "Der Name des benutzerdefinierten Blocks darf nur Buchstaben, Ziffern und Bindestriche/Unterstriche enthalten."
3 changes: 0 additions & 3 deletions locale/el_GR/locale.po
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,3 @@ msgstr "Αυτό είναι δομικό στοιχείο δημιουργημέ

msgid "plugins.generic.customBlock.nameRequired"
msgstr "Το όνομα του δομικού στοιχείου είναι απαραίτητο."

msgid "plugins.generic.customBlock.nameRegEx"
msgstr "Το όνομα του δομικού στοιχείου πρέπει να περιλαμβάνει μόνο γράμματα, αριθμούς και παύλες/κάτω παύλες."
3 changes: 0 additions & 3 deletions locale/en_US/locale.po
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,3 @@ msgstr "This is a user-generated block."

msgid "plugins.generic.customBlock.nameRequired"
msgstr "The custom block name is required."

msgid "plugins.generic.customBlock.nameRegEx"
msgstr "The custom block name must contain only letters, numbers, and hyphens/underscores."
3 changes: 0 additions & 3 deletions locale/es_ES/locale.po
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,3 @@ msgstr "Este es un Bloque generado por el usuario."

msgid "plugins.generic.customBlock.nameRequired"
msgstr "Se requiere el nombre del bloque personalizado."

msgid "plugins.generic.customBlock.nameRegEx"
msgstr "El nombre del bloque personalizado sólo debe contener letras, números y guiones/guiones bajos."
3 changes: 0 additions & 3 deletions locale/fi_FI/locale.po
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,3 @@ msgstr "Tämä on käyttäjän luoma lohko."

msgid "plugins.generic.customBlock.nameRequired"
msgstr "Mukautetun lohkon nimi vaaditaan."

msgid "plugins.generic.customBlock.nameRegEx"
msgstr "Mukautetun lohkon nimi voi sisältää vain kirjaimia, numeroita ja yhdysmerkkejä/alaviivoja."
3 changes: 0 additions & 3 deletions locale/fr_CA/locale.po
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,3 @@ msgstr "Aucun bloc personnalisé n'a été créé."

msgid "plugins.generic.customBlock.nameRequired"
msgstr "Le nom du bloc personnalisé est requis."

msgid "plugins.generic.customBlock.nameRegEx"
msgstr "Le nom du bloc personnalisé ne doit contenir que des lettres, des chiffres, des tirets et/ou des traits de soulignement."
3 changes: 0 additions & 3 deletions locale/hu_HU/locale.po
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,3 @@ msgstr "Ez egy felhasználó által generált blokk."

msgid "plugins.generic.customBlock.nameRequired"
msgstr "Egyéni blokknév kötelező."

msgid "plugins.generic.customBlock.nameRegEx"
msgstr "Az egyéni blokknév csak betűket, számokat és kötőjeleket/alulhúzásokat tartalmazhat."
4 changes: 0 additions & 4 deletions locale/id_ID/locale.po
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ msgstr "(Plugin Custom Block)"
msgid "plugins.generic.customBlock.description"
msgstr "Ini adalah user-generated block."

msgid "plugins.generic.customBlock.nameRegEx"
msgstr ""
"Nama blok baru hanya boleh mengandung huruf, angka, hyphens/underscores."

msgid "plugins.generic.customBlock.nameRequired"
msgstr "Nama blok baru wajib diisi."

Expand Down
3 changes: 0 additions & 3 deletions locale/it_IT/locale.po
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,3 @@ msgstr "Questo è un blocco generato dall'utente."

msgid "plugins.generic.customBlock.nameRequired"
msgstr "Inserire il nome del blocco personalizzato."

msgid "plugins.generic.customBlock.nameRegEx"
msgstr "Il nome del blocco personalizzato può contenere solo lettere, numeri e trattini alti o bassi."
3 changes: 0 additions & 3 deletions locale/pt_BR/locale.po
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,3 @@ msgstr "Não há blocos personalizados criados."

msgid "plugins.generic.customBlock.nameRequired"
msgstr "O nome do bloco personalizado é necessário."

msgid "plugins.generic.customBlock.nameRegEx"
msgstr "O nome do bloco personalizado deve conter apenas letras, números e hífens / sublinhados."
3 changes: 0 additions & 3 deletions locale/pt_PT/locale.po
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,3 @@ msgstr "Nenhum bloco personalizado criado."

msgid "plugins.generic.customBlock.nameRequired"
msgstr "O nome do bloco personalizado é obrigatório."

msgid "plugins.generic.customBlock.nameRegEx"
msgstr "O nome do bloco personalizado só pode conter letras, números e hífenes ou sublinhados."
3 changes: 0 additions & 3 deletions locale/ru_RU/locale.po
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,3 @@ msgstr "Это пользовательский блок."

msgid "plugins.generic.customBlock.nameRequired"
msgstr "Имя пользовательского блока обязательно."

msgid "plugins.generic.customBlock.nameRegEx"
msgstr "Имя пользовательского блока должно содержать только буквы, цифры, дефисы и подчеркивания."
3 changes: 0 additions & 3 deletions locale/sl_SI/locale.po
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,3 @@ msgstr "To blok, ki ga je ustvaril uporabnik."

msgid "plugins.generic.customBlock.nameRequired"
msgstr "Ime bloka je obvezno."

msgid "plugins.generic.customBlock.nameRegEx"
msgstr "Ime bloka lahko vsebuje le črke, številke in pomišljaj/podčrtaj."
3 changes: 0 additions & 3 deletions locale/sr_RS@latin/locale.po
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,3 @@ msgstr "Nema prilagođenih blokova."

msgid "plugins.generic.customBlock.nameRequired"
msgstr "Ime bloka je oavezno."

msgid "plugins.generic.customBlock.nameRegEx"
msgstr "Ime bloka može sadržati samo slova, brojeve, crte i podvlake."
Loading

0 comments on commit 41d340f

Please sign in to comment.