Code Authors: Forums
 

 

View next topic
View previous topic
Post new topic   Reply to topic    Code Authors Forum Index -> Coffee Shop
Author Message
Guardian
Site Admin
Site Admin



Joined: Jan 06, 2006
Posts: 4465
Location: Vsetin, Czech Republic

PostPosted: Sun Mar 04, 2012 8:24 pm Share on Linked InShare on FacebookShare on Twitter Reply with quote

With regard to moving some of those core functions, my proposal was merely to move them out of mainfile.php (it has way too much in it already) but since I was proposing a change to the way header.php is used (it would be theme specific with the core file as the default) that those functions that don't actually relate to header type functionality (but do have presentational code in them) could be moved out into header.php and therefore those functions would then also be theme specific.

The file /themes/Your_Theme/header.php wouldn't actually be needed/used or be present in the core package. It would only really be added as part of a theme where the developer needed to alter some specific functionality without the user having to alter the core code e.g. change the DOCTYPE for the theme, present the log-in form function differently (for a mobile theme as an example) from the core function or... something.
If you wanted, it should be equally possible to put those 'utility' type functions;
userblock()
loginbox()
adminblock()
blockfileinc()
message_box()
title()
themecenterbox()
into a new file like core_functions.php (included from mainfile.php) so they could be overridden for mobile display etc

_________________
TPD 
View user's profile Send private message Send e-mail Visit poster's website
spasticdonkey
Newbie
Newbie



Joined: May 12, 2009
Posts: 74
Location: TX

PostPosted: Sun Mar 04, 2012 11:37 pm Share on Linked InShare on FacebookShare on Twitter Reply with quote

Just got back in pocket, having some coffee and absorbing it all, but I like what I'm hearing. Smile

Short of a template system, which is a hard-sell at this juncture, a class system for core functions sounds great. Would also be perfectly happy with Guardian's solution as well. The class alternative may be more versatile as you would only have to extend the functions you want to override, correct? Somewhat how the nukeDH system defines it's functions then extends them by module?

Just FYI in regards to header.php, in my mobile theme @ current, I'm just checking for a constant early on within the head() function, and if it is defined I include an alternate header.php file and exit the function.

Code:
if (defined('RN_MOBILE_THEME')){

   if (file_exists(NUKE_THEMES_DIR . RN_MOBILE_THEME . '/header.php')) {
      include_once(NUKE_THEMES_DIR . RN_MOBILE_THEME . '/header.php');
      return;
   }
}


There is also a small edit regarding center block / message display so that the theme can control them if desired. Similar tweaks were required within the core footer.php file.

For my purposes the constant is defined in my mobile detection function (unless theme is selected as default theme via your-account), but in common practice you could define it within your theme.php... So many modules include header.php and footer.php directly, whatever logic/approach is used, it needs to be nested there, imo.
 
View user's profile Send private message Visit poster's website
Guardian
Site Admin
Site Admin



Joined: Jan 06, 2006
Posts: 4465
Location: Vsetin, Czech Republic

PostPosted: Mon Mar 05, 2012 9:41 am Share on Linked InShare on FacebookShare on Twitter Reply with quote

Regarding the use of a 'switching' constant, that's the approach I have taken here but I'm using it in config.php as so far, I'm not breaking anything (except validation on other themes). Really I should be checking which theme is loaded as well because there is only this theme (CA_Abyss) which I'm working on.

Totally agree with needing our existing header.php and footer.php being the 'controller' for this to maximise backward compatibility. Internally it's not an issue for core modules but if we changed the approach it would basically wipe out any third party module compatibility.

_________________
TPD 
View user's profile Send private message Send e-mail Visit poster's website
Guardian
Site Admin
Site Admin



Joined: Jan 06, 2006
Posts: 4465
Location: Vsetin, Czech Republic

PostPosted: Mon Mar 05, 2012 9:46 am Share on Linked InShare on FacebookShare on Twitter Reply with quote

Here's a Class that seems to do most of what we need, though obviously it needs modifications to incorporate our current nukeSEOdh functionality and the ides'a Spastic has pur forward with regard to supporting ALT stylesheets etc.
I did note one nice feature in here, it checks the JS file name to see if it starts with HTTP (possibly to cater for CDN use).
I have added some notes to the top of the file for reference and also just came across this site (Spastic may find useful)
http://www.html-5.com/metatags/

Code:


