DisplayController.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2015 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 
21 
22 /**
23  * DisplayController is used to read a Node instance.
24  *
25  * The controller supports the following actions:
26  *
27  * <div class="controller-action">
28  * <div> __Action__ _default_ </div>
29  * <div>
30  * Load the given Node instance.
31  * | Parameter | Description
32  * |------------------------|-------------------------
33  * | _in_ `oid` | The object id of the Node to read
34  * | _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)
35  * | _in_ `translateValues` | Boolean whether list values should be translated to their display values (optional, default: _true_)
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  array('invalidOids' => array($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() {
75  $persistenceFacade = $this->getPersistenceFacade();
76  $permissionManager = $this->getPermissionManager();
77  $request = $this->getRequest();
78  $response = $this->getResponse();
79  $logger = $this->getLogger();
80  $message = $this->getMessage();
81 
82  // load model
83  $oid = ObjectId::parse($request->getValue('oid'));
84  if ($oid && $permissionManager->authorize($oid, '', PersistenceAction::READ)) {
85  // determine the builddepth
86  $buildDepth = BuildDepth::SINGLE;
87  if ($request->hasValue('depth')) {
88  $buildDepth = $request->getValue('depth');
89  }
90  $node = $persistenceFacade->load($oid, $buildDepth);
91  if ($node == null) {
92  throw new IllegalArgumentException($message->getText("The object with oid '%0%' does not exist.", array($oid)));
93  }
94 
95  // translate all nodes to the requested language if requested
96  if ($this->isLocalizedRequest()) {
97  $localization = $this->getLocalization();
98  $node = $localization->loadTranslation($node, $request->getValue('language'), true, true);
99  }
100 
101  if ($logger->isDebugEnabled()) {
102  $logger->debug(nl2br($node->__toString()));
103  }
104 
105  // translate values if requested
106  if ($request->getBooleanValue('translateValues')) {
107  $nodes = array($node);
108  if ($this->isLocalizedRequest()) {
109  NodeUtil::translateValues($nodes, $request->getValue('language'));
110  }
111  else {
113  }
114  }
115 
116  // assign node data
117  $response->setValue('object', $node);
118  }
119  else {
120  throw new AuthorizationException($message->getText("Authorization failed for action '%0%' on '%1%'.",
121  array($message->getText('read'), $oid)));
122  }
123  // success
124  $response->setAction('ok');
125  }
126 }
127 ?>
getRequest()
Get the Request instance.
Definition: Controller.php:190
getMessage()
Get the Message instance.
Definition: Controller.php:254
Controller is the base class of all controllers.
Definition: Controller.php:48
IllegalArgumentException signals an exception in method arguments.
static translateValues(&$nodes, $language=null)
Translate all list values in a list of Nodes.
Definition: NodeUtil.php:253
AuthorizationException signals an exception in authorization.
getPermissionManager()
Get the PermissionManager instance.
Definition: Controller.php:230
isLocalizedRequest()
Check if the current request is localized.
Definition: Controller.php:300
getLocalization()
Get the Localization instance.
Definition: Controller.php:246
checkLanguageParameter()
Checks the language request parameter and adds an response error, if it is not contained in the Local...
Definition: Controller.php:315
static parse($oid)
Parse a serialized object id string into an ObjectId instance.
Definition: ObjectId.php:144
Application controllers.
Definition: namespaces.php:3
static get($code, $data=null)
Factory method for retrieving a predefind error instance.
DisplayController is used to read a Node instance.
getLogger()
Get the Logger instance.
Definition: Controller.php:206
getResponse()
Get the Response instance.
Definition: Controller.php:198
getPersistenceFacade()
Get the PersistenceFacade instance.
Definition: Controller.php:222