ServerGroveLiveChat /src/ServerGrove/SGLiveChatBundle/Controller/AdminController.php

Language PHP Lines 354
MD5 Hash bc2da065ecce64cdba154a10786409a0
Repository https://github.com/casoetan/ServerGroveLiveChat.git View Raw File
  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
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
<?php

namespace ServerGrove\SGLiveChatBundle\Controller;

use ServerGrove\SGLiveChatBundle\Document\Operator\Department;
use Doctrine\ODM\MongoDB\Mapping\Document;
use ServerGrove\SGLiveChatBundle\Form\OperatorDepartmentForm;
use ServerGrove\SGLiveChatBundle\Form\OperatorForm;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\SecurityContext;
use Symfony\Component\Form\PasswordField;
use Symfony\Component\Form\TextField;
use ServerGrove\SGLiveChatBundle\Document\Operator;
use Symfony\Component\Form\Form;
use ServerGrove\SGLiveChatBundle\Controller\BaseController;
use ServerGrove\SGLiveChatBundle\Document\Session as ChatSession;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

/**
 * Description of AdminController
 *
 * @author Ismael Ambrosi<ismael@servergrove.com>
 */
class AdminController extends BaseController
{

    private function createLoginForm($operator = null)
    {
        $form = new Form('login', array(
                    'validator' => $this->get('validator')));
        $form->add(new TextField('email'));
        $form->add(new PasswordField('passwd'));

        return $form;
    }

    private function isLogged()
    {
        return $this->getSessionStorage()->get('_operator');
    }

    private function checkLogin()
    {
        if (!$this->isLogged()) {
            return $this->forward('SGLiveChatBundle:Admin:login');
        }

        $operator = $this->getOperator();
        if (!$operator) {
            return $this->forward('SGLiveChatBundle:Admin:logout');
        }
        $operator->setIsOnline(true);
        $this->getDocumentManager()->persist($operator);
        $this->getDocumentManager()->flush();

        return null;
    }

    /**
     * @todo Search about security in Symfony2
     */
    public function checkLoginAction()
    {
        $form = $this->createLoginForm(new Operator());
        $form->bind($this->get('request'));

        if (!$form->isValid()) {

            return $this->redirect($this->generateUrl("_security_login", array(
                        'e' => __LINE__)));
        }
        try {
            /* @var $operator ServerGrove\SGLiveChatBundle\Document\Operator */
            $operator = $this->getDocumentManager()->getRepository('SGLiveChatBundle:Operator')->loadUserByUsername($form->get('email')->getDisplayedData());
            if (!$operator->encodePassword($form->get('passwd')->getDisplayedData(), $operator->getSalt())) {
                throw new UsernameNotFoundException('Invalid password');
            }

            $this->getSessionStorage()->set('_operator', $operator->getId());
            $operator->setIsOnline(true);
            $this->getDocumentManager()->persist($operator);
            $this->getDocumentManager()->flush();
        } catch (UsernameNotFoundException $e) {
            $this->getSessionStorage()->setFlash('_error', $e->getMessage());
            return $this->redirect($this->generateUrl("_security_login", array(
                        'e' => __LINE__)));
        }

        return $this->redirect($this->generateUrl("sglc_admin_index"));
    }

    public function indexAction()
    {
        if (!is_null($response = $this->checkLogin())) {
            return $response;
        }

        return $this->redirect($this->generateUrl('sglc_admin_console_sessions'));
    }

    public function loginAction()
    {
        $errorMsg = $this->getSessionStorage()->getFlash('_error');
        $form = $this->createLoginForm();

        return $this->renderTemplate('SGLiveChatBundle:Admin:login.html.twig', array(
            'form' => $form,
            'errorMsg' => $errorMsg));
    }

    public function logoutAction()
    {
        if ($this->isLogged()) {
            $operator = $this->getOperator();
            if ($operator) {
                $operator->setIsOnline(false);
                $this->getDocumentManager()->persist($operator);
                $this->getDocumentManager()->flush();
            }
        }

        $this->getSessionStorage()->remove('_operator');

        if (!is_null($response = $this->checkLogin())) {
            return $response;
        }
        return $this->redirect($this->generateUrl("_security_login"));
    }

    private function getRequestedChats()
    {
        return $this->getDocumentManager()->getRepository('SGLiveChatBundle:Session')->getRequestedChats();
    }

    private function getRequestedChatsArray()
    {
        return $this->getDocumentManager()->getRepository('SGLiveChatBundle:Session')->getRequestedChatsArray();
    }

    public function sessionsAction()
    {
        if (!is_null($response = $this->checkLogin())) {
            return $response;
        }

        $this->getDocumentManager()->getRepository('SGLiveChatBundle:Session')->closeSessions();

        return $this->renderTemplate('SGLiveChatBundle:Admin:requests.html.twig', array(
            'chats' => $this->getRequestedChats()));
    }

    public function sessionsApiAction($_format)
    {
        return $this->renderTemplate('SGLiveChatBundle:Admin:Sessions.' . $_format . '.twig');
    }

    public function sessionsServiceAction()
    {
        if (!is_null($response = $this->checkLogin())) {
            $this->getResponse()->setStatusCode(401);
            $this->getResponse()->setContent('');
            return $this->getResponse();
        }

        $this->getDocumentManager()->getRepository('SGLiveChatBundle:Session')->closeSessions();

        $this->getResponse()->headers->set('Content-type', 'application/json');

        $json = array();
        $json['requests'] = $this->getRequestedChatsArray();
        $json['count']['requests'] = count($json['requests']);
        $json['visits'] = $this->getDocumentManager()->getRepository('SGLiveChatBundle:Visit')->getLastVisitsArray();
        $json['count']['visits'] = count($json['visits']);
        $json['count']['online_operators'] = $this->getDocumentManager()->getRepository('SGLiveChatBundle:Operator')->getOnlineOperatorsCount();

        $this->getResponse()->setContent(json_encode($json));

        return $this->getResponse();
    }

