Open Journal 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 DAORegistry;
23 
25 
29  public function __construct() {
30  \HookRegistry::register('Publication::getProperties', [$this, 'getPublicationProperties']);
31  \HookRegistry::register('Publication::validate', [$this, 'validatePublication']);
32  \HookRegistry::register('Publication::validatePublish', [$this, 'validatePublishPublication']);
33  \HookRegistry::register('Publication::version', [$this, 'versionPublication']);
34  \HookRegistry::register('Publication::publish::before', [$this, 'publishPublicationBefore']);
35  \HookRegistry::register('Publication::delete::before', [$this, 'deletePublicationBefore']);
36  }
37 
49  public function getPublicationProperties($hookName, $args) {
50  $values =& $args[0];
51  $publication = $args[1];
52  $props = $args[2];
53  $dependencies = $args[3];
54  $request = $dependencies['request'];
55  $dispatcher = $request->getDispatcher();
56 
57  // Get required submission and context
58  $submission = !empty($args['submission'])
59  ? $args['submission']
60  : $args['submission'] = Services::get('submission')->get($publication->getData('submissionId'));
61 
62  $submissionContext = !empty($dependencies['context'])
63  ? $dependencies['context']
64  : $dependencies['context'] = Services::get('context')->get($submission->getData('contextId'));
65 
66  foreach ($props as $prop) {
67  switch ($prop) {
68  case 'galleys':
69  $values[$prop] = array_map(
70  function($galley) use ($dependencies) {
71  return Services::get('galley')->getSummaryProperties($galley, $dependencies);
72  },
73  $publication->getData('galleys')
74  );
75  break;
76  case 'urlPublished':
77  $values[$prop] = $dispatcher->url(
78  $request,
79  ROUTE_PAGE,
80  $submissionContext->getData('urlPath'),
81  'article',
82  'view',
83  [$submission->getBestId(), 'version', $publication->getId()]
84  );
85  break;
86  }
87  }
88  }
89 
102  public function validatePublication($hookName, $args) {
103  $errors =& $args[0];
104  $action = $args[1];
105  $props = $args[2];
106  $allowedLocales = $args[3];
107  $primaryLocale = $args[4];
108 
109  // Ensure that the specified section exists
110  $section = null;
111  if (isset($props['sectionId'])) {
112  $section = Application::get()->getSectionDAO()->getById($props['sectionId']);
113  if (!$section) {
114  $errors['sectionId'] = [__('publication.invalidSection')];
115  }
116  }
117 
118  // Get the section so we can validate section abstract requirements
119  if (!$section && isset($props['id'])) {
120  $publication = Services::get('publication')->get($props['id']);
121  $sectionDao = DAORegistry::getDAO('SectionDAO'); /* @var $sectionDao SectionDAO */
122  $section = $sectionDao->getById($publication->getData('sectionId'));
123  }
124 
125  if ($section) {
126 
127  // Require abstracts if the section requires them
128  if ($action === VALIDATE_ACTION_ADD && !$section->getData('abstractsNotRequired') && empty($props['abstract'])) {
129  $errors['abstract'][$primaryLocale] = [__('author.submit.form.abstractRequired')];
130  }
131 
132  if (isset($props['abstract']) && empty($errors['abstract'])) {
133 
134  // Require abstracts in the primary language if the section requires them
135  if (!$section->getData('abstractsNotRequired')) {
136  if (empty($props['abstract'][$primaryLocale])) {
137  if (!isset($errors['abstract'])) {
138  $errors['abstract'] = [];
139  };
140  AppLocale::requireComponents(LOCALE_COMPONENT_APP_AUTHOR);
141  $errors['abstract'][$primaryLocale] = [__('author.submit.form.abstractRequired')];
142  }
143  }
144 
145  // Check the word count on abstracts
146  foreach ($allowedLocales as $localeKey) {
147  if (empty($props['abstract'][$localeKey])) {
148  continue;
149  }
150  $wordCount = count(preg_split('/\s+/', trim(str_replace('&nbsp;', ' ', strip_tags($props['abstract'][$localeKey])))));
151  $wordCountLimit = $section->getData('wordCount');
152  if ($wordCountLimit && $wordCount > $wordCountLimit) {
153  if (!isset($errors['abstract'])) {
154  $errors['abstract'] = [];
155  };
156  $errors['abstract'][$localeKey] = [__('publication.wordCountLong', ['limit' => $wordCountLimit, 'count' => $wordCount])];
157  }
158  }
159  }
160  }
161 
162  // Ensure that the issueId exists
163  if (isset($props['issueId']) && empty($errors['issueId'])) {
164  $issue = Services::get('issue')->get($props['issueId']);
165  if (!$issue) {
166  $errors['issueId'] = [__('publication.invalidIssue')];
167  }
168  }
169  }
170 
184  public function validatePublishPublication($hookName, $args) {
185  $errors =& $args[0];
186  $publication = $args[1];
187 
188  // Every publication must be scheduled in an issue
189  if (!$publication->getData('issueId') || !Services::get('issue')->get($publication->getData('issueId'))) {
190  $errors['issueId'] = __('publication.required.issue');
191  }
192  }
193 
204  public function versionPublication($hookName, $args) {
205  $newPublication = $args[0];
206  $oldPublication = $args[1];
207  $request = $args[2];
208 
209  $galleys = $oldPublication->getData('galleys');
210  if (!empty($galleys)) {
211  foreach ($galleys as $galley) {
212  $newGalley = clone $galley;
213  $newGalley->setData('id', null);
214  $newGalley->setData('publicationId', $newPublication->getId());
215  Services::get('galley')->add($newGalley, $request);
216  }
217  }
218 
219  $newPublication->setData('galleys', $this->get($newPublication->getId())->getData('galleys'));
220  }
221 
231  public function publishPublicationBefore($hookName, $args) {
232  $newPublication = $args[0];
233  $oldPublication = $args[1];
234 
235  // In OJS, a publication may be scheduled in a future issue. In such cases,
236  // the datePublished should remain empty and the status should be set to
237  // scheduled.
238  $issue = Services::get('issue')->get($newPublication->getData('issueId'));
239  if ($issue && !$issue->getData('published')) {
240  $newPublication->setData('datePublished', '');
241  $newPublication->setData('status', STATUS_SCHEDULED);
242  }
243  }
244 
253  public function deletePublicationBefore($hookName, $args) {
254  $publication = $args[0];
255 
256  $galleysIterator = Services::get('galley')->getMany(['publicationIds' => $publication->getId()]);
257  foreach ($galleysIterator as $galley) {
258  Services::get('galley')->delete($galley);
259  }
260  }
261 }
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:184
APP\Services\PublicationService
Definition: PublicationService.inc.php:24
APP\Services\PublicationService\versionPublication
versionPublication($hookName, $args)
Definition: PublicationService.inc.php:204
DAORegistry\getDAO
static & getDAO($name, $dbconn=null)
Definition: DAORegistry.inc.php:57
PKP\Services\PKPPublicationService
Definition: PKPPublicationService.inc.php:32
APP\Services\PublicationService\validatePublication
validatePublication($hookName, $args)
Definition: PublicationService.inc.php:102
APP\Services
Definition: ContextService.inc.php:15
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:253
PKPApplication\get
static get()
Definition: PKPApplication.inc.php:235
APP\Services\PublicationService\getPublicationProperties
getPublicationProperties($hookName, $args)
Definition: PublicationService.inc.php:49
APP\Services\PublicationService\__construct
__construct()
Definition: PublicationService.inc.php:29
PKPServices\get
static get($service)
Definition: PKPServices.inc.php:49
APP\Services\PublicationService\publishPublicationBefore
publishPublicationBefore($hookName, $args)
Definition: PublicationService.inc.php:231