Skip to content

Commit

Permalink
feat(CommentsField): create
Browse files Browse the repository at this point in the history
  • Loading branch information
J9rem committed Jan 4, 2023
1 parent 66392cb commit 2ae293b
Show file tree
Hide file tree
Showing 13 changed files with 272 additions and 2 deletions.
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 @@ -534,6 +534,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
160 changes: 160 additions & 0 deletions tools/bazar/fields/CommentsField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
<?php

namespace YesWiki\Bazar\Field;

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

/**
* @Field({"comments"})
*/
class CommentsField extends SelectListField
{
public const OPTION_YES = 'oui';
public const OPTION_NO = 'non';
public const OPTIONS = [
'oui' => 'YES',
'non' => 'NO',
];
protected const FIELD_DEFAULT_RIGHTS = 7;

protected $aclService;
protected $defaultRights;
protected $params;

public function __construct(array $values, ContainerInterface $services)
{
parent::__construct($values, $services);
$this->defaultRights = (isset($values[self::FIELD_DEFAULT_RIGHTS]) && is_string($values[self::FIELD_DEFAULT_RIGHTS]))
? trim($values[self::FIELD_DEFAULT_RIGHTS])
: '+';
$this->listLabel = $this->name;
$this->propertyName = $this->name;
$this->keywords = '';
$this->queries = '';

if (empty($this->label) || empty(trim($this->label))) {
$this->label = _t('BAZ_ACTIVATE_COMMENTS');
}
if (empty($this->hint) || empty(trim($this->hint))) {
$this->hint = _t('BAZ_ACTIVATE_COMMENTS_HINT');
}

$this->params = $this->getService(ParameterBagInterface::class);
$this->aclService = $this->getService(AclService::class);
}

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

public function getLinkedObjectName()
{
// there is no linkedObjectName
return 'CommentsField';
}

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

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

protected function getValue($entry)
{
$value = parent::getValue($entry);
return $value == self::OPTION_YES ? self::OPTION_YES : self::OPTION_NO;
}

// Format input values before save
public function formatValuesBeforeSave($entry)
{
$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);
}

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

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

public function getDefaultRights(): string
{
return $this->defaultRights;
}

// change return of this method to keep compatible with php 7.3 (mixed is not managed)
#[\ReturnTypeWillChange]
public function jsonSerialize()
{
return array_merge(
parent::jsonSerialize(),
[
'commentsType' => $this->getCommentsType(),
'defaultRights' => $this->getDefaultRights()
]
);
}

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 = trim($this->getDefaultRights());
if (empty($defaultRights) || $defaultRights === 'comments-closed') {
// backup
$defaultRights = '+';
} else {
$defaultRights = str_replace(',', "\n", $defaultRights);
}
$this->aclService->save($entry['id_fiche'], 'comment', $defaultRights);
}
}
}
9 changes: 9 additions & 0 deletions tools/bazar/lang/bazar_fr.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' => 'Créer une fiche %{form}',

// fields/CommentsField.php
'BAZ_ACTIVATE_COMMENTS' => 'Activer les commentaires ?',
'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/bazarjs_fr.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,15 @@
'BAZ_FORM_EDIT_BOOKMARKLET_TEXT_LABEL' => "Texte affiché",
'BAZ_FORM_EDIT_BOOKMARKLET_TEXT_VALUE' => "Glisser-déposer le bouton dans votre barre de favoris",

'BAZ_ACTIVATE_COMMENTS' => 'Activer les commentaires ?',
'BAZ_ACTIVATE_COMMENTS_HINT' => 'Droits mis à jour lors de l\'enregistrement de la fiche',
'BAZ_FORM_EDIT_COMMENTS_FIELD_DEFAULT_ACTIVATION_LABEL' => 'Activation par défaut',
'BAZ_FORM_EDIT_COMMENTS_FIELD' => 'Commentaires',
'BAZ_FORM_EDIT_COMMENTS_FIELD_ACTIVATE_LABEL' => 'Texte pour édition',
'BAZ_FORM_EDIT_COMMENTS_FIELD_ACTIVATE_HINT' => 'Laisser vide pour utiliser le texte par défaut',
'BAZ_FORM_EDIT_COMMENTS_FIELD_DEFAULT_RIGHTS_LABEL' => 'Droits par défaut pour les commentaires ouverts',
'BAZ_FORM_EDIT_COMMENTS_FIELD_VALUE_HINT' => 'Mettre \'oui\' ou laisser vide',

// condition checking field
"BAZ_FORM_CONDITIONSCHEKING_HINT" => "La condition doit respecter le format suivant (sans les `):{\\n}".
" - ` and ` : donne ET{\\n}".
Expand Down
65 changes: 63 additions & 2 deletions tools/bazar/presentation/javascripts/form-edit-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@ var fields = [
name: "reactions",
attrs: { type: "reactions" },
icon: '<i class="far fa-thumbs-up"></i>',
},
{
label: _t('BAZ_FORM_EDIT_COMMENTS_FIELD'),
name: "comments",
attrs: { type: "comments" },
icon: '<i class="far fa-comment-dots"></i>',
}
]

Expand Down Expand Up @@ -546,6 +552,37 @@ var typeUserAttrs = {
},
read: readConf,
write: writeconf,
semantic: semanticConf
},
comments: {
fieldlabel: {
label: _t('BAZ_FORM_EDIT_COMMENTS_FIELD_ACTIVATE_LABEL'),
value: "",
placeholder: _t('BAZ_ACTIVATE_COMMENTS')
},
hint: {
label: _t('BAZ_FORM_EDIT_HELP'),
value: '',
placeholder: _t('BAZ_ACTIVATE_COMMENTS_HINT')
},
value: {
label: _t('BAZ_FORM_EDIT_COMMENTS_FIELD_DEFAULT_ACTIVATION_LABEL'),
value: "oui"
},
defaultrights: {
label: _t('BAZ_FORM_EDIT_COMMENTS_FIELD_DEFAULT_RIGHTS_LABEL'),
options: {
...{
' + ': visibilityOptions[' + '],
' % ': visibilityOptions[' % '],
'@admins': visibilityOptions['@admins']
},
...formattedGroupList
},
multiple: true
},
read: readConf,
write: writeconf,
semantic: semanticConf,
},
custom: {
Expand Down Expand Up @@ -687,15 +724,26 @@ var templates = {
templateHelper.defineLabelHintForGroup(field, 'images', _t('BAZ_REACTIONS_FIELD_IMAGES_HINT'))
templateHelper.defineLabelHintForGroup(field, 'labels', _t('BAZ_REACTIONS_FIELD_LABELS_HINT'))
}
};
}
},
comments: function (field) {
return {
field: `<i class="far fa-comment-dots"></i> ${field.fieldlabel || _t('BAZ_ACTIVATE_COMMENTS')}` ,
onRender() {
templateHelper.defineLabelHintForGroup(field, 'fieldlabel', _t('BAZ_FORM_EDIT_COMMENTS_FIELD_ACTIVATE_HINT'))
templateHelper.defineLabelHintForGroup(field, 'hint', _t('BAZ_FORM_EDIT_COMMENTS_FIELD_ACTIVATE_HINT'))
templateHelper.defineLabelHintForGroup(field, 'value', _t('BAZ_FORM_EDIT_COMMENTS_FIELD_VALUE_HINT'))
}
}
}
}

var typeUserDisabledAttrs = {
tabs: ['required', 'value', 'name', 'label'],
tabchange: ['required', 'value', 'name', 'label'],
bookmarklet: ['required', 'value'],
reactions: ['label','required']
reactions: ['label','required'],
comments: ['required']
}

var inputSets = [
Expand Down Expand Up @@ -866,6 +914,13 @@ var yesWikiMapping = {
4: "images",
6: "fieldlabel"
}
},
comments: {
...defaultMapping,
...{
6: "",
7: "defaultrights"
}
}
}
// Mapping betwwen yeswiki field type and standard field implemented by form builder
Expand Down Expand Up @@ -997,6 +1052,9 @@ function initializeFormbuilder(formAndListIds) {
if (field.type === 'acls' && !field.hasOwnProperty('comment')) {
field.comment = ['comments-closed']// comments-closed by default
}
if (field.type === 'comments' && !field.hasOwnProperty('defaultrights')) {
field.defaultrights = [' + ']// ' + ' by default
}
}
})

Expand Down Expand Up @@ -1250,6 +1308,9 @@ function parseWikiTextIntoJsonData(text) {
if (field) {
if (field == 'read' || field == 'write' || field == 'comment') {
fieldObject[field] = (value.trim() === '') ? [' * '] : value.split(',')
} else if (fieldType === 'comments' && field === 'defaultrights'){
fieldObject[field] = (value.trim() === '') ? [' + '] : value.split(',')
.map((e)=>(['+','*','%'].includes(e.trim())) ? ` ${e.trim()} ` : e)
} else {
fieldObject[field] = value
}
Expand Down
23 changes: 23 additions & 0 deletions tools/bazar/templates/inputs/comments.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{{ include('@bazar/inputs/select.twig') }}
{% if field.commentsType not in ['','yeswiki'] %}
{{ include('@templates/alert-message.twig',{
type: 'info',
message: '<i>'~((field.commentsType == 'external_humhub')
? _t('BAZ_COMMENTS_INFO_HUMHUB_EXTERNAL' )
: (
(field.commentsType == 'embedded_humhub')
? _t('BAZ_COMMENTS_INFO_HUMHUB_EMBEDDED' )
: ( (field.commentsType == 'discourse')
? _t('BAZ_COMMENTS_INFO_DISCOURSE' )
: _t('BAZ_COMMENTS_INFO_UNKNOWN' )
)
))~'</i>'
}) }}
{% endif %}
{% if showAlertForCommentsNotActivated %}
{{ include('@templates/alert-message.twig', {
type:'danger',
message: _t('BAZ_COMMENTS_NOT_ACTIVATED')
}
)}}
{% endif %}

0 comments on commit 2ae293b

Please sign in to comment.