Open Journal Systems  3.3.0
GalleyService.inc.php
1 <?php
2 
16 namespace APP\Services;
17 
18 use \DBResultRange;
19 use \DAOResultFactory;
20 use \DAORegistry;
21 use \Services;
22 use \PKP\Services\interfaces\EntityPropertyInterface;
23 use \PKP\Services\interfaces\EntityReadInterface;
24 use \PKP\Services\interfaces\EntityWriteInterface;
25 use \APP\Services\QueryBuilders\GalleyQueryBuilder;
26 
27 class GalleyService implements EntityReadInterface, EntityWriteInterface, EntityPropertyInterface {
28 
32  public function get($galleyId) {
33  $articleGalleyDao = DAORegistry::getDAO('ArticleGalleyDAO'); /* @var $articleGalleyDao ArticleGalleyDAO */
34  return $articleGalleyDao->getById($galleyId);
35  }
36 
40  public function getCount($args = []) {
41  return $this->getQueryBuilder($args)->getCount();
42  }
43 
47  public function getIds($args = []) {
48  return $this->getQueryBuilder($args)->getIds();
49  }
50 
60  public function getMany($args = []) {
61  $galleyQO = $this->getQueryBuilder($args)->getQuery();
62  $galleyDao = DAORegistry::getDAO('ArticleGalleyDAO'); /* @var $galleyDao ArticleGalleyDAO */
63  $result = $galleyDao->retrieveRange($galleyQO->toSql(), $galleyQO->getBindings());
64  $queryResults = new DAOResultFactory($result, $galleyDao, '_fromRow');
65 
66  return $queryResults->toIterator();
67  }
68 
72  public function getMax($args = []) {
73  // Count/offset is not supported so getMax is always
74  // the same as getCount
75  return $this->getCount();
76  }
77 
82  public function getQueryBuilder($args = []) {
83  $galleyQB = new GalleyQueryBuilder();
84  if (!empty($args['publicationIds'])) {
85  $galleyQB->filterByPublicationIds($args['publicationIds']);
86  }
87 
88  \HookRegistry::call('Galley::getMany::queryBuilder', array($galleyQB, $args));
89 
90  return $galleyQB;
91  }
92 
96  public function getProperties($galley, $props, $args = null) {
97  $request = $args['request'];
98  $context = $request->getContext();
99  $dispatcher = $request->getDispatcher();
100 
101  if (is_a($galley, 'ArticleGalley')) {
102  $publication = !empty($args['publication'])
103  ? $args['publication']
104  : $args['publication'] = Services::get('publication')->get($galley->getData('publicationId'));
105 
106  $submission = !empty($args['submission'])
107  ? $args['submission']
108  : $args['submission'] = Services::get('submission')->get($publication->getData('submissionId'));
109  }
110 
111 
112  $values = [];
113 
114  foreach ($props as $prop) {
115  switch ($prop) {
116  case 'urlPublished':
117  if (is_a($galley, 'IssueGalley')) {
118  $values[$prop] = $dispatcher->url(
119  $request,
120  ROUTE_PAGE,
121  $context->getPath(),
122  'issue',
123  'view',
124  [
125  $galley->getIssueId(),
126  $galley->getId()
127  ]
128  );
129  } else {
130  $values[$prop] = $dispatcher->url(
131  $request,
132  ROUTE_PAGE,
133  $context->getPath(),
134  'article',
135  'view',
136  [
137  $submission->getBestId(),
138  'version',
139  $publication->getId(),
140  $galley->getBestGalleyId(),
141  ]
142  );
143  }
144  break;
145  case 'file':
146  $values[$prop] = null;
147  $file = $galley->getFile();
148  if (!$file) {
149  break;
150  }
151  $values[$prop] = array(
152  'fileName' => $file->getOriginalFileName(),
153  );
154  if (is_a($file, 'SubmissionFile')) {
155  $values[$prop]['id'] = $file->getFileId();
156  $values[$prop]['revision'] = $file->getRevision();
157  $values[$prop]['fileStage'] = $file->getFileStage();
158  $values[$prop]['genreId'] = $file->getGenreId();
159  $values[$prop]['fileName'] = $file->getClientFileName();
160  } elseif (is_a($file, 'IssueFile')) {
161  $values[$prop]['id'] = $file->getId();
162  }
163  if (is_a($file, 'SupplementaryFile')) {
164  $values[$prop]['metadata'] = array(
165  'description' => $file->getDescription(null),
166  'creator' => $file->getCreator(null),
167  'publisher' => $file->getPublisher(null),
168  'source' => $file->getSource(null),
169  'subject' => $file->getSubject(null),
170  'sponsor' => $file->getSponsor(null),
171  'dateCreated' => $file->getDateCreated(),
172  'language' => $file->getLanguage(),
173  );
174  } elseif (is_a($file, 'SubmissionArtworkFile')) {
175  $values[$prop]['metadata'] = array(
176  'caption' => $file->getCaption(),
177  'credit' => $file->getCredit(),
178  'copyrightOwner' => $file->getCopyrightOwner(),
179  'terms' => $file->getPermissionTerms(),
180  'width' => $file->getWidth(),
181  'height' => $file->getHeight(),
182  'physicalWidth' => $file->getPhysicalWidth(300),
183  'physicalHeight' => $file->getPhysicalHeight(300),
184  );
185  }
186 
187  // Look for dependent files
188  if (is_a($file, 'SubmissionFile')) {
189  $values['dependentFiles'] = [];
190  $submissionFileDao = \DAORegistry::getDAO('SubmissionFileDAO'); /* @var $submissionFileDao SubmissionFileDAO */
191  $dependentFiles = $submissionFileDao->getLatestRevisionsByAssocId(ASSOC_TYPE_SUBMISSION_FILE, $file->getFileId(), $submission->getId(), SUBMISSION_FILE_DEPENDENT);
192  if ($dependentFiles) {
193  foreach ($dependentFiles as $dependentFile) {
194  $dependentFileProps = array(
195  'id' => $dependentFile->getFileId(),
196  'fileName' => $dependentFile->getOriginalFileName(),
197  );
198  if (is_a($dependentFile, 'SubmissionFile')) {
199  $dependentFileProps['revision'] = $dependentFile->getRevision();
200  $dependentFileProps['fileStage'] = $dependentFile->getFileStage();
201  $dependentFileProps['genreId'] = $dependentFile->getGenreId();
202  $dependentFileProps['fileName'] = $dependentFile->getClientFileName();
203  }
204  if (is_a($dependentFile, 'SupplementaryFile')) {
205  $dependentFileProps['description'] = $dependentFile->getDescription(null);
206  $dependentFileProps['creator'] = $dependentFile->getCreator(null);
207  $dependentFileProps['publisher'] = $dependentFile->getPublisher(null);
208  $dependentFileProps['source'] = $dependentFile->getSource(null);
209  $dependentFileProps['subject'] = $dependentFile->getSubject(null);
210  $dependentFileProps['sponsor'] = $dependentFile->getSponsor(null);
211  $dependentFileProps['dateCreated'] = $dependentFile->getDateCreated();
212  $dependentFileProps['language'] = $dependentFile->getLanguage();
213  } elseif (is_a($dependentFile, 'SubmissionArtworkFile')) {
214  $dependentFileProps['caption'] = $dependentFile->getCaption();
215  $dependentFileProps['credit'] = $dependentFile->getCredit();
216  $dependentFileProps['copyrightOwner'] = $dependentFile->getCopyrightOwner();
217  $dependentFileProps['terms'] = $dependentFile->getPermissionTerms();
218  $dependentFileProps['width'] = $dependentFile->getWidth();
219  $dependentFileProps['height'] = $dependentFile->getHeight();
220  $dependentFileProps['physicalWidth'] = $dependentFile->getPhysicalWidth(300);
221  $dependentFileProps['physicalHeight'] = $dependentFile->getPhysicalHeight(300);
222  }
223  $values['dependentFiles'][] = $dependentFileProps;
224  }
225  }
226  }
227  break;
228  default:
229  $values[$prop] = $galley->getData($prop);
230  break;
231  }
232  }
233 
234  $values = Services::get('schema')->addMissingMultilingualValues(SCHEMA_GALLEY, $values, $context->getSupportedSubmissionLocales());
235 
236  \HookRegistry::call('Galley::getProperties::values', array(&$values, $galley, $props, $args));
237 
238  ksort($values);
239 
240  return $values;
241  }
242 
246  public function getSummaryProperties($galley, $args = null) {
247  $props = Services::get('schema')->getSummaryProps(SCHEMA_GALLEY);
248 
249  return $this->getProperties($galley, $props, $args);
250  }
251 
255  public function getFullProperties($galley, $args = null) {
256  $props = Services::get('schema')->getFullProps(SCHEMA_GALLEY);
257 
258  return $this->getProperties($galley, $props, $args);
259  }
260 
264  public function validate($action, $props, $allowedLocales, $primaryLocale) {
265  $schemaService = Services::get('schema');
266 
267  import('lib.pkp.classes.validation.ValidatorFactory');
268  $validator = \ValidatorFactory::make(
269  $props,
270  $schemaService->getValidationRules(SCHEMA_GALLEY, $allowedLocales),
271  [
272  'locale.regex' => __('validator.localeKey'),
273  'urlPath.regex' => __('validator.alpha_dash'),
274  ]
275  );
276 
277  // Check required fields
279  $validator,
280  $action,
281  $schemaService->getRequiredProps(SCHEMA_GALLEY),
282  $schemaService->getMultilingualProps(SCHEMA_GALLEY),
283  $allowedLocales,
284  $primaryLocale
285  );
286 
287  // Check for input from disallowed locales
288  \ValidatorFactory::allowedLocales($validator, $schemaService->getMultilingualProps(SCHEMA_GALLEY), $allowedLocales);
289 
290  // The publicationId must match an existing publication that is not yet published
291  $validator->after(function($validator) use ($props) {
292  if (isset($props['publicationId']) && !$validator->errors()->get('publicationId')) {
293  $publication = Services::get('publication')->get($props['publicationId']);
294  if (!$publication) {
295  $validator->errors()->add('publicationId', __('galley.publicationNotFound'));
296  } else if (Services::get('publication')->isPublished($publication)) {
297  $validator->errors()->add('publicationId', __('galley.editPublishedDisabled'));
298  }
299  }
300  });
301 
302  if ($validator->fails()) {
303  $errors = $schemaService->formatValidationErrors($validator->errors(), $schemaService->get(SCHEMA_GALLEY), $allowedLocales);
304  }
305 
306  \HookRegistry::call('Galley::validate', array(&$errors, $action, $props, $allowedLocales, $primaryLocale));
307 
308  return $errors;
309  }
310 
314  public function add($galley, $request) {
315  $articleGalleyDao = DAORegistry::getDAO('ArticleGalleyDAO'); /* @var $articleGalleyDao ArticleGalleyDAO */
316  $galleyId = $articleGalleyDao->insertObject($galley);
317  $galley = $this->get($galleyId);
318 
319  \HookRegistry::call('Galley::add', array($galley, $request));
320 
321  return $galley;
322  }
323 
327  public function edit($galley, $params, $request) {
328  $galleyDao = DAORegistry::getDAO('ArticleGalleyDAO'); /* @var $galleyDao ArticleGalleyDAO */
329 
330  $newGalley = $galleyDao->newDataObject();
331  $newGalley->_data = array_merge($galley->_data, $params);
332 
333  \HookRegistry::call('Galley::edit', array($newGalley, $galley, $params, $request));
334 
335  $galleyDao->updateObject($newGalley);
336  $newGalley = $this->get($newGalley->getId());
337 
338  return $newGalley;
339  }
340 
344  public function delete($galley) {
345  \HookRegistry::call('Galley::delete::before', [$galley]);
346 
347  $articleGalleyDao = DAORegistry::getDAO('ArticleGalleyDAO'); /* @var $articleGalleyDao ArticleGalleyDAO */
348  $articleGalleyDao->deleteObject($galley);
349 
350  // Delete related submission files
351  $publication = Services::get('publication')->get($galley->getData('publicationId'));
352 
353  $submissionFileDao = DAORegistry::getDAO('SubmissionFileDAO'); /* @var $submissionFileDao SubmissionFileDAO */
354  import('lib.pkp.classes.submission.SubmissionFile'); // Import constants
355  $galleyFiles = $submissionFileDao->getLatestRevisionsByAssocId(ASSOC_TYPE_GALLEY, $galley->getId(), $publication->getData('submissionId'), SUBMISSION_FILE_PROOF);
356  foreach ($galleyFiles as $file) {
357  // delete dependent files for each galley file
358  $submissionFileDao->deleteAllRevisionsByAssocId(ASSOC_TYPE_SUBMISSION_FILE, $file->getFileId(), SUBMISSION_FILE_DEPENDENT);
359  }
360  // delete the galley files.
361  $submissionFileDao->deleteAllRevisionsByAssocId(ASSOC_TYPE_GALLEY, $galley->getId(), SUBMISSION_FILE_PROOF);
362 
363  \HookRegistry::call('Galley::delete', [$galley]);
364  }
365 }
APP\Services\GalleyService\validate
validate($action, $props, $allowedLocales, $primaryLocale)
Definition: GalleyService.inc.php:264
DAOResultFactory
Wrapper around ADORecordSet providing "factory" features for generating objects from DAOs.
Definition: DAOResultFactory.inc.php:21
DAORegistry\getDAO
static & getDAO($name, $dbconn=null)
Definition: DAORegistry.inc.php:57
APP\Services\GalleyService\add
add($galley, $request)
Definition: GalleyService.inc.php:314
ValidatorFactory\allowedLocales
static allowedLocales($validator, $multilingualProps, $allowedLocales)
Definition: ValidatorFactory.inc.php:320
ValidatorFactory\required
static required($validator, $action, $requiredProps, $multilingualProps, $allowedLocales, $primaryLocale)
Definition: ValidatorFactory.inc.php:261
APP\Services\GalleyService\getCount
getCount($args=[])
Definition: GalleyService.inc.php:40
ValidatorFactory\make
static make($props, $rules, $messages=[])
Definition: ValidatorFactory.inc.php:38
APP\Services\GalleyService\getSummaryProperties
getSummaryProperties($galley, $args=null)
Definition: GalleyService.inc.php:246
APP\Services\GalleyService\getMax
getMax($args=[])
Definition: GalleyService.inc.php:72
APP\Services\GalleyService\getFullProperties
getFullProperties($galley, $args=null)
Definition: GalleyService.inc.php:255
APP\Services\GalleyService\edit
edit($galley, $params, $request)
Definition: GalleyService.inc.php:327
APP\Services
Definition: ContextService.inc.php:15
APP\Services\GalleyService\getIds
getIds($args=[])
Definition: GalleyService.inc.php:47
APP\Services\GalleyService
Definition: GalleyService.inc.php:27
APP\Services\QueryBuilders\GalleyQueryBuilder
Definition: GalleyQueryBuilder.inc.php:20
APP\Services\GalleyService\getProperties
getProperties($galley, $props, $args=null)
Definition: GalleyService.inc.php:96
HookRegistry\call
static call($hookName, $args=null)
Definition: HookRegistry.inc.php:86
APP\Services\GalleyService\getMany
getMany($args=[])
Definition: GalleyService.inc.php:60
APP\Services\GalleyService\getQueryBuilder
getQueryBuilder($args=[])
Definition: GalleyService.inc.php:82
PKPServices\get
static get($service)
Definition: PKPServices.inc.php:49