Zen Cart v1.5.3: Auto-loaded Observers!

 php, plugins, v1.5.3, zen-cart  Comments Off on Zen Cart v1.5.3: Auto-loaded Observers!
Aug 312014
 

One of the features quietly introduced in Zen Cart v1.5.3 is auto-loaded observers.  If you’re developing a plugin that uses an observer-class, you normally have to provide two files in your plugin’s distribution to get that class loaded and instantiated:

  1. /includes/auto_loaders/config.your_plugin.php
  2. /includes/classes/observers/class.your_plugin.php

Starting with Zen Cart v1.5.3, built-in functionality will do the “heavy lifting” to get your class-file loaded and instantiated — so long as your class doesn’t have any special requirements on its load point (auto-loaded classes are loaded at point 175, after all other system dependencies are loaded).   Here are the requirements (as pulled from the file /includes/init_includes/init_observers.php):

  1. The file is in the /includes/classes/observers folder and named auto.your_plugin.php.  All files in this directory that start with auto. will be included (i.e. loaded).
  2. The file defines a class named zcObserver + the CamelCased filename, e.g. a file named auto.your_plugin.php will contain a class named zcObserverYourPlugin.  A myDEBUG*.log file will be generated if a properly-named file is loaded, but the class name doesn’t conform to these rules.

Here’s an example that displays a message in the header, but only on the index (main) page; the file is /includes/classes/observers/auto.test_observer.php:

<?php
// -----
// An example of an auto-loaded observer, based on a feature introduced in Zen Cart v1.5.3.
//
// Displays a header-message *only* on the index page.
//
class zcObserverTestObserver extends base {

  function __construct() {
    $this->attach($this, array('NOTIFY_HEADER_START_INDEX'));
    
  }

  function update (&$class, $eventID, $paramsArray = array()) {
    global $messageStack;
    $messageStack->add ('header', 'The auto-loaded observer is loaded!', 'success');
    
  }
}

Happy auto-loading!

Zen Cart Observers … They Work in the Admin, Too!

 hints, Uncategorized, v1.5.0, v1.5.1, zen-cart  Comments Off on Zen Cart Observers … They Work in the Admin, Too!
Sep 222013
 

The Zen Cart Observer Class is a great tool for plugin developers, allowing a plugin to watch for (“observe”) the occurrence of an event (a “notification”) and provide additional processing without making modifications to Zen Cart core files.  What hasn’t been stressed over the past years is that this notifier/observer interface also works in the Zen Cart admin panel!

It seems like every plugin (and its brother) need just a small update to /YOUR_ADMIN/orders.php, making the plugin installation (and Zen Cart upgrade) process just that more complex.  If plugin authors, myself included, would start adding a notifier at specific processing points in the popular (e.g. orders.php, customer.php) admin pages and providing observer code to insert their customizations, both the installation and upgrade processes would go much more smoothly.