Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
/**
* @file plugins/generic/myMetaplugin/MyMetapluginPlugin.inc.php
*
* Copyright (c) 2014-2017 Simon Fraser University
* Copyright (c) 2003-2017 John Willinsky
* Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
*
* @class MyMetapluginPlugin
* @ingroup plugins_generic_myMetaplugin
*
* @brief MyMetaplugin plugin class
*/
import('lib.pkp.classes.plugins.GenericPlugin');
class MyMetapluginPlugin extends GenericPlugin {
/**
* Called as a plugin is registered to the registry
* @param $category String Name of category plugin was registered to
* @return boolean True if plugin initialized successfully; if false,
* the plugin will not be registered.
*/
function register($category, $path) {
$success = parent::register($category, $path);
if ($success && $this->getEnabled()) {
HookRegistry::register('TemplateManager::display', array($this, 'metadataFieldEdit'));
HookRegistry::register('issueentrypublicationmetadataform::readuservars', array($this, 'metadataReadUserVars'));
HookRegistry::register('issueentrypublicationmetadataform::execute', array($this, 'metadataExecute'));
HookRegistry::register('articledao::getAdditionalFieldNames', array($this, 'metadataGetFieldNames'));
}
return $success;
}
/**
* @copydoc Plugin::getDisplayName()
*/
function getDisplayName() {
return __('plugins.generic.myMetaplugin.displayName');
}
/**
* @copydoc Plugin::getDescription()
*/
function getDescription() {
return __('plugins.generic.myMetaplugin.description');
}
/**
* Insert myMetaValue field into schedule publication form
*/
function metadataFieldEdit($hookName, $params) {
$template =& $params[1];
if ($template != "controllers/tab/issueEntry/form/publicationMetadataFormFields.tpl") return false;
$templateMgr =& $params[0];
$templateMgr->register_outputfilter(array($this, 'formFilter'));
return false;
}
/**
* Output filter to add myMetaValue form field
*/
function formFilter($output, &$templateMgr) {
// match for form buttons div
if (preg_match('/<div class=\"section formButtons/', $output, $matches, PREG_OFFSET_CAPTURE) AND !strpos($output, 'id="myMetaValue"')) {
$match = $matches[0][0];
$offset = $matches[0][1];
// Get the value and assign
$fbv = $templateMgr->getFBV();
$form = $fbv->getForm();
$article = $form->getSubmission();
$myMetaValue = $article->getData('myMetaValue');
$templateMgr->assign(array(
'myMetaValue' => $myMetaValue,
));
// insert the template before the buttons
$newOutput = substr($output, 0, $offset);
$newOutput .= $templateMgr->fetch($this->getTemplatePath() . 'myMetaField.tpl');
$newOutput .= substr($output, $offset);
$output = $newOutput;
}
$templateMgr->unregister_outputfilter('formFilter');
return $output;
}
/**
* Add myMetaValue element to the article
*/
function metadataGetFieldNames($hookName, $params) {
$fields =& $params[1];
$fields[] = 'myMetaValue';
return false;
}
/**
* Set article myMetaValue
*/
function metadataExecute($hookName, $params) {
$form =& $params[0];
$article = $form->getSubmission();
$myMetaValue = $form->getData('myMetaValue');
$article->setData('myMetaValue', $myMetaValue);
return false;
}
/**
* Concern myMetaValue field in the form
*/
function metadataReadUserVars($hookName, $params) {
$userVars =& $params[1];
$userVars[] = 'myMetaValue';
return false;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment