1
Mazarin
Preloads for only specific pages or parts of pages
  • 2010/2/17 7:44

  • Mazarin

  • Just can't stay away

  • Posts: 533

  • Since: 2008/12/10


In relation to the release of TS-MyStartupPage 0.4 it was suggested to remove the required hack and instead use preloads. However, not being really familiar with preloads, I don't see how this could be done in this case.

What I want to change is that every time a user logs in, and only when he logs in, he is redirected back to the main index of the site (i.e. the startup module). The way this is done now is that a variable in a specific if statement is changed to always point to the index page.

How could I address that specific case using preloads?

2
wishcraft
Re: Preloads for only specific pages or parts of pages

Hey I made use of preloads for xortify in version 1.16. The preloads is a method of loading code throughout the core library.

When you see for example a:

$xoopsPreload->eventTrigger('core.header.end');


this means in your module in the preload class you will have a corresponding event function called eventCoreHeaderEnd() which when XOOPS in it's runtime reaches this call ($xoopsPreload->eventTrigger('core.header.end');) the function in the modules preload is then executed.

For example say my module is called 'content' and I wanted to execute some code at the end of header.php loading, the file /modules/content/preloads/core.php will have the following code in it to execute the word 'Hello World!' to the output buffer when the code example above is reached loading the header.php file.

class ContentCorePreload extends XoopsPreloadItem
{
    
    function 
eventCoreHeaderEnd($args)
    {
        if (
ContentCorePreload::isActive()) {
            echo 
'Hello World!';
        }
    }

    function 
isActive()
    {
        
$module_handler =& xoops_getHandler('module');
        
$module $module_handler->getByDirname('content');
        return (
$module && $module->getVar('isactive')) ? true false;
    }
        
}


if the preload file has a different name apart from core, like presets, the code above for a file with the name /modules/content/preloads/presets.php would look like:

class ContentPresetsPreload extends XoopsPreloadItem
{
    
    function 
eventCoreHeaderEnd($args)
    {
        if (
ContentPresetsPreload::isActive()) {
            echo 
'Hello World!';
        }
    }

    function 
isActive()
    {
        
$module_handler =& xoops_getHandler('module');
        
$module $module_handler->getByDirname('content');
        return (
$module && $module->getVar('isactive')) ? true false;
    }
        
}


Personally I think the preloads folder should also be added to the theme library system as well in 2.6 - this means your theme could have a /themes/themename/preloads/presets.php folder with code like:

class ThemenamePresetsPreload extends XoopsPreloadItem
{
    
    function 
eventCoreHeaderEnd($args)
    {
        echo 
'Hello World!';
    }
        
}


This would made the scripting capabilities of theme design extremely powerful as it allows for an element of core interactions, I think in designing for interaction this is definitely a minor adjustment to Preloads and the theme.php file.

The changes to make if you want a theme preload are as follows.

First change /class/preload.php to include the following 2 new functions

/**
 You may not change or alter any portion of this comment or credits
 of supporting developers from this source code or any supporting source code
 which is considered copyrighted (c) material of the original comment or credit authors.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 */

/**
 * XOOPS Preload Classes
 *
 * @copyright The XOOPS Project http://sourceforge.net/projects/xoops/
 * @license http://www.fsf.org/copyleft/gpl.html GNU public license
 * @package kernel
 * @subpackage class
 * @since 2.6.0
 * @author trabis 
 * @author wishcraft  
 * @version $Id: preload.php 3437 2009-08-27 13:52:28Z trabis $
 */

defined('XOOPS_ROOT_PATH') or die('Restricted access');

XoopsLoad::load('XoopsLists');
XoopsLoad::load('XoopsCache');

/**
 * Class for handling events
 *
 * @copyright The XOOPS Project http://sourceforge.net/projects/xoops/
 * @license http://www.fsf.org/copyleft/gpl.ht
ml GNU public license
 * @package kernel
 * @subpackage class
 * @author trabis 
 * @author wishcraft  
 */
