Jump to content

Event Handlers - Client & Contact CRUD


Adam

Recommended Posts

Hello,

Is https://docs.blesta.com/display/dev/Event+Handlers up to date? 

I am trying to register a callback so that when any CRUD operation is performed on a client or sub-contact, our plugin gets notified. Reading the documentation makes it seems the only callback available is when a client is created.   What about updated, or deleted? Sames goes for sub-contacts.

 

Thanks,
-Adam

Link to comment
Share on other sites

4 hours ago, activa said:

I Dont think is available such event . You can check users model file to verify the existance of the event in edit function .

Yup, I figured that out pretty quickly. I ended up adding that functionality to Blesta itself. 

 

Here is the Pull Request. Hopefully the guys can accept it. In the mean time, use git apply to merge my changes.

From: Adam Brenner <adam@netops.me>
Date: Thu, 6 Apr 2017 22:45:22 -0700
Subject: [PATCH 1/1] CUD Callbacks on Clients and Contacts

This is a new feature to Blesta that will allow anyone to
get callbacks when any CUD operation occurs on a client or
contact.

Signed-off-by: Adam Brenner <adam@netops.me>
---
 app/models/clients.php                             |  6 +++
 app/models/contacts.php                            |  9 +++++
 .../events/default/events_clients_callback.php     | 22 +++++++++++
 .../events/default/events_contacts_callback.php    | 46 ++++++++++++++++++++++
 4 files changed, 83 insertions(+)
 create mode 100644 components/events/default/events_contacts_callback.php

diff --git a/app/models/clients.php b/app/models/clients.php
index c6fdef0..1bd4468 100644
--- a/app/models/clients.php
+++ b/app/models/clients.php
@@ -302,6 +302,9 @@ class Clients extends AppModel
             $fields = ['user_id', 'client_group_id', 'status'];
             $this->Record->where('id', '=', $client_id)->update('clients', $vars, $fields);
         }
+
+        $this->Events->register('Clients.edit', ['EventsClientsCallback', 'edit']);
+        $this->Events->trigger(new EventObject('Clients.edit', ['client' => $this->get($client_id)]));
     }
 
     /**
@@ -337,6 +340,9 @@ class Clients extends AppModel
                 $this->Users->delete($client->user_id);
             }
         }
+
+        $this->Events->register('Clients.delete', ['EventsClientsCallback', 'delete']);
+        $this->Events->trigger(new EventObject('Clients.delete', ['client' => $client]));
     }
 
     /**
diff --git a/app/models/contacts.php b/app/models/contacts.php
index 8776585..3e39efc 100644
--- a/app/models/contacts.php
+++ b/app/models/contacts.php
@@ -93,6 +93,9 @@ class Contacts extends AppModel
                 $this->setPermissions($contact_id, $vars['permissions']);
             }
 
+            $this->Events->register('Contacts.create', ['EventsContactsCallback', 'create']);
+            $this->Events->trigger(new EventObject('Contacts.create', ['contact' => $this->get($contact_id)]));
+
             return $contact_id;
         }
     }
@@ -227,6 +230,9 @@ class Contacts extends AppModel
                 $this->Logs->addContact(['contact_id'=>$contact_id, 'fields'=>$fields]);
             }
 
+            $this->Events->register('Contacts.edit', ['EventsContactsCallback', 'edit']);
+            $this->Events->trigger(new EventObject('Contacts.edit', ['contact' => $new_contact]));
+
             return $new_contact;
         }
     }
@@ -270,6 +276,9 @@ class Contacts extends AppModel
                 $this->Users->delete($contact->user_id);
             }
         }
+
+        $this->Events->register('Contacts.delete', ['EventsContactsCallback', 'delete']);
+        $this->Events->trigger(new EventObject('Contacts.delete', ['contact' => $contact]));
     }
 
     /**
diff --git a/components/events/default/events_clients_callback.php b/components/events/default/events_clients_callback.php
index b9fc40c..6c81144 100644
--- a/components/events/default/events_clients_callback.php
+++ b/components/events/default/events_clients_callback.php
@@ -21,4 +21,26 @@ class EventsClientsCallback extends EventCallback
     {
         return parent::triggerPluginEvent($event);
     }
+
+    /**
+     * Handle Clients.edit events
+     *
+     * @param EventObject $event An event object for Clients.edit events
+     * @return EventObject The processed event object
+     */
+    public static function edit(EventObject $event)
+    {
+        return parent::triggerPluginEvent($event);
+    }
+
+    /**
+     * Handle Clients.delete events
+     *
+     * @param EventObject $event An event object for Clients.delete events
+     * @return EventObject The processed event object
+     */
+    public static function delete(EventObject $event)
+    {
+        return parent::triggerPluginEvent($event);
+    }
 }