<?php
/**
 * Header Class
 * filename pageheader.class.php
 *
 * @author  Leandro Antonello
 * @license GNU Lesser General Public License
 * @internal tweaks by Guardian
 */

/**
 * @internal HTML5 does not allow the following so exceptions need to be made
 * (possibly based on the doctype constant?)
 * <meta http-equiv="Content-Script-Type" content="text/javascript" />
 * <meta http-equiv="Content-Style-Type" content="text/css" />
 * <meta http-equiv="Expires" content="0" />
 * <meta name="RESOURCE-TYPE" content="DOCUMENT" />
 * <meta name="DISTRIBUTION" content="GLOBAL" />
 *
 * Strongly recommend NAME attributes be made lowercase (doesn't break backward
 * compatibility with older doctypes)
 *
 * The 'language' attribute on JS file is not permitted
 * language="JavaScript" - needs to be dynamically removed
 *
 * html language attribute IS required for HTML5 and should immediately follow
 * the doctype declaration and be rendered before the opening HEAD
 * <html lang="en">
 *
 *

 */
class PageHeader
{
    // MEMBERS ====================================================================================
    /** @var string Holds the page DocType. */
    private $DocType;
    /** @var string Holds the Content-Type of page. Defaults to [text/html]. */
    private $ContentType;
    /** @var string Holds the page Character Set. Defaults to [UTF-8]. */
    private $Charset;

    /** @var string Holds the page title, showing in the browsers title bar. */
    public $Title;
    /** @var string Holds a page description. */
    public $Description;
    /** @var string Holds the keywords. */
    public $Keywords;
    /** @var string Holds the page rating. */
    public $Rating;
    /** @var array Array for robots meta tags. */
    public $Robots;
    /** @var string Path to favorite icon. */
    public $Favicon;

    /** @var array Holds the various page headers. */
    private $Headers;
    /** @var array Holds a the META tags. */
    private $Metas;
    /** @var array Array for page styles (CSS). */
    private $Styles;
    /** @var array Array for page scripts (javascript). */
    private $Scripts;

    // CONSTANTS ==================================================================================
    const DT_HTML_2      = '<!DOCTYPE html PUBLIC "-//IETF//DTD HTML 2.0//EN">';
    const DT_HTML_32     = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">';
    const DT_HTML_401_ST = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">';
    const DT_HTML_401_TR = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">';
    const DT_HTML_401_FS = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">';
    const DT_XHTML_10_BS = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.0//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic10.dtd">';
    const DT_XHTML_10_ST = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
    const DT_XHTML_10_TR = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
    const DT_XHTML_10_FS = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">';
    const DT_XHTML_11    = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">';
    const DT_XHTML_11_BS = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd">';
    const DT_HTML5       = '<!DOCTYPE html>';
    // CONSTRUCTOR ================================================================================
    public function PageHeader( $title, $doctype = self::DT_XHTML_11, $ctype = 'text/html', $charset = 'ISO-8859-1' )
    {
        $this->Title = $title;
        $this->DocType = $doctype;
        $this->ContentType = $ctype;
        $this->Charset = $charset;

        $this->Description = null;
        $this->Keywords = null;
        $this->Rating = "General";
        $this->Robots = null;
        $this->Favicon = null;

        $this->Headers = null;
        $this->Metas   = null;
        $this->Styles  = null;
        $this->Scripts = null;

        $this->updateMetas();
    }

    // DESTRUCTOR =================================================================================
    public function __destruct()
    {
        //
    }

    // PROPERTIES =================================================================================
    /**
     * Sets the DocType property.
     * @param constant $newDocType One of the DocType Constants
     */
    public function setDocType( $newDocType ) { $this->DocType = $newDocType; }

    /**
     * Sets the Charset property.
     * @param string $newCharset The Charset.
     */
    public function setCharset( $newCharset ) { $this->Charset = $newCharset; $this->updateMetas(); }

    /**
     * Sets the Content Type property.
     * @param string $newContentType The new Content Type.
     */
    public function setContentType( $newContentType ) { $this->ContentType = $newContentType; $this->updateMetas(); }