    public function requestedChatsAction($_format)
    {
        if (!is_null($response = $this->checkLogin())) {
            $this->getResponse()->setStatusCode(401);
            $this->getResponse()->setContent('');
            return $this->getResponse();
        }

        $this->getDocumentManager()->getRepository('SGLiveChatBundle:Session')->closeSessions();

        if ($_format == 'json') {
            $this->getResponse()->headers->set('Content-type', 'application/json');
            $this->getResponse()->setContent(json_encode($this->getRequestedChatsArray()));

            return $this->getResponse();
        }

        $chats = $this->getRequestedChats();

        return $this->renderTemplate('SGLiveChatBundle:Admin:requestedChats.' . $_format . '.twig', array(
            'chats' => $chats));
    }

    public function currentVisitsAction($_format)
    {
        if (!is_null($response = $this->checkLogin())) {
            $this->getResponse()->setStatusCode(401);
            $this->getResponse()->setContent('');
            return $this->getResponse();
        }

        if ($_format == 'json') {
            $visits = $this->getDocumentManager()->getRepository('SGLiveChatBundle:Visit')->getLastVisitsArray();
            $this->getResponse()->setContent(json_encode($visits));

            return $this->getResponse();
        }

        throw new NotFoundHttpException('Not supported format');

        return $this->renderTemplate('SGLiveChatBundle:Admin:currentVisits.' . $_format . '.twig', array(
            'visits' => $visits));
    }

    /**
     * @return ServerGrove\SGLiveChatBundle\Document\Session
     */
    private function getChatSession($id)
    {
        return $this->getDocumentManager()->getRepository('SGLiveChatBundle:Session')->find($id);
    }

    public function closeChatAction($id)
    {
        if (($chat = $this->getChatSession($id)) !== false) {
            $chat->close();
            $this->getDocumentManager()->persist($chat);
            $this->getDocumentManager()->flush();
        }

        return $this->redirect($this->generateUrl('sglc_admin_console_sessions'));
    }

    public function operatorsAction()
    {
        if (!is_null($response = $this->checkLogin())) {
            return $response;
        }

        $operators = $this->getDocumentManager()->getRepository('SGLiveChatBundle:Operator')->findAll();
        $msg = $this->getSessionStorage()->getFlash('msg', '');
        return $this->renderTemplate('SGLiveChatBundle:Admin:operators.html.twig', array(
            'operators' => $operators,
            'msg' => $msg));
    }

    public function operatorDepartmentAction($id = null)
    {
        if (!is_null($response = $this->checkLogin())) {
            return $response;
        }

        $message = null;

        if ($id) {
            $department = $this->getDocumentManager()->find('SGLiveChatBundle:Operator\Department', $id);
        } else {
            $department = new Department();
        }

        $form = new OperatorDepartmentForm('department', $department, $this->get('validator'));

        switch ($this->getRequest()->getMethod()) {
            case 'POST':
            case 'PUT':
                $params = $this->getRequest()->request->get($form->getName());
                if (!empty($params['name'])) {
                    $department->setName($params['name']);
                    $department->setIsActive(isset($params['isActive']) && $params['isActive']);
                    $this->getDocumentManager()->persist($department);
                    $this->getDocumentManager()->flush();
                    $this->getSessionStorage()->setFlash('msg', 'The department has been successfully updated');

                    return $this->redirect($this->generateUrl('sglc_admin_operator_departments'));
                }
                //}
                break;
            case 'DELETE':
                break;
        }

        return $this->renderTemplate('SGLiveChatBundle:Admin:operator-department.html.twig', array(
            'department' => $department,
            'form' => $form));
    }

    public function operatorDepartmentsAction()
    {
        $this->checkLogin();

        $departments = $this->getDocumentManager()->getRepository('SGLiveChatBundle:Operator\Department')->findAll();
        $msg = $this->getSessionStorage()->getFlash('msg', '');

        return $this->renderTemplate('SGLiveChatBundle:Admin:operator-departments.html.twig', array(
            'departments' => $departments,
            'msg' => $msg));
    }

    /**
     *
     */
    public function operatorAction($id = null)
    {
        if (!is_null($response = $this->checkLogin())) {
            return $response;
        }

        $message = null;

        if ($id) {
            $operator = $this->getDocumentManager()->find('SGLiveChatBundle:Operator', $id);
        } else {
            $operator = new Operator();
        }

        $form = new OperatorForm('operator', $operator, $this->get('validator'));

        switch ($this->getRequest()->getMethod()) {
            case 'POST':
            case 'PUT':
                $params = $this->getRequest()->request->get($form->getName());
                if (!empty($params['name']) && !empty($params['email']['first']) && !empty($params['passwd']['first'])) {
                    $operator->setName($params['name']);
                    $operator->setEmail($params['email']['first']);
                    $operator->setPasswd($params['passwd']['first']);
                    $operator->setIsActive(isset($params['isActive']) && $params['isActive']);
                    $this->getDocumentManager()->persist($operator);
                    $this->getDocumentManager()->flush();
                    $this->getSessionStorage()->setFlash('msg', 'The operator has been successfully updated');

                    return $this->redirect($this->generateUrl('sglc_admin_operators'));
                }
                //}
                break;
            case 'DELETE':
                break;
        }

        return $this->renderTemplate('SGLiveChatBundle:Admin:operator.html.twig', array(
            'operator' => $operator,
            'form' => $form));
    }

}
Back to Top