diff --git a/components/events/default/events_contacts_callback.php b/components/events/default/events_contacts_callback.php
new file mode 100644
index 0000000..42b55c9
--- /dev/null
+++ b/components/events/default/events_contacts_callback.php
@@ -0,0 +1,46 @@
+<?php
+/**
+ * Handle all default Contacts events callbacks
+ *
+ * @package blesta
+ * @subpackage blesta.components.events.default
+ * @copyright Copyright (c) 2010, Phillips Data, Inc.
+ * @license http://www.blesta.com/license/ The Blesta License Agreement
+ * @link http://www.blesta.com/ Blesta
+ */
+class EventsContactsCallback extends EventCallback
+{
+
+    /**
+     * Handle Contacts.create events
+     *
+     * @param EventObject $event An event object for Contacts.create events
+     * @return EventObject The processed event object
+     */
+    public static function create(EventObject $event)
+    {
+        return parent::triggerPluginEvent($event);
+    }
+
+    /**
+     * Handle Contacts.edit events
+     *
+     * @param EventObject $event An event object for Contacts.edit events
+     * @return EventObject The processed event object
+     */
+    public static function edit(EventObject $event)
+    {
+        return parent::triggerPluginEvent($event);
+    }
+
+    /**
+     * Handle Contacts.delete events
+     *
+     * @param EventObject $event An event object for Contacts.delete events
+     * @return EventObject The processed event object
+     */
+    public static function delete(EventObject $event)
+    {
+        return parent::triggerPluginEvent($event);
+    }
+}
-- 
2.12.1

 

Link to comment
Share on other sites

i remember i have sent the same events with a lot of events already made to @Paul a age ago, but it seem they have their own priority and roadmap to add events .

i have already integrated a gateways/contacts/clients_groups/companies/coupons/cronjob/calenders/staff/transactions if any one need the vent files i canshare theme, they need only to the event in the model i haven't time to search the old files, but it very easy to add them, thanks Besta for the open source files :)

Note, i don't have enough space to add more files/images !! can anyone add 0 in the end of my upload files limit :D

 

Link to comment
Share on other sites

8 hours ago, Adam said:

Yup, I figured that out pretty quickly. I ended up adding that functionality to Blesta itself.

personally i prefer two event ... one before the delete action and other after the delete action . the same for the edit action .

that way we can for example control a user before the edit and after the edit , so real case, we have a client group that we won't allow them to change thier name and country . so with pre Edit action event we can check if the user/contact belong to this client group and then we set a input->errors() so the change won't be able . they need to contact the staff :)

another idea for pre/after event callback is the gateways manager, we have made a custom work to dissalow some gateway from appearing in the gateway payment for some client based in country . we have added event for getInstalledNonmerchant() and we make a filter to remove the no desired gateway from the list of returned gateway :)

so more events more magic functions in blesta

 

Link to comment
Share on other sites

1 hour ago, Blesta Addons said:

Note, i don't have enough space to add more files/images !! can anyone add 0 in the end of my upload files limit :D

Just post a patch file. Stop uploading zips, they are a pain to work with and make it hard to merge.

 

Link to comment
Share on other sites

On 4/8/2017 at 9:31 AM, Adam said:

Added a feature request: https://requests.blesta.com/topic/pull-request-client-and-contact-create-update-and-delete-callbacks

Lets hope the Blesta team acknowledges it and gets it into main branch. Would be a shame to ignore it, seeing how the only thing left for them to do merge + QA it.

-Adam

https://dev.blesta.com/browse/CORE-2364

Link to comment
Share on other sites

  • 1 month later...

I

5 hours ago, Paul said:

We added 28 new events to 4.1. You can see them in the documentation at https://docs.blesta.com/display/dev/Event+Handlers (Search the page for 4.1)

Is geeat news. But i still found the lock of events  for pre action, like preaction users.add or edit , We miss this function from whmxx, a simple exemple, we prevent client of groub XXXX from changing passwords or info , with the pre edit event we can chech the client group belong to it, then se return a error message that prevent the info from bieng changed , we really miss this events .

Link to comment
Share on other sites

very glad to see this events, as @activa sayed, the Pre/Post event is a rock events in some situations. like activa post, in a custom plugin, we need to disable change info for some clients/contacts that has the country XX, without a Pre event for the events Contacts.edit & Clients.edit we can't achieve this task, in whmxx they has such events in some of hooks.

we can considerate Contacts.edit & Clients.edit as a post event, but we need something to handle theme as Pre events.

Link to comment
Share on other sites

On 5/16/2017 at 0:52 PM, Paul said:

We added 28 new events to 4.1. You can see them in the documentation at https://docs.blesta.com/display/dev/Event+Handlers (Search the page for 4.1)

Thanks Paul. That is awesome.

 

Once Blesta 4.1 is release I can release my Cerb (www.cerb.ai) module with Blesta.

-Adam

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...