ninegate/src/ninegate-1.0/src/Cadoles/WebsocketBundle/Topic/WebsocketTopic.php

148 lines
5.4 KiB
PHP

<?php
namespace Cadoles\WebsocketBundle\Topic;
use Gos\Bundle\WebSocketBundle\Topic\TopicInterface;
use Ratchet\ConnectionInterface;
use Ratchet\Wamp\Topic;
use Gos\Bundle\WebSocketBundle\Router\WampRequest;
use Doctrine\ORM\EntityManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Gos\Bundle\WebSocketBundle\Client\ClientManipulatorInterface;
use Cadoles\WebsocketBundle\Entity\Message;
class WebsocketTopic implements TopicInterface
{
protected $em;
protected $container;
protected $clientManipulator;
public function __construct(ClientManipulatorInterface $clientManipulator, EntityManager $em, ContainerInterface $container) {
$this->clientManipulator = $clientManipulator;
$this->em = $em;
$this->container = $container;
}
/**
* This will receive any Subscription requests for this topic.
*
* @param ConnectionInterface $connection
* @param Topic $topic
* @param WampRequest $request
* @return void
*/
public function onSubscribe(ConnectionInterface $connection, Topic $topic, WampRequest $request)
{
//this will broadcast the message to ALL subscribers of this topic.
$topic->broadcast(['log' => $connection->resourceId." has joined ".$topic->getId()]);
}
/**
* This will receive any UnSubscription requests for this topic.
*
* @param ConnectionInterface $connection
* @param Topic $topic
* @param WampRequest $request
* @return void
*/
public function onUnSubscribe(ConnectionInterface $connection, Topic $topic, WampRequest $request)
{
//this will broadcast the message to ALL subscribers of this topic.
$topic->broadcast(['log' => $connection->resourceId . " has left " . $topic->getId()]);
}
/**
* This will receive any Publish requests for this topic.
*
* @param ConnectionInterface $connection
* @param Topic $topic
* @param WampRequest $request
* @param $event
* @param array $exclude
* @param array $eligible
* @return mixed|void
*/
public function onPublish(ConnectionInterface $connection, Topic $topic, WampRequest $request, $event, array $exclude, array $eligible)
{
$groupid=$request->getAttributes()->get('group');
if(!is_array($event)) $event=json_decode($event, true);
$group=$this->em->getRepository("CadolesCoreBundle:Group")->find($groupid);
if(!$group) {
$topic->broadcast(['log' => 'group NOT FIND']);
}
$usergroup=$this->em->getRepository("CadolesCoreBundle:UserGroup")->findOneBy(['keyvalue'=>$event["mykey"]]);
if(!$usergroup) {
$topic->broadcast(['log' => $event]);
$topic->broadcast(['log' => 'user '.$event["mykey"].' NOT IN GROUP '.$groupid]);
}
else {
$user=$usergroup->getUser();
}
if($group&&$usergroup&&$user) {
if($event["type"]=="add") {
if(array_key_exists("mail",$event)) {
$mail = $this->container->get('cadoles.core.service.mail');
if($mail) {
$mail_params=array(
"subject" => $event["subject"],
"body_html"=>$event["message"],
"body_text"=>strip_tags($event["message"])
);
// Transformer la liste des destinataires en tableau : [0] tjr vide on l'unset
$to=explode(";",$event["to"]);
unset($to[0]);
$mail->sendEmail("template", $mail_params, $to, $user->getEmail(), $user->getLastname()." ".$user->getFirstname());
$event["message"].="<br><i>Notification envoyée par mail</i>";
}
}
$message=new Message();
$message->setTopic($event["message"]);
$message->setUser($user);
$message->setGroup($group);
$this->em->persist($message);
$this->em->flush();
$return["id"]=$message->getId();
$return["lastname"]=$user->getLastname()." ".$user->getFirstname();
$return["avatar"]=$user->getAvatar();
$return["submitdate"]=$message->getSubmitdate();
$return["message"]=$event["message"];
$return["userid"]=$user->getId();
//this will broadcast the message to ALL subscribers of this topic.
$topic->broadcast(['msg' => $return]);
}
if($event["type"]=="del") {
$message=$this->em->getRepository("CadolesWebsocketBundle:Message")->find($event["id"]);
if($message&&($usergroup->getFgmanager()||$message->getUser()==$user||$user->getRole()=="ROLE_ADMIN"||$user->getRole()=="ROLE_MODO" )) {
$id=$message->getId();
$this->em->remove($message);
$this->em->flush();
$topic->broadcast(['del' => $id]);
}
else $topic->broadcast(['log' => "Suppression interdite"]);
}
}
}
/**
* Like RPC is will use to prefix the channel
* @return string
*/
public function getName()
{
return 'websocket.topic';
}
}