Skip to content

Commit

Permalink
feat(comments): attach images by using tempTag to get the comments wo…
Browse files Browse the repository at this point in the history
…rking
  • Loading branch information
mrflos committed May 20, 2024
1 parent 87e831a commit 651401f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 35 deletions.
79 changes: 45 additions & 34 deletions includes/services/CommentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function addCommentIfAuthorized($content, $idComment = '')
} else {
$num = "1";
}
$idComment = "Comment".$num;
$idComment = "Comment" . $num;
} else {
$newComment = false;
}
Expand Down Expand Up @@ -118,12 +118,19 @@ public function addCommentIfAuthorized($content, $idComment = '')
$com['tag'] = $comment['tag'];
$com['commentOn'] = $comment['comment_on'];
$com['rawbody'] = $comment['body'];
$com['body'] = $this->wiki->Format($comment['body']);
// Do the page change in any case (useful for attach or grid)
$oldPage = $GLOBALS['wiki']->GetPageTag();
$oldPageArray = $GLOBALS['wiki']->page;
$GLOBALS['wiki']->tag = $comment['tag'];
$GLOBALS['wiki']->page = $comment;
$com['body'] = $GLOBALS['wiki']->Format($comment['body']);
$GLOBALS['wiki']->tag = $oldPage;
$GLOBALS['wiki']->page = $oldPageArray;
$this->setUserData($comment, 'user', $com);
$this->setUserData($comment, 'owner', $com);
$com['date'] = 'le '.date("d.m.Y à H:i:s", strtotime($comment['time']));
$com['date'] = 'le ' . date("d.m.Y à H:i:s", strtotime($comment['time']));
if ($this->wiki->HasAccess('comment', $comment['tag'])) {
$com['linkcomment'] = $this->wiki->href('pages/'.$comment['tag'].'/comments', 'api');
$com['linkcomment'] = $this->wiki->href('pages/' . $comment['tag'] . '/comments', 'api');
}
if ($this->wiki->UserIsOwner($comment['tag']) || $this->wiki->UserIsAdmin()) {
$com['linkeditcomment'] = $this->wiki->href('edit', $comment['tag']);
Expand All @@ -133,9 +140,9 @@ public function addCommentIfAuthorized($content, $idComment = '')
$com['reponses'] = $this->getCommentList($comment['tag'], false);
$com['parentPage'] = $this->getParentPage($comment['tag']);
$errors = $this->eventDispatcher->yesWikiDispatch($newComment ? 'comment.created' : 'comment.updated', [
'id' => $com['tag'],
'data' => $com,
]);
'id' => $com['tag'],
'data' => $com,
]);
return [
'code' => 200,
'success' => _t('COMMENT_PUBLISHED'),
Expand Down Expand Up @@ -167,22 +174,22 @@ public function delete(string $commentTag): array
$parentPage = $this->getParentPage($commentTag);
$this->pageManager->deleteOrphaned($commentTag);
$errors = $this->eventDispatcher->yesWikiDispatch('comment.deleted', [
'id' => $comment['tag'],
'data' => array_merge($comment, [
'associatedComments' => $comments,
'parentPage' => $parentPage
])
]);
'id' => $comment['tag'],
'data' => array_merge($comment, [
'associatedComments' => $comments,
'parentPage' => $parentPage
])
]);
return $errors;
}

/**
* Load comments for given page.
*
* @param string $tag Page name (Ex : "PagePrincipale") if empty, all comments
* @param bool $bypassAcls
* @return array All comments and their corresponding properties.
*/
* Load comments for given page.
*
* @param string $tag Page name (Ex : "PagePrincipale") if empty, all comments
* @param bool $bypassAcls
* @return array All comments and their corresponding properties.
*/
public function loadComments($tag, bool $bypassAcls = false)
{
$query = 'SELECT * FROM ' . $this->wiki->config['table_prefix'] . 'pages ' . 'WHERE ';
Expand All @@ -193,7 +200,7 @@ public function loadComments($tag, bool $bypassAcls = false)
}
if (!empty($username)) {
$query .=
<<<SQL
<<<SQL
AND (`user` = '{$this->dbService->escape($username)}' OR `owner` = '{$this->dbService->escape($username)}')
SQL;
}
Expand Down Expand Up @@ -234,13 +241,13 @@ public function getCommentList($tag, $first = true, $comments = null)
$com['comments'][$i]['body'] = $this->wiki->Format($comment['body']);
$this->setUserData($comment, 'user', $com['comments'][$i]);
$this->setUserData($comment, 'owner', $com['comments'][$i]);
$com['comments'][$i]['date'] = 'le '.date("d.m.Y à H:i:s", strtotime($comment['time']));
$com['comments'][$i]['date'] = 'le ' . date("d.m.Y à H:i:s", strtotime($comment['time']));
if ($this->wiki->HasAccess('comment', $comment['tag'])) {
$com['comments'][$i]['linkcomment'] = $this->wiki->href('pages/'.$comment['tag'].'/comments', 'api');
$com['comments'][$i]['linkcomment'] = $this->wiki->href('pages/' . $comment['tag'] . '/comments', 'api');
}
if ($this->wiki->UserIsOwner($comment['tag']) || $this->wiki->UserIsAdmin()) {
$com['comments'][$i]['linkeditcomment'] = $this->wiki->href('edit', $comment['tag']);
$com['comments'][$i]['linkdeletecomment'] = $this->wiki->href('comments/'.$comment['tag'].'/delete', 'api');
$com['comments'][$i]['linkdeletecomment'] = $this->wiki->href('comments/' . $comment['tag'] . '/delete', 'api');
}
$com['comments'][$i]['reponses'] = $this->getCommentList($comment['tag'], false);
}
Expand All @@ -251,14 +258,14 @@ public function getCommentList($tag, $first = true, $comments = null)

