Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(CommentsField): create #1044

Merged
merged 4 commits into from
Jan 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions actions/EditConfigAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class EditConfigAction extends YesWikiAction
'default_write_acl' => 'access',
'default_comment_acl' => 'access',
'comments_activated' => 'access',
'comments_handler' => 'access',
'password_for_editing' => 'access',
'password_for_editing_message' => 'access',
'allow_doubleclic' => 'access',
Expand Down
1 change: 1 addition & 0 deletions includes/YesWikiInit.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ public function getConfig($wakkaConfig = array())
'default_comment_acl' => 'comments-closed',
'default_comment_acl_updated' => false,
'comments_activated' => true,
'comments_handler' => 'yeswiki',
'preview_before_save' => 0,
'allow_raw_html' => true,
'disable_wiki_links' => false,
Expand Down
1 change: 1 addition & 0 deletions lang/yeswiki_ca.php
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,7 @@
// 'EDIT_CONFIG_HINT_DEFAULT_READ_ACL' => 'Droits de lecture par défaut des pages (* pour tous, + pour personnes identifiées, @admins pour groupe admin)',
// 'EDIT_CONFIG_HINT_DEFAULT_COMMENT_ACL' => 'Droits de commentaires par défaut des pages (comments-closed pour fermer, + pour personnes identifiées, @admins pour groupe admin)',
// 'EDIT_CONFIG_HINT_COMMENTS_ACTIVATED' => 'Commentaires activés (true ou false)',
// 'EDIT_CONFIG_HINT_COMMENTS_HANDLER' => 'Type de commentaires utilisés (\'\'/embedded_humhub/external_humhub/yeswiki)',
// 'EDIT_CONFIG_HINT_DEBUG' => 'Activer le mode de debug (yes ou no)',
// 'EDIT_CONFIG_HINT_DEFAULT_LANGUAGE' => 'Langue par défaut (fr ou en ou ... auto = langue du navigateur)',
// 'EDIT_CONFIG_HINT_CONTACT_FROM' => 'Remplacer le mail utilisé comme expéditeur des messages',
Expand Down
1 change: 1 addition & 0 deletions lang/yeswiki_en.php
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,7 @@
'EDIT_CONFIG_HINT_DEFAULT_READ_ACL' => 'Default read pages access control',
'EDIT_CONFIG_HINT_DEFAULT_COMMENT_ACL' => 'Default comment pages access control',
'EDIT_CONFIG_HINT_COMMENTS_ACTIVATED' => 'Comments activated (true or false)',
// 'EDIT_CONFIG_HINT_COMMENTS_HANDLER' => 'Type de commentaires utilisés (\'\'/embedded_humhub/external_humhub/yeswiki)',
'EDIT_CONFIG_HINT_DEBUG' => 'Activate debug mode (\'yes\'/\'no\')',
'EDIT_CONFIG_HINT_DEFAULT_LANGUAGE' => 'Default language (fr or en or ... auto = navigator\'s language)',
'EDIT_CONFIG_HINT_CONTACT_FROM' => 'E-mail address used as sender for messages from entries',
Expand Down
1 change: 1 addition & 0 deletions lang/yeswiki_es.php
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,7 @@
// 'EDIT_CONFIG_HINT_DEFAULT_READ_ACL' => 'Droits de lecture par défaut des pages (* pour tous, + pour personnes identifiées, @admins pour groupe admin)',
// 'EDIT_CONFIG_HINT_DEFAULT_COMMENT_ACL' => 'Droits de commentaires par défaut des pages (comments-closed pour fermer, + pour personnes identifiées, @admins pour groupe admin)',
// 'EDIT_CONFIG_HINT_COMMENTS_ACTIVATED' => 'Commentaires activés (true ou false)',
// 'EDIT_CONFIG_HINT_COMMENTS_HANDLER' => 'Type de commentaires utilisés (\'\'/embedded_humhub/external_humhub/yeswiki)',
// 'EDIT_CONFIG_HINT_DEBUG' => 'Activer le mode de debug (yes ou no)',
// 'EDIT_CONFIG_HINT_DEFAULT_LANGUAGE' => 'Langue par défaut (fr ou en ou ... auto = langue du navigateur)',
// 'EDIT_CONFIG_HINT_CONTACT_FROM' => 'Remplacer le mail utilisé comme expéditeur des messages',
Expand Down
1 change: 1 addition & 0 deletions lang/yeswiki_fr.php
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,7 @@
'EDIT_CONFIG_HINT_DEFAULT_READ_ACL' => 'Droits de lecture par défaut des pages (* pour tous, + pour personnes identifiées, @admins pour groupe admin)',
'EDIT_CONFIG_HINT_DEFAULT_COMMENT_ACL' => 'Droits de commentaires par défaut des pages (comments-closed pour ferme, * pour tous, + pour personnes identifiées, @admins pour groupe admin)',
'EDIT_CONFIG_HINT_COMMENTS_ACTIVATED' => 'Commentaires activés (true ou false)',
'EDIT_CONFIG_HINT_COMMENTS_HANDLER' => 'Type de commentaires utilisés (\'\'/embedded_humhub/external_humhub/yeswiki)',
'EDIT_CONFIG_HINT_DEBUG' => 'Activer le mode de debug (yes ou no)',
'EDIT_CONFIG_HINT_DEFAULT_LANGUAGE' => 'Langue par défaut (fr ou en ou ... auto = langue du navigateur)',
'EDIT_CONFIG_HINT_CONTACT_FROM' => 'Remplacer le mail utilisé comme expéditeur des messages',
Expand Down
1 change: 1 addition & 0 deletions lang/yeswiki_nl.php
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,7 @@
// 'EDIT_CONFIG_HINT_DEFAULT_READ_ACL' => 'Droits de lecture par défaut des pages (* pour tous, + pour personnes identifiées, @admins pour groupe admin)',
// 'EDIT_CONFIG_HINT_DEFAULT_COMMENT_ACL' => 'Droits de commentaires par défaut des pages (comments-closed pour fermer, + pour personnes identifiées, @admins pour groupe admin)',
// 'EDIT_CONFIG_HINT_COMMENTS_ACTIVATED' => 'Commentaires activés (true ou false)',
// 'EDIT_CONFIG_HINT_COMMENTS_HANDLER' => 'Type de commentaires utilisés (\'\'/embedded_humhub/external_humhub/yeswiki)',
// 'EDIT_CONFIG_HINT_DEBUG' => 'Activer le mode de debug (yes ou no)',
// 'EDIT_CONFIG_HINT_DEFAULT_LANGUAGE' => 'Langue par défaut (fr ou en ou ... auto = langue du navigateur)',
// 'EDIT_CONFIG_HINT_CONTACT_FROM' => 'Remplacer le mail utilisé comme expéditeur des messages',
Expand Down
1 change: 1 addition & 0 deletions lang/yeswiki_pt.php
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,7 @@
// 'EDIT_CONFIG_HINT_DEFAULT_READ_ACL' => 'Droits de lecture par défaut des pages (* pour tous, + pour personnes identifiées, @admins pour groupe admin)',
// 'EDIT_CONFIG_HINT_DEFAULT_COMMENT_ACL' => 'Droits de commentaires par défaut des pages (comments-closed pour fermer, + pour personnes identifiées, @admins pour groupe admin)',
// 'EDIT_CONFIG_HINT_COMMENTS_ACTIVATED' => 'Commentaires activés (true ou false)',
// 'EDIT_CONFIG_HINT_COMMENTS_HANDLER' => 'Type de commentaires utilisés (\'\'/embedded_humhub/external_humhub/yeswiki)',
// 'EDIT_CONFIG_HINT_DEBUG' => 'Activer le mode de debug (yes ou no)',
// 'EDIT_CONFIG_HINT_DEFAULT_LANGUAGE' => 'Langue par défaut (fr ou en ou ... auto = langue du navigateur)',
// 'EDIT_CONFIG_HINT_CONTACT_FROM' => 'Remplacer le mail utilisé comme expéditeur des messages',
Expand Down
173 changes: 153 additions & 20 deletions tools/bazar/fields/AclField.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,57 +3,136 @@
namespace YesWiki\Bazar\Field;

