Open Preprint Systems  3.3.0
ContextService.inc.php
1 <?php
15 namespace APP\Services;
16 
19  var $contextsFileDirName = 'journals';
20 
24  public function __construct() {
25  $this->installFileDirs = array(
26  \Config::getVar('files', 'files_dir') . '/%s/%d',
27  \Config::getVar('files', 'files_dir'). '/%s/%d/articles',
28  \Config::getVar('files', 'public_files_dir') . '/%s/%d',
29  );
30 
31  \HookRegistry::register('Context::add', array($this, 'afterAddContext'));
32  \HookRegistry::register('Context::edit', array($this, 'afterEditContext'));
33  \HookRegistry::register('Context::delete', array($this, 'afterDeleteContext'));
34  \HookRegistry::register('Context::validate', array($this, 'validateContext'));
35  }
36 
46  public function afterAddContext($hookName, $args) {
47  $context = $args[0];
48  $request = $args[1];
49 
50  // Create a default section
51  $sectionDao = \DAORegistry::getDAO('SectionDAO'); // constants
52  $section = new \Section();
53  $section->setTitle(__('section.default.title'), $context->getPrimaryLocale());
54  $section->setAbbrev(__('section.default.abbrev'), $context->getPrimaryLocale());
55  $section->setPath(__('section.default.path'), $context->getPrimaryLocale());
56  $section->setMetaIndexed(true);
57  $section->setMetaReviewed(true);
58  $section->setPolicy(__('section.default.policy'), $context->getPrimaryLocale());
59  $section->setEditorRestricted(false);
60  $section->setHideTitle(false);
61 
62  \Services::get('section')->addSection($section, $context);
63  }
64 
76  public function afterEditContext($hookName, $args) {
77  $newContext = $args[0];
78  $params = $args[2];
79  $request = $args[3];
80 
81  // Move an uploaded journal thumbnail and set the updated data
82  if (!empty($params['serverThumbnail'])) {
83  $supportedLocales = $newContext->getSupportedFormLocales();
84  foreach ($supportedLocales as $localeKey) {
85  if (!array_key_exists($localeKey, $params['serverThumbnail'])) {
86  continue;
87  }
88  $localeValue = $this->_saveFileParam(
89  $newContext,
90  $params['serverThumbnail'][$localeKey],
91  'serverThumbnail',
92  $request->getUser()->getId(),
93  $localeKey,
94  true
95  );
96  $newContext->setData('serverThumbnail', $localeValue, $localeKey);
97  }
98  }
99  }
100 
110  public function afterDeleteContext($hookName, $args) {
111  $context = $args[0];
112 
113  $sectionDao = \DAORegistry::getDAO('SectionDAO');
114  $sectionDao->deleteByJournalId($context->getId());
115 
116  $submissionDao = \DAORegistry::getDAO('SubmissionDAO');
117  $submissionDao->deleteByContextId($context->getId());
118 
119  import('classes.file.PublicFileManager');
120  $publicFileManager = new \PublicFileManager();
121  $publicFileManager->rmtree($publicFileManager->getContextFilesPath($context->getId()));
122  }
123 
133  public function validateContext($hookName, $args) {
134  $errors =& $args[0];
135  $props = $args[2];
136  $allowedLocales = $args[3];
137 
138  if (!isset($props['serverThumbnail'])) {
139  return;
140  }
141 
142  // If a journal thumbnail is passed, check that the temporary file exists
143  // and the current user owns it
144  $user = \Application::get()->getRequest()->getUser();
145  $userId = $user ? $user->getId() : null;
146  import('lib.pkp.classes.file.TemporaryFileManager');
147  $temporaryFileManager = new \TemporaryFileManager();
148  if (isset($props['serverThumbnail']) && empty($errors['serverThumbnail'])) {
149  foreach ($allowedLocales as $localeKey) {
150  if (empty($props['serverThumbnail'][$localeKey]) || empty($props['serverThumbnail'][$localeKey]['temporaryFileId'])) {
151  continue;
152  }
153  if (!$temporaryFileManager->getFile($props['serverThumbnail'][$localeKey]['temporaryFileId'], $userId)) {
154  if (!is_array($errors['serverThumbnail'])) {
155  $errors['serverThumbnail'] = [];
156  }
157  $errors['serverThumbnail'][$localeKey] = [__('common.noTemporaryFile')];
158  }
159  }
160  }
161  }
162 }
APP\Services\ContextService\afterDeleteContext
afterDeleteContext($hookName, $args)
Definition: ContextService.inc.php:110
APP\Services\ContextService\afterAddContext
afterAddContext($hookName, $args)
Definition: ContextService.inc.php:46
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:19
APP\Services
Definition: ContextService.inc.php:15
APP\Services\ContextService
Definition: ContextService.inc.php:17
APP\Services\ContextService\__construct
__construct()
Definition: ContextService.inc.php:24
APP\Services\ContextService\afterEditContext
afterEditContext($hookName, $args)
Definition: ContextService.inc.php:76
HookRegistry\register
static register($hookName, $callback, $hookSequence=HOOK_SEQUENCE_NORMAL)
Definition: HookRegistry.inc.php:70
PKPApplication\get
static get()
Definition: PKPApplication.inc.php:235
APP\Services\ContextService\validateContext
validateContext($hookName, $args)
Definition: ContextService.inc.php:133
PKPServices\get
static get($service)
Definition: PKPServices.inc.php:49