class XoopsPreload
{
 
/**
 * @var array $_preloads array containing information about the event observers
 */
 
var $_preloads = array();

 
/**
 * @var array $_preloadstheme array containing information about the event observers
 */
 
var $_preloadstheme = array();

 
/**
 * @var array $_events array containing the events that are being observed
 */
 
var $_events = array();

 
/**
 * Constructor
 *
 * @return    void
 */
 
function XoopsPreload()
 {
 
$this->setPreloads();
 
$this->setEvents();
 }

 
/**
 * Allow one instance only!
 *
 * @return object
 */
 
function &getInstance()
 {
 static 
$instance false;
 if (!
$instance) {
 
$instance = new XoopsPreload();
 }
 return 
$instance;
 }

 
/**
 * Get available preloads information and set them to go!
 *
 * @return void
 */
 
function setPreloads()
 {
 if (
$modules_list XoopsLists::getDirListAsArray(XOOPS_ROOT_PATH "/modules/")) {
 
$i 0;
 foreach (
$modules_list as $module) {
 if (
is_dir($dir XOOPS_ROOT_PATH "/modules/{$module}/preloads/")) {
 
$file_list XoopsLists::getFileListAsArray($dir);
 foreach (
$file_list as $file) {
 if (
preg_match('/(.php)$/i'$file)) {
 
$file substr($file0, -4);
 
$this->_preloads[$i]['module'] = $module;
 
$this->_preloads[$i]['file'] = $file;
 
$i++;
 }
 }
 }
 }
 }
 }

 
/**
 * Get available events and set them to go!
 *
 * @return void
 */
 
function setEvents()
 {
 foreach (
$this->_preloads as $preload) {
            include_once 
XOOPS_ROOT_PATH '/modules/' $preload['module'] . '/preloads/' $preload['file']. '.php';
            
$class_name ucfirst($preload['module']) . ucfirst($preload['file']) . 'Preload' ;
            if (!
class_exists($class_name)) {
                continue;
            }
            
$class_methods get_class_methods($class_name);
            foreach (
$class_methods as $method) {
                if (
strpos($method'event') === 0) {
                    
$event_name strtolower(str_replace('event'''$method));
                    
$event= array('class_name' => $class_name'method' => $method);
                    
$this->_events[$event_name][] = $event;
                }
            }
 }
 }

 
/**
 * Get available preloads information and set them to go!
 *
 * @return void
 */
 
function setThemePreloads($theme='')
 {
        if (
$theme=='')
            
$theme $GLOBALS['xoopsConfig']['theme_set'];

        if (
is_dir($dir XOOPS_ROOT_PATH "/modules/{$theme}/preloads/")) {
            
$file_list XoopsLists::getFileListAsArray($dir);
            foreach (
$file_list as $file) {
                if (
preg_match('/(.php)$/i'$file)) {
                    
$file substr($file0, -4);
                    
$this->_preloadstheme[$theme][$file]['theme'] = $theme;
                    
$this->_preloadstheme[$theme][$file]['file'] = $file;
                    
$i++;
                }
            }
 }
 }


    
 
/**
 * Get available events and set them to go!
 *
 * @return void
 */
 
function setThemeEvents($theme='')
 {
        if (
$theme=='')
            
$theme $GLOBALS['xoopsConfig']['theme_set'];

        foreach(
$this->_preloadstheme[$theme] as $file => $themepreload) {
            include_once 
XOOPS_ROOT_PATH '/themes/' $themepreload['theme'] . '/preloads/' $themepreload['file']. '.php';
            
$class_name ucfirst($themepreload['theme']) . ucfirst($themepreload['file']) . 'Preload' ;
            if (!
class_exists($class_name)) {
                continue;
            }
            
$class_methods get_class_methods($class_name);
            foreach (
$class_methods as $method) {
                if (
strpos($method'event') === 0) {
                    
$event_name strtolower(str_replace('event'''$method));
                    
$event= array('class_name' => $class_name'method' => $method);
                    
$this->_events[$event_name][] = $event;
                }
            }
        
}
 }
 
/**
 * Triggers a specific event
 *
 * @param $event_name string Name of the event to trigger
 * @param $args array Method arguments
 *
 * @return void
 */
 
function triggerEvent($event_name$args = array())
 {
 
$event_name strtolower(str_replace('.'''$event_name));
        if (isset(
$this->_events[$event_name])) {
 foreach (
$this->_events[$event_name] as $event) {
 
call_user_func(array($event['class_name'], $event['method']), $args);
 }
 }
 }

}

/**
 * XoopsPreloadItem
 *
 * Class which is extended by any preload item.
 *
 * @copyright The XOOPS Project http://sourceforge.net/projects/xoops/
 * @license http://www.fsf.org/copyleft/gpl.html GNU public license
 * @package kernel
 * @subpackage class
 * @author trabis 
 */
class XoopsPreloadItem
{
 function 
XoopsPreloadItem()
 {
 }
}

?>


now at line of 329 of /class/theme.php add the following 3 lines.

$xoopsPreload =& XoopsPreload::getInstance();
$xoopsPreload->setThemePreloads($GLOBALS['xoopsConfig']['theme_set']);
$xoopsPreload->setThemeEvents($GLOBALS['xoopsConfig']['theme_set']);

3
Mazarin
Re: Preloads for only specific pages or parts of pages
  • 2010/3/11 13:57

  • Mazarin

  • Just can't stay away

  • Posts: 533

  • Since: 2008/12/10


So basically, there are specified preload triggers spread throughout the core? Is there a list of these?

4
Mamba
Re: Preloads for only specific pages or parts of pages
  • 2010/3/11 20:36

  • Mamba

  • Moderator

  • Posts: 11409

  • Since: 2004/4/23


See this post from Dylian

However, according to DJ, the preloads mechanism in 3.0 will change, so don't incorporate this technique in your modules for the time being, because you might need to rewrite it. But you can definitely experiment with it, because it's a cool feature

I don't have any details, so we have to be patient till 3.0 Alpha is released.

5
bjuti
Re: Preloads for only specific pages or parts of pages
  • 2010/3/11 22:01

  • bjuti

  • Just can't stay away

  • Posts: 871

  • Since: 2009/1/7 2


We are... but is there any news about 3.0? :) Tell us something, please :)

Login

Who's Online

315 user(s) are online (175 user(s) are browsing Support Forums)


Members: 0


Guests: 315


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Nov 30
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits