wCMF  3.6
All Classes Namespaces Files Functions Variables Groups Pages
class.TreeViewController.php
Go to the documentation of this file.
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2014 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of any of the following licenses
7  * at your choice:
8  *
9  * - GNU Lesser General Public License (LGPL)
10  * http://www.gnu.org/licenses/lgpl.html
11  * - Eclipse Public License (EPL)
12  * http://www.eclipse.org/org/documents/epl-v10.php
13  *
14  * See the license.txt file distributed with this work for
15  * additional information.
16  *
17  * $Id: class.TreeViewController.php 1462 2014-02-04 23:52:27Z iherwig $
18  */
19 require_once(BASE."wcmf/lib/model/class.Node.php");
20 require_once(BASE."wcmf/lib/util/class.Message.php");
21 require_once(BASE."wcmf/lib/presentation/class.Controller.php");
22 require_once(BASE."wcmf/lib/util/class.StringUtil.php");
23 require_once(BASE."wcmf/lib/persistence/class.PersistenceFacade.php");
24 require_once(BASE."wcmf/lib/security/class.RightsManager.php");
25 
26 /**
27  * @class TreeViewController
28  * @ingroup Controller
29  * @brief TreeViewController is used to visualize cms data in a tree view.
30  *
31  * <b>Input actions:</b>
32  * - @em loadChilren Load the children of the given parent Node
33  *
34  * <b>Output actions:</b>
35  * - @em ok In any case
36  *
37  * @param[in] node The object id of the parent Node whose children should be loaded
38  * @param[in] sort The attribute to sort the children by
39  * @param[out] objects An array of associative arrays with keys 'oid', 'text', 'onClickAction', 'hasChildren'
40  *
41  * @author ingo herwig <ingo@wemove.com>
42  */
44 {
45  /**
46  * @see Controller::hasView()
47  */
48  function hasView()
49  {
50  if ($this->_request->getAction() == 'loadChildren') {
51  return false;
52  }
53  else {
54  return true;
55  }
56  }
57  /**
58  * Assign data to View.
59  * @return Array of given context and action 'failure' on failure.
60  * False on success (Stop action processing chain).
61  * In case of 'failure' a detailed description is provided by getErrorMsg().
62  * @see Controller::executeKernel()
63  */
64  function executeKernel()
65  {
66  // the tree component sends the object id of the parent node in the 'node' parameter
67  $oid = $this->_request->getValue('node');
68 
69  if ($this->_request->getAction() == 'loadChildren')
70  {
71  // load model
72  $nodes = $this->getChildren($oid);
73 
74  // translate all nodes to the requested language if requested
75  if ($this->isLocalizedRequest())
76  {
77  $localization = Localization::getInstance();
78  for ($i=0; $i<sizeof($nodes); $i++) {
79  $localization->loadTranslation($nodes[$i], $this->_request->getValue('language'), true, true);
80  }
81  }
82 
83  // sort nodes if requested
84  if ($this->_request->hasValue('sort')) {
85  Node::sort($nodes, $this->_request->getValue('sort'));
86  }
87 
88  // create the json response
89  $responseObjects = array();
90  for($i=0; $i<sizeof($nodes); $i++)
91  {
92  $node = &$nodes[$i];
93  if ($this->isVisible($node)) {
94  array_push($responseObjects, $this->getViewNode($node));
95  }
96  }
97  $this->_response->setValue('objects', $responseObjects);
98  }
99 
100  // success
101  $this->_response->setAction('ok');
102  return false;
103  }
104  /**
105  * Get the OIDs of the root nodes. TreeViewController will build the complete
106  * resource tree from these.
107  * @note subclasses will override this to implement special application requirements.
108  * @return An array of OIDs.
109  */
110  function getRootOIDs()
111  {
112  $oids = array();
113 
114  // get root types from ini file
115  $parser = &InifileParser::getInstance();
116  $rootTypes = $parser->getValue('rootTypes', 'cms');
117  if ($rootTypes === false || !is_array($rootTypes) || $rootTypes[0] == '') {
118  $this->setErrorMsg("No root types defined.");
119  }
120  else
121  {
122  $persistenceFacade = &PersistenceFacade::getInstance();
123  foreach($rootTypes as $rootType) {
124  $oids = array_merge($oids, $persistenceFacade->getOIDs($rootType));
125  }
126  }
127  return $oids;
128  }
129  /**
130  * Get the children for a given oid.
131  * @note subclasses will override this to implement special application requirements.
132  * @return An array of Node instances.
133  */
134  function getChildren($oid)
135  {
136  $persistenceFacade = &PersistenceFacade::getInstance();
137  $rightsManager = &RightsManager::getInstance();
138 
139  $nodes = array();
140  if ($oid != 'root' && PersistenceFacade::isValidOID($oid))
141  {
142  // load children
143  if ($rightsManager->authorize($oid, '', ACTION_READ))
144  {
145  $parentNode = &$persistenceFacade->load($oid, 1);
146  $nodes = $parentNode->getChildren();
147  }
148  }
149  else
150  {
151  // first call or reload
152  $rootOIDs = $this->getRootOIDs();
153  foreach ($rootOIDs as $rootOID)
154  {
155  if ($rightsManager->authorize($rootOID, '', ACTION_READ))
156  {
157  $node = &$persistenceFacade->load($rootOID, BUILDDEPTH_SINGLE);
158  $nodes[sizeof($nodes)] = &$node;
159  }
160  }
161  }
162  return $nodes;
163  }
164  /**
165  * Get the view of a Node
166  * @param node The Node to create the view for
167  * @param displayText The text to display (will be taken from TreeViewController::getDisplayText() if not specified) [default: '']
168  * @return An associative array whose keys correspond to Ext.tree.TreeNode config parameters
169  */
170  function getViewNode(&$node, $displayText='')
171  {
172  if (strlen($displayText) == 0) {
173  $displayText = trim($this->getDisplayText($node));
174  }
175  if (strlen($displayText) == 0) {
176  $displayText = '-';
177  }
178  // click action
179  $onClickAction = $this->getClickAction($node);
180  if ($onClickAction == null) {
181  $onClickAction = '#';
182  }
183  $hasChildren = sizeof($node->getProperty('childoids')) > 0;
184  return array('oid' => $node->getOID(), 'text' => $displayText, 'onClickAction' => $onClickAction,
185  'hasChildren' => $hasChildren);
186  }
187  /**
188  * Test if a Node should be displayed in the tree
189  * @note subclasses will override this to implement special application requirements.
190  * @param node A reference to the Node to display
191  * @return True/false (the default implementation always returns true)
192  */
193  function isVisible(&$node)
194  {
195  return true;
196  }
197  /**
198  * Get the display text for a Node
199  * @note subclasses will override this to implement special application requirements.
200  * @param node A reference to the Node to display
201  * @return The display text.
202  */
203  function getDisplayText(&$node)
204  {
205  return strip_tags(preg_replace("/[\r\n']/", " ", NodeUtil::getDisplayValue($node)));
206  }
207  /**
208  * Get the action to perform if a Node is clicked (The content of the anchor tag href attribute)
209  * @note subclasses will override this to implement special application requirements.
210  * @param node A reference to the Node
211  * @return The action
212  */
213  function getClickAction(&$node)
214  {
215  //return "setTarget('nodeview'); doDisplay('".$node->getOID()."'); submitAction('display'); return false";
216  return "javascript:opener.setContext('".$node->getType()."'); opener.doDisplay('".$node->getOID()."'); opener.submitAction('display');";
217  }
218 }
219 ?>
sort(&$nodeList, $criteria, $recursive=false, $changeSortkey=false, $sortFunction='')
Definition: class.Node.php:356
TreeViewController is used to visualize cms data in a tree view.
Controller is the base class of all controllers. If a Controller has a view it is expected to reside ...
getViewNode(&$node, $displayText='')
getDisplayValue(&$node, $useDisplayType=false, $language=null, $values=null)
const BUILDDEPTH_SINGLE
const ACTION_READ