    // PUBLIC METHODS =============================================================================
    /** Write down all page header. */
    public function write()
    {
        // Write HTTP Headers:
        $this->writeHeaders();

        // Write HTML Doc -------------------------------------------------------------------------
        echo $this->DocType, "\r\n";
        echo '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="pt-br" lang="pt-br">', "\r\n";
        echo '<head>', "\r\n";

        // Site Title
        echo '<title>', $this->Title, '</title>', "\r\n";

        // Metas ----------------------------------------------------------------------------------
        foreach( $this->Metas as $meta )
        {
            echo $meta, "\r\n";
        }

        // Description ----------------------------------------------------------------------------
        if( !is_null($this->Description) )
            echo '<meta name="description" content="'. $this->Description .'" />';

        // Keywords -------------------------------------------------------------------------------
        if( !is_null($this->Keywords) )
            echo '<meta name="keywords" content="'. $this->Keywords .'" />';

        // Rating ---------------------------------------------------------------------------------
        if( !is_null($this->Rating) )
            echo '<meta name="rating" content="'. $this->Rating .'" />';

        // Robots ---------------------------------------------------------------------------------
        if( !is_null($this->Robots) )
        {
            foreach( $this->Robots as $robot )
                echo '<meta name="'. $robot['name'] .'" content="'. $robot['content'] .'" />';
        }

        // Favicon --------------------------------------------------------------------------------
        if( !is_null($this->Favicon) )
        {
            echo '<link rel="shortcut icon" href="'. $this->Favicon .'" />';
            echo '<link rel="icon" href="'. $this->Favicon .'" />';
        }

        // TOKEN for avoiding cache.
        $token = microtime();

        // Styles ---------------------------------------------------------------------------------
        for($i = 0; $i < count($this->Styles); $i++)
        {
            $css = $this->Styles[$i]['file'];

            if( !$this->Styles[$i]['cache'] )
            {
                $css .= "?token=" . $token;
            }

            echo '<link rel="stylesheet" type="text/css" href="', $css,'" />', "\r\n";
        }

        // Scripts --------------------------------------------------------------------------------
        for($i = 0; $i < count($this->Scripts); $i++)
        {
            $js = $this->Scripts[$i]['file'];

            if( !$this->Scripts[$i]['cache'] )
            {
                $js .= "?token=" . $token;
            }

            echo '<script type="text/javascript" src="', $js,'"></script>', "\r\n";
        }

        echo '</head>', "\r\n";
    }
    /**
     * Adds a HTTP Header.
     * @param string $header_str Header with the same form of PHP's header() function.
     */
    public function addHeader( $header_str )
    {
        if( is_null($this->Headers) )
            $this->Headers = array();

        $this->Headers[] = $header_str;
    }

    /**
     * Defaults HTTP headers to avoiding page cache.
     */
    public function addDefaults()
    {
        if( is_null($this->Headers) )
            $this->Headers = array();

        $this->Headers[0] = "Expires: Mon, 26 Jul 1997 05:00:00 GMT";
        $this->Headers[1] = "Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT";
        $this->Headers[2] = "Cache-Control: no-store, no-cache, must-revalidate";
        $this->Headers[3] = "Cache-Control: post-check=0, pre-check=0";
        $this->Headers[4] = "Pragma: no-cache";
    }

    /**
     * Adds a meta tag.
     * @param string $meta The entire meta tag as string.
     */
    public function addMeta( $meta )
    {
        $this->updateMetas();

        $this->Metas[] = $meta;
    }

    /**
     * Adds a robot specific meta tag.
     * @param string $robot_name Robot name (robots, googlebot, msnbot, slurp...)
     * @param string $content    The value (index, follow, noodp...)
     */
    public function addRobot( $robot_name, $content )
    {
        if( is_null($this->Robots) )
            $this->Robots = array();

        $this->Robots[] = array('name'=>$robot_name, 'content'=>$content);
    }

    /**
     * Adds a cascade style sheet file.
     * @param string $css        Absolute or relative path.
     * @param boolean $cacheable Indicates if the file will be cacheable. Defaults to TRUE.
     */
    public function addStyle( $css, $cacheable = true )
    {
        // Check if file is local or url
        if( strpos($css, "http") === false )
        {
            // File is local. Check if file exists.
            if( !file_exists($css) )
            {
                echo "<li>File [$css] not found!</li>";
                return;
            }
        }

        if( is_null($this->Styles) )
            $this->Styles = array();

        $this->Styles[] = array('file'=>$css, 'cache'=>$cacheable);
    }
    /** Alias to addStyle() */
    public function addCss( $css, $cache = true ) { $this->addStyle($css, $cache); }