private function setUserData(array $comment, string $key, array &$data)
{
if (in_array($key, ['user','owner'], true) && !empty($comment[$key])) {
if (in_array($key, ['user', 'owner'], true) && !empty($comment[$key])) {
$data[$key] = $comment[$key];
$data["link$key"] = $this->wiki->href('', $comment[$key]);
$data["{$key}color"] = $this->genColorCodeFromText($comment[$key]);
$data["{$key}picture"] =
!empty($this->wiki->config['default_comment_avatar'])
? $this->wiki->config['default_comment_avatar']
: "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' fill='".str_replace('#', '%23', $data["{$key}color"])."' class='bi bi-person-circle' viewBox='0 0 16 16'%3E%3Cpath d='M11 6a3 3 0 1 1-6 0 3 3 0 0 1 6 0z'/%3E%3Cpath fill-rule='evenodd' d='M0 8a8 8 0 1 1 16 0A8 8 0 0 1 0 8zm8-7a7 7 0 0 0-5.468 11.37C3.242 11.226 4.805 10 8 10s4.757 1.225 5.468 2.37A7 7 0 0 0 8 1z'/%3E%3C/svg%3E";
: "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' fill='" . str_replace('#', '%23', $data["{$key}color"]) . "' class='bi bi-person-circle' viewBox='0 0 16 16'%3E%3Cpath d='M11 6a3 3 0 1 1-6 0 3 3 0 0 1 6 0z'/%3E%3Cpath fill-rule='evenodd' d='M0 8a8 8 0 1 1 16 0A8 8 0 0 1 0 8zm8-7a7 7 0 0 0-5.468 11.37C3.242 11.226 4.805 10 8 10s4.757 1.225 5.468 2.37A7 7 0 0 0 8 1z'/%3E%3C/svg%3E";
}
}

Expand All @@ -279,10 +286,12 @@ public function getCommentForm($tag)
}
$page = $this->pageManager->getOne($tag);
$commentOn = !empty($page['comment_on']) ? $page['comment_on'] : $page['tag'];
$tempTag = ($this->wiki->config['temp_tag_for_entry_creation'] ?? null) . '_' . bin2hex(random_bytes(10));
$options = [
'pagetag' => $commentOn,
'formlink' => $this->wiki->href('comments', 'api'),
'hashcash' => $hashCashCode
'hashcash' => $hashCashCode,
'tempTag' => $tempTag
];
} else {
$options['alerts'][] = [
Expand Down Expand Up @@ -361,25 +370,25 @@ public function genColorCodeFromText($text, $min_brightness = 100, $spec = 10)

$hash = md5($text); //Gen hash of text
$colors = array();
for ($i = 0;$i < 3;$i++) {
$colors[$i] = max(array(round(((hexdec(substr($hash, $spec * $i, $spec))) / hexdec(str_pad('', $spec, 'F'))) * 255),$min_brightness));
for ($i = 0; $i < 3; $i++) {
$colors[$i] = max(array(round(((hexdec(substr($hash, $spec * $i, $spec))) / hexdec(str_pad('', $spec, 'F'))) * 255), $min_brightness));
} //convert hash into 3 decimal values between 0 and 255

if ($min_brightness > 0) { //only check brightness requirements if min_brightness is about 100
while (array_sum($colors) / 3 < $min_brightness) { //loop until brightness is above or equal to min_brightness
for ($i = 0;$i < 3;$i++) {
for ($i = 0; $i < 3; $i++) {
$colors[$i] += 10;
}
}
} //increase each color by 10
} //increase each color by 10

$output = '';

for ($i = 0;$i < 3;$i++) {
for ($i = 0; $i < 3; $i++) {
$output .= str_pad(dechex($colors[$i]), 2, 0, STR_PAD_LEFT);
} //convert each color to hex and append to output

return '#'.$output;
return '#' . $output;
}

public function sendEmailAfterCreate(Event $event)
Expand Down Expand Up @@ -462,9 +471,11 @@ protected function extractTaggedUsernamesFromContent(array $comment, $loggedUser
// filter
$filteredUsers = [];
foreach ($users as $user) {
if ($user['email'] != $loggedUser['email'] &&
if (
$user['email'] != $loggedUser['email'] &&
(empty($owner) || ($user['email'] != $owner['email'])) &&
!in_array($user['name'], array_keys($filteredUsers))) {
!in_array($user['name'], array_keys($filteredUsers))
) {
$filteredUsers[$user['name']] = $user;
}
}
Expand Down
2 changes: 1 addition & 1 deletion templates/comment-form.twig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<input type="hidden" name="pagetag" value="{{ pagetag }}">
<div class="form-group">
<label>{{ _t('YW_ADD_COMMENT') }}</label>
{{ renderAction('aceditor', { name: "body", placeholder: _t('YW_WRITE_COMMENT_HERE'), rows: 3})|raw }}
{{ renderAction('aceditor', { name: "body", placeholder: _t('YW_WRITE_COMMENT_HERE'), rows: 3, tempTag: tempTag })|raw }}
</div>

{{ hashcash|raw }}
Expand Down

0 comments on commit 651401f

Please sign in to comment.