ApplicationSubmit.php 6.31 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
<?php

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\Database\Connection;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;

class ApplicationSubmit extends ControllerBase {
17

18 19 20
    protected $entityTypeManager;
    protected $logger;
    protected $connection;
21

22 23 24 25 26 27 28
	public function __construct(
		EntityTypeManagerInterface $entityTypeManager,
		Connection $connection,
		LoggerChannelFactoryInterface $loggerChannel)
		{
			$this->entityTypeManager = $entityTypeManager;
			$this->connection = $connection;
29
			$this->logger = $loggerChannel->get('epal');
30
    }
31

32 33 34
	public static function create(ContainerInterface $container)
    {
        return new static(
35
          $container->get('entity_type.manager'),
36 37 38 39 40
          $container->get('database'),
          $container->get('logger.factory')
      );
    }

41
 	public function appSubmit(Request $request) {
42

43 44 45 46 47
		if (!$request->isMethod('POST')) {
			return $this->respondWithStatus([
					"message" => t("Method Not Allowed")
				], Response::HTTP_METHOD_NOT_ALLOWED);
    	}
48

49
		$applicationForm = array();
50

51
		$content = $request->getContent();
52 53

		if (!empty($content)) {
54 55 56 57 58 59
			$applicationForm = json_decode($content, TRUE);
		}
		else {
			return $this->respondWithStatus([
					"message" => t("Bad Request")
				], Response::HTTP_BAD_REQUEST);
60 61
		}

62 63
		$transaction = $this->connection->startTransaction();
		try {
64
			//insert records in entity: epal_student
65 66 67 68 69

			$authToken = $request->headers->get('PHP_AUTH_USER');
	        $epalUsers = $this->entityTypeManager->getStorage('epal_users')->loadByProperties(array('authtoken' => $authToken));
	        $epalUser = reset($epalUsers);

70
			$student = array(
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
				'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]
90
            );
91 92

			$entity_storage_student = $this->entityTypeManager->getStorage('epal_student');
93 94 95 96
			$entity_object = $entity_storage_student->create($student);
			$entity_storage_student->save($entity_object);

			$created_student_id = $entity_object->id();
97 98 99

			//insert records in entity: epal_student_epal_chosen
			for ($i = 0; $i < sizeof($applicationForm[1]); $i++) {
100
				$epalchosen = array(
101
					//'name' => $applicationForm[1][$i][name],
102
					'student_id' => $created_student_id,
103 104 105 106 107
					'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],
108
				);
109
				$entity_storage_epalchosen = $this->entityTypeManager->getStorage('epal_student_epal_chosen');
110 111 112
				$entity_object = $entity_storage_epalchosen->create($epalchosen);
				$entity_storage_epalchosen->save($entity_object);
			}
113 114 115

			//insert records in entity: epal_student_moria
			for ($i = 0; $i < sizeof($applicationForm[2]); $i++) {
116
				$criteria = array(
117
					//'name' => $applicationForm[2][$i][name],
118
					'student_id' => $created_student_id,
119 120 121
					'income' => $applicationForm[2][$i][income],
					'criterio_id' => $applicationForm[2][$i][criterio_id],
					//'moria' => $applicationForm[2][$i][moria],
122
				);
123
				$entity_storage_criteria = $this->entityTypeManager->getStorage('epal_student_moria');
124 125 126
				$entity_object = $entity_storage_criteria->create($criteria);
				$entity_storage_criteria->save($entity_object);
			}
127

128 129
			//insert records in entity: 	epal_student_course_field (αφορά μαθητές Γ' Λυκείου)
			//						or:		epal_student_sector_field (αφορά μαθητές Β' Λυκείου)
130 131
			if ($applicationForm[0][currentclass] === "Γ' Λυκείου")	{
				//$course =  array('name
132 133 134 135
				$course = array(
					//'name' => $aitisi[3][name],
					//'student_id' => $aitis[3][student_i],
					'student_id' => $created_student_id,
136
					'coursefield_id' => $applicationForm[3][coursefield_id]
137
				);
138 139

				$entity_storage_course = $this->entityTypeManager->getStorage('epal_student_course_field');
140 141 142
				$entity_object = $entity_storage_course->create($course);
				$entity_storage_course->save($entity_object);
			}
143 144

			else if ($applicationForm[0][currentclass] === "Β' Λυκείου")	{
145
				$sector = array(
146
					//'name' => $applicationForm[3][name],
147 148
					'student_id' => $created_student_id,
					//'student_id' => $aitis[3][student_i],
149
					'sectorfield_id' => $applicationForm[3][sectorfield_id]
150
				);
151 152

				$entity_storage_sector = $this->entityTypeManager->getStorage('epal_student_sector_field');
153 154 155
				$entity_object = $entity_storage_sector->create($sector);
				$entity_storage_sector->save($entity_object);
			}
156 157 158
			return $this->respondWithStatus([
					"message" => t("Application saved successfully")
				], Response::HTTP_OK);
159 160 161 162 163
		}

		catch (\Exception $e) {
			$this->logger->warning($e->getMessage());
			$transaction->rollback();
164 165 166
			return $this->respondWithStatus([
					"message" => t("An unexpected problem occured")
				], Response::HTTP_INTERNAL_SERVER_ERROR);
167 168 169
		}
	 }

170 171 172 173 174
	 private function respondWithStatus($arr, $s) {
         $res = new JsonResponse($arr);
         $res->setStatusCode($s);
         return $res;
     }
175
}