2023-06-15 21:57:23 +02:00
|
|
|
<?php
|
|
|
|
|
2023-09-30 20:02:02 +02:00
|
|
|
namespace Drupal\yufu_concept\Plugin\rest\resource;
|
2023-06-15 21:57:23 +02:00
|
|
|
|
|
|
|
use Drupal\Core\Entity\EntityTypeManagerInterface;
|
2023-09-30 20:02:02 +02:00
|
|
|
use Drupal\Core\Session\AccountProxyInterface;
|
|
|
|
use Drupal\Core\StringTranslation\StringTranslationTrait;
|
2023-06-15 21:57:23 +02:00
|
|
|
use Drupal\Core\TypedData\Exception\MissingDataException;
|
2023-11-12 23:42:52 +01:00
|
|
|
use Drupal\node\NodeInterface;
|
2023-06-15 21:57:23 +02:00
|
|
|
use Drupal\rest\Plugin\ResourceBase;
|
|
|
|
use Drupal\rest\ResourceResponse;
|
2023-09-30 20:02:02 +02:00
|
|
|
use Psr\Log\LogLevel;
|
2023-06-15 21:57:23 +02:00
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface;
|
2023-09-30 20:02:02 +02:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2023-06-15 21:57:23 +02:00
|
|
|
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates post endpoint to create new concept.
|
|
|
|
*
|
|
|
|
* @RestResource(
|
|
|
|
* id = "add_concept_rest_resource",
|
|
|
|
* label = @Translation("Create or edit a concept"),
|
|
|
|
* uri_paths = {
|
2023-09-30 20:02:02 +02:00
|
|
|
* "canonical" = "/api/pojem/dodaj",
|
|
|
|
* "create" = "/api/pojem/dodaj",
|
2023-06-15 21:57:23 +02:00
|
|
|
* }
|
2023-09-30 20:02:02 +02:00
|
|
|
* )
|
2023-06-15 21:57:23 +02:00
|
|
|
*/
|
|
|
|
class AddConcept extends ResourceBase {
|
|
|
|
|
2023-09-30 20:02:02 +02:00
|
|
|
use StringTranslationTrait;
|
|
|
|
|
|
|
|
/**
|
2023-06-15 21:57:23 +02:00
|
|
|
* A current user instance.
|
|
|
|
*
|
|
|
|
* @var \Drupal\Core\Session\AccountProxyInterface
|
|
|
|
*/
|
|
|
|
protected $currentUser;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Entity type manager service.
|
|
|
|
*
|
|
|
|
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
|
|
|
|
*/
|
|
|
|
protected $entityTypeManager;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs a Drupal\rest\Plugin\ResourceBase object.
|
|
|
|
*
|
|
|
|
* @param array $configuration
|
|
|
|
* A configuration array containing information about the plugin instance.
|
|
|
|
* @param string $plugin_id
|
|
|
|
* The plugin_id for the plugin instance.
|
|
|
|
* @param mixed $plugin_definition
|
|
|
|
* The plugin implementation definition.
|
|
|
|
* @param array $serializer_formats
|
|
|
|
* The available serialization formats.
|
|
|
|
* @param \Psr\Log\LoggerInterface $logger
|
|
|
|
* A logger instance.
|
|
|
|
* @param \Drupal\Core\Session\AccountProxyInterface $current_user
|
|
|
|
* A current user instance.
|
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
array $configuration,
|
2023-09-30 20:02:02 +02:00
|
|
|
string $plugin_id,
|
|
|
|
array $plugin_definition,
|
2023-06-15 21:57:23 +02:00
|
|
|
array $serializer_formats,
|
|
|
|
LoggerInterface $logger,
|
|
|
|
AccountProxyInterface $current_user,
|
|
|
|
EntityTypeManagerInterface $entity_type_manager) {
|
|
|
|
parent::__construct($configuration, $plugin_id, $plugin_definition, $serializer_formats, $logger);
|
|
|
|
$this->currentUser = $current_user;
|
|
|
|
$this->entityTypeManager = $entity_type_manager;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
|
|
|
|
return new static(
|
|
|
|
$configuration,
|
|
|
|
$plugin_id,
|
|
|
|
$plugin_definition,
|
|
|
|
$container->getParameter('serializer.formats'),
|
2023-11-12 23:42:52 +01:00
|
|
|
$container->get('logger.channel.yufu_concept'),
|
2023-06-15 21:57:23 +02:00
|
|
|
$container->get('current_user'),
|
|
|
|
$container->get('entity_type.manager')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-09-30 20:02:02 +02:00
|
|
|
* Ustvari nov koncept.
|
2023-06-15 21:57:23 +02:00
|
|
|
*
|
2023-11-12 23:42:52 +01:00
|
|
|
* Logika:
|
|
|
|
* 1. Zloadamo uporabnika preko emaila, ce ne obstaja, ga ustvarimo.
|
|
|
|
* 2. Zloadamo koncept prek uuid, ce ne obstaja, da ustvarimo.
|
|
|
|
* - avtor je zloadan prek maila
|
|
|
|
*
|
2023-09-30 20:02:02 +02:00
|
|
|
* @param \Symfony\Component\HttpFoundation\Request;
|
|
|
|
* Post request.
|
2023-06-15 21:57:23 +02:00
|
|
|
*
|
|
|
|
* @return \Drupal\rest\ResourceResponse
|
|
|
|
* Returns rest resource.
|
|
|
|
*/
|
2023-09-30 20:02:02 +02:00
|
|
|
public function post(Request $request) {
|
2023-06-15 21:57:23 +02:00
|
|
|
$response_status['status'] = FALSE;
|
2023-09-30 20:02:02 +02:00
|
|
|
$data = json_decode($request->getContent(), TRUE);
|
2023-06-15 21:57:23 +02:00
|
|
|
// You must to implement the logic of your REST Resource here.
|
|
|
|
// Use current user after pass authentication to validate access.
|
|
|
|
if (!$this->currentUser->hasPermission('access content')) {
|
|
|
|
throw new AccessDeniedHttpException();
|
|
|
|
}
|
|
|
|
// Fields: naslov, text, povezani pojmi, email
|
|
|
|
// Optional fields: language, related concept.
|
2023-11-12 23:42:52 +01:00
|
|
|
if (empty($data['title']) || empty($data['text']) || empty($data['uuid'])) {
|
|
|
|
throw new MissingDataException('Title, uuid or text missing.');
|
|
|
|
}
|
|
|
|
$uid = $this->getUserIdByEmail($data['email'] ?? null);
|
|
|
|
if ($concept = $this->getConceptFromUuid($data['uuid'])) {
|
|
|
|
// Concept exists - create a new revision.
|
|
|
|
$concept->setNewRevision(TRUE);
|
|
|
|
$concept->setRevisionUserId($uid);
|
|
|
|
$concept->set('title', $data['title']);
|
|
|
|
$concept->set('body', $data['text'] ?? $concept->body->value);
|
|
|
|
$concept->isDefaultRevision(FALSE);
|
|
|
|
$concept->setRevisionLogMessage('New revision by concept endpoint.');
|
|
|
|
$concept->moderation_state->target_id = 'draft';
|
2024-03-17 20:21:54 +01:00
|
|
|
$concept->set('status', 0);
|
2024-03-10 22:33:47 +01:00
|
|
|
$concept->setUnpublished();
|
2023-11-12 23:42:52 +01:00
|
|
|
$concept->save();
|
|
|
|
$this->logger->notice('New concept @title revision @revid created by uid @uid.', [
|
|
|
|
'@title' => $concept->getTitle(),
|
|
|
|
'@revid' => $concept->getRevisionId(),
|
|
|
|
'@uid' => $uid,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Concept does not exist - create a new node.
|
|
|
|
$concept = [
|
|
|
|
'type' => 'concept',
|
|
|
|
'title' => $data['title'],
|
|
|
|
'body' => $data['text'],
|
|
|
|
'uuid' => $data['uuid'],
|
|
|
|
'uid' => $uid,
|
|
|
|
'moderation_state' => 'draft',
|
|
|
|
];
|
|
|
|
// @TODO Check if related concepts are set and add them to the concept.
|
|
|
|
// @TODO Check language and add set it on concept if exists.
|
|
|
|
/** @var \Drupal\node\Entity\NodeInterface $concept */
|
|
|
|
$concept = $this->entityTypeManager->getStorage('node')->create($concept);
|
|
|
|
$concept->save();
|
|
|
|
$this->logger->log(LogLevel::NOTICE, $this->t('Creating concept: @title', [
|
|
|
|
'@title' => $concept->getTitle(),
|
|
|
|
]));
|
2023-06-15 21:57:23 +02:00
|
|
|
}
|
2023-09-30 20:02:02 +02:00
|
|
|
$response_status['concept'] = [
|
|
|
|
'title' => $concept->label(),
|
|
|
|
'id' => $concept->id(),
|
|
|
|
'uuid' => $concept->uuid(),
|
2023-11-12 23:42:52 +01:00
|
|
|
'revision_id' => $concept->getRevisionId(),
|
|
|
|
'revision_create_time' => $concept->getRevisionCreationTime(),
|
|
|
|
'revision_uid' => $concept->getRevisionUserId(),
|
|
|
|
'uid' => $concept->uid->target_id,
|
2023-09-30 20:02:02 +02:00
|
|
|
];
|
2023-06-15 21:57:23 +02:00
|
|
|
$response = new ResourceResponse($response_status);
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2023-11-12 23:42:52 +01:00
|
|
|
/**
|
|
|
|
* Get concept from uuid.
|
|
|
|
*
|
|
|
|
* If concept is new, return null.
|
|
|
|
*
|
|
|
|
* @param string|null $uuid
|
|
|
|
* Uuid of the node.
|
|
|
|
*
|
|
|
|
* @return \Drupal\node\NodeInterface|null
|
|
|
|
*/
|
|
|
|
protected function getConceptFromUuid(?string $uuid): ?NodeInterface {
|
|
|
|
if (!$uuid) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
$node_storage = $this->entityTypeManager->getStorage('node');
|
|
|
|
if ($concept = end($node_storage->loadByProperties(['uuid' => $uuid]))) {
|
|
|
|
return $concept;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads user from email or creates a new blocked user.
|
|
|
|
*
|
|
|
|
* Side effect is to create a new user.
|
|
|
|
*
|
|
|
|
* @param string|null $email
|
|
|
|
* Email provided by the fe call to endpoint.
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
* Id of the user.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
protected function getUserIdByEmail(?string $email): int {
|
|
|
|
if (!$email) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
// Load user by email. If it doesn't exist, create one.
|
|
|
|
$user_storage = $this->entityTypeManager->getStorage('user');
|
|
|
|
if ($user = end($user_storage->loadByProperties(['mail' => $email]))) {
|
|
|
|
return $user->id();
|
|
|
|
}
|
|
|
|
$user = [
|
|
|
|
'mail' => $email,
|
|
|
|
'name' => $email,
|
|
|
|
'status' => 0,
|
|
|
|
];
|
|
|
|
$new_user = $user_storage->create($user);
|
|
|
|
$new_user->save();
|
|
|
|
return $new_user->id();
|
|
|
|
}
|
|
|
|
|
2023-06-15 21:57:23 +02:00
|
|
|
}
|