Open Monograph Press  3.3.0
PKPSiteHandler.inc.php
1 <?php
15 import('lib.pkp.classes.handler.APIHandler');
16 
17 class PKPSiteHandler extends APIHandler {
19  public $schemaName = SCHEMA_SITE;
20 
24  public function __construct() {
25  $this->_handlerPath = 'site';
26  $roles = [ROLE_ID_SITE_ADMIN];
27  $this->_endpoints = array(
28  'GET' => array(
29  array(
30  'pattern' => $this->getEndpointPattern(),
31  'handler' => array($this, 'get'),
32  'roles' => $roles,
33  ),
34  array(
35  'pattern' => $this->getEndpointPattern() . '/theme',
36  'handler' => array($this, 'getTheme'),
37  'roles' => $roles,
38  ),
39  ),
40  'PUT' => array(
41  array(
42  'pattern' => $this->getEndpointPattern(),
43  'handler' => array($this, 'edit'),
44  'roles' => $roles,
45  ),
46  array(
47  'pattern' => $this->getEndpointPattern() . '/theme',
48  'handler' => array($this, 'editTheme'),
49  'roles' => $roles,
50  ),
51  ),
52  );
53  parent::__construct();
54  }
55 
59  public function authorize($request, &$args, $roleAssignments) {
60  import('lib.pkp.classes.security.authorization.PolicySet');
61  $rolePolicy = new PolicySet(COMBINING_PERMIT_OVERRIDES);
62 
63  import('lib.pkp.classes.security.authorization.RoleBasedHandlerOperationPolicy');
64  foreach($roleAssignments as $role => $operations) {
65  $rolePolicy->addPolicy(new RoleBasedHandlerOperationPolicy($request, $role, $operations));
66  }
67  $this->addPolicy($rolePolicy);
68 
69  return parent::authorize($request, $args, $roleAssignments);
70  }
71 
81  public function get($slimRequest, $response, $args) {
82  $request = $this->getRequest();
83 
84  $siteProps = Services::get('site')
85  ->getFullProperties($request->getSite(), [
86  'request' => $request,
87  ]);
88 
89  return $response->withJson($siteProps, 200);
90  }
91 
101  public function getTheme($slimRequest, $response, $args) {
102  $site = $this->getRequest()->getSite();
103 
104  $allThemes = PluginRegistry::loadCategory('themes', true);
105  $activeTheme = null;
106  foreach ($allThemes as $theme) {
107  if ($site->getData('themePluginPath') === $theme->getDirName()) {
108  $activeTheme = $theme;
109  break;
110  }
111  }
112 
113  if (!$activeTheme) {
114  return $response->withStatus(404)->withJsonError('api.themes.404.themeUnavailable');
115  }
116 
117  $data = array_merge(
118  $activeTheme->getOptionValues(CONTEXT_ID_NONE),
119  ['themePluginPath' => $theme->getDirName()]
120  );
121 
122  ksort($data);
123 
124  return $response->withJson($data, 200);
125  }
126 
136  public function edit($slimRequest, $response, $args) {
137  $request = $this->getRequest();
138  $site = $request->getSite();
139  $siteService = Services::get('site');
140 
141  $params = $this->convertStringsToSchema(SCHEMA_SITE, $slimRequest->getParsedBody());
142 
143  $errors = $siteService->validate($params, $site->getSupportedLocales(), $site->getPrimaryLocale());
144 
145  if (!empty($errors)) {
146  return $response->withStatus(400)->withJson($errors);
147  }
148  $site = $siteService->edit($site, $params, $request);
149 
150  $siteProps = $siteService->getFullProperties($site, array(
151  'request' => $request,
152  'slimRequest' => $slimRequest
153  ));
154 
155  return $response->withJson($siteProps, 200);
156  }
157 
167  public function editTheme($slimRequest, $response, $args) {
168  $request = $this->getRequest();
169  $site = $request->getSite();
170  $siteService = Services::get('site');
171 
172  $params = $slimRequest->getParsedBody();
173 
174  // Validate the themePluginPath and allow themes to perform their own validation
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()
181  );
182  if (!empty($errors)) {
183  return $response->withJson($errors, 400);
184  }
185  $newSite = $siteService->edit($site, ['themePluginPath' => $themePluginPath], $request);
186  }
187 
188  // Get the appropriate theme plugin
189  $allThemes = PluginRegistry::loadCategory('themes', true);
190  $selectedTheme = null;
191  foreach ($allThemes as $theme) {
192  if ($themePluginPath === $theme->getDirName()) {
193  $selectedTheme = $theme;
194  break;
195  }
196  }
197 
198  // Run the theme's init() method if a new theme has been selected
199  if (isset($newSite)) {
200  $selectedTheme->init();
201  }
202 
203  $errors = $selectedTheme->validateOptions($params, $themePluginPath, CONTEXT_ID_NONE, $request);
204  if (!empty($errors)) {
205  return $response->withJson($errors, 400);
206  }
207 
208  // Only accept params that are defined in the theme options
209  $options = $selectedTheme->getOptionsConfig();
210  foreach ($options as $optionName => $optionConfig) {
211  if (!array_key_exists($optionName, $params)) {
212  continue;
213  }
214  $selectedTheme->saveOption($optionName, $params[$optionName], CONTEXT_ID_NONE);
215  }
216 
217  // Clear the template cache so that new settings can take effect
219  $templateMgr->clearTemplateCache();
220  $templateMgr->clearCssCache();
221 
222  $data = array_merge(
223  $selectedTheme->getOptionValues(CONTEXT_ID_NONE),
224  ['themePluginPath' => $themePluginPath]
225  );
226 
227  ksort($data);
228 
229  return $response->withJson($data, 200);
230  }
231 }
PKPSiteHandler\getTheme
getTheme($slimRequest, $response, $args)
Definition: PKPSiteHandler.inc.php:104
PKPSiteHandler\edit
edit($slimRequest, $response, $args)
Definition: PKPSiteHandler.inc.php:139
PKPSiteHandler\__construct
__construct()
Definition: PKPSiteHandler.inc.php:27
PKPSiteHandler\$schemaName
$schemaName
Definition: PKPSiteHandler.inc.php:22
PKPSiteHandler\authorize
authorize($request, &$args, $roleAssignments)
Definition: PKPSiteHandler.inc.php:62
PluginRegistry\loadCategory
static loadCategory($category, $enabledOnly=false, $mainContextId=null)
Definition: PluginRegistry.inc.php:103
PKPSiteHandler\editTheme
editTheme($slimRequest, $response, $args)
Definition: PKPSiteHandler.inc.php:170
APIHandler
Base request API handler.
Definition: APIHandler.inc.php:22
PKPTemplateManager\getManager
static & getManager($request=null)
Definition: PKPTemplateManager.inc.php:1239
APIHandler\convertStringsToSchema
convertStringsToSchema($schema, $params)
Definition: APIHandler.inc.php:281
RoleBasedHandlerOperationPolicy
Class to control access to handler operations via role based access control.
Definition: RoleBasedHandlerOperationPolicy.inc.php:18
PKPApplication\get
static get()
Definition: PKPApplication.inc.php:235
APIHandler\getRequest
getRequest()
Definition: APIHandler.inc.php:149
PKPHandler\addPolicy
addPolicy($authorizationPolicy, $addToTop=false)
Definition: PKPHandler.inc.php:157
PKPSiteHandler
Base class to handle API requests for the site object.
Definition: PKPSiteHandler.inc.php:17
APIHandler\getEndpointPattern
getEndpointPattern()
Definition: APIHandler.inc.php:186
PolicySet
An ordered list of policies. Policy sets can be added to decision managers like policies....
Definition: PolicySet.inc.php:26
PKPServices\get
static get($service)
Definition: PKPServices.inc.php:49