* Merging all changes from TYPO3_4-0 branch back into HEAD
[Packages/TYPO3.CMS.git] / typo3 / sysext / cms / tslib / media / scripts / example_callfunction.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2005 Kasper Skaarhoj (kasperYYYY@typo3.com)
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27 /**
28 * USER cObject EXAMPLE FILE
29 *
30 * This is an example of how to use your own functions and classes directly from TYPO3.
31 * Used in the "testsite" package
32 *
33 * $Id$
34 * Revised for TYPO3 3.6 June/2003 by Kasper Skaarhoj
35 * XHTML compliant
36 *
37 * @author Kasper Skaarhoj <kasperYYYY@typo3.com>
38 */
39
40
41
42
43
44
45
46 /**
47 * Call custom function from TypoScript for data processing
48 *
49 * Example can be found in the testsite package at the page-path "/Intro/TypoScript examples/Custom Dynamic Co.../Passing a string.../"
50 * This TypoScript configuration will also demonstrate it:
51 *
52 * includeLibs.something = media/scripts/example_callfunction.php
53 * page = PAGE
54 * page.10 = TEXT
55 * page.10 {
56 * value = Hello World
57 * preUserFunc = user_reverseString
58 * preUserFunc.uppercase = 1
59 * }
60 *
61 * @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.
62 * @param array TypoScript properties passed on to this function.
63 * @return string The input string reversed. If the TypoScript property "uppercase" was set it will also be in uppercase.
64 */
65 function user_reverseString($content,$conf) {
66 $content = strrev($content);
67 if ($conf['uppercase']) {
68 $content=strtoupper($content);
69 }
70 return $content;
71 }
72
73 /**
74 * Simply outputting the current time in red letters.
75 *
76 * Example can be found in the testsite package at the page-path "/Intro/TypoScript examples/Custom Dynamic Co.../Mixing cached and.../"
77 * This TypoScript configuration will also demonstrate it:
78 *
79 * includeLibs.something = media/scripts/example_callfunction.php
80 * page = PAGE
81 * page.10 = USER_INT
82 * page.10 {
83 * userFunc = user_printTime
84 * }
85 *
86 * @param string Empty string (no content to process)
87 * @param array TypoScript configuration
88 * @return string HTML output, showing the current server time.
89 */
90 function user_printTime($content,$conf) {
91 return '<font color="red">Dynamic time: '.date('H:i:s').'</font><br />';
92 }
93
94
95
96 /**
97 * Example of calling a method in a PHP class from TypoScript
98 *
99 */
100 class user_various {
101 var $cObj; // Reference to the parent (calling) cObj set from TypoScript
102
103 /**
104 * 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.
105 *
106 * @param string String to process (from stdWrap)
107 * @param array TypoScript properties passed on to this method.
108 * @return string The input string reversed. If the TypoScript property "uppercase" was set it will also be in uppercase. May also be linked.
109 * @see user_reverseString()
110 */
111 function reverseString($content,$conf) {
112 $content = strrev($content);
113 if ($conf['uppercase']) {
114 $content=$this->cObj->caseshift($content,'upper');
115 }
116 if ($conf['typolink']) {
117 $content=$this->cObj->getTypoLink($content,$conf['typolink']);
118 }
119 return $content;
120 }
121
122 /**
123 * Testing USER cObject:
124 *
125 * Example can be found in the testsite package at the page-path "/Intro/TypoScript examples/Custom Dynamic Co.../Calling a method.../"
126 * This TypoScript configuration will also demonstrate it:
127 *
128 * includeLibs.something = media/scripts/example_callfunction.php
129 * page = PAGE
130 * page.30 = USER
131 * page.30 {
132 * userFunc = user_various->listContentRecordsOnPage
133 * reverseOrder = 1
134 * }
135 *
136 * @param string Empty string (no content to process)
137 * @param array TypoScript configuration
138 * @return string HTML output, showing content elements (in reverse order if configured.)
139 */
140 function listContentRecordsOnPage($content,$conf) {
141 $query = $GLOBALS['TYPO3_DB']->SELECTquery(
142 'header',
143 'tt_content',
144 'pid='.intval($GLOBALS['TSFE']->id).$this->cObj->enableFields('tt_content'),
145 '',
146 'sorting'.($conf['reverseOrder'] ? ' DESC' : '')
147 );
148 $output = 'This is the query: <strong>'.$query.'</strong><br /><br />';
149 return $output.$this->selectThem($query);
150 }
151
152 /**
153 * Selecting the records by input $query and returning the header field values
154 *
155 * @param string SQL query selecting the content elements.
156 * @return string The header field values of the content elements imploded by a <br /> tag
157 * @access private
158 */
159 function selectThem($query) {
160 $res = $GLOBALS['TYPO3_DB']->sql(TYPO3_db,$query);
161 $output=array();
162 while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
163 $output[]=$row['header'];
164 }
165 return implode($output,'<br />');
166 }
167 }
168
169 ?>