[TYPO3] extend caching by a variable (like pi_autoCacheFields)

christian oettinger christian.oettinger at gmx.de
Sat Feb 2 00:24:42 CET 2008


In the end did it the other way round, dropped OverrideId (and with it 
real_uid) and used a user func (which is actually far more powerful, so 
maybe there is also another, easier TS-solution). Anyway it works.
Thanks to Peter Niederlag for hints and help and Peter Klein for the 
basic script.

The script contains some explanation and two example TS configurations 
for two different tasks (please leave it as it is to make this script 
useful for others also - and for future understanding as well)

Example 2 is my typoscript configuration for the problem of this thread.
Example 1 explains further uses of the script


---------- complete USER script oe_itemArrayProcFunc.php with example 
configurations: ----------



<?php
/***************************************************************
*  Copyright notice
*
*  (c) 1999-2005 Kasper Skaarhoj (kasperYYYY at typo3.com)
*  All rights reserved
*
*  This script is part of the TYPO3 project. The TYPO3 project is
*  free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  The GNU General Public License can be found at
*  http://www.gnu.org/copyleft/gpl.html.
*  A copy is found in the textfile GPL.txt and important notices to the 
license
*  from the author is found in LICENSE.txt distributed with these scripts.
*
*
*  This script 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.  See the
*  GNU General Public License for more details.
*
*  This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/**
  * This is an example of how to manipulate menu item arrays.
  * Used in the "testsite" package
  *
  * $Id: example_itemArrayProcFunc.php 639 2005-04-13 23:12:04Z mundaun $
  * Revised for TYPO3 3.6 June/2003 by Kasper Skaarhoj
  * XHTML compliant
  *
  * @author	Kasper Skaarhoj <kasperYYYY at typo3.com>
  *
  * Modified by Christian Oettinger. christian.oettinger at gmx.de
  * Modifications based on the script menuFunc.inc by Peter Klein. (all 
the names!)
  * For me it runs with typo3 4.0.4 XXXXX. But please note:
  * I am not too deep into typo3 and do not know if this is as compatible as
  * it should be regarding the core and extensions! Use at your own risk!
  */



