00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00076 class t3lib_loadDBGroup {
00077
00078 var $fromTC = 1;
00079 var $registerNonTableValues=0;
00080
00081
00082 var $tableArray=Array();
00083 var $itemArray=Array();
00084 var $nonTableArray=array();
00085 var $additionalWhere=array();
00086 var $checkIfDeleted = 1;
00087 var $dbPaths=Array();
00088 var $firstTable = '';
00089 var $secondTable = '';
00090
00091 var $MM_is_foreign = 0;
00092 var $MM_oppositeField = '';
00093 var $MM_oppositeTable = '';
00094 var $MM_oppositeFieldConf = '';
00095 var $MM_isMultiTableRelationship = 0;
00096 var $currentTable;
00097 var $undeleteRecord;
00098
00099
00100 var $MM_match_fields = array();
00101 var $MM_insert_fields = array();
00102 var $MM_table_where = '';
00103
00104
00116 function start($itemlist, $tablelist, $MMtable='', $MMuid=0, $currentTable='', $conf=array()) {
00117
00118 $this->MM_is_foreign = ($conf['MM_opposite_field']?1:0);
00119 $this->MM_oppositeField = $conf['MM_opposite_field'];
00120 $this->MM_table_where = $conf['MM_table_where'];
00121 $this->MM_match_fields = is_array($conf['MM_match_fields']) ? $conf['MM_match_fields'] : array();
00122 $this->MM_insert_fields = is_array($conf['MM_insert_fields']) ? $conf['MM_insert_fields'] : $this->MM_match_fields;
00123
00124 $this->currentTable = $currentTable;
00125 if ($this->MM_is_foreign) {
00126 $tmp = ($conf['type']==='group'?$conf['allowed']:$conf['foreign_table']);
00127
00128 $tmp = t3lib_div::trimExplode(',', $tmp);
00129 $this->MM_oppositeTable = $tmp[0];
00130 unset($tmp);
00131
00132
00133 $this->MM_oppositeFieldConf = $GLOBALS['TCA'][$this->MM_oppositeTable]['columns'][$this->MM_oppositeField]['config'];
00134
00135 if ($this->MM_oppositeFieldConf['allowed']) {
00136 $oppositeFieldConf_allowed = explode(',', $this->MM_oppositeFieldConf['allowed']);
00137 if (count($oppositeFieldConf_allowed) > 1) {
00138 $this->MM_isMultiTableRelationship = $oppositeFieldConf_allowed[0];
00139 }
00140 }
00141 }
00142
00143
00144
00145
00146 if (!strcmp(trim($tablelist),'*')) {
00147 $tablelist = implode(',',array_keys($GLOBALS['TCA']));
00148 }
00149
00150
00151 $tempTableArray = t3lib_div::trimExplode(',',$tablelist,1);
00152 foreach($tempTableArray as $key => $val) {
00153 $tName = trim($val);
00154 $this->tableArray[$tName] = Array();
00155 if ($this->checkIfDeleted && $GLOBALS['TCA'][$tName]['ctrl']['delete']) {
00156 $fieldN = $tName.'.'.$GLOBALS['TCA'][$tName]['ctrl']['delete'];
00157 $this->additionalWhere[$tName].=' AND '.$fieldN.'=0';
00158 }
00159 }
00160
00161 if (is_array($this->tableArray)) {
00162 reset($this->tableArray);
00163 } else {return 'No tables!';}
00164
00165
00166 $this->firstTable = key($this->tableArray);
00167 next($this->tableArray);
00168 $this->secondTable = key($this->tableArray);
00169
00170
00171 if ($MMtable) {
00172 $this->readMM($MMtable,$MMuid);
00173 } elseif ($MMuid && $conf['foreign_field']) {
00174
00175 $this->readForeignField($MMuid, $conf);
00176 } else {
00177
00178 $this->readList($itemlist);
00179
00180 if ($conf['foreign_default_sortby']) {
00181 $this->sortList($conf['foreign_default_sortby']);
00182 }
00183 }
00184 }
00185
00192 function readList($itemlist) {
00193 if ((string)trim($itemlist)!='') {
00194 $tempItemArray = t3lib_div::trimExplode(',', $itemlist);
00195 foreach($tempItemArray as $key => $val) {
00196 $isSet = 0;
00197
00198
00199 $val = strrev($val);
00200 $parts = explode('_',$val,2);
00201 $theID = strrev($parts[0]);
00202
00203
00204 if (t3lib_div::testInt($theID)) {
00205
00206 $theTable = trim($parts[1]) ? strrev(trim($parts[1])) : ($this->secondTable && $theID<0 ? $this->secondTable : $this->firstTable);
00207
00208 if ((string)$theID!='' && $theID && $theTable && isset($this->tableArray[$theTable])) {
00209
00210 $theID = $this->secondTable ? abs(intval($theID)) : intval($theID);
00211
00212 $this->itemArray[$key]['id'] = $theID;
00213 $this->itemArray[$key]['table'] = $theTable;
00214 $this->tableArray[$theTable][] = $theID;
00215
00216 $isSet=1;
00217 }
00218 }
00219
00220
00221 if (!$isSet && $this->registerNonTableValues) {
00222 $this->itemArray[$key]['id'] = $tempItemArray[$key];
00223 $this->itemArray[$key]['table'] = '_NO_TABLE';
00224 $this->nonTableArray[] = $tempItemArray[$key];
00225 }
00226 }
00227 }
00228 }
00229
00238 function sortList($sortby) {
00239
00240 if ($sortby == 'uid') {
00241 usort($this->itemArray, create_function('$a,$b', 'return $a["id"] < $b["id"] ? -1 : 1;'));
00242
00243 } elseif (count($this->tableArray) == 1) {
00244 reset($this->tableArray);
00245 $table = key($this->tableArray);
00246 $uidList = implode(',', current($this->tableArray));
00247
00248 if ($uidList) {
00249 $this->itemArray = array();
00250 $this->tableArray = array();
00251
00252 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid', $table, 'uid IN ('.$uidList.')', '', $sortby);
00253 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
00254 $this->itemArray[] = array('id' => $row['uid'], 'table' => $table);
00255 $this->tableArray[$table][] = $row['uid'];
00256 }
00257 $GLOBALS['TYPO3_DB']->sql_free_result($res);
00258 }
00259 }
00260 }
00261
00270 function readMM($tableName,$uid) {
00271 $key=0;
00272 $additionalWhere = '';
00273
00274 if ($this->MM_is_foreign) {
00275 $uidLocal_field = 'uid_foreign';
00276 $uidForeign_field = 'uid_local';
00277 $sorting_field = 'sorting_foreign';
00278
00279 if ($this->MM_isMultiTableRelationship) {
00280 $additionalWhere .= ' AND ( tablenames="'.$this->currentTable.'"';
00281 if ($this->currentTable == $this->MM_isMultiTableRelationship) {
00282 $additionalWhere .= ' OR tablenames=""';
00283 }
00284 $additionalWhere .= ' ) ';
00285 }
00286 $theTable = $this->MM_oppositeTable;
00287 } else {
00288 $uidLocal_field = 'uid_local';
00289 $uidForeign_field = 'uid_foreign';
00290 $sorting_field = 'sorting';
00291 }
00292
00293
00294 if ($this->MM_table_where) {
00295 $additionalWhere.= "\n".str_replace('###THIS_UID###', intval($uid), $this->MM_table_where);
00296 }
00297 foreach ($this->MM_match_fields as $field => $value) {
00298 $additionalWhere.= ' AND '.$field.'='.$GLOBALS['TYPO3_DB']->fullQuoteStr($value, $tableName);
00299 }
00300
00301
00302 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', $tableName, $uidLocal_field.'='.intval($uid).$additionalWhere, '', $sorting_field);
00303 while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
00304 if (!$this->MM_is_foreign) {
00305 $theTable = $row['tablenames'] ? $row['tablenames'] : $this->firstTable;
00306 }
00307 if (($row[$uidForeign_field] || $theTable=='pages') && $theTable && isset($this->tableArray[$theTable])) {
00308
00309 $this->itemArray[$key]['id'] = $row[$uidForeign_field];
00310 $this->itemArray[$key]['table'] = $theTable;
00311 $this->tableArray[$theTable][]= $row[$uidForeign_field];
00312 } elseif ($this->registerNonTableValues) {
00313 $this->itemArray[$key]['id'] = $row[$uidForeign_field];
00314 $this->itemArray[$key]['table'] = '_NO_TABLE';
00315 $this->nonTableArray[] = $row[$uidForeign_field];
00316 }
00317 $key++;
00318 }
00319 $GLOBALS['TYPO3_DB']->sql_free_result($res);
00320 }
00321
00330 function writeMM($tableName,$uid,$prependTableName=0) {
00331
00332 if ($this->MM_is_foreign) {
00333 $uidLocal_field = 'uid_foreign';
00334 $uidForeign_field = 'uid_local';
00335 $sorting_field = 'sorting_foreign';
00336 } else {
00337 $uidLocal_field = 'uid_local';
00338 $uidForeign_field = 'uid_foreign';
00339 $sorting_field = 'sorting';
00340 }
00341
00342
00343 $tableC = count($this->tableArray);
00344 if ($tableC) {
00345 $prep = ($tableC>1||$prependTableName||$this->MM_isMultiTableRelationship) ? 1 : 0;
00346 $c=0;
00347
00348 $additionalWhere_tablenames = '';
00349 if ($this->MM_is_foreign && $prep) {
00350 $additionalWhere_tablenames = ' AND tablenames="'.$this->currentTable.'"';
00351 }
00352
00353 $additionalWhere = '';
00354
00355 if ($this->MM_table_where) {
00356 $additionalWhere.= "\n".str_replace('###THIS_UID###', intval($uid), $this->MM_table_where);
00357 }
00358
00359 foreach ($this->MM_match_fields as $field => $value) {
00360 $additionalWhere.= ' AND '.$field.'='.$GLOBALS['TYPO3_DB']->fullQuoteStr($value, $tableName);
00361 }
00362
00363 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery($uidForeign_field.($prep?', tablenames':''), $tableName, $uidLocal_field.'='.$uid.$additionalWhere_tablenames.$additionalWhere);
00364
00365 $oldMMs = array();
00366 while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
00367 if (!$this->MM_is_foreign && $prep) {
00368 $oldMMs[] = array($row['tablenames'], $row[$uidForeign_field]);
00369 } else {
00370 $oldMMs[] = $row[$uidForeign_field];
00371 }
00372 }
00373
00374
00375 foreach($this->itemArray as $val) {
00376 $c++;
00377
00378 if ($prep || $val['table']=='_NO_TABLE') {
00379 if ($this->MM_is_foreign) {
00380 $tablename = $this->currentTable;
00381 } else {
00382 $tablename = $val['table'];
00383 }
00384 } else {
00385 $tablename = '';
00386 }
00387
00388 if(!$this->MM_is_foreign && $prep) {
00389 $item = array($val['table'], $val['id']);
00390 } else {
00391 $item = $val['id'];
00392 }
00393
00394 if (in_array($item, $oldMMs)) {
00395 unset($oldMMs[array_search($item, $oldMMs)]);
00396
00397 $whereClause = $uidLocal_field.'='.$uid.' AND '.$uidForeign_field.'='.$val['id'];
00398 if ($tablename) {
00399 $whereClause .= ' AND tablenames="'.$tablename.'"';
00400 }
00401 $GLOBALS['TYPO3_DB']->exec_UPDATEquery($tableName, $whereClause.$additionalWhere, array($sorting_field => $c));
00402 } else {
00403
00404 $insertFields = $this->MM_insert_fields;
00405 $insertFields[$uidLocal_field] = $uid;
00406 $insertFields[$uidForeign_field] = $val['id'];
00407 $insertFields[$sorting_field] = $c;
00408 if($tablename) {
00409 $insertFields['tablenames'] = $tablename;
00410 }
00411
00412 $GLOBALS['TYPO3_DB']->exec_INSERTquery($tableName, $insertFields);
00413 }
00414 }
00415
00416
00417 if(is_array($oldMMs) && count($oldMMs) > 0) {
00418 $removeClauses = array();
00419 foreach($oldMMs as $mmItem) {
00420 if(is_array($mmItem)) {
00421 $removeClauses[] = 'tablenames="'.$mmItem[0].'" AND '.$uidForeign_field.'='.$mmItem[1];
00422 } else {
00423 $removeClauses[] = $uidForeign_field.'='.$mmItem;
00424 }
00425 }
00426 $deleteAddWhere = ' AND ('.implode(' OR ', $removeClauses).')';
00427 $GLOBALS['TYPO3_DB']->exec_DELETEquery($tableName, $uidLocal_field.'='.intval($uid).$deleteAddWhere.$additionalWhere_tablenames.$additionalWhere);
00428 }
00429 }
00430 }
00431
00440 function readForeignField($uid, $conf) {
00441 $key = 0;
00442 $uid = intval($uid);
00443 $whereClause = '';
00444 $foreign_table = $conf['foreign_table'];
00445 $foreign_table_field = $conf['foreign_table_field'];
00446 $useDeleteClause = $this->undeleteRecord ? false : true;
00447
00448
00449 if ($conf['symmetric_field']) {
00450 $whereClause = '('.$conf['foreign_field'].'='.$uid.' OR '.$conf['symmetric_field'].'='.$uid.')';
00451 } else {
00452 $whereClause = $conf['foreign_field'].'='.$uid;
00453 }
00454
00455 if ($useDeleteClause) {
00456 $whereClause .= t3lib_BEfunc::deleteClause($foreign_table);
00457 }
00458
00459
00460 if ($foreign_table_field && $this->currentTable) {
00461 $whereClause .= ' AND '.$foreign_table_field.'='.$GLOBALS['TYPO3_DB']->fullQuoteStr($this->currentTable, $foreign_table);
00462 }
00463
00464
00465 if ($conf['foreign_sortby']) {
00466 if ($conf['symmetric_sortby'] && $conf['symmetric_field']) {
00467
00468 $sortby = '
00469 CASE
00470 WHEN '.$conf['foreign_field'].'='.$uid.'
00471 THEN '.$conf['foreign_sortby'].'
00472 ELSE '.$conf['symmetric_sortby'].'
00473 END';
00474 } else {
00475
00476 $sortby = $conf['foreign_sortby'];
00477 }
00478 } elseif ($conf['foreign_default_sortby']) {
00479 $sortby = $conf['foreign_default_sortby'];
00480 } elseif ($GLOBALS['TCA'][$foreign_table]['ctrl']['sortby']) {
00481 $sortby = $GLOBALS['TCA'][$foreign_table]['ctrl']['sortby'];
00482 } elseif ($GLOBALS['TCA'][$foreign_table]['ctrl']['default_sortby']) {
00483 $sortby = $GLOBALS['TCA'][$foreign_table]['ctrl']['default_sortby'];
00484 }
00485
00486
00487 $sortby = $GLOBALS['TYPO3_DB']->stripOrderBy($sortby);
00488
00489 $rows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('uid', $foreign_table, $whereClause, '', $sortby);
00490
00491 if (count($rows)) {
00492 foreach ($rows as $row) {
00493 $this->itemArray[$key]['id'] = $row['uid'];
00494 $this->itemArray[$key]['table'] = $foreign_table;
00495 $this->tableArray[$foreign_table][]= $row['uid'];
00496 $key++;
00497 }
00498 }
00499 }
00500
00510 function writeForeignField($conf, $parentUid, $updateToUid=0, $skipSorting=false) {
00511 $c = 0;
00512 $foreign_table = $conf['foreign_table'];
00513 $foreign_field = $conf['foreign_field'];
00514 $symmetric_field = $conf['symmetric_field'];
00515 $foreign_table_field = $conf['foreign_table_field'];
00516
00517
00518 if (t3lib_div::testInt($parentUid) && count($this->tableArray)) {
00519
00520 if (!(t3lib_div::testInt($updateToUid) && $updateToUid > 0)) $updateToUid = 0;
00521 $fields = 'uid,'.$foreign_field.($symmetric_field ? ','.$symmetric_field : '');
00522
00523
00524 foreach ($this->itemArray as $val) {
00525 $uid = $val['id'];
00526 $table = $val['table'];
00527
00528
00529 if ($conf['symmetric_field']) {
00530 $row = t3lib_BEfunc::getRecord($table,$uid,$fields,'',false);
00531 $isOnSymmetricSide = t3lib_loadDBGroup::isOnSymmetricSide($parentUid, $conf, $row);
00532 }
00533
00534 $updateValues = array();
00535
00536
00537
00538 if (!$updateToUid) {
00539
00540 if ($isOnSymmetricSide) {
00541 $updateValues[$symmetric_field] = $parentUid;
00542 } else {
00543 $updateValues[$foreign_field] = $parentUid;
00544 }
00545
00546
00547 if ($foreign_table_field && $this->currentTable) {
00548 $updateValues[$foreign_table_field] = $this->currentTable;
00549 }
00550
00551
00552 if (!$skipSorting) {
00553
00554 if ($conf['foreign_sortby']) {
00555 $sortby = $conf['foreign_sortby'];
00556 } elseif ($GLOBALS['TCA'][$foreign_table]['ctrl']['sortby']) {
00557 $sortby = $GLOBALS['TCA'][$foreign_table]['ctrl']['sortby'];
00558 }
00559
00560 $sortby = $GLOBALS['TYPO3_DB']->stripOrderBy($sortby);
00561 $symSortby = $conf['symmetric_sortby'];
00562
00563
00564 if ($isOnSymmetricSide && $symSortby) {
00565 $updateValues[$symSortby] = ++$c;
00566 } elseif ($sortby) {
00567 $updateValues[$sortby] = ++$c;
00568 }
00569 }
00570
00571
00572
00573 } else {
00574 if ($isOnSymmetricSide) {
00575 $updateValues[$symmetric_field] = $updateToUid;
00576 } else {
00577 $updateValues[$foreign_field] = $updateToUid;
00578 }
00579 }
00580
00581 if (count($updateValues)) {
00582 $GLOBALS['TYPO3_DB']->exec_UPDATEquery($table, "uid='$uid'", $updateValues);
00583 $this->updateRefIndex($table, $uid);
00584 }
00585 }
00586 }
00587 }
00588
00595 function getValueArray($prependTableName='') {
00596
00597 $valueArray=Array();
00598 $tableC = count($this->tableArray);
00599
00600
00601 if ($tableC) {
00602
00603 $prep = ($tableC>1||$prependTableName) ? 1 : 0;
00604
00605
00606 foreach($this->itemArray as $val) {
00607 $valueArray[]=(($prep && $val['table']!='_NO_TABLE') ? $val['table'].'_' : '').
00608 $val['id'];
00609 }
00610 }
00611
00612 return $valueArray;
00613 }
00614
00623 function convertPosNeg($valueArray,$fTable,$nfTable) {
00624 if (is_array($valueArray) && $fTable) {
00625 foreach($valueArray as $key => $val) {
00626 $val = strrev($val);
00627 $parts = explode('_',$val,2);
00628 $theID = strrev($parts[0]);
00629 $theTable = strrev($parts[1]);
00630
00631 if ( t3lib_div::testInt($theID) && (!$theTable || !strcmp($theTable,$fTable) || !strcmp($theTable,$nfTable)) ) {
00632 $valueArray[$key]= $theTable && strcmp($theTable,$fTable) ? $theID*-1 : $theID;
00633 }
00634 }
00635 }
00636 return $valueArray;
00637 }
00638
00645 function getFromDB() {
00646
00647 foreach($this->tableArray as $key => $val) {
00648 if (is_array($val)) {
00649 $itemList = implode(',',$val);
00650 if ($itemList) {
00651 $from = '*';
00652 if ($this->fromTC) {
00653 $from = 'uid,pid';
00654 if ($GLOBALS['TCA'][$key]['ctrl']['label']) {
00655 $from.= ','.$GLOBALS['TCA'][$key]['ctrl']['label'];
00656 }
00657 if ($GLOBALS['TCA'][$key]['ctrl']['label_alt']) {
00658 $from.= ','.$GLOBALS['TCA'][$key]['ctrl']['label_alt'];
00659 }
00660 if ($GLOBALS['TCA'][$key]['ctrl']['thumbnail']) {
00661 $from.= ','.$GLOBALS['TCA'][$key]['ctrl']['thumbnail'];
00662 }
00663 }
00664 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery($from, $key, 'uid IN ('.$itemList.')'.$this->additionalWhere[$key]);
00665 while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
00666 $this->results[$key][$row['uid']]=$row;
00667 }
00668 }
00669 }
00670 }
00671 return $this->results;
00672 }
00673
00680 function readyForInterface() {
00681 global $TCA;
00682
00683 if (!is_array($this->itemArray)) {return false;}
00684
00685 $output=array();
00686 $perms_clause = $GLOBALS['BE_USER']->getPagePermsClause(1);
00687 $titleLen=intval($GLOBALS['BE_USER']->uc['titleLen']);
00688
00689 foreach($this->itemArray as $key => $val) {
00690 $theRow = $this->results[$val['table']][$val['id']];
00691 if ($theRow && is_array($TCA[$val['table']])) {
00692 $label = t3lib_div::fixed_lgd_cs(strip_tags(t3lib_BEfunc::getRecordTitle($val['table'], $theRow)),$titleLen);
00693 $label = ($label)?$label:'[...]';
00694 $output[]=str_replace(',','',$val['table'].'_'.$val['id'].'|'.rawurlencode($label));
00695 }
00696 }
00697 return implode(',',$output);
00698 }
00699
00706 function countItems($returnAsArray = true) {
00707 $count = count($this->itemArray);
00708 if ($returnAsArray) $count = array($count);
00709 return $count;
00710 }
00711
00721 function updateRefIndex($table,$id) {
00722 $refIndexObj = t3lib_div::makeInstance('t3lib_refindex');
00723 $result = $refIndexObj->updateRefIndexTable($table,$id);
00724 }
00725
00734 function isOnSymmetricSide($parentUid, $parentConf, $childRec) {
00735 return t3lib_div::testInt($childRec['uid']) && $parentConf['symmetric_field'] && $parentUid == $childRec[$parentConf['symmetric_field']]
00736 ? true
00737 : false;
00738 }
00739 }
00740
00741
00742 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_loaddbgroup.php']) {
00743 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_loaddbgroup.php']);
00744 }
00745 ?>