forked from bichotll/slimchat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
executable file
·85 lines (67 loc) · 2.33 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
//require 'vendor/autoload.php';
//doctrine
require_once "bootstrap.php";
require_once 'entities/Msn.php';
$app = new \Slim\Slim(array(
'templates.path' => './templates'
));
//html
$app->get('/', function () use ($app) {
$app->render('/index.php');
});
//update avatar
$app->post('/avatar', function() use ($app, $entityManager){
$qb = $entityManager->createQueryBuilder();
$qb->update('models\Msn', 'm')
->set('m.avatar_URL', $qb->expr()->literal($_POST['avatar_URL']))
->where('m.user = ?1')
->setParameter(1, $_POST['user']);
$query = $qb->getQuery()->execute();
});
//send_msn
$app->post('/send_msn', function() use ($app, $entityManager){
$msn = new Msn();
$date = new DateTime('now');
$msn->setMsn( $_POST['msn'] );
$msn->setUser( $_POST['user'] );
$msn->setRoom( $_POST['room'] );
$msn->setEnviat($date->format('H:i'));
$msn->setAvatarURL($_POST['avatar_URL']);
$entityManager->persist($msn);
$entityManager->flush();
});
//refresh
$app->post('/refresh', function() use ($app, $entityManager){
$qb = $entityManager->createQueryBuilder();
$qb->add('select', 'm.id')
->add('from', 'Msn m')
->add('select', 'm')
->add('where', 'm.room = :r')
->add('orderBy', 'm.id DESC')
->setMaxResults( '1' )
->setParameter('r', $_POST['room']);
$query = $qb->getQuery()->getResult();
$qb2 = $entityManager->createQueryBuilder();
if ( !isset($_POST['last_id_group']) )
$_POST['last_id_group'] = 0;
if ( $query[0]->getId() != $_POST['last_id_group'] ){
$qb2->add('select', 'm.id')
->add('from', 'Msn m')
->add('select', 'm')
->add('where', 'm.room = :room and m.id > :lastid')
->add('orderBy', 'm.id DESC')
->setMaxResults( '10' )
->setParameters(
array(
'room' => $_POST['room'],
'lastid' => $_POST['last_id_group']
)
);
$qry = $qb2->getQuery()->getArrayResult();
echo json_encode($qry);
} else {
print true;
}
});
$app->run();