Documentation TYPO3 par Ameos |
00001 <?php 00002 /*************************************************************** 00003 * Copyright notice 00004 * 00005 * (c) 1999-2004 Kasper Skaarhoj (kasperYYYY@typo3.com) 00006 * All rights reserved 00007 * 00008 * This script is part of the TYPO3 project. The TYPO3 project is 00009 * free software; you can redistribute it and/or modify 00010 * it under the terms of the GNU General Public License as published by 00011 * the Free Software Foundation; either version 2 of the License, or 00012 * (at your option) any later version. 00013 * 00014 * The GNU General Public License can be found at 00015 * http://www.gnu.org/copyleft/gpl.html. 00016 * A copy is found in the textfile GPL.txt and important notices to the license 00017 * from the author is found in LICENSE.txt distributed with these scripts. 00018 * 00019 * 00020 * This script is distributed in the hope that it will be useful, 00021 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00022 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00023 * GNU General Public License for more details. 00024 * 00025 * This copyright notice MUST APPEAR in all copies of the script! 00026 ***************************************************************/ 00072 class t3lib_loadDBGroup { 00073 // External, static: 00074 var $fromTC = 1; // Means that only uid and the label-field is returned 00075 var $registerNonTableValues=0; // If set, values that are not ids in tables are normally discarded. By this options they will be preserved. 00076 00077 // Internal, dynamic: 00078 var $tableArray=Array(); // Contains the table names as keys. The values are the id-values for each table. Should ONLY contain proper table names. 00079 var $itemArray=Array(); // Contains items in an numeric array (table/id for each). Tablenames here might be "_NO_TABLE" 00080 var $nonTableArray=array(); // Array for NON-table elements 00081 var $additionalWhere=array(); 00082 var $checkIfDeleted = 1; // deleted-column is added to additionalWhere... if this is set... 00083 var $dbPaths=Array(); 00084 var $firstTable = ''; // Will contain the first table name in the $tablelist (for positive ids) 00085 var $secondTable = ''; // Will contain the second table name in the $tablelist (for negative ids) 00086 00087 00088 00089 00099 function start($itemlist,$tablelist, $MMtable='',$MMuid=0) { 00100 // If the table list is "*" then all tables are used in the list: 00101 if (!strcmp(trim($tablelist),'*')) { 00102 $tablelist = implode(',',array_keys($GLOBALS['TCA'])); 00103 } 00104 00105 // The tables are traversed and internal arrays are initialized: 00106 $tempTableArray = t3lib_div::trimExplode(',',$tablelist,1); 00107 foreach($tempTableArray as $key => $val) { 00108 $tName = trim($val); 00109 $this->tableArray[$tName] = Array(); 00110 if ($this->checkIfDeleted && $GLOBALS['TCA'][$tName]['ctrl']['delete']) { 00111 $fieldN = $tName.'.'.$GLOBALS['TCA'][$tName]['ctrl']['delete']; 00112 $this->additionalWhere[$tName].=' AND NOT '.$fieldN; 00113 } 00114 } 00115 00116 if (is_array($this->tableArray)) { 00117 reset($this->tableArray); 00118 } else {return 'No tables!';} 00119 00120 // Set first and second tables: 00121 $this->firstTable = key($this->tableArray); // Is the first table 00122 next($this->tableArray); 00123 $this->secondTable = key($this->tableArray); // If the second table is set and the ID number is less than zero (later) then the record is regarded to come from the second table... 00124 00125 // Now, populate the internal itemArray and tableArray arrays: 00126 if ($MMtable) { // If MM, then call this function to do that: 00127 $this->readMM($MMtable,$MMuid); 00128 } else { 00129 // If not MM, then explode the itemlist by "," and traverse the list: 00130 $this->readList($itemlist); 00131 } 00132 } 00133 00140 function readList($itemlist) { 00141 if ((string)trim($itemlist)!='') { 00142 $tempItemArray = t3lib_div::trimExplode(',', $itemlist); // Changed to trimExplode 31/3 04; HMENU special type "list" didn't work if there were spaces in the list... I suppose this is better overall... 00143 foreach($tempItemArray as $key => $val) { 00144 $isSet = 0; // Will be set to "1" if the entry was a real table/id: 00145 00146 // Extract table name and id. This is un the formular [tablename]_[id] where table name MIGHT contain "_", hence the reversion of the string! 00147 $val = strrev($val); 00148 $parts = explode('_',$val,2); 00149 $theID = strrev($parts[0]); 00150 00151 // Check that the id IS an integer: 00152 if (t3lib_div::testInt($theID)) { 00153 // Get the table name: If a part of the exploded string, use that. Otherwise if the id number is LESS than zero, use the second table, otherwise the first table 00154 $theTable = trim($parts[1]) ? strrev(trim($parts[1])) : ($this->secondTable && $theID<0 ? $this->secondTable : $this->firstTable); 00155 // If the ID is not blank and the table name is among the names in the inputted tableList, then proceed: 00156 if ((string)$theID!='' && $theID && $theTable && isset($this->tableArray[$theTable])) { 00157 // Get ID as the right value: 00158 $theID = $this->secondTable ? abs(intval($theID)) : intval($theID); 00159 // Register ID/table name in internal arrays: 00160 $this->itemArray[$key]['id'] = $theID; 00161 $this->itemArray[$key]['table'] = $theTable; 00162 $this->tableArray[$theTable][] = $theID; 00163 // Set update-flag: 00164 $isSet=1; 00165 } 00166 } 00167 00168 // If it turns out that the value from the list was NOT a valid reference to a table-record, then we might still set it as a NO_TABLE value: 00169 if (!$isSet && $this->registerNonTableValues) { 00170 $this->itemArray[$key]['id'] = $tempItemArray[$key]; 00171 $this->itemArray[$key]['table'] = '_NO_TABLE'; 00172 $this->nonTableArray[] = $tempItemArray[$key]; 00173 } 00174 } 00175 } 00176 } 00177 00186 function readMM($tableName,$uid) { 00187 $key=0; 00188 00189 // Select all MM relations: 00190 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', $tableName, 'uid_local='.intval($uid), '', 'sorting'); 00191 while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) { 00192 $theTable = $row['tablenames'] ? $row['tablenames'] : $this->firstTable; // If tablesnames columns exists and contain a name, then this value is the table, else it's the the firstTable... 00193 if (($row['uid_foreign'] || $theTable=='pages') && $theTable && isset($this->tableArray[$theTable])) { 00194 $this->itemArray[$key]['id'] = $row['uid_foreign']; 00195 $this->itemArray[$key]['table'] = $theTable; 00196 $this->tableArray[$theTable][]= $row['uid_foreign']; 00197 } elseif ($this->registerNonTableValues) { 00198 $this->itemArray[$key]['id'] = $row['uid_foreign']; 00199 $this->itemArray[$key]['table'] = '_NO_TABLE'; 00200 $this->nonTableArray[] = $row['uid_foreign']; 00201 } 00202 $key++; 00203 } 00204 $GLOBALS['TYPO3_DB']->sql_free_result($res); 00205 } 00206 00215 function writeMM($tableName,$uid,$prependTableName=0) { 00216 00217 // Delete all relations: 00218 $GLOBALS['TYPO3_DB']->exec_DELETEquery($tableName, 'uid_local='.intval($uid)); 00219 00220 // If there are tables... 00221 $tableC = count($this->tableArray); 00222 if ($tableC) { 00223 $prep = ($tableC>1||$prependTableName) ? 1 : 0; 00224 $c=0; 00225 $tName=array(); 00226 00227 // For each item, insert it: 00228 foreach($this->itemArray as $val) { 00229 $c++; 00230 00231 $insertFields = array( 00232 'uid_local' => $uid, 00233 'uid_foreign' => $val['id'], 00234 'sorting' => $c 00235 ); 00236 if ($prep || $val['table']=='_NO_TABLE') { 00237 $insertFields['tablenames'] = $val['table']; 00238 } 00239 00240 $GLOBALS['TYPO3_DB']->exec_INSERTquery($tableName, $insertFields); 00241 } 00242 } 00243 } 00244 00251 function getValueArray($prependTableName='') { 00252 // INIT: 00253 $valueArray=Array(); 00254 $tableC = count($this->tableArray); 00255 00256 // If there are tables in the table array: 00257 if ($tableC) { 00258 // If there are more than ONE table in the table array, then always prepend table names: 00259 $prep = ($tableC>1||$prependTableName) ? 1 : 0; 00260 00261 // Traverse the array of items: 00262 foreach($this->itemArray as $val) { 00263 $valueArray[]=(($prep && $val['table']!='_NO_TABLE') ? $val['table'].'_' : ''). 00264 $val['id']; 00265 } 00266 } 00267 // Return the array 00268 return $valueArray; 00269 } 00270 00279 function convertPosNeg($valueArray,$fTable,$nfTable) { 00280 if (is_array($valueArray) && $fTable) { 00281 foreach($valueArray as $key => $val) { 00282 $val = strrev($val); 00283 $parts = explode('_',$val,2); 00284 $theID = strrev($parts[0]); 00285 $theTable = strrev($parts[1]); 00286 00287 if ( t3lib_div::testInt($theID) && (!$theTable || !strcmp($theTable,$fTable) || !strcmp($theTable,$nfTable)) ) { 00288 $valueArray[$key]= $theTable && strcmp($theTable,$fTable) ? $theID*-1 : $theID; 00289 } 00290 } 00291 } 00292 return $valueArray; 00293 } 00294 00301 function getFromDB() { 00302 // Traverses the tables listed: 00303 foreach($this->tableArray as $key => $val) { 00304 if (is_array($val)) { 00305 $itemList = implode(',',$val); 00306 if ($itemList) { 00307 $from = '*'; 00308 if ($this->fromTC) { 00309 $from = 'uid,pid'; 00310 if ($GLOBALS['TCA'][$key]['ctrl']['label']) { 00311 $from.= ','.$GLOBALS['TCA'][$key]['ctrl']['label']; // Titel 00312 } 00313 if ($GLOBALS['TCA'][$key]['ctrl']['thumbnail']) { 00314 $from.= ','.$GLOBALS['TCA'][$key]['ctrl']['thumbnail']; // Thumbnail 00315 } 00316 } 00317 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery($from, $key, 'uid IN ('.$itemList.')'.$this->additionalWhere[$key]); 00318 while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) { 00319 $this->results[$key][$row['uid']]=$row; 00320 } 00321 } 00322 } 00323 } 00324 return $this->results; 00325 } 00326 00333 function readyForInterface() { 00334 global $TCA; 00335 00336 if (!is_array($this->itemArray)) {return false;} 00337 00338 $output=array(); 00339 $perms_clause = $GLOBALS['BE_USER']->getPagePermsClause(1); // For use when getting the paths.... 00340 $titleLen=intval($GLOBALS['BE_USER']->uc['titleLen']); 00341 00342 foreach($this->itemArray as $key => $val) { 00343 $theRow = $this->results[$val['table']][$val['id']]; 00344 if ($theRow && is_array($TCA[$val['table']])) { 00345 $label = t3lib_div::fixed_lgd_cs(strip_tags($theRow[$TCA[$val['table']]['ctrl']['label']]),$titleLen); 00346 $label = ($label)?$label:'[...]'; 00347 $output[]=str_replace(',','',$val['table'].'_'.$val['id'].'|'.rawurlencode($label)); 00348 } 00349 } 00350 return implode(',',$output); 00351 } 00352 } 00353 00354 00355 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_loaddbgroup.php']) { 00356 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_loaddbgroup.php']); 00357 } 00358 ?>