PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://github.com/casoetan/ServerGroveLiveChat
PHP | 354 lines | 270 code | 67 blank | 17 comment | 32 complexity | bc2da065ecce64cdba154a10786409a0 MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-3.0, ISC, BSD-3-Clause
  1. <?php
  2. namespace ServerGrove\SGLiveChatBundle\Controller;
  3. use ServerGrove\SGLiveChatBundle\Document\Operator\Department;
  4. use Doctrine\ODM\MongoDB\Mapping\Document;
  5. use ServerGrove\SGLiveChatBundle\Form\OperatorDepartmentForm;
  6. use ServerGrove\SGLiveChatBundle\Form\OperatorForm;
  7. use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
  8. use Symfony\Component\Security\SecurityContext;
  9. use Symfony\Component\Form\PasswordField;
  10. use Symfony\Component\Form\TextField;
  11. use ServerGrove\SGLiveChatBundle\Document\Operator;
  12. use Symfony\Component\Form\Form;
  13. use ServerGrove\SGLiveChatBundle\Controller\BaseController;
  14. use ServerGrove\SGLiveChatBundle\Document\Session as ChatSession;
  15. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  16. /**
  17. * Description of AdminController
  18. *
  19. * @author Ismael Ambrosi<ismael@servergrove.com>
  20. */
  21. class AdminController extends BaseController
  22. {
  23. private function createLoginForm($operator = null)
  24. {
  25. $form = new Form('login', array(
  26. 'validator' => $this->get('validator')));
  27. $form->add(new TextField('email'));
  28. $form->add(new PasswordField('passwd'));
  29. return $form;
  30. }
  31. private function isLogged()
  32. {
  33. return $this->getSessionStorage()->get('_operator');
  34. }
  35. private function checkLogin()
  36. {
  37. if (!$this->isLogged()) {
  38. return $this->forward('SGLiveChatBundle:Admin:login');
  39. }
  40. $operator = $this->getOperator();
  41. if (!$operator) {
  42. return $this->forward('SGLiveChatBundle:Admin:logout');
  43. }
  44. $operator->setIsOnline(true);
  45. $this->getDocumentManager()->persist($operator);
  46. $this->getDocumentManager()->flush();
  47. return null;
  48. }
  49. /**
  50. * @todo Search about security in Symfony2
  51. */
  52. public function checkLoginAction()
  53. {
  54. $form = $this->createLoginForm(new Operator());
  55. $form->bind($this->get('request'));
  56. if (!$form->isValid()) {
  57. return $this->redirect($this->generateUrl("_security_login", array(
  58. 'e' => __LINE__)));
  59. }
  60. try {
  61. /* @var $operator ServerGrove\SGLiveChatBundle\Document\Operator */
  62. $operator = $this->getDocumentManager()->getRepository('SGLiveChatBundle:Operator')->loadUserByUsername($form->get('email')->getDisplayedData());
  63. if (!$operator->encodePassword($form->get('passwd')->getDisplayedData(), $operator->getSalt())) {
  64. throw new UsernameNotFoundException('Invalid password');
  65. }
  66. $this->getSessionStorage()->set('_operator', $operator->getId());
  67. $operator->setIsOnline(true);
  68. $this->getDocumentManager()->persist($operator);
  69. $this->getDocumentManager()->flush();
  70. } catch (UsernameNotFoundException $e) {
  71. $this->getSessionStorage()->setFlash('_error', $e->getMessage());
  72. return $this->redirect($this->generateUrl("_security_login", array(
  73. 'e' => __LINE__)));
  74. }
  75. return $this->redirect($this->generateUrl("sglc_admin_index"));
  76. }
  77. public function indexAction()
  78. {
  79. if (!is_null($response = $this->checkLogin())) {
  80. return $response;
  81. }
  82. return $this->redirect($this->generateUrl('sglc_admin_console_sessions'));
  83. }
  84. public function loginAction()
  85. {
  86. $errorMsg = $this->getSessionStorage()->getFlash('_error');
  87. $form = $this->createLoginForm();
  88. return $this->renderTemplate('SGLiveChatBundle:Admin:login.html.twig', array(
  89. 'form' => $form,
  90. 'errorMsg' => $errorMsg));
  91. }
  92. public function logoutAction()
  93. {
  94. if ($this->isLogged()) {
  95. $operator = $this->getOperator();
  96. if ($operator) {
  97. $operator->setIsOnline(false);
  98. $this->getDocumentManager()->persist($operator);
  99. $this->getDocumentManager()->flush();
  100. }
  101. }
  102. $this->getSessionStorage()->remove('_operator');
  103. if (!is_null($response = $this->checkLogin())) {
  104. return $response;
  105. }
  106. return $this->redirect($this->generateUrl("_security_login"));
  107. }
  108. private function getRequestedChats()
  109. {
  110. return $this->getDocumentManager()->getRepository('SGLiveChatBundle:Session')->getRequestedChats();
  111. }
  112. private function getRequestedChatsArray()
  113. {
  114. return $this->getDocumentManager()->getRepository('SGLiveChatBundle:Session')->getRequestedChatsArray();
  115. }
  116. public function sessionsAction()
  117. {
  118. if (!is_null($response = $this->checkLogin())) {
  119. return $response;
  120. }
  121. $this->getDocumentManager()->getRepository('SGLiveChatBundle:Session')->closeSessions();
  122. return $this->renderTemplate('SGLiveChatBundle:Admin:requests.html.twig', array(
  123. 'chats' => $this->getRequestedChats()));
  124. }
  125. public function sessionsApiAction($_format)
  126. {
  127. return $this->renderTemplate('SGLiveChatBundle:Admin:Sessions.' . $_format . '.twig');
  128. }
  129. public function sessionsServiceAction()
  130. {
  131. if (!is_null($response = $this->checkLogin())) {
  132. $this->getResponse()->setStatusCode(401);
  133. $this->getResponse()->setContent('');
  134. return $this->getResponse();
  135. }
  136. $this->getDocumentManager()->getRepository('SGLiveChatBundle:Session')->closeSessions();
  137. $this->getResponse()->headers->set('Content-type', 'application/json');
  138. $json = array();
  139. $json['requests'] = $this->getRequestedChatsArray();
  140. $json['count']['requests'] = count($json['requests']);
  141. $json['visits'] = $this->getDocumentManager()->getRepository('SGLiveChatBundle:Visit')->getLastVisitsArray();
  142. $json['count']['visits'] = count($json['visits']);
  143. $json['count']['online_operators'] = $this->getDocumentManager()->getRepository('SGLiveChatBundle:Operator')->getOnlineOperatorsCount();
  144. $this->getResponse()->setContent(json_encode($json));
  145. return $this->getResponse();
  146. }
  147. public function requestedChatsAction($_format)
  148. {
  149. if (!is_null($response = $this->checkLogin())) {
  150. $this->getResponse()->setStatusCode(401);
  151. $this->getResponse()->setContent('');
  152. return $this->getResponse();
  153. }
  154. $this->getDocumentManager()->getRepository('SGLiveChatBundle:Session')->closeSessions();
  155. if ($_format == 'json') {
  156. $this->getResponse()->headers->set('Content-type', 'application/json');
  157. $this->getResponse()->setContent(json_encode($this->getRequestedChatsArray()));
  158. return $this->getResponse();
  159. }
  160. $chats = $this->getRequestedChats();
  161. return $this->renderTemplate('SGLiveChatBundle:Admin:requestedChats.' . $_format . '.twig', array(
  162. 'chats' => $chats));
  163. }
  164. public function currentVisitsAction($_format)
  165. {
  166. if (!is_null($response = $this->checkLogin())) {
  167. $this->getResponse()->setStatusCode(401);
  168. $this->getResponse()->setContent('');
  169. return $this->getResponse();
  170. }
  171. if ($_format == 'json') {
  172. $visits = $this->getDocumentManager()->getRepository('SGLiveChatBundle:Visit')->getLastVisitsArray();
  173. $this->getResponse()->setContent(json_encode($visits));
  174. return $this->getResponse();
  175. }
  176. throw new NotFoundHttpException('Not supported format');
  177. return $this->renderTemplate('SGLiveChatBundle:Admin:currentVisits.' . $_format . '.twig', array(
  178. 'visits' => $visits));
  179. }
  180. /**
  181. * @return ServerGrove\SGLiveChatBundle\Document\Session
  182. */
  183. private function getChatSession($id)
  184. {
  185. return $this->getDocumentManager()->getRepository('SGLiveChatBundle:Session')->find($id);
  186. }
  187. public function closeChatAction($id)
  188. {
  189. if (($chat = $this->getChatSession($id)) !== false) {
  190. $chat->close();
  191. $this->getDocumentManager()->persist($chat);
  192. $this->getDocumentManager()->flush();
  193. }
  194. return $this->redirect($this->generateUrl('sglc_admin_console_sessions'));
  195. }
  196. public function operatorsAction()
  197. {
  198. if (!is_null($response = $this->checkLogin())) {
  199. return $response;
  200. }
  201. $operators = $this->getDocumentManager()->getRepository('SGLiveChatBundle:Operator')->findAll();
  202. $msg = $this->getSessionStorage()->getFlash('msg', '');
  203. return $this->renderTemplate('SGLiveChatBundle:Admin:operators.html.twig', array(
  204. 'operators' => $operators,
  205. 'msg' => $msg));
  206. }
  207. public function operatorDepartmentAction($id = null)
  208. {
  209. if (!is_null($response = $this->checkLogin())) {
  210. return $response;
  211. }
  212. $message = null;
  213. if ($id) {
  214. $department = $this->getDocumentManager()->find('SGLiveChatBundle:Operator\Department', $id);
  215. } else {
  216. $department = new Department();
  217. }
  218. $form = new OperatorDepartmentForm('department', $department, $this->get('validator'));
  219. switch ($this->getRequest()->getMethod()) {
  220. case 'POST':
  221. case 'PUT':
  222. $params = $this->getRequest()->request->get($form->getName());
  223. if (!empty($params['name'])) {
  224. $department->setName($params['name']);
  225. $department->setIsActive(isset($params['isActive']) && $params['isActive']);
  226. $this->getDocumentManager()->persist($department);
  227. $this->getDocumentManager()->flush();
  228. $this->getSessionStorage()->setFlash('msg', 'The department has been successfully updated');
  229. return $this->redirect($this->generateUrl('sglc_admin_operator_departments'));
  230. }
  231. //}
  232. break;
  233. case 'DELETE':
  234. break;
  235. }
  236. return $this->renderTemplate('SGLiveChatBundle:Admin:operator-department.html.twig', array(
  237. 'department' => $department,
  238. 'form' => $form));
  239. }
  240. public function operatorDepartmentsAction()
  241. {
  242. $this->checkLogin();
  243. $departments = $this->getDocumentManager()->getRepository('SGLiveChatBundle:Operator\Department')->findAll();
  244. $msg = $this->getSessionStorage()->getFlash('msg', '');
  245. return $this->renderTemplate('SGLiveChatBundle:Admin:operator-departments.html.twig', array(
  246. 'departments' => $departments,
  247. 'msg' => $msg));
  248. }
  249. /**
  250. *
  251. */
  252. public function operatorAction($id = null)
  253. {
  254. if (!is_null($response = $this->checkLogin())) {
  255. return $response;
  256. }
  257. $message = null;
  258. if ($id) {
  259. $operator = $this->getDocumentManager()->find('SGLiveChatBundle:Operator', $id);
  260. } else {
  261. $operator = new Operator();
  262. }
  263. $form = new OperatorForm('operator', $operator, $this->get('validator'));
  264. switch ($this->getRequest()->getMethod()) {
  265. case 'POST':
  266. case 'PUT':
  267. $params = $this->getRequest()->request->get($form->getName());
  268. if (!empty($params['name']) && !empty($params['email']['first']) && !empty($params['passwd']['first'])) {
  269. $operator->setName($params['name']);
  270. $operator->setEmail($params['email']['first']);
  271. $operator->setPasswd($params['passwd']['first']);
  272. $operator->setIsActive(isset($params['isActive']) && $params['isActive']);
  273. $this->getDocumentManager()->persist($operator);
  274. $this->getDocumentManager()->flush();
  275. $this->getSessionStorage()->setFlash('msg', 'The operator has been successfully updated');
  276. return $this->redirect($this->generateUrl('sglc_admin_operators'));
  277. }
  278. //}
  279. break;
  280. case 'DELETE':
  281. break;
  282. }
  283. return $this->renderTemplate('SGLiveChatBundle:Admin:operator.html.twig', array(
  284. 'operator' => $operator,
  285. 'form' => $form));
  286. }
  287. }