15 import(
'lib.pkp.classes.handler.APIHandler');
25 $this->_handlerPath =
'site';
26 $roles = [ROLE_ID_SITE_ADMIN];
27 $this->_endpoints = array(
31 'handler' => array($this,
'get'),
36 'handler' => array($this,
'getTheme'),
43 'handler' => array($this,
'edit'),
48 'handler' => array($this,
'editTheme'),
53 parent::__construct();
59 public function authorize($request, &$args, $roleAssignments) {
60 import(
'lib.pkp.classes.security.authorization.PolicySet');
61 $rolePolicy =
new PolicySet(COMBINING_PERMIT_OVERRIDES);
63 import(
'lib.pkp.classes.security.authorization.RoleBasedHandlerOperationPolicy');
64 foreach($roleAssignments as $role => $operations) {
69 return parent::authorize($request, $args, $roleAssignments);
81 public function get($slimRequest, $response, $args) {
85 ->getFullProperties($request->getSite(), [
86 'request' => $request,
89 return $response->withJson($siteProps, 200);
101 public function getTheme($slimRequest, $response, $args) {
106 foreach ($allThemes as $theme) {
107 if ($site->getData(
'themePluginPath') === $theme->getDirName()) {
108 $activeTheme = $theme;
114 return $response->withStatus(404)->withJsonError(
'api.themes.404.themeUnavailable');
118 $activeTheme->getOptionValues(CONTEXT_ID_NONE),
119 [
'themePluginPath' => $theme->getDirName()]
124 return $response->withJson($data, 200);
136 public function edit($slimRequest, $response, $args) {
138 $site = $request->getSite();
143 $errors = $siteService->validate($params, $site->getSupportedLocales(), $site->getPrimaryLocale());
145 if (!empty($errors)) {
146 return $response->withStatus(400)->withJson($errors);
148 $site = $siteService->edit($site, $params, $request);
150 $siteProps = $siteService->getFullProperties($site, array(
151 'request' => $request,
152 'slimRequest' => $slimRequest
155 return $response->withJson($siteProps, 200);
167 public function editTheme($slimRequest, $response, $args) {
169 $site = $request->getSite();
172 $params = $slimRequest->getParsedBody();
175 $themePluginPath = empty($params[
'themePluginPath']) ? null : $params[
'themePluginPath'];
176 if ($themePluginPath !== $site->getData(
'themePluginPath')) {
177 $errors = $siteService->validate(
178 [
'themePluginPath' => $themePluginPath],
179 $site->getSupportedLocales(),
180 $site->getPrimaryLocale()
182 if (!empty($errors)) {
183 return $response->withJson($errors, 400);
185 $newSite = $siteService->edit($site, [
'themePluginPath' => $themePluginPath], $request);
190 $selectedTheme =
null;
191 foreach ($allThemes as $theme) {
192 if ($themePluginPath === $theme->getDirName()) {
193 $selectedTheme = $theme;
199 if (isset($newSite)) {
200 $selectedTheme->init();
203 $errors = $selectedTheme->validateOptions($params, $themePluginPath, CONTEXT_ID_NONE, $request);
204 if (!empty($errors)) {
205 return $response->withJson($errors, 400);
209 $options = $selectedTheme->getOptionsConfig();
210 foreach ($options as $optionName => $optionConfig) {
211 if (!array_key_exists($optionName, $params)) {
214 $selectedTheme->saveOption($optionName, $params[$optionName], CONTEXT_ID_NONE);
219 $templateMgr->clearTemplateCache();
220 $templateMgr->clearCssCache();
223 $selectedTheme->getOptionValues(CONTEXT_ID_NONE),
224 [
'themePluginPath' => $themePluginPath]
229 return $response->withJson($data, 200);