This commit was manufactured by cvs2svn to create tag
[Packages/TYPO3.CMS.git] / typo3 / sysext / cms / tslib / class.tslib_pibase.php
index 5e9088b..8189519 100755 (executable)
@@ -1,22 +1,22 @@
 <?php
 /***************************************************************
 *  Copyright notice
-*  
-*  (c) 1999-2003 Kasper Skårhøj (kasper@typo3.com)
+*
+*  (c) 1999-2004 Kasper Skaarhoj (kasper@typo3.com)
 *  All rights reserved
 *
-*  This script is part of the TYPO3 project. The TYPO3 project is 
+*  This script is part of the TYPO3 project. The TYPO3 project is
 *  free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
-* 
+*
 *  The GNU General Public License can be found at
 *  http://www.gnu.org/copyleft/gpl.html.
-*  A copy is found in the textfile GPL.txt and important notices to the license 
+*  A copy is found in the textfile GPL.txt and important notices to the license
 *  from the author is found in LICENSE.txt distributed with these scripts.
 *
-* 
+*
 *  This script is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *
 *  This copyright notice MUST APPEAR in all copies of the script!
 ***************************************************************/
-/** 
+/**
  * This script contains the parent class, 'pibase', providing an API with the most basic methods for frontend plugins
  *
- * Revised for TYPO3 3.6 June/2003 by Kasper Skårhøj
+ * $Id$
+ * Revised for TYPO3 3.6 June/2003 by Kasper Skaarhoj
  * XHTML compliant
  *
- * @author     Kasper Skårhøj <kasper@typo3.com>
- * @package TYPO3
- * @subpackage tslib
+ * @author     Kasper Skaarhoj <kasper@typo3.com>
  */
 /**
  * [CLASS/FUNCTION INDEX of SCRIPT]
  *
  *
  *
- *  125: class tslib_pibase 
+ *  132: class tslib_pibase
  *
  *              SECTION: Init functions
- *  204:     function tslib_pibase()   
- *  218:     function pi_setPiVarDefaults()    
+ *  211:     function tslib_pibase()
+ *  225:     function pi_setPiVarDefaults()
  *
  *              SECTION: Link functions
- *  257:     function pi_getPageLink($id,$target='',$urlParameters=array())    
- *  273:     function pi_linkToPage($str,$id,$target='',$urlParameters=array())        
- *  287:     function pi_linkTP($str,$urlParameters=array(),$cache=0)  
- *  309:     function pi_linkTP_keepPIvars($str,$overrulePIvars=array(),$cache=0,$clearAnyway=0)       
- *  332:     function pi_linkTP_keepPIvars_url($overrulePIvars=array(),$cache=0,$clearAnyway=0)        
- *  348:     function pi_list_linkSingle($str,$uid,$cache=0,$mergeArr=array()) 
- *  371:     function pi_openAtagHrefInJSwindow($str,$winName='',$winParams='width=670,height=500,status=0,menubar=0,scrollbars=1,resizable=1')        
+ *  264:     function pi_getPageLink($id,$target='',$urlParameters=array())
+ *  280:     function pi_linkToPage($str,$id,$target='',$urlParameters=array())
+ *  295:     function pi_linkTP($str,$urlParameters=array(),$cache=0,$altPageId=0)
+ *  318:     function pi_linkTP_keepPIvars($str,$overrulePIvars=array(),$cache=0,$clearAnyway=0,$altPageId=0)
+ *  342:     function pi_linkTP_keepPIvars_url($overrulePIvars=array(),$cache=0,$clearAnyway=0,$altPageId=0)
+ *  360:     function pi_list_linkSingle($str,$uid,$cache=FALSE,$mergeArr=array(),$urlOnly=FALSE,$altPageId=0)
+ *  388:     function pi_openAtagHrefInJSwindow($str,$winName='',$winParams='width=670,height=500,status=0,menubar=0,scrollbars=1,resizable=1')
  *
  *              SECTION: Functions for listing, browsing, searching etc.
- *  411:     function pi_list_browseresults($showResultCount=1,$tableParams='')        
- *  464:     function pi_list_searchBox($tableParams='')       
- *  483:     function pi_list_modeSelector($items=array(),$tableParams='')     
- *  510:     function pi_list_makelist($res,$tableParams='')   
- *  535:     function pi_list_row($c)  
- *  547:     function pi_list_header() 
+ *  428:     function pi_list_browseresults($showResultCount=1,$tableParams='')
+ *  500:     function pi_list_searchBox($tableParams='')
+ *  528:     function pi_list_modeSelector($items=array(),$tableParams='')
+ *  566:     function pi_list_makelist($res,$tableParams='')
+ *  601:     function pi_list_row($c)
+ *  613:     function pi_list_header()
  *
  *              SECTION: Stylesheet, CSS
- *  578:     function pi_getClassName($class)  
- *  590:     function pi_classParam($class)    
- *  604:     function pi_setClassStyle($class,$data,$selector='')      
- *  615:     function pi_wrapInBaseClass($str) 
+ *  644:     function pi_getClassName($class)
+ *  656:     function pi_classParam($class)
+ *  670:     function pi_setClassStyle($class,$data,$selector='')
+ *  681:     function pi_wrapInBaseClass($str)
  *
  *              SECTION: Frontend editing: Edit panel, edit icons
- *  651:     function pi_getEditPanel($row='',$tablename='',$label='',$conf=Array())   
- *  692:     function pi_getEditIcon($content,$fields,$title='',$row='',$tablename='') 
+ *  730:     function pi_getEditPanel($row='',$tablename='',$label='',$conf=Array())
+ *  772:     function pi_getEditIcon($content,$fields,$title='',$row='',$tablename='',$oConf=array())
  *
  *              SECTION: Localization, locallang functions
- *  738:     function pi_getLL($key,$alt='')   
- *  754:     function pi_loadLL()      
+ *  819:     function pi_getLL($key,$alt='',$hsc=FALSE)
+ *  840:     function pi_loadLL()
  *
  *              SECTION: Database, queries
- *  816:     function pi_list_query($table,$count=0,$addWhere='',$mm_cat='',$groupBy='',$orderBy='',$query='') 
- *  877:     function pi_getRecord($table,$uid,$checkPage=0)   
- *  888:     function pi_getPidList($pid_list,$recursive=0)    
- *  909:     function pi_prependFieldsWithTable($table,$fieldList)     
- *  926:     function pi_getCategoryTableContents($table,$pid,$addWhere='')    
+ *  905:     function pi_list_query($table,$count=0,$addWhere='',$mm_cat='',$groupBy='',$orderBy='',$query='',$returnQueryArray=FALSE)
+ *  997:     function pi_exec_query($table,$count=0,$addWhere='',$mm_cat='',$groupBy='',$orderBy='',$query='')
+ * 1012:     function pi_getRecord($table,$uid,$checkPage=0)
+ * 1023:     function pi_getPidList($pid_list,$recursive=0)
+ * 1048:     function pi_prependFieldsWithTable($table,$fieldList)
+ * 1068:     function pi_getCategoryTableContents($table,$pid,$whereClause='',$groupBy='',$orderBy='',$limit='')
  *
  *              SECTION: Various
- *  962:     function pi_isOnlyFields($fList,$lowerThan=-1)    
- *  982:     function pi_autoCache($inArray)   
- * 1013:     function pi_RTEcssText($str)      
+ * 1112:     function pi_isOnlyFields($fList,$lowerThan=-1)
+ * 1132:     function pi_autoCache($inArray)
+ * 1163:     function pi_RTEcssText($str)
+ *
+ *              SECTION: FlexForms related functions
+ * 1184:     function pi_initPIflexForm()
+ * 1202:     function pi_getFFvalue($T3FlexForm_array,$fieldName,$sheet='sDEF',$lang='lDEF',$value='vDEF')
+ * 1219:     function pi_getFFvalueFromSheetArray($sheetArray,$fieldNameArr,$value)
  *
- * TOTAL FUNCTIONS: 31
+ * TOTAL FUNCTIONS: 35
  * (This index is automatically created/updated by the extension "extdeveval")
  *
  */
  * Most modern frontend plugins are extension classes of this one.
  * This class contains functions which assists these plugins in creating lists, searching, displaying menus, page-browsing (next/previous/1/2/3) and handling links.
  * 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...)
