Open Preprint Systems  3.3.0
PublicationService.inc.php
1 <?php
15 namespace APP\Services;
16 
17 use \Application;
18 use \AppLocale;
19 use \Core;
20 use \Services;
21 use \PKP\Services\PKPPublicationService;
22 use \HookRegistry;
23 use \PluginRegistry;
24 use DAORegistry;
25 
27 
31  public function __construct() {
32  \HookRegistry::register('Publication::getProperties', [$this, 'getPublicationProperties']);
33  \HookRegistry::register('Publication::validate', [$this, 'validatePublication']);
34  \HookRegistry::register('Publication::validatePublish', [$this, 'validatePublishPublication']);
35  \HookRegistry::register('Publication::add', [$this, 'addPublication']);
36  \HookRegistry::register('Publication::version', [$this, 'versionPublication']);
37  \HookRegistry::register('Publication::publish::before', [$this, 'publishPublicationBefore']);
38  \HookRegistry::register('Publication::delete::before', [$this, 'deletePublicationBefore']);
39  }
40 
52  public function getPublicationProperties($hookName, $args) {
53  $values =& $args[0];
54  $publication = $args[1];
55  $props = $args[2];
56  $dependencies = $args[3];
57  $request = $dependencies['request'];
58  $dispatcher = $request->getDispatcher();
59 
60  // Get required submission and context
61  $submission = !empty($args['submission'])
62  ? $args['submission']
63  : $args['submission'] = Services::get('submission')->get($publication->getData('submissionId'));
64 
65  $submissionContext = !empty($dependencies['context'])
66  ? $dependencies['context']
67  : $dependencies['context'] = Services::get('context')->get($submission->getData('contextId'));
68 
69  foreach ($props as $prop) {
70  switch ($prop) {
71  case 'galleys':
72  $values[$prop] = array_map(
73  function($galley) use ($dependencies) {
74  return Services::get('galley')->getSummaryProperties($galley, $dependencies);
75  },
76  $publication->getData('galleys')
77  );
78  break;
79  case 'urlPublished':
80  $values[$prop] = $dispatcher->url(
81  $request,
82  ROUTE_PAGE,
83  $submissionContext->getData('urlPath'),
84  'article',
85  'view',
86  [$submission->getBestId(), 'version', $publication->getId()]
87  );
88  break;
89  }
90  }
91  }
92 
105  public function validatePublication($hookName, $args) {
106  $errors =& $args[0];
107  $action = $args[1];
108  $props = $args[2];
109  $allowedLocales = $args[3];
110  $primaryLocale = $args[4];
111 
112  // Ensure that the specified section exists
113  $section = null;
114  if (isset($props['sectionId'])) {
115  $section = Application::get()->getSectionDAO()->getById($props['sectionId']);
116  if (!$section) {
117  $errors['sectionId'] = [__('publication.invalidSection')];
118  }
119  }
120 
121  // Get the section so we can validate section abstract requirements
122  if (!$section && isset($props['id'])) {
123  $publication = Services::get('publication')->get($props['id']);
124  $sectionDao = DAORegistry::getDAO('SectionDAO'); /* @var $sectionDao SectionDAO */
125  $section = $sectionDao->getById($publication->getData('sectionId'));
126  }
127 
128  if ($section) {
129 
130  // Require abstracts if the section requires them
131  if ($action === VALIDATE_ACTION_ADD && !$section->getData('abstractsNotRequired') && empty($props['abstract'])) {
132  $errors['abstract'][$primaryLocale] = [__('author.submit.form.abstractRequired')];
133  }
134 
135  if (isset($props['abstract']) && empty($errors['abstract'])) {
136 
137  // Require abstracts in the primary language if the section requires them
138  if (!$section->getData('abstractsNotRequired')) {
139  if (empty($props['abstract'][$primaryLocale])) {
140  if (!isset($errors['abstract'])) {
141  $errors['abstract'] = [];
142  };
143  AppLocale::requireComponents(LOCALE_COMPONENT_APP_AUTHOR);
144  $errors['abstract'][$primaryLocale] = [__('author.submit.form.abstractRequired')];
145  }
146  }
147 
148  // Check the word count on abstracts
149  foreach ($allowedLocales as $localeKey) {
150  if (empty($props['abstract'][$localeKey])) {
151  continue;
152  }
153  $wordCount = count(preg_split('/\s+/', trim(str_replace('&nbsp;', ' ', strip_tags($props['abstract'][$localeKey])))));
154  $wordCountLimit = $section->getData('wordCount');
155  if ($wordCountLimit && $wordCount > $wordCountLimit) {
156  if (!isset($errors['abstract'])) {
157  $errors['abstract'] = [];
158  };
159  $errors['abstract'][$localeKey] = [__('publication.wordCountLong', ['limit' => $wordCountLimit, 'count' => $wordCount])];
160  }
161  }
162  }
163  }
164  }
165 
179  public function validatePublishPublication($hookName, $args) {
180  $errors =& $args[0];
181  $submission = $args[2];
182  if (!$this->canAuthorPublish($submission->getId())){
183  $errors['authorCheck'] = __('author.submit.authorsCanNotPublish');
184  }
185  }
186 
196  public function addPublication($hookName, $args) {
197  $publication = $args[0];
198  $request = $args[1];
199 
200  // Assign DOI if automatic assigment is enabled
201  $context = $request->getContext();
202  $pubIdPlugins = PluginRegistry::loadCategory('pubIds', true, $context->getId());
203  $doiPubIdPlugin = $pubIdPlugins['doipubidplugin'];
204  if ($doiPubIdPlugin && $doiPubIdPlugin->getSetting($context->getId(), 'enablePublicationDoiAutoAssign')){
205  $publication->setData('pub-id::doi', $doiPubIdPlugin->getPubId($publication));
206  }
207  }
208 
219  public function versionPublication($hookName, $args) {
220  $newPublication = $args[0];
221  $oldPublication = $args[1];
222  $request = $args[2];
223 
224  // Duplicate galleys
225  $galleys = $oldPublication->getData('galleys');
226  if (!empty($galleys)) {
227  foreach ($galleys as $galley) {
228  $newGalley = clone $galley;
229  $newGalley->setData('id', null);
230  $newGalley->setData('publicationId', $newPublication->getId());
231  Services::get('galley')->add($newGalley, $request);
232  }
233  }
234 
235  $newPublication->setData('galleys', $this->get($newPublication->getId())->getData('galleys'));
236 
237  // Version DOI if the pattern includes the publication id
238  $context = $request->getContext();
239  $pubIdPlugins = PluginRegistry::loadCategory('pubIds', true, $context->getId());
240  $doiPubIdPlugin = $pubIdPlugins['doipubidplugin'];
241  if ($doiPubIdPlugin){
242  $pattern = $doiPubIdPlugin->getSetting($context->getId(), 'doiPublicationSuffixPattern');
243  if (strpos($pattern, '%b')) {
244  $newPublication->setData('pub-id::doi', $doiPubIdPlugin->versionPubId($newPublication));
245  }
246  }
247 
248  }
249 
259  public function publishPublicationBefore($hookName, $args) {
260  $newPublication = $args[0];
261  $oldPublication = $args[1];
262 
263  // If the publish date is in the future, set the status to scheduled
264  $datePublished = $oldPublication->getData('datePublished');
265  if ($datePublished && strtotime($datePublished) > strtotime(\Core::getCurrentDate())) {
266  $newPublication->setData('status', STATUS_SCHEDULED);
267  }
268  }
269 
278  public function deletePublicationBefore($hookName, $args) {
279  $publication = $args[0];
280 
281  $galleysIterator = Services::get('galley')->getMany(['publicationIds' => $publication->getId()]);
282  foreach ($galleysIterator as $galley) {
283  Services::get('galley')->delete($galley);
284  }
285  }
286 
294  public function relate($publication, $params) {
295  $publication->setData('relationStatus', $params['relationStatus']);
296  $publication->setData('vorDoi', $params['vorDoi']);
297  DAORegistry::getDAO('PublicationDAO')->updateObject($publication);
298  return $publication;
299  }
300 
308  public function canAuthorPublish($submissionId) {
309 
310  // Check if current user is an author
311  $isAuthor = false;
312  $currentUser = Application::get()->getRequest()->getUser();
313  $stageAssignmentDao = DAORegistry::getDAO('StageAssignmentDAO'); /* @var $stageAssignmentDao StageAssignmentDAO */
314  $submitterAssignments = $stageAssignmentDao->getBySubmissionAndRoleId($submissionId, ROLE_ID_AUTHOR);
315  while ($assignment = $submitterAssignments->next()) {
316  if ($currentUser->getId() == $assignment->getUserId()) {
317  $isAuthor = true;
318  }
319  }
320 
321  // By default authors can not publish, but this can be overridden in screening plugins with the hook Publication::canAuthorPublish
322  if ($isAuthor) {
323  if (HookRegistry::call('Publication::canAuthorPublish', array($this))){
324  return true;
325  } else {
326  return false;
327  }
328  }
329 
330  // If the user is not an author, has to be an editor, return true
331  return true;
332  }
333 
340  public function getRelationOptions() {
341  return array(
342  array(
343  "value" => PUBLICATION_RELATION_NONE,
344  "label" => __('publication.relation.none')
345  ),
346  array(
347  "value" => PUBLICATION_RELATION_SUBMITTED,
348  "label" => __('publication.relation.submitted')
349  ),
350  array(
351  "value" => PUBLICATION_RELATION_PUBLISHED,
352  "label" => __('publication.relation.published')
353  )
354  );
355  }
356 }
DAORegistry
Maintains a static list of DAO objects so each DAO is instantiated only once.
Definition: DAORegistry.inc.php:20
AppLocale\requireComponents
static requireComponents()
Definition: env1/MockAppLocale.inc.php:56
APP\Services\PublicationService\validatePublishPublication
validatePublishPublication($hookName, $args)
Definition: PublicationService.inc.php:179
APP\Services\PublicationService
Definition: PublicationService.inc.php:26
APP\Services\PublicationService\versionPublication
versionPublication($hookName, $args)
Definition: PublicationService.inc.php:219
DAORegistry\getDAO
static & getDAO($name, $dbconn=null)
Definition: DAORegistry.inc.php:57
PluginRegistry\loadCategory
static loadCategory($category, $enabledOnly=false, $mainContextId=null)
Definition: PluginRegistry.inc.php:103
APP\Services\PublicationService\getRelationOptions
getRelationOptions()
Definition: PublicationService.inc.php:340
APP\Services\PublicationService\relate
relate($publication, $params)
Definition: PublicationService.inc.php:294
PKP\Services\PKPPublicationService
Definition: PKPPublicationService.inc.php:32
APP\Services\PublicationService\validatePublication
validatePublication($hookName, $args)
Definition: PublicationService.inc.php:105
APP\Services
Definition: ContextService.inc.php:15
Core\getCurrentDate
static getCurrentDate($ts=null)
Definition: Core.inc.php:63
APP\Services\PublicationService\addPublication
addPublication($hookName, $args)
Definition: PublicationService.inc.php:196
HookRegistry\register
static register($hookName, $callback, $hookSequence=HOOK_SEQUENCE_NORMAL)
Definition: HookRegistry.inc.php:70
APP\Services\PublicationService\deletePublicationBefore
deletePublicationBefore($hookName, $args)
Definition: PublicationService.inc.php:278
PKPApplication\get
static get()
Definition: PKPApplication.inc.php:235
HookRegistry\call
static call($hookName, $args=null)
Definition: HookRegistry.inc.php:86
APP\Services\PublicationService\getPublicationProperties
getPublicationProperties($hookName, $args)
Definition: PublicationService.inc.php:52
APP\Services\PublicationService\canAuthorPublish
canAuthorPublish($submissionId)
Definition: PublicationService.inc.php:308
APP\Services\PublicationService\__construct
__construct()
Definition: PublicationService.inc.php:31
PKPServices\get
static get($service)
Definition: PKPServices.inc.php:49
APP\Services\PublicationService\publishPublicationBefore
publishPublicationBefore($hookName, $args)
Definition: PublicationService.inc.php:259