Open Journal Systems  3.3.0
PKPPubIdPlugin.inc.php
1 <?php
2 
16 import('lib.pkp.classes.plugins.Plugin');
17 
18 abstract class PKPPubIdPlugin extends LazyLoadPlugin {
19 
20  //
21  // Implement template methods from Plugin
22  //
26  function register($category, $path, $mainContextId = null) {
27  if (!parent::register($category, $path, $mainContextId)) return false;
28  if ($this->getEnabled($mainContextId)) {
29  // Enable storage of additional fields.
30  foreach($this->getDAOs() as $dao) {
31  // Augment the object with the additional properties required by the pub ID plugin.
32  if ($dao instanceof SchemaDAO) {
33  // Schema-backed DAOs need the schema extended.
34  HookRegistry::register('Schema::get::' . $dao->schemaName, array($this, 'addToSchema'));
35  } else {
36  // For non-schema-backed DAOs, DAOName::getAdditionalFieldNames can be used.
37  HookRegistry::register(strtolower_codesafe(get_class($dao)).'::getAdditionalFieldNames', array($this, 'getAdditionalFieldNames'));
38  if (strtolower_codesafe(get_class($dao)) == 'submissionfiledao') {
39  // if it is a file, consider all file delegates
40  $fileDAOdelegates = $this->getFileDAODelegates();
41  foreach ($fileDAOdelegates as $fileDAOdelegate) {
42  HookRegistry::register(strtolower_codesafe($fileDAOdelegate).'::getAdditionalFieldNames', array($this, 'getAdditionalFieldNames'));
43  }
44  }
45  }
46  }
47  }
48  $this->addLocaleData();
49  return true;
50  }
51 
55  function getActions($request, $actionArgs) {
56  $router = $request->getRouter();
57  import('lib.pkp.classes.linkAction.request.AjaxModal');
58  return array_merge(
59  $this->getEnabled()?array(
60  new LinkAction(
61  'settings',
62  new AjaxModal(
63  $router->url($request, null, null, 'manage', null, $actionArgs),
64  $this->getDisplayName()
65  ),
66  __('manager.plugins.settings'),
67  null
68  ),
69  ):array(),
70  parent::getActions($request, $actionArgs)
71  );
72  }
73 
77  function manage($args, $request) {
78  $user = $request->getUser();
79  $router = $request->getRouter();
80  $context = $router->getContext($request);
81 
82  $form = $this->instantiateSettingsForm($context->getId());
83  $notificationManager = new NotificationManager();
84  switch ($request->getUserVar('verb')) {
85  case 'save':
86  $form->readInputData();
87  if ($form->validate()) {
88  $form->execute();
89  $notificationManager->createTrivialNotification($user->getId(), NOTIFICATION_TYPE_SUCCESS);
90  return new JSONMessage(true);
91  }
92  return new JSONMessage(true, $form->fetch($request));
93  case 'clearPubIds':
94  if (!$request->checkCSRF()) return new JSONMessage(false);
95  $contextDao = Application::getContextDAO();
96  $contextDao->deleteAllPubIds($context->getId(), $this->getPubIdType());
97  return new JSONMessage(true);
98  default:
99  $form->initData();
100  return new JSONMessage(true, $form->fetch($request));
101  }
102  return parent::manage($args, $request);
103  }
104 
105 
106  //
107  // Protected template methods to be implemented by sub-classes.
108  //
115  abstract function getPubId($pubObject);
116 
124  abstract function constructPubId($pubIdPrefix, $pubIdSuffix, $contextId);
125 
131  abstract function getPubIdType();
132 
137  abstract function getPubIdDisplayType();
138 
143  abstract function getPubIdFullName();
144 
151  abstract function getResolvingURL($contextId, $pubId);
152 
159  abstract function getPubIdMetadataFile();
160 
166  function addJavaScript($request, $templateMgr) { }
167 
174  abstract function getPubIdAssignFile();
175 
181  abstract function instantiateSettingsForm($contextId);
182 
188  abstract function getFormFieldNames();
189 
194  abstract function getAssignFormFieldName();
195 
200  abstract function getPrefixFieldName();
201 
206  abstract function getSuffixFieldName();
207 
213  abstract function getLinkActions($pubObject);
214 
219  abstract function getSuffixPatternsFieldNames();
220 
225  abstract function getDAOFieldNames();
226 
231  function getPubObjectTypes() {
232  return array('Publication', 'Representation', 'SubmissionFile');
233  }
234 
241  abstract function isObjectTypeEnabled($pubObjectType, $contextId);
242 
247  abstract function getNotUniqueErrorMsg();
248 
258  function verifyData($fieldName, $fieldValue, $pubObject, $contextId, &$errorMsg) {
259  // Verify pub id uniqueness.
260  if ($fieldName == $this->getSuffixFieldName()) {
261  if (empty($fieldValue)) return true;
262 
263  // Construct the potential new pub id with the posted suffix.
264  $pubIdPrefix = $this->getSetting($contextId, $this->getPrefixFieldName());
265  if (empty($pubIdPrefix)) return true;
266  $newPubId = $this->constructPubId($pubIdPrefix, $fieldValue, $contextId);
267 
268  if (!$this->checkDuplicate($newPubId, $pubObject->getId(), get_class($pubObject), $contextId)) {
269  $errorMsg = $this->getNotUniqueErrorMsg();
270  return false;
271  }
272  }
273  return true;
274  }
275 
281  function validatePubId($pubId) {
282  return true; // Assume a valid ID by default;
283  }
284 
290  function getDAOs() {
291  return array(
292  DAORegistry::getDAO('PublicationDAO'),
293  DAORegistry::getDAO('SubmissionDAO'),
295  DAORegistry::getDAO('SubmissionFileDAO'),
296  );
297  }
298 
303  function getFileDAODelegates() {
304  return array('SubmissionFileDAODelegate', 'SupplementaryFileDAODelegate', 'SubmissionArtworkFileDAODelegate');
305  }
306 
315  function canBeAssigned($pubObject) {
316  // Has the pub id already been assigned.
317  $pubIdType = $this->getPubIdType();
318  $storedPubId = $pubObject->getStoredPubId($pubIdType);
319  if ($storedPubId) return false;
320  // Get the pub id.
321  $pubId = $this->getPubId($pubObject);
322  // Is the custom suffix empty i.e. the pub id null.
323  if (!$pubId) return false;
324  // Does the suffix contain unresolved pattern.
325  $containPatterns = strpos($pubId, '%') !== false;
326  return !$containPatterns;
327  }
328 
337  public function addToSchema($hookName, $params) {
338  $schema =& $params[0];
339  foreach (array_merge($this->getFormFieldNames(), $this->getDAOFieldNames()) as $fieldName) {
340  $schema->properties->{$fieldName} = (object) [
341  'type' => 'string',
342  'apiSummary' => true,
343  'validation' => ['nullable'],
344  ];
345  }
346  return false;
347  }
348 
357  function getAdditionalFieldNames($hookName, $params) {
358  $fields =& $params[1];
359  foreach (array_merge($this->getFormFieldNames(), $this->getDAOFieldNames()) as $fieldName) {
360  $fields[] = $fieldName;
361  }
362  return false;
363  }
364 
370  function getPubObjectType($pubObject) {
371  $allowedTypes = $this->getPubObjectTypes();
372  $pubObjectType = null;
373  foreach ($allowedTypes as $allowedType) {
374  if (is_a($pubObject, $allowedType)) {
375  $pubObjectType = $allowedType;
376  break;
377  }
378  }
379  if (is_null($pubObjectType)) {
380  // This must be a dev error, so bail with an assertion.
381  assert(false);
382  return null;
383  }
384  return $pubObjectType;
385  }
386 
392  function setStoredPubId(&$pubObject, $pubId) {
393  $dao = $pubObject->getDAO();
394  $dao->changePubId($pubObject->getId(), $this->getPubIdType(), $pubId);
395  $pubObject->setStoredPubId($this->getPubIdType(), $pubId);
396  }
397 
398 
399  //
400  // Public API
401  //
414  function checkDuplicate($pubId, $pubObjectType, $excludeId, $contextId) {
415  foreach ($this->getPubObjectTypes() as $type) {
416  if ($type === 'Publication') {
417  $typeDao = DAORegistry::getDAO('PublicationDAO'); /* @var $typeDao PublicationDAO */
418  } elseif ($type === 'Representation') {
420  } elseif ($type === 'SubmissionFile') {
421  $typeDao = DAORegistry::getDAO('SubmissionFileDAO'); /* @var $typeDao SubmissionFileDAO */
422  }
423  $excludeTypeId = $type === $pubObjectType ? $excludeId : null;
424  if (isset($typeDao) && $typeDao->pubIdExists($this->getPubIdType(), $pubId, $excludeTypeId, $contextId)) {
425  return false;
426  }
427  }
428 
429  return true;
430  }
431 
437  function getContext($contextId) {
438  assert(is_numeric($contextId));
439 
440  // Get the context object from the context (optimized).
441  $request = Application::get()->getRequest();
442  $router = $request->getRouter();
443  $context = $router->getContext($request);
444  if ($context && $context->getId() == $contextId) return $context;
445 
446  // Fall back the database.
447  $contextDao = Application::getContextDAO();
448  return $contextDao->getById($contextId);
449  }
450 
451 }
452 
453 
PKPPubIdPlugin\getAdditionalFieldNames
getAdditionalFieldNames($hookName, $params)
Definition: PKPPubIdPlugin.inc.php:357
PKPPubIdPlugin\getPubIdAssignFile
getPubIdAssignFile()
Application\getContextDAO
static getContextDAO()
Definition: Application.inc.php:137
PKPPubIdPlugin\getContext
getContext($contextId)
Definition: PKPPubIdPlugin.inc.php:437
PKPPubIdPlugin\getFileDAODelegates
getFileDAODelegates()
Definition: PKPPubIdPlugin.inc.php:303
Application\getRepresentationDAO
static getRepresentationDAO()
Definition: Application.inc.php:162
DAORegistry\getDAO
static & getDAO($name, $dbconn=null)
Definition: DAORegistry.inc.php:57
PKPPubIdPlugin\addJavaScript
addJavaScript($request, $templateMgr)
Definition: PKPPubIdPlugin.inc.php:166
PKPPubIdPlugin\getNotUniqueErrorMsg
getNotUniqueErrorMsg()
PKPPubIdPlugin\getPubId
getPubId($pubObject)
PKPPubIdPlugin\getSuffixFieldName
getSuffixFieldName()
PKPPubIdPlugin\isObjectTypeEnabled
isObjectTypeEnabled($pubObjectType, $contextId)
PKPPubIdPlugin\getActions
getActions($request, $actionArgs)
Definition: PKPPubIdPlugin.inc.php:55
PKPPubIdPlugin\manage
manage($args, $request)
Definition: PKPPubIdPlugin.inc.php:77
PKPPubIdPlugin\setStoredPubId
setStoredPubId(&$pubObject, $pubId)
Definition: PKPPubIdPlugin.inc.php:392
PKPPubIdPlugin\getAssignFormFieldName
getAssignFormFieldName()
PKPPubIdPlugin\getPubIdFullName
getPubIdFullName()
PKPPubIdPlugin\addToSchema
addToSchema($hookName, $params)
Definition: PKPPubIdPlugin.inc.php:337
PKPPubIdPlugin\getPubIdDisplayType
getPubIdDisplayType()
Plugin\getEnabled
getEnabled()
Definition: Plugin.inc.php:868
LazyLoadPlugin
Definition: LazyLoadPlugin.inc.php:19
JSONMessage
Class to represent a JSON (Javascript Object Notation) message.
Definition: JSONMessage.inc.php:18
Plugin\getSetting
getSetting($contextId, $name)
Definition: Plugin.inc.php:473
PKPPubIdPlugin\getResolvingURL
getResolvingURL($contextId, $pubId)
AjaxModal
A modal that retrieves its content from via AJAX.
Definition: AjaxModal.inc.php:18
PKPPubIdPlugin\checkDuplicate
checkDuplicate($pubId, $pubObjectType, $excludeId, $contextId)
Definition: PKPPubIdPlugin.inc.php:414
LinkAction
Base class defining an action that can be performed by the user in the user interface.
Definition: LinkAction.inc.php:22
PKPPubIdPlugin\getPubObjectTypes
getPubObjectTypes()
Definition: PKPPubIdPlugin.inc.php:231
PKPPubIdPlugin\canBeAssigned
canBeAssigned($pubObject)
Definition: PKPPubIdPlugin.inc.php:315
PKPPubIdPlugin\getPrefixFieldName
getPrefixFieldName()
PKPPubIdPlugin\getLinkActions
getLinkActions($pubObject)
PKPPubIdPlugin
Abstract class for public identifiers plugins.
Definition: PKPPubIdPlugin.inc.php:18
PKPPubIdPlugin\getPubIdType
getPubIdType()
strtolower_codesafe
strtolower_codesafe($str)
Definition: functions.inc.php:280
PKPPubIdPlugin\getDAOs
getDAOs()
Definition: PKPPubIdPlugin.inc.php:290
PKPPubIdPlugin\getDAOFieldNames
getDAOFieldNames()
PKPPubIdPlugin\validatePubId
validatePubId($pubId)
Definition: PKPPubIdPlugin.inc.php:281
Plugin\$request
$request
Definition: Plugin.inc.php:68
PKPPubIdPlugin\getPubIdMetadataFile
getPubIdMetadataFile()
Plugin\addLocaleData
addLocaleData($locale=null)
Definition: Plugin.inc.php:454
PKPPubIdPlugin\getFormFieldNames
getFormFieldNames()
HookRegistry\register
static register($hookName, $callback, $hookSequence=HOOK_SEQUENCE_NORMAL)
Definition: HookRegistry.inc.php:70
NotificationManager
Definition: NotificationManager.inc.php:19
PKPApplication\get
static get()
Definition: PKPApplication.inc.php:235
PKPPubIdPlugin\getPubObjectType
getPubObjectType($pubObject)
Definition: PKPPubIdPlugin.inc.php:370
PKPPubIdPlugin\verifyData
verifyData($fieldName, $fieldValue, $pubObject, $contextId, &$errorMsg)
Definition: PKPPubIdPlugin.inc.php:258
PKPPubIdPlugin\constructPubId
constructPubId($pubIdPrefix, $pubIdSuffix, $contextId)
PKPPubIdPlugin\getSuffixPatternsFieldNames
getSuffixPatternsFieldNames()
PKPPubIdPlugin\instantiateSettingsForm
instantiateSettingsForm($contextId)
SchemaDAO
A base class for DAOs which rely on a json-schema file to define the data object.
Definition: SchemaDAO.inc.php:18