[BUGFIX] Fix error listing in belog
[Packages/TYPO3.CMS.git] / typo3 / sysext / belog / Classes / Domain / Repository / LogEntryRepository.php
1 <?php
2 namespace TYPO3\CMS\Belog\Domain\Repository;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2012 Christian Kuhn <lolli@schwarzbu.ch>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the textfile GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 * This script is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * This copyright notice MUST APPEAR in all copies of the script!
27 ***************************************************************/
28 /**
29 * Sys log entry repository
30 *
31 * @author Christian Kuhn <lolli@schwarzbu.ch>
32 */
33 class LogEntryRepository extends \TYPO3\CMS\Extbase\Persistence\Repository {
34
35 /**
36 * backend users, with UID as key
37 *
38 * @var array
39 */
40 protected $beUserList = array();
41
42 /**
43 * Initialize some local variables to be used during creation of objects
44 *
45 * @return void
46 */
47 public function initializeObject() {
48 $this->beUserList = \TYPO3\CMS\Backend\Utility\BackendUtility::getUserNames();
49 /** @var $defaultQuerySettings \TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface */
50 $defaultQuerySettings = $this->objectManager->create('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\QuerySettingsInterface');
51 $defaultQuerySettings->setRespectStoragePage(FALSE);
52 $this->setDefaultQuerySettings($defaultQuerySettings);
53 }
54
55 /**
56 * Finds all log entries that match all given constraints.
57 *
58 * @param \TYPO3\CMS\Belog\Domain\Model\Constraint $constraint
59 * @return \TYPO3\CMS\Extbase\Persistence\QueryResultInterface<\TYPO3\CMS\Belog\Domain\Model\LogEntry>
60 */
61 public function findByConstraint(\TYPO3\CMS\Belog\Domain\Model\Constraint $constraint) {
62 $query = $this->createQuery();
63 $queryConstraints = $this->createQueryConstraints($query, $constraint);
64 if (!empty($queryConstraints)) {
65 $query->matching($query->logicalAnd($queryConstraints));
66 }
67 $query->setOrderings(array('uid' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_DESCENDING));
68 $query->setLimit($constraint->getNumber());
69 return $query->execute();
70 }
71
72 /**
73 * Create an array of query constraints from constraint object
74 *
75 * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query
76 * @param \TYPO3\CMS\Belog\Domain\Model\Constraint $constraint
77 * @return array<\TYPO3\CMS\Extbase\Persistence\Generic\Qom\ConstraintInterface>
78 */
79 protected function createQueryConstraints(\TYPO3\CMS\Extbase\Persistence\QueryInterface $query, \TYPO3\CMS\Belog\Domain\Model\Constraint $constraint) {
80 $queryConstraints = array();
81 // User / group handling
82 $this->addUsersAndGroupsToQueryConstraints($constraint, $query, $queryConstraints);
83 // Workspace
84 if ($constraint->getWorkspaceUid() != \TYPO3\CMS\Belog\Domain\Model\Workspace::UID_ANY_WORKSPACE) {
85 $queryConstraints[] = $query->equals('workspace', $constraint->getWorkspaceUid());
86 }
87 // Action (type):
88 if ($constraint->getAction() > 0) {
89 $queryConstraints[] = $query->equals('type', $constraint->getAction());
90 } elseif ($constraint->getAction() == -1) {
91 $queryConstraints[] = $query->in('error', array(-1,1,2,3));
92 }
93 // Start / endtime handling: The timestamp calculation was already done
94 // in the controller, since we need those calculated values in the view as well.
95 $queryConstraints[] = $query->greaterThanOrEqual('tstamp', $constraint->getStartTimestamp());
96 $queryConstraints[] = $query->lessThan('tstamp', $constraint->getEndTimestamp());
97 // Page and level constraint if in page context
98 $this->addPageTreeConstraintsToQuery($constraint, $query, $queryConstraints);
99 return $queryConstraints;
100 }
101
102 /**
103 * Adds constraints for the page(s) to the query; this could be one single page or a whole subtree beneath a given
104 * page.
105 *
106 * @param \TYPO3\CMS\Belog\Domain\Model\Constraint $constraint
107 * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query
108 * @param array &$queryConstraints the query constraints to add to, will be modified
109 * @return void
110 */
111 protected function addPageTreeConstraintsToQuery(\TYPO3\CMS\Belog\Domain\Model\Constraint $constraint, \TYPO3\CMS\Extbase\Persistence\QueryInterface $query, array &$queryConstraints) {
112 if (!$constraint->getIsInPageContext()) {
113 return;
114 }
115 $pageIds = array();
116 // check if we should get a whole tree of pages and not only a single page
117 if ($constraint->getDepth() > 0) {
118 /** @var $pageTree \TYPO3\CMS\Backend\Tree\View\PageTreeView */
119 $pageTree = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Backend\\Tree\\View\\PageTreeView');
120 $pageTree->init('AND ' . $GLOBALS['BE_USER']->getPagePermsClause(1));
121 $pageTree->makeHTML = 0;
122 $pageTree->fieldArray = array('uid');
123 $pageTree->getTree($constraint->getPageId(), $constraint->getDepth());
124 $pageIds = $pageTree->ids;
125 }
126 $pageIds[] = $constraint->getPageId();
127 $queryConstraints[] = $query->in('eventPid', $pageIds);
128 }
129
130 /**
131 * Adds users and groups to the query constraints.
132 *
133 * @param \TYPO3\CMS\Belog\Domain\Model\Constraint $constraint
134 * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query
135 * @param array &$queryConstraints the query constraints to add to, will be modified
136 * @return void
137 */
138 protected function addUsersAndGroupsToQueryConstraints(\TYPO3\CMS\Belog\Domain\Model\Constraint $constraint, \TYPO3\CMS\Extbase\Persistence\QueryInterface $query, array &$queryConstraints) {
139 $userOrGroup = $constraint->getUserOrGroup();
140 if ($userOrGroup === '') {
141 return;
142 }
143 // Constraint for a group
144 if (substr($userOrGroup, 0, 3) === 'gr-') {
145 $groupId = intval(substr($userOrGroup, 3));
146 $userIds = array();
147 foreach ($this->beUserList as $userId => $userData) {
148 if (\TYPO3\CMS\Core\Utility\GeneralUtility::inList($userData['usergroup_cached_list'], $groupId)) {
149 $userIds[] = $userId;
150 }
151 }
152 if (!empty($userIds)) {
153 $queryConstraints[] = $query->in('userid', $userIds);
154 } else {
155 // If there are no group members -> use -1 as constraint to not find anything
156 $queryConstraints[] = $query->in('userid', array(-1));
157 }
158 } elseif (substr($userOrGroup, 0, 3) === 'us-') {
159 $queryConstraints[] = $query->equals('userid', intval(substr($userOrGroup, 3)));
160 } elseif ($userOrGroup === '-1') {
161 $queryConstraints[] = $query->equals('userid', intval($GLOBALS['BE_USER']->user['uid']));
162 }
163 }
164
165 }
166
167
168 ?>