    /**
     * Adds a script file.
     * @param string $js         Absolute or relative path to file.
     * @param boolean $cacheable Indicates if the file will be cacheable. Defaults to TRUE.
     */
    public function addScript( $js, $cacheable = true )
    {
        // Check if JS is local or url
        if( strpos($js, "http") === false )
        {
            // File is local. Check if file exists
            if( !file_exists($js) )
            {
                echo "<li>File [$js] not found!</li>";
                return;
            }
        }

        if( is_null($this->Scripts) )
            $this->Scripts = array();

        $this->Scripts[] = array('file'=>$js, 'cache'=>$cacheable);
    }
    /** Alias to addScript() */
    public function addJs( $js, $cache = true ) { $this->addScript($js, $cache); }

    /**
     * Adds a CSS or JS file without generates an error if file not exists.
     * @param string $css_or_js Path to CSS or JS file
     * @param bool $cacheable   Indicates if the file can be cacheable or not. Defaults to TRUE.
     */
    public function addSilently( $css_or_js, $cacheable = true )
    {
        // Check if file is local or remote
        if( strpos($css_or_js, "http") === false )
        {
            // Local file, check if file exists silently
            if( !file_exists($css_or_js) ) return;
        }

        // Check type
        $ext = substr($css_or_js, -3);

        switch( $ext )
        {
            case '.js':
                if( is_null($this->Scripts) )
                    $this->Scripts = array();

                $this->Scripts[] = array('file'=>$css_or_js, 'cache'=>$cacheable);
                break;
            case 'css':
                if( is_null($this->Styles) )
                    $this->Styles = array();

                $this->Styles[] = array('file'=>$css_or_js, 'cache'=>$cacheable);
                break;
        }
    }

    // PRIVATE METHODS ============================================================================
    /**
     * Write down all headers.
     */
    private function writeHeaders()
    {
        if( is_null($this->Headers) ) return;

        foreach($this->Headers as $hd)
        {
            header($hd);
        }

        header("Content-Type: ". $this->ContentType ."; charset=". $this->Charset ."");
    }

    /**
     * Updates the Meta tags.
     */
    private function updateMetas()
    {
        if( is_null($this->Metas) )
            $this->Metas = array();

        $this->Metas[0] = '<meta http-equiv="Content-Type" content="' . $this->ContentType . '; charset=' . $this->Charset . '" />';
    }

    // PUBLIC STATIC METHODS ======================================================================
    /**
     * Fast way to add the most common XHTML headers.
     * @param string %title   Page title.
     * @param string $charset Character SET of page ('UTF-8', 'ISO-8859-1', etc)
     * @param array $styles   Array of CSS files.
     * @param array $scripts  Array of javascript files.
     */
    public static function xhtmlHeaders( $title, $charset, $styles, $scripts )
    {
        //echo "PageHeaders::xhtmlHeaders('$title', '$charset', $styles, $scripts) <br />";
        // Check Arrays
        if( !is_array($styles) ) return;
        if( !is_array($scripts) ) return;

        // Create an instance
        $hd = new PageHeader($title);

        $hd->setCharset($charset);
        $hd->addDefaults();

        // Add Styles
        for($i = 0; $i < count($styles); $i++)
        {
            $hd->addCss($styles[$i]);
        }

        // Add Scripts
        for($i = 0; $i < count($scripts); $i++)
        {
            $hd->addJs($scripts[$i]);
        }

        // Write all
        $hd->write();
    }

    /**
     * Add headers to force download.
     * @param string $file Path to file
     * @return void
     */
    public static function forceDownload( $file )
    {
        // Check file
        if( !file_exists($file) ) return;

        // Add headers
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename='.basename($file));
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));

        // Flush
        ob_clean();
        flush();

        // Output file
        readfile($file);
    }

    /**
     * Static method to add headers that prevent page cache.
     * @param bool $output Indicates if headers will be printed out (true) or not (false).
     * @return mixed
     */
    public static function noCache( $output = true )
    {
        $headers = array();
        $headers[] = "Expires: Mon, 26 Jul 1997 05:00:00 GMT";
        $headers[] = "Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT";
        $headers[] = "Cache-Control: no-store, no-cache, must-revalidate";
        $headers[] = "Cache-Control: post-check=0, pre-check=0";
        $headers[] = "Pragma: no-cache";

        if( $output )
        {
            foreach($headers as $hd)
            {
                header($hd);
            }
            return;
        }

        return $headers;
    }
}

