181 lines
5.8 KiB
PHP
181 lines
5.8 KiB
PHP
<?php
|
|
|
|
namespace Drupal\yufu_concept\Plugin\rest\resource;
|
|
|
|
use Drupal\Core\Config\ConfigFactoryInterface;
|
|
use Drupal\Core\Entity\EntityTypeManagerInterface;
|
|
use Drupal\Core\Mail\MailManagerInterface;
|
|
use Drupal\Core\Session\AccountProxyInterface;
|
|
use Drupal\Core\StringTranslation\StringTranslationTrait;
|
|
use Drupal\Core\TypedData\Exception\MissingDataException;
|
|
use Drupal\rest\Plugin\ResourceBase;
|
|
use Drupal\rest\ResourceResponse;
|
|
use Psr\Log\LogLevel;
|
|
use Psr\Log\LoggerInterface;
|
|
use Symfony\Component\DependencyInjection\ContainerInterface;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
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 = {
|
|
* "canonical" = "/api/pojem/dodaj",
|
|
* "create" = "/api/pojem/dodaj",
|
|
* }
|
|
* )
|
|
*/
|
|
class AddConcept extends ResourceBase {
|
|
|
|
use StringTranslationTrait;
|
|
|
|
/**
|
|
* A current user instance.
|
|
*
|
|
* @var \Drupal\Core\Session\AccountProxyInterface
|
|
*/
|
|
protected $currentUser;
|
|
|
|
/**
|
|
* Mail manager service.
|
|
*
|
|
* @var \Drupal\Core\Mail\MailManagerInterface
|
|
*/
|
|
protected $mailManager;
|
|
|
|
/**
|
|
* Site configs.
|
|
*
|
|
* @var \Drupal\Core\Config\ImmutableConfig
|
|
*/
|
|
protected $config;
|
|
|
|
/**
|
|
* 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.
|
|
* @param \Drupal\Core\Mail\MailManagerInterface $mailManager
|
|
* Mail manager service.
|
|
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
|
|
* Config factory service.
|
|
*/
|
|
public function __construct(
|
|
array $configuration,
|
|
string $plugin_id,
|
|
array $plugin_definition,
|
|
array $serializer_formats,
|
|
LoggerInterface $logger,
|
|
AccountProxyInterface $current_user,
|
|
MailManagerInterface $mailManager,
|
|
ConfigFactoryInterface $configFactory,
|
|
EntityTypeManagerInterface $entity_type_manager) {
|
|
parent::__construct($configuration, $plugin_id, $plugin_definition, $serializer_formats, $logger);
|
|
$this->currentUser = $current_user;
|
|
$this->mailManager = $mailManager;
|
|
$this->config = $configFactory;
|
|
$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'),
|
|
$container->get('logger.factory')->get('yufu_concept'),
|
|
$container->get('current_user'),
|
|
$container->get('plugin.manager.mail'),
|
|
$container->get('config.factory'),
|
|
$container->get('entity_type.manager')
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Ustvari nov koncept.
|
|
*
|
|
* @param \Symfony\Component\HttpFoundation\Request;
|
|
* Post request.
|
|
*
|
|
* @return \Drupal\rest\ResourceResponse
|
|
* Returns rest resource.
|
|
*/
|
|
public function post(Request $request) {
|
|
$response_status['status'] = FALSE;
|
|
$data = json_decode($request->getContent(), TRUE);
|
|
// 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.
|
|
if (!empty($data['title']) && !empty($data['text'])) {
|
|
throw new MissingDataException('Title or text missing.');
|
|
}
|
|
$concept = [
|
|
'type' => 'concept',
|
|
'title' => $data['title'],
|
|
'body' => $data['body'],
|
|
];
|
|
$this->logger->log(LogLevel::NOTICE, $this->t('Creating concept: @title', [
|
|
'@title' => $concept['title'],
|
|
]));
|
|
// @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();
|
|
$response_status['concept'] = [
|
|
'title' => $concept->label(),
|
|
'id' => $concept->id(),
|
|
'uuid' => $concept->uuid(),
|
|
];
|
|
|
|
if (!empty($data['email'])) {
|
|
// @TODO Poslji mail uporabniku, da se lahko registrira.
|
|
$site_email = $this->config->get('system.site')->get('mail');
|
|
$module = 'yufu_concept';
|
|
$key = 'add_concept_rest_resource';
|
|
$to = $site_email;
|
|
// Send email to user to make him register to the website.
|
|
$params['message'] = $this->t('Creating concept: @title', [
|
|
'@title' => $concept->label(),
|
|
]);
|
|
$params['subject'] = $data['title'];
|
|
$params['from'] = $data['email'];
|
|
// @TODO Add validation for langode (must be from system configured endpoints).
|
|
$langcode = $data['lang'] ?? 'en';
|
|
$send = TRUE;
|
|
$result = $this->mailManager->mail($module, $key, $to, $langcode, $params, NULL, $send);
|
|
$response_status['status'] = $result['result'];
|
|
}
|
|
$response = new ResourceResponse($response_status);
|
|
return $response;
|
|
}
|
|
|
|
}
|