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($file, 0, -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($file, 0, -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']);