Login.php 5.57 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
<?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;
14
use Drupal\Core\Entity\EntityFieldManagerInterface;
15 16 17 18 19 20 21

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

  protected $query_factory;
22 23
  protected $entityTypeManager;
  protected $entityFieldManager;
24

25
public function __construct(EntityTypeManagerInterface $entityTypeManager, QueryFactory $query_factory, EntityFieldManagerInterface $entityFieldManager) {
26 27 28
//    public function __construct(QueryFactory $query_factory) {
      $this->entityTypeManager = $entityTypeManager;
      $this->query_factory = $query_factory;
29
      $this->entityFieldManager = $entityFieldManager;
30 31 32 33 34
  }


public static function create(ContainerInterface $container) {
    return new static(
35 36 37
        $container->get('entity_type.manager'),
        $container->get('entity.query'),
        $container->get('entity_field.manager')
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
    );
}


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);
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
}

public function object_2_array($result) {
  $array = array();
  foreach ($result as $key=>$value)
  {
      if (is_object($value))
      {
          $array[$key]=$this->object_2_array($value);
      }
      elseif (is_array($value))
      {
          $array[$key]=$this->object_2_array($value);
      }
      else
      {
          $array[$key]=$value;
      }
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
  return $array;
}


  public function testQuery() {
    $query = $this->query_factory->get('epal_users');
//      ->condition('status', 1);
    $scids = $query->execute();
    $epalUsers_storage = $this->entityTypeManager->getStorage('epal_users');
    $epalUsers = $epalUsers_storage->loadMultiple($scids);
    $arrayToReturn = array();

    $j=0;
    foreach ($epalUsers as $epalUser) {
    //    print_r($epalUser);

//            $arrayToReturn[$j] = implode(",", $this->object_2_array($epalUser->id) );
            $arrayToReturn[$j] = $epalUser->user_id->target_id;
            $j++;

/*        foreach ($epalUser->name as $delta => $item) {
            $arrayToReturn[$delta] = $item->value;
        } */
/*        array_push($arrayToReturn,
        $epalUser->surname->getValue()); */
    }

//    return array_values($arrayToReturn);
    $response = new JsonResponse($arrayToReturn);
    return $response;
}

126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187

  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()
      ]
    ];
  }

}