use Psr\Container\ContainerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use YesWiki\Bazar\Field\BazarField;
use YesWiki\Core\Service\AclService;

/**
* @Field({"acls"})
*/
class AclField extends BazarField
{
protected $aclService;
protected $askIfActivateComments;
protected $entryReadRight;
protected $entryWriteRight;
protected $entryCommentRight;
protected $params;
protected $options;

public const OPTIONS = [
'oui' => 'YES',
'non' => 'NO',
];
public const OPTION_YES = 'oui';

protected const FIELD_ENTRY_READ_RIGHT = 1;
protected const FIELD_ENTRY_WRITE_RIGHT = 2;
protected const FIELD_ENTRY_COMMENT_RIGHT = 3;
protected const FIELD_LABEL = 4;
protected const FIELD_NAME = 6;
protected const FIELD_ASK_IF_ACTIVATE_COMMENTS = 7;

public function __construct(array $values, ContainerInterface $services)
{
parent::__construct($values, $services);
$this->askIfActivateComments = in_array($this->filterNotEmptyString($values, self::FIELD_ASK_IF_ACTIVATE_COMMENTS, 'no'), [1,true,'1','true','yes'], true);

$this->name = $this->filterNotEmptyString($values, self::FIELD_NAME, $this->askIfActivateComments ? 'bf_commentaires' : null);
$this->propertyName = $this->name;

$this->default = $this->filterNotEmptyString(['default'=>$this->default], 'default', '');
$this->default = in_array($this->default, [...array_keys(self::OPTIONS),...['']], true) ? $this->default : '';

$this->label = $this->filterNotEmptyString($values, self::FIELD_LABEL, _t('BAZ_ACTIVATE_COMMENTS'));
$this->hint = $this->filterNotEmptyString(['hint'=>$this->hint], 'hint', _t('BAZ_ACTIVATE_COMMENTS_HINT'));

$this->entryReadRight = $this->filterNotEmptyString($values, self::FIELD_ENTRY_READ_RIGHT, '*');
$this->entryWriteRight = $this->filterNotEmptyString($values, self::FIELD_ENTRY_WRITE_RIGHT, '%');
$this->entryCommentRight = $this->filterNotEmptyString($values, self::FIELD_ENTRY_COMMENT_RIGHT, 'comments-closed');

$this->name = null;
$this->label = null ;
$this->propertyName = null;
$this->entryReadRight = $values[self::FIELD_ENTRY_READ_RIGHT];
$this->entryWriteRight = $values[self::FIELD_ENTRY_WRITE_RIGHT];
$this->entryCommentRight = $values[self::FIELD_ENTRY_COMMENT_RIGHT];
$this->aclService = $this->getService(AclService::class);
$this->params = $this->getService(ParameterBagInterface::class);

$this->options = null;
}

protected function filterNotEmptyString(array $data, string $key, ?string $default): ?string
{
return (
isset($data[$key]) &&
is_string($data[$key]) &&
!empty(trim($data[$key]))
)
? trim($data[$key])
: $default;
}

protected function renderInput($entry)
{
return "";
return ($this->askIfActivateComments)
? $this->render('@bazar/inputs/comments.twig', [
'value' => $this->getValue($entry),
'options' => $this->getOptions(),
'showAlertForCommentsNotActivated' =>
in_array($this->getCommentsType(), ['','yeswiki']) &&
$this->params->get('comments_activated') !== true
])
: '' ;
}

public function formatValuesBeforeSave($entry)
{
if (empty($GLOBALS['wiki']->LoadAcl($entry['id_fiche'], 'read', false)['list'])) {
$GLOBALS['wiki']->SaveAcl($entry['id_fiche'], 'read', $this->replaceWithCreator($this->entryReadRight, $entry));
}
if (empty($GLOBALS['wiki']->LoadAcl($entry['id_fiche'], 'write', false)['list'])) {
$GLOBALS['wiki']->SaveAcl($entry['id_fiche'], 'write', $this->replaceWithCreator($this->entryWriteRight, $entry));
if (empty($this->aclService->load($entry['id_fiche'], 'read', false)['list'])) {
$this->aclService->save($entry['id_fiche'], 'read', $this->replaceWithCreator($this->entryReadRight, $entry));
}
if (empty($GLOBALS['wiki']->LoadAcl($entry['id_fiche'], 'comment', false)['list'])) {
$GLOBALS['wiki']->SaveAcl($entry['id_fiche'], 'comment', $this->replaceWithCreator($this->entryCommentRight, $entry));
if (empty($this->aclService->load($entry['id_fiche'], 'write', false)['list'])) {
$this->aclService->save($entry['id_fiche'], 'write', $this->replaceWithCreator($this->entryWriteRight, $entry));
}

return [];
if ($this->askIfActivateComments) {
$commentsType = $this->getCommentsType();
switch ($commentsType) {
case 'yeswiki':
case '':
if ($this->getValue($entry) === self::OPTION_YES) {
$this->openComments($entry);
break;
}
// no break
case 'external_humhub':
case 'embedded_humhub':
case 'discourse':
default:
$this->closeComments($entry);
break;
}
return parent::formatValuesBeforeSave($entry);
} else {
if (empty($this->aclService->load($entry['id_fiche'], 'comment', false)['list'])) {
$this->aclService->save($entry['id_fiche'], 'comment', $this->replaceWithCreator($this->entryCommentRight, $entry));
}
return (!empty($this->propertyName))
? [
'fields-to-remove' => [
$this->propertyName
]
]
: [];
}
}

protected function renderStatic($entry)
{
return "";
}

protected function getValue($entry)
{
$value = parent::getValue($entry);
return in_array($value, array_keys(self::OPTIONS), true) ? $value : '';
}

private function replaceWithCreator($right, $entry)
{
// le signe # ou le mot user indiquent que le owner de la fiche sera utilisé pour les droits
Expand All @@ -62,16 +141,70 @@ private function replaceWithCreator($right, $entry)
}
return $right;
}


public function getCommentsType(): string
{
$commentsType = $this->params->get('comments_handler');

return (
empty($commentsType) ||
!is_string($commentsType)
)
? ''
: $commentsType;
}

public function getOptions()
{
if (is_null($this->options)) {
// force usage of predefined values with translation
$this->options = array_map('_t', self::OPTIONS);
}
return $this->options;
}

public function getAskIfActivateComments()
{
return $this->askIfActivateComments;
}

protected function closeComments($entry)
{
if (!empty($entry['id_fiche']) &&
is_string($entry['id_fiche']) &&
!empty(trim($entry['id_fiche']))) {
$this->aclService->save($entry['id_fiche'], 'comment', 'comments-closed');
}
}

protected function openComments($entry)
{
if (!empty($entry['id_fiche']) &&
is_string($entry['id_fiche']) &&
!empty(trim($entry['id_fiche']))) {
$defaultRights = (empty($this->entryCommentRight) || $this->entryCommentRight === 'comments-closed')
? '+' //backup
: $this->entryCommentRight;
$this->aclService->save($entry['id_fiche'], 'comment', $defaultRights);
}
}

// change return of this method to keep compatible with php 7.3 (mixed is not managed)
#[\ReturnTypeWillChange]
public function jsonSerialize()
{
return [
'type' => $this->getType(),
'entryReadRight' => $this->entryReadRight,
'entryWriteRight' => $this->entryWriteRight,
'entryCommentRight' => $this->entryCommentRight,
'type' => $this->getType(),
'entryReadRight' => $this->entryReadRight,
'entryWriteRight' => $this->entryWriteRight,
'entryCommentRight' => $this->entryCommentRight,
'name' => $this->getName(),
'id' => $this->getPropertyName(),
'propertyName' => $this->getPropertyName(),
'label' => $this->getLabel(),
'hint' => $this->getHint(),
'default' => $this->getDefault(),
'askIfActivateComments' => $this->getAskIfActivateComments(),
];
}
}
3 changes: 2 additions & 1 deletion tools/bazar/fields/ReactionsField.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ protected function renderStatic($entry)
'userReaction' => $userReactions,
'oldIdsUserReactions' => $oldIdsUserReactions,
'maxReaction' => self::MAX_REACTIONS,
'pageTag' => $currentEntryTag
'pageTag' => $currentEntryTag,
'showCommentMessage' => !empty($entry['bf_commentaires']) && $entry['bf_commentaires'] == 'oui'
]);
}

Expand Down
9 changes: 9 additions & 0 deletions tools/bazar/lang/bazar_ca.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,15 @@
// 'BAZ_CLOSE_THIS_WINDOW' => 'Fermer cette fenêtre',
// 'BAZ_BOOKMARKLET_LABEL' => 'Créer une fiche %{form}',

// fields/CommentsField.php
// 'BAZ_ACTIVATE_COMMENTS' => 'Activer les commentaires sur cette fiche ?',
// 'BAZ_ACTIVATE_COMMENTS_HINT' => 'Droits mis à jour lors de l\'enregistrement de la fiche',
// 'BAZ_COMMENTS_INFO_DISCOURSE' => 'Les commentaires sont gérés par la plateforme discourse : mais ça n\'est pas encore fonctionnel !',
// 'BAZ_COMMENTS_INFO_HUMHUB_EMBEDDED' => 'Les commentaires sont gérés par la plateforme sociale HumHub (Humhub intégré par YesWiki)',
// 'BAZ_COMMENTS_INFO_HUMHUB_EXTERNAL' => 'Les commentaires sont gérés par la plateforme sociale HumHub (YesWiki intégré par Humhub)',
// 'BAZ_COMMENTS_INFO_UNKNOWN' => 'Les commentaires ne sont pas gérés car le type de réglage des commentaires est inconnu',
// 'BAZ_COMMENTS_NOT_ACTIVATED' => 'Les commentaires sont gérés par YesWiki mais ceux-ci ne sont pas activés.<br/>Se rendre sur la page GererConfig pour activer les commentaires (paramètre `comments_activated`)',

