Commit 394f6e34 authored by Χάρης Παπαδόπουλος's avatar Χάρης Παπαδόπουλος
Browse files

code review-fixes, WIP:parent form

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