- * 
- * @author     Kasper Skårhøj <kasper@typo3.com>
+ *
+ * @author     Kasper Skaarhoj <kasper@typo3.com>
+ * @package TYPO3
+ * @subpackage tslib
  */
 class tslib_pibase {
 
@@ -154,26 +161,26 @@ class tslib_pibase {
        var $pi_lowerThan = 5;
        var $pi_moreParams='';
        var $pi_listFields='*';
-       
+
        var $pi_autoCacheFields=array();
        var $pi_autoCacheEn=0;
-       
+
        var $pi_USER_INT_obj = 0;       // If set, then links are 1) not using cHash and 2) allowing pages to be cached.
-       
+
        /**
         * Should normally be set in the main function with the TypoScript content passed to the method.
-        * 
+        *
         * $conf[LOCAL_LANG][_key_] is reserved for Local Language overrides.
         * $conf[userFunc] / $conf[includeLibs]  reserved for setting up the USER / USER_INT object. See TSref
-        */ 
-       var $conf = Array();    
-       
+        */
+       var $conf = Array();
+
        // internal, don't mess with...
-       var $pi_EPtemp_cObj;    
+       var $pi_EPtemp_cObj;
        var $pi_tmpPageId=0;
 
-       
-       
+
+
 
 
 
@@ -189,7 +196,7 @@ class tslib_pibase {
 
 
        /***************************
-        * 
+        *
         * Init functions
         *
         **************************/
@@ -198,8 +205,8 @@ class tslib_pibase {
         * Class Constructor (true constructor)
         * Initializes $this->piVars if $this->prefixId is set to any value
         * Will also set $this->LLkey based on the config.language setting.
-        * 
-        * @return      void            
+        *
+        * @return      void
         */
        function tslib_pibase() {
                if ($this->prefixId)    {
@@ -212,8 +219,8 @@ class tslib_pibase {
 
        /**
         * If internal TypoScript property "_DEFAULT_PI_VARS." is set then it will merge the current $this->piVars array onto these default values.
-        * 
-        * @return      void            
+        *
+        * @return      void
         */
        function pi_setPiVarDefaults()  {
                if (is_array($this->conf['_DEFAULT_PI_VARS.'])) {
@@ -222,7 +229,7 @@ class tslib_pibase {
        }
 
 
-       
+
 
 
 
@@ -236,7 +243,7 @@ class tslib_pibase {
 
 
        /***************************
-        * 
+        *
         * Link functions
         *
         **************************/
@@ -245,9 +252,9 @@ class tslib_pibase {
         * Get URL to some page.
         * Returns the URL to page $id with $target and an array of additional url-parameters, $urlParameters
         * Simple example: $this->pi_getPageLink(123) to get the URL for page-id 123.
-        * 
+        *
         * The function basically calls $this->cObj->getTypoLink_URL()
-        * 
+        *
         * @param       integer         Page id
         * @param       string          Target value to use. Affects the &type-value of the URL, defaults to current.
         * @param       array           Additional URL parameters to set (key/value pairs)
@@ -261,8 +268,8 @@ class tslib_pibase {
        /**
         * Link a string to some page.
         * Like pi_getPageLink() but takes a string as first parameter which will in turn be wrapped with the URL including target attribute
-        * 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)
-        * 
+        * Simple example: $this->pi_linkToPage('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)
+        *
         * @param       string          The content string to wrap in <a> tags
         * @param       integer         Page id
         * @param       string          Target value to use. Affects the &type-value of the URL, defaults to current.
@@ -277,20 +284,21 @@ class tslib_pibase {
        /**
         * Link string to the current page.
         * Returns the $str wrapped in <a>-tags with a link to the CURRENT page, but with $urlParameters set as extra parameters for the page.
-        * 
+        *
         * @param       string          The content string to wrap in <a> tags
         * @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.
-        * @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.
+        * @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.
+        * @param       integer         Alternative page ID for the link. (By default this function links to the SAME page!)
         * @return      string          The input string wrapped in <a> tags
         * @see pi_linkTP_keepPIvars(), tslib_cObj::typoLink()
         */
-       function pi_linkTP($str,$urlParameters=array(),$cache=0)        {
+       function pi_linkTP($str,$urlParameters=array(),$cache=0,$altPageId=0)   {
                $conf=array();
-               $conf['useCacheHash']=$this->pi_USER_INT_obj?0:$cache;
-               $conf['no_cache']=$this->pi_USER_INT_obj?0:!$cache;
-               $conf['parameter']=$this->pi_tmpPageId ? $this->pi_tmpPageId : $GLOBALS['TSFE']->id;
-               $conf['additionalParams']=$this->conf['parent.']['addParams'].t3lib_div::implodeArrayForUrl('',$urlParameters,'',1).$this->pi_moreParams;
-               
+               $conf['useCacheHash'] = $this->pi_USER_INT_obj ? 0 : $cache;
+               $conf['no_cache'] = $this->pi_USER_INT_obj ? 0 : !$cache;
+               $conf['parameter'] = $altPageId ? $altPageId : ($this->pi_tmpPageId ? $this->pi_tmpPageId : $GLOBALS['TSFE']->id);
+               $conf['additionalParams'] = $this->conf['parent.']['addParams'].t3lib_div::implodeArrayForUrl('',$urlParameters,'',1).$this->pi_moreParams;
+
                return $this->cObj->typoLink($str, $conf);
        }
 
@@ -298,15 +306,16 @@ class tslib_pibase {
         * Link a string to the current page while keeping currently set values in piVars.
         * Like pi_linkTP, but $urlParameters is by default set to $this->piVars with $overrulePIvars overlaid.
         * 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.
-        * 
+        *
         * @param       string          The content string to wrap in <a> tags
         * @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)
         * @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.
         * @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[]"
+        * @param       integer         Alternative page ID for the link. (By default this function links to the SAME page!)
         * @return      string          The input string wrapped in <a> tags
         * @see pi_linkTP()
         */
-       function pi_linkTP_keepPIvars($str,$overrulePIvars=array(),$cache=0,$clearAnyway=0)     {
+       function pi_linkTP_keepPIvars($str,$overrulePIvars=array(),$cache=0,$clearAnyway=0,$altPageId=0)        {
                if (is_array($this->piVars) && is_array($overrulePIvars) && !$clearAnyway)      {
                        $piVars = $this->piVars;
                        unset($piVars['DATA']);
@@ -315,46 +324,54 @@ class tslib_pibase {
                                $cache = $this->pi_autoCache($overrulePIvars);
                        }
                }
-               $res = $this->pi_linkTP($str,Array($this->prefixId=>$overrulePIvars),$cache);
+               $res = $this->pi_linkTP($str,Array($this->prefixId=>$overrulePIvars),$cache,$altPageId);
                return $res;
        }
 
        /**
         * Get URL to the current page while keeping currently set values in piVars.
         * Same as pi_linkTP_keepPIvars but returns only the URL from the link.
-        * 
+        *
         * @param       array           See pi_linkTP_keepPIvars
         * @param       boolean         See pi_linkTP_keepPIvars
         * @param       boolean         See pi_linkTP_keepPIvars
+        * @param       integer         See pi_linkTP_keepPIvars
         * @return      string          The URL ($this->cObj->lastTypoLinkUrl)
         * @see pi_linkTP_keepPIvars()
         */
-       function pi_linkTP_keepPIvars_url($overrulePIvars=array(),$cache=0,$clearAnyway=0)      {
-               $this->pi_linkTP_keepPIvars('|',$overrulePIvars,$cache,$clearAnyway);
+       function pi_linkTP_keepPIvars_url($overrulePIvars=array(),$cache=0,$clearAnyway=0,$altPageId=0) {
+               $this->pi_linkTP_keepPIvars('|',$overrulePIvars,$cache,$clearAnyway,$altPageId);
                return $this->cObj->lastTypoLinkUrl;
        }
 
        /**
         * Wraps the $str in a link to a single display of the record (using piVars[showUid])
         * Uses pi_linkTP for the linking
-        * 
+        *
         * @param       string          The content string to wrap in <a> tags
         * @param       integer         UID of the record for which to display details (basically this will become the value of [showUid]
         * @param       boolean         See pi_linkTP_keepPIvars
         * @param       array           Array of values to override in the current piVars. Same as $overrulePIvars in pi_linkTP_keepPIvars
+        * @param       boolean         If true, only the URL is returned, not a full link
+        * @param       integer         Alternative page ID for the link. (By default this function links to the SAME page!)
         * @return      string          The input string wrapped in <a> tags
         * @see pi_linkTP(), pi_linkTP_keepPIvars()
         */
-       function pi_list_linkSingle($str,$uid,$cache=0,$mergeArr=array())       {
+       function pi_list_linkSingle($str,$uid,$cache=FALSE,$mergeArr=array(),$urlOnly=FALSE,$altPageId=0)       {
                if ($this->prefixId)    {
                        if ($cache)     {
                                $overrulePIvars=$uid?array('showUid'=>$uid):Array();
                                $overrulePIvars=array_merge($overrulePIvars,$mergeArr);
-                               $str = $this->pi_linkTP($str,Array($this->prefixId=>$overrulePIvars),$cache);
+                               $str = $this->pi_linkTP($str,Array($this->prefixId=>$overrulePIvars),$cache,$altPageId);
                        } else {
                                $overrulePIvars=array('showUid'=>$uid?$uid:'');
                                $overrulePIvars=array_merge($overrulePIvars,$mergeArr);
-                               $str = $this->pi_linkTP_keepPIvars($str,$overrulePIvars,$cache);
+                               $str = $this->pi_linkTP_keepPIvars($str,$overrulePIvars,$cache,0,$altPageId);
+                       }
+
+                               // If urlOnly flag, return only URL as it has recently be generated.
+                       if ($urlOnly)   {
+                               $str = $this->cObj->lastTypoLinkUrl;
                        }
                }
                return $str;
@@ -362,7 +379,7 @@ class tslib_pibase {
 
        /**
         * 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
-        * 
+        *
         * @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.
         * @param       string          Window name for the pop-up window
         * @param       string          Window parameters, see the default list for inspiration
@@ -394,7 +411,7 @@ class tslib_pibase {
 
 
        /***************************
-        * 
+        *
         * Functions for listing, browsing, searching etc.
         *
         **************************/
@@ -403,13 +420,13 @@ class tslib_pibase {
         * 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.
         * Using $this->piVars['pointer'] as pointer to the page to display
         * 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.
-        * 
+        *
         * @param       boolean         If set (default) the text "Displaying results..." will be show, otherwise not.
         * @param       string          Attributes for the table tag which is wrapped around the table cells containing the browse links
         * @return      string          Output HTML, wrapped in <div>-tags with a class attribute
         */
        function pi_list_browseresults($showResultCount=1,$tableParams='')      {
-                       
+
                        // Initializing variables:
                $pointer=$this->piVars['pointer'];
                $count=$this->internal['res_count'];
@@ -419,63 +436,94 @@ class tslib_pibase {
                $pointer=intval($pointer);
                $links=array();
 
-                       // Make browse-table/links:     
+                       // Make browse-table/links:
                if ($this->pi_alwaysPrev>=0)    {
                        if ($pointer>0) {
-                               $links[]='<td nowrap="nowrap"><p>'.$this->pi_linkTP_keepPIvars(htmlspecialchars($this->pi_getLL('pi_list_browseresults_prev','< Previous')),array('pointer'=>($pointer-1?$pointer-1:'')),0).'</p></td>';
+                               $links[]='
+                                       <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>';
                        } elseif ($this->pi_alwaysPrev) {
-                               $links[]='<td nowrap="nowrap"><p>'.htmlspecialchars($this->pi_getLL('pi_list_browseresults_prev','< Previous')).'</p></td>';
+                               $links[]='
+                                       <td nowrap="nowrap"><p>'.$this->pi_getLL('pi_list_browseresults_prev','< Previous',TRUE).'</p></td>';
                        }
                }
                for($a=0;$a<$max;$a++)  {
-                       $links[]='<td'.($pointer==$a?$this->pi_classParam('browsebox-SCell'):'').' nowrap="nowrap"><p>'.
-                               $this->pi_linkTP_keepPIvars(htmlspecialchars(trim($this->pi_getLL('pi_list_browseresults_page','Page').' '.($a+1))),array('pointer'=>($a?$a:'')),$this->pi_isOnlyFields($this->pi_isOnlyFields)).
+                       $links[]='
+                                       <td'.($pointer==$a?$this->pi_classParam('browsebox-SCell'):'').' nowrap="nowrap"><p>'.
+                               $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)).
                                '</p></td>';
                }
                if ($pointer<ceil($count/$results_at_a_time)-1) {
-                       $links[]='<td nowrap="nowrap"><p>'.
-                               $this->pi_linkTP_keepPIvars(htmlspecialchars($this->pi_getLL('pi_list_browseresults_next','Next >')),array('pointer'=>$pointer+1)).
+                       $links[]='
+                                       <td nowrap="nowrap"><p>'.
+                               $this->pi_linkTP_keepPIvars($this->pi_getLL('pi_list_browseresults_next','Next >',TRUE),array('pointer'=>$pointer+1)).
                                '</p></td>';
                }
-               
+
                $pR1 = $pointer*$results_at_a_time+1;
                $pR2 = $pointer*$results_at_a_time+$results_at_a_time;
-               $sTables = '<div'.$this->pi_classParam('browsebox').'>'.
-                       ($showResultCount ? '<p>'.sprintf(
-                               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>')),
-                               $pR1,
-                               min(array($this->internal['res_count'],$pR2)),
-                               $this->internal['res_count']
-                               ).'</p>':''
+               $sTables = '
+
+               <!--
+                       List browsing box:
+               -->
+               <div'.$this->pi_classParam('browsebox').'>'.
+                       ($showResultCount ? '
+                       <p>'.
+                               ($this->internal['res_count'] ?
+                       sprintf(
+                                       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>')),
+                                       $this->internal['res_count'] > 0 ? $pR1 : 0,
+                                       min(array($this->internal['res_count'],$pR2)),
+                                       $this->internal['res_count']
+                               ) :
+                               $this->pi_getLL('pi_list_browseresults_noResults','Sorry, no items were found.')).'</p>':''
                        ).
-               '<'.trim('table '.$tableParams).'>
-                       <tr>'.implode('',$links).'</tr>
-               </table></div>';
+               '
+
+                       <'.trim('table '.$tableParams).'>
+                               <tr>
+                                       '.implode('',$links).'
+                               </tr>
+                       </table>
+               </div>';
+
                return $sTables;
        }
 
        /**
         * Returns a Search box, sending search words to piVars "sword" and setting the "no_cache" parameter as well in the form.
         * Submits the search request to the current REQUEST_URI
-        * 
+        *
         * @param       string          Attributes for the table tag which is wrapped around the table cells containing the search box
         * @return      string          Output HTML, wrapped in <div>-tags with a class attribute
         */
        function pi_list_searchBox($tableParams='')     {
                        // Search box design:
-               $sTables = '<div'.$this->pi_classParam('searchbox').'><'.trim('table '.$tableParams).'>
-               <form action="'.htmlspecialchars(t3lib_div::getIndpEnv('REQUEST_URI')).'" method="post">
-               <tr>
-                       <td><input type="text" name="'.$this->prefixId.'[sword]" value="'.htmlspecialchars($this->piVars['sword']).'"'.$this->pi_classParam('searchbox-sword').' /></td>
-                       <td><input type="submit" value="'.htmlspecialchars($this->pi_getLL('pi_list_searchBox_search','Search')).'"'.$this->pi_classParam('searchbox-button').' /><input type="hidden" name="no_cache" value="1" /></td>
-               </tr></form>
-               </table></div>';
+               $sTables = '
+
+               <!--
+                       List search box:
+               -->
+               <div'.$this->pi_classParam('searchbox').'>
+                       <form action="'.htmlspecialchars(t3lib_div::getIndpEnv('REQUEST_URI')).'" method="post" style="margin: 0 0 0 0;">
+                       <'.trim('table '.$tableParams).'>
+                               <tr>
+                                       <td><input type="text" name="'.$this->prefixId.'[sword]" value="'.htmlspecialchars($this->piVars['sword']).'"'.$this->pi_classParam('searchbox-sword').' /></td>
+                                       <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" />'.
+                                               '<input type="hidden" name="'.$this->prefixId.'[pointer]" value="" />'.
+                                               '</td>
+                               </tr>
+                       </table>
+                       </form>
+               </div>';
+
                return $sTables;
        }
 
        /**
         * Returns a mode selector; a little menu in a table normally put in the top of the page/list.
-        * 
+        *
         * @param       array           Key/Value pairs for the menu; keys are the piVars[mode] values and the "values" are the labels for them.
         * @param       string          Attributes for the table tag which is wrapped around the table cells containing the menu
         * @return      string          Output HTML, wrapped in <div>-tags with a class attribute
@@ -484,25 +532,36 @@ class tslib_pibase {
                $cells=array();
                reset($items);
                while(list($k,$v)=each($items)) {
-                       $cells[]='<td'.($this->piVars['mode']==$k?$this->pi_classParam('modeSelector-SCell'):'').'><p>'.
+                       $cells[]='
+                                       <td'.($this->piVars['mode']==$k?$this->pi_classParam('modeSelector-SCell'):'').'><p>'.
                                $this->pi_linkTP_keepPIvars(htmlspecialchars($v),array('mode'=>$k),$this->pi_isOnlyFields($this->pi_isOnlyFields)).
                                '</p></td>';
                }
-               
-               $sTables = '<div'.$this->pi_classParam('modeSelector').'><'.trim('table '.$tableParams).'>
-                       <tr>'.implode('',$cells).'</tr>
-               </table></div>';
+
+               $sTables = '
+
+               <!--
+                       Mode selector (menu for list):
+               -->
+               <div'.$this->pi_classParam('modeSelector').'>
+                       <'.trim('table '.$tableParams).'>
+                               <tr>
+                                       '.implode('',$cells).'
+                               </tr>
+                       </table>
+               </div>';
+
                return $sTables;
        }
 
        /**
-        * Returns the list of items based on the input MySQL result pointer
+        * Returns the list of items based on the input SQL result pointer
         * For each result row the internal var, $this->internal['currentRow'], is set with the row returned.
         * $this->pi_list_header() makes the header row for the list
         * $this->pi_list_row() is used for rendering each row
         * 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.
-        * 
-        * @param       pointer         Result pointer to a MySQL result which can be traversed.
+        *
+        * @param       pointer         Result pointer to a SQL result which can be traversed.
         * @param       string          Attributes for the table tag which is wrapped around the table rows containing the list
         * @return      string          Output HTML, wrapped in <div>-tags with a class attribute
         * @see pi_list_row(), pi_list_header()
@@ -511,16 +570,26 @@ class tslib_pibase {
                        // Make list table header:
                $tRows=array();
                $this->internal['currentRow']='';
-               $tRows[]=$this->pi_list_header();
+               $tRows[] = $this->pi_list_header();
 
                        // Make list table rows
                $c=0;
-               while($this->internal['currentRow'] = mysql_fetch_assoc($res))  {
-                       $tRows[]=$this->pi_list_row($c);
+               while($this->internal['currentRow'] = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))      {
+                       $tRows[] = $this->pi_list_row($c);
                        $c++;
                }
 
-               $out = '<div'.$this->pi_classParam('listrow').'><'.trim('table '.$tableParams).'>'.implode('',$tRows).'</table></div>';
+               $out = '
+
+               <!--
+                       Record list:
+               -->
+               <div'.$this->pi_classParam('listrow').'>
+                       <'.trim('table '.$tableParams).'>
+                               '.implode('',$tRows).'
+                       </table>
+               </div>';
+
                return $out;
        }
 
@@ -528,7 +597,7 @@ class tslib_pibase {
         * Returns a list row. Get data from $this->internal['currentRow'];
         * (Dummy)
         * 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.
-        * 
+        *
         * @param       integer         Row counting. Starts at 0 (zero). Used for alternating class values in the output rows.
         * @return      string          HTML output, a table row with a class attribute set (alternative based on odd/even rows)
         */
@@ -541,7 +610,7 @@ class tslib_pibase {
         * Returns a list header row.
         * (Dummy)
         * 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.
-        * 
+        *
         * @return      string          HTML output, a table row with a class attribute set
         */
        function pi_list_header()       {
@@ -563,7 +632,7 @@ class tslib_pibase {
 
 
        /***************************
-        * 
+        *
         * Stylesheet, CSS
         *
         **************************/
@@ -571,7 +640,7 @@ class tslib_pibase {
 
        /**
         * Returns a class-name prefixed with $this->prefixId and with all underscores substituted to dashes (-)
-        * 
+        *
         * @param       string          The class name (or the END of it since it will be prefixed by $this->prefixId.'-')
         * @return      string          The combined class name (with the correct prefix)
         */
@@ -582,7 +651,7 @@ class tslib_pibase {
        /**
         * Returns the class-attribute with the correctly prefixed classname
         * Using pi_getClassName()
-        * 
+        *
         * @param       string          The class name (suffix)
         * @return      string          A "class" attribute with value and a single space char before it.
         * @see pi_getClassName()
@@ -593,11 +662,11 @@ class tslib_pibase {
 
        /**
         * Sets CSS style-data for the $class-suffix (prefixed by pi_getClassName())
-        * 
+        *
         * @param       string          $class: Class suffix, see pi_getClassName
         * @param       string          $data: CSS data
         * @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
-        * @return      void            
+        * @return      void
         * @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.
         * @private
         */
@@ -608,12 +677,25 @@ class tslib_pibase {
        /**
         * Wraps the input string in a <div> tag with the class attribute set to the prefixId.
         * 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.
-        * 
+        *
         * @param       string          HTML content to wrap in the div-tags with the "main class" of the plugin
         * @return      string          HTML content wrapped, ready to return to the parent object.
         */
        function pi_wrapInBaseClass($str)       {
-               return '<div class="'.str_replace('_','-',$this->prefixId).'">'.$str.'</div>';
+               return '
+
+
+       <!--
+
+               BEGIN: Content of extension "'.$this->extKey.'", plugin "'.$this->prefixId.'"
+
+       -->
+       <div class="'.str_replace('_','-',$this->prefixId).'">
+               '.$str.'
+       </div>
+       <!-- END: Content of extension "'.$this->extKey.'", plugin "'.$this->prefixId.'" -->
+
+       ';
        }
 
 
@@ -633,14 +715,14 @@ class tslib_pibase {
 
 
        /***************************
-        * 
+        *
         * Frontend editing: Edit panel, edit icons
         *
         **************************/
 
        /**
         * Returns the Backend User edit panel for the $row from $tablename
-        * 
+        *
         * @param       array           Record array.
         * @param       string          Table name
         * @param       string          A label to show with the panel.
@@ -654,53 +736,54 @@ class tslib_pibase {
                        $row = $this->internal['currentRow'];
                        $tablename = $this->internal['currentTable'];
                }
-               
+
                if ($GLOBALS['TSFE']->beUserLogin)      {
                                // Create local cObj if not set:
                        if (!is_object($this->pi_EPtemp_cObj))  {
                                $this->pi_EPtemp_cObj = t3lib_div::makeInstance('tslib_cObj');
                                $this->pi_EPtemp_cObj->setParent($this->cObj->data,$this->cObj->currentRecord);
                        }
-                       
+
                                // Initialize the cObj object with current row
                        $this->pi_EPtemp_cObj->start($row,$tablename);
-                       
+
                                // Setting TypoScript values in the $conf array. See documentation in TSref for the EDITPANEL cObject.
                        $conf['allow'] = 'edit,new,delete,move,hide';
                        $panel = $this->pi_EPtemp_cObj->cObjGetSingle('EDITPANEL',$conf,'editpanel');
                }
-               
+
                if ($panel)     {
                        if ($label)     {
-                               return '<table border="0" cellpadding="0" cellspacing="0" width="100%"><tr><td valign="top">'.$label.'</td><td valign="top" align="right">'.$panel.'</td></tr></table>';
-                       } else return $panel;
+                               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 -->';
+                       } else return '<!-- BEGIN: EDIT PANEL -->'.$panel.'<!-- END: EDIT PANEL -->';
                } else return $label;
        }
 
        /**
         * Adds edit-icons to the input content.
         * tslib_cObj::editIcons used for rendering
-        * 
+        *
         * @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)
         * @param       string          The list of fields to edit when the icon is clicked.
         * @param       string          Title for the edit icon.
         * @param       array           Table record row
         * @param       string          Table name
+        * @param       array           Conf array
         * @return      string          The processed content
         * @see tslib_cObj::editIcons()
         */
-       function pi_getEditIcon($content,$fields,$title='',$row='',$tablename='')       {
+       function pi_getEditIcon($content,$fields,$title='',$row='',$tablename='',$oConf=array())        {
                if ($GLOBALS['TSFE']->beUserLogin){
                        if (!$row || !$tablename)       {
                                $row = $this->internal['currentRow'];
                                $tablename = $this->internal['currentTable'];
                        }
-                       $conf=array(
+                       $conf=array_merge(array(
                                'beforeLastTag'=>1,
                                'iconTitle' => $title
-                       );
+                       ),$oConf);
                        $content=$this->cObj->editIcons($content,$tablename.':'.$fields,$conf,$tablename.':'.$row['uid'],$row,'&viewUrl='.rawurlencode(t3lib_div::getIndpEnv('REQUEST_URI')));
-               }       
+               }
                return $content;
        }
 
@@ -721,7 +804,7 @@ class tslib_pibase {
 
 
        /***************************
-        * 
+        *
         * Localization, locallang functions
         *
         **************************/
@@ -730,26 +813,32 @@ class tslib_pibase {
        /**
         * Returns the localized label of the LOCAL_LANG key, $key
         * Notice that for debugging purposes prefixes for the output values can be set with the internal vars ->LLtestPrefixAlt and ->LLtestPrefix
-        * 
+        *
         * @param       string          The key from the LOCAL_LANG array for which to return the value.
         * @param       string          Alternative string to return IF no value is found set for the key, neither for the local language nor the default.
+        * @param       boolean         If true, the output label is passed through htmlspecialchars()
         * @return      string          The value from LOCAL_LANG.
         */
-       function pi_getLL($key,$alt='') {
+       function pi_getLL($key,$alt='',$hsc=FALSE)      {
                if (isset($this->LOCAL_LANG[$this->LLkey][$key]))       {
-                       $word = $this->LOCAL_LANG[$this->LLkey][$key];
+                       $word = $GLOBALS['TSFE']->csConv($this->LOCAL_LANG[$this->LLkey][$key]);
                } elseif (isset($this->LOCAL_LANG['default'][$key]))    {
                        $word = $this->LOCAL_LANG['default'][$key];
-               } else $word = $this->LLtestPrefixAlt.$alt;
+               } else {
+                       $word = $this->LLtestPrefixAlt.$alt;
+               }
 
-               return $this->LLtestPrefix.$word;
+               $output = $this->LLtestPrefix.$word;
+               if ($hsc)       $output = htmlspecialchars($output);
+
+               return $output;
        }
-       
+
        /**
         * Loads local-language values by looking for a "locallang.php" file in the plugin class directory ($this->scriptRelPath) and if found includes it.
         * Also locallang values set in the TypoScript property "_LOCAL_LANG" are merged onto the values found in the "locallang.php" file.
-        * 
-        * @return      void            
+        *
+        * @return      void
         */
        function pi_loadLL()    {
                if (!$this->LOCAL_LANG_loaded && $this->scriptRelPath)  {
@@ -770,7 +859,7 @@ class tslib_pibase {
                }
                $this->LOCAL_LANG_loaded = 1;
        }
-       
+
 
 
 
@@ -794,7 +883,7 @@ class tslib_pibase {
 
 
        /***************************
-        * 
+        *
         * Database, queries
         *
         **************************/
@@ -803,20 +892,24 @@ class tslib_pibase {
         * 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'])
         * Set $count to 1 if you wish to get a count(*) query for selecting the number of results.
         * Notice that the query will use $this->conf['pidList'] and $this->conf['recursive'] to generate a PID list within which to search for records.
-        * 
-        * @param       string          The table name to make the query for.
-        * @param       boolean         If set, you will get a "count(*)" query back instead of field selecting
-        * @param       string          Additional WHERE clauses (should be starting with " AND ....")
-        * @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.
-        * @param       string          If set, this is added as a " GROUP BY ...." part of the query.
-        * @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']
-        * @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
-        * @return      string          The query build.
+        *
+        * @param       string          See pi_exec_query()
+        * @param       boolean         See pi_exec_query()
+        * @param       string          See pi_exec_query()
+        * @param       mixed           See pi_exec_query()
+        * @param       string          See pi_exec_query()
+        * @param       string          See pi_exec_query()
+        * @param       string          See pi_exec_query()
+        * @param       boolean         If set, the function will return the query not as a string but array with the various parts.
+        * @return      mixed           The query build.
+        * @access private
+        * @depreciated         Use pi_exec_query() instead!
         */
-       function pi_list_query($table,$count=0,$addWhere='',$mm_cat='',$groupBy='',$orderBy='',$query='')       {
+       function pi_list_query($table,$count=0,$addWhere='',$mm_cat='',$groupBy='',$orderBy='',$query='',$returnQueryArray=FALSE)       {
+
                        // Begin Query:
                if (!$query)    {
-                               // Fetches the list of PIDs to select from. 
+                               // Fetches the list of PIDs to select from.
                                // TypoScript property .pidList is a comma list of pids. If blank, current page id is used.
                                // 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.
                        $pidList = $this->pi_getPidList($this->conf['pidList'],$this->conf['recursive']);
@@ -831,56 +924,101 @@ class tslib_pibase {
                                                $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!
                        }
                }
-                                               
+
+                       // Split the "FROM ... WHERE" string so we get the WHERE part and TABLE names separated...:
+               list($TABLENAMES,$WHERE) = spliti('WHERE', trim($query), 2);
+               $TABLENAMES = trim(substr(trim($TABLENAMES),5));
+               $WHERE = trim($WHERE);
+
                        // Add '$addWhere'
-               if ($addWhere)  {$query.=' '.$addWhere.chr(10);}
+               if ($addWhere)  {$WHERE.=' '.$addWhere.chr(10);}
 
-                       // Search word:                         
+                       // Search word:
                if ($this->piVars['sword'] && $this->internal['searchFieldList'])       {
-                       $query.=$this->cObj->searchWhere($this->piVars['sword'],$this->internal['searchFieldList'],$table).chr(10);
+                       $WHERE.=$this->cObj->searchWhere($this->piVars['sword'],$this->internal['searchFieldList'],$table).chr(10);
                }
 
                if ($count) {
-                       $query = 'SELECT count(*) '.chr(10).$query;
+                       $queryParts = array(
+                               'SELECT' => 'count(*)',
+                               'FROM' => $TABLENAMES,
+                               'WHERE' => $WHERE,
+                               'GROUPBY' => '',
+                               'ORDERBY' => '',
+                               'LIMIT' => ''
+                       );
                } else {
-                       if ($groupBy)   $query.=' '.$groupBy;
                                // Order by data:
-                       if ($orderBy)   {
-                               $query.=' '.$orderBy;
-                       } else {
+                       if (!$orderBy)  {
                                if (t3lib_div::inList($this->internal['orderByList'],$this->internal['orderBy']))       {
-                                       $query.= ' ORDER BY '.$table.'.'.$this->internal['orderBy'].($this->internal['descFlag']?' DESC':'').chr(10);
+                                       $orderBy = 'ORDER BY '.$table.'.'.$this->internal['orderBy'].($this->internal['descFlag']?' DESC':'');
                                }
                        }
-                       
+
                                // Limit data:
-                       $pointer=$this->piVars['pointer'];
-                       $pointer=intval($pointer);
+                       $pointer = $this->piVars['pointer'];
+                       $pointer = intval($pointer);
                        $results_at_a_time = t3lib_div::intInRange($this->internal['results_at_a_time'],1,1000);
-                       $query.= ' LIMIT '.($pointer*$results_at_a_time).','.$results_at_a_time.chr(10);
-
-                               // Add 'SELECT'                 
-                       $query = 'SELECT '.$this->pi_prependFieldsWithTable($table,$this->pi_listFields).' '.chr(10).$query;
+                       $LIMIT = ($pointer*$results_at_a_time).','.$results_at_a_time;
+
+                               // Add 'SELECT'
+                       $queryParts = array(
+                               'SELECT' => $this->pi_prependFieldsWithTable($table,$this->pi_listFields),
+                               'FROM' => $TABLENAMES,
+                               'WHERE' => $WHERE,
+                               'GROUPBY' => $GLOBALS['TYPO3_DB']->stripGroupBy($groupBy),
+                               'ORDERBY' => $GLOBALS['TYPO3_DB']->stripOrderBy($orderBy),
+                               'LIMIT' => $LIMIT
+                       );
                }
-               return $query;
+
+               $query = $GLOBALS['TYPO3_DB']->SELECTquery (
+                                       $queryParts['SELECT'],
+                                       $queryParts['FROM'],
+                                       $queryParts['WHERE'],
+                                       $queryParts['GROUPBY'],
+                                       $queryParts['ORDERBY'],
+                                       $queryParts['LIMIT']
+                               );
+               return $returnQueryArray ? $queryParts : $query;
+       }
+
+       /**
+        * Executes a standard SELECT 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'])
+        * Set $count to 1 if you wish to get a count(*) query for selecting the number of results.
+        * Notice that the query will use $this->conf['pidList'] and $this->conf['recursive'] to generate a PID list within which to search for records.
+        *
+        * @param       string          The table name to make the query for.
+        * @param       boolean         If set, you will get a "count(*)" query back instead of field selecting
+        * @param       string          Additional WHERE clauses (should be starting with " AND ....")
+        * @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.
+        * @param       string          If set, this is added as a " GROUP BY ...." part of the query.
+        * @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']
+        * @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. May be depreciated in the future!
+        * @return      pointer         SQL result pointer
+        */
+       function pi_exec_query($table,$count=0,$addWhere='',$mm_cat='',$groupBy='',$orderBy='',$query='')       {
+               $queryParts = $this->pi_list_query($table,$count,$addWhere,$mm_cat,$groupBy,$orderBy,$query, TRUE);
+
+               return $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
        }
 
        /**
         * Returns the row $uid from $table
         * (Simply calling $GLOBALS['TSFE']->sys_page->checkRecord())
-        * 
+        *
         * @param       string          The table name
         * @param       integer         The uid of the record from the table
         * @param       boolean         If $checkPage is set, it's required that the page on which the record resides is accessible
         * @return      array           If record is found, an array. Otherwise false.
         */
        function pi_getRecord($table,$uid,$checkPage=0) {
-               return $GLOBALS['TSFE']->sys_page->checkRecord($table,$uid,$checkPage); 
+               return $GLOBALS['TSFE']->sys_page->checkRecord($table,$uid,$checkPage);
        }
-       
+
        /**
         * Returns a commalist of page ids for a query (eg. 'WHERE pid IN (...)')
-        * 
+        *
         * @param       string          $pid_list is a comma list of page ids (if empty current page is used)
         * @param       integer         $recursive is an integer >=0 telling how deep to dig for pids under each entry in $pid_list
         * @return      string          List of PID values (comma separated)
@@ -888,20 +1026,24 @@ class tslib_pibase {
        function pi_getPidList($pid_list,$recursive=0)  {
                if (!strcmp($pid_list,''))      $pid_list = $GLOBALS['TSFE']->id;
                $recursive = t3lib_div::intInRange($recursive,0);
-               
+
                $pid_list_arr = array_unique(t3lib_div::trimExplode(',',$pid_list,1));
-               $pid_list='';
-               reset($pid_list_arr);
-               while(list(,$val)=each($pid_list_arr))  {       
+               $pid_list = array();
+
+               foreach($pid_list_arr as $val)  {
                        $val = t3lib_div::intInRange($val,0);
-                       if ($val)       $pid_list.=$val.','.$this->cObj->getTreeList($val,$recursive);
+                       if ($val)       {
+                               $_list = $this->cObj->getTreeList(-1*$val, $recursive);
+                               if ($_list)             $pid_list[] = $_list;
+                       }
                }
-               return ereg_replace(',$','',$pid_list);
+
+               return implode(',', $pid_list);
        }
-       
+
        /**
         * Having a comma list of fields ($fieldList) this is prepended with the $table.'.' name
-        * 
+        *
         * @param       string          Table name to prepend
         * @param       string          List of fields where each element will be prepended with the table name given.
         * @return      string          List of fields processed.
@@ -917,20 +1059,31 @@ class tslib_pibase {
 
        /**
         * Will select all records from the "category table", $table, and return them in an array.
-        * 
+        *
         * @param       string          The name of the category table to select from.
         * @param       integer         The page from where to select the category records.
-        * @param       string          Additional where clauses (basically the end of the query - could include a ORDER BY clause)
+        * @param       string          Optional additional WHERE clauses put in the end of the query. DO NOT PUT IN GROUP BY, ORDER BY or LIMIT!
+        * @param       string          Optional GROUP BY field(s), if none, supply blank string.
+        * @param       string          Optional ORDER BY field(s), if none, supply blank string.
+        * @param       string          Optional LIMIT value ([begin,]max), if none, supply blank string.
         * @return      array           The array with the category records in.
         */
-       function pi_getCategoryTableContents($table,$pid,$addWhere='')  {
-               $query = 'SELECT * FROM '.$table.' WHERE pid='.intval($pid).$this->cObj->enableFields($table).' '.$addWhere;
-
+       function pi_getCategoryTableContents($table,$pid,$whereClause='',$groupBy='',$orderBy='',$limit='')     {
+               $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
+                                       '*',
+                                       $table,
+                                       'pid='.intval($pid).
+                                               $this->cObj->enableFields($table).' '.
+                                               $whereClause,   // whereClauseMightContainGroupOrderBy
+                                       $groupBy,
+                                       $orderBy,
+                                       $limit
+                               );
                $outArr = array();
-               $res = mysql(TYPO3_db,$query);
-               while($row=mysql_fetch_assoc($res))     {
-                       $outArr[$row['uid']]=$row;
+               while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))       {
+                       $outArr[$row['uid']] = $row;
                }
+               $GLOBALS['TYPO3_DB']->sql_free_result($res);
                return $outArr;
        }
 
@@ -940,21 +1093,21 @@ class tslib_pibase {
 
 
 
-       
+
 
 
 
 
        /***************************
-        * 
+        *
         * Various
         *
         **************************/
-       
+
        /**
         * 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.
         * Notice that this function will only work as long as values are integers.
-        * 
+        *
         * @param       string          List of fields (keys from piVars) to evaluate on
         * @param       integer         Limit for the values.
         * @return      boolean         Returns true (1) if conditions are met.
@@ -969,12 +1122,12 @@ class tslib_pibase {
                }
                if (!count($tempPiVars))        return 1;
        }
-       
+
        /**
         * Returns true if the array $inArray contains only values allowed to be cached based on the configuration in $this->pi_autoCacheFields
         * Used by ->pi_linkTP_keepPIvars
         * This is an advanced form of evaluation of whether a URL should be cached or not.
-        * 
+        *
         * @param       array           An array with piVars values to evaluate
         * @return      boolean         Returns true (1) if conditions are met.
         * @see pi_linkTP_keepPIvars()
@@ -987,7 +1140,7 @@ class tslib_pibase {
                                        unset($inArray[$fN]);
                                } elseif (is_array($this->pi_autoCacheFields[$fN]))     {
                                        if (is_array($this->pi_autoCacheFields[$fN]['range'])
-                                                        && intval($inArray[$fN])>=intval($this->pi_autoCacheFields[$fN]['range'][0]) 
+                                                        && intval($inArray[$fN])>=intval($this->pi_autoCacheFields[$fN]['range'][0])
                                                         && intval($inArray[$fN])<=intval($this->pi_autoCacheFields[$fN]['range'][1]))  {
                                                                unset($inArray[$fN]);
                                        }
@@ -1005,7 +1158,7 @@ class tslib_pibase {
         * 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.
         * This is useful for rendering of content in RTE fields where the transformation mode is set to "ts_css" or so.
         * Notice that this requires the use of "css_styled_content" to work right.
-        * 
+        *
         * @param       string          The input text string to process
         * @return      string          The processed string
         * @see tslib_cObj::parseFunc()
@@ -1015,9 +1168,39 @@ class tslib_pibase {
                if (is_array($parseFunc))       $str = $this->cObj->parseFunc($str, $parseFunc);
                return $str;
        }
-       
+
+
+
+
+
+       /*******************************
+        *
+        * FlexForms related functions
+        *
+        *******************************/
+
+       /**
+        * Converts $this->cObj->data['pi_flexform'] from XML string to flexForm array.
+        *
+        * @return      void
+        */
+       function pi_initPIflexForm()    {
+                       // Converting flexform data into array:
+               if (!is_array($this->cObj->data['pi_flexform']) && $this->cObj->data['pi_flexform'])    {
+                       $this->cObj->data['pi_flexform'] = t3lib_div::xml2array($this->cObj->data['pi_flexform']);
+                       if (!is_array($this->cObj->data['pi_flexform']))        $this->cObj->data['pi_flexform']=array();
+               }
+       }
+
        /**
+        * Return value from somewhere inside a FlexForm structure
         *
+        * @param       array           FlexForm data
+        * @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.
+        * @param       string          Sheet pointer, eg. "sDEF"
+        * @param       string          Language pointer, eg. "lDEF"
+        * @param       string          Value pointer, eg. "vDEF"
+        * @return      string          The content.
         */
        function pi_getFFvalue($T3FlexForm_array,$fieldName,$sheet='sDEF',$lang='lDEF',$value='vDEF')   {
                $sheetArray = $T3FlexForm_array['data'][$sheet][$lang];
@@ -1025,10 +1208,40 @@ class tslib_pibase {
                        return $this->pi_getFFvalueFromSheetArray($sheetArray,explode('/',$fieldName),$value);
                }
        }
+
+       /**
+        * Returns part of $sheetArray pointed to by the keys in $fieldNameArray
+        *
+        * @param       array           Multidimensiona array, typically FlexForm contents
+        * @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...)
+        * @param       string          Value for outermost key, typ. "vDEF" depending on language.
+        * @return      mixed           The value, typ. string.
+        * @access private
+        * @see pi_getFFvalue()
+        */
        function pi_getFFvalueFromSheetArray($sheetArray,$fieldNameArr,$value)  {
-               return $sheetArray[$fieldNameArr[0]][$value];
+
+               $tempArr=$sheetArray;
+               foreach($fieldNameArr as $k => $v)      {
+                       if (t3lib_div::testInt($v))     {
+                               if (is_array($tempArr)) {
+                                       $c=0;
+                                       foreach($tempArr as $values)    {
+                                               if ($c==$v)     {
+                                                       #debug($values);
+                                                       $tempArr=$values;
+                                                       break;
+                                               }
+                                               $c++;
+                                       }
+                               }
+                       } else {
+                               $tempArr = $tempArr[$v];
+                       }
+               }
+               return $tempArr[$value];
        }
 }
 
 // NO extension of class - does not make sense here.
-?>
\ No newline at end of file
+?>