// fields/FileField.php
// 'BAZ_FILE_ALREADY_EXISTING' => 'fichier déja existant',
// 'BAZ_NOT_AUTHORIZED_FILE' => 'fichier non autorise',
Expand Down
9 changes: 9 additions & 0 deletions tools/bazar/lang/bazar_en.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,15 @@
// 'BAZ_CLOSE_THIS_WINDOW' => 'Fermer cette fen&ecirc;tre',
'BAZ_BOOKMARKLET_LABEL' => 'Create an entry %{form}',

// fields/CommentsField.php
// 'BAZ_ACTIVATE_COMMENTS' => 'Activer les commentaires sur cette fiche ?',
// 'BAZ_ACTIVATE_COMMENTS_HINT' => 'Droits mis à jour lors de l\'enregistrement de la fiche',
// 'BAZ_COMMENTS_INFO_DISCOURSE' => 'Les commentaires sont gérés par la plateforme discourse : mais ça n\'est pas encore fonctionnel !',
// 'BAZ_COMMENTS_INFO_HUMHUB_EMBEDDED' => 'Les commentaires sont gérés par la plateforme sociale HumHub (Humhub intégré par YesWiki)',
// 'BAZ_COMMENTS_INFO_HUMHUB_EXTERNAL' => 'Les commentaires sont gérés par la plateforme sociale HumHub (YesWiki intégré par Humhub)',
// 'BAZ_COMMENTS_INFO_UNKNOWN' => 'Les commentaires ne sont pas gérés car le type de réglage des commentaires est inconnu',
// 'BAZ_COMMENTS_NOT_ACTIVATED' => 'Les commentaires sont gérés par YesWiki mais ceux-ci ne sont pas activés.<br/>Se rendre sur la page GererConfig pour activer les commentaires (paramètre `comments_activated`)',

