HistoryController.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 
19 
20 /**
21  * HistoryController returns a list of last changed entity instances.
22  *
23  * The controller supports the following actions:
24  *
25  * <div class="controller-action">
26  * <div> __Action__ _default_ </div>
27  * <div>
28  * Search.
29  * | Parameter | Description
30  * |------------------------|-------------------------
31  * | __Response Actions__ | |
32  * | `ok` | In all cases
33  * </div>
34  * </div>
35  *
36  * For additional actions and parameters see [ListController actions](@ref ListController).
37  *
38  * @author ingo herwig <ingo@wemove.com>
39  */
41 
42  /**
43  * @see Controller::initialize()
44  */
45  public function initialize(Request $request, Response $response) {
46  $request->setValue('completeObjects', true);
47  parent::initialize($request, $response);
48  }
49 
50  /**
51  * @see Controller::validate()
52  */
53  protected function validate() {
54  // skip validation
55  return true;
56  }
57 
58  /**
59  * @see ListController::getObjects()
60  */
61  protected function getObjects($type, $queryCondition, $sortArray, $pagingInfo) {
62  $permissionManager = $this->getPermissionManager();
63  $persistenceFacade = $this->getPersistenceFacade();
64 
65  // find types with attributes created, creator, modified, last_editor
66  $requiredAttributes = ['created', 'creator', 'modified', 'last_editor'];
67  $types = [];
68  foreach ($persistenceFacade->getKnownTypes() as $type) {
69  $mapper = $persistenceFacade->getMapper($type);
70  $matches = true;
71  foreach ($requiredAttributes as $attribute) {
72  if (!$mapper->hasAttribute($attribute)) {
73  $matches = false;
74  break;
75  }
76  }
77  if ($matches) {
78  $types[] = $type;
79  }
80  }
81 
82  // load objects
83  // NOTE: we always get last changed objects and sort by requested value later
84  $historyItems = $persistenceFacade->loadObjects($types, BuildDepth::SINGLE,
85  null, ['modified DESC'], $pagingInfo);
86 
87  $objects = [];
88  foreach($historyItems as $historyItem) {
89  if ($permissionManager->authorize($historyItem->getOID(), '', PersistenceAction::READ)) {
90  $objects[] = $historyItem;
91  }
92  }
93  $pagingInfo->setTotalCount(sizeof($objects));
94  return $objects;
95  }
96 
97  /**
98  * @see ListController::modifyModel()
99  */
100  protected function modifyModel(&$nodes) {
101  parent::modifyModel($nodes);
102 
103  // add common values
104  $persistenceFacade = $this->getPersistenceFacade();
105  for ($i=0, $count=sizeof($nodes); $i<$count; $i++) {
106  $curNode = $nodes[$i];
107  $curNode->setValue('_displayValue', $curNode->getDisplayValue(), true);
108  $curNode->setValue('_type', $persistenceFacade->getSimpleType($curNode->getType()), true);
109  }
110 
111  // sort
112  $request = $this->getRequest();
113  if ($request->hasValue('sortFieldName')) {
114  $sortDir = $request->hasValue('sortDirection') ? $request->getValue('sortDirection') : 'asc';
115  $sortCriteria = [
116  $request->getValue('sortFieldName') => $sortDir == 'asc' ?
118  ];
119  $comparator = new ObjectComparator($sortCriteria);
120  usort($nodes, [$comparator, 'compare']);
121  }
122  }
123 }
124 ?>
Response holds the response values that are used as output from Controller instances.
Definition: Response.php:20
Request holds the request values that are used as input to Controller instances.
Definition: Request.php:18
HistoryController returns a list of last changed entity instances.
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
ListController is used to load Node lists.
getPersistenceFacade()
Get the PersistenceFacade instance.
Definition: Controller.php:283
setValue($name, $value)
Set a value.
getRequest()
Get the Request instance.
Definition: Controller.php:251
Application controllers.
Definition: namespaces.php:3
initialize(Request $request, Response $response)
getObjects($type, $queryCondition, $sortArray, $pagingInfo)
PersistenceAction values are used to define actions on PersistentObject instances.
ObjectComparator is used to compare persistent objects by given criterias.