Open Monograph Press  3.3.0
PubIdPlugin.inc.php
1 <?php
2 
16 import('lib.pkp.classes.plugins.PKPPubIdPlugin');
17 
18 abstract class PubIdPlugin extends PKPPubIdPlugin {
19 
23  function __construct() {
24  parent::__construct();
25  }
26 
27 
28  //
29  // Protected template methods from PKPPlubIdPlugin
30  //
34  function getPubObjectTypes() {
35  $pubObjectTypes = parent::getPubObjectTypes();
36  array_push($pubObjectTypes, 'Chapter');
37  return $pubObjectTypes;
38  }
39 
43  function getPubId($pubObject) {
44  // Get the pub id type
45  $pubIdType = $this->getPubIdType();
46 
47  // If we already have an assigned pub id, use it.
48  $storedPubId = $pubObject->getStoredPubId($pubIdType);
49  if ($storedPubId) return $storedPubId;
50 
51  // Determine the type of the publishing object.
52  $pubObjectType = $this->getPubObjectType($pubObject);
53 
54  // Initialize variables for publication objects.
55  $submission = ($pubObjectType == 'Submission' ? $pubObject : null);
56  $representation = ($pubObjectType == 'Representation' ? $pubObject : null);
57  $submissionFile = ($pubObjectType == 'SubmissionFile' ? $pubObject : null);
58  $chapter = ($pubObjectType == 'Chapter' ? $pubObject : null);
59 
60  // Get the context id.
61  if ($pubObjectType == 'Submission') {
62  $contextId = $pubObject->getContextId();
63  } else {
64  // Retrieve the submission.
65  if (is_a($pubObject, 'Chapter') || is_a($pubObject, 'Representation')) {
66  $publication = Services::get('publication')->get($pubObject->getData('publicationId'));
67  $submission = Services::get('submission')->get($publication->getData('submissionId'));
68  } else {
69  assert(is_a($pubObject, 'SubmissionFile'));
70  $submission = Services::get('submission')->get($pubObject->getSubmissionId());
71  }
72  if (!$submission) return null;
73  // Now we can identify the context.
74  $contextId = $submission->getContextId();
75  }
76  // Check the context
77  $context = $this->getContext($contextId);
78  if (!$context) return null;
79  $contextId = $context->getId();
80 
81  // Check whether pub ids are enabled for the given object type.
82  $objectTypeEnabled = $this->isObjectTypeEnabled($pubObjectType, $contextId);
83  if (!$objectTypeEnabled) return null;
84 
85  // Retrieve the pub id prefix.
86  $pubIdPrefix = $this->getSetting($contextId, $this->getPrefixFieldName());
87  if (empty($pubIdPrefix)) return null;
88 
89  // Generate the pub id suffix.
90  $suffixFieldName = $this->getSuffixFieldName();
91  $suffixGenerationStrategy = $this->getSetting($contextId, $suffixFieldName);
92  switch ($suffixGenerationStrategy) {
93  case 'customId':
94  $pubIdSuffix = $pubObject->getData($suffixFieldName);
95  break;
96 
97  case 'pattern':
98  $suffixPatternsFieldNames = $this->getSuffixPatternsFieldNames();
99  $pubIdSuffix = $this->getSetting($contextId, $suffixPatternsFieldNames[$pubObjectType]);
100 
101  // %p - press initials
102  $pubIdSuffix = PKPString::regexp_replace('/%p/', PKPString::strtolower($context->getAcronym($context->getPrimaryLocale())), $pubIdSuffix);
103 
104  // %x - custom identifier
105  if ($pubObject->getStoredPubId('publisher-id')) {
106  $pubIdSuffix = PKPString::regexp_replace('/%x/', $pubObject->getStoredPubId('publisher-id'), $pubIdSuffix);
107  }
108 
109  if ($submission) {
110  // %m - monograph id
111  $pubIdSuffix = PKPString::regexp_replace('/%m/', $submission->getId(), $pubIdSuffix);
112  }
113 
114  if ($chapter) {
115  // %c - chapter id
116  $pubIdSuffix = PKPString::regexp_replace('/%c/', $chapter->getId(), $pubIdSuffix);
117  }
118 
119  if ($representation) {
120  // %f - publication format id
121  $pubIdSuffix = PKPString::regexp_replace('/%f/', $representation->getId(), $pubIdSuffix);
122  }
123 
124  if ($submissionFile) {
125  // %s - file id
126  $pubIdSuffix = PKPString::regexp_replace('/%s/', $submissionFile->getFileId(), $pubIdSuffix);
127  }
128 
129  break;
130 
131  default:
132  $pubIdSuffix = PKPString::strtolower($context->getAcronym($context->getPrimaryLocale()));
133 
134  if ($submission) {
135  $pubIdSuffix .= '.' . $submission->getId();
136  }
137 
138  if ($chapter) {
139  $pubIdSuffix .= '.c' . $chapter->getId();
140  }
141 
142  if ($representation) {
143  $pubIdSuffix .= '.' . $representation->getId();
144  }
145 
146  if ($submissionFile) {
147  $pubIdSuffix .= '.' . $submissionFile->getFileId();
148  }
149  }
150  if (empty($pubIdSuffix)) return null;
151 
152  // Costruct the pub id from prefix and suffix.
153  $pubId = $this->constructPubId($pubIdPrefix, $pubIdSuffix, $contextId);
154 
155  return $pubId;
156  }
157 
161  function getDAOs() {
162  return array_merge(parent::getDAOs(), array('Chapter' => DAORegistry::getDAO('ChapterDAO')));
163  }
164 
168  function checkDuplicate($pubId, $pubObjectType, $excludeId, $contextId) {
169  foreach ($this->getPubObjectTypes() as $type) {
170  if ($type === 'Chapter') {
171  $excludeTypeId = $type === $pubObjectType ? $excludeId : null;
172  if (DAORegistry::getDAO('ChapterDAO')->pubIdExists($this->getPubIdType(), $pubId, $excludeTypeId, $contextId)) {
173  return false;
174  }
175  }
176  }
177 
178  return true;
179  }
180 }
181 
182 
PubIdPlugin\getDAOs
getDAOs()
Definition: PubIdPlugin.inc.php:161
PKPString\regexp_replace
static regexp_replace($pattern, $replacement, $subject, $limit=-1)
Definition: PKPString.inc.php:279
PKPPubIdPlugin\getContext
getContext($contextId)
Definition: PKPPubIdPlugin.inc.php:437
DAORegistry\getDAO
static & getDAO($name, $dbconn=null)
Definition: DAORegistry.inc.php:57
PubIdPlugin
Public identifiers plugins common functions.
Definition: PubIdPlugin.inc.php:18
PKPPubIdPlugin\getSuffixFieldName
getSuffixFieldName()
PKPPubIdPlugin\isObjectTypeEnabled
isObjectTypeEnabled($pubObjectType, $contextId)
PubIdPlugin\__construct
__construct()
Definition: PubIdPlugin.inc.php:23
PubIdPlugin\checkDuplicate
checkDuplicate($pubId, $pubObjectType, $excludeId, $contextId)
Definition: PubIdPlugin.inc.php:168
PubIdPlugin\getPubObjectTypes
getPubObjectTypes()
Definition: PubIdPlugin.inc.php:34
Plugin\getSetting
getSetting($contextId, $name)
Definition: Plugin.inc.php:473
PKPPubIdPlugin\getPrefixFieldName
getPrefixFieldName()
PubIdPlugin\getPubId
getPubId($pubObject)
Definition: PubIdPlugin.inc.php:43
PKPPubIdPlugin
Abstract class for public identifiers plugins.
Definition: PKPPubIdPlugin.inc.php:18
PKPPubIdPlugin\getPubIdType
getPubIdType()
PKPString\strtolower
static strtolower($string)
Definition: PKPString.inc.php:169
PKPPubIdPlugin\getPubObjectType
getPubObjectType($pubObject)
Definition: PKPPubIdPlugin.inc.php:370
PKPPubIdPlugin\constructPubId
constructPubId($pubIdPrefix, $pubIdSuffix, $contextId)
PKPPubIdPlugin\getSuffixPatternsFieldNames
getSuffixPatternsFieldNames()
PKPServices\get
static get($service)
Definition: PKPServices.inc.php:49