n1ed-8.x-2.31/src/Controller/N1EDController.php

src/Controller/N1EDController.php
<?php

namespace Drupal\n1ed\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Config\ConfigFactoryInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Response;

/**
 * Provides route responses for Flmngr file manager.
 */
class N1EDController extends ControllerBase {

  /**
   * Drupal\Core\Config\ConfigFactoryInterface definition.
   *
   * @var Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * Symfony\Component\HttpFoundation\RequestStack definition.
   *
   * @var Symfony\Component\HttpFoundation\RequestStack
   */
  private $requestStack;

  /**
   * {@inheritdoc}
   */
  public function __construct(ConfigFactoryInterface $config_factory,
      RequestStack $request_stack) {
    $this->configFactory = $config_factory;
    $this->requestStack = $request_stack;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('config.factory'),
      $container->get('request_stack')
    );
  }

  /**
   * Sets API key into Drupal config.
   */
  public function setApiKey() {
    $apiKey = $this->requestStack->getCurrentRequest()->request->get("n1edApiKey");
    $token = $this->requestStack->getCurrentRequest()->request->get("n1edToken");

    if ($apiKey == NULL || $token == NULL) {
      throw new AccessDeniedHttpException();
    }

    $config = $this->configFactory->getEditable('n1ed.settings');
    $config->set('apikey', $apiKey);
    $config->set('token', $token);
    $config->set('integrationType', 'n1ed');
    $config->save(TRUE);

    // Get integration type for this API key from the server.
    $options = [
      'http' => [
        'method' => 'POST',
        'header' => "Content-Type: application/json\r\n" .
        "Accept: application/json\r\n",
        'content' => http_build_query(
          [
            'apiKey' => $apiKey
          ]
        )
      ],
    ];
    $context = @stream_context_create($options);
    $result = @file_get_contents("https://cloud.n1ed.com/api/v1/conf/get-integration-type", FALSE, $context);
    if ($result !== FALSE) {
      $json = json_decode($result);
      if ($json->error !== NULL) {
        error_log("Unknown integrationType content received: " . $result);
      } else {
        $config = $this->configFactory->getEditable('n1ed.settings');
        $config->set('integrationType', $json->data);
        $config->save(TRUE);
      }
    } else {
      error_log("No integrationType response received.");
      error_log(print_r($http_response_header, TRUE));
    }

    return new Response();
  }

  /**
   * Toggles on the option defining attaching file manager to Drupa file fields.
   */
  public function toggleUseFlmngrOnFileFields() {
    $useFlmngrOnFileFields = json_decode(file_get_contents('php://input'))->useFlmngrOnFileFields;
    $config = $this->configFactory->getEditable('n1ed.settings');
    $config->set('useFlmngrOnFileFields', $useFlmngrOnFileFields);
    $config->save(TRUE);
    return new Response();
  }

  /**
   * Toggles on the option defining attaching file manager to Drupa file fields.
   */
  public function toggleUseLegacyFlmngrBackend() {
    $useLegacyFlmngrBackend = json_decode(file_get_contents('php://input'))->useLegacyFlmngrBackend;
    $config = $this->configFactory->getEditable('n1ed.settings');
    $config->set('useLegacyFlmngrBackend', $useLegacyFlmngrBackend);
    $config->save(TRUE);
    return new Response();
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc