Login / Status
developer.Resource
Home . Documentation . Document Library . Tutorials
Sponsors
hosted by punkt.deTYPO3 and Open Source MagazineAOE Media

2.3. The main script

Mandatory initialization

Looking inside the main script of the Photo Marathon module we see first of all a few lines of traditional includes and evaluations:

// **********************************************// Basic initialization, don't tamper with this!// **********************************************unset($MCONF);include ("conf.php");include ($BACK_PATH."init.php");include ($BACK_PATH."template.php");include ("locallang.php");$BE_USER->modAccess($MCONF,1);

  • As you see the conf.php file is included before anything else. This is necessary in order to set the TYPO3_MOD_PATH constant.

  • init.php is then included. This will include some libraries from t3lib/, include the typo3conf/localconf configuration, connect to the database, authenticate the backend user login, include the $TCA etc. This file should always be included by any Typo3 Backend script.

  • Then the template.php file is included. This contains some function for printing out a page header and footer, including the default Typo3 colorscheme. In addition the language handling functions are found in there.

  • Then the locallang.php file is included. This sets the global variable $LOCAL_LANG as we see here for the Photo Marathon example:

<?$LOCAL_LANG = Array (        "default" => Array (    // Default english labels:        "title" => "Photo Marathon (EXAMPLE)",        "menu" => "Select a function",        "menu_overview" => "Overview of Photo Marathon records",        "menu_list" => "List Photo Marathon records on the page"    ),    "dk" => Array (        // Danish labels:        "title" => "Foto Maraton (EKSEMPEL)",        "menu" => "Vælg en funktion",        "menu_overview" => "Overblik over foto maraton emner",        "menu_list" => "Liste over foto maraton emner på siden"    ))?>

  • And finally $BE_USER->modAccess() is called. This will determine if the user has access to this module and if not issue an error message and exit.

(For the authoritative list of the global variables available in the Backend scope, please refer to “Inside Typo3” document)

Now the module access is granted and you can do whatever you like. Following is a description of various repeatedly used concepts, which may come in handy for you.

Typo3 Project Guidelines and “GPvars”

First of all I would like to draw attension to “Inside Typo3” document where a list of project guidelines are specified. You're strongly encouraged to read them through and follow them. This will ensure that your code will be consistent with the style in which Typo3 is programmed.

One of these guidelines - or specifications - is that your code must comply with the php.ini-optimized settings of PHP. Although most PHP-installs does not use these settings in order to be backwards compatible, Typo3 must be able to work perfectly in both environments.

The consequence of the php.ini-optimized settings is that global variables are not registered as vigorously as originally. In particular the incoming variables from HTTP_POST_VARS and HTTP_GET_VARS are not registered in the global space.

There are two solutions to this:

  1. In order to register the GET or POST vars (with precedence to POST), eg. “bart” and “homer” to the globals $bart and $homer, simply make this function call: t3lib_div::setGPvars("bart,homer");Slashes are automatically stripped on strings, but not recursively on arrays (unless you set the second parameter to “1”).

  2. You can also retrieve single variables directly like this:$bart = t3lib_div::GPvar(“bart”);

Page access

You should make sure that the user has read-access to the page according to his permissions. You can get an appropriate WHERE-clause for this from the $BE_USER object. The value you should pass is “1” which will check for read access (2,4,8 and 16 is for the other permissions. Look inside the header of class t3lib_tcemain to see the full list and meaning).

With this query string you can try to select the page and if you get a page record back the user has read access. In short it looks like this:

<?  // WHERE-clause part which you should use for selecting records so you can be sure,   // the BE_USER (backend user) has read access.$perms_clause = $BE_USER->getPagePermsClause(1);      // integer id value$id = intval($id);  // The page record of the current page (set by $id).   // $perms_clause ensures that the BE user has read access to this module.$pageinfo = t3lib_BEfunc::readPageAccess($id,$perms_clause);  // Has access??if ($id && is_array($pageinfo)) {    // Do your main things here    } else {    die("Sorry, you did not have read access to this page!");}?>

Header and footer

The template.php script contains some classes for the framework of the page, eg. correct charset based on system language.

You can either choose to just output a header and footer on the page, or like this is case call the API which will provide Typo3 style “sections” in your modules, including headers etc:

<?    // Make instance of class mediumDoc:$doc = new mediumDoc;$doc->backPath = $BACK_PATH;$doc->JScode = '    <SCRIPT>        function jumpToUrl(URL)    {            document.location = URL;        }    </SCRIPT>';    // Draw the header.$content.=$doc->startPage($LANG->getLL("title"));$content.=$doc->header($LANG->getLL("title"));$content.=$doc->spacer(5);$content.='<form action="index.php" method="POST">        <input type="hidden" name="id" value="'.$id.'">        <input type="hidden" name="AB" value="'.$AB.'">';    // Add you main things to $content here! Eg:$content.=$doc->section('Hello World!',"What a great module!");$content.=$doc->divider(5);    // Ending the page$content.=$doc->endPage();$content.='</form>';echo $content;?>

Notice how a default form is initialized, pointing to the current script and preserving the AB variable.

Taking a look into the class “template” which $doc is an instance of also reveals the internal variables at your disposal:

<?class template {    var $bgColor = "#F7F3EF";        // Light background color    var $bgColor2 = "#9BA1A8";        // Steel-blue    var $bgColor3 = "#F6F2E6";        // dok.color    var $bgColor4 = "#D9D5C9";        // light tablerow background, brownish    var $bgColor5 = "#ABBBB4";        // light tablerow background, greenish    var $bgColor6 = "#E7DBA8";        // light tablerow background, yellowish, for section headers.    var $hoverColor = "#254D7B";

(typo3/template.php)

These can be reached through $doc, eg:

$doc->bgColor2

Language specific labels ($LANG object)

In the above code listing you also see how to retrieve the language labels from the $LOCAL_LANG array configured in locallang.php:

    // Draw the header.$content.=$doc->startPage($LANG->getLL("title"));$content.=$doc->header($LANG->getLL("title"));
This is a clear example, how to get the value of the label, named “title”:
<?$LOCAL_LANG = Array (        "default" => Array (    // Default english labels:        "title" => "Photo Marathon (EXAMPLE)",        "menu" => "Select a function",        "menu_overview" => "Overview of Photo Marathon records",        "menu_list" => "List Photo Marathon records on the page"    ),    "dk" => Array (        // Danish labels:        "title" => "Foto Maraton (EKSEMPEL)",        "menu" => "Vælg en funktion",        "menu_overview" => "Overblik over foto maraton emner",        "menu_list" => "Liste over foto maraton emner på siden"    ))?>

A selector box menu

Making a selector menu like the one found in the Photo Marathon module is done easily through an API call. The menu will reload the page “onChange”. In addition there is a straight forward way to configure page TSconfig to enable/disable items in the menu as you like.

// **************************// Menu initializing// **************************    // MENU-ITEMS:    // If array, then it's a selector box menu    // If empty string it's just a variable, that'll be saved. $MOD_MENU = array(    "mode" => array(    // variable        "overview" => $LANG->getLL("menu_overview"),        // Menu value/item        "list" => $LANG->getLL("menu_list")                    // Menu value/item    ));    // page/be_user TSconfig settings:$modTSconfig = t3lib_BEfunc::getModTSconfig($id,"mod.".$GLOBALS["MCONF"]["name"]);    // ... and blinding of menu-items:$MOD_MENU["mode"] =     t3lib_BEfunc::unsetMenuItems($modTSconfig["properties"],$MOD_MENU["mode"],"menu.mode");    // Settings cleansed:$MOD_SETTINGS = t3lib_BEfunc::getModuleData($MOD_MENU, $SET, $GLOBALS["MCONF"]["name"]);

In this list the menu items is configured:

  • The $MOD_MENU[“mode”] array is filled with element pairs, “key” => “label”. Notice how the labels are fetched by $LANG->getLL() from the $LOCAL_LANG array!

  • $modTSconfig is loaded with the TSconfiguration combined of page TSconfig and user TSconfig (see the Administrators Guide for details here). We'll get back to that.

  • Then the $MOD_MENU[“mode”] is passed to a function which removed unavailable menu items, if any. I'll get back to that as well.

  • Finally the $MOD_SETTINGS is set and if there has been any change in states, the value of $MOD_SETTINGS is saved to the session data for the backend user. This ensures that the state of the menu is always remembered from each visit to the module.

Now you just need to insert the selectorbox menu somewhere in your module. Following our style from before this is easily done like this:

        // Render the main menu (&mode)        // Good thing about this menu is that it saves the state automatically for the backend user.    $menuHTML = t3lib_BEfunc::getFuncMenu($id,"SET[mode]",$MOD_SETTINGS["mode"],$MOD_MENU["mode"]);    $content.=$doc->section($LANG->getLL("menu"),$menuHTML);

Using “module TS config”

One final thing for you to know is that there is a concept established called TSconfig. This has threads into all kinds of things in Typo3. Basically it's the wellknown TypoScript configuration style used to configure values other than those bringing a website to life through the TypoScript template records.

TSconfig fields are available in page records and in Backend users and groups. Both may play a role in configuring your module if you like.

Now, change the type of the page “Images” from “Standard” to “Advanced” and enter this configuration script:

In your module, try to output the content of $modTSconfig by this line:

    // page/be_user TSconfig settings:$modTSconfig = t3lib_BEfunc::getModTSconfig($id,"mod.".$GLOBALS["MCONF"]["name"]);debug($modTSconfig);

This is what you get:

When this is used in the lines below, it happens to disable the “Overview...” menu item in the menu, which now looks like:

This is a common principle used in most modules with a main menu. Basically you can determine all by yourself how you wish your module to handle the TypoScript configuration available in $modTSconfig. The common thing is to use the “menu”-node to blind menu items. But you could add any number of customly defined nodes and let then determine further settings.

For inspiration look inside the Administrators Guide for examples.

A final demonstration of the power of “page TSconfig” is now shown. The TSconfig from above was inserted on the page “Upload” and so covers the three subsequent pages Portrait, Landscape and Citylife. Imagine you wish to enable the overview item on the page “Landscape” only while letting it remain blank on the other pages. This is easily done by modifying the page “Landscape like we just did with the page “Upload”:

In effect both menu items are available on the “Landscape” page, but not on the Portrait and Citylife pages!

If you wish you can also examine this configuration by the Web>Info module: