DisplayController.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2020 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of the MIT License.
7  *
8  * See the LICENSE file distributed with this work for
9  * additional information.
10  */
12 
20 
21 /**
22  * DisplayController is used to read a Node instance.
23  *
24  * The controller supports the following actions:
25  *
26  * <div class="controller-action">
27  * <div> __Action__ _default_ </div>
28  * <div>
29  * Load the given Node instance.
30  * | Parameter | Description
31  * |------------------------|-------------------------
32  * | _in_ `oid` | The object id of the Node to read
33  * | _in_ `depth` | The number of levels referenced Node must be returned as complete objects. Below this level, Nodes are returned as references. The value -1 has the special meaning of unlimited depth (optional, default: 1)
34  * | _in_ `useDefaults` | Boolean whether to apply values from the default language, if they are not provided in a translation (optional, default: _true_)
35  * | _in_ `translateValues` | Boolean whether list values should be translated to their display values (optional, default: _false_)
36  * | _out_ `object` | The Node to read
37  * | __Response Actions__ | |
38  * | `ok` | In all cases
39  * </div>
40  * </div>
41  *
42  * @author ingo herwig <ingo@wemove.com>
43  */
45 
46  /**
47  * @see Controller::validate()
48  */
49  protected function validate() {
50  $request = $this->getRequest();
51  $response = $this->getResponse();
52  $oid = ObjectId::parse($request->getValue('oid'));
53  if(!$oid) {
54  $response->addError(ApplicationError::get('OID_INVALID',
55  ['invalidOids' => [$request->getValue('oid')]]));
56  return false;
57  }
58  if($request->hasValue('depth')) {
59  $depth = intval($request->getValue('depth'));
60  if ($depth < -1) {
61  $response->addError(ApplicationError::get('DEPTH_INVALID'));
62  }
63  }
64  if (!$this->checkLanguageParameter()) {
65  return false;
66  }
67  // do default validation
68  return parent::validate();
69  }
70 
71  /**
72  * @see Controller::doExecute()
73  */
74  protected function doExecute($method=null) {
75  $persistenceFacade = $this->getPersistenceFacade();
76  $permissionManager = $this->getPermissionManager();
77  $request = $this->getRequest();
78  $response = $this->getResponse();
79 
80  // check permission
81  $oid = ObjectId::parse($request->getValue('oid'));
82  if (!$permissionManager->authorize($oid, '', PersistenceAction::READ)) {
83  throw new ApplicationException($request, $response, ApplicationError::get('PERMISSION_DENIED'));
84  }
85 
86  // load model
87  $buildDepth = $request->getValue('depth', BuildDepth::SINGLE);
88  $node = $persistenceFacade->load($oid, $buildDepth);
89  if ($node == null) {
90  $response->setStatus(404);
91  return;
92  }
93 
94  // translate all nodes to the requested language if requested
95  if ($this->isLocalizedRequest()) {
96  $localization = $this->getLocalization();
97  $node = $localization->loadTranslation($node, $request->getValue('language'), $request->getBooleanValue('useDefaults', true), true);
98  }
99 
100  // translate values if requested
101  if ($request->getBooleanValue('translateValues')) {
102  $nodes = [$node];
103  if ($this->isLocalizedRequest()) {
104  NodeUtil::translateValues($nodes, $request->getValue('language'));
105  }
106  else {
108  }
109  }
110 
111  // assign node data
112  $response->setValue('object', $node);
113 
114  // success
115  $response->setAction('ok');
116  }
117 }
118 ?>
ObjectId is the unique identifier of an object.
Definition: ObjectId.php:28
getPermissionManager()
Get the PermissionManager instance.
Definition: Controller.php:291
BuildDepth values are used to define the depth when loading object trees.
Definition: BuildDepth.php:19
static translateValues(&$nodes, $language=null, $itemDelim=", ")
Translate all list values in a list of Nodes.
Definition: NodeUtil.php:249
DisplayController is used to read a Node instance.
ApplicationError is used to signal errors that occur while processing a request.
static parse($oid)
Parse a serialized object id string into an ObjectId instance.
Definition: ObjectId.php:135
static get($code, $data=null)
Factory method for retrieving a predefined error instance.
ApplicationException signals a general application exception.
getPersistenceFacade()
Get the PersistenceFacade instance.
Definition: Controller.php:283
getRequest()
Get the Request instance.
Definition: Controller.php:251
Application controllers.
Definition: namespaces.php:3
getLocalization()
Get the Localization instance.
Definition: Controller.php:307
Controller is the base class of all controllers.
Definition: Controller.php:49
isLocalizedRequest()
Check if the current request is localized.
Definition: Controller.php:366
getResponse()
Get the Response instance.
Definition: Controller.php:259
NodeUtil provides services for the Node class.
Definition: NodeUtil.php:28
PersistenceAction values are used to define actions on PersistentObject instances.
checkLanguageParameter()
Checks the language request parameter and adds an response error, if it is not contained in the Local...
Definition: Controller.php:381