Open Monograph Press  3.3.0
ContextService.inc.php
1 <?php
15 namespace APP\Services;
16 
17 use \Services;
18 
21  var $contextsFileDirName = 'presses';
22 
26  public function __construct() {
27  $this->installFileDirs = array(
28  \Config::getVar('files', 'files_dir') . '/%s/%d',
29  \Config::getVar('files', 'files_dir'). '/%s/%d/monographs',
30  \Config::getVar('files', 'public_files_dir') . '/%s/%d',
31  );
32 
33  \HookRegistry::register('Context::edit', array($this, 'afterEditContext'));
34  \HookRegistry::register('Context::delete::before', array($this, 'beforeDeleteContext'));
35  \HookRegistry::register('Context::delete', array($this, 'afterDeleteContext'));
36  \HookRegistry::register('Context::validate', array($this, 'validateContext'));
37  }
38 
50  public function afterEditContext($hookName, $args) {
51  $newContext = $args[0];
52  $currentContext = $args[1];
53  $params = $args[2];
54  $request = $args[3];
55 
56  // If the context is enabled or disabled, create or delete publication
57  // format tombstones for all published submissions
58  if ($newContext->getData('enabled') !== $currentContext->getData('enabled')) {
59  import('classes.publicationFormat.PublicationFormatTombstoneManager');
60  $publicationFormatTombstoneMgr = new \PublicationFormatTombstoneManager();
61  if ($newContext->getData('enabled')) {
62  $publicationFormatTombstoneMgr->deleteTombstonesByPressId($newContext->getId());
63  } else {
64  $publicationFormatTombstoneMgr->insertTombstonesByPress($newContext);
65  }
66  }
67 
68  // If the cover image sizes have been changed, resize existing images
69  if (($newContext->getData('coverThumbnailsMaxWidth') !== $currentContext->getData('coverThumbnailsMaxWidth'))
70  || ($newContext->getData('coverThumbnailsMaxHeight') !== $currentContext->getData('coverThumbnailsMaxHeight'))) {
71  $this->resizeCoverThumbnails($newContext, $newContext->getData('coverThumbnailsMaxWidth'), $newContext->getData('coverThumbnailsMaxHeight'));
72  }
73 
74  // Move an uploaded press thumbnail and set the updated data
75  if (!empty($params['pressThumbnail'])) {
76  $supportedLocales = $newContext->getSupportedFormLocales();
77  foreach ($supportedLocales as $localeKey) {
78  if (!array_key_exists($localeKey, $params['pressThumbnail'])) {
79  continue;
80  }
81  $localeValue = $this->_saveFileParam(
82  $newContext,
83  $params['pressThumbnail'][$localeKey],
84  'pressThumbnail',
85  $request->getUser()->getId(),
86  $localeKey,
87  true
88  );
89  $newContext->setData('pressThumbnail', $localeValue, $localeKey);
90  }
91  }
92  }
93 
107  public function beforeDeleteContext($hookName, $args) {
108  $context = $args[0];
109 
110  // Create publication format tombstones for all published submissions
111  import('classes.publicationFormat.PublicationFormatTombstoneManager');
112  $publicationFormatTombstoneMgr = new \PublicationFormatTombstoneManager();
113  $publicationFormatTombstoneMgr->insertTombstonesByPress($context);
114  }
115 
125  public function afterDeleteContext($hookName, $args) {
126  $context = $args[0];
127 
128  $seriesDao = \DAORegistry::getDAO('SeriesDAO');
129  $seriesDao->deleteByPressId($context->getId());
130 
131  $submissionDao = \DAORegistry::getDAO('SubmissionDAO');
132  $submissionDao->deleteByContextId($context->getId());
133 
134  $featureDao = \DAORegistry::getDAO('FeatureDAO');
135  $featureDao->deleteByAssoc(ASSOC_TYPE_PRESS, $context->getId());
136 
137  $newReleaseDao = \DAORegistry::getDAO('NewReleaseDAO');
138  $newReleaseDao->deleteByAssoc(ASSOC_TYPE_PRESS, $context->getId());
139 
140  import('classes.file.PublicFileManager');
141  $publicFileManager = new \PublicFileManager();
142  $publicFileManager->rmtree($publicFileManager->getContextFilesPath($context->getId()));
143  }
144 
154  public function validateContext($hookName, $args) {
155  $errors =& $args[0];
156  $props = $args[2];
157 
158  if (!empty($props['codeType'])) {
159  if (!\DAORegistry::getDAO('ONIXCodelistItemDAO')->codeExistsInList($props['codeType'], 'List44')) {
160  $errors['codeType'] = [__('manager.settings.publisherCodeType.invalid')];
161  }
162  }
163  }
164 
175  public function resizeCoverThumbnails($context, $maxWidth, $maxHeight) {
176  import('lib.pkp.classes.file.FileManager');
177  import('classes.file.PublicFileManager');
178  import('lib.pkp.classes.file.ContextFileManager');
179  $fileManager = new \FileManager();
180  $publicFileManager = new \PublicFileManager();
181  $contextFileManager = new \ContextFileManager($context->getId());
182 
183  $objectDaos = [
184  \DAORegistry::getDAO('CategoryDAO'),
185  \DAORegistry::getDAO('SeriesDAO'),
186  \DAORegistry::getDAO('SubmissionDAO'),
187  ];
188  foreach ($objectDaos as $objectDao) {
189  $objects = $objectDao->getByContextId($context->getId());
190  while ($object = $objects->next()) {
191  if (is_a($object, 'Submission')) {
192  foreach ($object->getData('publications') as $publication) {
193  foreach ((array) $publication->getData('coverImage') as $coverImage) {
194  $coverImageFilePath = $publicFileManager->getContextFilesPath($context->getId()) . '/' . $coverImage['uploadName'];
195  Services::get('publication')->makeThumbnail(
196  $coverImageFilePath,
197  Services::get('publication')->getThumbnailFileName($coverImage['uploadName']),
198  $maxWidth,
199  $maxHeight
200  );
201  }
202  }
203  } else {
204  $cover = $object->getImage();
205  if (is_a($object, 'Series')) {
206  $basePath = $contextFileManager->getBasePath() . 'series/';
207  } elseif (is_a($object, 'Category')) {
208  $basePath = $contextFileManager->getBasePath() . 'categories/';
209  }
210  }
211  if ($cover) {
212  // delete old cover thumbnail
213  $fileManager->deleteByPath($basePath . $cover['thumbnailName']);
214 
215  // get settings necessary for the new thumbnail
216  $coverExtension = $fileManager->getExtension($cover['name']);
217  $xRatio = min(1, $maxWidth / $cover['width']);
218  $yRatio = min(1, $maxHeight / $cover['height']);
219  $ratio = min($xRatio, $yRatio);
220  $thumbnailWidth = round($ratio * $cover['width']);
221  $thumbnailHeight = round($ratio * $cover['height']);
222 
223  // create a thumbnail image of the defined size
224  $thumbnail = imagecreatetruecolor($thumbnailWidth, $thumbnailHeight);
225 
226  // generate the image of the original cover
227  switch ($coverExtension) {
228  case 'jpg': $coverImage = imagecreatefromjpeg($basePath . $cover['name']); break;
229  case 'png': $coverImage = imagecreatefrompng($basePath . $cover['name']); break;
230  case 'gif': $coverImage = imagecreatefromgif($basePath . $cover['name']); break;
231  default: $coverImage = null; // Suppress warn
232  }
233  assert($coverImage);
234 
235  // copy the cover image to the thumbnail
236  imagecopyresampled($thumbnail, $coverImage, 0, 0, 0, 0, $thumbnailWidth, $thumbnailHeight, $cover['width'], $cover['height']);
237 
238  // create the thumbnail file
239  switch ($coverExtension) {
240  case 'jpg': imagejpeg($thumbnail, $basePath . $cover['thumbnailName']); break;
241  case 'png': imagepng($thumbnail, $basePath . $cover['thumbnailName']); break;
242  case 'gif': imagegif($thumbnail, $basePath . $cover['thumbnailName']); break;
243  }
244 
245  imagedestroy($thumbnail);
246  if (is_a($object, 'Submission')) {
247  $object->setCoverImage(array(
248  'name' => $cover['name'],
249  'width' => $cover['width'],
250  'height' => $cover['height'],
251  'thumbnailName' => $cover['thumbnailName'],
252  'thumbnailWidth' => $thumbnailWidth,
253  'thumbnailHeight' => $thumbnailHeight,
254  'uploadName' => $cover['uploadName'],
255  'dateUploaded' => $cover['dateUploaded'],
256  ));
257  } else {
258  $object->setImage(array(
259  'name' => $cover['name'],
260  'width' => $cover['width'],
261  'height' => $cover['height'],
262  'thumbnailName' => $cover['thumbnailName'],
263  'thumbnailWidth' => $thumbnailWidth,
264  'thumbnailHeight' => $thumbnailHeight,
265  'uploadName' => $cover['uploadName'],
266  'dateUploaded' => $cover['dateUploaded'],
267  ));
268  }
269  // Update category object to store new thumbnail information.
270  $objectDao->updateObject($object);
271  }
272  unset($object);
273  }
274  }
275  }
276 }
APP\Services\ContextService\afterDeleteContext
afterDeleteContext($hookName, $args)
Definition: ContextService.inc.php:125
APP\Services\ContextService\beforeDeleteContext
beforeDeleteContext($hookName, $args)
Definition: ContextService.inc.php:107
PKP\Services\PKPContextService\_saveFileParam
_saveFileParam($context, $value, $settingName, $userId, $localeKey='', $isImage=false)
Definition: PKPContextService.inc.php:614
DAORegistry\getDAO
static & getDAO($name, $dbconn=null)
Definition: DAORegistry.inc.php:57
PKP\Services\PKPContextService
Definition: PKPContextService.inc.php:30
Config\getVar
static getVar($section, $key, $default=null)
Definition: Config.inc.php:35
APP\Services\ContextService\$contextsFileDirName
$contextsFileDirName
Definition: ContextService.inc.php:21
APP\Services
Definition: ContextService.inc.php:15
APP\Services\ContextService
Definition: ContextService.inc.php:19
APP\Services\ContextService\__construct
__construct()
Definition: ContextService.inc.php:26
APP\Services\ContextService\afterEditContext
afterEditContext($hookName, $args)
Definition: ContextService.inc.php:50
HookRegistry\register
static register($hookName, $callback, $hookSequence=HOOK_SEQUENCE_NORMAL)
Definition: HookRegistry.inc.php:70
APP\Services\ContextService\resizeCoverThumbnails
resizeCoverThumbnails($context, $maxWidth, $maxHeight)
Definition: ContextService.inc.php:175
APP\Services\ContextService\validateContext
validateContext($hookName, $args)
Definition: ContextService.inc.php:154
PKPServices\get
static get($service)
Definition: PKPServices.inc.php:49