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: Add report to user posting rights #895

Open
wants to merge 13 commits into
base: develop
Choose a base branch
from
2 changes: 2 additions & 0 deletions config/user.role.authenticated.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ dependencies:
- filter.format.markdown_editor
- filter.format.token_markdown
- node.type.job
- node.type.report
- node.type.training
module:
- content_entity_clone
Expand All @@ -31,6 +32,7 @@ permissions:
- 'bookmark content'
- 'clone content entities'
- 'create job content'
- 'create report content'
- 'create training content'
- 'delete own job content'
- 'delete own training content'
Expand Down
12 changes: 10 additions & 2 deletions html/modules/custom/reliefweb_entities/src/Entity/Report.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Drupal\reliefweb_entities\BundleEntityInterface;
use Drupal\reliefweb_entities\DocumentInterface;
use Drupal\reliefweb_entities\DocumentTrait;
use Drupal\reliefweb_entities\OpportunityDocumentTrait;
use Drupal\reliefweb_moderation\EntityModeratedInterface;
use Drupal\reliefweb_moderation\EntityModeratedTrait;
use Drupal\reliefweb_revisions\EntityRevisionedInterface;
Expand All @@ -27,6 +28,7 @@ class Report extends Node implements BundleEntityInterface, EntityModeratedInter
use DocumentTrait;
use EntityModeratedTrait;
use EntityRevisionedTrait;
use OpportunityDocumentTrait;
use StringTranslationTrait;