// fields/FileField.php
// 'BAZ_FILE_ALREADY_EXISTING' => 'fichier déja existant',
// 'BAZ_NOT_AUTHORIZED_FILE' => 'fichier non autorise',
Expand Down
9 changes: 9 additions & 0 deletions tools/bazar/lang/bazar_es.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,15 @@
// 'BAZ_CLOSE_THIS_WINDOW' => 'Fermer cette fen&ecirc;tre',
// 'BAZ_BOOKMARKLET_LABEL' => 'Créer une fiche %{form}',

// fields/CommentsField.php
// 'BAZ_ACTIVATE_COMMENTS' => 'Activer les commentaires sur cette fiche ?',
// 'BAZ_ACTIVATE_COMMENTS_HINT' => 'Droits mis à jour lors de l\'enregistrement de la fiche',
// 'BAZ_COMMENTS_INFO_DISCOURSE' => 'Les commentaires sont gérés par la plateforme discourse : mais ça n\'est pas encore fonctionnel !',
// 'BAZ_COMMENTS_INFO_HUMHUB_EMBEDDED' => 'Les commentaires sont gérés par la plateforme sociale HumHub (Humhub intégré par YesWiki)',
// 'BAZ_COMMENTS_INFO_HUMHUB_EXTERNAL' => 'Les commentaires sont gérés par la plateforme sociale HumHub (YesWiki intégré par Humhub)',
// 'BAZ_COMMENTS_INFO_UNKNOWN' => 'Les commentaires ne sont pas gérés car le type de réglage des commentaires est inconnu',
// 'BAZ_COMMENTS_NOT_ACTIVATED' => 'Les commentaires sont gérés par YesWiki mais ceux-ci ne sont pas activés.<br/>Se rendre sur la page GererConfig pour activer les commentaires (paramètre `comments_activated`)',

// fields/FileField.php
// 'BAZ_FILE_ALREADY_EXISTING' => 'fichier déja existant',
// 'BAZ_NOT_AUTHORIZED_FILE' => 'fichier non autorise',
Expand Down