Zen Cart Logo
Forums / Code Collaboration / Using an admin observer only with an admin tool

Using an admin observer only with an admin tool

Views: 4,814

Results 1 to 4 of 4
9 Aug 2019, 10:49 AM
#1
torvista avatar

torvista

Totally Zenned

Join Date:
Aug 2007
Location:
Gijón, Asturias, Spain
Posts:
2,866
Plugin Contributions:
7

Using an admin observer only with an admin tool

The example for an observer
https://www.zen-cart.com/wiki/index.php/Developers_API_Tutorials

details having a dedicated file for the observer, and the autoloader to load it...meaning this observer class is always loaded on all pages.

If I only want to use a notifier for a specific admin-tool plugin, can I not just put that observer class in that file, so it is only loaded when needed?

I have tried it and it didn't work.

9 Aug 2019, 11:41 AM
#2
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,066
Plugin Contributions:
56

Re: Using an admin observer only with an admin tool

It's not totally clear what that file is, Steve. If you want, for instance, to observe notification 'ZEN_GET_PRODUCTS_BASE_PRICE' (from the zen_get_products_base_price function) *only *during Specials processing (specials.php), you can use logic similar to the following in the observer-class' __construct method:

if ($GLOBALS['current_page'] == FILENAME_SPECIALS . '.php') {
    $this->attach($this, array('ZEN_GET_PRODUCTS_BASE_PRICE'));
}

Just load the observer at point 200 (the $current_page value is set during init_languages.php's processing) and make sure that the observer-class extends base.

9 Aug 2019, 1:35 PM
#3
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: Using an admin observer only with an admin tool

Here's the logic that is needed regarding observing a notifier, the attachment to the notifier system needs to occur before the notifier is executed, after the notifier system is initiated, and in this case/request would need to process/instantiate the observer class before the notifier is triggered.

So if the notifier is in file X.php and the observer is only to trigger in execution of file X.php then with the class defined in the file, after the class definition, could execute:

$myNewObserverVariable = new myObserverClass();

Where the class name is myObserverClass.
Assuming that file X.php is only loaded to support the desired operation, then the class would be defined by its presence and then instantiated by the call to new, followed by the code in the file that would then initiate the notify event that is to be executed.

This doesn't necessarily support "clean" coding as in PSR type formatting, but it does centralize the operation and this somewhat "gosub" style code.

10 Aug 2019, 6:52 AM
#4
torvista avatar

torvista

Totally Zenned

Join Date:
Aug 2007
Location:
Gijón, Asturias, Spain
Posts:
2,866
Plugin Contributions:
7

Re: Using an admin observer only with an admin tool

...and then instantiated by the call to new,
That was the bit I was missing.

So in laymans terms/for googlers, the code sequence in the single file is:

  1. The observer class that watches for the notifier...but it does nothing until:
  2. The instantiation an instance of the class.
  3. The rest of the code that at some point will call something that has the occurrence of the notify event being observed.

Thanks!!