_________________
TPD 
View user's profile Send private message Send e-mail Visit poster's website
Guardian
Site Admin
Site Admin



Joined: Jan 06, 2006
Posts: 4465
Location: Vsetin, Czech Republic

PostPosted: Mon Mar 05, 2012 10:15 am Share on Linked InShare on FacebookShare on Twitter Reply with quote

spasticdonkey wrote:
Just FYI in regards to header.php, in my mobile theme @ current, I'm just checking for a constant early on within the head() function, and if it is defined I include an alternate header.php file and exit the function.

Code:
if (defined('RN_MOBILE_THEME')){

   if (file_exists(NUKE_THEMES_DIR . RN_MOBILE_THEME . '/header.php')) {
      include_once(NUKE_THEMES_DIR . RN_MOBILE_THEME . '/header.php');
      return;
   }
}


With my original proposal, you wouldn't need to do that. Mainfile would look to see if there was a header.php in your theme directory and automatically use that instead of the default one BUT you just brought up a very good point, one I had missed, which is why I love these brainstorming sessions.
Modules call the core header.php and footer files directly so we would actually need to do the detecting and switching in those files NOT in mainfile.php. (kicking myself now).

Sometimes I really loath all the legacy stuff that has been inherited, like mainfile. Strictly speaking, index.php should be our master (controller) file but obviously Burzi's "I learnt PHP in a week" meant he had to use mainfile so he could include it in modules to gain access to the core functions needed when porting third party scripts into modules - he obviously forgot that the root index.php file includes mainfile any way Smile.

_________________
TPD 
View user's profile Send private message Send e-mail Visit poster's website
Guardian
Site Admin
Site Admin



Joined: Jan 06, 2006
Posts: 4465
Location: Vsetin, Czech Republic

PostPosted: Mon Mar 05, 2012 4:08 pm Share on Linked InShare on FacebookShare on Twitter Reply with quote

I haven't actually got to the bits we are interested in yet but I did do some moving around today to see how things could potentially work, keeping mind RN might want to go with a templating system or user API or whatever so I started playing on my local box restructuring a few things to get them ready for future enhancements.
So far is is what I've done (sorry it's a bit of a blow-by-blow account) and done preliminary testing on.
For security, I added a new constant at the top of mainfile
Code:


define ('NUKE_CORE', true); // access control for core files

All the new 'core' files test against this to prevent direct file access.
Code:


PART 1

DUPLICATE the core header.php and place it in your test theme directory
themes/Your_Theme/header.php

MODIFY the core header.php file, place this code at the top

// start new code for theme dependent header file - G
global $ThemeSel; // site wide global
if(file_exists('themes/'.$ThemeSel.'/header.php')){
    include_once 'themes/'.$ThemeSel.'/header.php';
} else { //end switch- closing bracket at EOF


MODIFY core header.php place this closing curly bracket at the bottom
}


REMOVE (or comment out) the following functions from mainfile.php and put them in the core
header.php file AND your testing theme header.php file

writeHEAD()
writeBODYJS()
===============================================

PART 2
CREATE a new 'core' directory
webroot/includes/core/

CREATE file includes/core/detectTheme.php
COPY the function get_theme() from mainfile.php into the new file

REMOVE (or comment out) in mainfile.php the function;
get_theme()

EDIT mainfile.php
FIND
include_once NUKE_INCLUDE_DIR . 'gfx_check.php';

ADD AFTER
include_once NUKE_INCLUDE_DIR . 'core/detectTheme.php';

It's needed in that location because because it's used on the next line
in mainfile.php
=============================================================

STEP 3 Remove remaining header related functions

CREATE file include/core/core_Header.php
ADD to this file the following functions (in their entirety) from mainfile.php
function addCSSToHead()
function addJSToHead()
function addJSToBody()


