17 import(
'lib.pkp.classes.handler.APIHandler');
18 import(
'classes.core.Services');
26 $this->_handlerPath =
'announcements';
31 'handler' => [$this,
'getMany'],
32 'roles' => [ROLE_ID_MANAGER],
36 'handler' => [$this,
'get'],
37 'roles' => [ROLE_ID_MANAGER],
43 'handler' => [$this,
'add'],
44 'roles' => [ROLE_ID_MANAGER],
50 'handler' => [$this,
'edit'],
51 'roles' => [ROLE_ID_MANAGER],
57 'handler' => array($this,
'delete'),
58 'roles' => array(ROLE_ID_MANAGER),
62 parent::__construct();
68 public function authorize($request, &$args, $roleAssignments) {
69 import(
'lib.pkp.classes.security.authorization.PolicySet');
70 $rolePolicy =
new PolicySet(COMBINING_PERMIT_OVERRIDES);
72 import(
'lib.pkp.classes.security.authorization.RoleBasedHandlerOperationPolicy');
73 foreach ($roleAssignments as $role => $operations) {
78 return parent::authorize($request, $args, $roleAssignments);
88 public function get($slimRequest, $response, $args) {
90 $announcement =
Services::get(
'announcement')->get((
int) $args[
'announcementId']);
93 return $response->withStatus(404)->withJsonError(
'api.announcements.404.announcementNotFound');
97 if ($announcement->getData(
'assocId') !== $this->getRequest()->getContext()->getId()) {
98 return $response->withStatus(404)->withJsonError(
'api.announcements.400.contextsNotMatched');
105 'announcementContext' => $this->
getRequest()->getContext(),
109 return $response->withJson($props, 200);
119 public function getMany($slimRequest, $response, $args) {
127 $requestParams = $slimRequest->getQueryParams();
130 foreach ($requestParams as $param => $val) {
134 if (is_string($val) && strpos($val,
',') > -1) {
135 $val = explode(
',', $val);
136 } elseif (!is_array($val)) {
139 $params[$param] = array_map(
'intval', $val);
143 $params[$param] = (int) $val;
146 $params[$param] = $val;
151 $params[
'contextIds'] = [$this->
getRequest()->getContext()->getId()];
158 if ($result->valid()) {
159 foreach ($result as $announcement) {
160 $items[] =
Services::get(
'announcement')->getSummaryProperties($announcement, [
162 'announcementContext' => $this->
getRequest()->getContext(),
167 return $response->withJson([
168 'itemsMax' =>
Services::get(
'announcement')->getMax($params),
181 public function add($slimRequest, $response, $args) {
184 if (!$request->getContext()) {
185 throw new Exception(
'You can not add an announcement without sending a request to the API endpoint of a particular context.');
190 $params[
'assocId'] = $request->getContext()->getId();
192 $primaryLocale = $request->getContext()->getPrimaryLocale();
193 $allowedLocales = $request->getContext()->getSupportedFormLocales();
194 $errors =
Services::get(
'announcement')->validate(VALIDATE_ACTION_ADD, $params, $allowedLocales, $primaryLocale);
196 if (!empty($errors)) {
197 return $response->withStatus(400)->withJson($errors);
200 $announcement = DAORegistry::getDao(
'AnnouncementDAO')->newDataObject();
201 $announcement->_data = $params;
202 $announcement =
Services::get(
'announcement')->add($announcement, $request);
203 $announcementProps =
Services::get(
'announcement')->getFullProperties($announcement, [
204 'request' => $request,
205 'announcementContext' => $request->getContext(),
208 return $response->withJson($announcementProps, 200);
219 public function edit($slimRequest, $response, $args) {
222 $announcement =
Services::get(
'announcement')->get((
int) $args[
'announcementId']);
224 if (!$announcement) {
225 return $response->withStatus(404)->withJsonError(
'api.announcements.404.announcementNotFound');
228 if ($announcement->getData(
'assocType') !==
Application::get()->getContextAssocType()) {
229 throw new Exception(
'Announcement has an assocType that did not match the context.');
233 if ($request->getContext()->getId() !== $announcement->getData(
'assocId')) {
234 return $response->withStatus(403)->withJsonError(
'api.announcements.400.contextsNotMatched');
238 $params[
'id'] = $announcement->getId();
240 $context = $request->getContext();
241 $primaryLocale = $context->getPrimaryLocale();
242 $allowedLocales = $context->getSupportedFormLocales();
244 $errors =
Services::get(
'announcement')->validate(VALIDATE_ACTION_EDIT, $params, $allowedLocales, $primaryLocale);
245 if (!empty($errors)) {
246 return $response->withStatus(400)->withJson($errors);
249 $announcement =
Services::get(
'announcement')->edit($announcement, $params, $request);
251 $announcementProps =
Services::get(
'announcement')->getFullProperties($announcement, [
252 'request' => $request,
253 'announcementContext' => $context,
256 return $response->withJson($announcementProps, 200);
267 public function delete($slimRequest, $response, $args) {
270 $announcement =
Services::get(
'announcement')->get((
int) $args[
'announcementId']);
272 if (!$announcement) {
273 return $response->withStatus(404)->withJsonError(
'api.announcements.404.announcementNotFound');
276 if ($announcement->getData(
'assocType') !==
Application::get()->getContextAssocType()) {
277 throw new Exception(
'Announcement has an assocType that did not match the context.');
281 if ($request->getContext()->getId() !== $announcement->getData(
'assocId')) {
282 return $response->withStatus(403)->withJsonError(
'api.announcements.400.contextsNotMatched');
285 $announcementProps =
Services::get(
'announcement')->getSummaryProperties($announcement, array(
286 'request' => $request,
287 'announcementContext' => $request->getContext(),
292 return $response->withJson($announcementProps, 200);