-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlannerUtility.php
207 lines (182 loc) · 7.65 KB
/
PlannerUtility.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
declare(strict_types=1);
namespace Xima\XimaTypo3ContentPlanner\Utility;
use TYPO3\CMS\Core\DataHandling\DataHandler;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\StringUtility;
use Xima\XimaTypo3ContentPlanner\Domain\Model\BackendUser;
use Xima\XimaTypo3ContentPlanner\Domain\Model\Status;
use Xima\XimaTypo3ContentPlanner\Domain\Repository\BackendUserRepository;
use Xima\XimaTypo3ContentPlanner\Domain\Repository\CommentRepository;
use Xima\XimaTypo3ContentPlanner\Domain\Repository\RecordRepository;
use Xima\XimaTypo3ContentPlanner\Domain\Repository\StatusRepository;
class PlannerUtility
{
/**
* Simple function to get a list of all available status.
* \Xima\XimaTypo3ContentPlanner\Utility\PlannerUtility::getListOfStatus();
*
* @return array
* @throws \Doctrine\DBAL\Exception
*/
public static function getListOfStatus(): array
{
return GeneralUtility::makeInstance(StatusRepository::class)->findAll();
}
/**
* Simple function to update the status of a record.
* \Xima\XimaTypo3ContentPlanner\Utility\PlannerUtility::updateStatusForRecord('pages', 1, 'In Progress', 'admin');
*
* @param string $table
* @param int $uid
* @param \Xima\XimaTypo3ContentPlanner\Domain\Model\Status|int|string $status
* @param \Xima\XimaTypo3ContentPlanner\Domain\Model\BackendUser|int|string|null $assignee
* @throws \Doctrine\DBAL\Exception
*/
public static function updateStatusForRecord(string $table, int $uid, Status|int|string $status, BackendUser|int|string|null $assignee = null): void
{
self::preCheckRecordTable($table, $uid);
$statusId = $status;
if ($status instanceof Status) {
$statusId = $status->getUid();
} elseif (is_string($status)) {
$statusRepository = GeneralUtility::makeInstance(StatusRepository::class);
$statusId = $statusRepository->findByTitle($status)->getUid();
}
if (!$statusId) {
throw new \InvalidArgumentException('Status "' . $statusId . '" is not a valid content planner status.', 9220772840);
}
$assigneeId = $assignee;
if ($assignee instanceof BackendUser) {
$assigneeId = $assignee->getUid();
} elseif (is_string($assignee)) {
$backendUserRepository = GeneralUtility::makeInstance(BackendUserRepository::class);
$assigneeId = $backendUserRepository->findByUsername($assignee);
if ($assigneeId) {
$assigneeId = $assigneeId['uid'];
}
}
GeneralUtility::makeInstance(RecordRepository::class)->updateStatusByUid($table, $uid, $statusId, $assigneeId);
}
/**
* Simple function to get the status of a record.
* \Xima\XimaTypo3ContentPlanner\Utility\PlannerUtility::getStatusOfRecord('pages', 1);
*
* @param string $table
* @param int $uid
* @return \Xima\XimaTypo3ContentPlanner\Domain\Model\Status|null
* @throws \Doctrine\DBAL\Exception
*/
public static function getStatusOfRecord(string $table, int $uid): ?Status
{
$record = self::preCheckRecordTable($table, $uid);
return GeneralUtility::makeInstance(StatusRepository::class)->findByUid($record['tx_ximatypo3contentplanner_status']);
}
/**
* Simple function to get a status.
* \Xima\XimaTypo3ContentPlanner\Utility\PlannerUtility::getStatus('Needs review');
*
* @param int|string $identifier
* @return \Xima\XimaTypo3ContentPlanner\Domain\Model\Status|null
*/
public static function getStatus(int|string $identifier): ?Status
{
$statusRepository = GeneralUtility::makeInstance(StatusRepository::class);
if (is_string($identifier)) {
return $statusRepository->findByTitle($identifier);
}
return $statusRepository->findByUid($identifier);
}
/**
* Simple function to fetch all comments of a record.
* \Xima\XimaTypo3ContentPlanner\Utility\PlannerUtility::getCommentsOfRecord('pages', 1);
*
* @param string $table
* @param int $uid
* @param bool $raw
* @return array
* @throws \Doctrine\DBAL\Exception
*/
public static function getCommentsOfRecord(string $table, int $uid, bool $raw = false): array
{
self::preCheckRecordTable($table, $uid);
return GeneralUtility::makeInstance(CommentRepository::class)->findAllByRecord($uid, $table, $raw);
}
/**
* Simple function to add comment(s) to a content planner record.
* \Xima\XimaTypo3ContentPlanner\Utility\PlannerUtility::addCommentsToRecord('pages', 1, 'New Comment', 'admin');
*
* @param string $table
* @param int $uid
* @param array<int,string>|string $comments
* @param BackendUser|int|string $author
* @throws \Doctrine\DBAL\Exception
*/
public static function addCommentsToRecord(string $table, int $uid, array|string $comments, BackendUser|int|string|null $author = null): void
{
$record = self::preCheckRecordTable($table, $uid);
$authorId = $author;
if ($author instanceof BackendUser) {
$authorId = $author->getUid();
} elseif (is_string($author)) {
$backendUserRepository = GeneralUtility::makeInstance(BackendUserRepository::class);
$authorId = $backendUserRepository->findByUsername($author)['uid'];
}
if (!$authorId) {
throw new \InvalidArgumentException('Author "' . $authorId . '" is not a valid backend user.', 4723563571);
}
if (!is_array($comments)) {
$comments = [$comments];
}
$pid = $table === 'pages' ? $record['uid'] : $record['pid'];
$newIds = [];
foreach ($comments as $comment) {
$newId = StringUtility::getUniqueId('NEW');
$data['tx_ximatypo3contentplanner_comment'][$newId] = [
'foreign_uid' => $uid,
'foreign_table' => $table,
'content' => $comment,
'pid' => $pid,
'author' => $authorId,
];
$newIds[] = $newId;
}
$data[$table][$uid]['tx_ximatypo3contentplanner_comments'] = count($newIds);
/** @var DataHandler $dataHandler */
$dataHandler = GeneralUtility::makeInstance(DataHandler::class);
$dataHandler->start($data, []);
$dataHandler->process_datamap();
}
/**
* Simple function to clear all comment(s) of a content planner record.
* \Xima\XimaTypo3ContentPlanner\Utility\PlannerUtility::clearCommentsOfRecord('pages', 1);
*
* @param string $table
* @param int $uid
* @param string|null $like
* @throws \Doctrine\DBAL\Exception
*/
public static function clearCommentsOfRecord(string $table, int $uid, ?string $like = null): void
{
self::preCheckRecordTable($table, $uid);
$commentsRepository = GeneralUtility::makeInstance(CommentRepository::class);
$commentsRepository->deleteAllCommentsByRecord($uid, $table, $like);
}
/**
* @param string $table
* @param int $uid
* @return array|bool
* @throws \Doctrine\DBAL\Exception
*/
private static function preCheckRecordTable(string $table, int $uid): array|bool
{
if (!ExtensionUtility::isRegisteredRecordTable($table)) {
throw new \InvalidArgumentException('Table "' . $table . '" is not a valid content planner record table.', 9518991865);
}
$record = GeneralUtility::makeInstance(RecordRepository::class)->findByUid($table, $uid);
if (!$record) {
throw new \InvalidArgumentException('Record "' . $uid . '" in table "' . $table . '" not found.', 4064696674);
}
return $record;
}
}