/**
  * This function reads the data of a given table with given pid (both 
set in setup, see below)
  * and manipulates $menuArr to give out a Menu Part with a definable 
pi-Var, maybe from another
  * pid, maybe even from another table as 'pages'.
  * It is not a complete Content Object, but only a Menu Object, so you 
can combine "ordinary"
  * menus and (e.g. as submenus) the menus provided by this script. See 
complete TypoScript examples below:
  *
  * EXAMPLE 1:page.15.1 is an "ordinary" menu. Page.15.2 is altered by 
tis script to create a menu from
  * chosen records.
  *
  * ------ Begin Example Typoscript configuration 1: -------------
  *
  * includeLibs.oemenuitems = fileadmin/scripts/oe_itemArrayProcFunc.php
  *
  * page.15.2 = TMENU
  * page.15.2 {
  *    fontColor = #ffffff
  *    expAll = 0
  *    itemArrayProcFunc = user_itemArrayProcFuncTest
  *    itemArrayProcFunc.userFunc.oe_table = {$oe_menu_table}
  *
  *    # bit tricky to explain: The typolink below creates a GET variable to
  *    # choose whi record shall be shown. The name if that GET variable 
is set
  *    # inside of typolink by the line additionalParams.wrap
  *    # In the script we need that name again for NO/ACT-behaviour so here
  *    # we pass the parts of that name to the script. This is done for 
a Standard
  *    # Showuid-Variabel here. Example:
  *    # additionalParams.wrap= &table_whichpi[showUid]=|
  *    # itemArrayProcFunc.userFunc.oe_getvar_part1 = table_whichpi
  *    # itemArrayProcFunc.userFunc.oe_getvar_part2 = showUid
  *
  *    itemArrayProcFunc.userFunc.oe_getvar_part1 = tx_oecertificates_pi1
  *    itemArrayProcFunc.userFunc.oe_getvar_part2 = showUid
  *    itemArrayProcFunc.userFunc.select.pidInList = {$certificatesfolder}
  *    itemArrayProcFunc.userFunc.select.orderBy = sorting
  *
  *    NO {
  *       doNotLinkIt = 1
  *       ATagBeforeWrap = 1
  *       linkWrap = |
  *       stdWrap {
  *          data = field:name
  *          typolink {
  *             wrap = <SPAN class="dritteebene"> | </SPAN><BR>
  *             ATagBeforeWrap = 1
  *             parameter.data = TSFE:id
  *             additionalParams.field= uid
  *             additionalParams.wrap= &tx_oecertificates_pi1[showUid]=|
  *             useCacheHash = 1
  *             maxWidth = 40
  *          }
  *       }
  *      #stdWrap.data = debug:data
  *   }
  *
  *   ACT < .NO
  *   ACT = 1
  *   ACT {
  *       stdWrap {
  *          typolink {
  *          wrap = <SPAN class="womanist3"> |</SPAN><BR>
  *            #wrap = <SPAN class="womanist3"> |</SPAN><BR>
  *             ATagBeforeWrap = 1
  *         }
  *       }
  *    }
  * }
  *
  * ------ End Example Typoscript configuration 1 -------------
  *
  *
  * EXAMPLE 2: GMENU giving out a submenu form another pid (in my eyes 
beats built-in
  * OverrideId, for the possibility of choosing your variable and 
advanced caching-configurations)
  *
  *
  * ------ Begin Example Typoscript configuration 2: -------------

  *
  * includeLibs.oemenuitems = fileadmin/scripts/oe_itemArrayProcFunc.php

  *
  * page.15.25.1 = GMENU
  * page.15.25.1 {
  *    expAll = 0
  *    itemArrayProcFunc = user_itemArrayProcFuncTest
  *    itemArrayProcFunc.userFunc {
  *    	 oe_table = pages
  *
  *       # the follwoing two are a bit tricky to explain: The typolink 
below
  *       # creates a GET variable to choose which record shall be shown.
  *       # The name if that GET variable is set inside of typolink
  *       #  by the line additionalParams.wrap. In the script we
  *       # need that name again for NO/ACT-behaviour so here we pass
  *       # the parts of that name to the script. This is done for a
  *       # pi-Variable here, so easily used in the script. Example:
  *       # additionalParams.wrap= &table_whichpi[showUid]=|
  *       # itemArrayProcFunc.userFunc.oe_getvar_part1 = table_whichpi
  *       # itemArrayProcFunc.userFunc.oe_getvar_part2 = showUid
  *

  *    	 oe_getvar_part1 = tx_oeaimphotos_pi1
  *       oe_getvar_part2 = cat
  *
  *       select {
  *        	pidInList = 5
  *          orderBy = sorting
  *      }
  *    }
  *
  *    NO {
  *       wrap = <li>|</li>
  *       XY = 198,16
  *       backColor = #cecac3
  *       noLink = 1
  *       allStdWrap.typolink {
  *          data = field:name
  *             parameter.data = TSFE:id
  *             additionalParams.field= uid
  *             additionalParams.wrap= &tx_oeaimphotos_pi1[cat]=|
  *             useCacheHash = 1
  *       }
  *       10 = TEXT
  *       10 {
  *      	text.field = title
  * 		    fontFile =
  * 		    ... as usual
  *       }
  *    }
  *    ACT {
  *       noLink = 1
  *       ... as usual
  *    }
  *
  *    # for debug of the menu array:
  *    #stdWrap.data = debug:data
  * }
  * ------ End Example Typoscript configuration 2 -------------
  *
  *
  * Configuration:
  *
  *     itemArrayProcFunc.userFunc.oe_table = {$menu_table}
  *  is used to define, on which table the menu will be based. This is 
vital:
  *  if you configure your Setup to use tis script but Do not choose a 
table, script is terminated.
  *
  *     itemArrayProcFunc.userFunc.oe_getvar_part1
  *     itemArrayProcFunc.userFunc.oe_getvar_part2
  *  bit tricky to explain: The typplink below creates a GET variable to
  *  choose whi record shall be shown. The name if that GET variable is set
  *  inside of typolink by the line additionalParams.wrap
  *  In the script we need that name again for NO/ACT-behaviour so here
  *  we pass the parts of that name to the script. In the end it is simple.
  *  It's just your additionalParams.wrap split in two parts. Example:
  *  additionalParams.wrap= &table_whichpi[showUid]=|
  *  itemArrayProcFunc.userFunc.oe_getvar_part1 = table_whichpi
  *  itemArrayProcFunc.userFunc.oe_getvar_part2 = showUid

  *
  *  itemArrayProcFunc.userFunc.select.pidInList = {$certificatesfolder}
  *  starting point: points to the folder, where the records lie.
  *  though not tested you should be able to combine it with 
userFunc.select.recursive
  *
  *    stdWrap.data = field:name
  *  is used to define, which field of the above table is used to render 
the menu.
  *
  *    itemArrayProcFunc.userFunc.select.orderBy = sorting
  *  Like this of course only possible if you use manual ordernig in 
your extension.
  *  Otherwise the field sorting does not exist and it will not work.
  *
  *
  * @param	array		The $menuArr array which simply is a num-array of page 
records which goes into the menu.
  * @param	array		TypoScript configuration for the function. Notice that 
the property "parentObj" is a reference to the parent (calling) object 
(the tslib_Xmenu class instantiated)
  * @return	array		The modified $menuArr array
  */