/**
Expand Down Expand Up @@ -203,8 +205,6 @@ public function getAttachments(?array $build = NULL) {
* {@inheritdoc}
*/
public function preSave(EntityStorageInterface $storage) {
parent::preSave($storage);

// Change the publication date if bury is selected, to the original
// publication date.
if (!empty($this->field_bury->value) && !$this->field_original_publication_date->isEmpty()) {
Expand Down Expand Up @@ -249,6 +249,14 @@ public function preSave(EntityStorageInterface $storage) {

// Prepare notifications.
$this->preparePublicationNotification();

// Update the entity status based on the user posting rights.
$this->updateModerationStatusFromPostingRights();

// Update the entity status based on the source(s) moderation status.
$this->updateModerationStatusFromSourceStatus();

parent::preSave($storage);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,8 +384,8 @@ protected function addPotentialNewSourceFields(array &$form, FormStateInterface
$entity = $form_state->getFormObject()->getEntity();
$bundle = $entity->bundle();

// The following only applies to job and training nodes.
if (!in_array($bundle, ['job', 'training'])) {
// The following only applies to job, training and report nodes.
if (!in_array($bundle, ['job', 'training', 'report'])) {
return;
}

Expand Down Expand Up @@ -582,7 +582,7 @@ public static function retrievePotentialNewSourceInformation(EntityModeratedInte
}

/**
* Add the user information to a job/training node form.
* Add the user information to a job/training/report node form.
*
* @param array $form
* The entity form.
Expand All @@ -594,8 +594,8 @@ protected function addUserInformation(array &$form, FormStateInterface $form_sta
$entity_id = $entity->id();
$bundle = $entity->bundle();

// It's only for jobs and training.
if (!in_array($bundle, ['job', 'training'])) {
// It's only for jobs, trainings and reports.
if (!in_array($bundle, ['job', 'training', 'report'])) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Drupal\reliefweb_utility\Helpers\UserHelper;

/**
* Trait for "opportunity" documents like jobs and training.
* Trait for "opportunity" documents like jobs, trainings and reports.
*
* @see Drupal\reliefweb_entities\DocuemntInterface
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
flex-wrap: wrap;
align-items: center;
}
.field--type-reliefweb-user-posting-rights div[data-filters][data-job="all"][data-training="all"] ~ ul li {
.field--type-reliefweb-user-posting-rights div[data-filters][data-job="all"][data-training="all"][data-report="all"] ~ ul li {
display: flex;
}
.field--type-reliefweb-user-posting-rights div[data-filters][data-job="0"] ~ ul li[data-job="0"] {
Expand All @@ -68,8 +68,22 @@
.field--type-reliefweb-user-posting-rights div[data-filters][data-training="3"] ~ ul li[data-training="3"] {
display: flex;
}
.field--type-reliefweb-user-posting-rights div[data-filters][data-report="0"] ~ ul li[data-report="0"] {
display: flex;
}
.field--type-reliefweb-user-posting-rights div[data-filters][data-report="1"] ~ ul li[data-report="1"] {
display: flex;
}
.field--type-reliefweb-user-posting-rights div[data-filters][data-report="2"] ~ ul li[data-report="2"] {
display: flex;
}
.field--type-reliefweb-user-posting-rights div[data-filters][data-report="3"] ~ ul li[data-report="3"] {
display: flex;
}

.field--type-reliefweb-user-posting-rights [data-job] label.job span:after,
.field--type-reliefweb-user-posting-rights [data-training] label.training span:after {
.field--type-reliefweb-user-posting-rights [data-training] label.training span:after,
.field--type-reliefweb-user-posting-rights [data-report] label.report span:after {
display: inline-block;
width: 12px;
height: 12px;
Expand All @@ -79,21 +93,26 @@
border-radius: 4px;
}
.field--type-reliefweb-user-posting-rights [data-job="0"] label.job span:after,
.field--type-reliefweb-user-posting-rights [data-training="0"] label.training span:after {
.field--type-reliefweb-user-posting-rights [data-training="0"] label.training span:after,
.field--type-reliefweb-user-posting-rights [data-report="0"] label.report span:after {
background: #f49e2c;
}
.field--type-reliefweb-user-posting-rights [data-job="1"] label.job span:after,
.field--type-reliefweb-user-posting-rights [data-training="1"] label.training span:after {
.field--type-reliefweb-user-posting-rights [data-training="1"] label.training span:after,
.field--type-reliefweb-user-posting-rights [data-report="1"] label.report span:after {
background: #da190b;
}
.field--type-reliefweb-user-posting-rights [data-job="2"] label.job span:after,
.field--type-reliefweb-user-posting-rights [data-training="2"] label.training span:after {
.field--type-reliefweb-user-posting-rights [data-training="2"] label.training span:after,
.field--type-reliefweb-user-posting-rights [data-report="2"] label.report span:after {
background: #076d96;
}
.field--type-reliefweb-user-posting-rights [data-job="3"] label.job span:after,
.field--type-reliefweb-user-posting-rights [data-training="3"] label.training span:after {
.field--type-reliefweb-user-posting-rights [data-training="3"] label.training span:after,
.field--type-reliefweb-user-posting-rights [data-report="3"] label.report span:after {
background: #88bb09;
}

.field--type-reliefweb-user-posting-rights ul {
margin: 0;
padding: 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
container.setAttribute('data-status', data.status ? 'active' : 'blocked');
container.setAttribute('data-job', data.job);
container.setAttribute('data-training', data.training);
container.setAttribute('data-report', data.report);

// User info.
var info = document.createElement('div');
Expand Down Expand Up @@ -147,6 +148,7 @@
// Rights.
actions.appendChild(this.createSelect('job', data.job, disabled));
actions.appendChild(this.createSelect('training', data.training, disabled));
actions.appendChild(this.createSelect('report', data.report, disabled));

// Remove.
actions.appendChild(this.createButton('remove', t('Remove'), true, '', disabled));
Expand Down Expand Up @@ -205,6 +207,7 @@
container.setAttribute('data-filters', '');
container.setAttribute('data-job', 'all');
container.setAttribute('data-training', 'all');
container.setAttribute('data-report', 'all');

var title = document.createElement('span');
title.appendChild(document.createTextNode(t('Filter: ')));
Expand All @@ -213,6 +216,7 @@
// Rights filters.
container.appendChild(this.createSelect('job', '', false, true));
container.appendChild(this.createSelect('training', '', false, true));
container.appendChild(this.createSelect('report', '', false, true));

return container;
},
Expand Down Expand Up @@ -418,6 +422,7 @@
id: element.getAttribute('data-id'),
job: Math.max(element.querySelector('select[data-name="job"]').selectedIndex, 0),
training: Math.max(element.querySelector('select[data-name="training"]').selectedIndex, 0),
report: Math.max(element.querySelector('select[data-name="report"]').selectedIndex, 0),
notes: element.querySelector('textarea').value.trim()
};
},
Expand Down Expand Up @@ -511,7 +516,7 @@
var name = target.getAttribute('data-name');

// Update the rights attributes of the user row.
if (name === 'job' || name === 'training') {
if (name === 'job' || name === 'training' || name === 'report') {
var parent = target.parentNode.parentNode;

// If the parent is not the filter container, then it's a select
Expand Down
65 changes: 65 additions & 0 deletions html/modules/custom/reliefweb_fields/reliefweb_fields.install
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

/**
* @file
* Install file for the ReliefWeb Fields module.
*/

use Drupal\Core\Database\Database;

/**
* Update user posting rights field.
*/
function reliefweb_fields_update_10001() {
$kv_schema = \Drupal::keyValue('entity.storage_schema.sql')->get('taxonomy_term.field_schema_data.field_user_posting_rights');
foreach ($kv_schema as $table => &$info) {
$schema = Database::getConnection()->schema();
if (!$schema->fieldExists($table, 'field_user_posting_rights_report')) {
$spec = [
'description' => 'Report posting rights: 0 = unverified; 1 = blocked; 2 = allowed; 3 = trusted.',
'type' => 'int',
'size' => 'tiny',
'not null' => '',
'default' => 0,
];
$schema->addField($table, 'field_user_posting_rights_report', $spec);

$spec = [
'fields' => [
'field_user_posting_rights_report' => [
'description' => 'Report posting rights: 0 = unverified; 1 = blocked; 2 = allowed; 3 = trusted.',
'type' => 'int',
'size' => 'tiny',
'not null' => '',
'default' => 0,
],
],
'indexes' => [
'field_user_posting_rights_report' => ['field_user_posting_rights_report'],
],
];

$schema->addIndex($table, 'field_user_posting_rights_report', [
'field_user_posting_rights_report',
], $spec);
}

$info['fields']['field_user_posting_rights_report'] = [
'description' => 'Report posting rights: 0 = unverified; 1 = blocked; 2 = allowed; 3 = trusted.',
'type' => 'int',
'size' => 'tiny',
'not null' => '',
'default' => 0,
];
$info['indexes']['field_user_posting_rights_report'] = [
'field_user_posting_rights_report',
];
}

\Drupal::keyValue('entity.storage_schema.sql')->set('taxonomy_term.field_schema_data.field_user_posting_rights', $kv_schema);

$entity_definition_update_manager = \Drupal::entityDefinitionUpdateManager();
$field_storage_definition = $entity_definition_update_manager->getFieldStorageDefinition('field_user_posting_rights', 'taxonomy_term');
$field_storage_definition->setSetting('column_changes_handled', TRUE);
$entity_definition_update_manager->updateFieldStorageDefinition($field_storage_definition);
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ public static function schema(FieldStorageDefinitionInterface $field_definition)
'not null' => TRUE,
'default' => 0,
],
'report' => [
'description' => 'Report posting rights: 0 = unverified; 1 = blocked; 2 = allowed; 3 = trusted.',
'type' => 'int',
'size' => 'tiny',
'not null' => TRUE,
'default' => 0,
],
'notes' => [
'description' => 'Notes',
'type' => 'text',
Expand All @@ -60,6 +67,7 @@ public static function schema(FieldStorageDefinitionInterface $field_definition)
'id' => ['id'],
'job' => ['job'],
'training' => ['training'],
'report' => ['report'],
],
];
}
Expand All @@ -80,6 +88,10 @@ public static function propertyDefinitions(FieldStorageDefinitionInterface $fiel
->setLabel(new TranslatableMarkup('Training'))
->setRequired(FALSE);

$properties['report'] = DataDefinition::create('integer')
->setLabel(new TranslatableMarkup('Report'))
->setRequired(FALSE);

$properties['notes'] = DataDefinition::create('string')
->setLabel(new TranslatableMarkup('Notes'))
->setRequired(FALSE);
Expand Down Expand Up @@ -136,6 +148,17 @@ public function getConstraints() {
],
],
]);
$constraints[] = $constraint_manager->create('ComplexData', [
'report' => [
'AllowedValues' => [
'choices' => [0, 1, 2, 3],
'strict' => TRUE,
'message' => $this->t('%name: the Report rights must be one of 0, 1, 2 or 3.', [
'%name' => $this->getFieldDefinition()->getLabel(),
]),
],
],
]);

return $constraints;
}
Expand All @@ -147,6 +170,7 @@ public static function generateSampleValue(FieldDefinitionInterface $field_defin
$values['url'] = mt_rand(3, 1000000);
$values['job'] = mt_rand(0, 3);
$values['training'] = mt_rand(0, 3);
$values['report'] = mt_rand(0, 3);

return $values;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ public function massageFormValues(array $values, array $form, FormStateInterface
'id' => intval($item['id'], 10),
'job' => intval($item['job'], 10),
'training' => intval($item['training'], 10),
'report' => intval($item['report'], 10),
'notes' => $item['notes'],
];
}
Expand All @@ -200,6 +201,7 @@ public static function normalizeData(array $data) {
$data['id'] = intval($data['id'] ?? $data['uid'], 10);
$data['job'] = isset($data['job']) ? intval($data['job'], 10) : 0;
$data['training'] = isset($data['training']) ? intval($data['training'], 10) : 0;
$data['report'] = isset($data['report']) ? intval($data['report'], 10) : 0;
$data['notes'] = isset($data['notes']) ? trim($data['notes']) : '';

$data['name'] = trim($data['name']);
Expand All @@ -210,6 +212,7 @@ public static function normalizeData(array $data) {
if ($data['status'] === 0) {
$data['job'] = 1;
$data['training'] = 1;
$data['report'] = 1;
}

return $data;
Expand Down Expand Up @@ -410,7 +413,8 @@ public static function updateFields($op, UserInterface $user) {
$query->condition($query
->orConditionGroup()
->condition($field_name . '.job', 1, '<>')
->condition($field_name . '.training', 1, '<>'));
->condition($field_name . '.training', 1, '<>')
->condition($field_name . '.report', 1, '<>'));
}

$ids = $query?->execute();
Expand All @@ -435,9 +439,10 @@ public static function updateFields($op, UserInterface $user) {
}
// Set the rights to 'blocked' if the account is blocked.
elseif ($blocked) {
if ($item['job'] != 1 || $item['training'] != 1) {
if ($item['job'] != 1 || $item['training'] != 1 || $item['report'] != 1) {
$items[$delta]['job'] = 1;
$items[$delta]['training'] = 1;
$items[$delta]['report'] = 1;
if (!empty($item['notes'])) {
$items[$delta]['notes'] .= ' ' . $message;
}
Expand All @@ -450,9 +455,10 @@ public static function updateFields($op, UserInterface $user) {
// Reset the rights to 'unverified' if the email changed but
// preserve the 'blocked' rights.
elseif ($email_changed) {
if ($item['job'] > 1 || $item['training'] > 1) {
if ($item['job'] > 1 || $item['training'] > 1 || $item['report'] > 1) {
$items[$delta]['job'] = $item['job'] == 1 ? 1 : 0;
$items[$delta]['training'] = $item['training'] == 1 ? 1 : 0;
$items[$delta]['report'] = $item['report'] == 1 ? 1 : 0;
if (!empty($item['notes'])) {
$items[$delta]['notes'] .= ' ' . $message;
}
Expand Down
Loading