Login.php 4.05 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
<?php
/**
 * @file
 * Contains \Drupal\query_example\Controller\QueryExampleController.
 */

namespace Drupal\epal\Controller;

use Drupal\Core\Entity\Query\QueryFactory;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Drupal\Core\Controller\ControllerBase;

/**
 * Controller routines for page example routes.
 */
class Login extends ControllerBase {

  protected $query_factory;

public function __construct(EntityTypeManagerInterface $entityTypeManager, QueryFactory $query_factory) {
//    public function __construct(QueryFactory $query_factory) {
      $this->entityTypeManager = $entityTypeManager;
      $this->query_factory = $query_factory;
  }


public static function create(ContainerInterface $container) {
    return new static(
        $container->get('entity.manager'),
        $container->get('entity.query')
    );
}


public function helloWorld() {
    $name = \Drupal::request()->query->get('name');
    $authToken = "no authToken";
    $accessKey = "no accessKey";

    if (\Drupal::request()->headers->has('X-AUTH-TOKEN')) {
        $authToken = \Drupal::request()->headers->get( 'X-AUTH-TOKEN' );
    }
    if (\Drupal::request()->headers->has('X-ACCESS-KEY')) {
        $accessKey = \Drupal::request()->headers->get( 'X-ACCESS-KEY' );
    }
    $response = new JsonResponse(['hello' => 'world', 'name' => $name, 'authToken' => $authToken, 'accessKey' => $accessKey]);
    $response->headers->set('X-AUTH-TOKEN', 'HELLOTOKEN');
    return $response;
}

  protected function simpleQuery() {
    $query = $this->query_factory->get('student_class');
//      ->condition('status', 1);
    $scids = $query->execute();
    $studentClass_storage = $this->entityTypeManager->getStorage('student_class');
    $studentClasses = $studentClass_storage->loadMultiple($scids);
    $arrayToReturn = array();

    foreach ($studentClasses as $studentClass) {
        array_push($arrayToReturn,
            array(
                array('data' => $studentClass->get('name')->value, 'class' => 'not-editable'),
                array('data' => "hello")
            ));
    }

    return array_values($arrayToReturn);
  }

  public function basicQuery() {
    return [
      '#title' => 'All student class ids',
      'studentclasses' => array(
          '#attributes' => ['id' => 'studentclasses', 'name' => 'studentclasses'],
          '#theme' => 'table',
          '#caption' => t('Student Classes'),
          '#header' => array(t('Name'), t('Max No')),
          '#rows' => $this->simpleQuery(),
      ),
      '#attached' => [
          'library' => [
              'eepal/eepal-styles', //include our custom module library for this response
              'eepal/data-tables' //include data tables libraries with this response
          ]
      ]
    ];
  }

  protected function intermediateQuery() {
    $query = $this->query_factory->get('node')
      ->condition('status', 1)
      ->condition('changed', REQUEST_TIME, '<')
      ->condition('title', 'ipsum lorem', 'CONTAINS')
      ->condition('field_tags.entity.name', 'test');
    $nids = $query->execute();
    return array_values($nids);
  }

  public function conditionalQuery() {
    return [
      '#title' => 'Published Nodes Called "ipsum lorem" That Have a Tag "test"',
      'content' => [
        '#theme' => 'item_list',
        '#items' => $this->intermediateQuery()
      ]
    ];
  }

  protected function advancedQuery() {
    $query = $this->query_factory->get('node')
      ->condition('status', 1)
      ->condition('changed', REQUEST_TIME, '<');
    $group = $query->orConditionGroup()
      ->condition('title', 'ipsum lorem', 'CONTAINS')
      ->condition('field_tags.entity.name', 'test');
    $nids = $query->condition($group)->execute();
    return array_values($nids);
  }

  public function conditionalGroupQuery() {
    return [
      '#title' => 'Published Nodes That Are Called "ipsum lorem" Or Have a Tag "test"',
      'content' => [
        '#theme' => 'item_list',
        '#items' => $this->advancedQuery()
      ]
    ];
  }

}