Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2005
    Location
    Upstate NY
    Posts
    22,010
    Plugin Contributions
    25

    Default Substituting mod code for core code in v2.0

    I have been studying OOP coding in preparation for v2.0 mod rewriting. One thing I have not been able to find mention of anywhere on the net is how to substitute a new section of code for an original core file's code (for a modified functionality) using the observer-notifier system.

    Generally, when the observer is triggered, it executes the desired code and returns to the triggering point to continue.
    This will not work for the kind of thing I (and many others) will need to do, which is to execute a snippet of code instead of a snippet in the core file. If the observer returns to the triggering point, the original code will execute. I need it to return to another point after the original snippet, bypassing it.

    When I asked in the w3schools forum, the best answer I got was that the original code would need to be registered as a default event handler and that my code would need to tell the default not to execute.

    Can I get any more enlightenment on the specifics of accomplishing this result in the v2.0 structure?

  2. #2
    Join Date
    Jun 2003
    Location
    Newcastle UK
    Posts
    2,896
    Blog Entries
    2
    Plugin Contributions
    2

    Default Re: Substituting mod code for core code in v2.0

    Without a concrete example of what you are trying to achieve, it would be difficult to tell whether v2 structure would help you in your goal.

    However I can try and explain some general principles behind v2.

    For observers generally, you are correct in that they only allow you to 'add code' rather than override. However in v2 in some limited circumstances, the observer has access to a 'continuation flag'

    e.g.
    PHP Code:
    $continuationFlag TRUE;
    $this->notify('NOTIFY_SOMETHING'$continuationFlag)
    if (
    $continuationFlag
    {
     ... do 
    some core code 

    From that if the observer sets $continuationFlag to FALSE then core code will be skipped.

    Secondly in a number of places v2 introduces the concept of filter chains. These allow contributions to not only add to core code, but change whether some core code runs as well.

    One of the main reasons we added filter chains was to allow contributions to interact with customer registration. The idea being that 3rd party software such as CMS, ERP etc could add customers to their own databases.

    some example code

    PHP Code:
    ....
        
    $this->customerFilterChain = array();
        
    $this->customerFilterChain[] = 'standard';
        
    $this->notify('NOTIFY_SET_CUSTOMER_FILTER_CHAIN');
    ...
      function 
    validateLogin ($plain$encrypted)
      {
        
    $this->passwordValidated FALSE;
        
    $this->pwdPlain $plain;
        
    $this->pwdSecure $encrypted;
        foreach (
    $this->customerFilterChain as $filterChain)
        {
          if (
    $filterChain != ''zcFilterChain::execute('customer'$filterChain$this)->init()->executeMethod('validateLogin');
        }
        return 
    $this->passwordValidated;
      } 
    With the above, and using an observer, a contribution could add its own filter class to the filter chain array that ran after the standard login. However it would also be possible for the contribution to remove the 'standard' filter chain completely and substitute its own code.

    Currently filter chains are used in v2 for customer login/registration and for the routing subsytem.

    For other core code, there are usually other ways (apart from observers) to extend/override core functionality. In part this is done using overrides, both traditional and some new overrides that plugins have available.

    As an example I have written a demo plugin that adds a new field to the customer sign up page (a confirm email address field) that does not touch core code at a all, and using similar methods it would be possible to completely redesign the registration page without touching core code.

    HTH
    Last edited by wilt; 9 Sep 2009 at 07:29 PM.

  3. #3
    Join Date
    Jan 2004
    Posts
    66,364
    Blog Entries
    7
    Plugin Contributions
    274

    Default Re: Substituting mod code for core code in v2.0

    Additionally, one could use an observer to fire at a notifier point which exists at the end of a section of core code, and that observer could do something "else" and replace whatever values were prepared by the core code with whatever the (addon's) observer wanted it to do instead. This could let you completely replace whatever the core normally does, or simply augment what normally happens, or completely replace the results calculated by whatever happened.

    Again, without very specific details, it's hard to guess what exactly the right solution is for whatever you're dreaming up
    .

    Zen Cart - putting the dream of business ownership within reach of anyone!
    Donate to: DrByte directly or to the Zen Cart team as a whole

    Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
    Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

  4. #4
    Join Date
    Jul 2005
    Location
    Upstate NY
    Posts
    22,010
    Plugin Contributions
    25

    Default Re: Substituting mod code for core code in v2.0

    Thank you very much - that is the best info I have seen yet on what will be possible.

    Some of my mods only change a few lines inside a large file, e.g. Smart Backgrounds adds a bit of code to tpl_main_page.php for setting a body classname (could be done with a notifier hook), but then uses a changed line for outputting the body div HTML with classname. If the original body div HTML then executed, it would obviously cause big problems. I will need to swap my new output line for the original one.

    This code could be moved into an observer:
    PHP Code:
    <?php //Smart Backgrounds
    $smart_image '';
    if (
    $current_page_base == 'index' or $current_page_base == 'product_info') { //add _ and cPath to bg filename only if individual cat bg image exists, else add _ and top cat id to bg filename only if top cat bg image exists
      
    $smart_image file_exists(DIR_WS_TEMPLATE_IMAGES 'smartbg_' $_GET[cPath] . '.gif')?'_' $_GET[cPath]:(file_exists(DIR_WS_TEMPLATE_IMAGES 'smartbg_' . (int)$_GET[cPath] . '.gif')?'_' . (int)$_GET[cPath]:'');
    } elseif (
    $current_page_base == 'page') { //add _page and ez-page id to bg filename only if ez-page id bg image exists, else add _page to bg filename only if general ez-page bg image exists
      
    $smart_image file_exists(DIR_WS_TEMPLATE_IMAGES 'smartbg_page' $_GET[id] . '.gif')?'_page' $_GET['id']:(file_exists(DIR_WS_TEMPLATE_IMAGES 'smartbg_page.gif')?'_page':'');
    } else { 
    //add _ and page base to bg filename only if page bg image exists
      
    $smart_image file_exists(DIR_WS_TEMPLATE_IMAGES 'smartbg_' $current_page_base '.gif')?'_' $current_page_base:''//default/home page class will be just .smartBG, and filename smartbg.gif
    }// /Smart Backgrounds?>
    but this line needs to substitute for the original in tpl_main_page.php:
    PHP Code:
    <body id="<?php echo $body_id 'Body'?>" class="smartBG<?php echo $smart_image;?>"<?php if($zv_onload !='') echo ' onload="'.$zv_onload.'"'?>>
    I would love to be able to do this without having to make an override copy of the whole tpl_main_page.php.


    Coming back after a bit of core code executed and "redoing" it would sometimes work, but if you don't know the original values of variables the core was working on, it might not be possible to completely reverse the original action. If the core code performed output, that would be a bit difficult to "take back" :)
    Last edited by gjh42; 9 Sep 2009 at 07:47 PM.

  5. #5
    Join Date
    Jan 2004
    Posts
    66,364
    Blog Entries
    7
    Plugin Contributions
    274

    Default Re: Substituting mod code for core code in v2.0

    Hmmm...
    .

    Zen Cart - putting the dream of business ownership within reach of anyone!
    Donate to: DrByte directly or to the Zen Cart team as a whole

    Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
    Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

  6. #6
    Join Date
    Apr 2006
    Location
    London, UK
    Posts
    10,569
    Plugin Contributions
    25

    Default Re: Substituting mod code for core code in v2.0

    Hi Glenn

    I wouldn't see notifiers/observers as an appropriate way to insert HTML or HTML attributes. They're better suited to as ways to hook into processing logic.

    There's a much better way nowadays to add extra HTML such as you describe above to a template file, without the need to override it. I'm thinking specifically of javscript libraries such as jQuery, which it has been announced will be integrated into 2.0, are very effective for doing this.
    Kuroi Web Design and Development | Twitter

    (Questions answered in the forum only - so that any forum member can benefit - not by personal message)

  7. #7
    Join Date
    Jan 2004
    Posts
    66,364
    Blog Entries
    7
    Plugin Contributions
    274

    Default Re: Substituting mod code for core code in v2.0

    Thanks kuroi. I was thinking of suggesting just that.
    .

    Zen Cart - putting the dream of business ownership within reach of anyone!
    Donate to: DrByte directly or to the Zen Cart team as a whole

    Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
    Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

  8. #8
    Join Date
    Jul 2005
    Location
    Upstate NY
    Posts
    22,010
    Plugin Contributions
    25

    Default Re: Substituting mod code for core code in v2.0

    Thank you both! Looks like I'll have to get up to speed on jQuery as well as OOP.

 

 

Similar Threads

  1. Discount Code - Invalid Code Message Not Appearing - FEC
    By pennylane in forum All Other Contributions/Addons
    Replies: 1
    Last Post: 11 Jan 2010, 11:21 PM
  2. product description os utputting code -stylesheet and comment code
    By edt123 in forum Setting Up Categories, Products, Attributes
    Replies: 1
    Last Post: 15 Dec 2009, 07:06 PM
  3. CVV2 Code - where's the card security code entered?
    By jamieboulder in forum General Questions
    Replies: 2
    Last Post: 12 Mar 2008, 06:05 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
disjunctive-egg
Zen-Cart, Internet Selling Services, Klamath Falls, OR