CodeIgniter Forums
Observe "Events" - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Observe "Events" (/showthread.php?tid=2596)



Observe "Events" - El Forum - 08-14-2007

[eluser]IkeE65[/eluser]
Hi everybody,

I'm currently looking for a php-based framework to use for a relatively complex application.
The frontend will be based on ExtJS and a lot of people over at the forums at extjs.com prefer codeignitor, so here I am ;-)

One important question:
Is it possible to get something like observing events to work? I found some topics here in the forums, with talking about observer patterns, active records, event listeners, joomla framework-beta-stuff and so on, but I didn't really get behind it.

What I want to do:
I want to build a some kind of modular CRM (Customer Relationsship Management). So I have customers and some lets call them "modules", like the customerlist itself, a calendar, a todo list or for example an email client. If the client who hopefully is using the crm some day, there will be a bunch of modules extending this functionality.
So, when adding a new module, for example some kind of document management, i don't want to have to change anything inside the customer management module, where i add/edit/delete customers. But for example if i delete a customer, the customers documents have to be deleted too.
In short, when "firing" the "onCustomerDelete"-Event from the customer management module, some corresponding callback action has to be called inside any module which wants something to be done or better has registered this action, when a customer is deleted.

Is this possible with codeigniter and some coding? Or are there some basic traps, which deny such things?

Greets in advance

Ike

(Sorry for maybe bad english ;-))


Observe "Events" - El Forum - 08-14-2007

[eluser]johnwbaxter[/eluser]
Anything is possible with codeigniter and some coding.

Codeigniter does not impose any restrictions on what you can and can't do (apart from destroying GET i suppose) so you can code how you want.

For info on the design pattern CI is based on see here http://www.ellislab.com/codeigniter/user-guide/overview/mvc.html but also be aware that you do not have to follow that path.

There is a feature you may wish to look into here http://www.ellislab.com/codeigniter/user-guide/general/hooks.html

To be honest, to get a view of what codeigniter is good for, read the first few sections (or all!) of the user guide http://www.ellislab.com/codeigniter/user-guide

It's all you'll need.

Good Luck.


Observe "Events" - El Forum - 08-14-2007

[eluser]IkeE65[/eluser]
Thanks for your reply

I have already read almost the whole user guide and nearly everything is clear to me. I think I should build some examples to evaluate especially the hooks part for myself.

Do you know if someone has done something similiar yet using CI?

I've seen something like it in the Joomla-Component Communitybuilder. There a main file includes all plugin files and these register their callback-functions to a Plugin-Object or something. But CI doesn't include all application-files on every request, does it?


Observe "Events" - El Forum - 08-14-2007

[eluser]johnwbaxter[/eluser]
Errr i'm not sure! Perhaps if you autoload something? Autoloading makes it available globally.

Not that i'm an expert, but i would have thought that you should be able to have yourself some code that does the observing and have it autoloaded. To be honest if you were able to describe a little more how your observer code is expected to work i could give you a better idea of how you could use it in CI.


Observe "Events" - El Forum - 08-14-2007

[eluser]IkeE65[/eluser]
Hm litte difficult in english, but lets try...

I have thought of something like this:

We have three modules (or better 'applications', right?):
-Customer Management
-Calendar
-Document Management

When index.php is called, a Observer-Object is instantiated, then the controller-file of each applications is included and a method from the observer-object is called, for example in the document management like this:
Code:
$observer->registerEventCallback('onCustomerCreate', 'createCustomerArchive');
$observer->registerEventCallback('onCustomerDelete', 'deleteCustomerDocuments');
And in the calendar application:
Code:
$observer->registerEventCallback('onCustomerCreate', 'addCustomerBirthdayAlert');
$observer->registerEventCallback('onCustomerDelete', 'deleteCustomerBirthdayAlert');
Now if an application fires on of these Events, the controller of all registered applications is instantiated and the registered methods are called.
For example inside the customer management application:
Code:
...
function deleteCustomer() {
global $observer;
if($this->db->query("DELETE... blablabla")) { //dunno if this works, but we suppose the customer is deleted from DB successfully
$observer->fireEvent('onCustomerDelete');
}
}
...
So everything that has to be done when deleting a customer in every application is done, without really 'touching each other', that means I easily can add another application like a summary of the customers orders and handle the customer-deletion-stuff inside it, without changing anything in any of the other applications.

Did you get me?


Observe "Events" - El Forum - 08-14-2007

[eluser]johnwbaxter[/eluser]
Yes i see.

1st, i don't think modules = applications. Everything would be under application and then you can modularise it with sub-folders perhaps.

2ns, i think i may be getting a little out of my depth here. However, i think that what you are doing can be done within the scope of controllers.

When your app does something, (if you are following MVC) then that action will be processed via a controller which will then output the view and interact with the model. All actions go through the controller(s). So, if you have a super controller that contains event methods you can include it globally.

I think someone else should pick this discussion up to be honest, this is not my arena of expertise and i'd rather not confuse you or send you in the wrong direction.


Observe "Events" - El Forum - 08-14-2007

[eluser]IkeE65[/eluser]
hehe ok ;-)
thank you anyway


Observe "Events" - El Forum - 08-14-2007

[eluser]johnwbaxter[/eluser]
I'm sorry i couldn't be more help.

It is a shame no-one else (better!) has responded. I would quite like to get a better idea of how this would work properly from someone that has done it.

It would be very useful to know.


Observe "Events" - El Forum - 08-15-2007

[eluser]Sector[/eluser]
I'm pretty sure this is possible, as PHP allows 'strings' to be used for function calling. All you really need is a uniform argument (say, an associative array).

In your module Document Management (which has the classname dcm)
Code:
$observer->registerEventCallback('onCustomerCreate', 'dcm', 'createCustomerArchive');
$observer->registerEventCallback('onCustomerDelete', 'dcm', 'deleteCustomerDocuments');

In your main file (for example when the 'onCostumerDelete' is supposed to be fired
Code:
foreach (... as ...) {
   $this->$class->$function($arrayarg);
}

$class = the saved class ('dcm' in this case)
$function = the saved function ('createCustomerArchive' in this case)
$arrayarg = the only argument (an array)

Ofcourse here I am supposing that the dcm is a _library_ which you have autoloaded (so that $this->dcm->... works).

There are however other ways to do it.

Also, I might have mistaken myself somwhere, because I'm just awake and I've never done event passing in PHP myself. But it should certainly be possible.

Good luck


Observe "Events" - El Forum - 08-15-2007

[eluser]garymardell[/eluser]
I have done this before to make a plugin system. It is a design pattern.

http://www.phppatterns.com/docs/design/observer_pattern

Maybe that will help if you haven't seen it already.