Changed lots of stuff...
[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 (0/1), 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 * @param integer Alternative page ID for the link. (By default this function links to the SAME page!)
291 * @return string The input string wrapped in <a> tags
292 * @see pi_linkTP_keepPIvars(), tslib_cObj::typoLink()
293 */
294 function pi_linkTP($str,$urlParameters=array(),$cache=0,$altPageId=0) {
295 $conf=array();
296 $conf['useCacheHash'] = $this->pi_USER_INT_obj ? 0 : $cache;
297 $conf['no_cache'] = $this->pi_USER_INT_obj ? 0 : !$cache;
298 $conf['parameter'] = $altPageId ? $altPageId : ($this->pi_tmpPageId ? $this->pi_tmpPageId : $GLOBALS['TSFE']->id);
299 $conf['additionalParams'] = $this->conf['parent.']['addParams'].t3lib_div::implodeArrayForUrl('',$urlParameters,'',1).$this->pi_moreParams;
300
301 return $this->cObj->typoLink($str, $conf);
302 }
303
304 /**
305 * Link a string to the current page while keeping currently set values in piVars.
306 * Like pi_linkTP, but $urlParameters is by default set to $this->piVars with $overrulePIvars overlaid.
307 * 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.
308 *
309 * @param string The content string to wrap in <a> tags
310 * @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)
311 * @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.
312 * @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[]"
313 * @param integer Alternative page ID for the link. (By default this function links to the SAME page!)
314 * @return string The input string wrapped in <a> tags
315 * @see pi_linkTP()
316 */
317 function pi_linkTP_keepPIvars($str,$overrulePIvars=array(),$cache=0,$clearAnyway=0,$altPageId=0) {
318 if (is_array($this->piVars) && is_array($overrulePIvars) && !$clearAnyway) {
319 $piVars = $this->piVars;
320 unset($piVars['DATA']);
321 $overrulePIvars = t3lib_div::array_merge_recursive_overrule($piVars,$overrulePIvars);
322 if ($this->pi_autoCacheEn) {
323 $cache = $this->pi_autoCache($overrulePIvars);
324 }
325 }
326 $res = $this->pi_linkTP($str,Array($this->prefixId=>$overrulePIvars),$cache,$altPageId);
327 return $res;
328 }
329
330 /**
331 * Get URL to the current page while keeping currently set values in piVars.
332 * Same as pi_linkTP_keepPIvars but returns only the URL from the link.
333 *
334 * @param array See pi_linkTP_keepPIvars
335 * @param boolean See pi_linkTP_keepPIvars
336 * @param boolean See pi_linkTP_keepPIvars
337 * @param integer See pi_linkTP_keepPIvars
338 * @return string The URL ($this->cObj->lastTypoLinkUrl)
339 * @see pi_linkTP_keepPIvars()
340 */
341 function pi_linkTP_keepPIvars_url($overrulePIvars=array(),$cache=0,$clearAnyway=0,$altPageId=0) {
342 $this->pi_linkTP_keepPIvars('|',$overrulePIvars,$cache,$clearAnyway,$altPageId);
343 return $this->cObj->lastTypoLinkUrl;
344 }
345
346 /**
347 * Wraps the $str in a link to a single display of the record (using piVars[showUid])
348 * Uses pi_linkTP for the linking
349 *
350 * @param string The content string to wrap in <a> tags
351 * @param integer UID of the record for which to display details (basically this will become the value of [showUid]
352 * @param boolean See pi_linkTP_keepPIvars
353 * @param array Array of values to override in the current piVars. Same as $overrulePIvars in pi_linkTP_keepPIvars
354 * @param boolean If true, only the URL is returned, not a full link
355 * @param integer Alternative page ID for the link. (By default this function links to the SAME page!)
356 * @return string The input string wrapped in <a> tags
357 * @see pi_linkTP(), pi_linkTP_keepPIvars()
358 */
359 function pi_list_linkSingle($str,$uid,$cache=FALSE,$mergeArr=array(),$urlOnly=FALSE,$altPageId=0) {
360 if ($this->prefixId) {
361 if ($cache) {
362 $overrulePIvars=$uid?array('showUid'=>$uid):Array();
363 $overrulePIvars=array_merge($overrulePIvars,$mergeArr);
364 $str = $this->pi_linkTP($str,Array($this->prefixId=>$overrulePIvars),$cache,$altPageId);
365 } else {
366 $overrulePIvars=array('showUid'=>$uid?$uid:'');
367 $overrulePIvars=array_merge($overrulePIvars,$mergeArr);
368 $str = $this->pi_linkTP_keepPIvars($str,$overrulePIvars,$cache,0,$altPageId);
369 }
370
371 // If urlOnly flag, return only URL as it has recently be generated.
372 if ($urlOnly) {
373 $str = $this->cObj->lastTypoLinkUrl;
374 }
375 }
376 return $str;
377 }
378
379 /**
380 * 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
381 *
382 * @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.
383 * @param string Window name for the pop-up window
384 * @param string Window parameters, see the default list for inspiration
385 * @return string The processed input string, modified IF a <a> tag was found
386 */
387 function pi_openAtagHrefInJSwindow($str,$winName='',$winParams='width=670,height=500,status=0,menubar=0,scrollbars=1,resizable=1') {
388 if (eregi('(.*)(<a[^>]*>)(.*)',$str,$match)) {
389 $aTagContent = t3lib_div::get_tag_attributes($match[2]);
390 $match[2]='<a href="#" onclick="'.
391 htmlspecialchars('vHWin=window.open(\''.$aTagContent['href'].'\',\''.($winName?$winName:md5($aTagContent['href'])).'\',\''.$winParams.'\');vHWin.focus();return false;').
392 '">';
393 $str=$match[1].$match[2].$match[3];
394 }
395 return $str;
396 }
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412 /***************************
413 *
414 * Functions for listing, browsing, searching etc.
415 *
416 **************************/
417
418 /**
419 * 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.
420 * Using $this->piVars['pointer'] as pointer to the page to display
421 * 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.
422 *
423 * @param boolean If set (default) the text "Displaying results..." will be show, otherwise not.
424 * @param string Attributes for the table tag which is wrapped around the table cells containing the browse links
425 * @return string Output HTML, wrapped in <div>-tags with a class attribute
426 */
427 function pi_list_browseresults($showResultCount=1,$tableParams='') {
428
429 // Initializing variables:
430 $pointer=$this->piVars['pointer'];
431 $count=$this->internal['res_count'];
432 $results_at_a_time = t3lib_div::intInRange($this->internal['results_at_a_time'],1,1000);
433 $maxPages = t3lib_div::intInRange($this->internal['maxPages'],1,100);
434 $max = t3lib_div::intInRange(ceil($count/$results_at_a_time),1,$maxPages);
435 $pointer=intval($pointer);
436 $links=array();
437
438 // Make browse-table/links:
439 if ($this->pi_alwaysPrev>=0) {
440 if ($pointer>0) {
441 $links[]='
442 <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>';
443 } elseif ($this->pi_alwaysPrev) {
444 $links[]='
445 <td nowrap="nowrap"><p>'.$this->pi_getLL('pi_list_browseresults_prev','< Previous',TRUE).'</p></td>';
446 }
447 }
448 for($a=0;$a<$max;$a++) {
449 $links[]='
450 <td'.($pointer==$a?$this->pi_classParam('browsebox-SCell'):'').' nowrap="nowrap"><p>'.
451 $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)).
452 '</p></td>';
453 }
454 if ($pointer<ceil($count/$results_at_a_time)-1) {
455 $links[]='
456 <td nowrap="nowrap"><p>'.
457 $this->pi_linkTP_keepPIvars($this->pi_getLL('pi_list_browseresults_next','Next >',TRUE),array('pointer'=>$pointer+1)).
458 '</p></td>';
459 }
460
461 $pR1 = $pointer*$results_at_a_time+1;
462 $pR2 = $pointer*$results_at_a_time+$results_at_a_time;
463 $sTables = '
464
465 <!--
466 List browsing box:
467 -->
468 <div'.$this->pi_classParam('browsebox').'>'.
469 ($showResultCount ? '
470 <p>'.sprintf(
471 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>')),
472 $pR1,
473 min(array($this->internal['res_count'],$pR2)),
474 $this->internal['res_count']
475 ).'</p>':''
476 ).
477 '
478
479 <'.trim('table '.$tableParams).'>
480 <tr>
481 '.implode('',$links).'
482 </tr>
483 </table>
484 </div>';
485
486 return $sTables;
487 }
488
489 /**
490 * Returns a Search box, sending search words to piVars "sword" and setting the "no_cache" parameter as well in the form.
491 * Submits the search request to the current REQUEST_URI
492 *
493 * @param string Attributes for the table tag which is wrapped around the table cells containing the search box
494 * @return string Output HTML, wrapped in <div>-tags with a class attribute
495 */
496 function pi_list_searchBox($tableParams='') {
497 // Search box design:
498 $sTables = '
499
500 <!--
501 List search box:
502 -->
503 <div'.$this->pi_classParam('searchbox').'>
504 <form action="'.htmlspecialchars(t3lib_div::getIndpEnv('REQUEST_URI')).'" method="post" style="margin: 0 0 0 0;">
505 <'.trim('table '.$tableParams).'>
506 <tr>
507 <td><input type="text" name="'.$this->prefixId.'[sword]" value="'.htmlspecialchars($this->piVars['sword']).'"'.$this->pi_classParam('searchbox-sword').' /></td>
508 <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>
509 </tr>
510 </table>
511 </form>
512 </div>';
513
514 return $sTables;
515 }
516
517 /**
518 * Returns a mode selector; a little menu in a table normally put in the top of the page/list.
519 *
520 * @param array Key/Value pairs for the menu; keys are the piVars[mode] values and the "values" are the labels for them.
521 * @param string Attributes for the table tag which is wrapped around the table cells containing the menu
522 * @return string Output HTML, wrapped in <div>-tags with a class attribute
523 */
524 function pi_list_modeSelector($items=array(),$tableParams='') {
525 $cells=array();
526 reset($items);
527 while(list($k,$v)=each($items)) {
528 $cells[]='
529 <td'.($this->piVars['mode']==$k?$this->pi_classParam('modeSelector-SCell'):'').'><p>'.
530 $this->pi_linkTP_keepPIvars(htmlspecialchars($v),array('mode'=>$k),$this->pi_isOnlyFields($this->pi_isOnlyFields)).
531 '</p></td>';
532 }
533
534 $sTables = '
535
536 <!--
537 Mode selector (menu for list):
538 -->
539 <div'.$this->pi_classParam('modeSelector').'>
540 <'.trim('table '.$tableParams).'>
541 <tr>
542 '.implode('',$cells).'
543 </tr>
544 </table>
545 </div>';
546
547 return $sTables;
548 }
549
550 /**
551 * Returns the list of items based on the input MySQL result pointer
552 * For each result row the internal var, $this->internal['currentRow'], is set with the row returned.
553 * $this->pi_list_header() makes the header row for the list
554 * $this->pi_list_row() is used for rendering each row
555 * 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.
556 *
557 * @param pointer Result pointer to a MySQL result which can be traversed.
558 * @param string Attributes for the table tag which is wrapped around the table rows containing the list
559 * @return string Output HTML, wrapped in <div>-tags with a class attribute
560 * @see pi_list_row(), pi_list_header()
561 */
562 function pi_list_makelist($res,$tableParams='') {
563 // Make list table header:
564 $tRows=array();
565 $this->internal['currentRow']='';
566 $tRows[]=$this->pi_list_header();
567
568 // Make list table rows
569 $c=0;
570 while($this->internal['currentRow'] = mysql_fetch_assoc($res)) {
571 $tRows[]=$this->pi_list_row($c);
572 $c++;
573 }
574
575 $out = '
576
577 <!--
578 Record list:
579 -->
580 <div'.$this->pi_classParam('listrow').'>
581 <'.trim('table '.$tableParams).'>
582 '.implode('',$tRows).'
583 </table>
584 </div>';
585
586 return $out;
587 }
588
589 /**
590 * Returns a list row. Get data from $this->internal['currentRow'];
591 * (Dummy)
592 * 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.
593 *
594 * @param integer Row counting. Starts at 0 (zero). Used for alternating class values in the output rows.
595 * @return string HTML output, a table row with a class attribute set (alternative based on odd/even rows)
596 */
597 function pi_list_row($c) {
598 // Dummy
599 return '<tr'.($c%2 ? $this->pi_classParam('listrow-odd') : '').'><td><p>[dummy row]</p></td></tr>';
600 }
601
602 /**
603 * Returns a list header row.
604 * (Dummy)
605 * 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.
606 *
607 * @return string HTML output, a table row with a class attribute set
608 */
609 function pi_list_header() {
610 return '<tr'.$this->pi_classParam('listrow-header').'><td><p>[dummy header row]</p></td></tr>';
611 }
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627 /***************************
628 *
629 * Stylesheet, CSS
630 *
631 **************************/
632
633
634 /**
635 * Returns a class-name prefixed with $this->prefixId and with all underscores substituted to dashes (-)
636 *
637 * @param string The class name (or the END of it since it will be prefixed by $this->prefixId.'-')
638 * @return string The combined class name (with the correct prefix)
639 */
640 function pi_getClassName($class) {
641 return str_replace('_','-',$this->prefixId).($this->prefixId?'-':'').$class;
642 }
643
644 /**
645 * Returns the class-attribute with the correctly prefixed classname
646 * Using pi_getClassName()
647 *
648 * @param string The class name (suffix)
649 * @return string A "class" attribute with value and a single space char before it.
650 * @see pi_getClassName()
651 */
652 function pi_classParam($class) {
653 return ' class="'.$this->pi_getClassName($class).'"';
654 }
655
656 /**
657 * Sets CSS style-data for the $class-suffix (prefixed by pi_getClassName())
658 *
659 * @param string $class: Class suffix, see pi_getClassName
660 * @param string $data: CSS data
661 * @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
662 * @return void
663 * @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.
664 * @private
665 */
666 function pi_setClassStyle($class,$data,$selector='') {
667 $GLOBALS['TSFE']->setCSS($this->pi_getClassName($class).($selector?' '.$selector:''),'.'.$this->pi_getClassName($class).($selector?' '.$selector:'').' {'.$data.'}');
668 }
669
670 /**
671 * Wraps the input string in a <div> tag with the class attribute set to the prefixId.
672 * 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.
673 *
674 * @param string HTML content to wrap in the div-tags with the "main class" of the plugin
675 * @return string HTML content wrapped, ready to return to the parent object.
676 */
677 function pi_wrapInBaseClass($str) {
678 return '
679
680
681 <!--
682
683 BEGIN: Content of extension "'.$this->extKey.'", plugin "'.$this->prefixId.'"
684
685 -->
686 <div class="'.str_replace('_','-',$this->prefixId).'">
687 '.$str.'
688 </div>
689 <!-- END: Content of extension "'.$this->extKey.'", plugin "'.$this->prefixId.'" -->
690
691 ';
692 }
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710 /***************************
711 *
712 * Frontend editing: Edit panel, edit icons
713 *
714 **************************/
715
716 /**
717 * Returns the Backend User edit panel for the $row from $tablename
718 *
719 * @param array Record array.
720 * @param string Table name
721 * @param string A label to show with the panel.
722 * @param array TypoScript parameters to pass along to the EDITPANEL content Object that gets rendered. The property "allow" WILL get overridden/set though.
723 * @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).
724 * @see tslib_cObj::EDITPANEL()
725 */
726 function pi_getEditPanel($row='',$tablename='',$label='',$conf=Array()) {
727 $panel='';
728 if (!$row || !$tablename) {
729 $row = $this->internal['currentRow'];
730 $tablename = $this->internal['currentTable'];
731 }
732
733 if ($GLOBALS['TSFE']->beUserLogin) {
734 // Create local cObj if not set:
735 if (!is_object($this->pi_EPtemp_cObj)) {
736 $this->pi_EPtemp_cObj = t3lib_div::makeInstance('tslib_cObj');
737 $this->pi_EPtemp_cObj->setParent($this->cObj->data,$this->cObj->currentRecord);
738 }
739
740 // Initialize the cObj object with current row
741 $this->pi_EPtemp_cObj->start($row,$tablename);
742
743 // Setting TypoScript values in the $conf array. See documentation in TSref for the EDITPANEL cObject.
744 $conf['allow'] = 'edit,new,delete,move,hide';
745 $panel = $this->pi_EPtemp_cObj->cObjGetSingle('EDITPANEL',$conf,'editpanel');
746 }
747
748 if ($panel) {
749 if ($label) {
750 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 -->';
751 } else return '<!-- BEGIN: EDIT PANEL -->'.$panel.'<!-- END: EDIT PANEL -->';
752 } else return $label;
753 }
754
755 /**
756 * Adds edit-icons to the input content.
757 * tslib_cObj::editIcons used for rendering
758 *
759 * @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)
760 * @param string The list of fields to edit when the icon is clicked.
761 * @param string Title for the edit icon.
762 * @param array Table record row
763 * @param string Table name
764 * @param array Conf array
765 * @return string The processed content
766 * @see tslib_cObj::editIcons()
767 */
768 function pi_getEditIcon($content,$fields,$title='',$row='',$tablename='',$oConf=array()) {
769 if ($GLOBALS['TSFE']->beUserLogin){
770 if (!$row || !$tablename) {
771 $row = $this->internal['currentRow'];
772 $tablename = $this->internal['currentTable'];
773 }
774 $conf=array_merge(array(
775 'beforeLastTag'=>1,
776 'iconTitle' => $title
777 ),$oConf);
778 $content=$this->cObj->editIcons($content,$tablename.':'.$fields,$conf,$tablename.':'.$row['uid'],$row,'&viewUrl='.rawurlencode(t3lib_div::getIndpEnv('REQUEST_URI')));
779 }
780 return $content;
781 }
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799 /***************************
800 *
801 * Localization, locallang functions
802 *
803 **************************/
804
805
806 /**
807 * Returns the localized label of the LOCAL_LANG key, $key
808 * Notice that for debugging purposes prefixes for the output values can be set with the internal vars ->LLtestPrefixAlt and ->LLtestPrefix
809 *
810 * @param string The key from the LOCAL_LANG array for which to return the value.
811 * @param string Alternative string to return IF no value is found set for the key, neither for the local language nor the default.
812 * @param boolean If true, the output label is passed through htmlspecialchars()
813 * @return string The value from LOCAL_LANG.
814 */
815 function pi_getLL($key,$alt='',$hsc=FALSE) {
816 if (isset($this->LOCAL_LANG[$this->LLkey][$key])) {
817 $word = $GLOBALS['TSFE']->csConv($this->LOCAL_LANG[$this->LLkey][$key]);
818 } elseif (isset($this->LOCAL_LANG['default'][$key])) {
819 $word = $this->LOCAL_LANG['default'][$key];
820 } else {
821 $word = $this->LLtestPrefixAlt.$alt;
822 }
823
824 $output = $this->LLtestPrefix.$word;
825 if ($hsc) $output = htmlspecialchars($output);
826
827 return $output;
828 }
829
830 /**
831 * Loads local-language values by looking for a "locallang.php" file in the plugin class directory ($this->scriptRelPath) and if found includes it.
832 * Also locallang values set in the TypoScript property "_LOCAL_LANG" are merged onto the values found in the "locallang.php" file.
833 *
834 * @return void
835 */
836 function pi_loadLL() {
837 if (!$this->LOCAL_LANG_loaded && $this->scriptRelPath) {
838 $basePath = t3lib_extMgm::siteRelPath($this->extKey).dirname($this->scriptRelPath).'/locallang.php';
839 if (@is_file($basePath)) {
840 include('./'.$basePath);
841 $this->LOCAL_LANG = $LOCAL_LANG;
842 if (is_array($this->conf['_LOCAL_LANG.'])) {
843 reset($this->conf['_LOCAL_LANG.']);
844 while(list($k,$lA)=each($this->conf['_LOCAL_LANG.'])) {
845 if (is_array($lA)) {
846 $k = substr($k,0,-1);
847 $this->LOCAL_LANG[$k] = t3lib_div::array_merge_recursive_overrule(is_array($this->LOCAL_LANG[$k])?$this->LOCAL_LANG[$k]:array(), $lA);
848 }
849 }
850 }
851 }
852 }
853 $this->LOCAL_LANG_loaded = 1;
854 }
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878 /***************************
879 *
880 * Database, queries
881 *
882 **************************/
883
884 /**
885 * 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'])
886 * Set $count to 1 if you wish to get a count(*) query for selecting the number of results.
887 * Notice that the query will use $this->conf['pidList'] and $this->conf['recursive'] to generate a PID list within which to search for records.
888 *
889 * @param string The table name to make the query for.
890 * @param boolean If set, you will get a "count(*)" query back instead of field selecting
891 * @param string Additional WHERE clauses (should be starting with " AND ....")
892 * @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.
893 * @param string If set, this is added as a " GROUP BY ...." part of the query.
894 * @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']
895 * @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
896 * @return string The query build.
897 */
898 function pi_list_query($table,$count=0,$addWhere='',$mm_cat='',$groupBy='',$orderBy='',$query='') {
899 // Begin Query:
900 if (!$query) {
901 // Fetches the list of PIDs to select from.
902 // TypoScript property .pidList is a comma list of pids. If blank, current page id is used.
903 // 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.
904 $pidList = $this->pi_getPidList($this->conf['pidList'],$this->conf['recursive']);
905 if (is_array($mm_cat)) {
906 $query='FROM '.$table.','.$mm_cat['table'].','.$mm_cat['mmtable'].chr(10).
907 ' WHERE '.$table.'.uid='.$mm_cat['mmtable'].'.uid_local AND '.$mm_cat['table'].'.uid='.$mm_cat['mmtable'].'.uid_foreign '.chr(10).
908 (strcmp($mm_cat['catUidList'],'')?' AND '.$mm_cat['table'].'.uid IN ('.$mm_cat['catUidList'].')':'').chr(10).
909 ' AND '.$table.'.pid IN ('.$pidList.')'.chr(10).
910 $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!
911 } else {
912 $query='FROM '.$table.' WHERE pid IN ('.$pidList.')'.chr(10).
913 $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!
914 }
915 }
916
917 // Add '$addWhere'
918 if ($addWhere) {$query.=' '.$addWhere.chr(10);}
919
920 // Search word:
921 if ($this->piVars['sword'] && $this->internal['searchFieldList']) {
922 $query.=$this->cObj->searchWhere($this->piVars['sword'],$this->internal['searchFieldList'],$table).chr(10);
923 }
924
925 if ($count) {
926 $query = 'SELECT count(*) '.chr(10).$query;
927 } else {
928 if ($groupBy) $query.=' '.$groupBy;
929 // Order by data:
930 if ($orderBy) {
931 $query.=' '.$orderBy;
932 } else {
933 if (t3lib_div::inList($this->internal['orderByList'],$this->internal['orderBy'])) {
934 $query.= ' ORDER BY '.$table.'.'.$this->internal['orderBy'].($this->internal['descFlag']?' DESC':'').chr(10);
935 }
936 }
937
938 // Limit data:
939 $pointer=$this->piVars['pointer'];
940 $pointer=intval($pointer);
941 $results_at_a_time = t3lib_div::intInRange($this->internal['results_at_a_time'],1,1000);
942 $query.= ' LIMIT '.($pointer*$results_at_a_time).','.$results_at_a_time.chr(10);
943
944 // Add 'SELECT'
945 $query = 'SELECT '.$this->pi_prependFieldsWithTable($table,$this->pi_listFields).' '.chr(10).$query;
946 }
947 return $query;
948 }
949
950 /**
951 * Returns the row $uid from $table
952 * (Simply calling $GLOBALS['TSFE']->sys_page->checkRecord())
953 *
954 * @param string The table name
955 * @param integer The uid of the record from the table
956 * @param boolean If $checkPage is set, it's required that the page on which the record resides is accessible
957 * @return array If record is found, an array. Otherwise false.
958 */
959 function pi_getRecord($table,$uid,$checkPage=0) {
960 return $GLOBALS['TSFE']->sys_page->checkRecord($table,$uid,$checkPage);
961 }
962
963 /**
964 * Returns a commalist of page ids for a query (eg. 'WHERE pid IN (...)')
965 *
966 * @param string $pid_list is a comma list of page ids (if empty current page is used)
967 * @param integer $recursive is an integer >=0 telling how deep to dig for pids under each entry in $pid_list
968 * @return string List of PID values (comma separated)
969 */
970 function pi_getPidList($pid_list,$recursive=0) {
971 if (!strcmp($pid_list,'')) $pid_list = $GLOBALS['TSFE']->id;
972 $recursive = t3lib_div::intInRange($recursive,0);
973
974 $pid_list_arr = array_unique(t3lib_div::trimExplode(',',$pid_list,1));
975 $pid_list='';
976 reset($pid_list_arr);
977 while(list(,$val)=each($pid_list_arr)) {
978 $val = t3lib_div::intInRange($val,0);
979 if ($val) $pid_list.=$val.','.$this->cObj->getTreeList($val,$recursive);
980 }
981 return ereg_replace(',$','',$pid_list);
982 }
983
984 /**
985 * Having a comma list of fields ($fieldList) this is prepended with the $table.'.' name
986 *
987 * @param string Table name to prepend
988 * @param string List of fields where each element will be prepended with the table name given.
989 * @return string List of fields processed.
990 */
991 function pi_prependFieldsWithTable($table,$fieldList) {
992 $list=t3lib_div::trimExplode(',',$fieldList,1);
993 $return=array();
994 while(list(,$listItem)=each($list)) {
995 $return[]=$table.'.'.$listItem;
996 }
997 return implode(',',$return);
998 }
999
1000 /**
1001 * Will select all records from the "category table", $table, and return them in an array.
1002 *
1003 * @param string The name of the category table to select from.
1004 * @param integer The page from where to select the category records.
1005 * @param string Additional where clauses (basically the end of the query - could include a ORDER BY clause)
1006 * @return array The array with the category records in.
1007 */
1008 function pi_getCategoryTableContents($table,$pid,$addWhere='') {
1009 $query = 'SELECT * FROM '.$table.' WHERE pid='.intval($pid).$this->cObj->enableFields($table).' '.$addWhere;
1010
1011 $outArr = array();
1012 $res = mysql(TYPO3_db,$query);
1013 while($row=mysql_fetch_assoc($res)) {
1014 $outArr[$row['uid']]=$row;
1015 }
1016 return $outArr;
1017 }
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030 /***************************
1031 *
1032 * Various
1033 *
1034 **************************/
1035
1036 /**
1037 * 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.
1038 * Notice that this function will only work as long as values are integers.
1039 *
1040 * @param string List of fields (keys from piVars) to evaluate on
1041 * @param integer Limit for the values.
1042 * @return boolean Returns true (1) if conditions are met.
1043 */
1044 function pi_isOnlyFields($fList,$lowerThan=-1) {
1045 $lowerThan = $lowerThan==-1 ? $this->pi_lowerThan : $lowerThan;
1046
1047 $fList = t3lib_div::trimExplode(',',$fList,1);
1048 $tempPiVars = $this->piVars;
1049 while(list(,$k)=each($fList)) {
1050 if (!t3lib_div::testInt($tempPiVars[$k]) || $tempPiVars[$k]<$lowerThan) unset($tempPiVars[$k]);
1051 }
1052 if (!count($tempPiVars)) return 1;
1053 }
1054
1055 /**
1056 * Returns true if the array $inArray contains only values allowed to be cached based on the configuration in $this->pi_autoCacheFields
1057 * Used by ->pi_linkTP_keepPIvars
1058 * This is an advanced form of evaluation of whether a URL should be cached or not.
1059 *
1060 * @param array An array with piVars values to evaluate
1061 * @return boolean Returns true (1) if conditions are met.
1062 * @see pi_linkTP_keepPIvars()
1063 */
1064 function pi_autoCache($inArray) {
1065 if (is_array($inArray)) {
1066 reset($inArray);
1067 while(list($fN,$fV)=each($inArray)) {
1068 if (!strcmp($inArray[$fN],'')) {
1069 unset($inArray[$fN]);
1070 } elseif (is_array($this->pi_autoCacheFields[$fN])) {
1071 if (is_array($this->pi_autoCacheFields[$fN]['range'])
1072 && intval($inArray[$fN])>=intval($this->pi_autoCacheFields[$fN]['range'][0])
1073 && intval($inArray[$fN])<=intval($this->pi_autoCacheFields[$fN]['range'][1])) {
1074 unset($inArray[$fN]);
1075 }
1076 if (is_array($this->pi_autoCacheFields[$fN]['list'])
1077 && in_array($inArray[$fN],$this->pi_autoCacheFields[$fN]['list'])) {
1078 unset($inArray[$fN]);
1079 }
1080 }
1081 }
1082 }
1083 if (!count($inArray)) return 1;
1084 }
1085
1086 /**
1087 * 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.
1088 * This is useful for rendering of content in RTE fields where the transformation mode is set to "ts_css" or so.
1089 * Notice that this requires the use of "css_styled_content" to work right.
1090 *
1091 * @param string The input text string to process
1092 * @return string The processed string
1093 * @see tslib_cObj::parseFunc()
1094 */
1095 function pi_RTEcssText($str) {
1096 $parseFunc = $GLOBALS['TSFE']->tmpl->setup['lib.']['parseFunc_RTE.'];
1097 if (is_array($parseFunc)) $str = $this->cObj->parseFunc($str, $parseFunc);
1098 return $str;
1099 }
1100
1101
1102
1103
1104
1105 /*******************************
1106 *
1107 * FlexForms related functions
1108 *
1109 *******************************/
1110
1111 /**
1112 * Converts $this->cObj->data['pi_flexform'] from XML string to flexForm array.
1113 *
1114 * @return void
1115 */
1116 function pi_initPIflexForm() {
1117 // Converting flexform data into array:
1118 if (!is_array($this->cObj->data['pi_flexform']) && $this->cObj->data['pi_flexform']) {
1119 $this->cObj->data['pi_flexform'] = t3lib_div::xml2array($this->cObj->data['pi_flexform']);
1120 if (!is_array($this->cObj->data['pi_flexform'])) $this->cObj->data['pi_flexform']=array();
1121 }
1122 }
1123
1124 /**
1125 * Return value from somewhere inside a FlexForm structure
1126 *
1127 * @param array FlexForm data
1128 * @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.
1129 * @param string Sheet pointer, eg. "sDEF"
1130 * @param string Language pointer, eg. "lDEF"
1131 * @param string Value pointer, eg. "vDEF"
1132 * @return string The content.
1133 */
1134 function pi_getFFvalue($T3FlexForm_array,$fieldName,$sheet='sDEF',$lang='lDEF',$value='vDEF') {
1135 $sheetArray = $T3FlexForm_array['data'][$sheet][$lang];
1136 if (is_array($sheetArray)) {
1137 return $this->pi_getFFvalueFromSheetArray($sheetArray,explode('/',$fieldName),$value);
1138 }
1139 }
1140
1141 /**
1142 * Returns part of $sheetArray pointed to by the keys in $fieldNameArray
1143 *
1144 * @param array Multidimensiona array, typically FlexForm contents
1145 * @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...)
1146 * @param string Value for outermost key, typ. "vDEF" depending on language.
1147 * @return mixed The value, typ. string.
1148 * @access private
1149 * @see pi_getFFvalue()
1150 */
1151 function pi_getFFvalueFromSheetArray($sheetArray,$fieldNameArr,$value) {
1152
1153 $tempArr=$sheetArray;
1154 foreach($fieldNameArr as $k => $v) {
1155 if (t3lib_div::testInt($v)) {
1156 if (is_array($tempArr)) {
1157 $c=0;
1158 foreach($tempArr as $values) {
1159 if ($c==$v) {
1160 debug($values);
1161 $tempArr=$values;
1162 break;
1163 }
1164 $c++;
1165 }
1166 }
1167 } else {
1168 $tempArr = $tempArr[$v];
1169 }
1170 }
1171 return $tempArr[$value];
1172 }
1173 }
1174
1175 // NO extension of class - does not make sense here.
1176 ?>