HelpDesk.php 4.17 KB
Newer Older
1
2
3
<?php
/**
 * @file
4
 * Contains \Drupal\query_example\Controller\QueryExampleController
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 */

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 Symfony\Component\HttpFoundation\RedirectResponse;
use Drupal\Core\Database\Database;
use Drupal\Core\Database\Connection;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\TypedData\Plugin\DataType\TimeStamp;
use Drupal\Core\Language\LanguageManagerInterface;

class HelpDesk extends ControllerBase {

Open Source Developer's avatar
counter    
Open Source Developer committed
25
26
    protected $entity_query;
    protected $entityTypeManager;
27
28
29
30
31
    protected $logger;
    protected $connection;

    public function __construct(
        EntityTypeManagerInterface $entityTypeManager,
Open Source Developer's avatar
counter    
Open Source Developer committed
32
33
34
35
        QueryFactory $entity_query,
        Connection $connection,
        LoggerChannelFactoryInterface $loggerChannel
    ) {
36
37
38
39
40
        $this->entityTypeManager = $entityTypeManager;
        $this->entity_query = $entity_query;
        $connection = Database::getConnection();
        $this->connection = $connection;
        $this->logger = $loggerChannel->get('epal');
41
42
43
44
45
46
    }

    public static function create(ContainerInterface $container)
    {
        return new static(
            $container->get('entity_type.manager'),
Open Source Developer's avatar
counter    
Open Source Developer committed
47
48
49
            $container->get('entity.query'),
            $container->get('database'),
            $container->get('logger.factory')
50
51
52
53
54
55
56
57
58
        );
    }

	private function respondWithStatus($arr, $s) {
		$res = new JsonResponse($arr);
		$res->setStatusCode($s);
		return $res;
	}

59
    public function sendEmail(Request $request)
60
61
62
    {
        if (!$request->isMethod('POST')) {
			return $this->respondWithStatus([
63
64
                "message" => t("Method Not Allowed")
            ], Response::HTTP_METHOD_NOT_ALLOWED);
65
    	}
66

67
68
69
        $postData = null;
        if ($content = $request->getContent()) {
            $postData = json_decode($content);
70

71
72
73
74
75
76
77
78
79
80
                $this->sendEmailToHelpDesk($postData->userEmail, $postData->userName, $postData->userMessage,$postData->userSurname);
            return $this->respondWithStatus([
                'error_code' => 0,
            ], Response::HTTP_OK);
        }
        else {
            return $this->respondWithStatus([
                'message' => t("post with no data"),
            ], Response::HTTP_BAD_REQUEST);
        }
81
82
    }

83
    private function sendEmailToHelpDesk($email, $name, $cont_message, $surname) {
84
85
86

        $mailManager = \Drupal::service('plugin.manager.mail');

87
88
89
90
        $module = 'epal';
        $key = 'help_desk';
        $to = 'dialogos_eek@minedu.gov.gr';
        $params['message'] = '<p>Αποστολέας:'.$email.'</p><p>Όνομα: '.$name.'</p><p>Επώνυμο: '.$surname.'</p><p>Μήνυμα: '.$cont_message .'</p>';
91
92
93
94
95
96
97
98
99
        $langcode = 'el';
        $send = true;

        $mail_sent = $mailManager->mail($module, $key, $to, $langcode, $params, NULL, $send);

        if ($mail_sent) {
            $this->logger->info("Mail Sent successfully.");
        }
        else {
100
            $this->logger->info("There was an error in sending mail.");
101
102
103
104
        }
        return;
    }

105
    public function findTotalStudents(Request $request)
Open Source Developer's avatar
counter    
Open Source Developer committed
106
    {
107
108
109
110
111
112
113
114
        $authToken = $request->headers->get('PHP_AUTH_USER');
        $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
        $user = reset($users);
        if (!$user) {
            return $this->respondWithStatus([
                'message' => t("User not found"),
            ], Response::HTTP_FORBIDDEN);
        }
Open Source Developer's avatar
counter    
Open Source Developer committed
115

116
        $list = array();
Open Source Developer's avatar
counter    
Open Source Developer committed
117

118
119
120
121
122
        $sCon = $this->connection
            ->select('epal_student', 'eStudent')
            ->fields('eStudent', array('id'));
        $numApplications = $sCon->countQuery()->execute()->fetchField();
        array_push($list, (object) array('name' => "Αριθμός Αιτήσεων (συνολικά)", 'numStudents' => $numApplications));
Open Source Developer's avatar
counter    
Open Source Developer committed
123

124
        return $this->respondWithStatus($list, Response::HTTP_OK);
Open Source Developer's avatar
counter    
Open Source Developer committed
125
126
    }

127
}