TreeController.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 
20 
21 /**
22  * TreeController is used to visualize nodes in a tree view.
23  *
24  * The controller supports the following actions:
25  *
26  * <div class="controller-action">
27  * <div> __Action__ _default_ </div>
28  * <div>
29  * Load the cild nodes of the given Node.
30  * | Parameter | Description
31  * |------------------------|-------------------------
32  * | _in_ `oid` | The object id of the parent Node whose children should be loaded (optional)
33  * | _in_ `sort` | The attribute to sort the children by (optional)
34  * | _in_ `rootTypes` | Name of a configuration value in configuration section 'application', that defines an array of root types of the tree (optional, defaults to 'rootTypes')
35  * | _out_ `list` | An array of associative arrays with keys 'oid', 'displayText', 'isFolder', 'hasChildren'
36  * | __Response Actions__ | |
37  * | `ok` | In all cases
38  * </div>
39  * </div>
40  *
41  * @author ingo herwig <ingo@wemove.com>
42  */
43 class TreeController extends Controller {
44 
45  /**
46  * @see Controller::doExecute()
47  */
48  protected function doExecute() {
49  $request = $this->getRequest();
50  $response = $this->getResponse();
51 
52  $oidStr = $request->getValue('oid');
53  if ($oidStr == 'root') {
54  // types below root node
55  $objects = $this->getRootTypes();
56  }
57  else {
58  $oid = ObjectId::parse($oidStr);
59  if ($oid != null) {
60  // load children
61  $objects = $this->getChildren($oid);
62  }
63  }
64 
65  // sort nodes if requested
66  if ($request->hasValue('sort')) {
67  $objects = Node::sort($objects, $request->getValue('sort'));
68  }
69 
70  // create response
71  $responseObjects = array();
72  for ($i=0, $count=sizeof($objects); $i<$count; $i++) {
73  $object = $objects[$i];
74  if ($this->isVisible($object)) {
75  $responseObjects[] = $this->getViewNode($object);
76  }
77  }
78  $response->setValue('list', $responseObjects);
79 
80  // success
81  $response->setAction('ok');
82  }
83 
84  /**
85  * Get the children for a given oid.
86  * @param $oid The object id
87  * @return Array of Node instances.
88  */
89  protected function getChildren($oid) {
90 
91  $permissionManager = $this->getPermissionManager();
92  $persistenceFacade = $this->getPersistenceFacade();
93 
94  // check read permission on type
95  $type = $oid->getType();
96  if (!$permissionManager->authorize($type, '', PersistenceAction::READ)) {
97  return array();
98  }
99 
100  $objectsTmp = array();
101  if ($this->isRootTypeNode($oid)) {
102  // load instances of type
103  $objectsTmp = $persistenceFacade->loadObjects($type, BuildDepth::SINGLE);
104  }
105  else {
106  // load children of node
107  if ($permissionManager->authorize($oid, '', PersistenceAction::READ)) {
108  $node = $persistenceFacade->load($oid, 1);
109  if ($node) {
110  $objectsTmp = $node->getChildren();
111  }
112  }
113  }
114 
115  // check read permission on instances
116  $objects = array();
117  foreach ($objectsTmp as $object) {
118  if ($permissionManager->authorize($object->getOID(), '', PersistenceAction::READ)) {
119  $objects[] = $object;
120  }
121  }
122 
123  return $objects;
124  }
125 
126  /**
127  * Get the oids of the root nodes.
128  * @return An array of object ids.
129  */
130  protected function getRootOIDs() {
131  // types below root node
132  return $this->getRootTypes();
133  }
134 
135  /**
136  * Get the view of a Node
137  * @param $node The Node to create the view for
138  * @param $displayText The text to display (will be taken from TreeController::getDisplayText() if not specified) (default: '')
139  * @return An associative array whose keys correspond to Ext.tree.TreeNode config parameters
140  */
141  protected function getViewNode(Node $node, $displayText='') {
142  if (strlen($displayText) == 0) {
143  $displayText = trim($this->getDisplayText($node));
144  }
145  if (strlen($displayText) == 0) {
146  $displayText = '-';
147  }
148  $oid = $node->getOID();
149  $isFolder = $oid->containsDummyIds();
150  $hasChildren = $this->isRootTypeNode($oid) || sizeof($node->getNumChildren()) > 0;
151  return array(
152  'oid' => $node->getOID()->__toString(),
153  'displayText' => $displayText,
154  'isFolder' => $isFolder,
155  'hasChildren' => $hasChildren
156  );
157  }
158 
159  /**
160  * Test if a Node should be displayed in the tree
161  * @param $node Node to display
162  * @return Boolean
163  */
164  protected function isVisible(Node $node) {
165  return true;
166  }
167 
168  /**
169  * Get the display text for a Node
170  * @param $node Node to display
171  * @return The display text.
172  */
173  protected function getDisplayText(Node $node) {
174  if ($this->isRootTypeNode($node->getOID())) {
175  $mapper = $node->getMapper();
176  return $mapper->getTypeDisplayName($this->getMessage());
177  }
178  else {
179  return strip_tags(preg_replace("/[\r\n']/", " ", NodeUtil::getDisplayValue($node)));
180  }
181  }
182 
183  /**
184  * Get all root types
185  * @return Array of Node instances
186  */
187  protected function getRootTypes() {
188  $request = $this->getRequest();
189  $config = $this->getConfiguration();
190  $appConfig = $config->getSection('application');
191 
192  // get root types from configuration
193  // try request value first
194  $rootTypeVar = $request->getValue('rootTypes');
195  if (isset($appConfig[$rootTypeVar]) && is_array($appConfig[$rootTypeVar])) {
196  $types = $appConfig[$rootTypeVar];
197  }
198  else if (isset($appConfig['rootTypes']) && is_array($appConfig['rootTypes'])) {
199  // fall back to root types
200  $types = $appConfig['rootTypes'];
201  }
202  else {
203  throw new ConfigurationException("No root types defined.");
204  }
205 
206  // filter types by read permission
207  $permissionManager = $this->getPermissionManager();
208  $persistenceFacade = $this->getPersistenceFacade();
209  $nodes = array();
210  foreach($types as $type) {
211  if ($permissionManager->authorize($type, '', PersistenceAction::READ)) {
212  $node = $persistenceFacade->create($type, BuildDepth::SINGLE);
213  $nodes[] = $node;
214  }
215  }
216  return $nodes;
217  }
218 
219  /**
220  * Check if the given oid belongs to a root type node
221  * @param $oid The object id
222  * @return Boolean
223  */
224  protected function isRootTypeNode(ObjectId $oid) {
225  if ($oid->containsDummyIds()) {
226  $type = $oid->getType();
227  $rootTypes = $this->getRootTypes();
228  foreach ($rootTypes as $rootType) {
229  if ($rootType->getType() == $type) {
230  return true;
231  }
232  }
233  }
234  return false;
235  }
236 }
237 ?>
getViewNode(Node $node, $displayText='')
Get the view of a Node.
getNumChildren($memOnly=true)
Get the number of children of the Node.
Definition: Node.php:287
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
getPermissionManager()
Get the PermissionManager instance.
Definition: Controller.php:230
ObjectId is the unique identifier of an object.
Definition: ObjectId.php:27
getDisplayText(Node $node)
Get the display text for a Node.
getConfiguration()
Get the Configuration instance.
Definition: Controller.php:262
containsDummyIds()
Check if this object id contains a dummy id.
Definition: ObjectId.php:236
static parse($oid)
Parse a serialized object id string into an ObjectId instance.
Definition: ObjectId.php:144
getRootOIDs()
Get the oids of the root nodes.
getChildren($oid)
Get the children for a given oid.
Application controllers.
Definition: namespaces.php:3
TreeController is used to visualize nodes in a tree view.
static getDisplayValue(Node $node, $language=null)
Get the display value for a Node defined by the 'display_value' property.
Definition: NodeUtil.php:156
getType()
Get the type (including namespace)
Definition: ObjectId.php:106
getResponse()
Get the Response instance.
Definition: Controller.php:198
ConfigurationException signals an exception in the configuration.
isVisible(Node $node)
Test if a Node should be displayed in the tree.
Node adds the concept of relations to PersistentObject.
Definition: Node.php:34
getPersistenceFacade()
Get the PersistenceFacade instance.
Definition: Controller.php:222
isRootTypeNode(ObjectId $oid)
Check if the given oid belongs to a root type node.