Updated copyright notices to show "2004"
[Packages/TYPO3.CMS.git] / typo3 / sysext / cms / tslib / class.tslib_pibase.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2004 Kasper Skaarhoj (kasper@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 * This script contains the parent class, 'pibase', providing an API with the most basic methods for frontend plugins
29 *
30 * $Id$
31 * Revised for TYPO3 3.6 June/2003 by Kasper Skaarhoj
32 * XHTML compliant
33 *
34 * @author Kasper Skaarhoj <kasper@typo3.com>
35 */
36 /**
37 * [CLASS/FUNCTION INDEX of SCRIPT]
38 *
39 *
40 *
41 * 131: class tslib_pibase
42 *
43 * SECTION: Init functions
44 * 210: function tslib_pibase()
45 * 224: function pi_setPiVarDefaults()
46 *
47 * SECTION: Link functions
48 * 263: function pi_getPageLink($id,$target='',$urlParameters=array())
49 * 279: function pi_linkToPage($str,$id,$target='',$urlParameters=array())
50 * 293: function pi_linkTP($str,$urlParameters=array(),$cache=0)
51 * 315: function pi_linkTP_keepPIvars($str,$overrulePIvars=array(),$cache=0,$clearAnyway=0)
52 * 338: function pi_linkTP_keepPIvars_url($overrulePIvars=array(),$cache=0,$clearAnyway=0)
53 * 355: function pi_list_linkSingle($str,$uid,$cache=FALSE,$mergeArr=array(),$urlOnly=FALSE)
54 * 383: function pi_openAtagHrefInJSwindow($str,$winName='',$winParams='width=670,height=500,status=0,menubar=0,scrollbars=1,resizable=1')
55 *
56 * SECTION: Functions for listing, browsing, searching etc.
57 * 423: function pi_list_browseresults($showResultCount=1,$tableParams='')
58 * 492: function pi_list_searchBox($tableParams='')
59 * 520: function pi_list_modeSelector($items=array(),$tableParams='')
60 * 558: function pi_list_makelist($res,$tableParams='')
61 * 593: function pi_list_row($c)
62 * 605: function pi_list_header()
63 *
64 * SECTION: Stylesheet, CSS
65 * 636: function pi_getClassName($class)
66 * 648: function pi_classParam($class)
67 * 662: function pi_setClassStyle($class,$data,$selector='')
68 * 673: function pi_wrapInBaseClass($str)
69 *
70 * SECTION: Frontend editing: Edit panel, edit icons
71 * 722: function pi_getEditPanel($row='',$tablename='',$label='',$conf=Array())
72 * 763: function pi_getEditIcon($content,$fields,$title='',$row='',$tablename='')
73 *
74 * SECTION: Localization, locallang functions
75 * 810: function pi_getLL($key,$alt='',$hsc=FALSE)
76 * 831: function pi_loadLL()
77 *
78 * SECTION: Database, queries
79 * 893: function pi_list_query($table,$count=0,$addWhere='',$mm_cat='',$groupBy='',$orderBy='',$query='')
80 * 954: function pi_getRecord($table,$uid,$checkPage=0)
81 * 965: function pi_getPidList($pid_list,$recursive=0)
82 * 986: function pi_prependFieldsWithTable($table,$fieldList)
83 * 1003: function pi_getCategoryTableContents($table,$pid,$addWhere='')
84 *
85 * SECTION: Various
86 * 1039: function pi_isOnlyFields($fList,$lowerThan=-1)
87 * 1059: function pi_autoCache($inArray)
88 * 1090: function pi_RTEcssText($str)
89 *
90 * SECTION: FlexForms related functions
91 * 1111: function pi_initPIflexForm()
92 * 1129: function pi_getFFvalue($T3FlexForm_array,$fieldName,$sheet='sDEF',$lang='lDEF',$value='vDEF')
93 * 1146: function pi_getFFvalueFromSheetArray($sheetArray,$fieldNameArr,$value)
94 *
95 * TOTAL FUNCTIONS: 34
96 * (This index is automatically created/updated by the extension "extdeveval")
97 *
98 */
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121 /**
122 * Base class for frontend plugins
123 * Most modern frontend plugins are extension classes of this one.
124 * This class contains functions which assists these plugins in creating lists, searching, displaying menus, page-browsing (next/previous/1/2/3) and handling links.
125 * Functions are all prefixed "pi_" which is reserved for this class. Those functions can of course be overridden in the extension classes (that is the point...)
126 *
127 * @author Kasper Skaarhoj <kasper@typo3.com>
128 * @package TYPO3
129 * @subpackage tslib
130 */
131 class tslib_pibase {
132
133 // Reserved variables:
134 var $cObj; // The backReference to the mother cObj object set at call time
135 var $prefixId; // Should be same as classname of the plugin, used for CSS classes, variables
136 var $scriptRelPath; // Path to the plugin class script relative to extension directory, eg. 'pi1/class.tx_newfaq_pi1.php'
137 var $extKey; // Extension key.
138 var $piVars = Array ( // This is the incomming array by name $this->prefixId merged between POST and GET, POST taking precedence. Eg. if the class name is 'tx_myext' then the content of this array will be whatever comes into &tx_myext[...]=...
139 'pointer' => '', // Used as a pointer for lists
140 'mode' => '', // List mode
141 'sword' => '', // Search word
142 'sort' => '', // [Sorting column]:[ASC=0/DESC=1]
143 );
144 var $internal = Array( // Used internally for general storage of values between methods
145 'res_count' => 0, // Total query count
146 'results_at_a_time' => 20, // pi_list_browseresults(): Show number of results at a time
147 'maxPages' => 10, // pi_list_browseresults(): Max number of 'Page 1 - Page 2 - ...' in the list browser
148 'currentRow' => Array(), // Current result row
149 'currentTable' => '', // Current table
150 );
151
152 var $LOCAL_LANG = Array(); // Local Language content
153 var $LOCAL_LANG_loaded = 0; // Flag that tells if the locallang file has been fetch (or tried to be fetched) already.
154 var $LLkey='default'; // Pointer to the language to use.
155 var $LLtestPrefix=''; // You can set this during development to some value that makes it easy for you to spot all labels that ARe delivered by the getLL function.
156 var $LLtestPrefixAlt=''; // Save as LLtestPrefix, but additional prefix for the alternative value in getLL() function calls
157
158 var $pi_isOnlyFields = 'mode,pointer';
159 var $pi_alwaysPrev = 0;
160 var $pi_lowerThan = 5;
161 var $pi_moreParams='';
162 var $pi_listFields='*';
163
164 var $pi_autoCacheFields=array();
165 var $pi_autoCacheEn=0;
166
167 var $pi_USER_INT_obj = 0; // If set, then links are 1) not using cHash and 2) allowing pages to be cached.
168
169 /**
170 * Should normally be set in the main function with the TypoScript content passed to the method.
171 *
172 * $conf[LOCAL_LANG][_key_] is reserved for Local Language overrides.
173 * $conf[userFunc] / $conf[includeLibs] reserved for setting up the USER / USER_INT object. See TSref
174 */
175 var $conf = Array();
176
177 // internal, don't mess with...
178 var $pi_EPtemp_cObj;
179 var $pi_tmpPageId=0;
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197 /***************************
198 *
199 * Init functions
200 *
201 **************************/
202
203 /**
204 * Class Constructor (true constructor)
205 * Initializes $this->piVars if $this->prefixId is set to any value
206 * Will also set $this->LLkey based on the config.language setting.
207 *
208 * @return void
209 */
210 function tslib_pibase() {
211 if ($this->prefixId) {
212 $this->piVars = t3lib_div::GParrayMerged($this->prefixId);
213 }
214 if ($GLOBALS['TSFE']->config['config']['language']) {
215 $this->LLkey = $GLOBALS['TSFE']->config['config']['language'];
216 }
217 }
218
219 /**
220 * If internal TypoScript property "_DEFAULT_PI_VARS." is set then it will merge the current $this->piVars array onto these default values.
221 *
222 * @return void
223 */
224 function pi_setPiVarDefaults() {
225 if (is_array($this->conf['_DEFAULT_PI_VARS.'])) {
226 $this->piVars = t3lib_div::array_merge_recursive_overrule($this->conf['_DEFAULT_PI_VARS.'],is_array($this->piVars)?$this->piVars:array());
227 }
228 }
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244 /***************************
245 *
246 * Link functions
247 *
248 **************************/
249
250 /**
251 * Get URL to some page.
252 * Returns the URL to page $id with $target and an array of additional url-parameters, $urlParameters
253 * Simple example: $this->pi_getPageLink(123) to get the URL for page-id 123.
254 *
255 * The function basically calls $this->cObj->getTypoLink_URL()
256 *
257 * @param integer Page id
258 * @param string Target value to use. Affects the &type-value of the URL, defaults to current.
259 * @param array Additional URL parameters to set (key/value pairs)
260 * @return string The resulting URL
261 * @see pi_linkToPage()
262 */
263 function pi_getPageLink($id,$target='',$urlParameters=array()) {
264 return $this->cObj->getTypoLink_URL($id,$urlParameters,$target); // ?$target:$GLOBALS['TSFE']->sPre
265 }
266
267 /**
268 * Link a string to some page.
269 * Like pi_getPageLink() but takes a string as first parameter which will in turn be wrapped with the URL including target attribute
270 * Simple example: $this->pi_getPageLink('My link', 123) to get something like <a href="index.php?id=123&type=1">My link</a> (or <a href="123.1.html">My link</a> if simulateStaticDocuments is set)
271 *
272 * @param string The content string to wrap in <a> tags
273 * @param integer Page id
274 * @param string Target value to use. Affects the &type-value of the URL, defaults to current.
275 * @param array Additional URL parameters to set (key/value pairs)
276 * @return string The input string wrapped in <a> tags with the URL and target set.
277 * @see pi_getPageLink(), tslib_cObj::getTypoLink()
278 */
279 function pi_linkToPage($str,$id,$target='',$urlParameters=array()) {
280 return $this->cObj->getTypoLink($str,$id,$urlParameters,$target); // ?$target:$GLOBALS['TSFE']->sPre
281 }
282
283 /**
284 * Link string to the current page.
285 * Returns the $str wrapped in <a>-tags with a link to the CURRENT page, but with $urlParameters set as extra parameters for the page.
286 *
287 * @param string The content string to wrap in <a> tags
288 * @param array Array with URL parameters as key/value pairs. They will be "imploded" and added to the list of parameters defined in the plugins TypoScript property "parent.addParams" plus $this->pi_moreParams.
289 * @param boolean If $cache is set, the page is asked to be cached by a &cHash value (unless the current plugin using this class is a USER_INT). Otherwise the no_cache-parameter will be a part of the link.
290 * @return string The input string wrapped in <a> tags
291 * @see pi_linkTP_keepPIvars(), tslib_cObj::typoLink()
292 */
293 function pi_linkTP($str,$urlParameters=array(),$cache=0) {
294 $conf=array();
295 $conf['useCacheHash']=$this->pi_USER_INT_obj?0:$cache;
296 $conf['no_cache']=$this->pi_USER_INT_obj?0:!$cache;
297 $conf['parameter']=$this->pi_tmpPageId ? $this->pi_tmpPageId : $GLOBALS['TSFE']->id;
298 $conf['additionalParams']=$this->conf['parent.']['addParams'].t3lib_div::implodeArrayForUrl('',$urlParameters,'',1).$this->pi_moreParams;
299
300 return $this->cObj->typoLink($str, $conf);
301 }
302
303 /**
304 * Link a string to the current page while keeping currently set values in piVars.
305 * Like pi_linkTP, but $urlParameters is by default set to $this->piVars with $overrulePIvars overlaid.
306 * This means any current entries from this->piVars are passed on (except the key "DATA" which will be unset before!) and entries in $overrulePIvars will OVERRULE the current in the link.
307 *
308 * @param string The content string to wrap in <a> tags
309 * @param array Array of values to override in the current piVars. Contrary to pi_linkTP the keys in this array must correspond to the real piVars array and therefore NOT be prefixed with the $this->prefixId string. Further, if a value is a blank string it means the piVar key will not be a part of the link (unset)
310 * @param boolean If $cache is set, the page is asked to be cached by a &cHash value (unless the current plugin using this class is a USER_INT). Otherwise the no_cache-parameter will be a part of the link.
311 * @param boolean If set, then the current values of piVars will NOT be preserved anyways... Practical if you want an easy way to set piVars without having to worry about the prefix, "tx_xxxxx[]"
312 * @return string The input string wrapped in <a> tags
313 * @see pi_linkTP()
314 */
315 function pi_linkTP_keepPIvars($str,$overrulePIvars=array(),$cache=0,$clearAnyway=0) {
316 if (is_array($this->piVars) && is_array($overrulePIvars) && !$clearAnyway) {
317 $piVars = $this->piVars;
318 unset($piVars['DATA']);
319 $overrulePIvars = t3lib_div::array_merge_recursive_overrule($piVars,$overrulePIvars);
320 if ($this->pi_autoCacheEn) {
321 $cache = $this->pi_autoCache($overrulePIvars);
322 }
323 }
324 $res = $this->pi_linkTP($str,Array($this->prefixId=>$overrulePIvars),$cache);
325 return $res;
326 }
327
328 /**
329 * Get URL to the current page while keeping currently set values in piVars.
330 * Same as pi_linkTP_keepPIvars but returns only the URL from the link.
331 *
332 * @param array See pi_linkTP_keepPIvars
333 * @param boolean See pi_linkTP_keepPIvars
334 * @param boolean See pi_linkTP_keepPIvars
335 * @return string The URL ($this->cObj->lastTypoLinkUrl)
336 * @see pi_linkTP_keepPIvars()
337 */
338 function pi_linkTP_keepPIvars_url($overrulePIvars=array(),$cache=0,$clearAnyway=0) {
339 $this->pi_linkTP_keepPIvars('|',$overrulePIvars,$cache,$clearAnyway);
340 return $this->cObj->lastTypoLinkUrl;
341 }
342
343 /**
344 * Wraps the $str in a link to a single display of the record (using piVars[showUid])
345 * Uses pi_linkTP for the linking
346 *
347 * @param string The content string to wrap in <a> tags
348 * @param integer UID of the record for which to display details (basically this will become the value of [showUid]
349 * @param boolean See pi_linkTP_keepPIvars
350 * @param array Array of values to override in the current piVars. Same as $overrulePIvars in pi_linkTP_keepPIvars
351 * @param boolean If true, only the URL is returned, not a full link
352 * @return string The input string wrapped in <a> tags
353 * @see pi_linkTP(), pi_linkTP_keepPIvars()
354 */
355 function pi_list_linkSingle($str,$uid,$cache=FALSE,$mergeArr=array(),$urlOnly=FALSE) {
356 if ($this->prefixId) {
357 if ($cache) {
358 $overrulePIvars=$uid?array('showUid'=>$uid):Array();
359 $overrulePIvars=array_merge($overrulePIvars,$mergeArr);
360 $str = $this->pi_linkTP($str,Array($this->prefixId=>$overrulePIvars),$cache);
361 } else {
362 $overrulePIvars=array('showUid'=>$uid?$uid:'');
363 $overrulePIvars=array_merge($overrulePIvars,$mergeArr);
364 $str = $this->pi_linkTP_keepPIvars($str,$overrulePIvars,$cache);
365 }
366
367 // If urlOnly flag, return only URL as it has recently be generated.
368 if ($urlOnly) {
369 $str = $this->cObj->lastTypoLinkUrl;
370 }
371 }
372 return $str;
373 }
374
375 /**
376 * Will change the href value from <a> in the input string and turn it into an onclick event that will open a new window with the URL
377 *
378 * @param string The string to process. This should be a string already wrapped/including a <a> tag which will be modified to contain an onclick handler. Only the attributes "href" and "onclick" will be left.
379 * @param string Window name for the pop-up window
380 * @param string Window parameters, see the default list for inspiration
381 * @return string The processed input string, modified IF a <a> tag was found
382 */
383 function pi_openAtagHrefInJSwindow($str,$winName='',$winParams='width=670,height=500,status=0,menubar=0,scrollbars=1,resizable=1') {
384 if (eregi('(.*)(<a[^>]*>)(.*)',$str,$match)) {
385 $aTagContent = t3lib_div::get_tag_attributes($match[2]);
386 $match[2]='<a href="#" onclick="'.
387 htmlspecialchars('vHWin=window.open(\''.$aTagContent['href'].'\',\''.($winName?$winName:md5($aTagContent['href'])).'\',\''.$winParams.'\');vHWin.focus();return false;').
388 '">';
389 $str=$match[1].$match[2].$match[3];
390 }
391 return $str;
392 }
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408 /***************************
409 *
410 * Functions for listing, browsing, searching etc.
411 *
412 **************************/
413
414 /**
415 * Returns a results browser. This means a bar of page numbers plus a "previous" and "next" link. For each entry in the bar the piVars "pointer" will be pointing to the "result page" to show.
416 * Using $this->piVars['pointer'] as pointer to the page to display
417 * Using $this->internal['res_count'], $this->internal['results_at_a_time'] and $this->internal['maxPages'] for count number, how many results to show and the max number of pages to include in the browse bar.
418 *
419 * @param boolean If set (default) the text "Displaying results..." will be show, otherwise not.
420 * @param string Attributes for the table tag which is wrapped around the table cells containing the browse links
421 * @return string Output HTML, wrapped in <div>-tags with a class attribute
422 */
423 function pi_list_browseresults($showResultCount=1,$tableParams='') {
424
425 // Initializing variables:
426 $pointer=$this->piVars['pointer'];
427 $count=$this->internal['res_count'];
428 $results_at_a_time = t3lib_div::intInRange($this->internal['results_at_a_time'],1,1000);
429 $maxPages = t3lib_div::intInRange($this->internal['maxPages'],1,100);
430 $max = t3lib_div::intInRange(ceil($count/$results_at_a_time),1,$maxPages);
431 $pointer=intval($pointer);
432 $links=array();
433
434 // Make browse-table/links:
435 if ($this->pi_alwaysPrev>=0) {
436 if ($pointer>0) {
437 $links[]='
438 <td nowrap="nowrap"><p>'.$this->pi_linkTP_keepPIvars($this->pi_getLL('pi_list_browseresults_prev','< Previous',TRUE),array('pointer'=>($pointer-1?$pointer-1:'')),0).'</p></td>';
439 } elseif ($this->pi_alwaysPrev) {
440 $links[]='
441 <td nowrap="nowrap"><p>'.$this->pi_getLL('pi_list_browseresults_prev','< Previous',TRUE).'</p></td>';
442 }
443 }
444 for($a=0;$a<$max;$a++) {
445 $links[]='
446 <td'.($pointer==$a?$this->pi_classParam('browsebox-SCell'):'').' nowrap="nowrap"><p>'.
447 $this->pi_linkTP_keepPIvars(trim($this->pi_getLL('pi_list_browseresults_page','Page',TRUE).' '.($a+1)),array('pointer'=>($a?$a:'')),$this->pi_isOnlyFields($this->pi_isOnlyFields)).
448 '</p></td>';
449 }
450 if ($pointer<ceil($count/$results_at_a_time)-1) {
451 $links[]='
452 <td nowrap="nowrap"><p>'.
453 $this->pi_linkTP_keepPIvars($this->pi_getLL('pi_list_browseresults_next','Next >',TRUE),array('pointer'=>$pointer+1)).
454 '</p></td>';
455 }
456
457 $pR1 = $pointer*$results_at_a_time+1;
458 $pR2 = $pointer*$results_at_a_time+$results_at_a_time;
459 $sTables = '
460
461 <!--
462 List browsing box:
463 -->
464 <div'.$this->pi_classParam('browsebox').'>'.
465 ($showResultCount ? '
466 <p>'.sprintf(
467 str_replace('###SPAN_BEGIN###','<span'.$this->pi_classParam('browsebox-strong').'>',$this->pi_getLL('pi_list_browseresults_displays','Displaying results ###SPAN_BEGIN###%s to %s</span> out of ###SPAN_BEGIN###%s</span>')),
468 $pR1,
469 min(array($this->internal['res_count'],$pR2)),
470 $this->internal['res_count']
471 ).'</p>':''
472 ).
473 '
474
475 <'.trim('table '.$tableParams).'>
476 <tr>
477 '.implode('',$links).'
478 </tr>
479 </table>
480 </div>';
481
482 return $sTables;
483 }
484
485 /**
486 * Returns a Search box, sending search words to piVars "sword" and setting the "no_cache" parameter as well in the form.
487 * Submits the search request to the current REQUEST_URI
488 *
489 * @param string Attributes for the table tag which is wrapped around the table cells containing the search box
490 * @return string Output HTML, wrapped in <div>-tags with a class attribute
491 */
492 function pi_list_searchBox($tableParams='') {
493 // Search box design:
494 $sTables = '
495
496 <!--
497 List search box:
498 -->
499 <div'.$this->pi_classParam('searchbox').'>
500 <form action="'.htmlspecialchars(t3lib_div::getIndpEnv('REQUEST_URI')).'" method="post" style="margin: 0 0 0 0;">
501 <'.trim('table '.$tableParams).'>
502 <tr>
503 <td><input type="text" name="'.$this->prefixId.'[sword]" value="'.htmlspecialchars($this->piVars['sword']).'"'.$this->pi_classParam('searchbox-sword').' /></td>
504 <td><input type="submit" value="'.$this->pi_getLL('pi_list_searchBox_search','Search',TRUE).'"'.$this->pi_classParam('searchbox-button').' /><input type="hidden" name="no_cache" value="1" /></td>
505 </tr>
506 </table>
507 </form>
508 </div>';
509
510 return $sTables;
511 }
512
513 /**
514 * Returns a mode selector; a little menu in a table normally put in the top of the page/list.
515 *
516 * @param array Key/Value pairs for the menu; keys are the piVars[mode] values and the "values" are the labels for them.
517 * @param string Attributes for the table tag which is wrapped around the table cells containing the menu
518 * @return string Output HTML, wrapped in <div>-tags with a class attribute
519 */
520 function pi_list_modeSelector($items=array(),$tableParams='') {
521 $cells=array();
522 reset($items);
523 while(list($k,$v)=each($items)) {
524 $cells[]='
525 <td'.($this->piVars['mode']==$k?$this->pi_classParam('modeSelector-SCell'):'').'><p>'.
526 $this->pi_linkTP_keepPIvars(htmlspecialchars($v),array('mode'=>$k),$this->pi_isOnlyFields($this->pi_isOnlyFields)).
527 '</p></td>';
528 }
529
530 $sTables = '
531
532 <!--
533 Mode selector (menu for list):
534 -->
535 <div'.$this->pi_classParam('modeSelector').'>
536 <'.trim('table '.$tableParams).'>
537 <tr>
538 '.implode('',$cells).'
539 </tr>
540 </table>
541 </div>';
542
543 return $sTables;
544 }
545
546 /**
547 * Returns the list of items based on the input MySQL result pointer
548 * For each result row the internal var, $this->internal['currentRow'], is set with the row returned.
549 * $this->pi_list_header() makes the header row for the list
550 * $this->pi_list_row() is used for rendering each row
551 * Notice that these two functions are typically ALWAYS defined in the extension class of the plugin since they are directly concerned with the specific layout for that plugins purpose.
552 *
553 * @param pointer Result pointer to a MySQL result which can be traversed.
554 * @param string Attributes for the table tag which is wrapped around the table rows containing the list
555 * @return string Output HTML, wrapped in <div>-tags with a class attribute
556 * @see pi_list_row(), pi_list_header()
557 */
558 function pi_list_makelist($res,$tableParams='') {
559 // Make list table header:
560 $tRows=array();
561 $this->internal['currentRow']='';
562 $tRows[]=$this->pi_list_header();
563
564 // Make list table rows
565 $c=0;
566 while($this->internal['currentRow'] = mysql_fetch_assoc($res)) {
567 $tRows[]=$this->pi_list_row($c);
568 $c++;
569 }
570
571 $out = '
572
573 <!--
574 Record list:
575 -->
576 <div'.$this->pi_classParam('listrow').'>
577 <'.trim('table '.$tableParams).'>
578 '.implode('',$tRows).'
579 </table>
580 </div>';
581
582 return $out;
583 }
584
585 /**
586 * Returns a list row. Get data from $this->internal['currentRow'];
587 * (Dummy)
588 * Notice: This function should ALWAYS be defined in the extension class of the plugin since it is directly concerned with the specific layout of the listing for your plugins purpose.
589 *
590 * @param integer Row counting. Starts at 0 (zero). Used for alternating class values in the output rows.
591 * @return string HTML output, a table row with a class attribute set (alternative based on odd/even rows)
592 */
593 function pi_list_row($c) {
594 // Dummy
595 return '<tr'.($c%2 ? $this->pi_classParam('listrow-odd') : '').'><td><p>[dummy row]</p></td></tr>';
596 }
597
598 /**
599 * Returns a list header row.
600 * (Dummy)
601 * Notice: This function should ALWAYS be defined in the extension class of the plugin since it is directly concerned with the specific layout of the listing for your plugins purpose.
602 *
603 * @return string HTML output, a table row with a class attribute set
604 */
605 function pi_list_header() {
606 return '<tr'.$this->pi_classParam('listrow-header').'><td><p>[dummy header row]</p></td></tr>';
607 }
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623 /***************************
624 *
625 * Stylesheet, CSS
626 *
627 **************************/
628
629
630 /**
631 * Returns a class-name prefixed with $this->prefixId and with all underscores substituted to dashes (-)
632 *
633 * @param string The class name (or the END of it since it will be prefixed by $this->prefixId.'-')
634 * @return string The combined class name (with the correct prefix)
635 */
636 function pi_getClassName($class) {
637 return str_replace('_','-',$this->prefixId).($this->prefixId?'-':'').$class;
638 }
639
640 /**
641 * Returns the class-attribute with the correctly prefixed classname
642 * Using pi_getClassName()
643 *
644 * @param string The class name (suffix)
645 * @return string A "class" attribute with value and a single space char before it.
646 * @see pi_getClassName()
647 */
648 function pi_classParam($class) {
649 return ' class="'.$this->pi_getClassName($class).'"';
650 }
651
652 /**
653 * Sets CSS style-data for the $class-suffix (prefixed by pi_getClassName())
654 *
655 * @param string $class: Class suffix, see pi_getClassName
656 * @param string $data: CSS data
657 * @param string If $selector is set to any CSS selector, eg 'P' or 'H1' or 'TABLE' then the style $data will regard those HTML-elements only
658 * @return void
659 * @depreciated I think this function should not be used (and probably isn't used anywhere). It was a part of a concept which was left behind quite quickly.
660 * @private
661 */
662 function pi_setClassStyle($class,$data,$selector='') {
663 $GLOBALS['TSFE']->setCSS($this->pi_getClassName($class).($selector?' '.$selector:''),'.'.$this->pi_getClassName($class).($selector?' '.$selector:'').' {'.$data.'}');
664 }
665
666 /**
667 * Wraps the input string in a <div> tag with the class attribute set to the prefixId.
668 * All content returned from your plugins should be returned through this function so all content from your plugin is encapsulated in a <div>-tag nicely identifying the content of your plugin.
669 *
670 * @param string HTML content to wrap in the div-tags with the "main class" of the plugin
671 * @return string HTML content wrapped, ready to return to the parent object.
672 */
673 function pi_wrapInBaseClass($str) {
674 return '
675
676
677 <!--
678
679 BEGIN: Content of extension "'.$this->extKey.'", plugin "'.$this->prefixId.'"
680
681 -->
682 <div class="'.str_replace('_','-',$this->prefixId).'">
683 '.$str.'
684 </div>
685 <!-- END: Content of extension "'.$this->extKey.'", plugin "'.$this->prefixId.'" -->
686
687 ';
688 }
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706 /***************************
707 *
708 * Frontend editing: Edit panel, edit icons
709 *
710 **************************/
711
712 /**
713 * Returns the Backend User edit panel for the $row from $tablename
714 *
715 * @param array Record array.
716 * @param string Table name
717 * @param string A label to show with the panel.
718 * @param array TypoScript parameters to pass along to the EDITPANEL content Object that gets rendered. The property "allow" WILL get overridden/set though.
719 * @return string Returns false/blank if no BE User login and of course if the panel is not shown for other reasons. Otherwise the HTML for the panel (a table).
720 * @see tslib_cObj::EDITPANEL()
721 */
722 function pi_getEditPanel($row='',$tablename='',$label='',$conf=Array()) {
723 $panel='';
724 if (!$row || !$tablename) {
725 $row = $this->internal['currentRow'];
726 $tablename = $this->internal['currentTable'];
727 }
728
729 if ($GLOBALS['TSFE']->beUserLogin) {
730 // Create local cObj if not set:
731 if (!is_object($this->pi_EPtemp_cObj)) {
732 $this->pi_EPtemp_cObj = t3lib_div::makeInstance('tslib_cObj');
733 $this->pi_EPtemp_cObj->setParent($this->cObj->data,$this->cObj->currentRecord);
734 }
735
736 // Initialize the cObj object with current row
737 $this->pi_EPtemp_cObj->start($row,$tablename);
738
739 // Setting TypoScript values in the $conf array. See documentation in TSref for the EDITPANEL cObject.
740 $conf['allow'] = 'edit,new,delete,move,hide';
741 $panel = $this->pi_EPtemp_cObj->cObjGetSingle('EDITPANEL',$conf,'editpanel');
742 }
743
744 if ($panel) {
745 if ($label) {
746 return '<!-- BEGIN: EDIT PANEL --><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr><td valign="top">'.$label.'</td><td valign="top" align="right">'.$panel.'</td></tr></table><!-- END: EDIT PANEL -->';
747 } else return '<!-- BEGIN: EDIT PANEL -->'.$panel.'<!-- END: EDIT PANEL -->';
748 } else return $label;
749 }
750
751 /**
752 * Adds edit-icons to the input content.
753 * tslib_cObj::editIcons used for rendering
754 *
755 * @param string HTML content to add icons to. The icons will be put right after the last content part in the string (that means before the ending series of HTML tags)
756 * @param string The list of fields to edit when the icon is clicked.
757 * @param string Title for the edit icon.
758 * @param array Table record row
759 * @param string Table name
760 * @return string The processed content
761 * @see tslib_cObj::editIcons()
762 */
763 function pi_getEditIcon($content,$fields,$title='',$row='',$tablename='') {
764 if ($GLOBALS['TSFE']->beUserLogin){
765 if (!$row || !$tablename) {
766 $row = $this->internal['currentRow'];
767 $tablename = $this->internal['currentTable'];
768 }
769 $conf=array(
770 'beforeLastTag'=>1,
771 'iconTitle' => $title
772 );
773 $content=$this->cObj->editIcons($content,$tablename.':'.$fields,$conf,$tablename.':'.$row['uid'],$row,'&viewUrl='.rawurlencode(t3lib_div::getIndpEnv('REQUEST_URI')));
774 }
775 return $content;
776 }
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794 /***************************
795 *
796 * Localization, locallang functions
797 *
798 **************************/
799
800
801 /**
802 * Returns the localized label of the LOCAL_LANG key, $key
803 * Notice that for debugging purposes prefixes for the output values can be set with the internal vars ->LLtestPrefixAlt and ->LLtestPrefix
804 *
805 * @param string The key from the LOCAL_LANG array for which to return the value.
806 * @param string Alternative string to return IF no value is found set for the key, neither for the local language nor the default.
807 * @param boolean If true, the output label is passed through htmlspecialchars()
808 * @return string The value from LOCAL_LANG.
809 */
810 function pi_getLL($key,$alt='',$hsc=FALSE) {
811 if (isset($this->LOCAL_LANG[$this->LLkey][$key])) {
812 $word = $GLOBALS['TSFE']->csConv($this->LOCAL_LANG[$this->LLkey][$key]);
813 } elseif (isset($this->LOCAL_LANG['default'][$key])) {
814 $word = $this->LOCAL_LANG['default'][$key];
815 } else {
816 $word = $this->LLtestPrefixAlt.$alt;
817 }
818
819 $output = $this->LLtestPrefix.$word;
820 if ($hsc) $output = htmlspecialchars($output);
821
822 return $output;
823 }
824
825 /**
826 * Loads local-language values by looking for a "locallang.php" file in the plugin class directory ($this->scriptRelPath) and if found includes it.
827 * Also locallang values set in the TypoScript property "_LOCAL_LANG" are merged onto the values found in the "locallang.php" file.
828 *
829 * @return void
830 */
831 function pi_loadLL() {
832 if (!$this->LOCAL_LANG_loaded && $this->scriptRelPath) {
833 $basePath = t3lib_extMgm::siteRelPath($this->extKey).dirname($this->scriptRelPath).'/locallang.php';
834 if (@is_file($basePath)) {
835 include('./'.$basePath);
836 $this->LOCAL_LANG = $LOCAL_LANG;
837 if (is_array($this->conf['_LOCAL_LANG.'])) {
838 reset($this->conf['_LOCAL_LANG.']);
839 while(list($k,$lA)=each($this->conf['_LOCAL_LANG.'])) {
840 if (is_array($lA)) {
841 $k = substr($k,0,-1);
842 $this->LOCAL_LANG[$k] = t3lib_div::array_merge_recursive_overrule(is_array($this->LOCAL_LANG[$k])?$this->LOCAL_LANG[$k]:array(), $lA);
843 }
844 }
845 }
846 }
847 }
848 $this->LOCAL_LANG_loaded = 1;
849 }
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873 /***************************
874 *
875 * Database, queries
876 *
877 **************************/
878
879 /**
880 * Makes a standard query for listing of records based on standard input vars from the 'browser' ($this->internal['results_at_a_time'] and $this->piVars['pointer']) and 'searchbox' ($this->piVars['sword'] and $this->internal['searchFieldList'])
881 * Set $count to 1 if you wish to get a count(*) query for selecting the number of results.
882 * Notice that the query will use $this->conf['pidList'] and $this->conf['recursive'] to generate a PID list within which to search for records.
883 *
884 * @param string The table name to make the query for.
885 * @param boolean If set, you will get a "count(*)" query back instead of field selecting
886 * @param string Additional WHERE clauses (should be starting with " AND ....")
887 * @param mixed If an array, then it must contain the keys "table", "mmtable" and (optionally) "catUidList" defining a table to make a MM-relation to in the query (based on fields uid_local and uid_foreign). If not array, the query will be a plain query looking up data in only one table.
888 * @param string If set, this is added as a " GROUP BY ...." part of the query.
889 * @param string If set, this is added as a " ORDER BY ...." part of the query. The default is that an ORDER BY clause is made based on $this->internal['orderBy'] and $this->internal['descFlag'] where the orderBy field must be found in $this->internal['orderByList']
890 * @param string If set, this is taken as the first part of the query instead of what is created internally. Basically this should be a query starting with "FROM [table] WHERE ... AND ...". The $addWhere clauses and all the other stuff is still added. Only the tables and PID selecting clauses are bypassed
891 * @return string The query build.
892 */
893 function pi_list_query($table,$count=0,$addWhere='',$mm_cat='',$groupBy='',$orderBy='',$query='') {
894 // Begin Query:
895 if (!$query) {
896 // Fetches the list of PIDs to select from.
897 // TypoScript property .pidList is a comma list of pids. If blank, current page id is used.
898 // TypoScript property .recursive is a int+ which determines how many levels down from the pids in the pid-list subpages should be included in the select.
899 $pidList = $this->pi_getPidList($this->conf['pidList'],$this->conf['recursive']);
900 if (is_array($mm_cat)) {
901 $query='FROM '.$table.','.$mm_cat['table'].','.$mm_cat['mmtable'].chr(10).
902 ' WHERE '.$table.'.uid='.$mm_cat['mmtable'].'.uid_local AND '.$mm_cat['table'].'.uid='.$mm_cat['mmtable'].'.uid_foreign '.chr(10).
903 (strcmp($mm_cat['catUidList'],'')?' AND '.$mm_cat['table'].'.uid IN ('.$mm_cat['catUidList'].')':'').chr(10).
904 ' AND '.$table.'.pid IN ('.$pidList.')'.chr(10).
905 $this->cObj->enableFields($table).chr(10); // This adds WHERE-clauses that ensures deleted, hidden, starttime/endtime/access records are NOT selected, if they should not! Almost ALWAYS add this to your queries!
906 } else {
907 $query='FROM '.$table.' WHERE pid IN ('.$pidList.')'.chr(10).
908 $this->cObj->enableFields($table).chr(10); // This adds WHERE-clauses that ensures deleted, hidden, starttime/endtime/access records are NOT selected, if they should not! Almost ALWAYS add this to your queries!
909 }
910 }
911
912 // Add '$addWhere'
913 if ($addWhere) {$query.=' '.$addWhere.chr(10);}
914
915 // Search word:
916 if ($this->piVars['sword'] && $this->internal['searchFieldList']) {
917 $query.=$this->cObj->searchWhere($this->piVars['sword'],$this->internal['searchFieldList'],$table).chr(10);
918 }
919
920 if ($count) {
921 $query = 'SELECT count(*) '.chr(10).$query;
922 } else {
923 if ($groupBy) $query.=' '.$groupBy;
924 // Order by data:
925 if ($orderBy) {
926 $query.=' '.$orderBy;
927 } else {
928 if (t3lib_div::inList($this->internal['orderByList'],$this->internal['orderBy'])) {
929 $query.= ' ORDER BY '.$table.'.'.$this->internal['orderBy'].($this->internal['descFlag']?' DESC':'').chr(10);
930 }
931 }
932
933 // Limit data:
934 $pointer=$this->piVars['pointer'];
935 $pointer=intval($pointer);
936 $results_at_a_time = t3lib_div::intInRange($this->internal['results_at_a_time'],1,1000);
937 $query.= ' LIMIT '.($pointer*$results_at_a_time).','.$results_at_a_time.chr(10);
938
939 // Add 'SELECT'
940 $query = 'SELECT '.$this->pi_prependFieldsWithTable($table,$this->pi_listFields).' '.chr(10).$query;
941 }
942 return $query;
943 }
944
945 /**
946 * Returns the row $uid from $table
947 * (Simply calling $GLOBALS['TSFE']->sys_page->checkRecord())
948 *
949 * @param string The table name
950 * @param integer The uid of the record from the table
951 * @param boolean If $checkPage is set, it's required that the page on which the record resides is accessible
952 * @return array If record is found, an array. Otherwise false.
953 */
954 function pi_getRecord($table,$uid,$checkPage=0) {
955 return $GLOBALS['TSFE']->sys_page->checkRecord($table,$uid,$checkPage);
956 }
957
958 /**
959 * Returns a commalist of page ids for a query (eg. 'WHERE pid IN (...)')
960 *
961 * @param string $pid_list is a comma list of page ids (if empty current page is used)
962 * @param integer $recursive is an integer >=0 telling how deep to dig for pids under each entry in $pid_list
963 * @return string List of PID values (comma separated)
964 */
965 function pi_getPidList($pid_list,$recursive=0) {
966 if (!strcmp($pid_list,'')) $pid_list = $GLOBALS['TSFE']->id;
967 $recursive = t3lib_div::intInRange($recursive,0);
968
969 $pid_list_arr = array_unique(t3lib_div::trimExplode(',',$pid_list,1));
970 $pid_list='';
971 reset($pid_list_arr);
972 while(list(,$val)=each($pid_list_arr)) {
973 $val = t3lib_div::intInRange($val,0);
974 if ($val) $pid_list.=$val.','.$this->cObj->getTreeList($val,$recursive);
975 }
976 return ereg_replace(',$','',$pid_list);
977 }
978
979 /**
980 * Having a comma list of fields ($fieldList) this is prepended with the $table.'.' name
981 *
982 * @param string Table name to prepend
983 * @param string List of fields where each element will be prepended with the table name given.
984 * @return string List of fields processed.
985 */
986 function pi_prependFieldsWithTable($table,$fieldList) {
987 $list=t3lib_div::trimExplode(',',$fieldList,1);
988 $return=array();
989 while(list(,$listItem)=each($list)) {
990 $return[]=$table.'.'.$listItem;
991 }
992 return implode(',',$return);
993 }
994
995 /**
996 * Will select all records from the "category table", $table, and return them in an array.
997 *
998 * @param string The name of the category table to select from.
999 * @param integer The page from where to select the category records.
1000 * @param string Additional where clauses (basically the end of the query - could include a ORDER BY clause)
1001 * @return array The array with the category records in.
1002 */
1003 function pi_getCategoryTableContents($table,$pid,$addWhere='') {
1004 $query = 'SELECT * FROM '.$table.' WHERE pid='.intval($pid).$this->cObj->enableFields($table).' '.$addWhere;
1005
1006 $outArr = array();
1007 $res = mysql(TYPO3_db,$query);
1008 while($row=mysql_fetch_assoc($res)) {
1009 $outArr[$row['uid']]=$row;
1010 }
1011 return $outArr;
1012 }
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025 /***************************
1026 *
1027 * Various
1028 *
1029 **************************/
1030
1031 /**
1032 * Returns true if the piVars array has ONLY those fields entered that is set in the $fList (commalist) AND if none of those fields value is greater than $lowerThan field if they are integers.
1033 * Notice that this function will only work as long as values are integers.
1034 *
1035 * @param string List of fields (keys from piVars) to evaluate on
1036 * @param integer Limit for the values.
1037 * @return boolean Returns true (1) if conditions are met.
1038 */
1039 function pi_isOnlyFields($fList,$lowerThan=-1) {
1040 $lowerThan = $lowerThan==-1 ? $this->pi_lowerThan : $lowerThan;
1041
1042 $fList = t3lib_div::trimExplode(',',$fList,1);
1043 $tempPiVars = $this->piVars;
1044 while(list(,$k)=each($fList)) {
1045 if (!t3lib_div::testInt($tempPiVars[$k]) || $tempPiVars[$k]<$lowerThan) unset($tempPiVars[$k]);
1046 }
1047 if (!count($tempPiVars)) return 1;
1048 }
1049
1050 /**
1051 * Returns true if the array $inArray contains only values allowed to be cached based on the configuration in $this->pi_autoCacheFields
1052 * Used by ->pi_linkTP_keepPIvars
1053 * This is an advanced form of evaluation of whether a URL should be cached or not.
1054 *
1055 * @param array An array with piVars values to evaluate
1056 * @return boolean Returns true (1) if conditions are met.
1057 * @see pi_linkTP_keepPIvars()
1058 */
1059 function pi_autoCache($inArray) {
1060 if (is_array($inArray)) {
1061 reset($inArray);
1062 while(list($fN,$fV)=each($inArray)) {
1063 if (!strcmp($inArray[$fN],'')) {
1064 unset($inArray[$fN]);
1065 } elseif (is_array($this->pi_autoCacheFields[$fN])) {
1066 if (is_array($this->pi_autoCacheFields[$fN]['range'])
1067 && intval($inArray[$fN])>=intval($this->pi_autoCacheFields[$fN]['range'][0])
1068 && intval($inArray[$fN])<=intval($this->pi_autoCacheFields[$fN]['range'][1])) {
1069 unset($inArray[$fN]);
1070 }
1071 if (is_array($this->pi_autoCacheFields[$fN]['list'])
1072 && in_array($inArray[$fN],$this->pi_autoCacheFields[$fN]['list'])) {
1073 unset($inArray[$fN]);
1074 }
1075 }
1076 }
1077 }
1078 if (!count($inArray)) return 1;
1079 }
1080
1081 /**
1082 * Will process the input string with the parseFunc function from tslib_cObj based on configuration set in "lib.parseFunc_RTE" in the current TypoScript template.
1083 * This is useful for rendering of content in RTE fields where the transformation mode is set to "ts_css" or so.
1084 * Notice that this requires the use of "css_styled_content" to work right.
1085 *
1086 * @param string The input text string to process
1087 * @return string The processed string
1088 * @see tslib_cObj::parseFunc()
1089 */
1090 function pi_RTEcssText($str) {
1091 $parseFunc = $GLOBALS['TSFE']->tmpl->setup['lib.']['parseFunc_RTE.'];
1092 if (is_array($parseFunc)) $str = $this->cObj->parseFunc($str, $parseFunc);
1093 return $str;
1094 }
1095
1096
1097
1098
1099
1100 /*******************************
1101 *
1102 * FlexForms related functions
1103 *
1104 *******************************/
1105
1106 /**
1107 * Converts $this->cObj->data['pi_flexform'] from XML string to flexForm array.
1108 *
1109 * @return void
1110 */
1111 function pi_initPIflexForm() {
1112 // Converting flexform data into array:
1113 if (!is_array($this->cObj->data['pi_flexform']) && $this->cObj->data['pi_flexform']) {
1114 $this->cObj->data['pi_flexform'] = t3lib_div::xml2array($this->cObj->data['pi_flexform']);
1115 if (!is_array($this->cObj->data['pi_flexform'])) $this->cObj->data['pi_flexform']=array();
1116 }
1117 }
1118
1119 /**
1120 * Return value from somewhere inside a FlexForm structure
1121 *
1122 * @param array FlexForm data
1123 * @param string Field name to extract. Can be given like "test/el/2/test/el/field_templateObject" where each part will dig a level deeper in the FlexForm data.
1124 * @param string Sheet pointer, eg. "sDEF"
1125 * @param string Language pointer, eg. "lDEF"
1126 * @param string Value pointer, eg. "vDEF"
1127 * @return string The content.
1128 */
1129 function pi_getFFvalue($T3FlexForm_array,$fieldName,$sheet='sDEF',$lang='lDEF',$value='vDEF') {
1130 $sheetArray = $T3FlexForm_array['data'][$sheet][$lang];
1131 if (is_array($sheetArray)) {
1132 return $this->pi_getFFvalueFromSheetArray($sheetArray,explode('/',$fieldName),$value);
1133 }
1134 }
1135
1136 /**
1137 * Returns part of $sheetArray pointed to by the keys in $fieldNameArray
1138 *
1139 * @param array Multidimensiona array, typically FlexForm contents
1140 * @param array Array where each value points to a key in the FlexForms content - the input array will have the value returned pointed to by these keys. All integer keys will not take their integer counterparts, but rather traverse the current position in the array an return element number X (whether this is right behavior is not settled yet...)
1141 * @param string Value for outermost key, typ. "vDEF" depending on language.
1142 * @return mixed The value, typ. string.
1143 * @access private
1144 * @see pi_getFFvalue()
1145 */
1146 function pi_getFFvalueFromSheetArray($sheetArray,$fieldNameArr,$value) {
1147
1148 $tempArr=$sheetArray;
1149 foreach($fieldNameArr as $k => $v) {
1150 if (t3lib_div::testInt($v)) {
1151 if (is_array($tempArr)) {
1152 $c=0;
1153 foreach($tempArr as $values) {
1154 if ($c==$v) {
1155 debug($values);
1156 $tempArr=$values;
1157 break;
1158 }
1159 $c++;
1160 }
1161 }
1162 } else {
1163 $tempArr = $tempArr[$v];
1164 }
1165 }
1166 return $tempArr[$value];
1167 }
1168 }
1169
1170 // NO extension of class - does not make sense here.
1171 ?>