Initial revision
[Packages/TYPO3.CMS.git] / typo3 / class.db_list.inc
1 <?php
2 /***************************************************************
3 *  Copyright notice
4 *  
5 *  (c) 1999-2003 Kasper Skårhøj (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  * Include file extending t3lib_recordList
29  *
30  * @author      Kasper Skårhøj <kasper@typo3.com>
31  * @package TYPO3
32  * @subpackage core
33  *
34  */
35
36 class recordList extends t3lib_recordList {
37         var $thumbs = 0;                                                // Boolean. Thumbnails on records containing files (pictures)
38         var $itemsLimitPerTable = 20;                   // default Max items shown per table in "multi-table mode", may be overridden by tables.php
39         var $itemsLimitSingleTable = 100;               // default Max items shown per table in "single-table mode", may be overridden by tables.php
40         var $widthGif = '<img src="clear.gif" width=1 height=4 hspace=160>';
41         var $script = "db_list.php";
42         var $spaceSearchBoxFromLeft = 5;
43         var $allFields=0;
44         
45                 // internal
46         var $JScode = "";       
47         var $HTMLcode = "";
48         var $table="";          // set to the tablename if single-table mode
49         var $tableList="";      // Specify a list of tables which are the only ones allowed to be displayed.
50         var $searchString="";
51         var $searchLevels="";
52         var $returnUrl="";
53         var $showLimit=0;
54         var $pidSelect="";
55         var $iLimit=0;          // "LIMIT " in SQL...
56         var $firstElementNumber=0;
57
58         var $eCounter=0;                // Counting the elements no matter what...
59         var $totalItems="";
60         var $recPath_cache=array();
61         var $csvLines=array();
62         var $perms_clause="";
63         var $csvOutput=0;
64         var $setFields=array();
65         var $calcPerms=0;
66         var $currentTable = array();
67
68         var $temp_countQ="";
69         var $duplicateStack=array();
70         
71
72         function start($id,$table,$pointer,$search="",$levels="",$showLimit=0)  {
73                         // Starts the list-generation.
74                         // $id is the parent-id from which we generate the list
75                         // $table is the tablename - if single-table mode
76                 global $TCA;
77                 if ($TCA[$table])       {               // single-table mode
78                         $this->table=$table;    
79                 }
80                 $this->id=intval($id);  // sets the parent id
81
82                 $this->counter=0;       
83                 $this->JScode="";
84                 $this->HTMLcode="";
85                 $this->firstElementNumber=$pointer;
86                 $this->searchString=trim($search);
87                 $this->searchLevels=trim($levels);
88                 $this->showLimit=t3lib_div::intInRange($showLimit,0,10000);
89                 $this->csvOutput = t3lib_div::GPvar("csv") ? 1 : 0;
90                 $this->sortField = t3lib_div::GPvar("sortField");
91                 $this->sortRev = t3lib_div::GPvar("sortRev");
92                 
93                 if (!$GLOBALS["TYPO3_CONF_VARS"]["GFX"]["thumbnails"])  {
94                         $this->thumbScript='gfx/notfound_thumb.gif';
95                 }
96                 
97                         // Set select levels:
98                 $sL=intval($this->searchLevels);
99                 $this->perms_clause = $GLOBALS["BE_USER"]->getPagePermsClause(1);
100                 if ($sL>0)      {
101                         $tree = $this->getTreeObject($id,$sL,$this->perms_clause);
102                         $this->pidSelect = "pid IN (".implode($tree->ids,",").")";
103                 } else {
104                         $this->pidSelect = "pid=".intval($id);
105                 }
106         }
107         function setDispFields()        {
108                         // Display fields:
109                 $dispFields = $GLOBALS["BE_USER"]->getModuleData("db_list.php/displayFields");
110                 $dispFields_in = t3lib_div::GPvar("displayFields");
111                 if (is_array($dispFields_in))   {
112                         reset($dispFields_in);
113                         $tKey = key($dispFields_in);
114                         $dispFields[$tKey]=$dispFields_in[$tKey];
115                         $GLOBALS["BE_USER"]->pushModuleData("db_list.php/displayFields",$dispFields);
116                 }
117                 $this->setFields=$dispFields;
118         }
119         function generateList() {
120                         // For each table we wish to show, this->getTable is called.
121                         // Finishes off with a stopper-gif
122                 global $TCA;
123                 reset($TCA);
124                 $tableCount = 0;
125                 while (list($key)=each($TCA))   {
126                         t3lib_div::loadTCA($key);
127                         $val=$TCA[$key];
128                         if ((!$this->table || $key==$this->table) && (!$this->tableList || t3lib_div::inList($this->tableList,$key)) && $GLOBALS["BE_USER"]->check("tables_select",$key))       {               // Checks that we see only permitted/requested tables:
129                                 if ($this->table)       {               // iLimit is set depending on whether we're in single- or multi-table mode
130                                         $this->iLimit=(isset($val["interface"]["maxSingleDBListItems"])?intval($val["interface"]["maxSingleDBListItems"]):$this->itemsLimitSingleTable);
131                                 } else {
132                                         $this->iLimit=(isset($val["interface"]["maxDBListItems"])?intval($val["interface"]["maxDBListItems"]):$this->itemsLimitPerTable);
133                                 }
134                                 if ($this->showLimit)   $this->iLimit = $this->showLimit;
135 //                              $this->iLimit=3;
136                                 $tableCount++;
137
138                                 if ($this->allFields)   {
139                                         $fields = $this->makeFieldList($key);
140                                         $fields[]="_PATH_";
141                                         $fields[]="_CONTROL_";
142                                         if (is_array($this->setFields[$key]))   {
143                                                 $fields = array_intersect($fields,$this->setFields[$key]);
144                                         } else {
145                                                 $fields = array();
146                                         }
147                                 } else {
148                                         $fields = array();
149                                 }
150                                 $this->HTMLcode.=$this->getTable($key, $this->id,implode(",",$fields));
151                         }
152                 }
153         }
154         function thumbCode($row,$table,$field)  {
155                 return t3lib_BEfunc::thumbCode($row,$table,$field,$this->backPath,$this->thumbScript);
156         }
157         function makeQuery($table, $id, $addWhere="",$fieldList="*")    {
158                         // returns the SQL-query to select the records from a table $table with pid = $id
159                         // Depends on the global var $perms_clause to exist!!
160                 global $TCA,$perms_clause;
161                 $orderBy = ($TCA[$table]["ctrl"]["sortby"]) ? "ORDER BY ".$TCA[$table]["ctrl"]["sortby"] : $TCA[$table]["ctrl"]["default_sortby"];
162                 if ($this->sortField)   {
163                         if (in_array($this->sortField,$this->makeFieldList($table,1)))  {
164                                 $orderBy = "ORDER BY ".$this->sortField;
165                                 if ($this->sortRev)     $orderBy.=" DESC";
166                         }
167                 }
168                 
169                 $limit = ($this->iLimit) ? "LIMIT ".($this->firstElementNumber ? ($this->firstElementNumber)."," : "" ).($this->iLimit+1) : "";
170                 $pC = ($table=="pages" && $perms_clause)?" AND ".$perms_clause:"";
171                 $search = $this->makeSearchString($table);
172
173                 $query = sprintf("SELECT ".$fieldList." FROM %s WHERE ".$this->pidSelect." %s %s %s %s %s %s",
174                                 $table, $pC, t3lib_BEfunc::deleteClause($table), $addWhere, $search, $orderBy, $limit);
175                 $this->temp_countQ = sprintf("SELECT count(*) FROM %s WHERE ".$this->pidSelect." %s %s %s %s",
176                                 $table, $pC, t3lib_BEfunc::deleteClause($table), $addWhere, $search);
177 //              debug($query,1);
178                 return $query;
179         }
180         function setTotalItems()        {
181                 $result = mysql(TYPO3_db,$this->temp_countQ);
182                 if (mysql_error())      {
183                         echo "<HR>
184                         <strong>MySQL error:</strong> ".mysql_error()."<BR>
185                         <strong>Query:</strong> ".$this->temp_countQ."<BR>
186                         <HR>
187                         ";
188                 }
189                 list($rCount) = mysql_fetch_row($result);
190                 $this->totalItems = $rCount;
191         }
192         function makeSearchString($table)       {
193                 global $TCA;
194                 if ($TCA[$table] && $this->searchString)        {
195                         t3lib_div::loadTCA($table);
196
197                         $sfields=array();
198                         $sfields[]="uid";
199
200                         $columns = $TCA[$table]["columns"];
201                         reset($columns);
202                         while(list($fieldName,$info)=each($columns))    {
203                                 $type = $info["config"]["type"];
204                                 if ($type=="text" || ($type=="input" && !ereg("date|time|int",$info["config"]["eval"])))        {
205                                         $sfields[]=$fieldName;
206                                 }
207                         }
208                         $like=" LIKE '%".addslashes($this->searchString)."%'";
209                         if (count($sfields))    {
210                                 $queryPart = " AND (".implode($like." OR ",$sfields).$like.")";
211                                 return $queryPart;
212                         }
213                 }
214         }
215         function linkWrapTable($table,$code)    {
216                         // Returns the title (based on $code) of a table ($table) with the proper link around
217                 if ($this->table!=$table)       {
218                         return '<A HREF="'.$this->listURL("",$table).'">'.$code.'</a>';
219                 } else {
220                         return '<A HREF="'.$this->listURL("","","sortField,sortRev,table").'">'.$code.'</a>';
221                 }
222         }
223         function linkWrapItems($table,$uid,$code,$row)  {
224                         // Returns the title (based on $code) of a record (from table $table) with the proper link around (that is for "pages"-records a link to the level of that record...)
225                 if (!strcmp($code,"")) {$code="<i>[".$GLOBALS["LANG"]->sL("LLL:EXT:lang/locallang_core.php:labels.no_title")."]</i> - ".t3lib_BEfunc::getRecordTitle($table,$row);}
226                 $code=t3lib_div::fixed_lgd("&nbsp;".$code,$this->fixedL);
227                 if ($table=="pages")    {
228                         return '<A HREF="'.$this->listURL($uid).'">'.$code.'</a>';
229                 } else {
230                         return $code;
231                 }
232         }
233         function listURL($altId="",$table=-1,$exclList="")      {
234                 return $this->script.
235                         '?id='.(strcmp($altId,"")?$altId:$this->id).
236                         '&table='.rawurlencode($table==-1?$this->table:$table).
237                         ($this->thumbs?'&imagemode='.$this->thumbs:'').
238                         ($this->returnUrl?"&returnUrl=".rawurlencode($this->returnUrl):"").
239                         ($this->searchString?"&search_field=".rawurlencode($this->searchString):"").
240                         ($this->searchLevels?"&search_levels=".rawurlencode($this->searchLevels):"").
241                         ($this->showLimit?"&showLimit=".rawurlencode($this->showLimit):"").
242                         ((!$exclList || !t3lib_div::inList($exclList,"sortField")) && $this->sortField?"&sortField=".rawurlencode($this->sortField):"").
243                         ((!$exclList || !t3lib_div::inList($exclList,"sortRev")) && $this->sortRev?"&sortRev=".rawurlencode($this->sortRev):"")
244                         ;
245         }
246         function requestUri()   {
247                 return $this->listURL();
248         }
249         function getSearchBox($formFields=1)    {
250                 if ($GLOBALS["CLIENT"]["BROWSER"]=="net")       {
251 //                      $content.= '<img src=clear.gif width=1 height=100><BR>';
252                 }
253                 $formElements=array("","");
254                 if ($formFields)        {
255                         $formElements=array('<form action="'.$this->listURL().'" method="POST">','</form>');
256                 }
257                         // Make level selector:
258                 $opt=array();
259                 $parts = explode("|",$GLOBALS["LANG"]->sL("LLL:EXT:lang/locallang_core.php:labels.enterSearchLevels"));
260                 while(list($kv,$label)=each($parts))    {
261                         $opt[] = '<option value="'.$kv.'"'.($kv==intval($this->searchLevels)?" selected":"").'>'.htmlspecialchars($label).'</option>';
262                 }
263                 $lMenu = '<select name="search_levels">'.implode("",$opt).'</select>';
264                 
265                         // Table with the search box:
266                 $content.= '
267                 <table border=0 cellpadding=1 cellspacing=0>
268                 '.$formElements[0].'
269                         <tr>
270                                 <td><img src=clear.gif width='.$this->spaceSearchBoxFromLeft.' height=1></td>
271                                 <td bgcolor="#9BA1A8">
272                                         <table border=0 cellpadding=0 cellspacing=0 bgcolor="'.$GLOBALS["TBE_TEMPLATE"]->bgColor4.'">
273                                         <tr>
274                                                 <td nowrap>&nbsp;'.$GLOBALS["LANG"]->sL("LLL:EXT:lang/locallang_core.php:labels.enterSearchString").'&nbsp;&nbsp;<input type="Text" name="search_field" value="'.htmlspecialchars($this->searchString).'"'.$GLOBALS["TBE_TEMPLATE"]->formWidth(10).'></td>
275                                                 <td>'.$lMenu.'</td>
276                                                 <td><input type="Submit" name="search" value="'.$GLOBALS["LANG"]->sL("LLL:EXT:lang/locallang_core.php:labels.search").'"></td>
277                                         </tr>
278                                         <tr>
279                                                 <td nowrap colspan=3>&nbsp;'.$GLOBALS["LANG"]->sL("LLL:EXT:lang/locallang_core.php:labels.showRecords").':&nbsp;&nbsp;<input type="text" name="showLimit" value="'.($this->showLimit?$this->showLimit:"").'"'.$GLOBALS["SOBE"]->doc->formWidth(4).'></td>
280                                         </tr>
281                                         </table>                        
282                                 </td>
283                         </tr>'.$formElements[1].'
284                 </table>
285                 ';
286                 return $content;
287         }
288         function makeFieldList($table,$dontCheckUser=0) {
289                 global $TCA,$BE_USER;
290                 $fieldListArr = array();
291                 if (is_array($TCA[$table]))     {
292                         t3lib_div::loadTCA($table);
293                         reset($TCA[$table]["columns"]);
294                         while(list($fN,$fieldValue)=each($TCA[$table]["columns"]))      {
295                                 if ($dontCheckUser || 
296                                         ((!$fieldValue["exclude"] || $BE_USER->check("non_exclude_fields",$table.":".$fN)) && $fieldValue["config"]["type"]!="passthrough"))    {
297                                         $fieldListArr[]=$fN;
298                                 }
299                         }
300                         if ($dontCheckUser || $BE_USER->isAdmin())      {
301                                 $fieldListArr[]="uid";
302                                 $fieldListArr[]="pid";
303                                 if ($TCA[$table]["ctrl"]["tstamp"])     $fieldListArr[]=$TCA[$table]["ctrl"]["tstamp"];
304                                 if ($TCA[$table]["ctrl"]["crdate"])     $fieldListArr[]=$TCA[$table]["ctrl"]["crdate"];
305                                 if ($TCA[$table]["ctrl"]["cruser_id"])  $fieldListArr[]=$TCA[$table]["ctrl"]["cruser_id"];
306                                 if ($TCA[$table]["ctrl"]["sortby"])     $fieldListArr[]=$TCA[$table]["ctrl"]["sortby"];
307                         }
308                 }
309                 return $fieldListArr;
310         }
311         function getTreeObject($id,$depth,$perms_clause)        {
312                 $tree = t3lib_div::makeInstance("t3lib_pageTree");
313                 $tree->init("AND ".$perms_clause);
314                 $tree->makeHTML=0;
315                 $tree->fieldArray = Array("uid","php_tree_stop");
316                 if ($depth)     {
317                         $tree->getTree($id, $depth, "");
318                 }
319                 $tree->ids[]=$id;
320 //              debug($tree->ids);
321                 return $tree;
322         }
323         function showSysNotesForPage()  {
324                 global $TCA;
325
326                 if (!t3lib_extMgm::isLoaded("sys_note"))        return "";
327
328                 $delClause = t3lib_BEfunc::deleteClause("sys_note");
329                 $query = "SELECT * FROM sys_note where pid in (".$this->id.") AND (!personal OR cruser='".$GLOBALS["BE_USER"]->user["uid"]."')".$delClause;
330                 $result = mysql(TYPO3_db,$query);
331                 if (mysql_error()) {echo mysql_error(); debug($query);}
332                 $dbCount = mysql_num_rows($result);
333                 if ($dbCount)   {
334                         $cat = array();
335                         t3lib_div::loadTCA("sys_note");
336                         if ($TCA["sys_note"] && $TCA["sys_note"]["columns"]["category"] && is_array($TCA["sys_note"]["columns"]["category"]["config"]["items"]))        {
337                                 reset($TCA["sys_note"]["columns"]["category"]["config"]["items"]);
338                                 while(list(,$el)=each($TCA["sys_note"]["columns"]["category"]["config"]["items"]))      {
339                                         $cat[$el[1]]=$GLOBALS["LANG"]->sL($el[0]);
340                                 }
341                         }
342                         while($row=mysql_fetch_assoc($result))  {
343                                 $icon= t3lib_iconWorks::getIcon("sys_note",$row);
344                                 $fields=array();
345                                 $subject=$row["subject"];
346
347                                 $fields["Author:"]=$row["author"].($row["email"] && $row["author"] ? ", ":"").$row["email"];
348                                 $fields["Category:"]=$cat[$row["category"]];
349                                 $fields["Note:"]=nl2br($row[message]);
350                                 $bgcol=' bgcolor="'.$GLOBALS["SOBE"]->doc->bgColor2.'"';
351                                 $bgcol2=' bgcolor="'.$GLOBALS["SOBE"]->doc->bgColor4.'"';
352                                 
353                                 $out.='
354                                 <table border=0 cellpadding=1 cellspacing=1 width=1>
355                                         <tr><td colspan=2'.$bgcol.'><img src="'.$icon.'" width=18 height=16 align=top><strong>'.$subject.'</strong></td></tr>
356                                         <tr><td'.$bgcol2.'>'.$GLOBALS["LANG"]->sL("LLL:EXT:lang/locallang_core.php:labels.category").'</td><td'.$bgcol2.'>'.$fields["Category:"].'</td></tr>
357                                         <tr><td'.$bgcol2.'>'.$GLOBALS["LANG"]->sL("LLL:EXT:lang/locallang_core.php:labels.author").'</td><td'.$bgcol2.'>'.$fields["Author:"].'</td></tr>
358                                         <tr><td'.$bgcol2.'>'.$GLOBALS["LANG"]->sL("LLL:EXT:lang/locallang_core.php:labels.note").'</td><td'.$bgcol2.'>'.$fields["Note:"].'</td></tr>
359                                         <tr><td colspan=2><img src=clear.gif width=380 height=1></td></tr>
360                                 </table>
361                                 ';
362 //                              debug($fields);
363                         }
364                 }
365                 return $out ? "".$out : "";
366         }
367 }
368
369
370 if (defined("TYPO3_MODE") && $TYPO3_CONF_VARS[TYPO3_MODE]["XCLASS"]["typo3/class.db_list.inc"]) {
371         include_once($TYPO3_CONF_VARS[TYPO3_MODE]["XCLASS"]["typo3/class.db_list.inc"]);
372 }
373
374 ?>