ApplicationSubmit.php 6.36 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
				'epaluser_id' => $epalUser->id(),
				'name' => $applicationForm[0][name],
				'studentsurname' => $applicationForm[0][studentsurname],
74
75
76
77
78
				'birthdate' => $applicationForm[0][studentbirthdate],
				'fatherfirstname' => $applicationForm[0][fatherfirstname],
				'fathersurname' => $applicationForm[0][fathersurname],
				'motherfirstname' => $applicationForm[0][motherfirstname],
				'mothersurname' => $applicationForm[0][mothersurname],
79
80
81
82
83
84
85
86
87
				'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],
88
        'points' => $applicationForm[0][points],
89
90
				'relationtostudent' => $applicationForm[0][relationtostudent],
				'telnum' => $applicationForm[0][telnum]
91
            );
92
93

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

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

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

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

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

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

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

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

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

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