ApplicationSubmit.php 6.88 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
				'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],
86
                'guardian_name' => $applicationForm[0][cu_name],
87
                'guardian_surname' => $applicationForm[0][cu_surname],
88 89 90 91
                'guardian_fathername' => $applicationForm[0][cu_fathername],
                'guardian_mothername' => $applicationForm[0][cu_mothername],
                'agreement' => $applicationForm[0][disclaimer_checked],

92 93
				//'currentepal' => $applicationForm[0][currentepal],
				//'currentsector' => $applicationForm[0][currentsector],
94
        'points' => $applicationForm[0][points],
95 96
				'relationtostudent' => $applicationForm[0][relationtostudent],
				'telnum' => $applicationForm[0][telnum]
97
            );
98 99

			$entity_storage_student = $this->entityTypeManager->getStorage('epal_student');
100 101 102 103
			$entity_object = $entity_storage_student->create($student);
			$entity_storage_student->save($entity_object);

			$created_student_id = $entity_object->id();
104 105 106

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

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

135 136
			//insert records in entity: 	epal_student_course_field (αφορά μαθητές Γ' Λυκείου)
			//						or:		epal_student_sector_field (αφορά μαθητές Β' Λυκείου)
137 138

      //if ($applicationForm[0][currentclass] === "Γ' Λυκείου")	{
139
      if ($applicationForm[0][currentclass] === "3" || $applicationForm[0][currentclass] === "4" )	{
140
				//$course =  array('name
141 142 143 144
				$course = array(
					//'name' => $aitisi[3][name],
					//'student_id' => $aitis[3][student_i],
					'student_id' => $created_student_id,
145
					'coursefield_id' => $applicationForm[3][coursefield_id]
146
				);
147 148

				$entity_storage_course = $this->entityTypeManager->getStorage('epal_student_course_field');
149 150 151
				$entity_object = $entity_storage_course->create($course);
				$entity_storage_course->save($entity_object);
			}
152

153 154
			//else if ($applicationForm[0][currentclass] === "Β' Λυκείου")	{
      else if ($applicationForm[0][currentclass] === "2")	{
155
				$sector = array(
156
					//'name' => $applicationForm[3][name],
157 158
					'student_id' => $created_student_id,
					//'student_id' => $aitis[3][student_i],
159
					'sectorfield_id' => $applicationForm[3][sectorfield_id]
160
				);
161 162

				$entity_storage_sector = $this->entityTypeManager->getStorage('epal_student_sector_field');
163 164 165
				$entity_object = $entity_storage_sector->create($sector);
				$entity_storage_sector->save($entity_object);
			}
166 167 168
			return $this->respondWithStatus([
					"message" => t("Application saved successfully")
				], Response::HTTP_OK);
169 170 171 172 173
		}

		catch (\Exception $e) {
			$this->logger->warning($e->getMessage());
			$transaction->rollback();
174 175 176
			return $this->respondWithStatus([
					"message" => t("An unexpected problem occured")
				], Response::HTTP_INTERNAL_SERVER_ERROR);
177 178 179
		}
	 }

180 181 182 183 184
	 private function respondWithStatus($arr, $s) {
         $res = new JsonResponse($arr);
         $res->setStatusCode($s);
         return $res;
     }
185
}