[TYPO3-ect] recursive pid function for tx_div
Steve Ryan
stever at syntithenai.com
Wed Aug 15 12:31:05 CEST 2007
You're right. static declaration can be inside function. tidier. somehow
i had problems when I first tried inside the function.... somehow.
a better tested example closer to div naming.
In a test controller
require_once(t3lib_extMgm::extPath('div') . 'class.tx_div2.php');
and
function defaultAction() {
debug(array('finallist',tx_div2::pidListRecursive($this->parameters->get('pid'),'3')));
}
with the extra file in div extension -> class.tx_div2.php containing
<?
class tx_div2 {
static function findCObjectSingleton() {
static $cObj ;
if(!is_object($cObj)) {
$cObj=t3lib_div::makeInstance('tslib_cObj');
debug(array('creating cobj'));
}
debug(array('returning cobj'));
return $cObj;
}
static function pidListRecursive($pidList,$recursive) {
// ensure array format
if (!is_array($pidList)) {
$pidList=explode(',',$pidList);
}
debug(array($pidList,$recursion));
// dump when recursion reaches 0
if ($recursive==0) {
return $pidList;
} else {
// prep unique array
$unique=array();
foreach ($pidList as $thispid) {
// check for +ve integer
if ($thispid>0) $unique[$thispid]=$thispid;
}
$newTally=0;
if (count($unique)>0) {
debug('next level');
// get next level of pids
$pidQuery='(pid='.implode(' or pid=',array_keys($unique)).')';
$cObj=tx_div2::findCObjectSingleton();
$query=$pidQuery.$cObj->enableFields('pages');
$res=$GLOBALS['TYPO3_DB']->exec_SELECTquery('uid,pid','pages',$query,'','','');
while ($row=$GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
$unique[$row['uid']]=$row['uid'];
$newTally++;
}
// no results, bail on recursion now
if ($newTally>0) {
return tx_div2::pidListRecursive(array_keys($unique),$recursive-1);
} else {
return array_keys($unique);
}
} else {
return array();
}
}
}
}
?>
----------------------
RESULT
0
0 1
1
|next level|
0 creating cobj
0 returning cobj
0
0 1
1 273
2 263
3 262
4 261
5 259
6 252
38 15
39 12
40 5
41 27
1
|next level|
0 returning cobj
0
0 1
1 273
2 263
3 262
4 261
.
.
etc
.
.
.
More information about the TYPO3-team-extension-coordination
mailing list