function user_itemArrayProcFuncTest($menuArr,$conf)	{
	$lConf = $conf["userFunc."];
$get_vars=t3lib_div::_GET();

$getvar_part1 = $lConf['oe_getvar_part1'];
$getvar_part2 = $lConf['oe_getvar_part2'];
	$oe_actual_record = $get_vars[$getvar_part1][$getvar_part2];
	if ($conf['demoItemStates'])	{		// Used in the example of item states
		reset($menuArr);
		$c=0;
		$teststates=explode(',','NO,ACT,IFSUB,CUR,USR,SPC,USERDEF1,USERDEF2');
		while(list($k,$v)=each($menuArr))	{
			$menuArr[$k]['ITEM_STATE']=$teststates[$c];
			$menuArr[$k]['title'].= ($teststates[$c] ? ' ['.$teststates[$c].']' : 
'');

			$c++;
		}
	} else {	// used in the fake menu item example!		
		if ($lConf['oe_table']) {		// if table is set,
	
			$oe_this_page=$GLOBALS['TSFE']->id;
			#t3lib_div::debug ($lConf);
		
			$menuArr = array(); // resetting the array
			
			$res = 
$GLOBALS['TSFE']->cObj->exec_getQuery($lConf["oe_table"],$lConf["select."]);
			if ($error = $GLOBALS['TYPO3_DB']->sql_error()) {
				$GLOBALS['TT']->setTSlogMessage($error,3);
			} else {
				$GLOBALS['TT']->setTSlogMessage('NUMROWS: 
'.$GLOBALS['TYPO3_DB']->sql_num_rows($res));
				while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
					
					//$row['_OVERRIDE_HREF'] = 'index.php?id='.$oe_this_page;										
					//$row['_ADD_GETVARS'] = '&'.$lConf["oe_table"].'='.$row['uid'];

					if ($row['uid'] == $oe_actual_record) {
						$row['ITEM_STATE'] = 'ACT';
					} else {
						$row['ITEM_STATE'] = 'NO';
					}
					$menuArr[] = $row;

				}
			}
			
		} else {					
								
				die ('user_itemArrayProcFuncTest: you have not set a table. This is 
vital. '.
				 	'Please use itemArrayProcFunc.userFunc.oe_table inside NO in your 
Menu object.');
		}
	}
	#t3lib_div::debug ($menuArr);
	return $menuArr;
}

/**
  * Used in the menu item state example of the "testsite" package at 
page-path "/Intro/TypoScript examples/Menu object examples/Menu state test/"
  *
  * @param	array		The menu item array, $this->I (in the parent object)
  * @param	array		TypoScript configuration for the function. Notice that 
the property "parentObj" is a reference to the parent (calling) object 
(the tslib_Xmenu class instantiated)
  * @return	array		The processed $I array returned (and stored in 
$this->I of the parent object again)
  * @see tslib_menu::userProcess(), tslib_tmenu::writeMenu(), 
tslib_gmenu::writeMenu()
  */
function user_IProcFuncTest($I,$conf)	{
	$itemRow = $conf['parentObj']->menuArr[$I['key']];

		// Setting the document status content to the value of the page title 
on mouse over
	$I['linkHREF']['onMouseover'].='extraRollover(\''.rawurlencode($itemRow['title']).'\');';
	$conf['parentObj']->I = $I;
	$conf['parentObj']->setATagParts();
	$I = $conf['parentObj']->I;
	if ($I['parts']['ATag_begin'])	$I['parts']['ATag_begin']=$I['A1'];

	if ($conf['debug'])	{
			// Outputting for debug example:
		echo 'ITEM: <h2>'.htmlspecialchars($itemRow['uid'].': 
'.$itemRow['title']).'</h2>';
		t3lib_div::debug($itemRow);
		t3lib_div::debug($I);
		echo '<hr />';
	}
		// Returns:
	return $I;
}


?>


More information about the TYPO3-english mailing list