code review-fixes, WIP:parent form

parent 73207377
epal_is_logged_in:
path: '/epal/login/check'
defaults:
_controller: '\Drupal\epal\Controller\Login::helloWorld'
requirements:
_access: 'TRUE'
epal_entityapi_test:
path: '/epal/entityapi/test'
user_data:
path: '/epal/userdata'
options:
_auth: [ 'basic_auth' ]
requirements:
_user_is_logged_in: 'TRUE'
defaults:
_controller: '\Drupal\epal\Controller\Login::testQuery'
epal_auth_test:
path: 'epal/auth/test'
options:
_auth: [ 'oauthost' ]
defaults:
_controller: '\Drupal\epal\Controller\Login::helloWorld'
_title: 'Greeting'
_controller: '\Drupal\epal\Controller\CurrentUser::getEpalUserData'
requirements:
_permission: 'access content'
_user_is_logged_in: 'TRUE'
current_user:
path: '/epal/curuser'
options:
_auth: [ 'basic_auth' ]
defaults:
_controller: '\Drupal\epal\Controller\CurrentUser::content'
_controller: '\Drupal\epal\Controller\CurrentUser::getLoginInfo'
requirements:
_user_is_logged_in: 'TRUE'
epal.application_submit:
......
services:
logger.channel.oauth:
parent: 'logger.channel_base'
arguments: ['oauthost']
<?php
/**
* @file
* Contains \Drupal\query_example\Controller\QueryExampleController.
*/
namespace Drupal\epal\Controller;
......@@ -12,238 +8,168 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Drupal\Core\Database\Connection;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
class ApplicationSubmit extends ControllerBase {
protected $entity_query;
protected $entityTypeManager;
protected $logger;
protected $connection;
public function __construct(
EntityTypeManagerInterface $entityTypeManager,
QueryFactory $entity_query,
Connection $connection,
LoggerChannelFactoryInterface $loggerChannel)
{
$this->entityTypeManager = $entityTypeManager;
$this->entity_query = $entity_query;
$this->connection = $connection;
$this->logger = $loggerChannel->get('oauthost');
$this->logger = $loggerChannel->get('epal');
}
public static function create(ContainerInterface $container)
{
return new static(
$container->get('entity.manager'),
$container->get('entity.query'),
$container->get('entity_type.manager'),
$container->get('database'),
$container->get('logger.factory')
);
}
public function appSubmitWORK() {
$db = \Drupal\Core\Database\Database::getConnection();
$transaction = $db->startTransaction();
try {
$student = array('epaluser_id'=> 1, 'name'=> 'ΝΙΚΟΣ', 'studentsurname'=>'ΚΑΤΣΑ', 'status'=> 1);
$entity_manager = \Drupal::entityManager();
$entity_storage_student = $entity_manager->getStorage('epal_student');
$entity_storage_eidikotita = $entity_manager->getStorage('epal_student_course_field');
$entity_object = $entity_storage_student->create($student);
$entity_storage_student->save($entity_object);
$created_student_id = $entity_object->id();
public function appSubmit(Request $request) {
$eidikotita = array('name'=>'record1', 'student_id' => 1, 'coursefield_id' => 4, 'status'=> 1 );
$entity_object = $entity_storage_eidikotita->create($eidikotita);
$entity_storage_eidikotita->save($entity_object);
$created_eidikotites_id = $entity_object->id();
}
catch (\Exception $e) {
$transaction->rollback();
}
}
public function appSubmit2() {
$db = \Drupal\Core\Database\Database::getConnection();
$transaction = $db->startTransaction();
try {
$student = array('epaluser_id'=> 1, 'name'=> 'ΝΙΚΟΣ', 'studentsurname'=>'ΚΑΤΣΑ', 'status'=> 1);
$entity_manager = \Drupal::entityManager();
$entity_storage_student = $entity_manager->getStorage('epal_student');
$entity_storage_course = $entity_manager->getStorage('epal_student_course_field');
$entity_object = $entity_storage_student->create($student);
$entity_storage_student->save($entity_object);
if (!$request->isMethod('POST')) {
return $this->respondWithStatus([
"message" => t("Method Not Allowed")
], Response::HTTP_METHOD_NOT_ALLOWED);
}
$created_student_id = $entity_object->id();
$course = array('name'=>'record1', 'student_id' => 1, 'coursefield_id' => 4, 'status'=> 1 );
$entity_object = $entity_storage_course->create($course);
$entity_storage_course->save($entity_object);
}
$applicationForm = array();
catch (\Exception $e) {
$transaction->rollback();
}
$content = $request->getContent();
}
public function appSubmit() {
$aitisi = array();
$content = \Drupal::request()->getContent();
if (!empty($content)) {
$aitisi = json_decode($content, TRUE);
//$aitisi[0] --> student data, $aitisi[1][] --> epals chosen, $aitisi[2][] --> criteria, $aitisi[3] --> sector or course chosen
$applicationForm = json_decode($content, TRUE);
}
else {
return $this->respondWithStatus([
"message" => t("Bad Request")
], Response::HTTP_BAD_REQUEST);
}
$db = \Drupal\Core\Database\Database::getConnection();
$transaction = $db->startTransaction();
try {
$transaction = $this->connection->startTransaction();
try {
//insert records in entity: epal_student
$entity_manager = \Drupal::entityTypeManager();
$epaluserid = \Drupal::currentUser()->id();
$authToken = $request->headers->get('PHP_AUTH_USER');
$epalUsers = $this->entityTypeManager->getStorage('epal_users')->loadByProperties(array('authtoken' => $authToken));
$epalUser = reset($epalUsers);
$student = array(
//'epaluser_id' => $aitisi[0][epaluser_id],
'epaluser_id' => $epaluserid,
'name' => $aitisi[0][name],
'studentsurname' => $aitisi[0][studentsurname],
//'birthdate' => $aitisi[0][birthdate],
//'fatherfirstname' => $aitisi[0][fatherfirstname],
//'fathersurname' => $aitisi[0][fathersurname],
//'motherfirstname' => $aitisi[0][motherfirstname],
//'mothersurname' => $aitisi[0][mothersurname],
'studentamka' => $aitisi[0][studentamka],
'regionaddress' => $aitisi[0][regionaddress],
'regionarea' => $aitisi[0][regionarea],
'regiontk' => $aitisi[0][regiontk],
'certificatetype' => $aitisi[0][certificatetype],
//'lastam' => $aitisi[0][lastam],
'currentclass' => $aitisi[0][currentclass],
//'currentepal' => $aitisi[0][currentepal],
//'currentsector' => $aitisi[0][currentsector],
'relationtostudent' => $aitisi[0][relationtostudent],
'telnum' => $aitisi[0][telnum]
'epaluser_id' => $epalUser->id(),
'name' => $applicationForm[0][name],
'studentsurname' => $applicationForm[0][studentsurname],
//'birthdate' => $applicationForm[0][birthdate],
//'fatherfirstname' => $applicationForm[0][fatherfirstname],
//'fathersurname' => $applicationForm[0][fathersurname],
//'motherfirstname' => $applicationForm[0][motherfirstname],
//'mothersurname' => $applicationForm[0][mothersurname],
'studentamka' => $applicationForm[0][studentamka],
'regionaddress' => $applicationForm[0][regionaddress],
'regionarea' => $applicationForm[0][regionarea],
'regiontk' => $applicationForm[0][regiontk],
'certificatetype' => $applicationForm[0][certificatetype],
//'lastam' => $applicationForm[0][lastam],
'currentclass' => $applicationForm[0][currentclass],
//'currentepal' => $applicationForm[0][currentepal],
//'currentsector' => $applicationForm[0][currentsector],
'relationtostudent' => $applicationForm[0][relationtostudent],
'telnum' => $applicationForm[0][telnum]
);
$entity_storage_student = $entity_manager->getStorage('epal_student');
$entity_storage_student = $this->entityTypeManager->getStorage('epal_student');
$entity_object = $entity_storage_student->create($student);
$entity_storage_student->save($entity_object);
$created_student_id = $entity_object->id();
//insert records in entity: epal_student_epal_chosen
for ($i = 0; $i < sizeof($aitisi[1]); $i++) {
//insert records in entity: epal_student_epal_chosen
for ($i = 0; $i < sizeof($applicationForm[1]); $i++) {
$epalchosen = array(
//'name' => $aitisi[1][$i][name],
//'name' => $applicationForm[1][$i][name],
'student_id' => $created_student_id,
'epal_id' => $aitisi[1][$i][epal_id],
'choice_no' => $aitisi[1][$i][choice_no]
//'points_for_order' => $aitisi[1][$i][points_for_order],
//'distance_from_epal' => $aitisi[1][$i][distance_from_epal],
//'points_for_distance' => $aitisi[1][$i][points_for_distance],
'epal_id' => $applicationForm[1][$i][epal_id],
'choice_no' => $applicationForm[1][$i][choice_no]
//'points_for_order' => $applicationForm[1][$i][points_for_order],
//'distance_from_epal' => $applicationForm[1][$i][distance_from_epal],
//'points_for_distance' => $applicationForm[1][$i][points_for_distance],
);
$entity_storage_epalchosen = $entity_manager->getStorage('epal_student_epal_chosen');
$entity_storage_epalchosen = $this->entityTypeManager->getStorage('epal_student_epal_chosen');
$entity_object = $entity_storage_epalchosen->create($epalchosen);
$entity_storage_epalchosen->save($entity_object);
}
//insert records in entity: epal_student_moria
for ($i = 0; $i < sizeof($aitisi[2]); $i++) {
//insert records in entity: epal_student_moria
for ($i = 0; $i < sizeof($applicationForm[2]); $i++) {
$criteria = array(
//'name' => $aitisi[2][$i][name],
//'name' => $applicationForm[2][$i][name],
'student_id' => $created_student_id,
'income' => $aitisi[2][$i][income],
'criterio_id' => $aitisi[2][$i][criterio_id],
//'moria' => $aitisi[2][$i][moria],
'income' => $applicationForm[2][$i][income],
'criterio_id' => $applicationForm[2][$i][criterio_id],
//'moria' => $applicationForm[2][$i][moria],
);
$entity_storage_criteria = $entity_manager->getStorage('epal_student_moria');
$entity_storage_criteria = $this->entityTypeManager->getStorage('epal_student_moria');
$entity_object = $entity_storage_criteria->create($criteria);
$entity_storage_criteria->save($entity_object);
}
//insert records in entity: epal_student_course_field (αφορά μαθητές Γ' Λυκείου)
// or: epal_student_sector_field (αφορά μαθητές Β' Λυκείου)
if ($aitisi[0][currentclass] === "Γ' Λυκείου") {
//$course = array('name'=>'record1', 'student_id' => 1, 'coursefield_id' => 4, 'status'=> 1 );
if ($applicationForm[0][currentclass] === "Γ' Λυκείου") {
//$course = array('name
$course = array(
//'name' => $aitisi[3][name],
//'student_id' => $aitis[3][student_i],
'student_id' => $created_student_id,
'coursefield_id' => $aitisi[3][coursefield_id]
'coursefield_id' => $applicationForm[3][coursefield_id]
);
$entity_storage_course = $entity_manager->getStorage('epal_student_course_field');
$entity_storage_course = $this->entityTypeManager->getStorage('epal_student_course_field');
$entity_object = $entity_storage_course->create($course);
$entity_storage_course->save($entity_object);
}
else if ($aitisi[0][currentclass] === "Β' Λυκείου") {
else if ($applicationForm[0][currentclass] === "Β' Λυκείου") {
$sector = array(
//'name' => $aitisi[3][name],
//'name' => $applicationForm[3][name],
'student_id' => $created_student_id,
//'student_id' => $aitis[3][student_i],
'sectorfield_id' => $aitisi[3][sectorfield_id]
'sectorfield_id' => $applicationForm[3][sectorfield_id]
);
$entity_storage_sector = $entity_manager->getStorage('epal_student_sector_field');
$entity_storage_sector = $this->entityTypeManager->getStorage('epal_student_sector_field');
$entity_object = $entity_storage_sector->create($sector);
$entity_storage_sector->save($entity_object);
}
$returnmsg = "Επιτυχής καταχώρηση στοιχείων αίτησης!";
$response = new JsonResponse([$returnmsg]);
return $response;
return $this->respondWithStatus([
"message" => t("Application saved successfully")
], Response::HTTP_OK);
}
catch (\Exception $e) {
$this->logger->warning($e->getMessage());
$transaction->rollback();
$returnmsg = "Αποτυχία καταχώρησης στοιχείων αίτησης!";
$response = new JsonResponse([$returnmsg]);
return $response;
return $this->respondWithStatus([
"message" => t("An unexpected problem occured")
], Response::HTTP_INTERNAL_SERVER_ERROR);
}
}
private function respondWithStatus($arr, $s) {
$res = new JsonResponse($arr);
$res->setStatusCode($s);
return $res;
}
}
......@@ -2,59 +2,84 @@
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;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class CurrentUser extends ControllerBase
{
protected $entityTypeManager;
public function __construct(EntityTypeManagerInterface $entityTypeManager)
{
$this->entityTypeManager = $entityTypeManager;
}
public static function create(ContainerInterface $container)
{
return new static(
$container->get('entity_type.manager')
);
}
class CurrentUser extends ControllerBase {
protected $entityTypeManager;
public function __construct(EntityTypeManagerInterface $entityTypeManager )
public function getLoginInfo(Request $request)
{
$this->entityTypeManager1 = $entityTypeManager;
$authToken = $request->headers->get('PHP_AUTH_USER');
$epalUsers = $this->entityTypeManager->getStorage('epal_users')->loadByProperties(array('authtoken' => $authToken));
$epalUser = reset($epalUsers);
if ($epalUser) {
return $this->respondWithStatus([
'name' => $epalUser->name->value,
], Response::HTTP_OK);
} else {
return $this->respondWithStatus([
'message' => t("EPAL user not found"),
], Response::HTTP_FORBIDDEN);
}
}
public function getEpalUserData(Request $request)
{
$authToken = $request->headers->get('PHP_AUTH_USER');
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity_type.manager'),
$container->get('entity.query'),
$container->get('entity_field.manager')
);
}
$epalUsers = $this->entityTypeManager->getStorage('epal_users')->loadByProperties(array('authtoken' => $authToken));
$epalUser = reset($epalUsers);
if ($epalUser) {
$user = $this->entityTypeManager->getStorage('user')->load($epalUser->user_id->target_id);
if ($user) {
$userName = $epalUser->name->value;
$userSurname = $epalUser->surname->value;
$userFathername = $epalUser->fathername->value;
$userMothername = $epalUser->mothername->value;
$userEmail = $user->mail->value;
return $this->respondWithStatus([
'userName' => mb_substr($epalUser->name->value,0,4,'UTF-8') !== "####" ? $epalUser->name->value : '',
'userSurname' => mb_substr($epalUser->surname->value,0,4,'UTF-8') !== "####" ? $epalUser->surname->value : '',
'userFathername' => mb_substr($epalUser->fathername->value,0,4,'UTF-8') !== "####" ? $epalUser->fathername->value : '',
'userMothername' => mb_substr($epalUser->mothername->value,0,4,'UTF-8') !== "####" ? $epalUser->mothername->value : '',
'userEmail' => mb_substr($user->mail->value,0,4,'UTF-8') !== "####" ? $user->mail->value : '',
], Response::HTTP_OK);
} else {
return $this->respondWithStatus([
'message' => t("user not found"),
], Response::HTTP_INTERNAL_SERVER_ERROR);
}
public function content(Request $request) {
$authToken = $request->headers->get('PHP_AUTH_USER');
$epalUsers = $this->entityTypeManager1->getStorage('epal_users')->loadByProperties(array('authtoken' => $authToken));
$epalUser = reset($epalUsers);
if ($epalUser)
{
$currentUserName = $epalUser->name->value;
$response = new JsonResponse(['name' =>$currentUserName]);
}
else {
$response = new Response();
$response->setContent('forbidden');
$response->setStatusCode(Response::HTTP_FORBIDDEN);
$response->headers->set('Content-Type', 'application/json');
}
return $response;
}
} else {
return $this->respondWithStatus([
'message' => t("EPAL user not found"),
], Response::HTTP_FORBIDDEN);
}
}
private function respondWithStatus($arr, $s) {
$res = new JsonResponse($arr);
$res->setStatusCode($s);
return $res;
}
}
<?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;
use Drupal\Core\Entity\EntityFieldManagerInterface;
/**
* Controller routines for page example routes.
*/
class Login extends ControllerBase {
protected $query_factory;
protected $entityTypeManager;
protected $entityFieldManager;
public function __construct(EntityTypeManagerInterface $entityTypeManager, QueryFactory $query_factory, EntityFieldManagerInterface $entityFieldManager) {
// public function __construct(QueryFactory $query_factory) {
$this->entityTypeManager = $entityTypeManager;
$this->query_factory = $query_factory;
$this->entityFieldManager = $entityFieldManager;
}
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity_type.manager'),
$container->get('entity.query'),
$container->get('entity_field.manager')
);
}
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 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;
}
}
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;
}
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()
]
];
}
}