Open Journal Systems  3.3.0
PKPSiteService.inc.php
1 <?php
15 namespace PKP\Services;
16 
17 use \Application;
18 use \DAORegistry;
19 use \Services;
20 use \PKP\Services\interfaces\EntityPropertyInterface;
21 
22 class PKPSiteService implements EntityPropertyInterface {
23 
27  public function getProperties($site, $props, $args = null) {
28  $request = $args['request'];
29  $router = $request->getRouter();
30  $dispatcher = $request->getDispatcher();
31 
32  $values = array();
33  foreach ($props as $prop) {
34  $values[$prop] = $site->getData($prop);
35  }
36 
37  $values = Services::get('schema')->addMissingMultilingualValues(SCHEMA_SITE, $values, $site->getSupportedLocales());
38 
39  \HookRegistry::call('Site::getProperties', array(&$values, $site, $props, $args));
40 
41  ksort($values);
42 
43  return $values;
44  }
45 
49  public function getSummaryProperties($site, $args = null) {
50  return $this->getFullProperties($site, $args);
51  }
52 
56  public function getFullProperties($site, $args = null) {
57  $props = Services::get('schema')->getFullProps(SCHEMA_SITE);
58 
59  return $this->getProperties($site, $props, $args);
60  }
61 
75  public function validate($props, $allowedLocales, $primaryLocale) {
77  LOCALE_COMPONENT_PKP_ADMIN,
78  LOCALE_COMPONENT_APP_ADMIN,
79  LOCALE_COMPONENT_PKP_MANAGER,
80  LOCALE_COMPONENT_APP_MANAGER
81  );
82  $schemaService = Services::get('schema');
83 
84  import('lib.pkp.classes.validation.ValidatorFactory');
85  $validator = \ValidatorFactory::make(
86  $props,
87  $schemaService->getValidationRules(SCHEMA_SITE, $allowedLocales),
88  [
89  'primaryLocale.regex' => __('validator.localeKey'),
90  'supportedLocales.regex' => __('validator.localeKey'),
91  ]
92  );
93 
94  // Check required fields
96  $validator,
97  VALIDATE_ACTION_EDIT,
98  $schemaService->getRequiredProps(SCHEMA_PUBLICATION),
99  $schemaService->getMultilingualProps(SCHEMA_PUBLICATION),
100  $allowedLocales,
101  $primaryLocale
102  );
103 
104  // Check for input from disallowed locales
106  $validator,
107  $schemaService->getMultilingualProps(SCHEMA_SITE),
108  $allowedLocales
109  );
110 
111  // If a new file has been uploaded, check that the temporary file exists and
112  // the current user owns it
113  $user = Application::get()->getRequest()->getUser();
115  $validator,
116  ['pageHeaderTitleImage', 'styleSheet'],
117  ['pageHeaderTitleImage'],
118  $props,
119  $allowedLocales,
120  $user ? $user->getId() : null
121  );
122 
123  // If sidebar blocks are passed, ensure the block plugin exists and is
124  // enabled
125  $validator->after(function($validator) use ($props) {
126  if (!empty($props['sidebar']) && !$validator->errors()->get('sidebar')) {
127  $plugins = \PluginRegistry::loadCategory('blocks', true);
128  foreach ($props['sidebar'] as $pluginName) {
129  if (empty($plugins[$pluginName])) {
130  $validator->errors()->add('sidebar', __('manager.setup.layout.sidebar.invalidBlock', ['name' => $pluginName]));
131  }
132  }
133  }
134  });
135 
136  // Ensure the theme plugin is installed and enabled
137  $validator->after(function($validator) use ($props) {
138  if (!empty($props['themePluginPath']) && !$validator->errors()->get('themePluginPath')) {
139  $plugins = \PluginRegistry::loadCategory('themes', true);
140  $found = false;
141  foreach ($plugins as $plugin) {
142  if ($props['themePluginPath'] === $plugin->getDirName()) {
143  $found = true;
144  break;
145  }
146  }
147  if (!$found) {
148  $validator->errors()->add('themePluginPath', __('manager.setup.theme.notFound'));
149  }
150  }
151  });
152 
153  if ($validator->fails()) {
154  $errors = $schemaService->formatValidationErrors($validator->errors(), $schemaService->get(SCHEMA_SITE), $allowedLocales);
155  }
156 
157  \HookRegistry::call('Site::validate', array(&$errors, $props, $allowedLocales, $primaryLocale));
158 
159  return $errors;
160  }
161 
173  public function edit($site, $params, $request) {
174  $siteDao = DAORegistry::getDAO('SiteDAO'); /* @var $siteDao SiteDAO */
175 
176  // Move uploaded files into place and update the params
177  $userId = $request->getUser() ? $request->getUser()->getId() : null;
178  $supportedLocales = $site->getSupportedLocales();
179  if (array_key_exists('pageHeaderTitleImage', $params)) {
180  foreach ($supportedLocales as $localeKey) {
181  if (!array_key_exists($localeKey, $params['pageHeaderTitleImage'])) {
182  continue;
183  }
184  $params['pageHeaderTitleImage'][$localeKey] = $this->_saveFileParam($site, $params['pageHeaderTitleImage'][$localeKey], 'pageHeaderTitleImage', $userId, $localeKey, true);
185  }
186  }
187  if (array_key_exists('styleSheet', $params)) {
188  $params['styleSheet'] = $this->_saveFileParam($site, $params['styleSheet'], 'styleSheet', $userId);
189  }
190 
191  $newSite = $siteDao->newDataObject();
192  $newSite->_data = array_merge($site->_data, $params);
193 
194  \HookRegistry::call('Site::edit', array($newSite, $site, $params, $request));
195 
196  $siteDao->updateObject($newSite);
197  $newSite = $siteDao->getSite();
198 
199  return $newSite;
200  }
201 
214  public function moveTemporaryFile($context, $temporaryFile, $fileNameBase, $userId, $localeKey = '') {
215  import('classes.file.PublicFileManager');
216  $publicFileManager = new \PublicFileManager();
217  import('lib.pkp.classes.file.TemporaryFileManager');
218  $temporaryFileManager = new \TemporaryFileManager();
219 
220  $fileName = $fileNameBase;
221  if ($localeKey) {
222  $fileName .= '_' . $localeKey;
223  }
224 
225  $extension = $publicFileManager->getDocumentExtension($temporaryFile->getFileType());
226  if (!$extension) {
227  $extension = $publicFileManager->getImageExtension($temporaryFile->getFileType());
228  }
229  $fileName .= $extension;
230 
231  if (!$publicFileManager->copyFile($temporaryFile->getFilePath(), $publicFileManager->getSiteFilesPath() . '/' . $fileName)) {
232  return false;
233  }
234 
235  $temporaryFileManager->deleteById($temporaryFile->getId(), $userId);
236 
237  return $fileName;
238  }
239 
261  protected function _saveFileParam($site, $value, $settingName, $userId, $localeKey = '', $isImage = false) {
262  import('lib.pkp.classes.file.TemporaryFileManager');
263  $temporaryFileManager = new \TemporaryFileManager();
264 
265  // If the value is null, clean up any existing file in the system
266  if (is_null($value)) {
267  $setting = $site->getData($settingName, $localeKey);
268  if ($setting) {
269  $fileName = $isImage ? $setting['uploadName'] : $setting;
270  import('classes.file.PublicFileManager');
271  $publicFileManager = new \PublicFileManager();
272  $publicFileManager->removeSiteFile($fileName);
273  }
274  return null;
275  }
276 
277  // Check if there is something to upload
278  if (empty($value['temporaryFileId'])) {
279  return $value;
280  }
281 
282  $temporaryFile = $temporaryFileManager->getFile((int) $value['temporaryFileId'], $userId);
283  $fileName = $this->moveTemporaryFile($site, $temporaryFile, $settingName, $userId, $localeKey);
284 
285  if ($fileName) {
286  // Get the details for image uploads
287  if ($isImage) {
288  import('classes.file.PublicFileManager');
289  $publicFileManager = new \PublicFileManager();
290 
291  list($width, $height) = getimagesize($publicFileManager->getSiteFilesPath() . '/' . $fileName);
292  $altText = !empty($value['altText']) ? $value['altText'] : '';
293 
294  return [
295  'originalFilename' => $temporaryFile->getOriginalFileName(),
296  'uploadName' => $fileName,
297  'width' => $width,
298  'height' => $height,
299  'dateUploaded' => \Core::getCurrentDate(),
300  'altText' => $altText,
301  ];
302  } else {
303  return [
304  'originalFilename' => $temporaryFile->getOriginalFileName(),
305  'uploadName' => $fileName,
306  'dateUploaded' => \Core::getCurrentDate(),
307  ];
308  }
309  }
310 
311  return false;
312  }
313 }
PKP\Services
AppLocale\requireComponents
static requireComponents()
Definition: env1/MockAppLocale.inc.php:56
PKP\Services\SCHEMA_SITE
const SCHEMA_SITE
Definition: PKPSchemaService.inc.php:27
ValidatorFactory\temporaryFilesExist
static temporaryFilesExist($validator, $uploadProps, $multilingualUploadProps, $props, $allowedLocales, $userId)
Definition: ValidatorFactory.inc.php:354
DAORegistry\getDAO
static & getDAO($name, $dbconn=null)
Definition: DAORegistry.inc.php:57
PKP\Services\PKPSiteService\validate
validate($props, $allowedLocales, $primaryLocale)
Definition: PKPSiteService.inc.php:75
PKP\Services\PKPSiteService\getSummaryProperties
getSummaryProperties($site, $args=null)
Definition: PKPSiteService.inc.php:49
PKP\Services\SCHEMA_PUBLICATION
const SCHEMA_PUBLICATION
Definition: PKPSchemaService.inc.php:23
ValidatorFactory\allowedLocales
static allowedLocales($validator, $multilingualProps, $allowedLocales)
Definition: ValidatorFactory.inc.php:320
PKP\Services\PKPSiteService\edit
edit($site, $params, $request)
Definition: PKPSiteService.inc.php:173
PKP\Services\PKPSiteService\getFullProperties
getFullProperties($site, $args=null)
Definition: PKPSiteService.inc.php:56
ValidatorFactory\required
static required($validator, $action, $requiredProps, $multilingualProps, $allowedLocales, $primaryLocale)
Definition: ValidatorFactory.inc.php:261
ValidatorFactory\make
static make($props, $rules, $messages=[])
Definition: ValidatorFactory.inc.php:38
PKP\Services\PKPSiteService\_saveFileParam
_saveFileParam($site, $value, $settingName, $userId, $localeKey='', $isImage=false)
Definition: PKPSiteService.inc.php:261
Core\getCurrentDate
static getCurrentDate($ts=null)
Definition: Core.inc.php:63
PKPApplication\get
static get()
Definition: PKPApplication.inc.php:235
HookRegistry\call
static call($hookName, $args=null)
Definition: HookRegistry.inc.php:86
PKP\Services\PKPSiteService\moveTemporaryFile
moveTemporaryFile($context, $temporaryFile, $fileNameBase, $userId, $localeKey='')
Definition: PKPSiteService.inc.php:214
PKP\Services\PKPSiteService\getProperties
getProperties($site, $props, $args=null)
Definition: PKPSiteService.inc.php:27
PKPServices\get
static get($service)
Definition: PKPServices.inc.php:49
PKP\Services\PKPSiteService
Definition: PKPSiteService.inc.php:22