TYPO3 API  SVNRelease
example_callfunction.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 1999-2011 Kasper Skårhøj (kasperYYYY@typo3.com)
00006 *  All rights reserved
00007 *
00008 *  This script is part of the TYPO3 project. The TYPO3 project is
00009 *  free software; you can redistribute it and/or modify
00010 *  it under the terms of the GNU General Public License as published by
00011 *  the Free Software Foundation; either version 2 of the License, or
00012 *  (at your option) any later version.
00013 *
00014 *  The GNU General Public License can be found at
00015 *  http://www.gnu.org/copyleft/gpl.html.
00016 *  A copy is found in the textfile GPL.txt and important notices to the license
00017 *  from the author is found in LICENSE.txt distributed with these scripts.
00018 *
00019 *
00020 *  This script is distributed in the hope that it will be useful,
00021 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023 *  GNU General Public License for more details.
00024 *
00025 *  This copyright notice MUST APPEAR in all copies of the script!
00026 ***************************************************************/
00027 /**
00028  * USER cObject EXAMPLE FILE
00029  *
00030  * This is an example of how to use your own functions and classes directly from TYPO3.
00031  * Used in the "testsite" package
00032  *
00033  * $Id: example_callfunction.php 5165 2009-03-09 18:28:59Z ohader $
00034  * Revised for TYPO3 3.6 June/2003 by Kasper Skårhøj
00035  * XHTML compliant
00036  *
00037  * @author  Kasper Skårhøj <kasperYYYY@typo3.com>
00038  */
00039 
00040 
00041 
00042 
00043 
00044 
00045 
00046 /**
00047  * Call custom function from TypoScript for data processing
00048  *
00049  * Example can be found in the testsite package at the page-path "/Intro/TypoScript examples/Custom Dynamic Co.../Passing a string.../"
00050  * This TypoScript configuration will also demonstrate it:
00051  *
00052  * includeLibs.something = media/scripts/example_callfunction.php
00053  * page = PAGE
00054  * page.10 = TEXT
00055  * page.10 {
00056  *     value = Hello World
00057  *     preUserFunc = user_reverseString
00058  *     preUserFunc.uppercase = 1
00059  * }
00060  *
00061  * @param   string      When custom functions are used for data processing the $content variable will hold the value to be processed. When functions are meant to just return some generated content this variable is empty.
00062  * @param   array       TypoScript properties passed on to this function.
00063  * @return  string      The input string reversed. If the TypoScript property "uppercase" was set it will also be in uppercase.
00064  */
00065 function user_reverseString($content,$conf) {
00066     $content = strrev($content);
00067     if ($conf['uppercase']) {
00068         $content=strtoupper($content);
00069     }
00070     return $content;
00071 }
00072 
00073 /**
00074  * Simply outputting the current time in red letters.
00075  *
00076  * Example can be found in the testsite package at the page-path "/Intro/TypoScript examples/Custom Dynamic Co.../Mixing cached and.../"
00077  * This TypoScript configuration will also demonstrate it:
00078  *
00079  * includeLibs.something = media/scripts/example_callfunction.php
00080  * page = PAGE
00081  * page.10 = USER_INT
00082  * page.10 {
00083  *   userFunc = user_printTime
00084  * }
00085  *
00086  * @param   string      Empty string (no content to process)
00087  * @param   array       TypoScript configuration
00088  * @return  string      HTML output, showing the current server time.
00089  */
00090 function user_printTime($content,$conf) {
00091     return '<font color="red">Dynamic time: '.date('H:i:s').'</font><br />';
00092 }
00093 
00094 
00095 
00096 /**
00097  * Example of calling a method in a PHP class from TypoScript
00098  *
00099  */
00100 class user_various  {
00101     var $cObj;      // Reference to the parent (calling) cObj set from TypoScript
00102 
00103     /**
00104      * Doing the same as user_reverseString() but with a class. Also demonstrates how this gives us the ability to use methods in the parent object.
00105      *
00106      * @param   string      String to process (from stdWrap)
00107      * @param   array       TypoScript properties passed on to this method.
00108      * @return  string      The input string reversed. If the TypoScript property "uppercase" was set it will also be in uppercase. May also be linked.
00109      * @see user_reverseString()
00110      */
00111     function reverseString($content,$conf)  {
00112         $content = strrev($content);
00113         if ($conf['uppercase']) {
00114             $content=$this->cObj->caseshift($content,'upper');
00115         }
00116         if ($conf['typolink'])  {
00117             $content=$this->cObj->getTypoLink($content,$conf['typolink']);
00118         }
00119         return $content;
00120     }
00121 
00122     /**
00123      * Testing USER cObject:
00124      *
00125      * Example can be found in the testsite package at the page-path "/Intro/TypoScript examples/Custom Dynamic Co.../Calling a method.../"
00126      * This TypoScript configuration will also demonstrate it:
00127      *
00128      * includeLibs.something = media/scripts/example_callfunction.php
00129      * page = PAGE
00130      * page.30 = USER
00131      * page.30 {
00132      *   userFunc = user_various->listContentRecordsOnPage
00133      *   reverseOrder = 1
00134      * }
00135      *
00136      * @param   string      Empty string (no content to process)
00137      * @param   array       TypoScript configuration
00138      * @return  string      HTML output, showing content elements (in reverse order if configured.)
00139      */
00140     function listContentRecordsOnPage($content,$conf)   {
00141         $query = $GLOBALS['TYPO3_DB']->SELECTquery(
00142                         'header',
00143                         'tt_content',
00144                         'pid='.intval($GLOBALS['TSFE']->id).$this->cObj->enableFields('tt_content'),
00145                         '',
00146                         'sorting'.($conf['reverseOrder'] ? ' DESC' : '')
00147                     );
00148         $output = 'This is the query: <strong>'.$query.'</strong><br /><br />';
00149         return $output.$this->selectThem($query);
00150     }
00151 
00152     /**
00153      * Selecting the records by input $query and returning the header field values
00154      *
00155      * @param   string      SQL query selecting the content elements.
00156      * @return  string      The header field values of the content elements imploded by a <br /> tag
00157      * @access private
00158      */
00159     function selectThem($query) {
00160         $res = $GLOBALS['TYPO3_DB']->sql_query($query);
00161         $output=array();
00162         while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))   {
00163             $output[]=$row['header'];
00164         }
00165         return implode($output,'<br />');
00166     }
00167 }
00168 
00169 ?>