I'm trying to fire an event when I create a calendar item (appointment) in exchange, using PHP-EWS. Currently, I am able to fire an event when an email is sent:
$subscribe_request = new \EWSType_SubscribeType();
$pushSubscription = new \EWSType_PushSubscriptionRequestType();
$pushSubscription->StatusFrequency = 1;
$pushSubscription->URL = 'http://someserver/log.php';
$folderIDs = new \EWSType_NonEmptyArrayOfBaseFolderIdsType();
$eventTypes = new \EWSType_NonEmptyArrayOfNotificationEventTypesType();
$folderIDs->DistinguishedFolderId = new \EWSType_DistinguishedFolderIdType();
$folderIDs->DistinguishedFolderId->Id = \EWSType_DistinguishedFolderIdNameType::INBOX;
$eventTypes->EventType = "NewMailEvent";
$pushSubscription->FolderIds = $folderIDs;
$pushSubscription->EventTypes = $eventTypes;
$subscribe_request->PushSubscriptionRequest = $pushSubscription;
return $this->ews->Subscribe($subscribe_request);
log.php
class ewsService {
public function SendNotification($arg) {
file_put_contents("C:\\exchangelogs\\log_".time().".txt", print_r($arg,1));
$result = new EWSType_SendNotificationResultType();
$result->SubscriptionStatus = 'OK';
//$result->SubscriptionStatus = 'Unsubscribe';
return $result;
}
}
$opts = array();
$server = new SoapServer(
'NotificationService.wsdl',
array('uri' => 'http://someserver/log.php'));
$server->setObject($service = new ewsService());
$server->handle();
Every minute a 'Keep alive message' is sent and the SendNotification function is called. The same thing happens when a mail is sent (using outlook or whatever).
This all works fine.
However, now I want to do the same when a calendar item is created, such as an appointment. I tried changing the DistinguishedFolderIdNameType to CALENDAR and the EventType to CreatedEvent, but I receive no message when an appointment is created.
Any help is greatly appreciated.
Related
I am trying to do a simple operation: create 2 entries; on each creation, first check if the entity exists, create it, if it does not, and then do the adjustment. In some cases (on first such operation) we need to create the entity in the first entry and work with it in the second. Unfortunately, this does not happen and we end up with two entries that are useless. After this initial issue, everything works as expected (i.e. on next iteration the entity is properly found).
Here is the code for the entries:
// Create first entry
$debitCode = 'bank';
$creditCode = 'equity';
// Create entry
$accountEntry = new AccountEntry();
$accountEntry->setAmount($amount);
$debitAccount = $unit->getAccountByType($debitCode);
if (!$debitAccount) {
// Create debit account
$debitAccountType = $em->getRepository('App:AccountType')->findOneBy(['code' => $debitCode]);
$debitAccount = new Account();
$debitAccount->setType($debitAccountType);
$em->persist($debitAccount);
}
$debitAccount->debit($amount);
$accountEntry->setDebitAccount($debitAccount);
$creditAccount = $unit->getAccountByType($creditCode);
if (!$creditAccount) {
// Create credit account
$creditAccountType = $em->getRepository('App:AccountType')->findOneBy(['code' => $creditCode]);
$creditAccount = new Account();
$creditAccount->setType($creditAccountType);
$em->persist($creditAccount);
}
$creditAccount->credit($amount);
$accountEntry->setCreditAccount($creditAccount);
$em->persist($accountEntry);
$em->flush();
// Create second entry
$debitCode2 = 'accountsPayable';
$creditCode2 = 'bank';
$accountEntry2 = new AccountEntry();
$accountEntry2->setAmount($amount);
$debitAccount = $unit->getAccountByType($debitCode2);
if (!$debitAccount) {
// Create debit account
$debitAccountType = $em->getRepository('App:AccountType')->findOneBy(['code' => $debitCode2]);
$debitAccount = new Account();
$debitAccount->setUnit($unit);
$debitAccount->setType($debitAccountType);
$em->persist($debitAccount);
}
$debitAccount->debit($amount);
$accountEntry2->setDebitAccount($debitAccount);
$creditAccount = $unit->getAccountByType($creditCode2);
if (!$creditAccount) {
// Create credit account
$creditAccountType = $em->getRepository('App:AccountType')->findOneBy(['code' => $creditCode2]);
$creditAccount = new Account();
$creditAccount->setUnit($unit);
$creditAccount->setType($creditAccountType);
$em->persist($creditAccount);
}
$creditAccount->credit($amount);
$accountEntry2->setCreditAccount($creditAccount);
$em->persist($accountEntry2);
$em->flush;
And here is the getAccountByType function:
/**
* Get Account by type of account.
*/
public function getAccountByType($code)
{
$filter = function ($account) use ($code) {
if ($account->getType()->getCode() == $code) {
return $account;
}
};
$accounts = $this->accounts->filter($filter)->getValues();
return isset($accounts[0]) ? $accounts[0] : null;
}
And, of course, 30 minutes after I posted the question, I found the answer myself (despite having been banging my head against the wall for a couple of days before posting).
Basically, we need to refresh the $unit entity after persisting/flushing the initial entry:
$em->refresh($unit);
Otherwise, the getAccountByType method apparently does not take into account the changes. So, it appears that entity methods do not take into account flushed changes to the database, if the entity is not refreshed. Probably basic stuff, but I did not know that. I hope this will save someone lots of trouble.
I am not sure whether this question is related to stomp-php or ActiveMQ Docker (running with defaults).
I have a simple Queue helper class written in PHP that handles both sending the message to the queue (Queue::push), as well as consumes it (Queue::fetch). See code below.
As you can see, fetch() should subscribe to the queue, read one message and unsubscribe. The message should be acknowledged automatically (\Stomp\StatefulStomp::subscribe(), 3rd. argument).
For some reason, about 5-7% of the messages are received by the customer twice or even three times. Why messages are delivered multiple times and how to avoid it?
Publisher (pushing 1000 messages):
$mq = new Queue('tcp://activemq:61613','test');
for ($msgCount = 0; $msgCount < 1000; $msgCount++) {
$mq->push('Message #' . $msgCount);
}
Consumer (receiving ~1070 messages):
$mq = new Queue('tcp://activemq:61613','test');
$received = 0;
while (true) {
$message = $mq->fetch();
if (null === $message) { break; }
$received++;
}
Queue class code:
use Stomp\Client;
use Stomp\Network\Connection;
use Stomp\SimpleStomp;
use Stomp\StatefulStomp;
use Stomp\Transport\Message;
class Queue
{
/**
* #var \Stomp\StatefulStomp
*/
private $stomp;
private $queue;
public function __construct($uri, $queue) {
$connection = new Connection('tcp://activemq:61613');
$this->stomp = new StatefulStomp(new Client($connection));
$connection->setReadTimeout(1);
$this->queue = $queue;
}
public function push($body) {
$message = new Message($body, ['activemq.maximumRedeliveries' => 0]);
$this->stomp->send('/queue/' . $this->queue, $message);
}
public function fetch() {
$subscriptionId = $this->stomp->subscribe('/queue/' . $this->queue, null, 'auto', ['activemq.prefetchSize' => 1]);
$msg = $this->stomp->read();
$this->stomp->unsubscribe($subscriptionId);
return $msg;
}
}
I am getting a issue when I remove a event from my application (even from google calendar too). I have my calendar with all my events, I try to remove a event for first time, and it "works": I change the tab of my browser, and see the Google Calendar panel and the event had successfully removed, great!! BUT, if I reload the tab of my application... I still see the event!!!
Also, if I remove the data from the Google Calendar Panel, the application still recover this event on the API call:
$service->events->listEvents($calendar,$eventsParam);
$service is a Google_Service_Calendar object.
Thank you.
EDIT
This is the way how I delete the events:
public function removedate()
{
$service = getService();
$service->events->delete($this->input->post("calendar"), getEventId($service));
}
I am working with codeigniter and I am using a helper made by myself for the most habitual functions, between them these two:
function getService($file=false)
{
$ci =& get_instance();
if($file)
$file = CREDENTIALS_PATH.$file;
else
$file = CREDENTIALS_PATH.$ci->session->userdata("identity").".json";
$ci->g_client->setAccessToken(file_get_contents($file));
return new Google_Service_Calendar($ci->g_client);
}
function getEventId($service)
{
$ci =& get_instance();
$id = $ci->input->post("id");
$dni = $id;
if(!is_numeric($id))
$id = $ci->general_model->getData("users","phone", array("dni"=>$id), true)->phone;
$min = false;
$max = false;
if($ci->input->post("min"))
$min = date("c", strtotime ( '-2 hour' , strtotime ( (new DateTime(explode("GMT", $ci->input->post("min"))[0]))->format("c"))));
if($ci->input->post("max"))
$max = (new DateTime(explode("GMT", $ci->input->post("max"))[0]))->format("c");
$events = getDates($service, $ci->input->post("calendar"), $min, $max);
foreach ($events->getItems() as $event)
{
if($id==$event->getSummary() || $dni==$event->getSummary())
return $event->getId();
}
}
NEW EDIT
One more thing... when I remove a event for first time there is no problem (and the event is DELETED on the google calendar), and when I refresh my app page the event still remains, and when I try to delete it again I get this error:
<br />
<b>Fatal error</b>: Uncaught exception 'Google_Service_Exception' with message 'Error calling DELETE
https://www.googleapis.com/calendar/v3/calendars/cnhkehagcve7rm8s1g88hauer8%40group.calendar.google.com/events/97smpf2320s3lmg8nnnpb9dndc: (410)
Resource has been deleted' in
/var/www/html/prototipo/application/third_party/vendor/google/apiclient/src/Google/Http/REST.php:110
RESOURCE HAS BEEN DELETED... Yes!!! I know!!! why does it remain??????
Your app should not display events with status=cancelled.
To get all events
$events = $service->events->listEvents('<calendar_id>');
To remove any event from calendar
$result = $service->events->delete('<calendar_id>', '<event_id>');
echo "<br>========================<br>";
var_dump($result);
echo "<br>========================<br>";
I try to build a little realtime websocket use-case, where users can login and see all other users logged in, get notified when a new user signs in or an existing user logs out.
For this scenario i use the ZMQ PUSH Socket in my UserController when a user logs in or logs out.
UserConstroller
public function login() {
//... here is the auth code, model call etc...
$aUserData = array();// user data comes from the database with username, logintime, etc....
$context = new \ZMQContext();
$oSocket = $context->getSocket(\ZMQ::SOCKET_PUSH, 'USER_LOGIN_PUSHER'); // use persistent_id
if($oSocket instanceof \ZMQSocket) {
$oSocket->connect("tcp://127.0.0.1:5555"); //
$oSocket->send(json_encode($aUserData));
}
}
public function logout() {
//... here is the logout code, model call etc ....
$aUserData = array();// user data comes from the SESSION with username, logintime, etc....
$context = new \ZMQContext();
$oSocket = $context->getSocket(\ZMQ::SOCKET_PUSH, 'USER_LOGOUT_PUSHER'); // use persistent_id
if($oSocket instanceof \ZMQSocket) {
$oSocket->connect("tcp://127.0.0.1:5555"); //
$oSocket->send(json_encode($aUserData));
}
}
Then i've got a Pusher class like in the Ratchet docs: link
In this class there are two methods: onUserLogin and onUserLogout and of course all the other stuff like
onSubscribe, onOpen, onPublish
UserInformationPusher
public function onUserLogin($aUserData) {
//var_dump("onUserLogin");
$sUserData = json_decode($aUserData, true);
$oTopic = $this->subscribedTopics["user_login"];
if($oTopic instanceof Topic) {
$oTopic->broadcast($sUserData);
} else {
return;
}
}
public function onUserLogout($aUserData) {
//var_dump("onUserLogout");
$entryData = json_decode($aUserData, true);
$oTopic = $this->subscribedTopics["user_logout"];
if($oTopic instanceof Topic) {
$oTopic->broadcast($entryData);
} else {
return;
}
}
The last piece is the WampServer/WsServer/HttpServer with a Loop that listens to the incoming connections. There is also my ZMQ PULL socket
RatchetServerConsole
public function start_server() {
$oPusher = new UserInformationPusher();
$oLoop = \React\EventLoop\Factory::create();
$oZMQContext = new \React\ZMQ\Context($oLoop);
$oPullSocket = $oZMQContext->getSocket(\ZMQ::SOCKET_PULL);
$oPullSocket->bind('tcp://127.0.0.1:5555'); // Binding to 127.0.0.1 means the only client that can connect is itself
$oPullSocket->on('message', array($oPusher, 'onUserLogin'));
$oPullSocket->on('message', array($oPusher, 'onUserLogout'));
$oMemcache = new \Memcache();
$oMemcache->connect('127.0.0.1', 11211);
$oMemcacheHandler = new Handler\MemcacheSessionHandler($oMemcache);
$oSession = new SessionProvider(
new \Ratchet\Wamp\WampServer(
$oPusher
),
$oMemcacheHandler
);
//$this->Output->info("Server start initiation with memcache!...");
$webSock = new \React\Socket\Server($oLoop);
$webSock->listen(8080, '0.0.0.0'); // Binding to 0.0.0.0 means remotes can connect
$oServer = new \Ratchet\Server\IoServer(
new \Ratchet\Http\HttpServer(
new \Ratchet\WebSocket\WsServer(
$oSession
)
),
$webSock
);
$this->Output->info("Server started ");
$oLoop->run();
}
In this example, the call from login() or logout() would always call both methods(onUserLogin and onUserLogout).
I was not able to find some docs, which describe what events i can use in the on($event, callable $listener) method, does anyone have a link/knowledge base?
What is the best approach to check which method from the UserController was fired?
I could add some information to the $sUserData in the Controller and check this in the Pusher
I could bind an other socket to a different port (e.g. 5554 for PULL and PUSH) and use the on() method on this one
I could... is there another best practice to solve this?
No Client code needed cause it works fine
In your RatchetServerConsole,
Remove,
$oPullSocket->on('message', array($oPusher, 'onUserLogin'));
$oPullSocket->on('message', array($oPusher, 'onUserLogout'));
Add,
$oPullSocket->on('message', array($oPusher, 'onUserActionBroadcast'));
.
In your UserInformationPusher,
Remove onUserLogin() and onUserLogout().
Add,
public function onUserActionBroadcast($aUserData)
{
$entryData = json_decode($aUserData, true);
// If the lookup topic object isn't set there is no one to publish to
if (!array_key_exists($entryData['topic'], $this->subscribedTopics)) {
return;
}
$topic = $this->subscribedTopics[$entryData['topic']];
unset($entryData['topic']);
// re-send the data to all the clients subscribed to that category
$topic->broadcast($entryData);
}
.
Your UserConstroller (add the topic in $aUserData),
public function login() {
//... here is the auth code, model call etc...
$aUserData = array();// user data comes from the database with username, logintime, etc....
$aUserData['topic'] = 'USER_LOGIN'; // add the topic name
$context = new \ZMQContext();
$oSocket = $context->getSocket(\ZMQ::SOCKET_PUSH, 'my pusher'); // use persistent_id
if($oSocket instanceof \ZMQSocket) {
$oSocket->connect("tcp://127.0.0.1:5555"); //
$oSocket->send(json_encode($aUserData));
}
}
public function logout() {
//... here is the logout code, model call etc ....
$aUserData = array();// user data comes from the SESSION with username, logintime, etc....
$aUserData['topic'] = 'USER_LOGOUT'; // add the topic name
$context = new \ZMQContext();
$oSocket = $context->getSocket(\ZMQ::SOCKET_PUSH, 'my pusher'); // use persistent_id
if($oSocket instanceof \ZMQSocket) {
$oSocket->connect("tcp://127.0.0.1:5555"); //
$oSocket->send(json_encode($aUserData));
}
}
.
Finally in your view file,
<script>
var conn = new ab.Session('ws://yourdomain.dev:9000', // Add the correct domain and port here
function() {
conn.subscribe('USER_LOGIN', function(topic, data) {
console.log(topic);
console.log(data);
});
conn.subscribe('USER_LOGOUT', function(topic, data) {
console.log(topic);
console.log(data);
});
},
function() {
console.warn('WebSocket connection closed');
},
{'skipSubprotocolCheck': true}
);
</script>
.
NOTE: The basic idea was to use a single broadcast function in the pusher class.
After one month of intensive handling with PHPs best practice in websockets i changed from my approach to the Crossbar.io, voryx/Thruway in the PHP Backend and Autobahn|JS in the Frontend.
All of these componentes support the WAMP V2 Websocket Standard and are able to handle my requirements.
If there are some requests i can post the solution to my problem above, with the usage of the mentioned components.
for our company intranet, I created a page with a Form to create an E-Mailing from the frontend (Create a New E-Mailing Page).
The page should be Live after saving the form.
I did it like this, but I think I made a mistake somewhere. Because KW1, KW2, Date and SendDate are only visible on the frontend if I go to the backend and click publish again.
public static $allowed_actions = array(
'MailingForm'
);
public function MailingForm() {
$date = new DateField('EndDate', 'Gültig bis');
$date->setConfig('showcalendar', true);
$sendDate = new DateField('SendDate', 'Versanddatum');
$sendDate->setConfig('showcalendar', true);
$fields = new FieldList(
new TextField('Title', 'Title'),
new TextField('KW1', 'Start KW'),
new TextField('KW2', 'End KW'),
$date,
$sendDate
);
$actions = new FieldList(
new FormAction('createMailing', 'Erstellen')
);
//$validator = new RequiredFields('Title');
return new Form($this, 'MailingForm', $fields, $actions);//, $validator);
}
public function createMailing($data, $form) {
$member = Member::currentUser();
$filter = new URLSegmentFilter();
$page = new Mailing();
$form->saveInto($page);
$page->PublisherID = $member->ID;
$page->AuthorID = $member->ID;
$page->ParentID = $this->ID;
$page->URLSegment = $filter->filter($page->Title);
$page->writeToStage('Stage');
$page->publish('Stage', 'Live');
// EMAIL BEG
$email = new Email();
$email->setTo('mail#mail.de');
$email->setFrom('intranet#mail.de');
$email->setSubject('Neues E-Mailing für '.$this->Title);
$messageBody = "
<p>Neues E-Mailing wurde angelegt und wartet auf Freischaltung</p>
<p><strong>Name:</strong> {$data['Title']}</p>
<p><strong>KWs:</strong> {$data['KW1']} - {$data['KW2']}</p>
<p><strong>Gültig bis:</strong> {$data['EndDate']}</p>
<p><strong>Versanddatum:</strong> {$data['SendDate']}</p>
";
$email->setBody($messageBody);
$email->send();
// EMAIL END
return $this->redirect($this->Parent()->URLSegment.'/'.$this->URLSegment.'/'.$page->URLSegment);
}
If I replace $page->writeToStage('Stage');
$page->publish('Stage', 'Live'); with $page->write() than the page isn't published if i add $page->write() to the other two than i receive this error
Can't find [Title of Page]/[Page ID] in stage Stage
Can someone help me here?
Thank you in advance
To break down the problem again
If I publish the Page with
$page->write();
$page->writeToStage('Stage');
$page->publish('Stage', 'Live');
than all data is submitted correctly but I receive the following error http://www.picbutler.de/bild/301819/erroroirpt.jpg and the page is only saved as live version. In the backend the page is than marked as "deleted from draft". So I think this is the right direction.
If I publish the Page with
$page->writeToStage('Stage');
$page->publish('Stage', 'Live');
I receive no error, the submitted data appears in the backend BUT NOT in the published version. I have to publish the Page again in the backend to make the data visible in frontend.
So any ideas how to fix this?
ok, a million tries later i got it! :)
For everybody else who get stuck on this.
You just need to restore the live page to stage after writing
$page->write();
$page->doRestoreToStage();
That's all :)
Many many thanks for your two lines of codes
$page->write();
$page->doRestoreToStage();
I have been struggling with SiteTree table for a last few days,trying to write a new record to this table.
One important point is never use SiteTree class directly otherwise it won't work
eg $mySiteTree = new SiteTree(); (Bad)
Create a new page extends SiteTree() , say, Mypage
$mypage = new Mypage();
$mypage->Title = 'My Page';
$mypage->URLSegment = 'testing';
$mypage->ShowInMenus = '1';
$mypage->Version = '1';
$mypage->ParentID = '24';
$mypage->write();
$mypage->doRestoreToStage();
Enjoy!!!!