b64df404c83273c42ca323352d485b091d7fd977
[Packages/TYPO3.CMS.git] / typo3 / sysext / statictemplates / media / scripts / example_callfunction.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2011 Kasper Skårhøj (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
31 * directly from TYPO3.
32 * Used in the "testsite" package
33 *
34 * Revised for TYPO3 3.6 June/2003 by Kasper Skårhøj
35 * XHTML compliant
36 *
37 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
38 */
39 /**
40 * Call custom function from TypoScript for data processing
41 *
42 * Example can be found in the testsite package at the page-path
43 * "/Intro/TypoScript examples/Custom Dynamic Co.../Passing a string.../"
44 * This TypoScript configuration will also demonstrate it:
45 *
46 * includeLibs.something = media/scripts/example_callfunction.php
47 * page = PAGE
48 * page.10 = TEXT
49 * page.10 {
50 * value = Hello World
51 * preUserFunc = user_reverseString
52 * preUserFunc.uppercase = 1
53 * }
54 *
55 * @param string $content 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.
56 * @param array $conf TypoScript properties passed on to this function.
57 * @return string The input string reversed. If the TypoScript property "uppercase" was set it will also be in uppercase.
58 */
59 function user_reverseString($content, $conf)
60 {
61 $content = strrev($content);
62 if ($conf['uppercase']) {
63 $content = strtoupper($content);
64 }
65 return $content;
66 }
67 /**
68 * Simply outputting the current time in red letters.
69 *
70 * Example can be found in the testsite package at the page-path "/Intro/TypoScript examples/Custom Dynamic Co.../Mixing cached and.../"
71 * This TypoScript configuration will also demonstrate it:
72 *
73 * includeLibs.something = media/scripts/example_callfunction.php
74 * page = PAGE
75 * page.10 = USER_INT
76 * page.10 {
77 * userFunc = user_printTime
78 * }
79 *
80 * @param string $content Empty string (no content to process)
81 * @param array $conf TypoScript configuration
82 * @return string HTML output, showing the current server time.
83 */
84 function user_printTime($content, $conf)
85 {
86 return '<font color="red">Dynamic time: ' . date('H:i:s') . '</font><br />';
87 }
88 /**
89 * Example of calling a method in a PHP class from TypoScript
90 */
91 class user_various {
92
93 // Reference to the parent (calling) cObj set from TypoScript
94 /**
95 * @todo Define visibility
96 */
97 public $cObj;
98
99 /**
100 * 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.
101 *
102 * @param string $content String to process (from stdWrap)
103 * @param array $conf TypoScript properties passed on to this method.
104 * @return string The input string reversed. If the TypoScript property "uppercase" was set it will also be in uppercase. May also be linked.
105 * @see user_reverseString()
106 * @todo Define visibility
107 */
108 public function reverseString($content, $conf) {
109 $content = strrev($content);
110 if ($conf['uppercase']) {
111 $content = $this->cObj->caseshift($content, 'upper');
112 }
113 if ($conf['typolink']) {
114 $content = $this->cObj->getTypoLink($content, $conf['typolink']);
115 }
116 return $content;
117 }
118
119 /**
120 * Testing USER cObject:
121 *
122 * Example can be found in the testsite package at the page-path "/Intro/TypoScript examples/Custom Dynamic Co.../Calling a method.../"
123 * This TypoScript configuration will also demonstrate it:
124 *
125 * includeLibs.something = media/scripts/example_callfunction.php
126 * page = PAGE
127 * page.30 = USER
128 * page.30 {
129 * userFunc = user_various->listContentRecordsOnPage
130 * reverseOrder = 1
131 * }
132 *
133 * @param string $content Empty string (no content to process)
134 * @param array $conf TypoScript configuration
135 * @return string HTML output, showing content elements (in reverse order if configured.)
136 * @todo Define visibility
137 */
138 public function listContentRecordsOnPage($content, $conf) {
139 $query = $GLOBALS['TYPO3_DB']->SELECTquery('header', 'tt_content', 'pid=' . intval($GLOBALS['TSFE']->id) . $this->cObj->enableFields('tt_content'), '', 'sorting' . ($conf['reverseOrder'] ? ' DESC' : ''));
140 $output = 'This is the query: <strong>' . $query . '</strong><br /><br />';
141 return $output . $this->selectThem($query);
142 }
143
144 /**
145 * Selecting the records by input $query and returning the header field values
146 *
147 * @param string $query SQL query selecting the content elements.
148 * @return string The header field values of the content elements imploded by a <br /> tag
149 * @access private
150 * @todo Define visibility
151 */
152 public function selectThem($query) {
153 $res = $GLOBALS['TYPO3_DB']->sql_query($query);
154 $output = array();
155 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
156 $output[] = $row['header'];
157 }
158 return implode($output, '<br />');
159 }
160
161 }
162
163 ?>