Requests | Blesta

Requests

Pull Request: Client and Contact Create, Update and Delete Callbacks

Adam Brenner shared this idea 1 year ago
Completed

Problem: Blesta only supports callbacks for when a new client is created. When a client is updated, deleted or if a contact for a client is created, updated or deleted, no callbacks exists. This makes developing plugins harder within Blesta.

Solution: Accept the Pull Request below which adds full callback support (Create, Update & Delete) for both Clients and Contacts.

Reference Link: https://www.blesta.com/forums/index.php?/topic/9348-event-handlers-client-contact-crud/

  1. From: Adam Brenner <adam@netops.me>
  2. Date: Thu, 6 Apr 2017 22:45:22 -0700
  3. Subject: [PATCH 1/1] CUD Callbacks on Clients and Contacts
  4. This is a new feature to Blesta that will allow anyone to
  5. get callbacks when any CUD operation occurs on a client or
  6. contact.
  7. Signed-off-by: Adam Brenner <adam@netops.me>
  8. ---
  9. app/models/clients.php | 6 +++
  10. app/models/contacts.php | 9 +++++
  11. .../events/default/events_clients_callback.php | 22 +++++++++++
  12. .../events/default/events_contacts_callback.php | 46 ++++++++++++++++++++++
  13. 4 files changed, 83 insertions(+)
  14. create mode 100644 components/events/default/events_contacts_callback.php
  15. diff --git a/app/models/clients.php b/app/models/clients.php
  16. index c6fdef0..1bd4468 100644
  17. --- a/app/models/clients.php
  18. +++ b/app/models/clients.php
  19. @@ -302,6 +302,9 @@ class Clients extends AppModel
  20. $fields = ['user_id', 'client_group_id', 'status'];
  21. $this->Record->where('id', '=', $client_id)->update('clients', $vars, $fields);
  22. }
  23. +
  24. + $this->Events->register('Clients.edit', ['EventsClientsCallback', 'edit']);
  25. + $this->Events->trigger(new EventObject('Clients.edit', ['client' => $this->get($client_id)]));
  26. }
  27. /**
  28. @@ -337,6 +340,9 @@ class Clients extends AppModel
  29. $this->Users->delete($client->user_id);
  30. }
  31. }
  32. +
  33. + $this->Events->register('Clients.delete', ['EventsClientsCallback', 'delete']);
  34. + $this->Events->trigger(new EventObject('Clients.delete', ['client' => $client]));
  35. }
  36. /**
  37. diff --git a/app/models/contacts.php b/app/models/contacts.php
  38. index 8776585..3e39efc 100644
  39. --- a/app/models/contacts.php
  40. +++ b/app/models/contacts.php
  41. @@ -93,6 +93,9 @@ class Contacts extends AppModel
  42. $this->setPermissions($contact_id, $vars['permissions']);
  43. }
  44. + $this->Events->register('Contacts.create', ['EventsContactsCallback', 'create']);
  45. + $this->Events->trigger(new EventObject('Contacts.create', ['contact' => $this->get($contact_id)]));
  46. +
  47. return $contact_id;
  48. }
  49. }
  50. @@ -227,6 +230,9 @@ class Contacts extends AppModel
  51. $this->Logs->addContact(['contact_id'=>$contact_id, 'fields'=>$fields]);
  52. }
  53. + $this->Events->register('Contacts.edit', ['EventsContactsCallback', 'edit']);
  54. + $this->Events->trigger(new EventObject('Contacts.edit', ['contact' => $new_contact]));
  55. +
  56. return $new_contact;
  57. }
  58. }
  59. @@ -270,6 +276,9 @@ class Contacts extends AppModel
  60. $this->Users->delete($contact->user_id);
  61. }
  62. }
  63. +
  64. + $this->Events->register('Contacts.delete', ['EventsContactsCallback', 'delete']);
  65. + $this->Events->trigger(new EventObject('Contacts.delete', ['contact' => $contact]));
  66. }
  67. /**
  68. diff --git a/components/events/default/events_clients_callback.php b/components/events/default/events_clients_callback.php
  69. index b9fc40c..6c81144 100644
  70. --- a/components/events/default/events_clients_callback.php
  71. +++ b/components/events/default/events_clients_callback.php
  72. @@ -21,4 +21,26 @@ class EventsClientsCallback extends EventCallback
  73. {
  74. return parent::triggerPluginEvent($event);
  75. }
  76. +
  77. + /**
  78. + * Handle Clients.edit events
  79. + *
  80. + * @param EventObject $event An event object for Clients.edit events
  81. + * @return EventObject The processed event object
  82. + */
  83. + public static function edit(EventObject $event)
  84. + {
  85. + return parent::triggerPluginEvent($event);
  86. + }
  87. +
  88. + /**
  89. + * Handle Clients.delete events
  90. + *
  91. + * @param EventObject $event An event object for Clients.delete events
  92. + * @return EventObject The processed event object
  93. + */
  94. + public static function delete(EventObject $event)
  95. + {
  96. + return parent::triggerPluginEvent($event);
  97. + }
  98. }
  99. diff --git a/components/events/default/events_contacts_callback.php b/components/events/default/events_contacts_callback.php
  100. new file mode 100644
  101. index 0000000..42b55c9
  102. --- /dev/null
  103. +++ b/components/events/default/events_contacts_callback.php
  104. @@ -0,0 +1,46 @@
  105. +<?php
  106. +/**
  107. + * Handle all default Contacts events callbacks
  108. + *
  109. + * @package blesta
  110. + * @subpackage blesta.components.events.default
  111. + * @copyright Copyright (c) 2010, Phillips Data, Inc.
  112. + * @license http://www.blesta.com/license/ The Blesta License Agreement
  113. + * @link http://www.blesta.com/ Blesta
  114. + */
  115. +class EventsContactsCallback extends EventCallback
  116. +{
  117. +
  118. + /**
  119. + * Handle Contacts.create events
  120. + *
  121. + * @param EventObject $event An event object for Contacts.create events
  122. + * @return EventObject The processed event object
  123. + */
  124. + public static function create(EventObject $event)
  125. + {
  126. + return parent::triggerPluginEvent($event);
  127. + }
  128. +
  129. + /**
  130. + * Handle Contacts.edit events
  131. + *
  132. + * @param EventObject $event An event object for Contacts.edit events
  133. + * @return EventObject The processed event object
  134. + */
  135. + public static function edit(EventObject $event)
  136. + {
  137. + return parent::triggerPluginEvent($event);
  138. + }
  139. +
  140. + /**
  141. + * Handle Contacts.delete events
  142. + *
  143. + * @param EventObject $event An event object for Contacts.delete events
  144. + * @return EventObject The processed event object
  145. + */
  146. + public static function delete(EventObject $event)
  147. + {
  148. + return parent::triggerPluginEvent($event);
  149. + }
  150. +}
  151. --
  152. 2.12.1

Comments (1)