EDIT mainfile.php
FIND
function addCSSToHead($content, $type='file') {

ADD BEFORE
include_once NUKE_INCLUDE_DIR . 'core/core_Header.php';

DELETE or comment out in mainfile.php the function
function addCSSToHead()
function addJSToHead()
function addJSToBody()

======================================================

STEP 4 - moving the ads() function from mainfile.php
FIND in mainfile.php
function ads($position)
ADD BEFORE
include_once NUKE_INCLUDE_DIR .'core/core_Ads.php';


FIND function ads()
REMOVE entire function and place in new file called
includes/core/core_Ads.php
==================================================

STEP 5 moving themepreview (used by News module) which might need theme dependent styling
FIND in mainfile.php
if(!function_exists('themepreview')) {

ADD BEFORE
if(file_exists('themes/'.$ThemeSel.'/themePreview.php')) {
    include_once 'themes/'.$ThemeSel.'/themePreview.php';
    } else {
        include_once 'includes/core/core_themePreview.php';
    }

REMOVE from mainfile.php the function themepreview()
PASTE in into file 'includes/core/core_themePreview.php

*NOTE* the function is wrapped in an if function exists test - this does not
seem to be needed as the function is only declared once in RN's core
===============================================

STEP 6 - moving the language related code
FIND the following 2 functions
function get_lang($module)
function lang_select_list(...)

CUT and PASTE the entire functions into new file
includes/core/core_detectLanguage.php

EDIT mainfile.php
FIND
// This must come after $currentlang is defined
require_once NUKE_MODULES_DIR . 'Groups/includes/nsngr_func.php';

ADD BEFORE
include_once NUKE_INCLUDE_DIR .'core/core_detectLanguage.php';
============================================

STEP 7 - moving user access control related functions
FIND in mainfile.php the following functions
function is_admin()
function is_mod_admin()
function is_user()
function is_group()

CUT and PASTE into new file
includes/core/core_userAccessControl.php

EDIT mainfile.php
FIND
require_once NUKE_INCLUDE_DIR . 'nukesentinel.php';

ADD BEFORE
include_once NUKE_INCLUDE_DIR .'core/core_userAccessControl.php';
===============================================


I still have more to do before I can get to the nitty gritty stuff (new header Class) but this represents what I've done today plus around 8 hours of testing to make sure nothing fell over.
I'll do some more work later/tomorrow and I'll then create a package of changed files if anyone else (RN Team only) want to do there own testing / merge into RN or whatever they see fit.

_________________
TPD 
View user's profile Send private message Send e-mail Visit poster's website
spasticdonkey
Newbie
Newbie



Joined: May 12, 2009
Posts: 74
Location: TX

PostPosted: Mon Mar 05, 2012 5:40 pm Share on Linked InShare on FacebookShare on Twitter Reply with quote

Not having a whole lot of experience working with classes, there is a little grey area for me here; but I think I see where you are going. I assume the functions are being separated out so they can be extended by a given theme?

I imagine in most cases if "function_exists" is used in mainfile, it is so that a theme.php has a chance to declare the functions first. Palbin would have to speak to this, but there may be themes that declare their own themepreview function.

Not to muddy the waters even further as this discussion is already going in several directions, but while it's on my mind... Smile

I would also like to be able to override blocks by theme in blockfileinc()

Code:
if (file_exists('themes/' . $ThemeSel . '/blocks/' . $blockfile . '.php')) {

   include_once 'themes/' . $ThemeSel . '/blocks/' . $blockfile . '.php';
}


Not sure if it's good practice to be checking for files that usually won't exist, but we could certainly set a constant to determine if the check even needs to be run. Same could be said for the little used functionality of index.php, which allows for overriding any module $file by theme.. In the context of my mobile theme, this feature ended up being essential for including lightweight mobile versions of a few modules; but 99.9% of the time this check is unnecessary. Although maybe file_exists is efficient enough where it doesn't really make a difference...?
 
View user's profile Send private message Visit poster's website
Guardian
Site Admin
Site Admin



Joined: Jan 06, 2006
Posts: 4465
Location: Vsetin, Czech Republic

PostPosted: Mon Mar 05, 2012 6:05 pm Share on Linked InShare on FacebookShare on Twitter Reply with quote

Yes, you have pretty much hit the nail on the head.
The object of moving some stuff out of mainfile and into individual core/core_XX.php files is as a pre-cursor to re-writing the core/core_XX.php file at some point in the future without clogging up mainfile more than it already is.
As an example, the file that holds all the user access related stuff is now in core/core_userAccessControl.php
This could potential be re-written as a user access Class. The same for the detectLanguage.php file - that could eventually be re-written and made more efficient, taking into account our need to use lang-en.php instead of lang-english.php or it could even do both, depending on what files we have in the core but since the file would only be dealing with language related code, it isn't clogging up mainfile and would potentially be easier to maintain.

Of course I'm making lot's of assumptions here. The RN Team might not want to head in this direction but it makes sense to me and I'll be using on this site when I'm finished tinkering with it, so it isn't as though it's wasted time (until RN 3.0 comes out and I wipe the changes here lol).

With regard to the if function exists test, that's what I love about the RN Team. Even though I could see quite clearly in the code, the function was only ever declared that once in mainfile, I knew that test was there for a very good reason and you've just confirmed the reason for it, so thanks for that (I'll tweak my code accordingly). Yes, since themes/YOUR_THEME/theme.php is loading before that function is declared in mainfile, it would have caused a problem declaring the function twice (hence the test).

Regarding blockfileince() I'm about to tackle that one so I'll see what can be done it. I had envisaged it would need to be (optionally) style by the theme so I'll keep that in mind.

I haven't tidied up my mainfile.php yet, there is lot's of empty whitespace I can get rid of but so far I have moved out about 600 lines of code.

_________________
TPD 
View user's profile Send private message Send e-mail Visit poster's website
Guardian
Site Admin
Site Admin



Joined: Jan 06, 2006
Posts: 4465
Location: Vsetin, Czech Republic

PostPosted: Mon Mar 05, 2012 6:45 pm Share on Linked InShare on FacebookShare on Twitter Reply with quote

Just took a quick look at blockfileinc() and it seems to reference functions that should already be in theme.php (once function at least so far) so I'll have to dig deeper on that one but out of time for today unless I pull another all-nighter.

_________________
TPD 
View user's profile Send private message Send e-mail Visit poster's website
Guardian
Site Admin
Site Admin



Joined: Jan 06, 2006
Posts: 4465
Location: Vsetin, Czech Republic

PostPosted: Mon Mar 05, 2012 7:02 pm Share on Linked InShare on FacebookShare on Twitter Reply with quote

OK, within blockfileinc() it uses two other function;
themesidebox() and themecenterbox()
Both of those functions should be declared within the theme.php though I see only three of the core themes actually have a themecenterbox() function.

Currently blockfileince() only looks to see if files exist in the blocks directory, are you saying there may be a need to also look in themes/blocks/ as well?

_________________
TPD 
View user's profile Send private message Send e-mail Visit poster's website
spasticdonkey
Newbie
Newbie



Joined: May 12, 2009
Posts: 74
Location: TX

PostPosted: Mon Mar 05, 2012 7:57 pm Share on Linked InShare on FacebookShare on Twitter Reply with quote

yes, basically i was thinking along the lines of
Code:
function blockfileinc($title, $blockfile, $side, $bid = '') {

   $blockfiletitle = $title;
   if (file_exists('themes/' . $ThemeSel . '/blocks/' . $blockfile)) {
      include_once 'themes/' . $ThemeSel . '/blocks/' . $blockfile;
   } else if(file_exists('blocks/'.$blockfile)) {
      include_once NUKE_BLOCKS_DIR . $blockfile;
   } else {
      $content = _BLOCKPROBLEM;
   }
   if (empty($content)) {
      $content = _BLOCKPROBLEM2;
   } else {
      global $tnsl_bAutoTapBlocks;
      if (defined('TNSL_USE_SHORTLINKS') && isset($tnsl_bAutoTapBlocks) && $tnsl_bAutoTapBlocks) {
         $content = tnsl_fShortenBlockURLs($blockfile, $content);
      }
   }

   if ($side == 'l' or $side == 'r') {
      themesidebox($blockfiletitle, $content, $bid);
   } else {
      themecenterbox($blockfiletitle, $content, $bid);
   }
}


or if you don't want to check that every block load you could do
Code:
if (defined('THEME_BLOCKS')) {

   if (file_exists('themes/' . $ThemeSel . '/blocks/' . $blockfile)) {
      include_once 'themes/' . $ThemeSel . '/blocks/' . $blockfile;
   }
} else if(file_exists('blocks/'.$blockfile)) {
   include_once NUKE_BLOCKS_DIR . $blockfile;
} else {
   $content = _BLOCKPROBLEM;
}

and define THEME_BLOCKS within your theme.php if you want to override blocks.
 
View user's profile Send private message Visit poster's website
Palbin
Newbie
Newbie



Joined: Nov 20, 2008
Posts: 55

PostPosted: Mon Mar 05, 2012 7:59 pm Share on Linked InShare on FacebookShare on Twitter Reply with quote

spasticdonkey wrote:
yes, basically i was thinking along the lines of
Code:
function blockfileinc($title, $blockfile, $side, $bid = '') {

   $blockfiletitle = $title;
   if (file_exists('themes/' . $ThemeSel . '/blocks/' . $blockfile)) {
      include_once 'themes/' . $ThemeSel . '/blocks/' . $blockfile;
   } else if(file_exists('blocks/'.$blockfile)) {
      include_once NUKE_BLOCKS_DIR . $blockfile;
   } else {
      $content = _BLOCKPROBLEM;
   }
   if (empty($content)) {
      $content = _BLOCKPROBLEM2;
   } else {
      global $tnsl_bAutoTapBlocks;
      if (defined('TNSL_USE_SHORTLINKS') && isset($tnsl_bAutoTapBlocks) && $tnsl_bAutoTapBlocks) {
         $content = tnsl_fShortenBlockURLs($blockfile, $content);
      }
   }

   if ($side == 'l' or $side == 'r') {
      themesidebox($blockfiletitle, $content, $bid);
   } else {
      themecenterbox($blockfiletitle, $content, $bid);
   }
}


or if you don't want to check that every block load you could do
Code:
if (defined('THEME_BLOCKS')) {

   if (file_exists('themes/' . $ThemeSel . '/blocks/' . $blockfile)) {
      include_once 'themes/' . $ThemeSel . '/blocks/' . $blockfile;
   }
} else if(file_exists('blocks/'.$blockfile)) {
   include_once NUKE_BLOCKS_DIR . $blockfile;
} else {
   $content = _BLOCKPROBLEM;
}

and define THEME_BLOCKS within your theme.php if you want to override blocks.


You could building an array of blocks you want to override in the theme and use the array against block names.
 
View user's profile Send private message Send e-mail
Guardian
Site Admin
Site Admin



Joined: Jan 06, 2006
Posts: 4465
Location: Vsetin, Czech Republic

PostPosted: Mon Mar 05, 2012 9:44 pm Share on Linked InShare on FacebookShare on Twitter Reply with quote

That's definitely an idea Smile
For now, because I don't want to get side tracked too much, I've simple done
Code:


global $ThemeSel;
if(file_exists('themes/'.$ThemeSel.'/blocks/'.$blockfile.'')){
        $file = 'themes/'.$ThemeSel.'/blocks/'.$blockfile.'';
    } else {
        $file = file_exists('blocks/' . $blockfile);
    }


Probably not the most efficient way but it works.

I think I'm pretty much done with mainfile stripping now. Got it down from 1928 lines of code to 1060, so it's roughly half the size it was.
Of course I haven't actually saved that much as the code has just moved locations but I think with the restructuring, it's a bit easier to maintain and definitely easier to concentrate on a particular area of functionality.
You could probably go even further and strip out the code relating the wysiwyg editor (which would make it real easy to switch from FCK to CKEditor - you just include a different file) and probably also strip out some of the more general functions like validateEmailFormat() into a core_Functions.php file but I need to get back on track and start diving into the header related functions.

I'll make up a package and send it Palbin and Spastic for perusal, not sure if it will be tonight (almost 11pm here) but if not, first thing tomorrow.
I had to juggle the order in which some of the files were included from mainfile but I've tested as extensively as I can; logging in/out as a user and admin, tested Group permission access, created/deleted blocks and mosdules, moved the order of blocks and of course tested the alternative includes work as expected.

_________________
TPD 
View user's profile Send private message Send e-mail Visit poster's website
Guardian
Site Admin
Site Admin



Joined: Jan 06, 2006
Posts: 4465
Location: Vsetin, Czech Republic

PostPosted: Mon Mar 05, 2012 10:19 pm Share on Linked InShare on FacebookShare on Twitter Reply with quote

Actually, I'm attaching them here zipped up, I'll just delete them in a day or two

_________________
TPD 
View user's profile Send private message Send e-mail Visit poster's website
Palbin
Newbie
Newbie



Joined: Nov 20, 2008
Posts: 55

PostPosted: Mon Mar 05, 2012 10:57 pm Share on Linked InShare on FacebookShare on Twitter Reply with quote

got it
 
View user's profile Send private message Send e-mail
Display posts from previous:       
Post new topic   Reply to topic    Code Authors Forum Index -> Coffee Shop

View next topic
View previous topic
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum

 
Forums ©
linear-bunchlinear-bunchlinear-bunch