diff --git a/web/modules/custom/yufu_concept/src/Plugin/rest/resource/AddConcept.php b/web/modules/custom/yufu_concept/src/Plugin/rest/resource/AddConcept.php new file mode 100644 index 0000000..1d8800d --- /dev/null +++ b/web/modules/custom/yufu_concept/src/Plugin/rest/resource/AddConcept.php @@ -0,0 +1,163 @@ +currentUser = $current_user; + $this->mailManager = $mailManager; + $this->config = $configFactory->get('system.site'); + $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('custom_rest'), + $container->get('current_user'), + $container->get('plugin.manager.mail'), + $container->get('config.factory'), + $container->get('entity_type.manager') + ); + } + + /** + * Send a test email. + * + * @param $data + * Post date. + * + * @return \Drupal\rest\ResourceResponse + * Returns rest resource. + */ + public function post($data) { + $response_status['status'] = FALSE; + // 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['text'], + ]; + // @TODO Check if related concepts are set and add them to the concept. + // @TODO Check language and add set it on concept if exists. + $concept = $this->entityTypeManger->getStorage('node')->create($concept); + $concept->save(); + + if (!empty($data['email'])) { + // @TODO Poslji mail uporabniku, da se lahko registrira. + $site_email = $this->config->get('mail'); + $module = 'yufu_concept'; + $key = 'notice'; + $to = $site_email; + // Send email to user to make him register to the website. + $params['message'] = $data['message']['value']; + $params['title'] = $data['subject']['value']; + $params['from'] = $data['email']['value']; + $langcode = $data['lang']['value']; + $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; + } + +} diff --git a/web/modules/custom/yufu_concept/tests/local-emacs-test.rest b/web/modules/custom/yufu_concept/tests/local-emacs-test.rest new file mode 100644 index 0000000..daaa1c3 --- /dev/null +++ b/web/modules/custom/yufu_concept/tests/local-emacs-test.rest @@ -0,0 +1,12 @@ +# _*_ restclient _*_ + +# Makes post http request to a resource from yufu_concept module. + +POST https://yufu-manifest.ddev.site/api/pojem/dodaj +User-Agent: Emacs Restclient +Content-Type: application/json + +{ +"title": "Emacs restclient test", +"body": "O wau. Emacs restclient. How nice." +} \ No newline at end of file diff --git a/web/modules/custom/yufu_concept/yufu_concept.info.yml b/web/modules/custom/yufu_concept/yufu_concept.info.yml new file mode 100644 index 0000000..cfc664a --- /dev/null +++ b/web/modules/custom/yufu_concept/yufu_concept.info.yml @@ -0,0 +1,7 @@ +name: Yufu Concept +type: module +description: Provides concept content type related functionality. +package: Yufu +core_version_requirement: ^9 || ^10 +dependencies: + - drupal:rest diff --git a/web/modules/custom/yufu_concept/yufu_concept.module b/web/modules/custom/yufu_concept/yufu_concept.module new file mode 100644 index 0000000..0a8a750 --- /dev/null +++ b/web/modules/custom/yufu_concept/yufu_concept.module @@ -0,0 +1,6 @@ +